From 46680788f94703ba93bfa4106405955c1143d4b0 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 5 May 2016 01:54:43 -0700 Subject: [PATCH] xl/fs: cleanup '/.minio/tmp' directory on each initialization. (#1490) --- fs-objects-multipart.go | 3 ++- fs-objects.go | 5 +++++ object-common-multipart.go | 29 +++++++++++++++++++---------- xl-objects.go | 5 +++++ 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/fs-objects-multipart.go b/fs-objects-multipart.go index 06d5d11ca..2c714b8fd 100644 --- a/fs-objects-multipart.go +++ b/fs-objects-multipart.go @@ -100,7 +100,8 @@ func (fs fsObjects) CompleteMultipartUpload(bucket string, object string, upload } // Cleanup all the parts. - if err = cleanupUploadedParts(fs.storage, mpartMetaPrefix, bucket, object, uploadID); err != nil { + recursive := false + if err = cleanupUploadedParts(fs.storage, mpartMetaPrefix, bucket, object, uploadID, recursive); err != nil { return "", err } diff --git a/fs-objects.go b/fs-objects.go index 5a7d99b87..f10641e96 100644 --- a/fs-objects.go +++ b/fs-objects.go @@ -46,6 +46,11 @@ func newFSObjects(exportPath string) (ObjectLayer, error) { return nil, err } } + + // Cleanup all temp entries upon start. + cleanupAllTmpEntries(storage) + + // Return successfully initialized object layer. return fsObjects{storage}, nil } diff --git a/object-common-multipart.go b/object-common-multipart.go index 1879da910..00912b3f8 100644 --- a/object-common-multipart.go +++ b/object-common-multipart.go @@ -25,6 +25,7 @@ import ( "path" "strconv" "strings" + "sync" "github.com/Sirupsen/logrus" "github.com/skyrings/skyring-common/tools/uuid" @@ -163,30 +164,37 @@ func putObjectPartCommon(storage StorageAPI, bucket string, object string, uploa // Cleanup all temp entries inside tmpMetaPrefix directory, upon server initialization. func cleanupAllTmpEntries(storage StorageAPI) error { - return cleanupUploadedParts(storage, tmpMetaPrefix, "", "", "") + recursive := true // Recursively delete all files inside 'tmp' directory. + return cleanupUploadedParts(storage, tmpMetaPrefix, "", "", "", recursive) } // Wrapper to which removes all the uploaded parts after a successful // complete multipart upload. -func cleanupUploadedParts(storage StorageAPI, prefix, bucket, object, uploadID string) error { +func cleanupUploadedParts(storage StorageAPI, prefix, bucket, object, uploadID string, recursive bool) error { markerPath := "" + var wg = &sync.WaitGroup{} for { uploadIDPath := path.Join(prefix, bucket, object, uploadID) - fileInfos, eof, err := storage.ListFiles(minioMetaBucket, uploadIDPath, markerPath, false, 1000) + fileInfos, eof, err := storage.ListFiles(minioMetaBucket, uploadIDPath, markerPath, recursive, 1000) if err != nil { - if err == errFileNotFound { - return InvalidUploadID{UploadID: uploadID} - } - return toObjectErr(err) + return toObjectErr(err, bucket, object) } + // Loop through all files and delete each in go-routine, while + // adding each operation to a wait group. for _, fileInfo := range fileInfos { - storage.DeleteFile(minioMetaBucket, fileInfo.Name) - markerPath = fileInfo.Name + wg.Add(1) + go func(fi FileInfo) { + defer wg.Done() + storage.DeleteFile(minioMetaBucket, fi.Name) + }(fileInfo) } if eof { break } + markerPath = fileInfos[len(fileInfos)-1].Name } + // Wait for all the routines. + wg.Wait() return nil } @@ -205,7 +213,8 @@ func abortMultipartUploadCommon(storage StorageAPI, bucket, object, uploadID str } else if !status { return InvalidUploadID{UploadID: uploadID} } - return cleanupUploadedParts(storage, mpartMetaPrefix, bucket, object, uploadID) + recursive := false // Cleanup all the top level files and folders matching uploadID. + return cleanupUploadedParts(storage, mpartMetaPrefix, bucket, object, uploadID, recursive) } // listLeafEntries - lists all entries if a given prefixPath is a leaf diff --git a/xl-objects.go b/xl-objects.go index c2c2f1b8a..e6f147a31 100644 --- a/xl-objects.go +++ b/xl-objects.go @@ -42,6 +42,11 @@ func newXLObjects(exportPaths ...string) (ObjectLayer, error) { if err != nil { return nil, err } + + // Cleanup all temporary entries. + cleanupAllTmpEntries(storage) + + // Return successfully initialized object layer. return xlObjects{storage}, nil }