Skip to content

Commit

Permalink
Merge pull request #189 from safing/fix/fs-error-handling
Browse files Browse the repository at this point in the history
Fix fs error handling
  • Loading branch information
dhaavi authored Oct 11, 2022
2 parents 2b4c15c + 0d3a0eb commit 985a174
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 15 deletions.
5 changes: 3 additions & 2 deletions config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -63,12 +64,12 @@ func start() error {
}

err = registerAsDatabase()
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

err = loadConfig(false)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion database/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path"
"regexp"
Expand Down Expand Up @@ -116,7 +117,7 @@ func loadRegistry() error {
filePath := path.Join(rootStructure.Path, registryFileName)
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
Expand Down
11 changes: 6 additions & 5 deletions database/storage/fstree/fstree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -46,7 +47,7 @@ func NewFSTree(name, location string) (storage.Interface, error) {

file, err := os.Stat(basePath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
err = os.MkdirAll(basePath, defaultDirMode)
if err != nil {
return nil, fmt.Errorf("fstree: failed to create directory %s: %w", basePath, err)
Expand Down Expand Up @@ -89,7 +90,7 @@ func (fst *FSTree) Get(key string) (record.Record, error) {

data, err := os.ReadFile(dstPath)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return nil, storage.ErrNotFound
}
return nil, fmt.Errorf("fstree: failed to read file %s: %w", dstPath, err)
Expand Down Expand Up @@ -176,7 +177,7 @@ func (fst *FSTree) Query(q *query.Query, local, internal bool) (*iterator.Iterat
walkRoot = walkPrefix
case err == nil:
walkRoot = filepath.Dir(walkPrefix)
case os.IsNotExist(err):
case errors.Is(err, fs.ErrNotExist):
walkRoot = filepath.Dir(walkPrefix)
default: // err != nil
return nil, fmt.Errorf("fstree: could not stat query root %s: %w", walkPrefix, err)
Expand Down Expand Up @@ -211,7 +212,7 @@ func (fst *FSTree) queryExecutor(walkRoot string, queryIter *iterator.Iterator,
// read file
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return fmt.Errorf("fstree: failed to read file %s: %w", path, err)
Expand Down Expand Up @@ -274,7 +275,7 @@ func (fst *FSTree) Shutdown() error {
return nil
}

// writeFile mirrors ioutil.WriteFile, replacing an existing file with the same
// writeFile mirrors os.WriteFile, replacing an existing file with the same
// name atomically. This is not atomic on Windows, but still an improvement.
// TODO: Replace with github.com/google/renamio.WriteFile as soon as it is fixed on Windows.
// TODO: This has become a wont-fix. Explore other options.
Expand Down
4 changes: 3 additions & 1 deletion updater/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package updater

import (
"errors"
"io"
"io/fs"
"os"
"strings"

Expand Down Expand Up @@ -121,7 +123,7 @@ func (file *File) Unpack(suffix string, unpacker Unpacker) (string, error) {
return path, nil
}

if !os.IsNotExist(err) {
if !errors.Is(err, fs.ErrNotExist) {
return "", err
}

Expand Down
2 changes: 1 addition & 1 deletion updater/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ boundarySearch:

// Remove if it exists, or an error occurs on access.
_, err = os.Stat(unpackedPath)
if err == nil || !os.IsNotExist(err) {
if err == nil || !errors.Is(err, fs.ErrNotExist) {
err = os.Remove(unpackedPath)
if err != nil {
log.Warningf("%s: failed to purge unpacked resource %s v%s: %s", res.registry.Name, rv.resource.Identifier, rv.VersionNumber, err)
Expand Down
3 changes: 2 additions & 1 deletion updater/unpacking.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (res *Resource) unpackZipArchive() error {
// Check status of destination.
dstStat, err := os.Stat(destDir)
switch {
case os.IsNotExist(err):
case errors.Is(err, fs.ErrNotExist):
// The destination does not exist, continue with unpacking.
case err != nil:
return fmt.Errorf("cannot access destination for unpacking: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion utils/atomic.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package utils

import (
"errors"
"fmt"
"io"
"io/fs"
"os"

"github.com/safing/portbase/utils/renameio"
Expand Down Expand Up @@ -94,7 +96,7 @@ func ReplaceFileAtomic(dest string, src string, opts *AtomicFileOptions) error {
stat, err := os.Stat(dest)
if err == nil {
opts.Mode = stat.Mode()
} else if !os.IsNotExist(err) {
} else if !errors.Is(err, fs.ErrNotExist) {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func EnsureDirectory(path string, perm os.FileMode) error {
}
}
// file does not exist (or has been deleted)
if err == nil || os.IsNotExist(err) {
if err == nil || errors.Is(err, fs.ErrNotExist) {
err = os.Mkdir(path, perm)
if err != nil {
return fmt.Errorf("could not create dir %s: %w", path, err)
Expand Down
4 changes: 3 additions & 1 deletion utils/renameio/tempfile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package renameio

import (
"errors"
"io/fs"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -137,7 +139,7 @@ func TempFile(dir, path string) (*PendingFile, error) {
func Symlink(oldname, newname string) error {
// Fast path: if newname does not exist yet, we can skip the whole dance
// below.
if err := os.Symlink(oldname, newname); err == nil || !os.IsExist(err) {
if err := os.Symlink(oldname, newname); err == nil || !errors.Is(err, fs.ErrExist) {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion utils/renameio/writefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package renameio

import "os"

// WriteFile mirrors ioutil.WriteFile, replacing an existing file with the same
// WriteFile mirrors os.WriteFile, replacing an existing file with the same
// name atomically.
func WriteFile(filename string, data []byte, perm os.FileMode) error {
t, err := TempFile("", filename)
Expand Down

0 comments on commit 985a174

Please sign in to comment.