github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/utils/dir.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 package utils 16 17 import ( 18 "os" 19 "os/exec" 20 "strconv" 21 "strings" 22 ) 23 24 func GetDirSize(dir string) int64 { 25 if _, err := os.Stat(dir); os.IsNotExist(err) { 26 return 0 27 } 28 res := strings.Split(Command("du", "-sb", dir), "\t") 29 if len(res) < 2 || len(res) > 2 { 30 return 0 31 } 32 size, err := strconv.ParseInt(res[0], 10, 64) 33 if err != nil { 34 return 0 35 } 36 return size 37 } 38 39 func Command(key string, arg ...string) string { 40 cmd := exec.Command(key, arg...) 41 b, _ := cmd.CombinedOutput() 42 return strings.TrimSpace(string(b)) 43 } 44 45 func IsFileNotExist(name string) bool { 46 if len(name) == 0 { 47 return true 48 } 49 _, err := os.Stat(name) 50 return err != nil && os.IsNotExist(err) 51 } 52 53 func IsFileExist(name string) bool { 54 if len(name) == 0 { 55 return false 56 } 57 _, err := os.Stat(name) 58 return err == nil || !os.IsNotExist(err) 59 }