aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Richey <joerichey94@gmail.com>2017-10-05 04:55:01 -0700
committerJoseph Richey <joerichey94@gmail.com>2017-10-19 02:22:23 -0700
commit7d16a9fb37b8a6204d76f187444330b8faae4e4e (patch)
tree5fe74c7ba686715f1cf754896db639f2d6a4df38
parentef2407fd0ae0ccc31cd894f0e2f84e0a871547a4 (diff)
ext4: Build working, args parsing
-rw-r--r--Makefile24
-rw-r--r--ext4/ext4.go83
2 files changed, 85 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index a28522e..80354b3 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@
RELEASE_VERSION = 0.2.0
NAME = fscrypt
+EXT4_NAME = $(NAME)-ext4
PAM_NAME = pam_$(NAME)
PAM_MODULE = $(PAM_NAME).so
@@ -27,8 +28,10 @@ DESTDIR ?= /usr/local/bin
PAM_MODULE_DIR ?= /lib/security
PAM_CONFIG_DIR ?= /usr/share/pam-configs
-CMD_PKG = github.com/google/$(NAME)/cmd/$(NAME)
-PAM_PKG = github.com/google/$(NAME)/$(PAM_NAME)
+PKG_DIR = github.com/google/$(NAME)
+CMD_PKG = $(PKG_DIR)/cmd/$(NAME)
+EXT4_PKG =$(PKG_DIR)/ext4
+PAM_PKG = $(PKG_DIR)/$(PAM_NAME)
SRC_FILES = $(shell find . -type f -name '*.go' -o -name "*.h" -o -name "*.c")
GO_FILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
@@ -95,12 +98,15 @@ override GO_FLAGS += --ldflags '$(GO_LINK_FLAGS)'
.PHONY: default all
-default: $(NAME) $(PAM_MODULE)
+default: $(NAME) $(EXT4_NAME) $(PAM_MODULE)
all: format lint default test
$(NAME): $(SRC_FILES)
go build $(GO_FLAGS) -o $(NAME) $(CMD_PKG)
+$(EXT4_NAME): $(SRC_FILES)
+ go build $(GO_FLAGS) -o $(EXT4_NAME) $(EXT4_PKG)
+
$(PAM_MODULE): $(SRC_FILES)
go build -buildmode=c-shared $(GO_FLAGS) -o $(PAM_MODULE) $(PAM_PKG)
rm -f $(PAM_NAME).h
@@ -130,8 +136,8 @@ update:
# Format all the Go and C code
.PHONY: format format-check
format:
- goimports -l -w $(GO_FILES)
- clang-format -i -style=Google $(C_FILES)
+ @goreturns -l -w $(GO_FILES)
+ @clang-format -i -style=Google $(C_FILES)
format-check:
@goimports -d $(GO_FILES) \
@@ -148,18 +154,22 @@ lint:
@megacheck -unused.exported $(GO_PKGS)
###### Installation commands #####
-.PHONY: install_bin install_pam install uninstall
+.PHONY: install_bin install_ext4 install_pam install uninstall
install_bin: $(NAME)
$(INSTALL) -d $(DESTDIR)
$(INSTALL) $(NAME) $(DESTDIR)
+install_ext4: $(EXT4_NAME)
+ $(INSTALL) -d $(DESTDIR)
+ $(INSTALL) $(EXT4_NAME) $(DESTDIR)
+
install_pam: $(PAM_MODULE)
$(INSTALL) -d $(PAM_MODULE_DIR)
$(INSTALL) $(PAM_MODULE) $(PAM_MODULE_DIR)
$(INSTALL) -d $(PAM_CONFIG_DIR)
$(INSTALL) $(PAM_NAME)/config $(PAM_CONFIG_DIR)/$(NAME)
-install: install_bin install_pam
+install: install_bin install_ext4 install_pam
uninstall:
rm -f $(DESTDIR)/$(NAME) $(PAM_MODULE_DIR)/$(PAM_MODULE) $(PAM_CONFIG_DIR)/$(NAME)
diff --git a/ext4/ext4.go b/ext4/ext4.go
index cbe87da..401d208 100644
--- a/ext4/ext4.go
+++ b/ext4/ext4.go
@@ -1,37 +1,90 @@
package main
import (
+ "flag"
"fmt"
"io"
+ "io/ioutil"
"os"
)
-func printUsage(w io.Writer, name string) {
- fmt.Fprintf(w, "Usage: %s [enable|disable] <device> [--force]\n\n", name)
- fmt.Fprintln(w, "Enable or disable encryption on an ext4 filesystem.")
- fmt.Fprintln(w, " <device> - Path to the filesystem device")
- fmt.Fprintln(w, " --force - Automatically proceed with the operation")
+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 printUsageAndExit(err error) {
+ 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)
+ }
+
+ 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)
+ os.Exit(rc)
}
func main() {
- if len(os.Args) != 3 {
- printUsage(os.Stderr, os.Args[0])
- os.Exit(1)
+ set.SetOutput(ioutil.Discard)
+ if err := set.Parse(os.Args[1:]); err != nil {
+ printUsageAndExit(err)
+ }
+ if *helpFlag {
+ printUsageAndExit(nil)
}
- switch os.Args[1] {
+ if *versionFlag {
+ fmt.Println(version)
+ return
+ }
+ if set.NArg() != 2 {
+ printUsageAndExit(fmt.Errorf("expected 2 arguments, got %d", set.NArg()))
+ }
+
+ command, mountpoint := set.Arg(0), set.Arg(1)
+ switch command {
case "enable":
fmt.Println("Enabling encryption!!")
case "disable":
fmt.Println("Disabling encryption!!")
default:
- fmt.Fprintf(os.Stderr, "%s: invalid command %q\n", os.Args[0], os.Args[1])
- printUsage(os.Stderr, os.Args[0])
- os.Exit(1)
+ printUsageAndExit(fmt.Errorf("invalid command %q", command))
}
- if isExt4EncryptionEnabled(os.Args[2]) {
- fmt.Printf("%q has encryption\n", os.Args[2])
+ if isExt4EncryptionEnabled(mountpoint) {
+ fmt.Printf("%q has encryption\n", mountpoint)
} else {
- fmt.Printf("%q doesn't have encryption\n", os.Args[2])
+ fmt.Printf("%q doesn't have encryption\n", mountpoint)
}
}