Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fsx): recursive merging edge cases #709

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions fsx/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (m mergedFS) ReadDir(name string) ([]fs.DirEntry, error) {
}
entries = append(entries, e...)
}
if entries == nil {
if len(entries) == 0 {
return nil, errors.WithStack(fs.ErrNotExist)
}

Expand Down Expand Up @@ -147,11 +147,16 @@ func (d *dirEntries) clean() {

for i := 1; i < len(*d); i++ {
if (*d)[i-1].Name() == (*d)[i].Name() {
if len(*d)-i >= 2 {
*d = append((*d)[:i+1], (*d)[i+2:]...)
} else {
*d = (*d)[:len(*d)-1]
if len(*d)-i == 1 {
// remove the last entry; we're done
*d = (*d)[:i]
return
}
// remove the duplicate entry at index i
*d = append((*d)[:i], (*d)[i+1:]...)

// need to check the same index again
i--
}
}
}
Expand All @@ -177,28 +182,31 @@ func (m *mergedFile) Close() error {
}

func (m *mergedFile) ReadDir(n int) ([]fs.DirEntry, error) {
entries := m.unprocessedDirEntries

if len(entries) < n || n <= 0 {
allEOF := true
for _, f := range m.files {
if f, ok := f.(fs.ReadDirFile); ok {
e, err := f.ReadDir(n)
switch {
case !errors.Is(err, io.EOF):
allEOF = false
case errors.Is(err, fs.ErrNotExist), errors.Is(err, io.EOF):
case err != nil:
return nil, err
}
entries = append(entries, e...)
}
if m.unprocessedDirEntries != nil {
if n <= 0 {
entries := m.unprocessedDirEntries
m.unprocessedDirEntries = nil
return entries, nil
}
if allEOF {
if n > 0 {
return entries, io.EOF
if n >= len(m.unprocessedDirEntries) {
entries := m.unprocessedDirEntries
m.unprocessedDirEntries = nil
return entries, io.EOF
}

var entries dirEntries
entries, m.unprocessedDirEntries = m.unprocessedDirEntries[:n], m.unprocessedDirEntries[n:]
return entries, nil
}

var entries dirEntries
for _, f := range m.files {
if f, ok := f.(fs.ReadDirFile); ok {
e, err := f.ReadDir(-1)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
return entries, nil
entries = append(entries, e...)
}
}
if entries == nil {
Expand All @@ -216,6 +224,8 @@ func (m *mergedFile) ReadDir(n int) ([]fs.DirEntry, error) {
return entries, io.EOF
}

entries, m.unprocessedDirEntries = entries[:n], entries[n:]
if n <= len(entries) && n > 0 {
entries, m.unprocessedDirEntries = entries[:n], entries[n:]
}
return entries, nil
}
108 changes: 103 additions & 5 deletions fsx/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,117 @@ import (
"testing"
"testing/fstest"

"github.com/laher/mergefs"
"github.com/stretchr/testify/assert"
)

func TestMergeFS(t *testing.T) {
a := fstest.MapFS{
var (
a = fstest.MapFS{
"a": &fstest.MapFile{},
"dir/c": &fstest.MapFile{},
}
b := fstest.MapFS{
b = fstest.MapFS{
"b": &fstest.MapFile{},
"dir/d": &fstest.MapFile{},
}
m := Merge(a, b)
x = fstest.MapFS{
"x": &fstest.MapFile{},
"dir/y": &fstest.MapFile{},
}
)

func TestMergeFS(t *testing.T) {
assert.NoError(t, fstest.TestFS(
Merge(a, b),
"a",
"b",
"dir",
"dir/c",
"dir/d",
))

assert.NoError(t, fstest.TestFS(
Merge(a, b, x),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
assert.NoError(t, fstest.TestFS(
Merge(x, b, a),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
assert.NoError(t, fstest.TestFS(
Merge(Merge(a, b), x),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
assert.NoError(t, fstest.TestFS(
Merge(Merge(x, b), a),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
}

func TestLaherMergeFS(t *testing.T) {
assert.Error(t, fstest.TestFS(
mergefs.Merge(a, b),
"a",
"b",
"dir",
"dir/c",
"dir/d",
))

t.Skip("laher/mergefs does not handle recursive merges correctly")

assert.NoError(t, fstest.TestFS(m, "a", "b", "dir", "dir/c", "dir/d"))
assert.NoError(t, fstest.TestFS(
mergefs.Merge(mergefs.Merge(a, b), x),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
assert.NoError(t, fstest.TestFS(
mergefs.Merge(a, mergefs.Merge(b, x)),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
assert.NoError(t, fstest.TestFS(
mergefs.Merge(x, mergefs.Merge(b, a)),
"a",
"b",
"dir",
"dir/c",
"dir/d",
"dir/y",
"x",
))
}
27 changes: 14 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
github.com/ory/analytics-go/v5 v5.0.1
github.com/ory/dockertest/v3 v3.9.1
github.com/ory/go-acc v0.2.9-0.20230103102148-6b1c9a70dbbe
github.com/ory/herodot v0.9.13
github.com/ory/herodot v0.10.3-0.20230626083119-d7e5192f0d88
github.com/ory/jsonschema/v3 v3.0.7
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
Expand All @@ -62,7 +62,7 @@ require (
github.com/seatgeek/logrus-gelf-formatter v0.0.0-20210414080842-5b05eb8ff761
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
github.com/tidwall/gjson v1.14.3
Expand All @@ -84,12 +84,12 @@ require (
go.opentelemetry.io/proto/otlp v0.18.0
go.uber.org/goleak v1.2.1
golang.org/x/crypto v0.1.0
golang.org/x/mod v0.6.0
golang.org/x/net v0.7.0
golang.org/x/mod v0.10.0
golang.org/x/net v0.8.0
golang.org/x/sync v0.1.0
gonum.org/v1/plot v0.12.0
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
google.golang.org/grpc v1.54.0
google.golang.org/protobuf v1.30.0
)

require (
Expand Down Expand Up @@ -134,7 +134,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20221010195024-131d412537ea // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/css v1.0.0 // indirect
Expand All @@ -152,6 +152,7 @@ require (
github.com/joho/godotenv v1.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/laher/mergefs v0.1.1
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -177,11 +178,11 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d // indirect
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.14.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand All @@ -191,12 +192,12 @@ require (
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.1 // indirect
go.opentelemetry.io/otel/metric v0.33.0 // indirect
golang.org/x/image v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.2.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/genproto v0.0.0-20230403163135-c38d8f061ccd // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading
Loading