Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Replace use of os.IsNotExist(err) with errors.Is(err, fs.ErrNotExist) #1245

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/incus-agent/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (s *execWs) Do(op *operations.Operation) error {
if err != nil {
exitStatus := -1

if errors.Is(err, exec.ErrNotFound) || os.IsNotExist(err) {
if errors.Is(err, exec.ErrNotFound) || errors.Is(err, fs.ErrNotExist) {
exitStatus = 127
} else if errors.Is(err, fs.ErrPermission) {
exitStatus = 126
Expand Down
4 changes: 3 additions & 1 deletion cmd/incus-agent/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"crypto/tls"
"encoding/json"
"errors"
"io/fs"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -65,7 +67,7 @@ func reconfigureNetworkInterfaces() {
nicDirEntries, err := os.ReadDir(deviceConfig.NICConfigDir)
if err != nil {
// Abort if configuration folder does not exist (nothing to do), otherwise log and return.
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/incus-simplestreams/main_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"strings"
"time"
Expand Down Expand Up @@ -291,7 +293,7 @@ func (c *cmdAdd) Run(cmd *cobra.Command, args []string) error {

body, err = os.ReadFile("streams/v1/images.json")
if err != nil {
if !os.IsNotExist(err) {
if !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/incus-simplestreams/main_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

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

"github.com/spf13/cobra"
Expand Down Expand Up @@ -39,7 +41,7 @@ func (c *cmdRemove) remove(path string) error {
}

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

Expand Down Expand Up @@ -97,7 +99,7 @@ func (c *cmdRemove) Run(cmd *cobra.Command, args []string) error {
} else if metaEntry.CombinedSha256SquashFs == image.Fingerprint {
// Deleting a container image.
err = c.remove(version.Items["squashfs"].Path)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/incus-simplestreams/main_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -47,7 +49,7 @@ func (c *cmdVerify) Run(cmd *cobra.Command, args []string) error {

body, err := os.ReadFile("streams/v1/images.json")
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return nil
}

Expand All @@ -67,7 +69,7 @@ func (c *cmdVerify) Run(cmd *cobra.Command, args []string) error {
// Open the data.
dataFile, err := os.Open(item.Path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Missing image file %q", item.Path)
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/incus/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -451,7 +453,7 @@ func (c *cmdFilePull) Run(cmd *cobra.Command, args []string) error {

targetIsDir := false
sb, err := os.Stat(target)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down
20 changes: 12 additions & 8 deletions cmd/incusd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"errors"
"io/fs"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -423,10 +425,11 @@ type uiHttpDir struct {
http.FileSystem
}

func (fs uiHttpDir) Open(name string) (http.File, error) {
fsFile, err := fs.FileSystem.Open(name)
if err != nil && os.IsNotExist(err) {
return fs.FileSystem.Open("index.html")
// Open is part of the http.FileSystem interface.
func (httpFS uiHttpDir) Open(name string) (http.File, error) {
fsFile, err := httpFS.FileSystem.Open(name)
if err != nil && errors.Is(err, fs.ErrNotExist) {
return httpFS.FileSystem.Open("index.html")
}

return fsFile, err
Expand All @@ -436,10 +439,11 @@ type documentationHttpDir struct {
http.FileSystem
}

func (fs documentationHttpDir) Open(name string) (http.File, error) {
fsFile, err := fs.FileSystem.Open(name)
if err != nil && os.IsNotExist(err) {
return fs.FileSystem.Open("index.html")
// Open is part of the http.FileSystem interface.
func (httpFS documentationHttpDir) Open(name string) (http.File, error) {
fsFile, err := httpFS.FileSystem.Open(name)
if err != nil && errors.Is(err, fs.ErrNotExist) {
return httpFS.FileSystem.Open("index.html")
}

return fsFile, err
Expand Down
9 changes: 5 additions & 4 deletions cmd/incusd/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"math"
"math/rand"
"mime"
Expand Down Expand Up @@ -2623,14 +2624,14 @@ func pruneExpiredImages(ctx context.Context, s *state.State, op *operations.Oper
// Remove main image file.
fname := filepath.Join(s.OS.VarDir, "images", fingerprint)
err = os.Remove(fname)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Error deleting image file %q: %w", fname, err)
}

// Remove the rootfs file for the image.
fname = filepath.Join(s.OS.VarDir, "images", fingerprint) + ".rootfs"
err = os.Remove(fname)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Error deleting image file %q: %w", fname, err)
}

Expand Down Expand Up @@ -2856,7 +2857,7 @@ func imageDeleteFromDisk(fingerprint string) {
fname := internalUtil.VarPath("images", fingerprint)
if util.PathExists(fname) {
err := os.Remove(fname)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
logger.Errorf("Error deleting image file %s: %s", fname, err)
}
}
Expand All @@ -2865,7 +2866,7 @@ func imageDeleteFromDisk(fingerprint string) {
fname = internalUtil.VarPath("images", fingerprint) + ".rootfs"
if util.PathExists(fname) {
err := os.Remove(fname)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
logger.Errorf("Error deleting image file %s: %s", fname, err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/incusd/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -326,12 +328,12 @@ func instancesOnDisk(s *state.State) ([]instance.Instance, error) {
instanceTypeNames := make(map[instancetype.Type][]os.DirEntry, 2)

instanceTypeNames[instancetype.Container], err = os.ReadDir(instancePaths[instancetype.Container])
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}

instanceTypeNames[instancetype.VM], err = os.ReadDir(instancePaths[instancetype.VM])
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/incusd/main_forkproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ void forkproxy(void)
import "C"

import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -453,7 +455,7 @@ func (c *cmdForkproxy) Run(cmd *cobra.Command, args []string) error {

if lAddr.ConnType == "unix" && !lAddr.Abstract {
err := os.Remove(lAddr.Address)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/lxd-to-incus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -575,19 +577,19 @@ Instead this tool will be providing specific commands for each of the servers.`)
_, _ = logFile.WriteString("Wiping the target server\n")

err = os.RemoveAll(targetPaths.logs)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to remove %q: %w", targetPaths.logs, err)
}

err = os.RemoveAll(targetPaths.cache)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to remove %q: %w", targetPaths.cache, err)
}

err = os.RemoveAll(targetPaths.daemon)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to remove %q: %w", targetPaths.daemon, err)
}
Expand Down Expand Up @@ -713,7 +715,7 @@ Instead this tool will be providing specific commands for each of the servers.`)

_ = unix.Unmount(filepath.Join(targetPaths.daemon, dir), unix.MNT_DETACH)
err = os.RemoveAll(filepath.Join(targetPaths.daemon, dir))
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
_, _ = logFile.WriteString(fmt.Sprintf("ERROR: %v\n", err))
return fmt.Errorf("Failed to delete %q: %w", dir, err)
}
Expand All @@ -722,7 +724,7 @@ Instead this tool will be providing specific commands for each of the servers.`)
for _, dir := range []string{"containers", "containers-snapshots", "snapshots", "virtual-machines", "virtual-machines-snapshots"} {
entries, err := os.ReadDir(filepath.Join(targetPaths.daemon, dir))
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
continue
}

Expand Down
8 changes: 5 additions & 3 deletions internal/server/apparmor/apparmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package apparmor

import (
"crypto/sha256"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -114,7 +116,7 @@ func deleteNamespace(sysOS *sys.OS, name string) error {

p := filepath.Join("/sys/kernel/security/apparmor/policy/namespaces", name)
err := os.Remove(p)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down Expand Up @@ -195,12 +197,12 @@ func deleteProfile(sysOS *sys.OS, fullName string, name string) error {
}

err = os.Remove(filepath.Join(aaCacheDir, name))
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Failed to remove %s: %w", filepath.Join(aaCacheDir, name), err)
}

err = os.Remove(filepath.Join(aaPath, "profiles", name))
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("Failed to remove %s: %w", filepath.Join(aaPath, "profiles", name), err)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/server/apparmor/instance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package apparmor

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -123,7 +125,7 @@ func instanceProfileGenerate(sysOS *sys.OS, inst instance, extraBinaries []strin
*/
profile := filepath.Join(aaPath, "profiles", instanceProfileFilename(inst))
content, err := os.ReadFile(profile)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/server/apparmor/instance_forkproxy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package apparmor

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -170,7 +172,7 @@ func ForkproxyLoad(sysOS *sys.OS, inst instance, dev device) error {
*/
profile := filepath.Join(aaPath, "profiles", forkproxyProfileFilename(inst, dev))
content, err := os.ReadFile(profile)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/server/apparmor/network.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package apparmor

import (
"errors"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -30,7 +32,7 @@ func NetworkLoad(sysOS *sys.OS, n network) error {
// dnsmasq
profile := filepath.Join(aaPath, "profiles", dnsmasqProfileFilename(n))
content, err := os.ReadFile(profile)
if err != nil && !os.IsNotExist(err) {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}

Expand Down
Loading
Loading