Replace `godirwalk` with standard Go 1.16 `filepath.WalkDir`

Should fix https://github.com/navidrome/navidrome/issues/1048
This commit is contained in:
Deluan 2021-04-27 11:28:22 -04:00
parent 10cfaad95c
commit d1605dcfbe
3 changed files with 35 additions and 31 deletions

1
go.mod
View File

@ -24,7 +24,6 @@ require (
github.com/golangci/golangci-lint v1.39.0
github.com/google/uuid v1.2.0
github.com/google/wire v0.5.0
github.com/karrick/godirwalk v1.16.1
github.com/kennygrant/sanitize v0.0.0-20170120101633-6a0bfdde8629
github.com/kr/pretty v0.2.1
github.com/mattn/go-sqlite3 v2.0.3+incompatible

2
go.sum
View File

@ -423,8 +423,6 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julz/importas v0.0.0-20210228071311-d0bf5cb4e1db h1:ZmwBthGFMVAieuVpLzuedUH9l4pY/0iFG16DN9dS38o=
github.com/julz/importas v0.0.0-20210228071311-d0bf5cb4e1db/go.mod h1:oSFU2R4XK/P7kNBrnL/FEQlDGN1/6WoxXEjSSXO0DV0=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kennygrant/sanitize v0.0.0-20170120101633-6a0bfdde8629 h1:m1E9veL+2sjZOMSM7y3a6jJ9fNVaGyIJCXYDPm9U+/0=

View File

@ -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)
}