Suppose the contents of the Foo.txt file are as follows.
Foo Bar Bar Foo
Consider the following short program.
package main import "syscall" import "fmt" func main() { fd, err := syscall.Open("Foo.txt", syscall.O_RDONLY, 0) if err != nil { fmt.Println("Failed on open: ", err) } data := make([]byte, 100) _, err = syscall.Read(fd, data) if err != nil { fmt.Println("Failed on read: ", err) } syscall.Close(fd) }
When we run the program above, we get no errors, which is the correct behavior.
Now I am changing the syscall.Open line as follows.
fd, err := syscall.Open("Foo.txt", syscall.O_RDONLY | syscall.O_SYNC | syscall.O_DIRECT, 0)
When I run the program again, I get the following (unwanted) output.
Failed on read: invalid argument
How to pass the flags syscall.O_SYNC and syscall.O_DIRECT , as indicated in the open man page to skip the file system cache?
Please note that I use the syscall interface directly, and not through the os interface, because I could not find a way to pass these flags into the functions provided by os , but I am open to solutions that use os provided that they work correctly so that disable file system cache while reading.
Please note that I am running on Ubuntu 14.04 with ext4 as my file system.
Update . I tried using the @Nick Craig-Wood package in the code below.
package main import "io" import "github.com/ncw/directio" import "os" import "fmt" func main() { in, err := directio.OpenFile("Foo.txt", os.O_RDONLY, 0666) if err != nil { fmt.Println("Error on open: ", err) } block := directio.AlignedBlock(directio.BlockSize) _, err = io.ReadFull(in, block) if err != nil { fmt.Println("Error on read: ", err) } }
The next way out
Error on read: unexpected EOF