github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/sysutil/large_file_linux.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  	"golang.org/x/sys/unix"
    20  )
    21  
    22  // CreateLargeFile creates a large file at the given path with bytes size. On
    23  // Linux, it uses the fallocate syscall to efficiently create a file of the
    24  // given size. On other platforms, it naively writes the specified number of
    25  // bytes, which can take a long time.
    26  func CreateLargeFile(path string, bytes int64) error {
    27  	f, err := os.Create(path)
    28  	if err != nil {
    29  		return errors.Wrapf(err, "failed to create file %s", path)
    30  	}
    31  	defer f.Close()
    32  	if err := unix.Fallocate(int(f.Fd()), 0, 0, bytes); err != nil {
    33  		return err
    34  	}
    35  	return f.Sync()
    36  }