diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 54af176..1f3b02d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,8 +30,8 @@ jobs: shell: bash - name: Vet run: go vet ./... - #- name: Staticcheck - # if: matrix.go-version != '1.16.x' - # run: staticcheck ./... + - name: Staticcheck + if: matrix.go-version != '1.16.x' + run: staticcheck ./... - name: Test run: go test -race ./... \ No newline at end of file diff --git a/afero.go b/afero.go index 469ff7d..199480c 100644 --- a/afero.go +++ b/afero.go @@ -103,8 +103,8 @@ type Fs interface { var ( ErrFileClosed = errors.New("File is closed") - ErrOutOfRange = errors.New("Out of range") - ErrTooLarge = errors.New("Too large") + ErrOutOfRange = errors.New("out of range") + ErrTooLarge = errors.New("too large") ErrFileNotFound = os.ErrNotExist ErrFileExists = os.ErrExist ErrDestinationExists = os.ErrExist diff --git a/afero_test.go b/afero_test.go index e2c1362..5cd0a1a 100644 --- a/afero_test.go +++ b/afero_test.go @@ -98,7 +98,7 @@ func TestOpenFile(t *testing.T) { io.WriteString(f, "|append") f.Close() - f, err = fs.OpenFile(path, os.O_RDONLY, 0600) + f, _ = fs.OpenFile(path, os.O_RDONLY, 0600) contents, _ := ioutil.ReadAll(f) expectedContents := "initial|append" if string(contents) != expectedContents { @@ -700,7 +700,7 @@ func removeAllTestFiles(t *testing.T) { func equal(name1, name2 string) (r bool) { switch runtime.GOOS { case "windows": - r = strings.ToLower(name1) == strings.ToLower(name2) + r = strings.EqualFold(name1, name2) default: r = name1 == name2 } diff --git a/composite_test.go b/composite_test.go index 9cfbed9..05605fb 100644 --- a/composite_test.go +++ b/composite_test.go @@ -79,7 +79,7 @@ func TestUnionCreateExisting(t *testing.T) { fh.Close() fh, _ = base.Open("/home/test/file.txt") - data, err = ioutil.ReadAll(fh) + data, _ = ioutil.ReadAll(fh) if string(data) != "This is a test" { t.Errorf("Got wrong data in base file") } @@ -326,9 +326,9 @@ func TestUnionCacheWrite(t *testing.T) { t.Errorf("Failed to write file") } - fh.Seek(0, os.SEEK_SET) + fh.Seek(0, io.SeekStart) buf := make([]byte, 4) - _, err = fh.Read(buf) + _, _ = fh.Read(buf) fh.Write([]byte(" IS A")) fh.Close() diff --git a/gcsfs/fs.go b/gcsfs/fs.go index 51db995..9b0dde4 100644 --- a/gcsfs/fs.go +++ b/gcsfs/fs.go @@ -302,6 +302,9 @@ func (fs *Fs) Remove(name string) error { } var infos []os.FileInfo infos, err = dir.Readdir(0) + if err != nil { + return err + } if len(infos) > 0 { return syscall.ENOTEMPTY } @@ -345,6 +348,9 @@ func (fs *Fs) RemoveAll(path string) error { var infos []os.FileInfo infos, err = dir.Readdir(0) + if err != nil { + return err + } for _, info := range infos { nameToRemove := fs.normSeparators(info.Name()) err = fs.RemoveAll(path + fs.separator + nameToRemove) diff --git a/gcsfs/gcs_test.go b/gcsfs/gcs_test.go index 0809c2b..feedfb0 100644 --- a/gcsfs/gcs_test.go +++ b/gcsfs/gcs_test.go @@ -496,7 +496,7 @@ func TestGcsOpenFile(t *testing.T) { t.Fatalf("failed to close a file \"%s\": %s", name, err) } - file, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600) + _, err = gcsAfs.OpenFile(name, os.O_CREATE, 0600) if !errors.Is(err, syscall.EPERM) { t.Errorf("%v: open for write: got %v, expected %v", name, err, syscall.EPERM) } diff --git a/httpFs.go b/httpFs.go index 2b86e30..ac0de6d 100644 --- a/httpFs.go +++ b/httpFs.go @@ -29,7 +29,7 @@ type httpDir struct { } func (d httpDir) Open(name string) (http.File, error) { - if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 || + if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) || strings.Contains(name, "\x00") { return nil, errors.New("http: invalid character in file path") } diff --git a/ioutil_test.go b/ioutil_test.go index 892705b..540bf56 100644 --- a/ioutil_test.go +++ b/ioutil_test.go @@ -37,13 +37,13 @@ func TestReadFile(t *testing.T) { testFS.Create("this_exists.go") filename := "rumpelstilzchen" - contents, err := fsutil.ReadFile(filename) + _, err := fsutil.ReadFile(filename) if err == nil { t.Fatalf("ReadFile %s: error expected, none found", filename) } filename = "this_exists.go" - contents, err = fsutil.ReadFile(filename) + contents, err := fsutil.ReadFile(filename) if err != nil { t.Fatalf("ReadFile %s: %v", filename, err) } diff --git a/mem/file.go b/mem/file.go index 5ef8b6a..258880b 100644 --- a/mem/file.go +++ b/mem/file.go @@ -186,7 +186,7 @@ func (f *File) Readdirnames(n int) (names []string, err error) { func (f *File) Read(b []byte) (n int, err error) { f.fileData.Lock() defer f.fileData.Unlock() - if f.closed == true { + if f.closed { return 0, ErrFileClosed } if len(b) > 0 && int(f.at) == len(f.fileData.data) { @@ -214,7 +214,7 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) { } func (f *File) Truncate(size int64) error { - if f.closed == true { + if f.closed { return ErrFileClosed } if f.readOnly { @@ -236,7 +236,7 @@ func (f *File) Truncate(size int64) error { } func (f *File) Seek(offset int64, whence int) (int64, error) { - if f.closed == true { + if f.closed { return 0, ErrFileClosed } switch whence { @@ -251,7 +251,7 @@ func (f *File) Seek(offset int64, whence int) (int64, error) { } func (f *File) Write(b []byte) (n int, err error) { - if f.closed == true { + if f.closed { return 0, ErrFileClosed } if f.readOnly { @@ -330,8 +330,8 @@ func (s *FileInfo) Size() int64 { var ( ErrFileClosed = errors.New("File is closed") - ErrOutOfRange = errors.New("Out of range") - ErrTooLarge = errors.New("Too large") + ErrOutOfRange = errors.New("out of range") + ErrTooLarge = errors.New("too large") ErrFileNotFound = os.ErrNotExist ErrFileExists = os.ErrExist ErrDestinationExists = os.ErrExist diff --git a/memmap_test.go b/memmap_test.go index 881a61b..94f70b2 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -214,7 +214,7 @@ func TestMultipleOpenFiles(t *testing.T) { if err != nil { t.Error("fh.Write failed: " + err.Error()) } - _, err = fh1.Seek(0, os.SEEK_SET) + _, err = fh1.Seek(0, io.SeekStart) if err != nil { t.Error(err) } diff --git a/ro_regexp_test.go b/ro_regexp_test.go index ef8a35d..ef3a14f 100644 --- a/ro_regexp_test.go +++ b/ro_regexp_test.go @@ -16,12 +16,12 @@ func TestFilterReadOnly(t *testing.T) { func TestFilterReadonlyRemoveAndRead(t *testing.T) { mfs := &MemMapFs{} - fh, err := mfs.Create("/file.txt") + fh, _ := mfs.Create("/file.txt") fh.Write([]byte("content here")) fh.Close() fs := NewReadOnlyFs(mfs) - err = fs.Remove("/file.txt") + err := fs.Remove("/file.txt") if err == nil { t.Errorf("Did not fail to remove file") } diff --git a/sftpfs/sftp_test.go b/sftpfs/sftp_test.go index 4dba7fc..a865139 100644 --- a/sftpfs/sftp_test.go +++ b/sftpfs/sftp_test.go @@ -215,6 +215,9 @@ func MakeSSHKeyPair(bits int, pubKeyPath, privateKeyPath string) error { // generate and write private key as PEM privateKeyFile, err := os.Create(privateKeyPath) + if err != nil { + return err + } defer privateKeyFile.Close() if err != nil { return err diff --git a/tarfs/tarfs_test.go b/tarfs/tarfs_test.go index c588c79..aa2d6a4 100644 --- a/tarfs/tarfs_test.go +++ b/tarfs/tarfs_test.go @@ -266,7 +266,7 @@ func TestOpenFile(t *testing.T) { } file.Close() - file, err = afs.OpenFile(f.name, os.O_CREATE, 0600) + _, err = afs.OpenFile(f.name, os.O_CREATE, 0600) 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 34f99a4..333d367 100644 --- a/unionFile.go +++ b/unionFile.go @@ -65,7 +65,7 @@ func (f *UnionFile) ReadAt(s []byte, o int64) (int, error) { if f.Layer != nil { n, err := f.Layer.ReadAt(s, o) if (err == nil || err == io.EOF) && f.Base != nil { - _, err = f.Base.Seek(o+int64(n), os.SEEK_SET) + _, err = f.Base.Seek(o+int64(n), io.SeekStart) } return n, err } diff --git a/util.go b/util.go index 4f253f4..cb7de23 100644 --- a/util.go +++ b/util.go @@ -25,6 +25,7 @@ import ( "strings" "unicode" + "golang.org/x/text/runes" "golang.org/x/text/transform" "golang.org/x/text/unicode/norm" ) @@ -158,16 +159,12 @@ func UnicodeSanitize(s string) string { // Transform characters with accents into plain forms. func NeuterAccents(s string) string { - t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC) + t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) result, _, _ := transform.String(t, string(s)) return result } -func isMn(r rune) bool { - return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks -} - func (a Afero) FileContainsBytes(filename string, subslice []byte) (bool, error) { return FileContainsBytes(a.Fs, filename, subslice) } @@ -299,6 +296,9 @@ func IsEmpty(fs Fs, path string) (bool, error) { } defer f.Close() list, err := f.Readdir(-1) + if err != nil { + return false, err + } return len(list) == 0, nil } return fi.Size() == 0, nil diff --git a/util_test.go b/util_test.go index d2145cf..985b7a1 100644 --- a/util_test.go +++ b/util_test.go @@ -185,7 +185,7 @@ func createZeroSizedFileInTempDir() (File, error) { func createNonZeroSizedFileInTempDir() (File, error) { f, err := createZeroSizedFileInTempDir() if err != nil { - // no file ?? + return nil, err } byteString := []byte("byteString") err = WriteFile(testFS, f.Name(), byteString, 0644) @@ -200,7 +200,7 @@ func createNonZeroSizedFileInTempDir() (File, error) { func deleteFileInTempDir(f File) { err := testFS.Remove(f.Name()) if err != nil { - // now what? + panic(err) } } @@ -217,7 +217,7 @@ func createEmptyTempDir() (string, error) { func createTempDirWithZeroLengthFiles() (string, error) { d, dirErr := createEmptyTempDir() if dirErr != nil { - //now what? + return "", dirErr } filePrefix := "_path_test_" _, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir() @@ -235,7 +235,7 @@ func createTempDirWithZeroLengthFiles() (string, error) { func createTempDirWithNonZeroLengthFiles() (string, error) { d, dirErr := createEmptyTempDir() if dirErr != nil { - //now what? + return "", dirErr } filePrefix := "_path_test_" f, fileErr := TempFile(testFS, d, filePrefix) // dir is os.TempDir() @@ -406,7 +406,7 @@ func TestGetTempDir(t *testing.T) { func deleteTempDir(d string) { err := os.RemoveAll(d) if err != nil { - // now what? + panic(err) } } diff --git a/zipfs/file.go b/zipfs/file.go index 355f5f4..9b7ddbe 100644 --- a/zipfs/file.go +++ b/zipfs/file.go @@ -85,10 +85,10 @@ func (f *File) Seek(offset int64, whence int) (int64, error) { return 0, afero.ErrFileClosed } switch whence { - case os.SEEK_SET: - case os.SEEK_CUR: + case io.SeekStart: + case io.SeekCurrent: offset += f.offset - case os.SEEK_END: + case io.SeekEnd: offset += int64(f.zipfile.UncompressedSize64) default: return 0, syscall.EINVAL