blob: 50bd84f02ada8fcb540e24458952128838a59bd0 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// 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
17package fileutil
18
19import (
20 "os"
21 "syscall"
22)
23
24func 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
38func 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}