Created
October 22, 2021 11:17
-
-
Save holiman/ce51945058d2befaf718de3804fb9352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Author
holiman
commented
Oct 22, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment