1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-02 09:53:27 +00:00

beego: close the file when finish init

This commit is contained in:
astaxie 2014-08-18 14:42:17 +08:00
parent 986e91b7d6
commit 51ee1e77c2

View File

@ -29,7 +29,6 @@ var (
// File session store // File session store
type FileSessionStore struct { type FileSessionStore struct {
f *os.File
sid string sid string
lock sync.RWMutex lock sync.RWMutex
values map[interface{}]interface{} values map[interface{}]interface{}
@ -77,14 +76,23 @@ func (fs *FileSessionStore) SessionID() string {
// Write file session to local file with Gob string // Write file session to local file with Gob string
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) { func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
defer fs.f.Close()
b, err := EncodeGob(fs.values) b, err := EncodeGob(fs.values)
if err != nil { if err != nil {
return return
} }
fs.f.Truncate(0) _, err = os.Stat(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
fs.f.Seek(0, 0) var f *os.File
fs.f.Write(b) if err == nil {
f, err = os.OpenFile(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0777)
} else if os.IsNotExist(err) {
f, err = os.Create(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
} else {
return
}
f.Truncate(0)
f.Seek(0, 0)
f.Write(b)
f.Close()
} }
// File session provider // File session provider
@ -137,8 +145,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
} }
} }
f.Close() f.Close()
f, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_WRONLY|os.O_CREATE, 0777) ss := &FileSessionStore{sid: sid, values: kv}
ss := &FileSessionStore{f: f, sid: sid, values: kv}
return ss, nil return ss, nil
} }
@ -235,9 +242,7 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, err
return nil, err return nil, err
} }
} }
ss := &FileSessionStore{sid: sid, values: kv}
newf, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_WRONLY|os.O_CREATE, 0777)
ss := &FileSessionStore{f: newf, sid: sid, values: kv}
return ss, nil return ss, nil
} }