main: also accept options at the end via "-o"

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".
This commit is contained in:
Jakob Unterwurzacher 2016-10-09 19:38:49 +02:00
parent 25a8802403
commit 9cf3ced0ce
3 changed files with 34 additions and 3 deletions

View File

@ -18,7 +18,7 @@ gocryptfs -init [OPTIONS] CIPHERDIR
Mount
-----
gocryptfs [OPTIONS] CIPHERDIR MOUNTPOINT
gocryptfs [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]
Change password
---------------
@ -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
========

View File

@ -4,6 +4,7 @@ import (
"flag"
"os"
"strconv"
"strings"
"github.com/rfjakob/gocryptfs/internal/configfile"
"github.com/rfjakob/gocryptfs/internal/prefer_openssl"
@ -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

View File

@ -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)
@ -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))