github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/sysutil/large_file_naive.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // +build !linux 12 13 package sysutil 14 15 import ( 16 "os" 17 18 "github.com/cockroachdb/errors" 19 ) 20 21 // CreateLargeFile creates a large file at the given path with bytes size. On 22 // Linux, it uses the fallocate syscall to efficiently create the file. On other 23 // platforms, it naively writes the specified number of bytes, which can take a 24 // long time when the number of bytes is large. 25 func CreateLargeFile(path string, bytes int64) error { 26 f, err := os.Create(path) 27 if err != nil { 28 return errors.Wrapf(err, "failed to create file %s", path) 29 } 30 defer f.Close() 31 sixtyFourMB := make([]byte, 64<<20) 32 for bytes > 0 { 33 z := sixtyFourMB 34 if bytes < int64(len(z)) { 35 z = sixtyFourMB[:bytes] 36 } 37 if _, err := f.Write(z); err != nil { 38 return err 39 } 40 bytes -= int64(len(z)) 41 } 42 return f.Sync() 43 }