From aea88d3756803ca2493aeb69954112a7bcd26cfe Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sun, 29 Nov 2015 22:29:17 -0800 Subject: [PATCH] handle stdin pipe properly --- pipe-main.go | 31 ++++++++++++------------------- pkg/client/fs/fs.go | 33 ++++++++++++++++----------------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/pipe-main.go b/pipe-main.go index ee8ac941..04033e9e 100644 --- a/pipe-main.go +++ b/pipe-main.go @@ -43,7 +43,7 @@ var pipeCmd = cli.Command{ mc {{.Name}} - {{.Usage}} USAGE: - mc {{.Name}} [FLAGS] TARGET + mc {{.Name}} [FLAGS] [TARGET] FLAGS: {{range .Flags}}{{.}} @@ -63,16 +63,8 @@ EXAMPLES: `, } -// Validate command line arguments. -func checkPipeSyntax(ctx *cli.Context) { - if !ctx.Args().Present() || len(ctx.Args()) != 1 { - exitCode := 1 - cli.ShowCommandHelpAndExit(ctx, "pipe", exitCode) - } -} - func pipe(targetURL string) *probe.Error { - if len(targetURL) == 0 || targetURL == "" { + if targetURL == "" { // When no target is specified, pipe cat's stdin to stdout. return catOut(os.Stdin).Trace() } @@ -94,16 +86,17 @@ func pipe(targetURL string) *probe.Error { // mainPipe is the main entry point for pipe command. func mainPipe(ctx *cli.Context) { - // Check cmd args. - checkPipeSyntax(ctx) - // Set global flags from context. setGlobalsFromContext(ctx) - // extract URLs. - URLs, err := args2URLs(ctx.Args()) - fatalIf(err.Trace(ctx.Args()...), "Unable to parse arguments.") - - err = pipe(URLs[0]) - fatalIf(err.Trace(URLs...), "Unable to write to one or more targets.") + if len(ctx.Args()) == 0 { + err := pipe("") + fatalIf(err.Trace("stdout"), "Unable to write to one or more targets.") + } else { + // extract URLs. + URLs, err := args2URLs(ctx.Args()) + fatalIf(err.Trace(ctx.Args()...), "Unable to parse arguments.") + err = pipe(URLs[0]) + fatalIf(err.Trace(URLs[0]), "Unable to write to one or more targets.") + } } diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 06f4f030..52abbc00 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -121,9 +121,13 @@ func (f *fsClient) fsStat() (os.FileInfo, *probe.Error) { // Put - create a new file. func (f *fsClient) Put(data io.ReadSeeker, size int64) *probe.Error { + // Extract dir name. objectDir, _ := filepath.Split(f.PathURL.Path) objectPath := f.PathURL.Path + + // Write to a temporary file "object.part.mc" before commiting. objectPartPath := objectPath + partSuffix + if objectDir != "" { // Create any missing top level directories. if e := os.MkdirAll(objectDir, 0700); e != nil { @@ -135,22 +139,9 @@ func (f *fsClient) Put(data io.ReadSeeker, size int64) *probe.Error { return probe.NewError(e) } } - var partFile *os.File - st, e := os.Stat(objectPath + partSuffix) - if e != nil { - if !os.IsNotExist(e) { - return probe.NewError(e) - } - partFile, e = os.OpenFile(objectPartPath, os.O_CREATE|os.O_WRONLY, 0600) - } else { - if st != nil { - // Seek to the location where the previous copy was left off. - if _, e = data.Seek(st.Size(), 0); e != nil { - return probe.NewError(e) - } - } - partFile, e = os.OpenFile(objectPartPath, os.O_APPEND|os.O_WRONLY, 0600) - } + + // If exists, open in append mode. If not create it the part file. + partFile, e := os.OpenFile(objectPartPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if e != nil { if os.IsPermission(e) { return probe.NewError(client.PathInsufficientPermission{ @@ -159,11 +150,19 @@ func (f *fsClient) Put(data io.ReadSeeker, size int64) *probe.Error { } return probe.NewError(e) } - _, e = io.CopyN(partFile, data, size) + + // Write to the part file. + if size < 0 { // Read till EOF. + _, e = io.Copy(partFile, data) + } else { // Read till N bytes. + _, e = io.CopyN(partFile, data, size) + } if e != nil { return probe.NewError(e) } partFile.Close() + + // Safely completed put. Now commit by renaming to actual filename. if e = os.Rename(objectPartPath, objectPath); e != nil { return probe.NewError(e) }