51ee1e77 by astaxie

beego: close the file when finish init

1 parent 986e91b7
...@@ -29,7 +29,6 @@ var ( ...@@ -29,7 +29,6 @@ var (
29 29
30 // File session store 30 // File session store
31 type FileSessionStore struct { 31 type FileSessionStore struct {
32 f *os.File
33 sid string 32 sid string
34 lock sync.RWMutex 33 lock sync.RWMutex
35 values map[interface{}]interface{} 34 values map[interface{}]interface{}
...@@ -77,14 +76,23 @@ func (fs *FileSessionStore) SessionID() string { ...@@ -77,14 +76,23 @@ func (fs *FileSessionStore) SessionID() string {
77 76
78 // Write file session to local file with Gob string 77 // Write file session to local file with Gob string
79 func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) { 78 func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
80 defer fs.f.Close()
81 b, err := EncodeGob(fs.values) 79 b, err := EncodeGob(fs.values)
82 if err != nil { 80 if err != nil {
83 return 81 return
84 } 82 }
85 fs.f.Truncate(0) 83 _, err = os.Stat(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
86 fs.f.Seek(0, 0) 84 var f *os.File
87 fs.f.Write(b) 85 if err == nil {
86 f, err = os.OpenFile(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0777)
87 } else if os.IsNotExist(err) {
88 f, err = os.Create(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
89 } else {
90 return
91 }
92 f.Truncate(0)
93 f.Seek(0, 0)
94 f.Write(b)
95 f.Close()
88 } 96 }
89 97
90 // File session provider 98 // File session provider
...@@ -137,8 +145,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) { ...@@ -137,8 +145,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
137 } 145 }
138 } 146 }
139 f.Close() 147 f.Close()
140 f, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_WRONLY|os.O_CREATE, 0777) 148 ss := &FileSessionStore{sid: sid, values: kv}
141 ss := &FileSessionStore{f: f, sid: sid, values: kv}
142 return ss, nil 149 return ss, nil
143 } 150 }
144 151
...@@ -235,9 +242,7 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, err ...@@ -235,9 +242,7 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, err
235 return nil, err 242 return nil, err
236 } 243 }
237 } 244 }
238 245 ss := &FileSessionStore{sid: sid, values: kv}
239 newf, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_WRONLY|os.O_CREATE, 0777)
240 ss := &FileSessionStore{f: newf, sid: sid, values: kv}
241 return ss, nil 246 return ss, nil
242 } 247 }
243 248
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!