Skip to content

Instantly share code, notes, and snippets.

@holiman
Created October 22, 2021 11:17
Show Gist options
  • Save holiman/ce51945058d2befaf718de3804fb9352 to your computer and use it in GitHub Desktop.
Save holiman/ce51945058d2befaf718de3804fb9352 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"strconv"
)
var (
// dumpMagic is a dataset dump header to sanity check a data dump.
dumpMagic = []uint32{0xbaddcafe, 0xfee1dead}
)
func main() {
if err := runit(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runit() error {
dir := os.TempDir()
size := 10 * 1_073_739_912 // A bit north of 10Gb
path := filepath.Join(dir, fmt.Sprintf("cachefile"))
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
// Create a huge temporary empty file to fill with data
temp := path + "." + strconv.Itoa(rand.Int())
dump, err := os.Create(temp)
if err != nil {
return err
}
fSize := uint64(len(dumpMagic))*4 + uint64(size)
if p := uint64(os.Getpagesize()); p > 0 {
fSize = p * ((fSize + p - 1) / p)
}
// write to the end of the file
if _, err := dump.WriteAt(make([]byte, 1000), int64(fSize-1000)); err != nil {
return fmt.Errorf("Writing to EOF failed: %v", err)
}
fmt.Printf("Writing to EOF worked fine\n")
// fill with zeroes
for i := uint64(0); i < fSize-1000; i += 1000 {
if _, err := dump.Write(make([]byte, 1000)); err != nil {
return fmt.Errorf("Error at i=%d: %w", i, err)
}
}
err = dump.Sync()
fmt.Printf("Did sync, err = %v\n", err)
fmt.Printf("Did truncate file %s size %d pageSize %d\n", path, int64(fSize), os.Getpagesize())
return nil
}
@holiman
Copy link
Author

holiman commented Oct 22, 2021

Writing to EOF worked fine
Error: Error at i=1071734000: write /tmp/cachefile.5577006791947779410: no space left on device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment