diff --git a/helper/chroot/chroot_posix.go b/helper/chroot/chroot_posix.go new file mode 100644 index 0000000..04e8eb5 --- /dev/null +++ b/helper/chroot/chroot_posix.go @@ -0,0 +1,19 @@ +//go:build !plan9 && !windows && !wasm + +package chroot + +type FileDescriptor interface { + Fd() (uintptr, bool) +} + +// Fd exposes the underlying [os.File.Fd] func, which returns the +// system file descriptor or handle referencing the open file. +// If the underlying Filesystem does not expose this func, +// the return will always be (0, false). +func (f *file) Fd() (uintptr, bool) { + fd, ok := f.File.(FileDescriptor) + if ok { + return fd.Fd() + } + return 0, false +} diff --git a/osfs/os_posix.go b/osfs/os_posix.go index c89509e..65a6bc9 100644 --- a/osfs/os_posix.go +++ b/osfs/os_posix.go @@ -13,14 +13,14 @@ func (f *file) Lock() error { f.m.Lock() defer f.m.Unlock() - return unix.Flock(int(f.Fd()), unix.LOCK_EX) + return unix.Flock(int(f.File.Fd()), unix.LOCK_EX) } func (f *file) Unlock() error { f.m.Lock() defer f.m.Unlock() - return unix.Flock(int(f.Fd()), unix.LOCK_UN) + return unix.Flock(int(f.File.Fd()), unix.LOCK_UN) } func (f *file) Sync() error { @@ -39,3 +39,9 @@ func umask(m int) func() { syscall.Umask(old) } } + +// Fd exposes the underlying [os.File.Fd] func, which returns the +// system file descriptor or handle referencing the open file. +func (f *file) Fd() (uintptr, bool) { + return f.File.Fd(), true +}