find: Fix --exec parsing (#3907)

Find would replace placeholders before splitting input, meaning files with spaces would be executed as separate args.

Split before replacing args, and use shell-style parsing.

Technically, this requires quotes in input to be escaped, so to specify `--exec='echo {""}'` you must escape them like this: `--exec='echo {\"\"}'`. Quoted inputs aren't documented, but it is a change.

Errors would not be displayed, like `./mc find play/test123/ --exec='unknown {}'` would just exit. Instead it now prints `exec: "unknown": executable file not found in $PATH`.

Fixes #3906
This commit is contained in:
Klaus Post
2022-01-03 23:56:55 -08:00
committed by GitHub
parent 37e0f8ba27
commit eef80d47cd
3 changed files with 23 additions and 7 deletions
+20 -7
View File
@@ -30,6 +30,7 @@ import (
"time"
"github.com/dustin/go-humanize"
"github.com/google/shlex"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
@@ -114,16 +115,28 @@ func getExitStatus(err error) int {
// execFind executes the input command line, additionally formats input
// for the command line in accordance with subsititution arguments.
func execFind(command string) {
commandArgs := strings.Split(command, " ")
cmd := exec.Command(commandArgs[0], commandArgs[1:]...)
func execFind(ctx context.Context, args string, fileContent contentMessage) {
split, err := shlex.Split(args)
if err != nil {
console.Println(console.Colorize("FindExecErr", "Unable to parse --exec: "+err.Error()))
os.Exit(getExitStatus(err))
}
if len(split) == 0 {
return
}
for i, arg := range split {
split[i] = stringsReplace(ctx, arg, fileContent)
}
cmd := exec.Command(split[0], split[1:]...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
console.Print(console.Colorize("FindExecErr", stderr.String()))
if stderr.Len() > 0 {
console.Println(console.Colorize("FindExecErr", strings.TrimSpace(stderr.String())))
}
console.Println(console.Colorize("FindExecErr", err.Error()))
// Return exit status of the command run
os.Exit(getExitStatus(err))
}
@@ -230,7 +243,7 @@ func find(ctxCtx context.Context, ctx *findContext, fileContent contentMessage)
// proceed to either exec, format the output string.
if ctx.execCmd != "" {
execFind(stringsReplace(ctxCtx, ctx.execCmd, fileContent))
execFind(ctxCtx, ctx.execCmd, fileContent)
return
}
if ctx.printFmt != "" {
@@ -290,7 +303,7 @@ func doFind(ctxCtx context.Context, ctx *findContext) error {
// proceed to either exec, format the output string.
if ctx.execCmd != "" {
execFind(stringsReplace(ctxCtx, ctx.execCmd, fileContent))
execFind(ctxCtx, ctx.execCmd, fileContent)
continue
}
if ctx.printFmt != "" {
+1
View File
@@ -8,6 +8,7 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.13.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.3.0
github.com/inconshreveable/mousetrap v1.0.0
github.com/json-iterator/go v1.1.12
+2
View File
@@ -175,6 +175,8 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=