aboutsummaryrefslogtreecommitdiff
path: root/ext4
diff options
context:
space:
mode:
Diffstat (limited to 'ext4')
-rw-r--r--ext4/ext4.go155
-rw-r--r--ext4/feature_flag.go6
2 files changed, 48 insertions, 113 deletions
diff --git a/ext4/ext4.go b/ext4/ext4.go
index 063d68c..80d229d 100644
--- a/ext4/ext4.go
+++ b/ext4/ext4.go
@@ -20,124 +20,63 @@
package main
import (
- "flag"
"fmt"
- "io"
- "io/ioutil"
- "os"
- "time"
- "github.com/urfave/cli"
+ "github.com/google/fscrypt/cmd"
)
+// Arguments used with the ext4 enable/disable commands.
var (
- // Setup command parsing
- cmdName = os.Args[0]
- set = flag.NewFlagSet(cmdName, flag.ContinueOnError)
- // Flags for our command
- forceFlag = set.Bool("force", false, "Suppress all warnings and do not prompt")
- versionFlag = set.Bool("version", false, "Print the fscrypt version.")
- helpFlag = set.Bool("help", false, "Print this help text.")
- // fscrypt's version (set by Makefile)
- version string
-)
-
-const (
- manPage = "fscrypt-ext4(8)"
- manBrief = "enable or disable encryption on an ext4 filesystem"
- usageFmt = `
-Usage:
- %[1]s [enable | disable] <mountpoint> [--force]
- %[1]s --help
- %[1]s --version
-
-Arguments:
- <mountpoint> - path to an ext4 filesystem
-`
-)
-
-func printAndExit(err error, printUsage bool) {
- var w io.Writer
- var rc int
- if err == nil {
- w = os.Stdout
- rc = 0
- fmt.Fprintf(w, "%s - %s\n", cmdName, manBrief)
- } else {
- w = os.Stderr
- rc = 1
- fmt.Fprintf(w, "%s: %v\n", cmdName, err)
+ MountpointArg = &cmd.Argument{
+ ArgName: "mountpoint",
+ Usage: "the path to an ext4 filesystem's mountpoint",
}
- if printUsage {
- fmt.Fprintf(w, usageFmt, cmdName)
- fmt.Fprintln(w, "\nOptions:")
- set.VisitAll(func(f *flag.Flag) {
- fmt.Fprintf(w, "\t--%s\n\t\t%s\n", f.Name, f.Usage)
- })
- fmt.Fprintf(w, "\nSee the %s man page for more info.\n", manPage)
+ DeviceArg = &cmd.Argument{
+ ArgName: "device",
+ Usage: "the path to a device containing an ext4 filesystem",
}
- os.Exit(rc)
-}
-
-func main() {
- // Create our command line application
- app := cli.NewApp()
- app.Usage = shortUsage
- app.Authors = Authors
- app.Copyright = apache2GoogleCopyright
-
- // Grab the version and compilation time passed in from the Makefile.
- app.Version = version
- app.Compiled, _ = time.Parse(time.UnixDate, buildTime)
- app.OnUsageError = onUsageError
-
- // Setup global flags
- cli.HelpFlag = helpFlag
- cli.VersionFlag = versionFlag
- cli.VersionPrinter = func(c *cli.Context) {
- cli.HelpPrinter(c.App.Writer, versionInfoTemplate, c.App)
- }
- app.Flags = universalFlags
-
- // We hide the help subcommand so that "fscrypt <command> --help" works
- // and "fscrypt <command> help" does not.
- app.HideHelp = true
+ Ext4Usage = fmt.Sprintf("(%s | %s) [options]", MountpointArg, DeviceArg)
+)
- // Initialize command list and setup all of the commands.
- app.Action = defaultAction
- app.Commands = []cli.Command{Setup, Encrypt, Unlock, Purge, Status, Metadata}
- for i := range app.Commands {
- setupCommand(&app.Commands[i])
- }
+// Commands for running the ext4 enable/disable commands.
+var ()
+
+var Ext4Command = &cmd.Command{
+ Title: "toggle ext4 filesystem encryption flag",
+ UsageLines: []string{
+ fmt.Sprintf("(enable | disable) %s", Ext4Usage),
+ cmd.VersionUsage,
+ },
+ SubCommands: []*cmd.Command{EnableCommand, DisableCommand, cmd.VersionCommand},
+ Arguments: []*cmd.Argument{MountpointArg, DeviceArg},
+ Flags: []cmd.Flag{cmd.ForceFlag, cmd.VerboseFlag, cmd.HelpFlag},
+ ManPage: &cmd.ManPage{
+ Title: "fscrypt-ext4",
+ Section: 8,
+ },
+}
- app.Run(os.Args)
+var EnableCommand = &cmd.Command{
+ Name: "enable",
+ Title: "turn on encryption for an ext4 filesystem",
+ UsageLines: []string{Ext4Usage},
+ InheritArguments: true,
+ InheritFlags: true,
+ Action: func(ctx *cmd.Context) error { return toggleState(ctx, true) },
+}
- set.SetOutput(ioutil.Discard)
- if err := set.Parse(os.Args[1:]); err != nil {
- printAndExit(err, true)
- }
- if *helpFlag {
- printAndExit(nil, true)
- }
- if *versionFlag {
- fmt.Println(version)
- return
- }
- if set.NArg() != 2 {
- printAndExit(fmt.Errorf("expected 2 arguments (got %d)", set.NArg()), true)
- }
+var DisableCommand = &cmd.Command{
+ Name: "disable",
+ Title: "turn off encryption for an ext4 filesystem",
+ UsageLines: []string{Ext4Usage},
+ InheritArguments: true,
+ InheritFlags: true,
+ Action: func(ctx *cmd.Context) error { return toggleState(ctx, false) },
+}
- _, err := NewExt4Filesystem(set.Arg(1))
- if err != nil {
- printAndExit(err, false)
- }
+func main() { Ext4Command.Run() }
- switch command := set.Arg(0); command {
- case "enable":
- fmt.Println("Enabling encryption not implemented")
- case "disable":
- fmt.Println("Disabling encryption not implemented")
- default:
- printAndExit(fmt.Errorf("invalid command %q", command), true)
- }
+func toggleState(ctx *cmd.Context, enable bool) error {
+ fmt.Fprintf(cmd.Output, "Toggle value = %v", enable)
+ return nil
}
diff --git a/ext4/feature_flag.go b/ext4/feature_flag.go
index 4b588d6..ab618e2 100644
--- a/ext4/feature_flag.go
+++ b/ext4/feature_flag.go
@@ -42,11 +42,7 @@ type Ext4Filesystem struct {
// NewExt4Filesystem creates a new Ext4Filesystem from a mountpoint path. Fail
// if the path is not the mountpoint of an ext4 filesystem or cannot be opened.
-func NewExt4Filesystem(mountpoint string) (*Ext4Filesystem, error) {
- mount, err := filesystem.FindMount(set.Arg(1))
- if err != nil {
- return nil, err
- }
+func NewExt4Filesystem(mount *filesystem.Mount) (*Ext4Filesystem, error) {
if mount.Filesystem != "ext4" {
err := fmt.Errorf("%q is not an ext4 filesystem (type %q)", mount.Path, mount.Filesystem)
return nil, err