From 16645511195ad5ba27361eb906e948ebf868b6ef Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 30 Nov 2015 10:57:35 -0800 Subject: [PATCH] cp: Working resumable cp fixes #1505 --- pkg/client/errors.go | 7 +++++++ pkg/client/fs/fs.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pkg/client/errors.go b/pkg/client/errors.go index 442141b4..9151dea2 100644 --- a/pkg/client/errors.go +++ b/pkg/client/errors.go @@ -106,6 +106,13 @@ func (e PathNotFound) Error() string { return "Requested file ‘" + e.Path + "’ not found" } +// PathIsDir (EISDIR) - file is a folder. +type PathIsDir GenericFileError + +func (e PathIsDir) Error() string { + return "Requested file ‘" + e.Path + "’ is folder." +} + // PathInsufficientPermission (EPERM) - permission denied. type PathInsufficientPermission GenericFileError diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 52abbc00..6201612d 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -125,6 +125,24 @@ func (f *fsClient) Put(data io.ReadSeeker, size int64) *probe.Error { objectDir, _ := filepath.Split(f.PathURL.Path) objectPath := f.PathURL.Path + // Verify if destination already exists. + st, e := os.Stat(objectPath) + if e == nil { + // If the destination exists and is a directory. + if st.IsDir() { + return probe.NewError(client.PathIsDir{ + Path: objectPath, + }) + } + } + + // Proceed if file does not exist. return for all other errors. + if e != nil { + if !os.IsNotExist(e) { + return probe.NewError(e) + } + } + // Write to a temporary file "object.part.mc" before commiting. objectPartPath := objectPath + partSuffix @@ -151,6 +169,15 @@ func (f *fsClient) Put(data io.ReadSeeker, size int64) *probe.Error { return probe.NewError(e) } + // Get stat to get the current size. + partSt, e := partFile.Stat() + if e != nil { + return probe.NewError(e) + } + + // Seek to current position for incoming reader. + data.Seek(partSt.Size(), 0) + // Write to the part file. if size < 0 { // Read till EOF. _, e = io.Copy(partFile, data)