use filepath.Join() to build file paths (#5617)

* use filepath.Join() to build file paths

* use filepath.Join() to build file paths
This commit is contained in:
Seiya 2024-03-31 22:26:35 +09:00 committed by GitHub
parent 97066459ed
commit 0609076950
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 14 deletions

View File

@ -42,7 +42,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path" "path/filepath"
"strings" "strings"
"time" "time"
@ -626,7 +626,7 @@ func (b *BeegoHTTPRequest) ToFile(filename string) error {
// Check if the file directory exists. If it doesn't then it's created // Check if the file directory exists. If it doesn't then it's created
func pathExistAndMkdir(filename string) (err error) { func pathExistAndMkdir(filename string) (err error) {
filename = path.Dir(filename) filename = filepath.Dir(filename)
_, err = os.Stat(filename) _, err = os.Stat(filename)
if err == nil { if err == nil {
return nil return nil

View File

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -226,7 +225,7 @@ func (w *fileLogWriter) createLogFile() (*os.File, error) {
return nil, err return nil, err
} }
filepath := path.Dir(w.Filename) filepath := filepath.Dir(w.Filename)
os.MkdirAll(filepath, os.FileMode(dirperm)) os.MkdirAll(filepath, os.FileMode(dirperm))
fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm)) fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm))

View File

@ -21,7 +21,6 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -88,16 +87,16 @@ func (fs *FileSessionStore) SessionRelease(ctx context.Context, w http.ResponseW
SLogger.Println(err) SLogger.Println(err)
return return
} }
_, err = os.Stat(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid)) _, err = os.Stat(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
var f *os.File var f *os.File
if err == nil { if err == nil {
f, err = os.OpenFile(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0o777) f, err = os.OpenFile(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0o777)
if err != nil { if err != nil {
SLogger.Println(err) SLogger.Println(err)
return return
} }
} else if os.IsNotExist(err) { } else if os.IsNotExist(err) {
f, err = os.Create(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid)) f, err = os.Create(filepath.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
if err != nil { if err != nil {
SLogger.Println(err) SLogger.Println(err)
return return
@ -195,7 +194,7 @@ func (fp *FileProvider) SessionExist(ctx context.Context, sid string) (bool, err
return false, errors.New("min length of session id is 2") return false, errors.New("min length of session id is 2")
} }
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid)) _, err := os.Stat(filepath.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
return err == nil, nil return err == nil, nil
} }
@ -203,7 +202,7 @@ func (fp *FileProvider) SessionExist(ctx context.Context, sid string) (bool, err
func (fp *FileProvider) SessionDestroy(ctx context.Context, sid string) error { func (fp *FileProvider) SessionDestroy(ctx context.Context, sid string) error {
filepder.lock.Lock() filepder.lock.Lock()
defer filepder.lock.Unlock() defer filepder.lock.Unlock()
os.Remove(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid)) os.Remove(filepath.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
return nil return nil
} }
@ -234,10 +233,10 @@ func (fp *FileProvider) SessionRegenerate(ctx context.Context, oldsid, sid strin
filepder.lock.Lock() filepder.lock.Lock()
defer filepder.lock.Unlock() defer filepder.lock.Unlock()
oldPath := path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])) oldPath := filepath.Join(fp.savePath, string(oldsid[0]), string(oldsid[1]))
oldSidFile := path.Join(oldPath, oldsid) oldSidFile := filepath.Join(oldPath, oldsid)
newPath := path.Join(fp.savePath, string(sid[0]), string(sid[1])) newPath := filepath.Join(fp.savePath, string(sid[0]), string(sid[1]))
newSidFile := path.Join(newPath, sid) newSidFile := filepath.Join(newPath, sid)
// new sid file is exist // new sid file is exist
_, err := os.Stat(newSidFile) _, err := os.Stat(newSidFile)