fs: remove Open method from FS interface

This commit is contained in:
Michael Eischer 2024-07-21 16:30:49 +02:00
parent 263709da8c
commit 507842b614
8 changed files with 21 additions and 104 deletions

View file

@ -1664,15 +1664,6 @@ type MockFS struct {
bytesRead map[string]int // tracks bytes read from all opened files 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) { func (m *MockFS) OpenFile(name string, flag int, perm os.FileMode) (fs.File, error) {
f, err := m.FS.OpenFile(name, flag, perm) f, err := m.FS.OpenFile(name, flag, perm)
if err != nil { if err != nil {
@ -2061,14 +2052,6 @@ type TrackFS struct {
m sync.Mutex 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) { func (m *TrackFS) OpenFile(name string, flag int, perm os.FileMode) (fs.File, error) {
m.m.Lock() m.m.Lock()
m.opened[name]++ m.opened[name]++

View file

@ -72,7 +72,7 @@ func TestFileSaver(t *testing.T) {
var results []FutureNode var results []FutureNode
for _, filename := range files { for _, filename := range files {
f, err := testFs.Open(filename) f, err := testFs.OpenFile(filename, os.O_RDONLY, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -18,16 +18,6 @@ func (fs Local) VolumeName(path string) string {
return filepath.VolumeName(path) 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 // OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag // or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,

View file

@ -125,11 +125,6 @@ func (fs *LocalVss) DeleteSnapshots() {
fs.snapshots = activeSnapshots 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. // OpenFile wraps the Open method of the underlying file system.
func (fs *LocalVss) OpenFile(name string, flag int, perm os.FileMode) (File, error) { func (fs *LocalVss) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
return os.OpenFile(fs.snapshotPath(name), flag, perm) return os.OpenFile(fs.snapshotPath(name), flag, perm)

View file

@ -39,29 +39,6 @@ func (fs *Reader) VolumeName(_ string) string {
return "" 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 { func (fs *Reader) fi() os.FileInfo {
return fakeFileInfo{ return fakeFileInfo{
name: fs.Name, name: fs.Name,
@ -82,6 +59,8 @@ func (fs *Reader) OpenFile(name string, flag int, _ os.FileMode) (f File, err er
fmt.Errorf("invalid combination of flags 0x%x", flag)) fmt.Errorf("invalid combination of flags 0x%x", flag))
} }
switch name {
case fs.Name:
fs.open.Do(func() { fs.open.Do(func() {
f = newReaderFile(fs.ReadCloser, fs.fi(), fs.AllowEmptyFile) f = newReaderFile(fs.ReadCloser, fs.fi(), fs.AllowEmptyFile)
}) })
@ -91,6 +70,14 @@ func (fs *Reader) OpenFile(name string, flag int, _ os.FileMode) (f File, err er
} }
return f, nil return f, nil
case "/", ".":
f = fakeDir{
entries: []os.FileInfo{fs.fi()},
}
return f, nil
}
return nil, pathError("open", name, syscall.ENOENT)
} }
// Stat returns a FileInfo describing the named file. If there is an error, it // Stat returns a FileInfo describing the named file. If there is an error, it

View file

@ -15,27 +15,6 @@ import (
"github.com/restic/restic/internal/test" "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) { func verifyFileContentOpenFile(t testing.TB, fs FS, filename string, want []byte) {
f, err := fs.OpenFile(filename, O_RDONLY, 0) f, err := fs.OpenFile(filename, O_RDONLY, 0)
if err != nil { 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) { 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 { if err != nil {
t.Fatal(err) 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) { 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -219,12 +198,6 @@ func TestFSReader(t *testing.T) {
verifyDirectoryContentsFI(t, fs, ".", []os.FileInfo{fi}) verifyDirectoryContentsFI(t, fs, ".", []os.FileInfo{fi})
}, },
}, },
{
name: "file/Open",
f: func(t *testing.T, fs FS) {
verifyFileContentOpen(t, fs, filename, data)
},
},
{ {
name: "file/OpenFile", name: "file/OpenFile",
f: func(t *testing.T, fs FS) { f: func(t *testing.T, fs FS) {
@ -245,7 +218,7 @@ func TestFSReader(t *testing.T) {
{ {
name: "file/Stat", name: "file/Stat",
f: func(t *testing.T, fs FS) { f: func(t *testing.T, fs FS) {
f, err := fs.Open(filename) f, err := fs.OpenFile(filename, os.O_RDONLY, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -417,7 +390,7 @@ func TestFSReaderMinFileSize(t *testing.T) {
AllowEmptyFile: test.allowEmpty, AllowEmptyFile: test.allowEmpty,
} }
f, err := fs.Open("testfile") f, err := fs.OpenFile("testfile", os.O_RDONLY, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -15,16 +15,6 @@ type Track struct {
FS 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. // OpenFile wraps the OpenFile method of the underlying file system.
func (fs Track) OpenFile(name string, flag int, perm os.FileMode) (File, error) { func (fs Track) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
f, err := fs.FS.OpenFile(fixpath(name), flag, perm) f, err := fs.FS.OpenFile(fixpath(name), flag, perm)

View file

@ -7,7 +7,6 @@ import (
// FS bundles all methods needed for a file system. // FS bundles all methods needed for a file system.
type FS interface { type FS interface {
Open(name string) (File, error)
OpenFile(name string, flag int, perm os.FileMode) (File, error) OpenFile(name string, flag int, perm os.FileMode) (File, error)
Stat(name string) (os.FileInfo, error) Stat(name string) (os.FileInfo, error)
Lstat(name string) (os.FileInfo, error) Lstat(name string) (os.FileInfo, error)