Skip to content

Commit e19cf91

Browse files
authored
Merge pull request #170 from pjbgf/mmap
osfs: Expose `Fd()` to enable mmap
2 parents e7889f9 + 5636cea commit e19cf91

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

helper/chroot/chroot_posix.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build !plan9 && !windows && !wasm
2+
3+
package chroot
4+
5+
type FileDescriptor interface {
6+
Fd() (uintptr, bool)
7+
}
8+
9+
// Fd exposes the underlying [os.File.Fd] func, which returns the
10+
// system file descriptor or handle referencing the open file.
11+
// If the underlying Filesystem does not expose this func,
12+
// the return will always be (0, false).
13+
func (f *file) Fd() (uintptr, bool) {
14+
fd, ok := f.File.(FileDescriptor)
15+
if ok {
16+
return fd.Fd()
17+
}
18+
return 0, false
19+
}

osfs/os_posix.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ func (f *file) Lock() error {
1313
f.m.Lock()
1414
defer f.m.Unlock()
1515

16-
return unix.Flock(int(f.Fd()), unix.LOCK_EX)
16+
return unix.Flock(int(f.File.Fd()), unix.LOCK_EX)
1717
}
1818

1919
func (f *file) Unlock() error {
2020
f.m.Lock()
2121
defer f.m.Unlock()
2222

23-
return unix.Flock(int(f.Fd()), unix.LOCK_UN)
23+
return unix.Flock(int(f.File.Fd()), unix.LOCK_UN)
2424
}
2525

2626
func (f *file) Sync() error {
@@ -39,3 +39,9 @@ func umask(m int) func() {
3939
syscall.Umask(old)
4040
}
4141
}
42+
43+
// Fd exposes the underlying [os.File.Fd] func, which returns the
44+
// system file descriptor or handle referencing the open file.
45+
func (f *file) Fd() (uintptr, bool) {
46+
return f.File.Fd(), true
47+
}

0 commit comments

Comments
 (0)