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