1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

FileOperation: Deduplicate file names on move

For file copying, when there is a file with the same name in the
destination directory, the file will be automatically renamed to
"file-2.txt", for example. This change expands that special-case
handling to file moving.
This commit is contained in:
Tetsui Ohkubo 2021-09-15 00:03:24 +09:00 committed by Andreas Kling
parent 588ba72fe7
commit 4bfe060336

View file

@ -321,14 +321,21 @@ int execute_work_items(Vector<WorkItem> const& items)
} }
case WorkItem::Type::MoveFile: { case WorkItem::Type::MoveFile: {
if (rename(item.source.characters(), item.destination.characters()) == 0) { String destination = item.destination;
while (true) {
if (rename(item.source.characters(), destination.characters()) == 0) {
item_done += item.size; item_done += item.size;
executed_work_bytes += item.size; executed_work_bytes += item.size;
print_progress(); print_progress();
break;
}
auto original_errno = errno;
if (original_errno == EEXIST) {
destination = deduplicate_destination_file_name(destination);
continue; continue;
} }
auto original_errno = errno;
if (original_errno != EXDEV) { if (original_errno != EXDEV) {
report_warning(String::formatted("Failed to move {}: {}", item.source, strerror(original_errno))); report_warning(String::formatted("Failed to move {}: {}", item.source, strerror(original_errno)));
return 1; return 1;
@ -343,6 +350,8 @@ int execute_work_items(Vector<WorkItem> const& items)
report_error(String::formatted("unlink: {}", strerror(original_errno))); report_error(String::formatted("unlink: {}", strerror(original_errno)));
return 1; return 1;
} }
break;
}
break; break;
} }