2015-10-11 18:33:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-11 18:51:56 +02:00
|
|
|
"io/ioutil"
|
2015-10-11 18:33:28 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2015-11-14 21:25:10 +01:00
|
|
|
// checkDirEmpty - check if "dir" exists and is an empty directory
|
2015-10-11 18:33:28 +02:00
|
|
|
func checkDirEmpty(dir string) error {
|
|
|
|
err := checkDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
entries, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("directory %s not empty", dir)
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:25:10 +01:00
|
|
|
// checkDir - check if "dir" exists and is a directory
|
2015-10-11 18:33:28 +02:00
|
|
|
func checkDir(dir string) error {
|
|
|
|
fi, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-11 18:51:56 +02:00
|
|
|
if !fi.IsDir() {
|
2015-11-01 11:56:33 +01:00
|
|
|
return fmt.Errorf("%s is not a directory", dir)
|
2015-10-11 18:33:28 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|