mirror of
https://github.com/safing/portbase
synced 2025-04-11 13:09:09 +00:00
Fix fs error handling
This commit is contained in:
parent
2b4c15c1f7
commit
0d3a0ebb95
10 changed files with 25 additions and 15 deletions
config
database
updater
utils
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
@ -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
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
|
@ -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
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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.
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
@ -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)
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/safing/portbase/utils/renameio"
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package renameio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue