github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/greenhouse/diskutil/diskutil.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package diskutil implements disk related utilities for greenhouse 18 package diskutil 19 20 import ( 21 "syscall" 22 "time" 23 24 "github.com/djherbis/atime" 25 log "github.com/sirupsen/logrus" 26 ) 27 28 // GetDiskUsage wraps syscall.Statfs for usage in GCing the disk 29 func GetDiskUsage(path string) (percentBlocksFree float64, bytesFree, bytesUsed uint64, err error) { 30 var stat syscall.Statfs_t 31 err = syscall.Statfs(path, &stat) 32 if err != nil { 33 return 0, 0, 0, err 34 } 35 percentBlocksFree = float64(stat.Bfree) / float64(stat.Blocks) * 100 36 bytesFree = stat.Bfree * uint64(stat.Bsize) 37 bytesUsed = (stat.Blocks - stat.Bfree) * uint64(stat.Bsize) 38 return percentBlocksFree, bytesFree, bytesUsed, nil 39 } 40 41 // GetATime the atime for a file, logging errors instead of failing 42 // and returning defaultTime instead 43 func GetATime(path string, defaultTime time.Time) time.Time { 44 at, err := atime.Stat(path) 45 if err != nil { 46 log.WithError(err).Errorf("Could not get atime for %s", path) 47 return defaultTime 48 } 49 return at 50 }