Skip to content
Closed
Changes from all commits
Commits
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
Call SyscallConn() instead of Fd() to use file's fd
[os.File.SyscallConn] is the safer alternative to [os.File.Fd], without the pitfalls mentioned in Fd's doc comment. The SyscallConn method is implemented on all platforms except Plan 9, where it returns [syscall.EPLAN9], which matches [errors.ErrUnsupported] and goes to the fallback path.
  • Loading branch information
database64128 committed Jul 24, 2025
commit df806266c539c25bc23b78b4fe376a155fb7f74f
17 changes: 16 additions & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func Open(file string, options ...ReaderOption) (*Reader, error) {
return nil, errors.New("file too large")
}

data, err := mmap(int(mapFile.Fd()), size)
data, err := openMmap(mapFile, size)
if err != nil {
if errors.Is(err, errors.ErrUnsupported) {
data, err = openFallback(mapFile, size)
Expand All @@ -257,6 +257,21 @@ func Open(file string, options ...ReaderOption) (*Reader, error) {
return reader, nil
}

func openMmap(f *os.File, size int) (data []byte, err error) {
rawConn, err := f.SyscallConn()
if err != nil {
return nil, err
}

if cerr := rawConn.Control(func(fd uintptr) {
data, err = mmap(int(fd), size)
}); cerr != nil {
return nil, cerr
}

return data, err
}

func openFallback(f *os.File, size int) (data []byte, err error) {
data = make([]byte, size)
_, err = io.ReadFull(f, data)
Expand Down
Loading