diff options
author | Dmitry Chestnykh <dmitry@codingrobots.com> | 2015-03-26 00:35:41 +0300 |
---|---|---|
committer | Dmitry Chestnykh <dmitry@codingrobots.com> | 2015-03-26 00:35:41 +0300 |
commit | 4b0af472301b1081c4821c65875c9f50824ebd3a (patch) | |
tree | 433cbe6b4eecbcb7de4bb860f9504b52ee1d12f8 /store.go | |
parent | 1f45645972a051deb63c8e8e3c594041a6145e35 (diff) |
Simplify FilesystemStore save and load.
Use ioutil package to write and read files.
Diffstat (limited to 'store.go')
-rw-r--r-- | store.go | 28 |
1 files changed, 3 insertions, 25 deletions
@@ -6,7 +6,7 @@ package sessions import ( "encoding/base32" - "io" + "io/ioutil" "net/http" "os" "strings" @@ -218,38 +218,16 @@ func (s *FilesystemStore) save(session *Session) error { filename := s.path + "session_" + session.ID fileMutex.Lock() defer fileMutex.Unlock() - fp, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - return err - } - defer fp.Close() - if _, err = fp.Write([]byte(encoded)); err != nil { - return err - } - return nil + return ioutil.WriteFile(filename, []byte(encoded), 0600) } // load reads a file and decodes its content into session.Values. func (s *FilesystemStore) load(session *Session) error { filename := s.path + "session_" + session.ID - fp, err := os.OpenFile(filename, os.O_RDONLY, 0400) + fdata, err := ioutil.ReadFile(filename) if err != nil { return err } - defer fp.Close() - var fdata []byte - buf := make([]byte, 128) - for { - var n int - n, err = fp.Read(buf[0:]) - fdata = append(fdata, buf[0:n]...) - if err != nil { - if err == io.EOF { - break - } - return err - } - } if err = securecookie.DecodeMulti(session.Name(), string(fdata), &session.Values, s.Codecs...); err != nil { return err |