github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/retention/retutils.go (about) 1 /* 2 Copyright 2023. 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 retention 18 19 import ( 20 "io" 21 "os" 22 "path" 23 24 "github.com/siglens/siglens/pkg/config" 25 ) 26 27 func IsDirEmpty(name string) bool { 28 f, err := os.Open(name) 29 if err != nil { 30 return false 31 } 32 defer f.Close() 33 34 // read in ONLY one file 35 _, err = f.Readdir(1) 36 37 // and if the file is EOF... well, the dir is empty. 38 return err == io.EOF 39 } 40 41 func RecursivelyDeleteParentDirectories(filePath string) { 42 temp := path.Dir(filePath) 43 for { 44 if temp == config.GetDataPath() { 45 break 46 } 47 if IsDirEmpty(temp) { 48 os.RemoveAll(temp) 49 } else { 50 break 51 } 52 temp = path.Dir(temp) 53 } 54 }