mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:27:45 +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:
parent
588ba72fe7
commit
4bfe060336
1 changed files with 29 additions and 20 deletions
|
@ -321,27 +321,36 @@ 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;
|
||||||
item_done += item.size;
|
while (true) {
|
||||||
executed_work_bytes += item.size;
|
if (rename(item.source.characters(), destination.characters()) == 0) {
|
||||||
print_progress();
|
item_done += item.size;
|
||||||
continue;
|
executed_work_bytes += item.size;
|
||||||
}
|
print_progress();
|
||||||
|
break;
|
||||||
auto original_errno = errno;
|
}
|
||||||
if (original_errno != EXDEV) {
|
|
||||||
report_warning(String::formatted("Failed to move {}: {}", item.source, strerror(original_errno)));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// EXDEV means we have to copy the file data and then remove the original
|
|
||||||
if (!copy_file(item.source, item.destination))
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (unlink(item.source.characters()) < 0) {
|
|
||||||
auto original_errno = errno;
|
auto original_errno = errno;
|
||||||
report_error(String::formatted("unlink: {}", strerror(original_errno)));
|
|
||||||
return 1;
|
if (original_errno == EEXIST) {
|
||||||
|
destination = deduplicate_destination_file_name(destination);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (original_errno != EXDEV) {
|
||||||
|
report_warning(String::formatted("Failed to move {}: {}", item.source, strerror(original_errno)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXDEV means we have to copy the file data and then remove the original
|
||||||
|
if (!copy_file(item.source, item.destination))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (unlink(item.source.characters()) < 0) {
|
||||||
|
auto original_errno = errno;
|
||||||
|
report_error(String::formatted("unlink: {}", strerror(original_errno)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue