Skip to content

Commit 9bd5f38

Browse files
committed
Delete completed upload instead of move if the file already exists in shardedfilestore
1 parent 98b9056 commit 9bd5f38

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

shardedfilestore/shardedfilestore.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func RemoveWithDirs(path string, basePath string) (err error) {
200200
return err
201201
}
202202

203-
empty, err := isDirEmpty(parent);
203+
empty, err := isDirEmpty(parent)
204204
if empty {
205205
err = os.Remove(parent)
206206
}
@@ -431,14 +431,28 @@ func (store *ShardedFileStore) FinishUpload(id string) error {
431431
newPath := store.completeBinPath(hash)
432432
os.MkdirAll(filepath.Dir(newPath), defaultDirectoryPerm)
433433
oldPath := store.incompleteBinPath(id)
434-
err = os.Rename(oldPath, newPath)
435-
if err != nil {
436-
store.log.Error().
437-
Err(err).
438-
Str("oldPath", oldPath).
439-
Str("newPath", newPath).
440-
Msg("Failed to rename")
434+
435+
if _, err := os.Stat(newPath); err != nil {
436+
// file needs moving to the sharded filestore
437+
err = os.Rename(oldPath, newPath)
438+
if err != nil {
439+
store.log.Error().
440+
Err(err).
441+
Str("oldPath", oldPath).
442+
Str("newPath", newPath).
443+
Msg("Failed to rename")
444+
}
445+
} else {
446+
// file already exists just remove the tempoary upload
447+
err = os.Remove(oldPath)
448+
if err != nil {
449+
store.log.Error().
450+
Err(err).
451+
Str("oldPath", oldPath).
452+
Msg("Failed to remove")
453+
}
441454
}
455+
442456
return err
443457
}
444458

@@ -465,8 +479,8 @@ func isDirEmpty(path string) (bool, error) {
465479
defer f.Close()
466480

467481
_, err = f.Readdirnames(1)
468-
if err == io.EOF {
469-
return true, nil
470-
}
471-
return false, err
482+
if err == io.EOF {
483+
return true, nil
484+
}
485+
return false, err
472486
}

0 commit comments

Comments
 (0)