Skip to content

Commit

Permalink
utils.go platform imp
Browse files Browse the repository at this point in the history
  • Loading branch information
abe-winter committed Dec 9, 2024
1 parent 66fe210 commit 758477b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
23 changes: 2 additions & 21 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"path"
"path/filepath"
"strings"
"syscall"
"time"

errw "github.com/pkg/errors"
"github.com/ulikunitz/xz"
"golang.org/x/sys/unix"
"google.golang.org/protobuf/types/known/structpb"
)

Expand Down Expand Up @@ -71,13 +69,8 @@ func InitPaths() error {
}
return errw.Wrapf(err, "checking directory %s", p)
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
// should be impossible on Linux
return errw.New("cannot convert to syscall.Stat_t")
}
if uid != int(stat.Uid) {
return errw.Errorf("%s is owned by UID %d but the current UID is %d", p, stat.Uid, uid)
if err := checkPathOwner(uid, info); err != nil {
return err
}
if !info.IsDir() {
return errw.Errorf("%s should be a directory, but is not", p)
Expand Down Expand Up @@ -283,18 +276,6 @@ func ForceSymlink(orig, symlink string) error {
return SyncFS(symlink)
}

func SyncFS(syncPath string) (errRet error) {
file, errRet := os.Open(filepath.Dir(syncPath))
if errRet != nil {
return errw.Wrapf(errRet, "syncing fs %s", syncPath)
}
_, _, err := unix.Syscall(unix.SYS_SYNCFS, file.Fd(), 0, 0)
if err != 0 {
errRet = errw.Wrapf(err, "syncing fs %s", syncPath)
}
return errors.Join(errRet, file.Close())
}

func WriteFileIfNew(outPath string, data []byte) (bool, error) {
//nolint:gosec
curFileBytes, err := os.ReadFile(outPath)
Expand Down
37 changes: 37 additions & 0 deletions utils_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package agent

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

errw "github.com/pkg/errors"
"golang.org/x/sys/unix"
)

// platform-specific UID check.
func checkPathOwner(uid int, info fs.FileInfo) error {
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
// should be impossible on Linux
return errw.New("cannot convert to syscall.Stat_t")
}
if uid != int(stat.Uid) {
return errw.Errorf("%s is owned by UID %d but the current UID is %d", info.Name(), stat.Uid, uid)
}
return nil
}

func SyncFS(syncPath string) (errRet error) {
file, errRet := os.Open(filepath.Dir(syncPath))
if errRet != nil {
return errw.Wrapf(errRet, "syncing fs %s", syncPath)
}
_, _, err := unix.Syscall(unix.SYS_SYNCFS, file.Fd(), 0, 0)
if err != 0 {
errRet = errw.Wrapf(err, "syncing fs %s", syncPath)
}
return errors.Join(errRet, file.Close())
}
25 changes: 25 additions & 0 deletions utils_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package agent

import (
"io/fs"
"syscall"
)

// platform-specific UID check.
func checkPathOwner(uid int, info fs.FileInfo) error {
// todo: figure this out on windows.
return nil
}

func SyncFS(syncPath string) error {
handle, err := syscall.Open(syncPath, syscall.O_RDWR, 0)
if err != nil {
return err
}
defer syscall.CloseHandle(handle)
err = syscall.Fsync(handle)
if err != nil {
return err
}
return nil
}

0 comments on commit 758477b

Please sign in to comment.