libgocryptfs/init_dir.go

69 lines
2.1 KiB
Go
Raw Normal View History

2016-09-20 19:59:08 +02:00
package main
import (
"os"
"path/filepath"
"strings"
"github.com/rfjakob/gocryptfs/internal/configfile"
"github.com/rfjakob/gocryptfs/internal/nametransform"
"github.com/rfjakob/gocryptfs/internal/readpassword"
"github.com/rfjakob/gocryptfs/internal/tlog"
)
2016-09-20 20:15:55 +02:00
// 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.
// In reverse mode, we create .gocryptfs.reverse.conf and the directory does
// not to be empty.
2016-09-20 19:59:08 +02:00
func initDir(args *argContainer) {
2016-09-20 20:15:55 +02:00
var err error
if args.reverse {
_, err = os.Stat(args.config)
if err == nil {
tlog.Fatal.Printf("Config file %q already exists", args.config)
os.Exit(ERREXIT_INIT)
}
} else {
2016-09-20 20:15:55 +02:00
err = checkDirEmpty(args.cipherdir)
if err != nil {
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
os.Exit(ERREXIT_INIT)
}
2016-09-20 19:59:08 +02:00
}
2016-09-20 20:15:55 +02:00
// Choose password for config file
2016-09-20 19:59:08 +02:00
if args.extpass == "" {
tlog.Info.Printf("Choose a password for protecting your files.")
} else {
tlog.Info.Printf("Using password provided via -extpass.")
}
password := readpassword.Twice(args.extpass)
creator := tlog.ProgramName + " " + GitVersion
err = configfile.CreateConfFile(args.config, password, args.plaintextnames, args.scryptn, creator, args.reverse)
2016-09-20 19:59:08 +02:00
if err != nil {
tlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
2016-09-20 20:15:55 +02:00
// Forward mode with filename encryption enabled needs a gocryptfs.diriv
// in the root dir
if !args.plaintextnames && !args.reverse {
2016-09-20 19:59:08 +02:00
err = nametransform.WriteDirIV(args.cipherdir)
if err != nil {
tlog.Fatal.Println(err)
os.Exit(ERREXIT_INIT)
}
}
tlog.Info.Printf(tlog.ColorGreen + "The filesystem has been created successfully." + tlog.ColorReset)
wd, _ := os.Getwd()
friendlyPath, _ := filepath.Rel(wd, args.cipherdir)
if strings.HasPrefix(friendlyPath, "../") {
// A relative path that starts with "../" is pretty unfriendly, just
// keep the absolute path.
friendlyPath = args.cipherdir
}
tlog.Info.Printf(tlog.ColorGrey+"You can now mount it using: %s %s MOUNTPOINT"+tlog.ColorReset,
tlog.ProgramName, friendlyPath)
os.Exit(0)
}