Skip to content

Commit 2639c76

Browse files
authored
At file package that can wrap files with some helpers (#54)
* 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
1 parent e7a4df2 commit 2639c76

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/file/file_linux.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build linux
2+
3+
package file
4+
5+
import (
6+
"fmt"
7+
"os"
8+
9+
"golang.org/x/sys/unix"
10+
)
11+
12+
// OpenFileFADVDONTNEED opens file with FADV_DONTNEED
13+
func OpenFileFADVDONTNEED(path string) (*os.File, error) {
14+
// Open the file
15+
file, err := os.Open(path)
16+
if err != nil {
17+
return nil, fmt.Errorf("error opening file: %w", err)
18+
}
19+
20+
// File descriptor
21+
fd := int(file.Fd())
22+
23+
// Use FADV_DONTNEED to suggest not caching pages
24+
err = unix.Fadvise(fd, 0, 0, unix.FADV_DONTNEED)
25+
if err != nil {
26+
return nil, fmt.Errorf("error setting FADV_DONTNEED: %w", err)
27+
}
28+
29+
return file, nil
30+
}

pkg/file/file_notlinux.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !linux
2+
3+
package file
4+
5+
import (
6+
"os"
7+
)
8+
9+
// OpenFileFADVDONTNEED opens file with FADV_DONTNEED
10+
func OpenFileFADVDONTNEED(path string) (*os.File, error) {
11+
return os.Open(path)
12+
}

0 commit comments

Comments
 (0)