From cf95922e71986c0116204b6eeb3b345a01ffd842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 23 Feb 2023 10:14:33 +0100 Subject: [PATCH] all: Run gofumpt -l -w . --- afero.go | 2 +- afero_test.go | 25 +++++++++-------- basepath.go | 1 - basepath_test.go | 19 ++++++------- composite_test.go | 35 ++++++++++++------------ copyOnWriteFs.go | 9 +++--- copyOnWriteFs_test.go | 10 +++---- gcsfs/file.go | 10 +++---- gcsfs/file_info.go | 1 + gcsfs/file_resource.go | 6 ++-- gcsfs/fs.go | 7 +++-- gcsfs/gcs.go | 12 ++++++++ gcsfs/gcs_mocks.go | 2 +- gcsfs/gcs_test.go | 26 +++++++++--------- iofs_test.go | 13 ++------- ioutil.go | 11 +++++--- ioutil_test.go | 4 +-- lstater_test.go | 4 +-- match_test.go | 6 ++-- mem/file.go | 7 +++-- mem/file_test.go | 10 +++---- memmap.go | 6 ++-- memmap_test.go | 62 +++++++++++++++++++++--------------------- regexpfs.go | 1 - ro_regexp_test.go | 3 +- sftpfs/sftp_test.go | 14 +++++----- symlink.go | 6 ++-- symlink_test.go | 12 ++++---- tarfs/tarfs_test.go | 6 ++-- unionFile.go | 5 ++-- util.go | 7 ++--- util_test.go | 14 ++++------ zipfs/zipfs_test.go | 4 +-- 33 files changed, 182 insertions(+), 178 deletions(-) diff --git a/afero.go b/afero.go index 199480cd..39f65852 100644 --- a/afero.go +++ b/afero.go @@ -97,7 +97,7 @@ type Fs interface { // Chown changes the uid and gid of the named file. Chown(name string, uid, gid int) error - //Chtimes changes the access and modification times of the named file + // Chtimes changes the access and modification times of the named file Chtimes(name string, atime time.Time, mtime time.Time) error } diff --git a/afero_test.go b/afero_test.go index f875ae12..9a1d84f2 100644 --- a/afero_test.go +++ b/afero_test.go @@ -28,8 +28,10 @@ import ( "testing" ) -var testName = "test.txt" -var Fss = []Fs{&MemMapFs{}, &OsFs{}} +var ( + testName = "test.txt" + Fss = []Fs{&MemMapFs{}, &OsFs{}} +) var testRegistry map[Fs][]string = make(map[Fs][]string) @@ -45,7 +47,6 @@ func testDir(fs Fs) string { func tmpFile(fs Fs) File { x, err := TempFile(fs, "", "afero") - if err != nil { panic(fmt.Sprint("unable to work with temp file", err)) } @@ -55,7 +56,7 @@ func tmpFile(fs Fs) File { return x } -//Read with length 0 should not return EOF. +// Read with length 0 should not return EOF. func TestRead0(t *testing.T) { for _, fs := range Fss { f := tmpFile(fs) @@ -83,7 +84,7 @@ func TestOpenFile(t *testing.T) { tmp := testDir(fs) path := filepath.Join(tmp, testName) - f, err := fs.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600) + f, err := fs.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600) if err != nil { t.Error(fs.Name(), "OpenFile (O_CREATE) failed:", err) continue @@ -91,7 +92,7 @@ func TestOpenFile(t *testing.T) { io.WriteString(f, "initial") f.Close() - f, err = fs.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0600) + f, err = fs.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0o600) if err != nil { t.Error(fs.Name(), "OpenFile (O_APPEND) failed:", err) continue @@ -99,7 +100,7 @@ func TestOpenFile(t *testing.T) { io.WriteString(f, "|append") f.Close() - f, _ = fs.OpenFile(path, os.O_RDONLY, 0600) + f, _ = fs.OpenFile(path, os.O_RDONLY, 0o600) contents, _ := ioutil.ReadAll(f) expectedContents := "initial|append" if string(contents) != expectedContents { @@ -107,7 +108,7 @@ func TestOpenFile(t *testing.T) { } f.Close() - f, err = fs.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0600) + f, err = fs.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0o600) if err != nil { t.Error(fs.Name(), "OpenFile (O_TRUNC) failed:", err) continue @@ -333,7 +334,7 @@ func TestSeek(t *testing.T) { whence int out int64 } - var tests = []test{ + tests := []test{ {0, 1, int64(len(data))}, {0, 0, 0}, {5, 0, 5}, @@ -424,7 +425,7 @@ func setupTestDirReusePath(t *testing.T, fs Fs, path string) string { func setupTestFiles(t *testing.T, fs Fs, path string) string { testSubDir := filepath.Join(path, "more", "subdirectories", "for", "testing", "we") - err := fs.MkdirAll(testSubDir, 0700) + err := fs.MkdirAll(testSubDir, 0o700) if err != nil && !os.IsExist(err) { t.Fatal(err) } @@ -637,7 +638,7 @@ func TestReaddirAll(t *testing.T) { if err != nil { t.Fatal(err) } - var namesRoot = []string{} + namesRoot := []string{} for _, e := range rootInfo { namesRoot = append(namesRoot, e.Name()) } @@ -652,7 +653,7 @@ func TestReaddirAll(t *testing.T) { if err != nil { t.Fatal(err) } - var namesSub = []string{} + namesSub := []string{} for _, e := range subInfo { namesSub = append(namesSub, e.Name()) } diff --git a/basepath.go b/basepath.go index 70a1d916..2e72793a 100644 --- a/basepath.go +++ b/basepath.go @@ -40,7 +40,6 @@ func (f *BasePathFile) Name() string { func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error) { if rdf, ok := f.File.(fs.ReadDirFile); ok { return rdf.ReadDir(n) - } return readDirFile{f.File}.ReadDir(n) } diff --git a/basepath_test.go b/basepath_test.go index e314c05f..1c46abd2 100644 --- a/basepath_test.go +++ b/basepath_test.go @@ -9,7 +9,7 @@ import ( func TestBasePath(t *testing.T) { baseFs := &MemMapFs{} - baseFs.MkdirAll("/base/path/tmp", 0777) + baseFs.MkdirAll("/base/path/tmp", 0o777) bp := NewBasePathFs(baseFs, "/base/path") if _, err := bp.Create("/tmp/foo"); err != nil { @@ -23,8 +23,8 @@ func TestBasePath(t *testing.T) { func TestBasePathRoot(t *testing.T) { baseFs := &MemMapFs{} - baseFs.MkdirAll("/base/path/foo/baz", 0777) - baseFs.MkdirAll("/base/path/boo/", 0777) + baseFs.MkdirAll("/base/path/foo/baz", 0o777) + baseFs.MkdirAll("/base/path/boo/", 0o777) bp := NewBasePathFs(baseFs, "/base/path") rd, err := ReadDir(bp, string(os.PathSeparator)) @@ -56,7 +56,6 @@ func TestRealPath(t *testing.T) { subDir := filepath.Join(baseDir, "s1") realPath, err := bp.RealPath("/s1") - if err != nil { t.Errorf("Got error %s", err) } @@ -77,7 +76,6 @@ func TestRealPath(t *testing.T) { // is not inside the base file system. // The user will receive an os.ErrNotExist later. surrealPath, err := bp.RealPath(anotherDir) - if err != nil { t.Errorf("Got error %s", err) } @@ -88,7 +86,6 @@ func TestRealPath(t *testing.T) { t.Errorf("Expected \n%s got \n%s", excpected, surrealPath) } } - } func TestNestedBasePaths(t *testing.T) { @@ -119,7 +116,7 @@ func TestNestedBasePaths(t *testing.T) { } for _, s := range specs { - if err := s.BaseFs.MkdirAll(s.FileName, 0755); err != nil { + if err := s.BaseFs.MkdirAll(s.FileName, 0o755); err != nil { t.Errorf("Got error %s", err.Error()) } if _, err := s.BaseFs.Stat(s.FileName); err != nil { @@ -143,9 +140,9 @@ func TestNestedBasePaths(t *testing.T) { func TestBasePathOpenFile(t *testing.T) { baseFs := &MemMapFs{} - baseFs.MkdirAll("/base/path/tmp", 0777) + baseFs.MkdirAll("/base/path/tmp", 0o777) bp := NewBasePathFs(baseFs, "/base/path") - f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0600) + f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0o600) if err != nil { t.Fatalf("failed to open file: %v", err) } @@ -156,7 +153,7 @@ func TestBasePathOpenFile(t *testing.T) { func TestBasePathCreate(t *testing.T) { baseFs := &MemMapFs{} - baseFs.MkdirAll("/base/path/tmp", 0777) + baseFs.MkdirAll("/base/path/tmp", 0o777) bp := NewBasePathFs(baseFs, "/base/path") f, err := bp.Create("/tmp/file.txt") if err != nil { @@ -169,7 +166,7 @@ func TestBasePathCreate(t *testing.T) { func TestBasePathTempFile(t *testing.T) { baseFs := &MemMapFs{} - baseFs.MkdirAll("/base/path/tmp", 0777) + baseFs.MkdirAll("/base/path/tmp", 0o777) bp := NewBasePathFs(baseFs, "/base/path") tDir, err := TempDir(bp, "/tmp", "") diff --git a/composite_test.go b/composite_test.go index 05605fb6..88fd49b9 100644 --- a/composite_test.go +++ b/composite_test.go @@ -54,12 +54,12 @@ func TestUnionCreateExisting(t *testing.T) { roBase := &ReadOnlyFs{source: base} ufs := NewCopyOnWriteFs(roBase, &MemMapFs{}) - base.MkdirAll("/home/test", 0777) + base.MkdirAll("/home/test", 0o777) fh, _ := base.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() - fh, err := ufs.OpenFile("/home/test/file.txt", os.O_RDWR, 0666) + fh, err := ufs.OpenFile("/home/test/file.txt", os.O_RDWR, 0o666) if err != nil { t.Errorf("Failed to open file r/w: %s", err) } @@ -95,7 +95,6 @@ func TestUnionCreateExisting(t *testing.T) { default: t.Errorf("Create failed on existing file") } - } func TestUnionMergeReaddir(t *testing.T) { @@ -104,7 +103,7 @@ func TestUnionMergeReaddir(t *testing.T) { ufs := &CopyOnWriteFs{base: roBase, layer: &MemMapFs{}} - base.MkdirAll("/home/test", 0777) + base.MkdirAll("/home/test", 0o777) fh, _ := base.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() @@ -130,12 +129,12 @@ func TestExistingDirectoryCollisionReaddir(t *testing.T) { ufs := &CopyOnWriteFs{base: roBase, layer: overlay} - base.MkdirAll("/home/test", 0777) + base.MkdirAll("/home/test", 0o777) fh, _ := base.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() - overlay.MkdirAll("home/test", 0777) + overlay.MkdirAll("home/test", 0o777) fh, _ = overlay.Create("/home/test/file2.txt") fh.WriteString("This is a test") fh.Close() @@ -170,7 +169,7 @@ func TestNestedDirBaseReaddir(t *testing.T) { ufs := &CopyOnWriteFs{base: roBase, layer: overlay} - base.MkdirAll("/home/test/foo/bar", 0777) + base.MkdirAll("/home/test/foo/bar", 0o777) fh, _ := base.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() @@ -182,7 +181,7 @@ func TestNestedDirBaseReaddir(t *testing.T) { fh.WriteString("This is a test") fh.Close() - overlay.MkdirAll("/", 0777) + overlay.MkdirAll("/", 0o777) // Opening something only in the base fh, _ = ufs.Open("/home/test/foo") @@ -205,8 +204,8 @@ func TestNestedDirOverlayReaddir(t *testing.T) { ufs := &CopyOnWriteFs{base: roBase, layer: overlay} - base.MkdirAll("/", 0777) - overlay.MkdirAll("/home/test/foo/bar", 0777) + base.MkdirAll("/", 0o777) + overlay.MkdirAll("/home/test/foo/bar", 0o777) fh, _ := overlay.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() @@ -239,8 +238,8 @@ func TestNestedDirOverlayOsFsReaddir(t *testing.T) { ufs := &CopyOnWriteFs{base: roBase, layer: overlay} - base.MkdirAll("/", 0777) - overlay.MkdirAll("/home/test/foo/bar", 0777) + base.MkdirAll("/", 0o777) + overlay.MkdirAll("/home/test/foo/bar", 0o777) fh, _ := overlay.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() @@ -274,12 +273,12 @@ func TestCopyOnWriteFsWithOsFs(t *testing.T) { ufs := &CopyOnWriteFs{base: roBase, layer: overlay} - base.MkdirAll("/home/test", 0777) + base.MkdirAll("/home/test", 0o777) fh, _ := base.Create("/home/test/file.txt") fh.WriteString("This is a test") fh.Close() - overlay.MkdirAll("home/test", 0777) + overlay.MkdirAll("home/test", 0o777) fh, _ = overlay.Create("/home/test/file2.txt") fh.WriteString("This is a test") fh.Close() @@ -315,7 +314,7 @@ func TestUnionCacheWrite(t *testing.T) { ufs := NewCacheOnReadFs(base, layer, 0) - base.Mkdir("/data", 0777) + base.Mkdir("/data", 0o777) fh, err := ufs.Create("/data/file.txt") if err != nil { @@ -344,7 +343,7 @@ func TestUnionCacheExpire(t *testing.T) { layer := &MemMapFs{} ufs := &CacheOnReadFs{base: base, layer: layer, cacheTime: 1 * time.Second} - base.Mkdir("/data", 0777) + base.Mkdir("/data", 0o777) fh, err := ufs.Create("/data/file.txt") if err != nil { @@ -449,7 +448,7 @@ func TestUnionFileReaddirDuplicateEmpty(t *testing.T) { // Overlay shares same empty directory as base overlay := NewMemMapFs() - err = overlay.Mkdir(dir, 0700) + err = overlay.Mkdir(dir, 0o700) if err != nil { t.Fatal(err) } @@ -479,7 +478,7 @@ func TestUnionFileReaddirAskForTooMany(t *testing.T) { const testFiles = 5 for i := 0; i < testFiles; i++ { - WriteFile(base, fmt.Sprintf("file%d.txt", i), []byte("afero"), 0777) + WriteFile(base, fmt.Sprintf("file%d.txt", i), []byte("afero"), 0o777) } ufs := &CopyOnWriteFs{base: base, layer: overlay} diff --git a/copyOnWriteFs.go b/copyOnWriteFs.go index 6ff8f309..184d6dd7 100644 --- a/copyOnWriteFs.go +++ b/copyOnWriteFs.go @@ -223,7 +223,7 @@ func (u *CopyOnWriteFs) OpenFile(name string, flag int, perm os.FileMode) (File, return nil, err } if isaDir { - if err = u.layer.MkdirAll(dir, 0777); err != nil { + if err = u.layer.MkdirAll(dir, 0o777); err != nil { return nil, err } return u.layer.OpenFile(name, flag, perm) @@ -247,8 +247,9 @@ func (u *CopyOnWriteFs) OpenFile(name string, flag int, perm os.FileMode) (File, // This function handles the 9 different possibilities caused // by the union which are the intersection of the following... -// layer: doesn't exist, exists as a file, and exists as a directory -// base: doesn't exist, exists as a file, and exists as a directory +// +// layer: doesn't exist, exists as a file, and exists as a directory +// base: doesn't exist, exists as a file, and exists as a directory func (u *CopyOnWriteFs) Open(name string) (File, error) { // Since the overlay overrides the base we check that first b, err := u.isBaseFile(name) @@ -322,5 +323,5 @@ func (u *CopyOnWriteFs) MkdirAll(name string, perm os.FileMode) error { } func (u *CopyOnWriteFs) Create(name string) (File, error) { - return u.OpenFile(name, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) + return u.OpenFile(name, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0o666) } diff --git a/copyOnWriteFs_test.go b/copyOnWriteFs_test.go index 94ba7255..c94ff1ea 100644 --- a/copyOnWriteFs_test.go +++ b/copyOnWriteFs_test.go @@ -16,9 +16,9 @@ func TestCopyOnWrite(t *testing.T) { compositeFs := NewCopyOnWriteFs(NewReadOnlyFs(NewOsFs()), osFs) - var dir = filepath.Join(writeDir, "some/path") + dir := filepath.Join(writeDir, "some/path") - err = compositeFs.MkdirAll(dir, 0744) + err = compositeFs.MkdirAll(dir, 0o744) if err != nil { t.Fatal(err) } @@ -31,13 +31,13 @@ func TestCopyOnWrite(t *testing.T) { // We want the composite file system to behave like the OS file system // on Mkdir and MkdirAll for _, fs := range []Fs{osFs, compositeFs} { - err = fs.Mkdir(dir, 0744) + err = fs.Mkdir(dir, 0o744) if err == nil || !os.IsExist(err) { t.Errorf("Mkdir: Got %q for %T", err, fs) } // MkdirAll does not return an error when the directory already exists - err = fs.MkdirAll(dir, 0744) + err = fs.MkdirAll(dir, 0o744) if err != nil { t.Errorf("MkdirAll: Got %q for %T", err, fs) } @@ -49,7 +49,7 @@ func TestCopyOnWriteFileInMemMapBase(t *testing.T) { base := &MemMapFs{} layer := &MemMapFs{} - if err := WriteFile(base, "base.txt", []byte("base"), 0755); err != nil { + if err := WriteFile(base, "base.txt", []byte("base"), 0o755); err != nil { t.Fatalf("Failed to write file: %s", err) } diff --git a/gcsfs/file.go b/gcsfs/file.go index f916bd22..671c0466 100644 --- a/gcsfs/file.go +++ b/gcsfs/file.go @@ -36,7 +36,7 @@ import ( // GcsFs is the Afero version adapted for GCS type GcsFile struct { openFlags int - fhOffset int64 //File handle specific offset + fhOffset int64 // File handle specific offset closed bool ReadDirIt stiface.ObjectIterator resource *gcsFileResource @@ -105,13 +105,13 @@ func (o *GcsFile) Seek(newOffset int64, whence int) (int64, error) { return 0, ErrFileClosed } - //Since this is an expensive operation; let's make sure we need it + // Since this is an expensive operation; let's make sure we need it if (whence == 0 && newOffset == o.fhOffset) || (whence == 1 && newOffset == 0) { return o.fhOffset, nil } log.Printf("WARNING: Seek behavior triggered, highly inefficent. Offset before seek is at %d\n", o.fhOffset) - //Fore the reader/writers to be reopened (at correct offset) + // Fore the reader/writers to be reopened (at correct offset) err := o.Sync() if err != nil { return 0, err @@ -197,7 +197,7 @@ func (o *GcsFile) readdirImpl(count int) ([]*FileInfo, error) { path := o.resource.fs.ensureTrailingSeparator(o.resource.name) if o.ReadDirIt == nil { - //log.Printf("Querying path : %s\n", path) + // log.Printf("Querying path : %s\n", path) bucketName, bucketPath := o.resource.fs.splitName(path) o.ReadDirIt = o.resource.fs.client.Bucket(bucketName).Objects( @@ -246,7 +246,7 @@ func (o *GcsFile) readdirImpl(count int) ([]*FileInfo, error) { // break //} } - //return res, nil + // return res, nil } func (o *GcsFile) Readdir(count int) ([]os.FileInfo, error) { diff --git a/gcsfs/file_info.go b/gcsfs/file_info.go index 0a42d38f..92e30460 100644 --- a/gcsfs/file_info.go +++ b/gcsfs/file_info.go @@ -110,6 +110,7 @@ func (fi *FileInfo) Name() string { func (fi *FileInfo) Size() int64 { return fi.size } + func (fi *FileInfo) Mode() os.FileMode { if fi.IsDir() { return os.ModeDir | fi.fileMode diff --git a/gcsfs/file_resource.go b/gcsfs/file_resource.go index c25ad51d..06d5c544 100644 --- a/gcsfs/file_resource.go +++ b/gcsfs/file_resource.go @@ -146,7 +146,7 @@ func (o *gcsFileResource) ReadAt(p []byte, off int64) (n int, err error) { return 0, err } - //Then read at the correct offset. + // Then read at the correct offset. r, err := o.obj.NewRangeReader(o.ctx, off, -1) if err != nil { return 0, err @@ -160,7 +160,7 @@ func (o *gcsFileResource) ReadAt(p []byte, off int64) (n int, err error) { } func (o *gcsFileResource) WriteAt(b []byte, off int64) (n int, err error) { - //If the writer is opened and at the correct offset we're good! + // If the writer is opened and at the correct offset we're good! if off == o.offset && o.writer != nil { n, err = o.writer.Write(b) o.offset += int64(n) @@ -251,7 +251,7 @@ func (o *gcsFileResource) Truncate(wantedSize int64) error { } for written < wantedSize { - //Bulk up padding writes + // Bulk up padding writes paddingBytes := bytes.Repeat([]byte(" "), min(maxWriteSize, int(wantedSize-written))) n := 0 diff --git a/gcsfs/fs.go b/gcsfs/fs.go index 9b0dde44..b2a78fcc 100644 --- a/gcsfs/fs.go +++ b/gcsfs/fs.go @@ -29,7 +29,7 @@ import ( ) const ( - defaultFileMode = 0755 + defaultFileMode = 0o755 gsPrefix = "gs://" ) @@ -42,7 +42,7 @@ type Fs struct { buckets map[string]stiface.BucketHandle rawGcsObjects map[string]*GcsFile - autoRemoveEmptyFolders bool //trigger for creating "virtual folders" (not required by GCSs) + autoRemoveEmptyFolders bool // trigger for creating "virtual folders" (not required by GCSs) } func NewGcsFs(ctx context.Context, client stiface.Client) *Fs { @@ -71,6 +71,7 @@ func (fs *Fs) ensureTrailingSeparator(s string) string { } return s } + func (fs *Fs) ensureNoLeadingSeparator(s string) string { if len(s) > 0 && strings.HasPrefix(s, fs.separator) { s = s[len(fs.separator):] @@ -200,7 +201,7 @@ func (fs *Fs) MkdirAll(path string, perm os.FileMode) error { if f == "" && i != 0 { continue // it's the last item - it should be empty } - //Don't force a delimiter prefix + // Don't force a delimiter prefix if root != "" { root = root + fs.separator + f } else { diff --git a/gcsfs/gcs.go b/gcsfs/gcs.go index 78cc924f..c94b1421 100644 --- a/gcsfs/gcs.go +++ b/gcsfs/gcs.go @@ -76,39 +76,51 @@ func NewGcsFSFromClientWithSeparator(ctx context.Context, client *storage.Client func (fs *GcsFs) Name() string { return fs.source.Name() } + func (fs *GcsFs) Create(name string) (afero.File, error) { return fs.source.Create(name) } + func (fs *GcsFs) Mkdir(name string, perm os.FileMode) error { return fs.source.Mkdir(name, perm) } + func (fs *GcsFs) MkdirAll(path string, perm os.FileMode) error { return fs.source.MkdirAll(path, perm) } + func (fs *GcsFs) Open(name string) (afero.File, error) { return fs.source.Open(name) } + func (fs *GcsFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { return fs.source.OpenFile(name, flag, perm) } + func (fs *GcsFs) Remove(name string) error { return fs.source.Remove(name) } + func (fs *GcsFs) RemoveAll(path string) error { return fs.source.RemoveAll(path) } + func (fs *GcsFs) Rename(oldname, newname string) error { return fs.source.Rename(oldname, newname) } + func (fs *GcsFs) Stat(name string) (os.FileInfo, error) { return fs.source.Stat(name) } + func (fs *GcsFs) Chmod(name string, mode os.FileMode) error { return fs.source.Chmod(name, mode) } + func (fs *GcsFs) Chtimes(name string, atime time.Time, mtime time.Time) error { return fs.source.Chtimes(name, atime, mtime) } + func (fs *GcsFs) Chown(name string, uid, gid int) error { return fs.source.Chown(name, uid, gid) } diff --git a/gcsfs/gcs_mocks.go b/gcsfs/gcs_mocks.go index 272eb9f1..b71b2924 100644 --- a/gcsfs/gcs_mocks.go +++ b/gcsfs/gcs_mocks.go @@ -166,7 +166,7 @@ func (w *writerMock) Close() error { if w.file == nil { var err error if strings.HasSuffix(w.name, "/") { - err = w.fs.Mkdir(w.name, 0755) + err = w.fs.Mkdir(w.name, 0o755) if err != nil { return err } diff --git a/gcsfs/gcs_test.go b/gcsfs/gcs_test.go index feedfb0d..67794be5 100644 --- a/gcsfs/gcs_test.go +++ b/gcsfs/gcs_test.go @@ -122,7 +122,7 @@ func TestMain(m *testing.M) { gcsAfs = &afero.Afero{Fs: &GcsFs{NewGcsFs(ctx, mockClient)}} // Uncomment to use the real, not mocked, client - //gcsAfs = &Afero{Fs: &GcsFs{gcsfs.NewGcsFs(ctx, client)}} + // gcsAfs = &Afero{Fs: &GcsFs{gcsfs.NewGcsFs(ctx, client)}} exitCode = m.Run() } @@ -341,7 +341,7 @@ func TestGcsSeek(t *testing.T) { t.Fatalf("opening %v: %v", name, err) } - var tests = []struct { + tests := []struct { offIn int64 whence int offOut int64 @@ -477,7 +477,7 @@ func TestGcsOpenFile(t *testing.T) { } for _, name := range names { - file, err := gcsAfs.OpenFile(name, os.O_RDONLY, 0400) + file, err := gcsAfs.OpenFile(name, os.O_RDONLY, 0o400) if !f.exists { if (f.name != "" && !errors.Is(err, syscall.ENOENT)) || (f.name == "" && !errors.Is(err, ErrNoBucketInName)) { @@ -496,7 +496,7 @@ func TestGcsOpenFile(t *testing.T) { t.Fatalf("failed to close a file \"%s\": %s", name, err) } - _, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600) + _, err = gcsAfs.OpenFile(name, os.O_CREATE, 0o600) if !errors.Is(err, syscall.EPERM) { t.Errorf("%v: open for write: got %v, expected %v", name, err, syscall.EPERM) } @@ -714,7 +714,7 @@ func TestGcsMkdir(t *testing.T) { t.Run("empty", func(t *testing.T) { emptyDirName := bucketName - err := gcsAfs.Mkdir(emptyDirName, 0755) + err := gcsAfs.Mkdir(emptyDirName, 0o755) if err == nil { t.Fatal("did not fail upon creation of an empty folder") } @@ -723,7 +723,7 @@ func TestGcsMkdir(t *testing.T) { dirName := filepath.Join(bucketName, "a-test-dir") var err error - err = gcsAfs.Mkdir(dirName, 0755) + err = gcsAfs.Mkdir(dirName, 0o755) if err != nil { t.Fatal("failed to create a folder with error", err) } @@ -739,7 +739,7 @@ func TestGcsMkdir(t *testing.T) { t.Errorf("%s: mode is not directory", dirName) } - if info.Mode() != os.ModeDir|0755 { + if info.Mode() != os.ModeDir|0o755 { t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", dirName, info.Mode()) } @@ -754,7 +754,7 @@ func TestGcsMkdirAll(t *testing.T) { t.Run("empty", func(t *testing.T) { emptyDirName := bucketName - err := gcsAfs.MkdirAll(emptyDirName, 0755) + err := gcsAfs.MkdirAll(emptyDirName, 0o755) if err == nil { t.Fatal("did not fail upon creation of an empty folder") } @@ -762,7 +762,7 @@ func TestGcsMkdirAll(t *testing.T) { t.Run("success", func(t *testing.T) { dirName := filepath.Join(bucketName, "a/b/c") - err := gcsAfs.MkdirAll(dirName, 0755) + err := gcsAfs.MkdirAll(dirName, 0o755) if err != nil { t.Fatal(err) } @@ -774,7 +774,7 @@ func TestGcsMkdirAll(t *testing.T) { if !info.Mode().IsDir() { t.Errorf("%s: mode is not directory", filepath.Join(bucketName, "a")) } - if info.Mode() != os.ModeDir|0755 { + if info.Mode() != os.ModeDir|0o755 { t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", filepath.Join(bucketName, "a"), info.Mode()) } info, err = gcsAfs.Stat(filepath.Join(bucketName, "a/b")) @@ -784,7 +784,7 @@ func TestGcsMkdirAll(t *testing.T) { if !info.Mode().IsDir() { t.Errorf("%s: mode is not directory", filepath.Join(bucketName, "a/b")) } - if info.Mode() != os.ModeDir|0755 { + if info.Mode() != os.ModeDir|0o755 { t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", filepath.Join(bucketName, "a/b"), info.Mode()) } info, err = gcsAfs.Stat(dirName) @@ -794,7 +794,7 @@ func TestGcsMkdirAll(t *testing.T) { if !info.Mode().IsDir() { t.Errorf("%s: mode is not directory", dirName) } - if info.Mode() != os.ModeDir|0755 { + if info.Mode() != os.ModeDir|0o755 { t.Errorf("%s: wrong permissions, expected drwxr-xr-x, got %s", dirName, info.Mode()) } @@ -816,7 +816,7 @@ func TestGcsRemoveAll(t *testing.T) { aDir := filepath.Join(bucketName, "a") bDir := filepath.Join(aDir, "b") - err := gcsAfs.MkdirAll(bDir, 0755) + err := gcsAfs.MkdirAll(bDir, 0o755) if err != nil { t.Fatal(err) } diff --git a/iofs_test.go b/iofs_test.go index 579345e1..cd940f22 100644 --- a/iofs_test.go +++ b/iofs_test.go @@ -66,7 +66,6 @@ func TestIOFS(t *testing.T) { t.Error(err) } }) - } func TestIOFSNativeDirEntryWhenPossible(t *testing.T) { @@ -145,7 +144,6 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) { } return nil - }) if err != nil { @@ -155,7 +153,6 @@ func TestIOFSNativeDirEntryWhenPossible(t *testing.T) { if fileCount != numFiles { t.Fatalf("expected %d, got %d", numFiles, fileCount) } - } func TestFromIOFS(t *testing.T) { @@ -360,7 +357,6 @@ func TestFromIOFS_File(t *testing.T) { // MapFS files implements io.ReaderAt b := make([]byte, 2) _, err := file.ReadAt(b, 2) - if err != nil { t.Errorf("ReadAt failed: %v", err) return @@ -420,7 +416,7 @@ func TestFromIOFS_File(t *testing.T) { return } - var expectedItems = []struct { + expectedItems := []struct { Name string IsDir bool Size int64 @@ -472,7 +468,7 @@ func TestFromIOFS_File(t *testing.T) { return } - var expectedItems = []string{"dir1", "dir2", "test.txt"} + expectedItems := []string{"dir1", "dir2", "test.txt"} if len(expectedItems) != len(items) { t.Errorf("Items count mismatch, expected %d, got %d", len(expectedItems), len(items)) @@ -530,7 +526,7 @@ func BenchmarkWalkDir(b *testing.B) { dirname := "" for i := 0; i < level; i++ { dirname = filepath.Join(dirname, fmt.Sprintf("dir%d", i)) - err := osfs.MkdirAll(dirname, 0755) + err := osfs.MkdirAll(dirname, 0o755) if err != nil && !os.IsExist(err) { b.Fatal(err) } @@ -547,12 +543,9 @@ func BenchmarkWalkDir(b *testing.B) { return err } return nil - }) - if err != nil { b.Fatal(err) } } - } diff --git a/ioutil.go b/ioutil.go index 386c9cdc..fa6abe1e 100644 --- a/ioutil.go +++ b/ioutil.go @@ -141,8 +141,10 @@ func WriteFile(fs Fs, filename string, data []byte, perm os.FileMode) error { // We generate random temporary file names so that there's a good // chance the file doesn't exist yet - keeps the number of tries in // TempFile to a minimum. -var randNum uint32 -var randmu sync.Mutex +var ( + randNum uint32 + randmu sync.Mutex +) func reseed() uint32 { return uint32(time.Now().UnixNano() + int64(os.Getpid())) @@ -190,7 +192,7 @@ func TempFile(fs Fs, dir, pattern string) (f File, err error) { nconflict := 0 for i := 0; i < 10000; i++ { name := filepath.Join(dir, prefix+nextRandom()+suffix) - f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) + f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600) if os.IsExist(err) { if nconflict++; nconflict > 10 { randmu.Lock() @@ -214,6 +216,7 @@ func TempFile(fs Fs, dir, pattern string) (f File, err error) { func (a Afero) TempDir(dir, prefix string) (name string, err error) { return TempDir(a.Fs, dir, prefix) } + func TempDir(fs Fs, dir, prefix string) (name string, err error) { if dir == "" { dir = os.TempDir() @@ -222,7 +225,7 @@ func TempDir(fs Fs, dir, prefix string) (name string, err error) { nconflict := 0 for i := 0; i < 10000; i++ { try := filepath.Join(dir, prefix+nextRandom()) - err = fs.Mkdir(try, 0700) + err = fs.Mkdir(try, 0o700) if os.IsExist(err) { if nconflict++; nconflict > 10 { randmu.Lock() diff --git a/ioutil_test.go b/ioutil_test.go index 540bf56a..004c66ba 100644 --- a/ioutil_test.go +++ b/ioutil_test.go @@ -63,7 +63,7 @@ func TestWriteFile(t *testing.T) { "build bigger and better idiot-proof programs, and the Universe trying " + "to produce bigger and better idiots. So far, the Universe is winning." - if err := fsutil.WriteFile(filename, []byte(data), 0644); err != nil { + if err := fsutil.WriteFile(filename, []byte(data), 0o644); err != nil { t.Fatalf("WriteFile %s: %v", filename, err) } @@ -83,7 +83,7 @@ func TestWriteFile(t *testing.T) { func TestReadDir(t *testing.T) { testFS = &MemMapFs{} - testFS.Mkdir("/i-am-a-dir", 0777) + testFS.Mkdir("/i-am-a-dir", 0o777) testFS.Create("/this_exists.go") dirname := "rumpelstilzchen" _, err := ReadDir(testFS, dirname) diff --git a/lstater_test.go b/lstater_test.go index 477924f6..cb5585f9 100644 --- a/lstater_test.go +++ b/lstater_test.go @@ -50,8 +50,8 @@ func TestLstatIfPossible(t *testing.T) { pathFileMem := filepath.Join(memWorkDir, "aferom.txt") - WriteFile(osFs, filepath.Join(workDir, "afero.txt"), []byte("Hi, Afero!"), 0777) - WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777) + WriteFile(osFs, filepath.Join(workDir, "afero.txt"), []byte("Hi, Afero!"), 0o777) + WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0o777) os.Chdir(workDir) if err := os.Symlink("afero.txt", "symafero.txt"); err != nil { diff --git a/match_test.go b/match_test.go index fa2c17b5..3f24b58d 100644 --- a/match_test.go +++ b/match_test.go @@ -44,7 +44,7 @@ func setupGlobDirReusePath(t *testing.T, fs Fs, path string) string { func setupGlobFiles(t *testing.T, fs Fs, path string) string { testSubDir := filepath.Join(path, "globs", "bobs") - err := fs.MkdirAll(testSubDir, 0700) + err := fs.MkdirAll(testSubDir, 0o700) if err != nil && !os.IsExist(err) { t.Fatal(err) } @@ -84,7 +84,7 @@ func TestGlob(t *testing.T) { } } - var globTests = []struct { + globTests := []struct { pattern, result string }{ {testDir + "/globs/bobs/matcher", testDir + "/globs/bobs/matcher"}, @@ -136,7 +136,7 @@ func TestGlobSymlink(t *testing.T) { t.Skipf("skipping on %s", runtime.GOOS) } - var globSymlinkTests = []struct { + globSymlinkTests := []struct { path, dest string brokenLink bool }{ diff --git a/mem/file.go b/mem/file.go index 3cf4693b..62fe4498 100644 --- a/mem/file.go +++ b/mem/file.go @@ -245,7 +245,7 @@ func (f *File) Truncate(size int64) error { defer f.fileData.Unlock() if size > int64(len(f.fileData.data)) { diff := size - int64(len(f.fileData.data)) - f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...) + f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{0o0}, int(diff))...) } else { f.fileData.data = f.fileData.data[0:size] } @@ -285,7 +285,7 @@ func (f *File) Write(b []byte) (n int, err error) { tail = f.fileData.data[n+int(cur):] } if diff > 0 { - f.fileData.data = append(f.fileData.data, append(bytes.Repeat([]byte{00}, int(diff)), b...)...) + f.fileData.data = append(f.fileData.data, append(bytes.Repeat([]byte{0o0}, int(diff)), b...)...) f.fileData.data = append(f.fileData.data, tail...) } else { f.fileData.data = append(f.fileData.data[:cur], b...) @@ -321,16 +321,19 @@ func (s *FileInfo) Name() string { s.Unlock() return name } + func (s *FileInfo) Mode() os.FileMode { s.Lock() defer s.Unlock() return s.mode } + func (s *FileInfo) ModTime() time.Time { s.Lock() defer s.Unlock() return s.modtime } + func (s *FileInfo) IsDir() bool { s.Lock() defer s.Unlock() diff --git a/mem/file_test.go b/mem/file_test.go index 998a5d0b..2b123f32 100644 --- a/mem/file_test.go +++ b/mem/file_test.go @@ -66,8 +66,8 @@ func TestFileDataModTimeRace(t *testing.T) { func TestFileDataModeRace(t *testing.T) { t.Parallel() - const someMode = 0777 - const someOtherMode = 0660 + const someMode = 0o777 + const someOtherMode = 0o660 d := FileData{ mode: someMode, @@ -167,7 +167,7 @@ func TestFileDataIsDirRace(t *testing.T) { s.Unlock() }() - //just logging the value to trigger a read: + // just logging the value to trigger a read: t.Logf("Value is %v", s.IsDir()) } @@ -196,10 +196,10 @@ func TestFileDataSizeRace(t *testing.T) { s.Unlock() }() - //just logging the value to trigger a read: + // just logging the value to trigger a read: t.Logf("Value is %v", s.Size()) - //Testing the Dir size case + // Testing the Dir size case d.dir = true if s.Size() != int64(42) { t.Errorf("Failed to read correct value for dir, was %v", s.Size()) diff --git a/memmap.go b/memmap.go index 5cfc0a69..3f4ef42d 100644 --- a/memmap.go +++ b/memmap.go @@ -43,7 +43,7 @@ func (m *MemMapFs) getData() map[string]*mem.FileData { // Root should always exist, right? // TODO: what about windows? root := mem.CreateDir(FilePathSeparator) - mem.SetMode(root, os.ModeDir|0755) + mem.SetMode(root, os.ModeDir|0o755) m.data[FilePathSeparator] = root }) return m.data @@ -96,12 +96,12 @@ func (m *MemMapFs) registerWithParent(f *mem.FileData, perm os.FileMode) { pdir := filepath.Dir(filepath.Clean(f.Name())) err := m.lockfreeMkdir(pdir, perm) if err != nil { - //log.Println("Mkdir error:", err) + // log.Println("Mkdir error:", err) return } parent, err = m.lockfreeOpen(pdir) if err != nil { - //log.Println("Open after Mkdir error:", err) + // log.Println("Open after Mkdir error:", err) return } } diff --git a/memmap_test.go b/memmap_test.go index 318e95ff..ee75672c 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -40,7 +40,7 @@ func TestPathErrors(t *testing.T) { path := filepath.Join(".", "some", "path") path2 := filepath.Join(".", "different", "path") fs := NewMemMapFs() - perm := os.FileMode(0755) + perm := os.FileMode(0o755) uid := 1000 gid := 1000 @@ -115,7 +115,7 @@ func checkPathError(t *testing.T, err error, op string) { // Ensure os.O_EXCL is correctly handled. func TestOpenFileExcl(t *testing.T) { const fileName = "/myFileTest" - const fileMode = os.FileMode(0765) + const fileMode = os.FileMode(0o765) fs := NewMemMapFs() @@ -141,7 +141,7 @@ func TestPermSet(t *testing.T) { const dirPath = "/myDirTest" const dirPathAll = "/my/path/to/dir" - const fileMode = os.FileMode(0765) + const fileMode = os.FileMode(0o765) // directories will also have the directory bit set const dirMode = fileMode | os.ModeDir @@ -204,7 +204,7 @@ func TestMultipleOpenFiles(t *testing.T) { defer removeAllTestFiles(t) const fileName = "afero-demo2.txt" - var data = make([][]byte, len(Fss)) + data := make([][]byte, len(Fss)) for i, fs := range Fss { dir := testDir(fs) @@ -222,7 +222,7 @@ func TestMultipleOpenFiles(t *testing.T) { t.Error(err) } - fh2, err := fs.OpenFile(path, os.O_RDWR, 0777) + fh2, err := fs.OpenFile(path, os.O_RDWR, 0o777) if err != nil { t.Error("fs.OpenFile failed: " + err.Error()) } @@ -295,7 +295,7 @@ func TestReadOnly(t *testing.T) { } f.Close() - f, err = fs.OpenFile(path, os.O_RDONLY, 0644) + f, err = fs.OpenFile(path, os.O_RDONLY, 0o644) if err != nil { t.Error("fs.Open failed: " + err.Error()) } @@ -386,7 +386,7 @@ func TestMemFsDataRace(t *testing.T) { const dir = "test_dir" fs := NewMemMapFs() - if err := fs.MkdirAll(dir, 0777); err != nil { + if err := fs.MkdirAll(dir, 0o777); err != nil { t.Fatal(err) } @@ -397,7 +397,7 @@ func TestMemFsDataRace(t *testing.T) { defer close(done) for i := 0; i < n; i++ { fname := filepath.Join(dir, fmt.Sprintf("%d.txt", i)) - if err := WriteFile(fs, fname, []byte(""), 0777); err != nil { + if err := WriteFile(fs, fname, []byte(""), 0o777); err != nil { panic(err) } if err := fs.Remove(fname); err != nil { @@ -442,7 +442,7 @@ func TestMemFsMkdirAllMode(t *testing.T) { t.Parallel() fs := NewMemMapFs() - err := fs.MkdirAll("/a/b/c", 0755) + err := fs.MkdirAll("/a/b/c", 0o755) if err != nil { t.Fatal(err) } @@ -456,7 +456,7 @@ func TestMemFsMkdirAllMode(t *testing.T) { if !info.ModTime().After(time.Now().Add(-1 * time.Hour)) { t.Errorf("/a: mod time not set, got %s", info.ModTime()) } - if info.Mode() != os.FileMode(os.ModeDir|0755) { + if info.Mode() != os.FileMode(os.ModeDir|0o755) { t.Errorf("/a: wrong permissions, expected drwxr-xr-x, got %s", info.Mode()) } info, err = fs.Stat("/a/b") @@ -466,7 +466,7 @@ func TestMemFsMkdirAllMode(t *testing.T) { if !info.Mode().IsDir() { t.Error("/a/b: mode is not directory") } - if info.Mode() != os.FileMode(os.ModeDir|0755) { + if info.Mode() != os.FileMode(os.ModeDir|0o755) { t.Errorf("/a/b: wrong permissions, expected drwxr-xr-x, got %s", info.Mode()) } if !info.ModTime().After(time.Now().Add(-1 * time.Hour)) { @@ -479,7 +479,7 @@ func TestMemFsMkdirAllMode(t *testing.T) { if !info.Mode().IsDir() { t.Error("/a/b/c: mode is not directory") } - if info.Mode() != os.FileMode(os.ModeDir|0755) { + if info.Mode() != os.FileMode(os.ModeDir|0o755) { t.Errorf("/a/b/c: wrong permissions, expected drwxr-xr-x, got %s", info.Mode()) } if !info.ModTime().After(time.Now().Add(-1 * time.Hour)) { @@ -492,7 +492,7 @@ func TestMemFsMkdirAllNoClobber(t *testing.T) { t.Parallel() fs := NewMemMapFs() - err := fs.MkdirAll("/a/b/c", 0755) + err := fs.MkdirAll("/a/b/c", 0o755) if err != nil { t.Fatal(err) } @@ -500,10 +500,10 @@ func TestMemFsMkdirAllNoClobber(t *testing.T) { if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(os.ModeDir|0755) { + if info.Mode() != os.FileMode(os.ModeDir|0o755) { t.Errorf("/a/b: wrong permissions, expected drwxr-xr-x, got %s", info.Mode()) } - err = fs.MkdirAll("/a/b/c/d/e/f", 0710) + err = fs.MkdirAll("/a/b/c/d/e/f", 0o710) // '/a/b' is unchanged if err != nil { t.Fatal(err) @@ -512,7 +512,7 @@ func TestMemFsMkdirAllNoClobber(t *testing.T) { if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(os.ModeDir|0755) { + if info.Mode() != os.FileMode(os.ModeDir|0o755) { t.Errorf("/a/b: wrong permissions, expected drwxr-xr-x, got %s", info.Mode()) } // new directories created with proper permissions @@ -520,32 +520,32 @@ func TestMemFsMkdirAllNoClobber(t *testing.T) { if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(os.ModeDir|0710) { + if info.Mode() != os.FileMode(os.ModeDir|0o710) { t.Errorf("/a/b/c/d: wrong permissions, expected drwx--x---, got %s", info.Mode()) } info, err = fs.Stat("/a/b/c/d/e") if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(os.ModeDir|0710) { + if info.Mode() != os.FileMode(os.ModeDir|0o710) { t.Errorf("/a/b/c/d/e: wrong permissions, expected drwx--x---, got %s", info.Mode()) } info, err = fs.Stat("/a/b/c/d/e/f") if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(os.ModeDir|0710) { + if info.Mode() != os.FileMode(os.ModeDir|0o710) { t.Errorf("/a/b/c/d/e/f: wrong permissions, expected drwx--x---, got %s", info.Mode()) } } func TestMemFsDirMode(t *testing.T) { fs := NewMemMapFs() - err := fs.Mkdir("/testDir1", 0644) + err := fs.Mkdir("/testDir1", 0o644) if err != nil { t.Error(err) } - err = fs.MkdirAll("/sub/testDir2", 0644) + err = fs.MkdirAll("/sub/testDir2", 0o644) if err != nil { t.Error(err) } @@ -576,7 +576,7 @@ func TestMemFsUnexpectedEOF(t *testing.T) { fs := NewMemMapFs() - if err := WriteFile(fs, "file.txt", []byte("abc"), 0777); err != nil { + if err := WriteFile(fs, "file.txt", []byte("abc"), 0o777); err != nil { t.Fatal(err) } @@ -605,7 +605,7 @@ func TestMemFsChmod(t *testing.T) { fs := NewMemMapFs() const file = "hello" - if err := fs.Mkdir(file, 0700); err != nil { + if err := fs.Mkdir(file, 0o700); err != nil { t.Fatal(err) } @@ -636,7 +636,7 @@ func TestMemFsMkdirModeIllegal(t *testing.T) { t.Parallel() fs := NewMemMapFs() - err := fs.Mkdir("/a", os.ModeSocket|0755) + err := fs.Mkdir("/a", os.ModeSocket|0o755) if err != nil { t.Fatal(err) } @@ -644,7 +644,7 @@ func TestMemFsMkdirModeIllegal(t *testing.T) { if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(os.ModeDir|0755) { + if info.Mode() != os.FileMode(os.ModeDir|0o755) { t.Fatalf("should not be able to use Mkdir to set illegal mode: %s", info.Mode().String()) } } @@ -654,7 +654,7 @@ func TestMemFsOpenFileModeIllegal(t *testing.T) { t.Parallel() fs := NewMemMapFs() - file, err := fs.OpenFile("/a", os.O_CREATE, os.ModeSymlink|0644) + file, err := fs.OpenFile("/a", os.O_CREATE, os.ModeSymlink|0o644) if err != nil { t.Fatal(err) } @@ -663,7 +663,7 @@ func TestMemFsOpenFileModeIllegal(t *testing.T) { if err != nil { t.Fatal(err) } - if info.Mode() != os.FileMode(0644) { + if info.Mode() != os.FileMode(0o644) { t.Fatalf("should not be able to use OpenFile to set illegal mode: %s", info.Mode().String()) } } @@ -717,7 +717,7 @@ func TestMemMapFsConfurrentMkdir(t *testing.T) { go func() { defer wg.Done() - if err := mfs.MkdirAll(filepath.Dir(fp), 0755); err != nil { + if err := mfs.MkdirAll(filepath.Dir(fp), 0o755); err != nil { t.Error(err) } @@ -785,7 +785,7 @@ func TestMemFsRenameDir(t *testing.T) { fs := NewMemMapFs() - err := fs.MkdirAll(srcPath+FilePathSeparator+subDir, 0777) + err := fs.MkdirAll(srcPath+FilePathSeparator+subDir, 0o777) if err != nil { t.Fatalf("MkDirAll failed: %s", err) } @@ -823,12 +823,12 @@ func TestMemFsRenameDir(t *testing.T) { t.Fatalf("SubFile stat in the destination dir: %s", err) } - err = fs.Mkdir(srcPath, 0777) + err = fs.Mkdir(srcPath, 0o777) if err != nil { t.Fatalf("Cannot recreate the source dir: %s", err) } - err = fs.Mkdir(srcPath+FilePathSeparator+subDir, 0777) + err = fs.Mkdir(srcPath+FilePathSeparator+subDir, 0o777) if err != nil { t.Errorf("Cannot recreate the subdir in the source dir: %s", err) } diff --git a/regexpfs.go b/regexpfs.go index ac359c62..218f3b23 100644 --- a/regexpfs.go +++ b/regexpfs.go @@ -10,7 +10,6 @@ import ( // The RegexpFs filters files (not directories) by regular expression. Only // files matching the given regexp will be allowed, all others get a ENOENT error ( // "No such file or directory"). -// type RegexpFs struct { re *regexp.Regexp source Fs diff --git a/ro_regexp_test.go b/ro_regexp_test.go index ef3a14f1..3dc8930b 100644 --- a/ro_regexp_test.go +++ b/ro_regexp_test.go @@ -54,7 +54,6 @@ func TestFilterRegexp(t *testing.T) { fs := NewRegexpFs(&MemMapFs{}, regexp.MustCompile(`\.txt$`)) _, err := fs.Create("/file.html") if err == nil { - t.Errorf("Did not fail to create file") } // t.Logf("ERR=%s", err) @@ -75,7 +74,7 @@ func TestFilterRegexReadDir(t *testing.T) { fs1 := &RegexpFs{re: regexp.MustCompile(`\.txt$`), source: mfs} fs := &RegexpFs{re: regexp.MustCompile(`^a`), source: fs1} - mfs.MkdirAll("/dir/sub", 0777) + mfs.MkdirAll("/dir/sub", 0o777) for _, name := range []string{"afile.txt", "afile.html", "bfile.txt"} { for _, dir := range []string{"/dir/", "/dir/sub/"} { fh, _ := mfs.Create(dir + name) diff --git a/sftpfs/sftp_test.go b/sftpfs/sftp_test.go index a8651392..bba2f361 100644 --- a/sftpfs/sftp_test.go +++ b/sftpfs/sftp_test.go @@ -234,11 +234,11 @@ func MakeSSHKeyPair(bits int, pubKeyPath, privateKeyPath string) error { return err } - return ioutil.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0655) + return ioutil.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0o655) } func TestSftpCreate(t *testing.T) { - os.Mkdir("./test", 0777) + os.Mkdir("./test", 0o777) MakeSSHKeyPair(1024, "./test/id_rsa.pub", "./test/id_rsa") go RunSftpServer("./test/") @@ -250,12 +250,12 @@ func TestSftpCreate(t *testing.T) { } defer ctx.Disconnect() - var fs = New(ctx.sftpc) + fs := New(ctx.sftpc) - fs.MkdirAll("test/dir1/dir2/dir3", os.FileMode(0777)) - fs.Mkdir("test/foo", os.FileMode(0000)) - fs.Chmod("test/foo", os.FileMode(0700)) - fs.Mkdir("test/bar", os.FileMode(0777)) + fs.MkdirAll("test/dir1/dir2/dir3", os.FileMode(0o777)) + fs.Mkdir("test/foo", os.FileMode(0o000)) + fs.Chmod("test/foo", os.FileMode(0o700)) + fs.Mkdir("test/bar", os.FileMode(0o777)) file, err := fs.Create("file1") if err != nil { diff --git a/symlink.go b/symlink.go index d1c6ea53..aa6ae125 100644 --- a/symlink.go +++ b/symlink.go @@ -21,9 +21,9 @@ import ( // filesystems saying so. // It indicates support for 3 symlink related interfaces that implement the // behaviors of the os methods: -// - Lstat -// - Symlink, and -// - Readlink +// - Lstat +// - Symlink, and +// - Readlink type Symlinker interface { Lstater Linker diff --git a/symlink_test.go b/symlink_test.go index 5a9e5db6..77dd742f 100644 --- a/symlink_test.go +++ b/symlink_test.go @@ -38,14 +38,14 @@ func TestSymlinkIfPossible(t *testing.T) { pathFileMem := filepath.Join(memWorkDir, "aferom.txt") osPath := filepath.Join(workDir, "afero.txt") - WriteFile(osFs, osPath, []byte("Hi, Afero!"), 0777) - WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777) + WriteFile(osFs, osPath, []byte("Hi, Afero!"), 0o777) + WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0o777) testLink := func(l Linker, source, destination string, output *string) { if fs, ok := l.(Fs); ok { dir := filepath.Dir(destination) if dir != "" { - fs.MkdirAll(dir, 0777) + fs.MkdirAll(dir, 0o777) } } @@ -117,14 +117,14 @@ func TestReadlinkIfPossible(t *testing.T) { pathFileMem := filepath.Join(memWorkDir, "aferom.txt") osPath := filepath.Join(workDir, "afero.txt") - WriteFile(osFs, osPath, []byte("Hi, Afero!"), 0777) - WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777) + WriteFile(osFs, osPath, []byte("Hi, Afero!"), 0o777) + WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0o777) createLink := func(l Linker, source, destination string) error { if fs, ok := l.(Fs); ok { dir := filepath.Dir(destination) if dir != "" { - fs.MkdirAll(dir, 0777) + fs.MkdirAll(dir, 0o777) } } diff --git a/tarfs/tarfs_test.go b/tarfs/tarfs_test.go index aa2d6a41..228ed9b4 100644 --- a/tarfs/tarfs_test.go +++ b/tarfs/tarfs_test.go @@ -161,7 +161,7 @@ func TestSeek(t *testing.T) { t.Fatalf("opening %v: %v", f.name, err) } - var tests = []struct { + tests := []struct { offin int64 whence int offout int64 @@ -252,7 +252,7 @@ func TestClose(t *testing.T) { func TestOpenFile(t *testing.T) { for _, f := range files { - file, err := afs.OpenFile(f.name, os.O_RDONLY, 0400) + file, err := afs.OpenFile(f.name, os.O_RDONLY, 0o400) if !f.exists { if !errors.Is(err, syscall.ENOENT) { t.Errorf("%v: got %v, expected%v", f.name, err, syscall.ENOENT) @@ -266,7 +266,7 @@ func TestOpenFile(t *testing.T) { } file.Close() - _, err = afs.OpenFile(f.name, os.O_CREATE, 0600) + _, err = afs.OpenFile(f.name, os.O_CREATE, 0o600) if !errors.Is(err, syscall.EPERM) { t.Errorf("%v: open for write: got %v, expected %v", f.name, err, syscall.EPERM) } diff --git a/unionFile.go b/unionFile.go index 333d367f..f02e7553 100644 --- a/unionFile.go +++ b/unionFile.go @@ -130,7 +130,7 @@ func (f *UnionFile) Name() string { type DirsMerger func(lofi, bofi []os.FileInfo) ([]os.FileInfo, error) var defaultUnionMergeDirsFn = func(lofi, bofi []os.FileInfo) ([]os.FileInfo, error) { - var files = make(map[string]os.FileInfo) + files := make(map[string]os.FileInfo) for _, fi := range lofi { files[fi.Name()] = fi @@ -151,7 +151,6 @@ var defaultUnionMergeDirsFn = func(lofi, bofi []os.FileInfo) ([]os.FileInfo, err } return rfi, nil - } // Readdir will weave the two directories together and @@ -275,7 +274,7 @@ func copyFile(base Fs, layer Fs, name string, bfh File) error { return err } if !exists { - err = layer.MkdirAll(filepath.Dir(name), 0777) // FIXME? + err = layer.MkdirAll(filepath.Dir(name), 0o777) // FIXME? if err != nil { return err } diff --git a/util.go b/util.go index cb7de23f..9e4cba27 100644 --- a/util.go +++ b/util.go @@ -43,7 +43,7 @@ func WriteReader(fs Fs, path string, r io.Reader) (err error) { ospath := filepath.FromSlash(dir) if ospath != "" { - err = fs.MkdirAll(ospath, 0777) // rwx, rw, r + err = fs.MkdirAll(ospath, 0o777) // rwx, rw, r if err != nil { if err != os.ErrExist { return err @@ -71,7 +71,7 @@ func SafeWriteReader(fs Fs, path string, r io.Reader) (err error) { ospath := filepath.FromSlash(dir) if ospath != "" { - err = fs.MkdirAll(ospath, 0777) // rwx, rw, r + err = fs.MkdirAll(ospath, 0o777) // rwx, rw, r if err != nil { return } @@ -124,7 +124,7 @@ func GetTempDir(fs Fs, subPath string) string { return addSlash(dir) } - err := fs.MkdirAll(dir, 0777) + err := fs.MkdirAll(dir, 0o777) if err != nil { panic(err) } @@ -197,7 +197,6 @@ func FileContainsAnyBytes(fs Fs, filename string, subslices [][]byte) (bool, err // readerContains reports whether any of the subslices is within r. func readerContainsAny(r io.Reader, subslices ...[]byte) bool { - if r == nil || len(subslices) == 0 { return false } diff --git a/util_test.go b/util_test.go index 985b7a14..13ec23b1 100644 --- a/util_test.go +++ b/util_test.go @@ -36,8 +36,8 @@ func TestDirExists(t *testing.T) { } // First create a couple directories so there is something in the filesystem - //testFS := new(MemMapFs) - testFS.MkdirAll("/foo/bar", 0777) + // testFS := new(MemMapFs) + testFS.MkdirAll("/foo/bar", 0o777) data := []test{ {".", true}, @@ -155,7 +155,8 @@ func TestReaderContains(t *testing.T) { {"", nil, false}, {"", [][]byte{[]byte("a")}, false}, {"a", [][]byte{[]byte("")}, false}, - {"", [][]byte{[]byte("")}, false}} { + {"", [][]byte{[]byte("")}, false}, + } { result := readerContainsAny(strings.NewReader(this.v1), this.v2...) if result != this.expect { t.Errorf("[%d] readerContains: got %t but expected %t", i, result, this.expect) @@ -188,7 +189,7 @@ func createNonZeroSizedFileInTempDir() (File, error) { return nil, err } byteString := []byte("byteString") - err = WriteFile(testFS, f.Name(), byteString, 0644) + err = WriteFile(testFS, f.Name(), byteString, 0o644) if err != nil { // delete the file deleteFileInTempDir(f) @@ -229,7 +230,6 @@ func createTempDirWithZeroLengthFiles() (string, error) { } // the dir now has one, zero length file in it return d, nil - } func createTempDirWithNonZeroLengthFiles() (string, error) { @@ -246,7 +246,7 @@ func createTempDirWithNonZeroLengthFiles() (string, error) { return "", fileErr } byteString := []byte("byteString") - fileErr = WriteFile(testFS, f.Name(), byteString, 0644) + fileErr = WriteFile(testFS, f.Name(), byteString, 0o644) if fileErr != nil { // delete the file deleteFileInTempDir(f) @@ -257,7 +257,6 @@ func createTempDirWithNonZeroLengthFiles() (string, error) { // the dir now has one, zero length file in it return d, nil - } func TestExists(t *testing.T) { @@ -292,7 +291,6 @@ func TestExists(t *testing.T) { t.Errorf("Test %d failed. Expected %q got %q", i, d.expectedErr, err) } } - } func TestSafeWriteToDisk(t *testing.T) { diff --git a/zipfs/zipfs_test.go b/zipfs/zipfs_test.go index bf781cc0..6fb9743f 100644 --- a/zipfs/zipfs_test.go +++ b/zipfs/zipfs_test.go @@ -1,12 +1,12 @@ package zipfs import ( - "github.com/spf13/afero" - "archive/zip" "path/filepath" "reflect" "testing" + + "github.com/spf13/afero" ) func TestZipFS(t *testing.T) {