quintodrome/utils/cache/spread_fs.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.1 KiB
Go
Raw Normal View History

2020-10-23 17:30:45 -08:00
package cache
import (
2020-10-24 08:26:57 -08:00
"crypto/sha1"
2020-10-23 17:30:45 -08:00
"fmt"
"io/fs"
2020-10-23 17:30:45 -08:00
"os"
"path/filepath"
"strings"
"github.com/djherbis/atime"
2020-10-23 17:30:45 -08:00
"github.com/djherbis/fscache"
2022-07-26 11:16:37 -08:00
"github.com/djherbis/stream"
"github.com/navidrome/navidrome/log"
2020-10-23 17:30:45 -08:00
)
2020-10-24 08:26:57 -08:00
type spreadFS struct {
2020-10-23 17:30:45 -08:00
root string
mode os.FileMode
init func() error
}
2021-02-09 09:15:43 -09:00
// NewSpreadFS returns a FileSystem rooted at directory dir. This FS hashes the key and
// distributes all files in a layout like XX/XX/XXXXXXXXXX. Ex:
2022-09-26 17:28:10 -08:00
//
// Key is abc123.300x300.jpg
// Hash would be: c574aeb3caafcf93ee337f0cf34e31a428ba3f13
// File in cache would be: c5 / 74 / c574aeb3caafcf93ee337f0cf34e31a428ba3f13
//
2021-02-09 09:15:43 -09:00
// The idea is to avoid having too many files in one dir, which could potentially cause performance issues
// and may hit limitations depending on the OS.
// See discussion here: https://github.com/djherbis/fscache/issues/8#issuecomment-614319323
//
// dir is created with specified mode if it doesn't exist.
func NewSpreadFS(dir string, mode os.FileMode) (*spreadFS, error) {
2022-07-25 19:01:19 -08:00
f := &spreadFS{root: dir, mode: mode, init: func() error {
2020-10-23 17:30:45 -08:00
return os.MkdirAll(dir, mode)
}}
2022-07-25 19:01:19 -08:00
return f, f.init()
2020-10-23 17:30:45 -08:00
}
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 {
2022-09-30 16:18:14 -08:00
return nil //nolint:nilerr
}
2020-10-24 08:26:57 -08:00
// Skip if name is not in the format XX/XX/XXXXXXXXXXXX
parts := strings.Split(path, string(os.PathSeparator))
2022-07-25 19:01:19 -08:00
if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 || len(parts[2]) != 40 {
2020-10-23 17:30:45 -08:00
return nil
}
f(absoluteFilePath, absoluteFilePath)
count++
return nil
2020-10-23 17:30:45 -08:00
})
if err == nil {
log.Debug("Loaded cache", "dir", sfs.root, "numItems", count)
}
return err
2020-10-23 17:30:45 -08:00
}
func (sfs *spreadFS) Create(name string) (stream.File, error) {
path := filepath.Dir(name)
err := os.MkdirAll(path, sfs.mode)
2020-10-23 17:30:45 -08:00
if err != nil {
return nil, err
}
return os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
2020-10-23 17:30:45 -08:00
}
func (sfs *spreadFS) Open(name string) (stream.File, error) {
2020-10-23 17:30:45 -08:00
return os.Open(name)
}
func (sfs *spreadFS) Remove(name string) error {
2020-10-23 17:30:45 -08:00
return os.Remove(name)
}
func (sfs *spreadFS) Stat(name string) (fscache.FileInfo, error) {
2020-10-23 17:30:45 -08:00
stat, err := os.Stat(name)
if err != nil {
return fscache.FileInfo{}, err
}
return fscache.FileInfo{FileInfo: stat, Atime: atime.Get(stat)}, nil
}
func (sfs *spreadFS) RemoveAll() error {
if err := os.RemoveAll(sfs.root); err != nil {
2020-10-23 17:30:45 -08:00
return err
}
return sfs.init()
2020-10-23 17:30:45 -08:00
}
func (sfs *spreadFS) KeyMapper(key string) string {
2022-07-25 19:01:19 -08:00
// When running the Haunter, fscache can call this KeyMapper with the cached filepath instead of the key.
// That's because we don't inform the original cache keys when reloading in the Reload function above.
// If that's the case, just return the file path, as it is the actual mapped key.
if strings.HasPrefix(key, sfs.root) {
return key
}
hash := fmt.Sprintf("%x", sha1.Sum([]byte(key)))
return filepath.Join(sfs.root, hash[0:2], hash[2:4], hash)
}