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