本文整理汇总了Golang中syscall.Fallocate函数的典型用法代码示例。如果您正苦于以下问题:Golang Fallocate函数的具体用法?Golang Fallocate怎么用?Golang Fallocate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Fallocate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFallocate
func TestFallocate(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
if ts.state.KernelSettings().Minor < 19 {
t.Log("FUSE does not support Fallocate.")
return
}
rwFile, err := os.OpenFile(ts.mnt+"/file", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
t.Fatalf("OpenFile failed: %v", err)
}
defer rwFile.Close()
err = syscall.Fallocate(int(rwFile.Fd()), 0, 1024, 4096)
if err != nil {
t.Fatalf("FUSE Fallocate failed: %v", err)
}
fi, err := os.Lstat(ts.orig + "/file")
if err != nil {
t.Fatalf("Lstat failed: %v", err)
}
if fi.Size() < (1024 + 4096) {
t.Fatalf("fallocate should have changed file size. Got %d bytes",
fi.Size())
}
}
示例2: New
func New(capacity int, filename string) (*Buffer, error) {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
return nil, ErrFileExists
}
f, err := os.Create(filename)
if err != nil {
return nil, err
}
defer f.Close()
fsize := capacity + metadata
if err := syscall.Fallocate(int(f.Fd()), 0, 0, int64(fsize)); err != nil {
return nil, err
}
data, err := syscall.Mmap(
int(f.Fd()), 0, fsize,
syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED,
)
// TODO(ashish): Call msync periodically.
if err != nil {
return nil, err
}
b := &Buffer{
capacity: uint64(capacity), // since it's used with uint64s a lot more
filename: f.Name(),
data: data,
}
b.updateMeta()
return b, nil
}
示例3: fallocate
func fallocate(f *os.File, sz int64) error {
err := syscall.Fallocate(int(f.Fd()), 0, 0, sz)
if err == syscall.ENOTSUP {
return f.Truncate(sz)
}
return err
}
示例4: punchHoleLinux
// puncHoleLinux punches a hole into the given file starting at offset,
// measuring "size" bytes
func punchHoleLinux(file *os.File, offset int64, size int64) error {
err := syscall.Fallocate(int(file.Fd()),
falloc_fl_punch_hole|falloc_fl_keep_size,
offset, size)
if err == syscall.ENOSYS || err == syscall.EOPNOTSUPP {
return errNoPunch
}
return err
}
示例5: Allocate
func (f *LoopbackFile) Allocate(off uint64, sz uint64, mode uint32) Status {
f.lock.Lock()
err := syscall.Fallocate(int(f.File.Fd()), mode, int64(off), int64(sz))
f.lock.Unlock()
if err != nil {
return ToStatus(err)
}
return OK
}
示例6: fallocateRetry
// fallocateRetry - syscall.Fallocate() with retry for EINTR.
func fallocateRetry(fd int, mode uint32, off int64, len int64) (err error) {
for {
err = syscall.Fallocate(fd, mode, off, len)
if err == syscall.EINTR {
continue
}
return err
}
}
示例7: Allocate
func (f *AzukiFile) Allocate(off uint64, sz uint64, mode uint32) fuse.Status {
f.lock.Lock()
err := syscall.Fallocate(int(f.File.Fd()), mode, int64(off), int64(sz))
f.lock.Unlock()
if err != nil {
return fuse.ToStatus(err)
}
go event.Notify(event.Fallocate, f.File.Name())
return fuse.OK
}
示例8: prealloc
// prealloc - preallocate space without changing the file size. This prevents
// us from running out of space in the middle of an operation.
func prealloc(fd int, off int64, len int64) (err error) {
for {
err = syscall.Fallocate(fd, FALLOC_FL_KEEP_SIZE, off, len)
if err == syscall.EINTR {
// fallocate, like many syscalls, can return EINTR. This is not an
// error and just signifies that the operation was interrupted by a
// signal and we should try again.
continue
}
return err
}
}
示例9: preallocFixed
func preallocFixed(f *os.File, sizeInBytes int64) error {
// use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE
err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes)
if err != nil {
errno, ok := err.(syscall.Errno)
// treat not supported as nil error
if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) {
return nil
}
}
return err
}
示例10: createBackingFile
func createBackingFile(backing string) error {
flags := syscall.O_RDWR | syscall.O_CREAT | syscall.O_EXCL
file, err := os.OpenFile(backing, flags, 0755)
if err != nil {
return fmt.Errorf("Failed to create backing file %s: %s.", backing, err)
}
err = syscall.Fallocate(int(file.Fd()), 0, 0, fileSizeInBytes)
if err != nil {
return fmt.Errorf("Failed to allocate %s: %s.", backing, err)
}
return nil
}
示例11: preallocExtend
func preallocExtend(f *os.File, sizeInBytes int64) error {
// use mode = 0 to change size
err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes)
if err != nil {
errno, ok := err.(syscall.Errno)
// treat not support as nil error
if ok && errno == syscall.ENOTSUP {
return preallocExtendTrunc(f, sizeInBytes)
}
}
return err
}
示例12: preallocExtend
func preallocExtend(f *os.File, sizeInBytes int64) error {
// use mode = 0 to change size
err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes)
if err != nil {
errno, ok := err.(syscall.Errno)
// not supported; fallback
// fallocate EINTRs frequently in some enviroments; fallback
if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) {
return preallocExtendTrunc(f, sizeInBytes)
}
}
return err
}
示例13: Preallocate
// Preallocate tries to allocate the space for given
// file. This operation is only supported on linux by a
// few filesystems (btrfs, ext4, etc.).
// If the operation is unsupported, no error will be returned.
// Otherwise, the error encountered will be returned.
func Preallocate(f *os.File, sizeInBytes int) error {
// use mode = 1 to keep size
// see FALLOC_FL_KEEP_SIZE
err := syscall.Fallocate(int(f.Fd()), 1, 0, int64(sizeInBytes))
if err != nil {
errno, ok := err.(syscall.Errno)
// treat not support as nil error
if ok && errno == syscall.ENOTSUP {
return nil
}
return err
}
return nil
}
示例14: main
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
status := 0
for _, name := range flag.Args() {
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0644)
if ek(err) {
continue
}
ek(syscall.Fallocate(int(f.Fd()), 0, *offset, *size))
}
os.Exit(status)
}
示例15: NewOutputValueFile
// NewOutputValueFile creates an entity implmenting the OutputValueFile interface
func NewOutputValueFile(fileSpaceInfo tools.FileSpaceInfo) (OutputValueFile, error) {
var valueFile outputValueFile
var err error
valueFile.creationTime = tools.Timestamp()
valueFile.md5Sum = md5.New()
valueFile.collectionIDSet = make(map[uint32]struct{})
repositoryPath := os.Getenv("NIMBUSIO_REPOSITORY_PATH")
if valueFile.spaceID, err = fileSpaceInfo.FindMaxAvailSpaceID(tools.FileSpaceJournal); err != nil {
return nil, err
}
if err = valueFile.insertValueFileRow(); err != nil {
return nil, err
}
valueFile.filePath = tools.ComputeValueFilePath(repositoryPath, valueFile.spaceID,
valueFile.valueFileID)
fog.Debug("NewOutputValueFile %s", valueFile.filePath)
dirPath := path.Dir(valueFile.filePath)
if err = os.MkdirAll(dirPath, os.ModeDir|0755); err != nil {
return nil, fmt.Errorf("os.MkdirAll(%s...", err)
}
valueFile.fileHandle, err = os.Create(valueFile.filePath)
if err != nil {
return nil, fmt.Errorf("os.Create(%s) %s", valueFile.filePath, err)
}
err = syscall.Fallocate(int(valueFile.fileHandle.Fd()), 0, 0,
int64(MaxValueFileSize))
if err != nil {
return nil, fmt.Errorf("Fallocate failed %s", err)
}
valueFile.enableFsync = os.Getenv("NIMBUSIO_ENABLE_FSYNC") == "1"
fog.Info("NewOutputValueFile: NIMBUSIO_ENABLE_FSYNC = %t", valueFile.enableFsync)
return &valueFile, nil
}