Skip to content

Commit

Permalink
main: also accept options at the end via "-o"
Browse files Browse the repository at this point in the history
For compatability with mount(1), options are also accepted as
"-o COMMA-SEPARATED-OPTIONS" at the end of the command line.
For example, "-o q,zerokey" is equivalent to "-q -zerokey".
  • Loading branch information
rfjakob committed Oct 9, 2016
1 parent 25a8802 commit 9cf3ced
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Documentation/MANPAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gocryptfs -init [OPTIONS] CIPHERDIR
Mount
-----

gocryptfs [OPTIONS] CIPHERDIR MOUNTPOINT
gocryptfs [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]

Change password
---------------
Expand Down Expand Up @@ -143,6 +143,12 @@ useful in regression testing.
automated testing as it does not provide any security.


Comma-Separated-Options:

For compatability with mount(1), options are also accepted as
"-o COMMA-SEPARATED-OPTIONS" at the end of the command line.
For example, "-o q,zerokey" is equivalent to "-q -zerokey".

EXAMPLES
========

Expand Down
25 changes: 25 additions & 0 deletions cli_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"os"
"strconv"
"strings"

"github.com/rfjakob/gocryptfs/internal/configfile"
"github.com/rfjakob/gocryptfs/internal/prefer_openssl"
Expand All @@ -27,8 +28,32 @@ type argContainer struct {

var flagSet *flag.FlagSet

// prefixOArgs transform options passed via "-o foo,bar" into regular options
// like "-foo -bar" and prefixes them to the command line.
func prefixOArgs(osArgs []string) []string {
l := len(osArgs)
// Need at least 3, example: gocryptfs -o foo,bar
if l < 3 {
return osArgs
}
if osArgs[l-2] != "-o" {
return osArgs
}
oOpts := strings.Split(osArgs[l-1], ",")
osArgs = osArgs[:l-2]
newArgs := []string{osArgs[0]}
// Add options from "-o"
for _, a := range oOpts {
newArgs = append(newArgs, "-"+a)
}
newArgs = append(newArgs, osArgs[1:]...)
return newArgs
}

// parseCliOpts - parse command line options (i.e. arguments that start with "-")
func parseCliOpts() (args argContainer) {
os.Args = prefixOArgs(os.Args)

var err error
var opensslAuto string

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func usageText() {
printVersion()
fmt.Printf(`
Usage: %s -init|-passwd [OPTIONS] CIPHERDIR
or %s [OPTIONS] CIPHERDIR MOUNTPOINT
or %s [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]
Options:
`, tlog.ProgramName, tlog.ProgramName)
Expand Down Expand Up @@ -221,7 +221,7 @@ func main() {
prettyArgs = prettyArgs[1 : len(prettyArgs)-1]
tlog.Info.Printf("Wrong number of arguments (have %d, want 2). You passed: %s",
flagSet.NArg(), prettyArgs)
tlog.Fatal.Printf("Usage: %s [OPTIONS] CIPHERDIR MOUNTPOINT", tlog.ProgramName)
tlog.Fatal.Printf("Usage: %s [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]", tlog.ProgramName)
os.Exit(ErrExitUsage)
}
os.Exit(doMount(&args))
Expand Down

0 comments on commit 9cf3ced

Please sign in to comment.