update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
100
utils/merge/merge_fs.go
Normal file
100
utils/merge/merge_fs.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package merge
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"maps"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// FS implements a simple merged fs.FS, that can combine a Base FS with an Overlay FS. The semantics are:
|
||||
// - Files from the Overlay FS will override files with the same name in the Base FS
|
||||
// - Directories are combined, with priority for the Overlay FS over the Base FS for files with matching names
|
||||
type FS struct {
|
||||
Base fs.FS
|
||||
Overlay fs.FS
|
||||
}
|
||||
|
||||
func (m FS) Open(name string) (fs.File, error) {
|
||||
file, err := m.Overlay.Open(name)
|
||||
if err != nil {
|
||||
return m.Base.Open(name)
|
||||
}
|
||||
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
overlayDirFile, ok := file.(fs.ReadDirFile)
|
||||
if !info.IsDir() || !ok {
|
||||
return file, nil
|
||||
}
|
||||
|
||||
baseDir, _ := m.Base.Open(name)
|
||||
defer func() {
|
||||
_ = baseDir.Close()
|
||||
_ = file.Close()
|
||||
}()
|
||||
baseDirFile, ok := baseDir.(fs.ReadDirFile)
|
||||
if !ok {
|
||||
return nil, fs.ErrInvalid
|
||||
}
|
||||
return m.mergeDirs(name, info, baseDirFile, overlayDirFile)
|
||||
}
|
||||
|
||||
func (m FS) mergeDirs(name string, info fs.FileInfo, baseDir fs.ReadDirFile, overlayDir fs.ReadDirFile) (fs.File, error) {
|
||||
baseFiles, err := baseDir.ReadDir(-1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
overlayFiles, err := overlayDir.ReadDir(-1)
|
||||
if err != nil {
|
||||
overlayFiles = nil
|
||||
}
|
||||
|
||||
merged := map[string]fs.DirEntry{}
|
||||
for _, f := range baseFiles {
|
||||
merged[f.Name()] = f
|
||||
}
|
||||
for _, f := range overlayFiles {
|
||||
merged[f.Name()] = f
|
||||
}
|
||||
it := maps.Values(merged)
|
||||
entries := slices.SortedFunc(it, func(i, j fs.DirEntry) int { return cmp.Compare(i.Name(), j.Name()) })
|
||||
return &mergedDir{
|
||||
name: name,
|
||||
info: info,
|
||||
entries: entries,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type mergedDir struct {
|
||||
name string
|
||||
info fs.FileInfo
|
||||
entries []fs.DirEntry
|
||||
pos int
|
||||
}
|
||||
|
||||
var _ fs.ReadDirFile = (*mergedDir)(nil)
|
||||
|
||||
func (d *mergedDir) ReadDir(count int) ([]fs.DirEntry, error) {
|
||||
if d.pos >= len(d.entries) && count > 0 {
|
||||
return nil, io.EOF
|
||||
}
|
||||
if count <= 0 || count > len(d.entries)-d.pos {
|
||||
count = len(d.entries) - d.pos
|
||||
}
|
||||
entries := d.entries[d.pos : d.pos+count]
|
||||
d.pos += count
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func (d *mergedDir) Close() error { return nil }
|
||||
func (d *mergedDir) Stat() (fs.FileInfo, error) { return d.info, nil }
|
||||
func (d *mergedDir) Read([]byte) (int, error) {
|
||||
return 0, &fs.PathError{Op: "read", Path: d.name, Err: errors.New("is a directory")}
|
||||
}
|
||||
117
utils/merge/merge_fs_test.go
Normal file
117
utils/merge/merge_fs_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package merge_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/merge"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestMergeFS(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "MergeFS Suite")
|
||||
}
|
||||
|
||||
var _ = Describe("FS", func() {
|
||||
var baseName, overlayName string
|
||||
var mergedDir fs.FS
|
||||
|
||||
BeforeEach(func() {
|
||||
baseName, _ = os.MkdirTemp("", "merge_fs_base_test")
|
||||
overlayName, _ = os.MkdirTemp("", "merge_fs_overlay_test")
|
||||
baseDir := os.DirFS(baseName)
|
||||
overlayDir := os.DirFS(overlayName)
|
||||
mergedDir = merge.FS{Base: baseDir, Overlay: overlayDir}
|
||||
})
|
||||
AfterEach(func() {
|
||||
_ = os.RemoveAll(baseName)
|
||||
_ = os.RemoveAll(overlayName)
|
||||
})
|
||||
|
||||
It("reads from Base dir if not found in Overlay", func() {
|
||||
_f(baseName, "a.json")
|
||||
file, err := mergedDir.Open("a.json")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
stat, err := file.Stat()
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(stat.Name()).To(Equal("a.json"))
|
||||
})
|
||||
|
||||
It("reads overridden file", func() {
|
||||
_f(baseName, "b.json", "original")
|
||||
_f(baseName, "b.json", "overridden")
|
||||
|
||||
file, err := mergedDir.Open("b.json")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
content, err := io.ReadAll(file)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(string(content)).To(Equal("overridden"))
|
||||
})
|
||||
|
||||
It("reads only files from Base if Overlay is empty", func() {
|
||||
_f(baseName, "test.txt")
|
||||
|
||||
dir, err := mergedDir.Open(".")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
list, err := dir.(fs.ReadDirFile).ReadDir(-1)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(list).To(HaveLen(1))
|
||||
Expect(list[0].Name()).To(Equal("test.txt"))
|
||||
})
|
||||
|
||||
It("reads merged dirs", func() {
|
||||
_f(baseName, "1111.txt")
|
||||
_f(overlayName, "2222.json")
|
||||
|
||||
dir, err := mergedDir.Open(".")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
list, err := dir.(fs.ReadDirFile).ReadDir(-1)
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
Expect(list).To(HaveLen(2))
|
||||
Expect(list[0].Name()).To(Equal("1111.txt"))
|
||||
Expect(list[1].Name()).To(Equal("2222.json"))
|
||||
})
|
||||
|
||||
It("allows to seek to the beginning of the directory", func() {
|
||||
_f(baseName, "1111.txt")
|
||||
_f(baseName, "2222.txt")
|
||||
_f(baseName, "3333.txt")
|
||||
|
||||
dir, err := mergedDir.Open(".")
|
||||
Expect(err).To(BeNil())
|
||||
|
||||
list, _ := dir.(fs.ReadDirFile).ReadDir(2)
|
||||
Expect(list).To(HaveLen(2))
|
||||
Expect(list[0].Name()).To(Equal("1111.txt"))
|
||||
Expect(list[1].Name()).To(Equal("2222.txt"))
|
||||
|
||||
list, _ = dir.(fs.ReadDirFile).ReadDir(2)
|
||||
Expect(list).To(HaveLen(1))
|
||||
Expect(list[0].Name()).To(Equal("3333.txt"))
|
||||
})
|
||||
})
|
||||
|
||||
func _f(dir, name string, content ...string) string {
|
||||
path := filepath.Join(dir, name)
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if len(content) > 0 {
|
||||
_, _ = file.WriteString(content[0])
|
||||
}
|
||||
_ = file.Close()
|
||||
return path
|
||||
}
|
||||
Reference in New Issue
Block a user