Skip to content

Commit 4e55473

Browse files
committed
Check for direct mode and skip all this logic if not direct
1 parent 558fd5b commit 4e55473

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

pkg/directfile/directfile.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ func openNodirect(path string) (*DirectFile, error) {
3131

3232
// Close closes the file
3333
func (df *DirectFile) Close() error {
34-
return df.file.Close()
34+
if df.file != nil {
35+
return df.file.Close()
36+
}
37+
38+
return nil
3539
}
3640

3741
// Read satisfies the io.Reader
3842
func (df *DirectFile) Read(p []byte) (n int, err error) {
43+
if !df.Direct {
44+
return df.file.Read(p)
45+
}
3946
blockSize := int(df.blockSize)
4047

4148
// Ensure buffer length is aligned with BlockSize for O_DIRECT
@@ -52,7 +59,7 @@ func (df *DirectFile) Read(p []byte) (n int, err error) {
5259

5360
req := len(p)
5461
act := len(buf)
55-
percent := act/req * 100
62+
percent := act / req * 100
5663
safeLog("Read into aligned buffer", "requested", req, "reading", act, "percentage of requested", percent)
5764

5865
// Perform the read
@@ -66,6 +73,9 @@ func (df *DirectFile) Read(p []byte) (n int, err error) {
6673

6774
// ReadAt satisfies the io.ReaderAt interface
6875
func (df *DirectFile) ReadAt(p []byte, off int64) (n int, err error) {
76+
if !df.Direct {
77+
return df.file.ReadAt(p, off)
78+
}
6979
blockSize := int(df.blockSize)
7080
// Calculate aligned offset by rounding down to the nearest BlockSize boundary
7181
// Integer division in go always discards remainder
@@ -86,7 +96,7 @@ func (df *DirectFile) ReadAt(p []byte, off int64) (n int, err error) {
8696

8797
req := len(p)
8898
act := len(buf)
89-
percent := act/req * 100
99+
percent := act / req * 100
90100
safeLog("ReadAt into aligned buffer", "requested", req, "reading", act, "percentage of requested", percent)
91101

92102
// Perform the read at the aligned offset

0 commit comments

Comments
 (0)