/
filesystem.go
161 lines (134 loc) · 3.35 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2012 Lightpoke. All rights reserved.
// This source code is subject to the terms and
// conditions defined in the "License.txt" file.
// Package filesystem implements file-based session storage.
package filesystem
import (
"github.com/sinni800/organics"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
)
func stringSections(s string, n int) []string {
sections := make([]string, 0)
for len(s) > n {
sections = append(sections, s[:n])
s = s[n:]
}
sections = append(sections, s)
return sections
}
func sessionKeyToPath(key string) string {
keyPath := url.QueryEscape(key)
sections := stringSections(keyPath, 10)
return filepath.Join(sections...)
}
type provider struct {
access sync.RWMutex
stores map[string]*organics.Store
fileWritingLocks map[string]*sync.Mutex
directory string
}
func (p *provider) setStore(key string, store *organics.Store) {
p.access.Lock()
defer p.access.Unlock()
p.stores[key] = store
}
func (p *provider) store(key string) (store *organics.Store, ok bool) {
p.access.RLock()
defer p.access.RUnlock()
store, ok = p.stores[key]
return
}
func (p *provider) getWriteLock(key string) *sync.Mutex {
p.access.Lock()
defer p.access.Unlock()
lock, ok := p.fileWritingLocks[key]
if !ok {
lock = new(sync.Mutex)
p.fileWritingLocks[key] = lock
}
return lock
}
func (p *provider) Save(key string, whatChanged string, s *organics.Store) error {
p.setStore(key, s)
lock := p.getWriteLock(key)
lock.Lock()
defer lock.Unlock()
keyPath := filepath.Join(p.directory, sessionKeyToPath(key))
if _, err := os.Stat(keyPath); err != nil {
if os.IsNotExist(err) {
keyPathDir := strings.Split(keyPath, string(os.PathSeparator))
keyPathDir = keyPathDir[:len(keyPathDir)-1]
dirPath := filepath.Join(keyPathDir...)
err = os.MkdirAll(dirPath, os.ModeDir)
if err != nil {
return fmt.Errorf("Error saving session %v", err)
}
} else {
return fmt.Errorf("Error saving session %v", err)
}
}
data, err := s.GobEncode()
if err != nil {
return fmt.Errorf("Error saving session %v", err)
}
err = ioutil.WriteFile(keyPath, data, 0666)
if err != nil {
return fmt.Errorf("Error saving session %v", err)
}
return nil
}
func (p *provider) Load(key string) *organics.Store {
store, ok := p.store(key)
if ok {
// We have it in memory already
return store
}
// We don't have it in memory, so load it from file.
keyPath := filepath.Join(p.directory, sessionKeyToPath(key))
file, err := os.Open(keyPath)
if err != nil {
if os.IsNotExist(err) {
return nil
} else {
log.Println("Error reading session file", err)
return nil
}
}
store = organics.NewStore()
data, err := ioutil.ReadAll(file)
if err != nil {
log.Println("Error reading session file", err)
return nil
}
err = store.GobDecode(data)
if err != nil {
log.Println("Error decoding session file", err)
return nil
}
p.stores[key] = store
return store
}
func Provider(directory string) (organics.SessionProvider, error) {
p := new(provider)
p.stores = make(map[string]*organics.Store)
p.fileWritingLocks = make(map[string]*sync.Mutex)
p.directory = directory
if _, err := os.Stat(directory); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(directory, os.ModeDir)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
return p, nil
}