init: Check if dir is empty part II (done)

This commit is contained in:
Jakob Unterwurzacher 2015-09-14 22:56:59 +02:00
parent fdc0fef6ee
commit 69b3d53f21
1 changed files with 26 additions and 15 deletions

41
main.go
View File

@ -25,7 +25,7 @@ import (
const ( const (
USE_CLUEFS = false // Use cluefs or pathfs FUSE frontend USE_CLUEFS = false // Use cluefs or pathfs FUSE frontend
USE_OPENSSL = true // 3x speed increase USE_OPENSSL = true // 3x speed increase compared to Go's built-in GCM
PATHFS_DEBUG = false PATHFS_DEBUG = false
PROGRAM_NAME = "gocryptfs" PROGRAM_NAME = "gocryptfs"
@ -42,6 +42,26 @@ const (
ERREXIT_PASSWORD = 9 ERREXIT_PASSWORD = 9
) )
func initDir(dirArg string) {
dir, _ := filepath.Abs(dirArg)
if dirEmpty(dir) == false {
fmt.Printf("Error: Directory \"%s\" is not empty\n", dirArg)
os.Exit(ERREXIT_INIT)
}
confName := filepath.Join(dir, cryptfs.ConfDefaultName)
fmt.Printf("Choose a password for protecting your files.\n")
password := readPasswordTwice()
err := cryptfs.CreateConfFile(confName, password)
if err != nil {
fmt.Println(err)
os.Exit(ERREXIT_INIT)
}
fmt.Printf("The filesystem is now ready for mounting.\n")
os.Exit(0)
}
func main() { func main() {
// Parse command line arguments // Parse command line arguments
var debug bool var debug bool
@ -58,17 +78,7 @@ func main() {
fmt.Printf("usage: %s --init CIPHERDIR\n", PROGRAM_NAME) fmt.Printf("usage: %s --init CIPHERDIR\n", PROGRAM_NAME)
os.Exit(ERREXIT_USAGE) os.Exit(ERREXIT_USAGE)
} }
dir, _ := filepath.Abs(flag.Arg(0)) initDir(flag.Arg(0))
filename := filepath.Join(dir, cryptfs.ConfDefaultName)
fmt.Printf("Choose a password for protecting your files.\n")
password := readPasswordTwice()
err := cryptfs.CreateConfFile(filename, password)
if err != nil {
fmt.Println(err)
os.Exit(ERREXIT_INIT)
}
fmt.Printf("The filesystem is now ready for mounting.\n")
os.Exit(0)
} }
if flag.NArg() < 2 { if flag.NArg() < 2 {
fmt.Printf("usage: %s CIPHERDIR MOUNTPOINT\n", PROGRAM_NAME) fmt.Printf("usage: %s CIPHERDIR MOUNTPOINT\n", PROGRAM_NAME)
@ -152,15 +162,16 @@ func readPassword() string {
return string(p) return string(p)
} }
func dirEmpty(dir string) { func dirEmpty(dir string) bool {
entries, err := ioutil.ReadDir(dir) entries, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(ERREXIT_CIPHERDIR) os.Exit(ERREXIT_CIPHERDIR)
} }
for _, e := range(entries) { if len(entries) == 0 {
fmt.Println(e.Name()) return true
} }
return false
} }
func cluefsFrontend(key []byte, cipherdir string, mountpoint string) { func cluefsFrontend(key []byte, cipherdir string, mountpoint string) {