I am intended to use fdatasync in a system like log or diskqueue. The first thing is to create a 10MB file with "000000..." in file system like ext4. But I don't know how to do it properly.
jnml@fsc-r630:~/src/tmp/SO/16797380$ ls -l
celkem 4
-rw-rw-r-- 1 jnml jnml 186 kvě 29 07:54 main.go
jnml@fsc-r630:~/src/tmp/SO/16797380$ cat main.go
package main
import (
"log"
"os"
)
func main() {
f, err := os.Create("foo.bar")
if err != nil {
log.Fatal(err)
}
if err := f.Truncate(1e7); err != nil {
log.Fatal(err)
}
}
jnml@fsc-r630:~/src/tmp/SO/16797380$ go run main.go
jnml@fsc-r630:~/src/tmp/SO/16797380$ ls -l
celkem 4
-rw-rw-r-- 1 jnml jnml 10000000 kvě 29 07:55 foo.bar
-rw-rw-r-- 1 jnml jnml 186 kvě 29 07:54 main.go
jnml@fsc-r630:~/src/tmp/SO/16797380$
Try this:
package foo
import "io"
func WriteBytes(w io.Writer, c byte, n uint) {
buf := make([]byte,0x1000)
for i := 0 ; i < len(buf) ; i++ {
buf[i] = c
}
for i := 0 ; i < n >> 24 ; i++ {
w.Write(buf)
}
w.Write(buf[:n & 0xfff])
}
If you are using unix, then you can create a sparse file very quickly. A sparse file is filled with zero (ascii NUL) and doesn't actually take up the disk space until it is written to, but reads correctly.
package main
import (
"log"
"os"
)
func main() {
size := int64(10 * 1024 * 1024)
fd, err := os.Create("output")
if err != nil {
log.Fatal("Failed to create output")
}
_, err = fd.Seek(size-1, 0)
if err != nil {
log.Fatal("Failed to seek")
}
_, err = fd.Write([]byte{0})
if err != nil {
log.Fatal("Write failed")
}
err = fd.Close()
if err != nil {
log.Fatal("Failed to close file")
}
}
Which produces a 10MB file called output
.
$ ls -l output
-rw-r--r-- 1 user user 10485760 May 28 18:58 output
$ du -hs output
4.0K output
This will create a 10000000 byte file in the current directory named testfile
.
package main
import (
"fmt"
"os"
)
func main() {
data := make([]byte, int(1e7), int(1e7)) // Initialize an empty byte slice
f, err := os.Create("testfile")
if err != nil {
fmt.Printf("Error: %s", err)
}
defer f.Close()
size, err := f.Write(data) // Write it to the file
if err != nil {
fmt.Printf("Error: %s", err)
}
fmt.Printf("%d bytes written", size)
}
Hope that helps.