github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/vfs/preallocate_darwin.go (about) 1 // Copyright 2016 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build darwin 16 17 package vfs 18 19 import ( 20 "syscall" 21 "unsafe" 22 ) 23 24 func preallocExtend(fd uintptr, offset, length int64) error { 25 if err := preallocFixed(fd, offset, length); err != nil { 26 return err 27 } 28 return syscall.Ftruncate(int(fd), offset+length) 29 } 30 31 func preallocFixed(fd uintptr, offset, length int64) error { 32 // allocate all requested space or no space at all 33 // TODO: allocate contiguous space on disk with F_ALLOCATECONTIG flag 34 fstore := &syscall.Fstore_t{ 35 Flags: syscall.F_ALLOCATEALL, 36 Posmode: syscall.F_PEOFPOSMODE, 37 Length: length} 38 p := unsafe.Pointer(fstore) 39 _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, fd, uintptr(syscall.F_PREALLOCATE), uintptr(p)) 40 if errno == 0 || errno == syscall.ENOTSUP { 41 return nil 42 } 43 44 // wrong argument to fallocate syscall 45 if errno == syscall.EINVAL { 46 // filesystem "st_blocks" are allocated in the units of 47 // "Allocation Block Size" (run "diskutil info /" command) 48 var stat syscall.Stat_t 49 syscall.Fstat(int(fd), &stat) 50 51 // syscall.Statfs_t.Bsize is "optimal transfer block size" 52 // and contains matching 4096 value when latest OS X kernel 53 // supports 4,096 KB filesystem block size 54 var statfs syscall.Statfs_t 55 syscall.Fstatfs(int(fd), &statfs) 56 blockSize := int64(statfs.Bsize) 57 58 if stat.Blocks*blockSize >= offset+length { 59 // enough blocks are already allocated 60 return nil 61 } 62 } 63 return errno 64 }