aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--checkdir.go36
-rw-r--r--init_dir.go33
-rw-r--r--main.go2
-rw-r--r--mount.go4
4 files changed, 35 insertions, 40 deletions
diff --git a/checkdir.go b/checkdir.go
deleted file mode 100644
index 63dfef0..0000000
--- a/checkdir.go
+++ /dev/null
@@ -1,36 +0,0 @@
-package main
-
-import (
- "fmt"
- "io/ioutil"
- "os"
-)
-
-// checkDirEmpty - check if "dir" exists and is an empty directory.
-// Returns an *os.PathError if Stat() on the path fails.
-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)
-}
-
-// checkDir - check if "dir" exists and is a directory
-func checkDir(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
-}
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)
diff --git a/main.go b/main.go
index e8d2364..ea5efb3 100644
--- a/main.go
+++ b/main.go
@@ -174,7 +174,7 @@ func main() {
}
// Check that CIPHERDIR exists
args.cipherdir, _ = filepath.Abs(flagSet.Arg(0))
- err = checkDir(args.cipherdir)
+ err = isDir(args.cipherdir)
if err != nil {
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
os.Exit(exitcodes.CipherDir)
diff --git a/mount.go b/mount.go
index 96ff7f6..c5b1c05 100644
--- a/mount.go
+++ b/mount.go
@@ -58,9 +58,9 @@ func doMount(args *argContainer) {
os.Exit(exitcodes.MountPoint)
}
if args.nonempty {
- err = checkDir(args.mountpoint)
+ err = isDir(args.mountpoint)
} else {
- err = checkDirEmpty(args.mountpoint)
+ err = isDirEmpty(args.mountpoint)
// OSXFuse will create the mountpoint for us ( https://github.com/rfjakob/gocryptfs/issues/194 )
if runtime.GOOS == "darwin" && os.IsNotExist(err) {
tlog.Info.Printf("Mountpoint %q does not exist, but should be created by OSXFuse",