From 2a860f0658791683a40f9a18206fd881a4dfd853 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 17 Jul 2023 13:44:29 +0200 Subject: wip --- lib/fs/basicfs.go | 4 ++++ lib/fs/fakefs.go | 4 ++++ lib/fs/filesystem.go | 21 +++++++++++++++++++++ lib/fs/filesystem_copy_range_test.go | 14 ++------------ lib/fs/metrics.go | 4 ++++ lib/fs/mtimefs_test.go | 6 ++++-- lib/scanner/virtualfs_test.go | 2 +- 7 files changed, 40 insertions(+), 15 deletions(-) diff --git a/lib/fs/basicfs.go b/lib/fs/basicfs.go index 58afc0e27..cb76de769 100644 --- a/lib/fs/basicfs.go +++ b/lib/fs/basicfs.go @@ -350,6 +350,10 @@ func (f basicFile) Stat() (FileInfo, error) { return basicFileInfo{info}, nil } +func (f basicFile) underlying() (File, bool) { + return nil, false +} + // basicFileInfo implements the fs.FileInfo interface on top of an os.FileInfo. type basicFileInfo struct { os.FileInfo diff --git a/lib/fs/fakefs.go b/lib/fs/fakefs.go index 65151dbc3..bca00a9eb 100644 --- a/lib/fs/fakefs.go +++ b/lib/fs/fakefs.go @@ -961,6 +961,10 @@ func (*fakeFile) Sync() error { return nil } +func (f *fakeFile) underlying() (File, bool) { + return nil, false +} + // fakeFileInfo is the stat result. type fakeFileInfo struct { fakeEntry // intentionally a copy of the struct diff --git a/lib/fs/filesystem.go b/lib/fs/filesystem.go index 959407739..436603361 100644 --- a/lib/fs/filesystem.go +++ b/lib/fs/filesystem.go @@ -94,6 +94,10 @@ type File interface { Sync() error } +type wrappedFile interface { + underlying() (File, bool) +} + // The FileInfo interface is almost the same as os.FileInfo, but with the // Sys method removed (as we don't want to expose whatever is underlying) // and with a couple of convenience methods added. @@ -360,6 +364,23 @@ func unwrapFilesystem(fs Filesystem, wrapperType filesystemWrapperType) (Filesys } } +func unwrapFileToBasic(f File) (File, bool) { + for { + basic, ok := f.(basicFile) + if ok { + return basic, true + } + wrapped, ok := f.(wrappedFile) + if !ok { + return nil, false + } + f, ok = wrapped.underlying() + if !ok { + return nil, false + } + } +} + // WriteFile writes data to the named file, creating it if necessary. // If the file does not exist, WriteFile creates it with permissions perm (before umask); // otherwise WriteFile truncates it before writing, without changing permissions. diff --git a/lib/fs/filesystem_copy_range_test.go b/lib/fs/filesystem_copy_range_test.go index 890db7199..36c36c99b 100644 --- a/lib/fs/filesystem_copy_range_test.go +++ b/lib/fs/filesystem_copy_range_test.go @@ -320,21 +320,11 @@ func TestCopyRange(tttt *testing.T) { t.Fatal(err) } - srcBasic, ok := src.(basicFile) - if !ok { - if srcMetric, isMetr := src.(*metricsFile); isMetr { - srcBasic, ok = srcMetric.next.(basicFile) - } - } + srcBasic, ok := unwrapFileToBasic(src) if !ok { t.Fatal("src file is not a basic file") } - dstBasic, ok := dst.(basicFile) - if !ok { - if dstMetric, isMetr := src.(*metricsFile); isMetr { - dstBasic, ok = dstMetric.next.(basicFile) - } - } + dstBasic, ok := unwrapFileToBasic(dst) if !ok { t.Fatal("dst file is not a basic file") } diff --git a/lib/fs/metrics.go b/lib/fs/metrics.go index 191872c47..9c876932f 100644 --- a/lib/fs/metrics.go +++ b/lib/fs/metrics.go @@ -287,3 +287,7 @@ func (m *metricsFile) Name() string { defer m.fs.account("Name")(-1) return m.next.Name() } + +func (m *metricsFile) underlying() (File, bool) { + return m.next, true +} diff --git a/lib/fs/mtimefs_test.go b/lib/fs/mtimefs_test.go index 1ce5407ec..22f02b383 100644 --- a/lib/fs/mtimefs_test.go +++ b/lib/fs/mtimefs_test.go @@ -260,6 +260,8 @@ func newMtimeFS(path string, db database, options ...MtimeFSOption) *mtimeFS { } func newMtimeFSWithWalk(path string, db database, options ...MtimeFSOption) (*mtimeFS, *walkFilesystem) { - wfs := NewFilesystem(FilesystemTypeBasic, path, NewMtimeOption(db, options...)).(*walkFilesystem) - return wfs.Filesystem.(*mtimeFS), wfs + fs := NewFilesystem(FilesystemTypeBasic, path, NewMtimeOption(db, options...)) + wfs, _ := unwrapFilesystem(fs, filesystemWrapperTypeWalk) + mfs, _ := unwrapFilesystem(fs, filesystemWrapperTypeMtime) + return mfs.(*mtimeFS), wfs.(*walkFilesystem) } diff --git a/lib/scanner/virtualfs_test.go b/lib/scanner/virtualfs_test.go index 4b97e2e52..d1c5515b5 100644 --- a/lib/scanner/virtualfs_test.go +++ b/lib/scanner/virtualfs_test.go @@ -115,7 +115,7 @@ type fakeInfo struct { } func (f fakeInfo) Name() string { return f.name } -func (fakeInfo) Mode() fs.FileMode { return 0755 } +func (fakeInfo) Mode() fs.FileMode { return 0o755 } func (f fakeInfo) Size() int64 { return f.size } func (fakeInfo) ModTime() time.Time { return time.Unix(1234567890, 0) } func (f fakeInfo) IsDir() bool { -- cgit v1.2.3-54-g00ecf From fe35d5a465be5bfdc1d8c83d12e226d5f945097d Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 17 Jul 2023 13:49:48 +0200 Subject: wip --- lib/fs/basicfs.go | 4 ---- lib/fs/fakefs.go | 4 ---- lib/fs/filesystem.go | 21 --------------------- lib/fs/filesystem_copy_range_test.go | 4 ++-- lib/fs/metrics.go | 4 ++-- 5 files changed, 4 insertions(+), 33 deletions(-) diff --git a/lib/fs/basicfs.go b/lib/fs/basicfs.go index cb76de769..58afc0e27 100644 --- a/lib/fs/basicfs.go +++ b/lib/fs/basicfs.go @@ -350,10 +350,6 @@ func (f basicFile) Stat() (FileInfo, error) { return basicFileInfo{info}, nil } -func (f basicFile) underlying() (File, bool) { - return nil, false -} - // basicFileInfo implements the fs.FileInfo interface on top of an os.FileInfo. type basicFileInfo struct { os.FileInfo diff --git a/lib/fs/fakefs.go b/lib/fs/fakefs.go index bca00a9eb..65151dbc3 100644 --- a/lib/fs/fakefs.go +++ b/lib/fs/fakefs.go @@ -961,10 +961,6 @@ func (*fakeFile) Sync() error { return nil } -func (f *fakeFile) underlying() (File, bool) { - return nil, false -} - // fakeFileInfo is the stat result. type fakeFileInfo struct { fakeEntry // intentionally a copy of the struct diff --git a/lib/fs/filesystem.go b/lib/fs/filesystem.go index 436603361..959407739 100644 --- a/lib/fs/filesystem.go +++ b/lib/fs/filesystem.go @@ -94,10 +94,6 @@ type File interface { Sync() error } -type wrappedFile interface { - underlying() (File, bool) -} - // The FileInfo interface is almost the same as os.FileInfo, but with the // Sys method removed (as we don't want to expose whatever is underlying) // and with a couple of convenience methods added. @@ -364,23 +360,6 @@ func unwrapFilesystem(fs Filesystem, wrapperType filesystemWrapperType) (Filesys } } -func unwrapFileToBasic(f File) (File, bool) { - for { - basic, ok := f.(basicFile) - if ok { - return basic, true - } - wrapped, ok := f.(wrappedFile) - if !ok { - return nil, false - } - f, ok = wrapped.underlying() - if !ok { - return nil, false - } - } -} - // WriteFile writes data to the named file, creating it if necessary. // If the file does not exist, WriteFile creates it with permissions perm (before umask); // otherwise WriteFile truncates it before writing, without changing permissions. diff --git a/lib/fs/filesystem_copy_range_test.go b/lib/fs/filesystem_copy_range_test.go index 36c36c99b..04328be0f 100644 --- a/lib/fs/filesystem_copy_range_test.go +++ b/lib/fs/filesystem_copy_range_test.go @@ -320,11 +320,11 @@ func TestCopyRange(tttt *testing.T) { t.Fatal(err) } - srcBasic, ok := unwrapFileToBasic(src) + srcBasic, ok := unwrap(src).(basicFile) if !ok { t.Fatal("src file is not a basic file") } - dstBasic, ok := unwrapFileToBasic(dst) + dstBasic, ok := unwrap(dst).(basicFile) if !ok { t.Fatal("dst file is not a basic file") } diff --git a/lib/fs/metrics.go b/lib/fs/metrics.go index 9c876932f..fe09a9a18 100644 --- a/lib/fs/metrics.go +++ b/lib/fs/metrics.go @@ -288,6 +288,6 @@ func (m *metricsFile) Name() string { return m.next.Name() } -func (m *metricsFile) underlying() (File, bool) { - return m.next, true +func (m *metricsFile) unwrap() File { + return m.next } -- cgit v1.2.3-54-g00ecf