aboutsummaryrefslogtreecommitdiff
path: root/init_dir.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2018-04-01 12:31:44 +0200
committerJakob Unterwurzacher2018-04-01 12:31:44 +0200
commit85056def909c994b66c40763f6d6c2d41c7a61d0 (patch)
tree2ced74b3bd968a45a0b810666db487473b9758ce /init_dir.go
parent5da5e467a6c92a658b00e2f6a94ed03a6fcd3bf0 (diff)
main: move and rename checkDir*() helper
To avoid confusion with fsck, rename to isDir*() and move the functions into init_dir.go.
Diffstat (limited to 'init_dir.go')
-rw-r--r--init_dir.go33
1 files changed, 32 insertions, 1 deletions
diff --git a/init_dir.go b/init_dir.go
index 16ece1a..0e1ad95 100644
--- a/init_dir.go
+++ b/init_dir.go
@@ -1,6 +1,8 @@
package main
import (
+ "fmt"
+ "io/ioutil"
"os"
"path/filepath"
"strings"
@@ -12,6 +14,35 @@ import (
"github.com/rfjakob/gocryptfs/internal/tlog"
)
+// isDirEmpty checks if "dir" exists and is an empty directory.
+// Returns an *os.PathError if Stat() on the path fails.
+func isDirEmpty(dir string) error {
+ err := isDir(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)
+}
+
+// isDir checks if "dir" exists and is a directory.
+func isDir(dir string) error {
+ fi, err := os.Stat(dir)
+ if err != nil {
+ return err
+ }
+ if !fi.IsDir() {
+ return fmt.Errorf("%s is not a directory", dir)
+ }
+ return nil
+}
+
// initDir prepares a directory for use as a gocryptfs storage directory.
// In forward mode, this means creating the gocryptfs.conf and gocryptfs.diriv
// files in an empty directory.
@@ -26,7 +57,7 @@ func initDir(args *argContainer) {
os.Exit(exitcodes.Init)
}
} else {
- err = checkDirEmpty(args.cipherdir)
+ err = isDirEmpty(args.cipherdir)
if err != nil {
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
os.Exit(exitcodes.Init)