Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
872a9641
Commit
872a9641
authored
Feb 09, 2017
by
liming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.add "defer f.close()" in SessionRead to fix file handle leak if DecodeGob failed
2.rewrite SessionRegenerate
parent
dade92d9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
36 deletions
+47
-36
sess_file.go
session/sess_file.go
+47
-36
No files found.
session/sess_file.go
View file @
872a9641
...
...
@@ -15,8 +15,7 @@
package
session
import
(
"errors"
"io"
"fmt"
"io/ioutil"
"net/http"
"os"
...
...
@@ -135,6 +134,9 @@ func (fp *FileProvider) SessionRead(sid string) (Store, error) {
}
else
{
return
nil
,
err
}
defer
f
.
Close
()
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
(
f
)
...
...
@@ -149,7 +151,7 @@ func (fp *FileProvider) SessionRead(sid string) (Store, error) {
return
nil
,
err
}
}
f
.
Close
()
ss
:=
&
FileSessionStore
{
sid
:
sid
,
values
:
kv
}
return
ss
,
nil
}
...
...
@@ -204,49 +206,58 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
filepder
.
lock
.
Lock
()
defer
filepder
.
lock
.
Unlock
()
err
:=
os
.
MkdirAll
(
path
.
Join
(
fp
.
savePath
,
string
(
oldsid
[
0
]),
string
(
oldsid
[
1
])),
0777
)
if
err
!=
nil
{
SLogger
.
Println
(
err
.
Error
())
oldPath
:=
path
.
Join
(
fp
.
savePath
,
string
(
oldsid
[
0
]),
string
(
oldsid
[
1
]))
oldSidFile
:=
path
.
Join
(
oldPath
,
oldsid
)
newPath
:=
path
.
Join
(
fp
.
savePath
,
string
(
sid
[
0
]),
string
(
sid
[
1
]))
newSidFile
:=
path
.
Join
(
newPath
,
sid
)
// new sid file is exist
_
,
err
:=
os
.
Stat
(
newSidFile
)
if
err
==
nil
{
return
nil
,
fmt
.
Errorf
(
"newsid %s exist"
,
newSidFile
)
}
err
=
os
.
MkdirAll
(
path
.
Join
(
fp
.
savePath
,
string
(
sid
[
0
]),
string
(
sid
[
1
])),
0777
)
err
=
os
.
MkdirAll
(
newPath
,
0777
)
if
err
!=
nil
{
SLogger
.
Println
(
err
.
Error
())
}
_
,
err
=
os
.
Stat
(
path
.
Join
(
fp
.
savePath
,
string
(
sid
[
0
]),
string
(
sid
[
1
]),
sid
))
var
newf
*
os
.
File
if
err
==
nil
{
return
nil
,
errors
.
New
(
"newsid exist"
)
}
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
))
var
f
*
os
.
File
// if old sid file exist
// 1.read and parse file content
// 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
{
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
{
return
nil
,
err
}
if
len
(
b
)
==
0
{
kv
=
make
(
map
[
interface
{}]
interface
{})
}
else
{
kv
,
err
=
DecodeGob
(
b
)
b
,
err
:=
ioutil
.
ReadFile
(
oldSidFile
)
if
err
!=
nil
{
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
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment