From 507842b614bb80c07a37872c304274d5b2378ea8 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 21 Jul 2024 16:30:49 +0200 Subject: [PATCH] fs: remove Open method from FS interface --- internal/archiver/archiver_test.go | 17 ----------- internal/archiver/file_saver_test.go | 2 +- internal/fs/fs_local.go | 10 ------- internal/fs/fs_local_vss.go | 5 ---- internal/fs/fs_reader.go | 45 ++++++++++------------------ internal/fs/fs_reader_test.go | 35 +++------------------- internal/fs/fs_track.go | 10 ------- internal/fs/interface.go | 1 - 8 files changed, 21 insertions(+), 104 deletions(-) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index 18151eb02..d67b5b06a 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -1664,15 +1664,6 @@ type MockFS struct { bytesRead map[string]int // tracks bytes read from all opened files } -func (m *MockFS) Open(name string) (fs.File, error) { - f, err := m.FS.Open(name) - if err != nil { - return f, err - } - - return MockFile{File: f, fs: m, filename: name}, nil -} - func (m *MockFS) OpenFile(name string, flag int, perm os.FileMode) (fs.File, error) { f, err := m.FS.OpenFile(name, flag, perm) if err != nil { @@ -2061,14 +2052,6 @@ type TrackFS struct { m sync.Mutex } -func (m *TrackFS) Open(name string) (fs.File, error) { - m.m.Lock() - m.opened[name]++ - m.m.Unlock() - - return m.FS.Open(name) -} - func (m *TrackFS) OpenFile(name string, flag int, perm os.FileMode) (fs.File, error) { m.m.Lock() m.opened[name]++ diff --git a/internal/archiver/file_saver_test.go b/internal/archiver/file_saver_test.go index 4a4327572..948d7ce3c 100644 --- a/internal/archiver/file_saver_test.go +++ b/internal/archiver/file_saver_test.go @@ -72,7 +72,7 @@ func TestFileSaver(t *testing.T) { var results []FutureNode for _, filename := range files { - f, err := testFs.Open(filename) + f, err := testFs.OpenFile(filename, os.O_RDONLY, 0) if err != nil { t.Fatal(err) } diff --git a/internal/fs/fs_local.go b/internal/fs/fs_local.go index 48c40dc90..0bcbf7f3a 100644 --- a/internal/fs/fs_local.go +++ b/internal/fs/fs_local.go @@ -18,16 +18,6 @@ func (fs Local) VolumeName(path string) string { return filepath.VolumeName(path) } -// Open opens a file for reading. -func (fs Local) Open(name string) (File, error) { - f, err := os.Open(fixpath(name)) - if err != nil { - return nil, err - } - _ = setFlags(f) - return f, nil -} - // OpenFile is the generalized open call; most users will use Open // or Create instead. It opens the named file with specified flag // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, diff --git a/internal/fs/fs_local_vss.go b/internal/fs/fs_local_vss.go index 718dfc46d..46b40d013 100644 --- a/internal/fs/fs_local_vss.go +++ b/internal/fs/fs_local_vss.go @@ -125,11 +125,6 @@ func (fs *LocalVss) DeleteSnapshots() { fs.snapshots = activeSnapshots } -// Open wraps the Open method of the underlying file system. -func (fs *LocalVss) Open(name string) (File, error) { - return os.Open(fs.snapshotPath(name)) -} - // OpenFile wraps the Open method of the underlying file system. func (fs *LocalVss) OpenFile(name string, flag int, perm os.FileMode) (File, error) { return os.OpenFile(fs.snapshotPath(name), flag, perm) diff --git a/internal/fs/fs_reader.go b/internal/fs/fs_reader.go index 47af74245..93a42f9eb 100644 --- a/internal/fs/fs_reader.go +++ b/internal/fs/fs_reader.go @@ -39,29 +39,6 @@ func (fs *Reader) VolumeName(_ string) string { return "" } -// Open opens a file for reading. -func (fs *Reader) Open(name string) (f File, err error) { - switch name { - case fs.Name: - fs.open.Do(func() { - f = newReaderFile(fs.ReadCloser, fs.fi(), fs.AllowEmptyFile) - }) - - if f == nil { - return nil, pathError("open", name, syscall.EIO) - } - - return f, nil - case "/", ".": - f = fakeDir{ - entries: []os.FileInfo{fs.fi()}, - } - return f, nil - } - - return nil, pathError("open", name, syscall.ENOENT) -} - func (fs *Reader) fi() os.FileInfo { return fakeFileInfo{ name: fs.Name, @@ -82,15 +59,25 @@ func (fs *Reader) OpenFile(name string, flag int, _ os.FileMode) (f File, err er fmt.Errorf("invalid combination of flags 0x%x", flag)) } - fs.open.Do(func() { - f = newReaderFile(fs.ReadCloser, fs.fi(), fs.AllowEmptyFile) - }) + switch name { + case fs.Name: + fs.open.Do(func() { + f = newReaderFile(fs.ReadCloser, fs.fi(), fs.AllowEmptyFile) + }) - if f == nil { - return nil, pathError("open", name, syscall.EIO) + if f == nil { + return nil, pathError("open", name, syscall.EIO) + } + + return f, nil + case "/", ".": + f = fakeDir{ + entries: []os.FileInfo{fs.fi()}, + } + return f, nil } - return f, nil + return nil, pathError("open", name, syscall.ENOENT) } // Stat returns a FileInfo describing the named file. If there is an error, it diff --git a/internal/fs/fs_reader_test.go b/internal/fs/fs_reader_test.go index d3ef5608a..9fa67b5ac 100644 --- a/internal/fs/fs_reader_test.go +++ b/internal/fs/fs_reader_test.go @@ -15,27 +15,6 @@ import ( "github.com/restic/restic/internal/test" ) -func verifyFileContentOpen(t testing.TB, fs FS, filename string, want []byte) { - f, err := fs.Open(filename) - if err != nil { - t.Fatal(err) - } - - buf, err := io.ReadAll(f) - if err != nil { - t.Fatal(err) - } - - err = f.Close() - if err != nil { - t.Fatal(err) - } - - if !cmp.Equal(want, buf) { - t.Error(cmp.Diff(want, buf)) - } -} - func verifyFileContentOpenFile(t testing.TB, fs FS, filename string, want []byte) { f, err := fs.OpenFile(filename, O_RDONLY, 0) if err != nil { @@ -58,7 +37,7 @@ func verifyFileContentOpenFile(t testing.TB, fs FS, filename string, want []byte } func verifyDirectoryContents(t testing.TB, fs FS, dir string, want []string) { - f, err := fs.Open(dir) + f, err := fs.OpenFile(dir, os.O_RDONLY, 0) if err != nil { t.Fatal(err) } @@ -96,7 +75,7 @@ func (s fiSlice) Swap(i, j int) { } func verifyDirectoryContentsFI(t testing.TB, fs FS, dir string, want []os.FileInfo) { - f, err := fs.Open(dir) + f, err := fs.OpenFile(dir, os.O_RDONLY, 0) if err != nil { t.Fatal(err) } @@ -219,12 +198,6 @@ func TestFSReader(t *testing.T) { verifyDirectoryContentsFI(t, fs, ".", []os.FileInfo{fi}) }, }, - { - name: "file/Open", - f: func(t *testing.T, fs FS) { - verifyFileContentOpen(t, fs, filename, data) - }, - }, { name: "file/OpenFile", f: func(t *testing.T, fs FS) { @@ -245,7 +218,7 @@ func TestFSReader(t *testing.T) { { name: "file/Stat", f: func(t *testing.T, fs FS) { - f, err := fs.Open(filename) + f, err := fs.OpenFile(filename, os.O_RDONLY, 0) if err != nil { t.Fatal(err) } @@ -417,7 +390,7 @@ func TestFSReaderMinFileSize(t *testing.T) { AllowEmptyFile: test.allowEmpty, } - f, err := fs.Open("testfile") + f, err := fs.OpenFile("testfile", os.O_RDONLY, 0) if err != nil { t.Fatal(err) } diff --git a/internal/fs/fs_track.go b/internal/fs/fs_track.go index 0c65a8564..366bbee76 100644 --- a/internal/fs/fs_track.go +++ b/internal/fs/fs_track.go @@ -15,16 +15,6 @@ type Track struct { FS } -// Open wraps the Open method of the underlying file system. -func (fs Track) Open(name string) (File, error) { - f, err := fs.FS.Open(fixpath(name)) - if err != nil { - return nil, err - } - - return newTrackFile(debug.Stack(), name, f), nil -} - // OpenFile wraps the OpenFile method of the underlying file system. func (fs Track) OpenFile(name string, flag int, perm os.FileMode) (File, error) { f, err := fs.FS.OpenFile(fixpath(name), flag, perm) diff --git a/internal/fs/interface.go b/internal/fs/interface.go index b26c56944..e1f4ef2d9 100644 --- a/internal/fs/interface.go +++ b/internal/fs/interface.go @@ -7,7 +7,6 @@ import ( // FS bundles all methods needed for a file system. type FS interface { - Open(name string) (File, error) OpenFile(name string, flag int, perm os.FileMode) (File, error) Stat(name string) (os.FileInfo, error) Lstat(name string) (os.FileInfo, error)