2020-01-18 03:38:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2023-12-16 09:19:34 -05:00
|
|
|
#include <AK/ByteString.h>
|
2020-05-26 07:52:44 -04:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-02-21 06:56:05 -05:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-23 12:09:30 -05:00
|
|
|
#include <LibCore/System.h>
|
2023-03-21 11:35:30 -04:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-01-23 12:09:30 -05:00
|
|
|
#include <LibMain/Main.h>
|
2019-06-07 05:49:31 -04:00
|
|
|
#include <stdio.h>
|
2021-11-06 21:15:10 -04:00
|
|
|
#include <string.h>
|
2019-06-07 05:49:31 -04:00
|
|
|
#include <sys/stat.h>
|
2021-03-12 11:29:37 -05:00
|
|
|
#include <unistd.h>
|
2019-04-07 17:35:26 -04:00
|
|
|
|
2022-01-23 12:09:30 -05:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-04-07 17:35:26 -04:00
|
|
|
{
|
2022-01-23 12:09:30 -05:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath fattr"));
|
2020-01-13 06:30:12 -05:00
|
|
|
|
2020-08-11 07:43:25 -04:00
|
|
|
bool force = false;
|
2022-10-20 00:33:56 -04:00
|
|
|
bool no_clobber = false;
|
2020-11-16 20:39:18 -05:00
|
|
|
bool verbose = false;
|
2020-08-11 07:43:25 -04:00
|
|
|
|
2023-12-16 09:19:34 -05:00
|
|
|
Vector<ByteString> paths;
|
2019-04-07 17:35:26 -04:00
|
|
|
|
2020-02-21 06:56:05 -05:00
|
|
|
Core::ArgsParser args_parser;
|
2020-08-11 07:43:25 -04:00
|
|
|
args_parser.add_option(force, "Force", "force", 'f');
|
2022-10-20 00:33:56 -04:00
|
|
|
args_parser.add_option(no_clobber, "Do not overwrite existing files", "no-clobber", 'n');
|
2020-11-16 20:39:18 -05:00
|
|
|
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
|
2020-11-28 09:42:09 -05:00
|
|
|
args_parser.add_positional_argument(paths, "Paths to files being moved followed by target location", "paths");
|
2022-01-23 12:09:30 -05:00
|
|
|
args_parser.parse(arguments);
|
2019-04-07 17:35:26 -04:00
|
|
|
|
2020-11-28 09:42:09 -05:00
|
|
|
if (paths.size() < 2) {
|
2023-02-21 06:44:41 -05:00
|
|
|
args_parser.print_usage(stderr, arguments.strings[0]);
|
2020-11-28 09:42:09 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:33:56 -04:00
|
|
|
if (force && no_clobber) {
|
|
|
|
warnln("-f (--force) overrides -n (--no-clobber)");
|
|
|
|
no_clobber = false;
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:42:09 -05:00
|
|
|
auto original_new_path = paths.take_last();
|
|
|
|
|
2019-04-07 17:35:26 -04:00
|
|
|
struct stat st;
|
2020-02-21 06:56:05 -05:00
|
|
|
|
2022-04-02 11:48:05 -04:00
|
|
|
int rc = lstat(original_new_path.characters(), &st);
|
2020-02-21 06:56:05 -05:00
|
|
|
if (rc != 0 && errno != ENOENT) {
|
|
|
|
perror("lstat");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:42:09 -05:00
|
|
|
if (paths.size() > 1 && !S_ISDIR(st.st_mode)) {
|
|
|
|
warnln("Target is not a directory: {}", original_new_path);
|
2019-04-07 17:35:26 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2020-11-16 20:39:18 -05:00
|
|
|
|
2021-02-18 09:34:43 -05:00
|
|
|
auto my_umask = umask(0);
|
|
|
|
umask(my_umask);
|
|
|
|
|
2020-11-28 09:42:09 -05:00
|
|
|
for (auto& old_path : paths) {
|
2023-12-16 09:19:34 -05:00
|
|
|
ByteString combined_new_path;
|
2022-04-02 11:48:05 -04:00
|
|
|
auto new_path = original_new_path;
|
2021-05-06 13:28:18 -04:00
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2021-06-29 10:46:16 -04:00
|
|
|
auto old_basename = LexicalPath::basename(old_path);
|
2023-12-16 09:19:34 -05:00
|
|
|
combined_new_path = ByteString::formatted("{}/{}", original_new_path, old_basename);
|
2020-11-28 09:42:09 -05:00
|
|
|
new_path = combined_new_path.characters();
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:35:30 -04:00
|
|
|
if (no_clobber && FileSystem::exists(new_path))
|
2022-10-20 00:33:56 -04:00
|
|
|
continue;
|
|
|
|
|
2022-04-02 11:48:05 -04:00
|
|
|
rc = rename(old_path.characters(), new_path.characters());
|
2020-11-28 09:42:09 -05:00
|
|
|
if (rc < 0) {
|
2021-02-18 09:34:43 -05:00
|
|
|
if (errno == EXDEV) {
|
2024-12-09 16:38:17 -05:00
|
|
|
if (auto result = FileSystem::copy_file_or_directory(
|
|
|
|
new_path, old_path,
|
|
|
|
FileSystem::RecursionMode::Allowed,
|
|
|
|
FileSystem::LinkMode::Disallowed,
|
|
|
|
FileSystem::AddDuplicateFileMarker::No);
|
|
|
|
result.is_error()) {
|
2023-05-13 07:33:09 -04:00
|
|
|
warnln("mv: could not move '{}': {}", old_path, result.error());
|
2021-02-18 09:34:43 -05:00
|
|
|
return 1;
|
2021-02-20 19:54:37 -05:00
|
|
|
}
|
2024-12-09 16:38:17 -05:00
|
|
|
|
|
|
|
if (auto result = FileSystem::remove(old_path.view(), FileSystem::RecursionMode::Allowed); result.is_error())
|
|
|
|
warnln("mv: could not remove '{}': {}", old_path, result.error());
|
2021-05-06 13:28:18 -04:00
|
|
|
} else {
|
|
|
|
warnln("mv: cannot move '{}' : {}", old_path, strerror(errno));
|
2021-02-18 09:34:43 -05:00
|
|
|
}
|
2020-11-28 09:42:09 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:34:43 -05:00
|
|
|
if (verbose && rc == 0)
|
2021-05-31 10:43:25 -04:00
|
|
|
outln("renamed '{}' -> '{}'", old_path, new_path);
|
2020-11-28 09:42:09 -05:00
|
|
|
}
|
2020-11-16 20:39:18 -05:00
|
|
|
|
2019-04-07 17:35:26 -04:00
|
|
|
return 0;
|
|
|
|
}
|