Skip to content

Commit

Permalink
At file package that can wrap files with some helpers (#54)
Browse files Browse the repository at this point in the history
* Add directfile package that satisfies io.Reader and io.ReaderAt

* Add Close()

* Add logging around requested bytes vs how many we're actually reading

* Check for direct mode and skip all this logic if not direct

* Add open with FADV_DONTNEED

* Fix unix return

* Fix linux

* Make go happy about filenames

* sdfkdjghdfg

* Remove all the directio stuff and rename to file
  • Loading branch information
cmmarslender authored Oct 28, 2024
1 parent e7a4df2 commit 2639c76
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions pkg/file/file_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build linux

package file

import (
"fmt"
"os"

"golang.org/x/sys/unix"
)

// OpenFileFADVDONTNEED opens file with FADV_DONTNEED
func OpenFileFADVDONTNEED(path string) (*os.File, error) {
// Open the file
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}

// File descriptor
fd := int(file.Fd())

// Use FADV_DONTNEED to suggest not caching pages
err = unix.Fadvise(fd, 0, 0, unix.FADV_DONTNEED)
if err != nil {
return nil, fmt.Errorf("error setting FADV_DONTNEED: %w", err)
}

return file, nil
}
12 changes: 12 additions & 0 deletions pkg/file/file_notlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !linux

package file

import (
"os"
)

// OpenFileFADVDONTNEED opens file with FADV_DONTNEED
func OpenFileFADVDONTNEED(path string) (*os.File, error) {
return os.Open(path)
}

0 comments on commit 2639c76

Please sign in to comment.