Utilities: Fix mv command requesting incorrect path

When moving multiple files by using *, e.g.,: mv * /new_path/
If there was an error while trying to move a file to the new path
the next file in the file list to be moved would have its path
incorrectly set.

- Fixed mv loop to always append the correct path to the destination
path.
- Added proper error message when mv fails.
This commit is contained in:
r-paiva 2021-05-06 18:28:18 +01:00 committed by Andreas Kling
parent 2fe6242f15
commit 90b6f999e5
Notes: sideshowbarker 2024-07-18 18:31:01 +09:00

View File

@ -58,7 +58,7 @@ int main(int argc, char** argv)
for (auto& old_path : paths) {
String combined_new_path;
const char* new_path = original_new_path;
if (rc == 0 && S_ISDIR(st.st_mode)) {
if (S_ISDIR(st.st_mode)) {
auto old_basename = LexicalPath(old_path).basename();
combined_new_path = String::formatted("{}/{}", original_new_path, old_basename);
new_path = combined_new_path.characters();
@ -67,7 +67,12 @@ int main(int argc, char** argv)
rc = rename(old_path, new_path);
if (rc < 0) {
if (errno == EXDEV) {
auto result = Core::File::copy_file_or_directory(new_path, old_path, Core::File::RecursionMode::Allowed, Core::File::LinkMode::Disallowed, Core::File::AddDuplicateFileMarker::No);
auto result = Core::File::copy_file_or_directory(
new_path, old_path,
Core::File::RecursionMode::Allowed,
Core::File::LinkMode::Disallowed,
Core::File::AddDuplicateFileMarker::No);
if (result.is_error()) {
warnln("mv: could not move '{}': {}", old_path, result.error().error_code);
return 1;
@ -75,6 +80,8 @@ int main(int argc, char** argv)
rc = unlink(old_path);
if (rc < 0)
fprintf(stderr, "mv: unlink '%s': %s\n", old_path, strerror(errno));
} else {
warnln("mv: cannot move '{}' : {}", old_path, strerror(errno));
}
}