From a6600be34ad812ff13c89f45c3cadaac6d994e67 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Mon, 30 Mar 2026 18:15:56 +0900 Subject: all: use filepath This makes package check portable, and removes nonportable behaviour from package pkg, pipewire, and system. All other packages remain nonportable due to their nature. No latency increase was observed due to this change on amd64 and arm64 linux. Signed-off-by: Ophestra --- container/init.go | 4 ++-- container/initdev.go | 16 ++++++++-------- container/initsymlink.go | 6 +++--- container/path.go | 8 ++++---- container/path_test.go | 24 ++++++++++++------------ 5 files changed, 29 insertions(+), 29 deletions(-) (limited to 'container') diff --git a/container/init.go b/container/init.go index 7a561733..e8e95a7a 100644 --- a/container/init.go +++ b/container/init.go @@ -8,7 +8,7 @@ import ( "os" "os/exec" "os/signal" - "path" + "path/filepath" "slices" "strconv" "sync" @@ -569,7 +569,7 @@ func TryArgv0(msg message.Msg) { msg = message.New(log.Default()) } - if len(os.Args) > 0 && path.Base(os.Args[0]) == initName { + if len(os.Args) > 0 && filepath.Base(os.Args[0]) == initName { Init(msg) msg.BeforeExit() os.Exit(0) diff --git a/container/initdev.go b/container/initdev.go index 38cb6e18..0847f587 100644 --- a/container/initdev.go +++ b/container/initdev.go @@ -3,7 +3,7 @@ package container import ( "encoding/gob" "fmt" - "path" + "path/filepath" . "syscall" "hakurei.app/check" @@ -46,7 +46,7 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { } for _, name := range []string{"null", "zero", "full", "random", "urandom", "tty"} { - targetPath := path.Join(target, name) + targetPath := filepath.Join(target, name) if err := k.ensureFile(targetPath, 0444, state.ParentPerm); err != nil { return err } @@ -62,7 +62,7 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { for i, name := range []string{"stdin", "stdout", "stderr"} { if err := k.symlink( fhs.Proc+"self/fd/"+string(rune(i+'0')), - path.Join(target, name), + filepath.Join(target, name), ); err != nil { return err } @@ -72,13 +72,13 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { {fhs.Proc + "kcore", "core"}, {"pts/ptmx", "ptmx"}, } { - if err := k.symlink(pair[0], path.Join(target, pair[1])); err != nil { + if err := k.symlink(pair[0], filepath.Join(target, pair[1])); err != nil { return err } } - devShmPath := path.Join(target, "shm") - devPtsPath := path.Join(target, "pts") + devShmPath := filepath.Join(target, "shm") + devPtsPath := filepath.Join(target, "pts") for _, name := range []string{devShmPath, devPtsPath} { if err := k.mkdir(name, state.ParentPerm); err != nil { return err @@ -92,7 +92,7 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { if state.RetainSession { if k.isatty(Stdout) { - consolePath := path.Join(target, "console") + consolePath := filepath.Join(target, "console") if err := k.ensureFile(consolePath, 0444, state.ParentPerm); err != nil { return err } @@ -110,7 +110,7 @@ func (d *MountDevOp) apply(state *setupState, k syscallDispatcher) error { } if d.Mqueue { - mqueueTarget := path.Join(target, "mqueue") + mqueueTarget := filepath.Join(target, "mqueue") if err := k.mkdir(mqueueTarget, state.ParentPerm); err != nil { return err } diff --git a/container/initsymlink.go b/container/initsymlink.go index f4f608a6..e72e5ef4 100644 --- a/container/initsymlink.go +++ b/container/initsymlink.go @@ -3,7 +3,7 @@ package container import ( "encoding/gob" "fmt" - "path" + "path/filepath" "hakurei.app/check" ) @@ -30,7 +30,7 @@ func (l *SymlinkOp) Valid() bool { return l != nil && l.Target != nil && l.LinkN func (l *SymlinkOp) early(_ *setupState, k syscallDispatcher) error { if l.Dereference { - if !path.IsAbs(l.LinkName) { + if !filepath.IsAbs(l.LinkName) { return check.AbsoluteError(l.LinkName) } if name, err := k.readlink(l.LinkName); err != nil { @@ -44,7 +44,7 @@ func (l *SymlinkOp) early(_ *setupState, k syscallDispatcher) error { func (l *SymlinkOp) apply(state *setupState, k syscallDispatcher) error { target := toSysroot(l.Target.String()) - if err := k.mkdirAll(path.Dir(target), state.ParentPerm); err != nil { + if err := k.mkdirAll(filepath.Dir(target), state.ParentPerm); err != nil { return err } return k.symlink(l.LinkName, target) diff --git a/container/path.go b/container/path.go index 5170fceb..5ae52dee 100644 --- a/container/path.go +++ b/container/path.go @@ -4,7 +4,7 @@ import ( "errors" "io/fs" "os" - "path" + "path/filepath" "strconv" "strings" "syscall" @@ -29,16 +29,16 @@ const ( func toSysroot(name string) string { name = strings.TrimLeftFunc(name, func(r rune) bool { return r == '/' }) - return path.Join(sysrootPath, name) + return filepath.Join(sysrootPath, name) } func toHost(name string) string { name = strings.TrimLeftFunc(name, func(r rune) bool { return r == '/' }) - return path.Join(hostPath, name) + return filepath.Join(hostPath, name) } func createFile(name string, perm, pperm os.FileMode, content []byte) error { - if err := os.MkdirAll(path.Dir(name), pperm); err != nil { + if err := os.MkdirAll(filepath.Dir(name), pperm); err != nil { return err } f, err := os.OpenFile(name, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY, perm) diff --git a/container/path_test.go b/container/path_test.go index 7f28b5f4..03406fb0 100644 --- a/container/path_test.go +++ b/container/path_test.go @@ -4,7 +4,7 @@ import ( "io" "math" "os" - "path" + "path/filepath" "reflect" "syscall" "testing" @@ -61,7 +61,7 @@ func TestCreateFile(t *testing.T) { Path: "/proc/nonexistent", Err: syscall.ENOENT, } - if err := createFile(path.Join(Nonexistent, ":3"), 0644, 0755, nil); !reflect.DeepEqual(err, wantErr) { + if err := createFile(filepath.Join(Nonexistent, ":3"), 0644, 0755, nil); !reflect.DeepEqual(err, wantErr) { t.Errorf("createFile: error = %#v, want %#v", err, wantErr) } }) @@ -72,7 +72,7 @@ func TestCreateFile(t *testing.T) { Path: "/proc/nonexistent", Err: syscall.ENOENT, } - if err := createFile(path.Join(Nonexistent), 0644, 0755, nil); !reflect.DeepEqual(err, wantErr) { + if err := createFile(filepath.Join(Nonexistent), 0644, 0755, nil); !reflect.DeepEqual(err, wantErr) { t.Errorf("createFile: error = %#v, want %#v", err, wantErr) } }) @@ -80,7 +80,7 @@ func TestCreateFile(t *testing.T) { t.Run("touch", func(t *testing.T) { tempDir := t.TempDir() - pathname := path.Join(tempDir, "empty") + pathname := filepath.Join(tempDir, "empty") if err := createFile(pathname, 0644, 0755, nil); err != nil { t.Fatalf("createFile: error = %v", err) } @@ -93,7 +93,7 @@ func TestCreateFile(t *testing.T) { t.Run("write", func(t *testing.T) { tempDir := t.TempDir() - pathname := path.Join(tempDir, "zero") + pathname := filepath.Join(tempDir, "zero") if err := createFile(pathname, 0644, 0755, []byte{0}); err != nil { t.Fatalf("createFile: error = %v", err) } @@ -107,7 +107,7 @@ func TestCreateFile(t *testing.T) { func TestEnsureFile(t *testing.T) { t.Run("create", func(t *testing.T) { - if err := ensureFile(path.Join(t.TempDir(), "ensure"), 0644, 0755); err != nil { + if err := ensureFile(filepath.Join(t.TempDir(), "ensure"), 0644, 0755); err != nil { t.Errorf("ensureFile: error = %v", err) } }) @@ -115,7 +115,7 @@ func TestEnsureFile(t *testing.T) { t.Run("stat", func(t *testing.T) { t.Run("inaccessible", func(t *testing.T) { tempDir := t.TempDir() - pathname := path.Join(tempDir, "inaccessible") + pathname := filepath.Join(tempDir, "inaccessible") if f, err := os.Create(pathname); err != nil { t.Fatalf("Create: error = %v", err) } else { @@ -150,7 +150,7 @@ func TestEnsureFile(t *testing.T) { t.Run("ensure", func(t *testing.T) { tempDir := t.TempDir() - pathname := path.Join(tempDir, "ensure") + pathname := filepath.Join(tempDir, "ensure") if f, err := os.Create(pathname); err != nil { t.Fatalf("Create: error = %v", err) } else { @@ -195,12 +195,12 @@ func TestProcPaths(t *testing.T) { t.Run("sample", func(t *testing.T) { tempDir := t.TempDir() - if err := os.MkdirAll(path.Join(tempDir, "proc/self"), 0755); err != nil { + if err := os.MkdirAll(filepath.Join(tempDir, "proc/self"), 0755); err != nil { t.Fatalf("MkdirAll: error = %v", err) } t.Run("clean", func(t *testing.T) { - if err := os.WriteFile(path.Join(tempDir, "proc/self/mountinfo"), []byte(`15 20 0:3 / /proc rw,relatime - proc /proc rw + if err := os.WriteFile(filepath.Join(tempDir, "proc/self/mountinfo"), []byte(`15 20 0:3 / /proc rw,relatime - proc /proc rw 16 20 0:15 / /sys rw,relatime - sysfs /sys rw 17 20 0:5 / /dev rw,relatime - devtmpfs udev rw,size=1983516k,nr_inodes=495879,mode=755`), 0644); err != nil { t.Fatalf("WriteFile: error = %v", err) @@ -243,8 +243,8 @@ func TestProcPaths(t *testing.T) { }) t.Run("malformed", func(t *testing.T) { - path.Join(tempDir, "proc/self/mountinfo") - if err := os.WriteFile(path.Join(tempDir, "proc/self/mountinfo"), []byte{0}, 0644); err != nil { + filepath.Join(tempDir, "proc/self/mountinfo") + if err := os.WriteFile(filepath.Join(tempDir, "proc/self/mountinfo"), []byte{0}, 0644); err != nil { t.Fatalf("WriteFile: error = %v", err) } -- cgit v1.3.1