Skip to content

Commit

Permalink
临时修复:spf13/afero#428
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Jun 22, 2024
1 parent 3bd4ace commit 042f83d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion modules/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func ListBackupFiles(fsys fs.FS, dir string) ([]*proto.BackupFileSpec, error) {
// NOTE:安全写:先写临时文件,再移动过去。临时文件写在目标目录,不存在跨设备移动文件的问题。
// NOTE:如果 r 超过 size,会报错。
func WriteFile(fs afero.Fs, path string, mode fs.FileMode, modified time.Time, size int64, r io.Reader) error {
tmp, err := afero.TempFile(fs, filepath.Dir(path), `taoblog-*`)
tmp, err := afero.TempFile(fs, `.`, `taoblog-*`)
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions service/modules/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"io/fs"
fspkg "io/fs"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -86,7 +85,8 @@ func (fs *Storage) Sub(dir string) (fspkg.FS, error) {
if err != nil {
return nil, err
}
return &_FsOpen{afero.NewBasePathFs(fs.root, path)}, nil
// TODO: afero 有个 bug:https://github.com/spf13/afero/issues/428
return &Storage{root: afero.NewBasePathFs(fs.root, "/"+path)}, nil
}

// 会自动创建不存在的目录。
Expand Down Expand Up @@ -115,13 +115,20 @@ func (fs *Storage) Stat(name string) (fspkg.FileInfo, error) {
if err != nil {
return nil, err
}
return os.Stat(path)
return fs.root.Stat(path)
}

func (fs *Storage) ReadDir(name string) ([]fspkg.DirEntry, error) {
path, err := fs.pathOf(name)
if err != nil {
return nil, err
}
return os.ReadDir(path)
fi, err := afero.ReadDir(fs.root, path)
if err != nil {
return nil, err
}

return utils.Map(fi, func(i fspkg.FileInfo) fspkg.DirEntry {
return fspkg.FileInfoToDirEntry(i)
}), nil
}

0 comments on commit 042f83d

Please sign in to comment.