-
Notifications
You must be signed in to change notification settings - Fork 60
Implement Darwin (MacOS) #169
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
Open
lancepioch
wants to merge
9
commits into
main
Choose a base branch
from
lance/mac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+813
−162
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
61966fb
Initial Darwin Implementation
lancepioch 64360a3
Reduce log level
lancepioch a85e01c
Only linux supports io.weight
lancepioch 561baa9
This should not be an error
lancepioch 3a7a902
Document everything
lancepioch ede819f
Fix test
lancepioch 1fb4197
Panic fix
lancepioch c5a538a
Data race fix
lancepioch 3532707
Fix build targets
lancepioch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package config | ||
|
|
||
| // UseOpenat2 always returns false on Darwin as the openat2 syscall is | ||
| // Linux-specific (kernel 5.6+). | ||
| func UseOpenat2() bool { | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
|
|
||
| "github.com/apex/log" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| var ( | ||
| openat2 atomic.Bool | ||
| openat2Set atomic.Bool | ||
| ) | ||
|
|
||
| func UseOpenat2() bool { | ||
| if openat2Set.Load() { | ||
| return openat2.Load() | ||
| } | ||
| defer openat2Set.Store(true) | ||
|
|
||
| c := Get() | ||
| openatMode := c.System.OpenatMode | ||
| switch openatMode { | ||
| case "openat2": | ||
| openat2.Store(true) | ||
| return true | ||
| case "openat": | ||
| openat2.Store(false) | ||
| return false | ||
| default: | ||
| fd, err := unix.Openat2(unix.AT_FDCWD, "/", &unix.OpenHow{}) | ||
| if err != nil { | ||
| log.WithError(err).Warn("error occurred while checking for openat2 support, falling back to openat") | ||
| openat2.Store(false) | ||
| return false | ||
| } | ||
| _ = unix.Close(fd) | ||
| openat2.Store(true) | ||
| return true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package config | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestUseOpenat2ConfigOverride(t *testing.T) { | ||
| Set(&Configuration{ | ||
| AuthenticationToken: "test", | ||
| System: SystemConfiguration{ | ||
| OpenatMode: "openat", | ||
| }, | ||
| }) | ||
| openat2Set.Store(false) | ||
|
|
||
| if UseOpenat2() { | ||
| t.Error("expected UseOpenat2() to return false when mode is 'openat'") | ||
| } | ||
|
|
||
| openat2Set.Store(false) | ||
| Update(func(c *Configuration) { | ||
| c.System.OpenatMode = "openat2" | ||
| }) | ||
|
|
||
| if !UseOpenat2() { | ||
| t.Error("expected UseOpenat2() to return true when mode is 'openat2'") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestUseOpenat2(t *testing.T) { | ||
| // Ensure UseOpenat2 doesn't panic. | ||
| Set(&Configuration{ | ||
| AuthenticationToken: "test", | ||
| System: SystemConfiguration{ | ||
| OpenatMode: "auto", | ||
| }, | ||
| }) | ||
|
|
||
| result := UseOpenat2() | ||
|
|
||
| switch runtime.GOOS { | ||
| case "darwin": | ||
| if result { | ||
| t.Error("expected UseOpenat2() to return false on Darwin") | ||
| } | ||
| case "linux": | ||
| // On Linux it may be true or false depending on kernel version. | ||
| // Just verify it returns without error. | ||
| t.Logf("UseOpenat2() returned %v on Linux", result) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package ufs | ||
|
|
||
| // O_LARGEFILE is a no-op on Darwin as all files support large offsets. | ||
| const O_LARGEFILE = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package ufs | ||
|
|
||
| import "golang.org/x/sys/unix" | ||
|
|
||
| const O_LARGEFILE = unix.O_LARGEFILE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package ufs | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "path/filepath" | ||
| "runtime" | ||
| "time" | ||
| "unsafe" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // fdPath returns the filesystem path associated with a file descriptor using | ||
| // the F_GETPATH fcntl command on macOS. | ||
| func fdPath(fd int) (string, error) { | ||
| buf := make([]byte, unix.PathMax) | ||
| _, _, errno := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), uintptr(unix.F_GETPATH), uintptr(unsafe.Pointer(&buf[0]))) | ||
| runtime.KeepAlive(buf) | ||
| if errno != 0 { | ||
| return "", errno | ||
| } | ||
| n := bytes.IndexByte(buf, 0) | ||
| if n < 0 { | ||
| n = len(buf) | ||
| } | ||
| return filepath.EvalSymlinks(string(buf[:n])) | ||
| } | ||
|
|
||
| // _openat2 is a stub on Darwin. The openat2 syscall is Linux-specific (kernel | ||
| // 5.6+). On Darwin, this always returns ENOSYS to signal that the caller | ||
| // should fall back to the regular openat path. | ||
| func (fs *UnixFS) _openat2(dirfd int, name string, flag, mode uint64) (int, error) { | ||
| return 0, unix.ENOSYS | ||
| } | ||
|
|
||
| // Chtimesat is like Chtimes but allows passing an existing directory file | ||
| // descriptor rather than needing to resolve one. On Darwin, UTIME_OMIT is not | ||
| // available, so zero times are handled by reading the current timestamps and | ||
| // preserving them. | ||
| func (fs *UnixFS) Chtimesat(dirfd int, name string, atime, mtime time.Time) error { | ||
| if atime.IsZero() || mtime.IsZero() { | ||
| var st unix.Stat_t | ||
| if err := unix.Fstatat(dirfd, name, &st, 0); err != nil { | ||
| return ensurePathError(err, "chtimes", name) | ||
| } | ||
| if atime.IsZero() { | ||
| atime = time.Unix(st.Atim.Unix()) | ||
| } | ||
| if mtime.IsZero() { | ||
| mtime = time.Unix(st.Mtim.Unix()) | ||
| } | ||
| } | ||
|
|
||
| utimes := [2]unix.Timespec{ | ||
| unix.NsecToTimespec(atime.UnixNano()), | ||
| unix.NsecToTimespec(mtime.UnixNano()), | ||
| } | ||
| return ensurePathError(unix.UtimesNanoAt(dirfd, name, utimes[0:], 0), "chtimes", name) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package ufs | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // fdPath returns the filesystem path associated with a file descriptor by | ||
| // reading the /proc/self/fd/ symlink. | ||
| func fdPath(fd int) (string, error) { | ||
| return filepath.EvalSymlinks(filepath.Join("/proc/self/fd/", strconv.Itoa(fd))) | ||
| } | ||
|
|
||
| // _openat2 is a wonderful syscall that supersedes the `openat` syscall. It has | ||
| // improved validation and security characteristics that weren't available or | ||
| // considered when `openat` was originally implemented. As such, it is only | ||
| // present in Kernel 5.6 and above. | ||
| // | ||
| // This method should never be directly called, use `openat` instead. | ||
| func (fs *UnixFS) _openat2(dirfd int, name string, flag, mode uint64) (int, error) { | ||
| // Ensure the O_CLOEXEC flag is set. | ||
| // Go sets this when using the os package, but since we are directly using | ||
| // the unix package we need to set it ourselves. | ||
| if flag&O_CLOEXEC == 0 { | ||
| flag |= O_CLOEXEC | ||
| } | ||
| // Ensure the O_LARGEFILE flag is set. | ||
| // Go sets this for unix.Open, unix.Openat, but not unix.Openat2. | ||
| if flag&O_LARGEFILE == 0 { | ||
| flag |= O_LARGEFILE | ||
| } | ||
| fd, err := unix.Openat2(dirfd, name, &unix.OpenHow{ | ||
| Flags: flag, | ||
| Mode: mode, | ||
| // This is the bread and butter of preventing a symlink escape, without | ||
| // this option, we have to handle path validation fully on our own. | ||
| // | ||
| // This is why using Openat2 over Openat is preferred if available. | ||
| Resolve: unix.RESOLVE_BENEATH, | ||
| }) | ||
| switch { | ||
| case err == nil: | ||
| return fd, nil | ||
| case err == unix.EINTR: | ||
| return fd, err | ||
| case err == unix.EAGAIN: | ||
| return fd, err | ||
| default: | ||
| return fd, ensurePathError(err, "openat2", name) | ||
| } | ||
| } | ||
|
|
||
| // Chtimesat is like Chtimes but allows passing an existing directory file | ||
| // descriptor rather than needing to resolve one. | ||
| func (fs *UnixFS) Chtimesat(dirfd int, name string, atime, mtime time.Time) error { | ||
| var utimes [2]unix.Timespec | ||
| set := func(i int, t time.Time) { | ||
| if t.IsZero() { | ||
| utimes[i] = unix.Timespec{Sec: unix.UTIME_OMIT, Nsec: unix.UTIME_OMIT} | ||
| } else { | ||
| utimes[i] = unix.NsecToTimespec(t.UnixNano()) | ||
| } | ||
| } | ||
| set(0, atime) | ||
| set(1, mtime) | ||
|
|
||
| // This does support `AT_SYMLINK_NOFOLLOW` as well if needed. | ||
| return ensurePathError(unix.UtimesNanoAt(dirfd, name, utimes[0:], 0), "chtimes", name) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: pelican-dev/wings
Length of output: 194
🌐 Web query:
golang.org/x/sys/unix Stat_t Darwin Atimespec Mtimespec field names💡 Result:
On Darwin (
GOOS=darwin),golang.org/x/sys/unix.Stat_tuses the BSD-style timestamp fields:Atimespec(access time)Mtimespec(modification time)Ctimespec(inode change time)Birthtimespec(creation/birth time) (git.linux.ucla.edu)Each of those fields is a
unix.Timespec, whose field names are:Sec(seconds)Nsec(nanoseconds) (docs.activestate.com)So you access them like:
st.Atimespec.Sec,st.Atimespec.Nsec,st.Mtimespec.Sec, etc. (docs.activestate.com)Citations:
🏁 Script executed:
Repository: pelican-dev/wings
Length of output: 781
🏁 Script executed:
Repository: pelican-dev/wings
Length of output: 826
Fix field names for Darwin Stat_t struct —
Atim/Mtimdon't exist on macOS.On Darwin,
unix.Stat_tusesAtimespecandMtimespec(notAtim/Mtimas on Linux). Lines 47 and 50 reference nonexistent fields and will fail to compile on macOS.Fix
if atime.IsZero() { - atime = time.Unix(st.Atim.Unix()) + atime = time.Unix(st.Atimespec.Unix()) } if mtime.IsZero() { - mtime = time.Unix(st.Mtim.Unix()) + mtime = time.Unix(st.Mtimespec.Unix()) }📝 Committable suggestion
🤖 Prompt for AI Agents