diff options
author | Jakob Unterwurzacher | 2020-05-10 00:04:14 +0200 |
---|---|---|
committer | Jakob Unterwurzacher | 2020-05-10 00:04:14 +0200 |
commit | a9895b34872d2299cfe424444fab15516e7f96c9 (patch) | |
tree | 0d5a1c28e0f4cbac700b28c5298cdbbf8dd77b83 /gocryptfs-xray/paths_ctlsock.go | |
parent | f2e8b776f8d2f4cff928e127798fc8c98ca2fbc5 (diff) |
gocryptfs-xray: add -0 flag, add tests
The -0 flags works like xargs -0.
Diffstat (limited to 'gocryptfs-xray/paths_ctlsock.go')
-rw-r--r-- | gocryptfs-xray/paths_ctlsock.go | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/gocryptfs-xray/paths_ctlsock.go b/gocryptfs-xray/paths_ctlsock.go index 3c69ec3..c489f0e 100644 --- a/gocryptfs-xray/paths_ctlsock.go +++ b/gocryptfs-xray/paths_ctlsock.go @@ -8,36 +8,55 @@ import ( "github.com/rfjakob/gocryptfs/ctlsock" ) -func decryptPaths(socketPath string) { +func decryptPaths(socketPath string, sep0 bool) { var req ctlsock.RequestStruct - transformPaths(socketPath, &req, &req.DecryptPath) + transformPaths(socketPath, &req, &req.DecryptPath, sep0) } -func encryptPaths(socketPath string) { +func encryptPaths(socketPath string, sep0 bool) { var req ctlsock.RequestStruct - transformPaths(socketPath, &req, &req.EncryptPath) + transformPaths(socketPath, &req, &req.EncryptPath, sep0) } -func transformPaths(socketPath string, req *ctlsock.RequestStruct, in *string) { +func transformPaths(socketPath string, req *ctlsock.RequestStruct, in *string, sep0 bool) { + errorCount := 0 c, err := ctlsock.New(socketPath) if err != nil { fmt.Printf("fatal: %v\n", err) os.Exit(1) } - line := 0 - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - line++ - *in = scanner.Text() + line := 1 + var separator byte = '\n' + if sep0 { + separator = '\000' + } + r := bufio.NewReader(os.Stdin) + for eof := false; eof == false; line++ { + val, err := r.ReadBytes(separator) + if len(val) == 0 { + break + } + if err != nil { + // break the loop after we have processed the last val + eof = true + } else { + // drop trailing separator + val = val[:len(val)-1] + } + *in = string(val) resp, err := c.Query(req) if err != nil { fmt.Fprintf(os.Stderr, "error at input line %d %q: %v\n", line, *in, err) + errorCount++ continue } if resp.WarnText != "" { fmt.Fprintf(os.Stderr, "warning at input line %d %q: %v\n", line, *in, resp.WarnText) } - fmt.Println(resp.Result) + fmt.Printf("%s%c", resp.Result, separator) + } + if errorCount == 0 { + os.Exit(0) } - os.Exit(0) + os.Exit(1) } |