mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-05 13:37:38 +03:00
Replace godirwalk
with standard Go 1.16 filepath.WalkDir
Should fix https://github.com/navidrome/navidrome/issues/1048
This commit is contained in:
parent
10cfaad95c
commit
d1605dcfbe
3 changed files with 35 additions and 31 deletions
63
utils/cache/spread_fs.go
vendored
63
utils/cache/spread_fs.go
vendored
|
@ -3,12 +3,13 @@ package cache
|
|||
import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/djherbis/fscache"
|
||||
"github.com/karrick/godirwalk"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"gopkg.in/djherbis/atime.v1"
|
||||
"gopkg.in/djherbis/stream.v1"
|
||||
)
|
||||
|
@ -36,45 +37,51 @@ func NewSpreadFS(dir string, mode os.FileMode) (*spreadFS, error) {
|
|||
return fs, fs.init()
|
||||
}
|
||||
|
||||
func (fs *spreadFS) Reload(f func(key string, name string)) error {
|
||||
return godirwalk.Walk(fs.root, &godirwalk.Options{
|
||||
Callback: func(absoluteFilePath string, de *godirwalk.Dirent) error {
|
||||
path, err := filepath.Rel(fs.root, absoluteFilePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip if name is not in the format XX/XX/XXXXXXXXXXXX
|
||||
parts := strings.Split(path, string(os.PathSeparator))
|
||||
if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
f(absoluteFilePath, absoluteFilePath)
|
||||
func (sfs *spreadFS) Reload(f func(key string, name string)) error {
|
||||
count := 0
|
||||
err := filepath.WalkDir(sfs.root, func(absoluteFilePath string, de fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
log.Error("Error loading cache", "dir", sfs.root, err)
|
||||
}
|
||||
path, err := filepath.Rel(sfs.root, absoluteFilePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
},
|
||||
Unsorted: true,
|
||||
}
|
||||
|
||||
// Skip if name is not in the format XX/XX/XXXXXXXXXXXX
|
||||
parts := strings.Split(path, string(os.PathSeparator))
|
||||
if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 {
|
||||
return nil
|
||||
}
|
||||
|
||||
f(absoluteFilePath, absoluteFilePath)
|
||||
count++
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
log.Debug("Loaded cache", "dir", sfs.root, "numItems", count)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (fs *spreadFS) Create(name string) (stream.File, error) {
|
||||
func (sfs *spreadFS) Create(name string) (stream.File, error) {
|
||||
path := filepath.Dir(name)
|
||||
err := os.MkdirAll(path, fs.mode)
|
||||
err := os.MkdirAll(path, sfs.mode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
}
|
||||
|
||||
func (fs *spreadFS) Open(name string) (stream.File, error) {
|
||||
func (sfs *spreadFS) Open(name string) (stream.File, error) {
|
||||
return os.Open(name)
|
||||
}
|
||||
|
||||
func (fs *spreadFS) Remove(name string) error {
|
||||
func (sfs *spreadFS) Remove(name string) error {
|
||||
return os.Remove(name)
|
||||
}
|
||||
|
||||
func (fs *spreadFS) Stat(name string) (fscache.FileInfo, error) {
|
||||
func (sfs *spreadFS) Stat(name string) (fscache.FileInfo, error) {
|
||||
stat, err := os.Stat(name)
|
||||
if err != nil {
|
||||
return fscache.FileInfo{}, err
|
||||
|
@ -82,14 +89,14 @@ func (fs *spreadFS) Stat(name string) (fscache.FileInfo, error) {
|
|||
return fscache.FileInfo{FileInfo: stat, Atime: atime.Get(stat)}, nil
|
||||
}
|
||||
|
||||
func (fs *spreadFS) RemoveAll() error {
|
||||
if err := os.RemoveAll(fs.root); err != nil {
|
||||
func (sfs *spreadFS) RemoveAll() error {
|
||||
if err := os.RemoveAll(sfs.root); err != nil {
|
||||
return err
|
||||
}
|
||||
return fs.init()
|
||||
return sfs.init()
|
||||
}
|
||||
|
||||
func (fs *spreadFS) KeyMapper(key string) string {
|
||||
func (sfs *spreadFS) KeyMapper(key string) string {
|
||||
hash := fmt.Sprintf("%x", sha1.Sum([]byte(key)))
|
||||
return filepath.Join(fs.root, hash[0:2], hash[2:4], hash)
|
||||
return filepath.Join(sfs.root, hash[0:2], hash[2:4], hash)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue