github.com/kaituanwang/hyperledger@v2.0.1+incompatible/bccsp/utils/io.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 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 utils 18 19 import ( 20 "io" 21 "os" 22 ) 23 24 // DirMissingOrEmpty checks is a directory is missing or empty 25 func DirMissingOrEmpty(path string) (bool, error) { 26 dirExists, err := DirExists(path) 27 if err != nil { 28 return false, err 29 } 30 if !dirExists { 31 return true, nil 32 } 33 34 dirEmpty, err := DirEmpty(path) 35 if err != nil { 36 return false, err 37 } 38 if dirEmpty { 39 return true, nil 40 } 41 return false, nil 42 } 43 44 // DirExists checks if a directory exists 45 func DirExists(path string) (bool, error) { 46 _, err := os.Stat(path) 47 if err == nil { 48 return true, nil 49 } 50 if os.IsNotExist(err) { 51 return false, nil 52 } 53 return false, err 54 } 55 56 // DirEmpty checks if a directory is empty 57 func DirEmpty(path string) (bool, error) { 58 f, err := os.Open(path) 59 if err != nil { 60 return false, err 61 } 62 defer f.Close() 63 64 _, err = f.Readdir(1) 65 if err == io.EOF { 66 return true, nil 67 } 68 return false, err 69 }