summaryrefslogtreecommitdiff
path: root/Userland/Utilities/mv.cpp
diff options
context:
space:
mode:
authorr-paiva <rui.paiva.10@hotmail.com>2021-05-06 18:28:18 +0100
committerAndreas Kling <kling@serenityos.org>2021-05-08 15:22:47 +0200
commit90b6f999e599548b83f0e310146c6121a7496c30 (patch)
tree8e9a9afefe51d06332553927f5d71811c0ec2439 /Userland/Utilities/mv.cpp
parent2fe6242f155c26155cba6f7e23ac17c5f26cd014 (diff)
downloadserenity-90b6f999e599548b83f0e310146c6121a7496c30.zip
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.
Diffstat (limited to 'Userland/Utilities/mv.cpp')
-rw-r--r--Userland/Utilities/mv.cpp11
1 files changed, 9 insertions, 2 deletions
diff --git a/Userland/Utilities/mv.cpp b/Userland/Utilities/mv.cpp
index 457663f2ba..ba9ce07ea9 100644
--- a/Userland/Utilities/mv.cpp
+++ b/Userland/Utilities/mv.cpp
@@ -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));
}
}