mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 17:10:55 +00:00
1.add "defer f.close()" in SessionRead to fix file handle leak if DecodeGob failed
2.rewrite SessionRegenerate
This commit is contained in:
parent
dade92d98b
commit
872a964145
@ -15,8 +15,7 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -135,6 +134,9 @@ func (fp *FileProvider) SessionRead(sid string) (Store, error) {
|
|||||||
} else {
|
} else {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
os.Chtimes(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), time.Now(), time.Now())
|
os.Chtimes(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), time.Now(), time.Now())
|
||||||
var kv map[interface{}]interface{}
|
var kv map[interface{}]interface{}
|
||||||
b, err := ioutil.ReadAll(f)
|
b, err := ioutil.ReadAll(f)
|
||||||
@ -149,7 +151,7 @@ func (fp *FileProvider) SessionRead(sid string) (Store, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.Close()
|
|
||||||
ss := &FileSessionStore{sid: sid, values: kv}
|
ss := &FileSessionStore{sid: sid, values: kv}
|
||||||
return ss, nil
|
return ss, nil
|
||||||
}
|
}
|
||||||
@ -204,49 +206,58 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
|
|||||||
filepder.lock.Lock()
|
filepder.lock.Lock()
|
||||||
defer filepder.lock.Unlock()
|
defer filepder.lock.Unlock()
|
||||||
|
|
||||||
err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777)
|
oldPath := path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1]))
|
||||||
if err != nil {
|
oldSidFile := path.Join(oldPath, oldsid)
|
||||||
SLogger.Println(err.Error())
|
newPath := path.Join(fp.savePath, string(sid[0]), string(sid[1]))
|
||||||
}
|
newSidFile := path.Join(newPath, sid)
|
||||||
err = os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
|
|
||||||
if err != nil {
|
// new sid file is exist
|
||||||
SLogger.Println(err.Error())
|
_, err := os.Stat(newSidFile)
|
||||||
}
|
|
||||||
_, err = os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
||||||
var newf *os.File
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil, errors.New("newsid exist")
|
return nil, fmt.Errorf("newsid %s exist", newSidFile)
|
||||||
} else if os.IsNotExist(err) {
|
|
||||||
newf, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1]), oldsid))
|
err = os.MkdirAll(newPath, 0777)
|
||||||
var f *os.File
|
|
||||||
if err == nil {
|
|
||||||
f, err = os.OpenFile(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1]), oldsid), os.O_RDWR, 0777)
|
|
||||||
io.Copy(newf, f)
|
|
||||||
} else if os.IsNotExist(err) {
|
|
||||||
newf, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
os.Remove(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])))
|
|
||||||
os.Chtimes(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), time.Now(), time.Now())
|
|
||||||
var kv map[interface{}]interface{}
|
|
||||||
b, err := ioutil.ReadAll(newf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
SLogger.Println(err.Error())
|
||||||
}
|
}
|
||||||
if len(b) == 0 {
|
|
||||||
kv = make(map[interface{}]interface{})
|
// if old sid file exist
|
||||||
} else {
|
// 1.read and parse file content
|
||||||
kv, err = DecodeGob(b)
|
// 2.write content to new sid file
|
||||||
|
// 3.remove old sid file, change new sid file atime and ctime
|
||||||
|
// 4.return FileSessionStore
|
||||||
|
_, err = os.Stat(oldSidFile)
|
||||||
|
if err == nil {
|
||||||
|
b, err := ioutil.ReadFile(oldSidFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var kv map[interface{}]interface{}
|
||||||
|
if len(b) == 0 {
|
||||||
|
kv = make(map[interface{}]interface{})
|
||||||
|
} else {
|
||||||
|
kv, err = DecodeGob(b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ioutil.WriteFile(newSidFile, b, 0777)
|
||||||
|
os.Remove(oldSidFile)
|
||||||
|
os.Chtimes(newSidFile, time.Now(), time.Now())
|
||||||
|
ss := &FileSessionStore{sid: sid, values: kv}
|
||||||
|
return ss, nil
|
||||||
}
|
}
|
||||||
ss := &FileSessionStore{sid: sid, values: kv}
|
|
||||||
|
// if old sid file not exist, just create new sid file and return
|
||||||
|
newf, err := os.Create(newSidFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
newf.Close()
|
||||||
|
ss := &FileSessionStore{sid: sid, values: make(map[interface{}]interface{})}
|
||||||
return ss, nil
|
return ss, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user