main: move initDir into its own file
This commit is contained in:
parent
9ad49088fa
commit
72efa5c9b1
10
cli_args.go
10
cli_args.go
@ -10,20 +10,22 @@ import (
|
|||||||
"github.com/rfjakob/gocryptfs/internal/tlog"
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// argContainer stores the parsed CLI arguments
|
// argContainer stores the parsed CLI options and arguments
|
||||||
type argContainer struct {
|
type argContainer struct {
|
||||||
debug, init, zerokey, fusedebug, openssl, passwd, foreground, version,
|
debug, init, zerokey, fusedebug, openssl, passwd, foreground, version,
|
||||||
plaintextnames, quiet, nosyslog, wpanic,
|
plaintextnames, quiet, nosyslog, wpanic,
|
||||||
longnames, allow_other, ro, reverse bool
|
longnames, allow_other, ro, reverse bool
|
||||||
masterkey, mountpoint, cipherdir, cpuprofile, config, extpass,
|
masterkey, mountpoint, cipherdir, cpuprofile, extpass,
|
||||||
memprofile, o string
|
memprofile, o string
|
||||||
|
// Configuration file name override
|
||||||
|
config string
|
||||||
notifypid, scryptn int
|
notifypid, scryptn int
|
||||||
}
|
}
|
||||||
|
|
||||||
var flagSet *flag.FlagSet
|
var flagSet *flag.FlagSet
|
||||||
|
|
||||||
// parseCliArgs - parse command line arguments
|
// parseCliOpts - parse command line options (i.e. arguments that start with "-")
|
||||||
func parseCliArgs() (args argContainer) {
|
func parseCliOpts() (args argContainer) {
|
||||||
var err error
|
var err error
|
||||||
var opensslAuto string
|
var opensslAuto string
|
||||||
|
|
||||||
|
56
init_dir.go
Normal file
56
init_dir.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// initDir initializes an empty directory for use as a gocryptfs cipherdir.
|
||||||
|
func initDir(args *argContainer) {
|
||||||
|
err := checkDirEmpty(args.cipherdir)
|
||||||
|
if err != nil {
|
||||||
|
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
|
||||||
|
os.Exit(ERREXIT_INIT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create gocryptfs.conf
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
tlog.Fatal.Println(err)
|
||||||
|
os.Exit(ERREXIT_INIT)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !args.plaintextnames {
|
||||||
|
// Create gocryptfs.diriv in the root dir
|
||||||
|
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)
|
||||||
|
}
|
49
main.go
49
main.go
@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
||||||
"github.com/rfjakob/gocryptfs/internal/fusefrontend"
|
"github.com/rfjakob/gocryptfs/internal/fusefrontend"
|
||||||
"github.com/rfjakob/gocryptfs/internal/fusefrontend_reverse"
|
"github.com/rfjakob/gocryptfs/internal/fusefrontend_reverse"
|
||||||
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
|
||||||
"github.com/rfjakob/gocryptfs/internal/readpassword"
|
"github.com/rfjakob/gocryptfs/internal/readpassword"
|
||||||
"github.com/rfjakob/gocryptfs/internal/tlog"
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
||||||
)
|
)
|
||||||
@ -50,50 +49,6 @@ var GitVersionFuse = pleaseBuildBash
|
|||||||
// Unix timestamp, set by build.bash
|
// Unix timestamp, set by build.bash
|
||||||
var BuildTime = "0"
|
var BuildTime = "0"
|
||||||
|
|
||||||
// initDir initializes an empty directory for use as a gocryptfs cipherdir.
|
|
||||||
func initDir(args *argContainer) {
|
|
||||||
err := checkDirEmpty(args.cipherdir)
|
|
||||||
if err != nil {
|
|
||||||
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
|
|
||||||
os.Exit(ERREXIT_INIT)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create gocryptfs.conf
|
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
tlog.Fatal.Println(err)
|
|
||||||
os.Exit(ERREXIT_INIT)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !args.plaintextnames {
|
|
||||||
// Create gocryptfs.diriv in the root dir
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func usageText() {
|
func usageText() {
|
||||||
printVersion()
|
printVersion()
|
||||||
fmt.Printf(`
|
fmt.Printf(`
|
||||||
@ -157,7 +112,9 @@ func main() {
|
|||||||
runtime.GOMAXPROCS(4)
|
runtime.GOMAXPROCS(4)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
args := parseCliArgs()
|
// Parse all command-line options (i.e. arguments starting with "-")
|
||||||
|
// into "args". Path arguments are parsed below.
|
||||||
|
args := parseCliOpts()
|
||||||
|
|
||||||
// Fork a child into the background if "-f" is not set AND we are mounting
|
// Fork a child into the background if "-f" is not set AND we are mounting
|
||||||
// a filesystem. The child will do all the work.
|
// a filesystem. The child will do all the work.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user