handle stdin pipe properly
This commit is contained in:
+12
-19
@@ -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.")
|
||||
}
|
||||
}
|
||||
|
||||
+16
-17
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user