go.etcd.io/etcd@v3.3.27+incompatible/pkg/fileutil/preallocate_unix.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 linux 16 17 package fileutil 18 19 import ( 20 "os" 21 "syscall" 22 ) 23 24 func preallocExtend(f *os.File, sizeInBytes int64) error { 25 // use mode = 0 to change size 26 err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes) 27 if err != nil { 28 errno, ok := err.(syscall.Errno) 29 // not supported; fallback 30 // fallocate EINTRs frequently in some environments; fallback 31 if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) { 32 return preallocExtendTrunc(f, sizeInBytes) 33 } 34 } 35 return err 36 } 37 38 func preallocFixed(f *os.File, sizeInBytes int64) error { 39 // use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE 40 err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes) 41 if err != nil { 42 errno, ok := err.(syscall.Errno) 43 // treat not supported as nil error 44 if ok && errno == syscall.ENOTSUP { 45 return nil 46 } 47 } 48 return err 49 }