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
|
|
|
*/
|
|
|
|
|
2020-05-26 11:52:44 +00:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-01-13 11:30:12 +00:00
|
|
|
#include <AK/String.h>
|
2020-02-21 11:56:05 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-02-21 00:54:37 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-01-23 17:09:30 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#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
|
|
|
// NOTE: The "force" option is a dummy for now, it's just here to silence scripts that use "mv -f"
|
|
|
|
// In the future, it might be used to cancel out an "-i" interactive option.
|
|
|
|
bool force = false;
|
2020-11-17 01:39:18 +00:00
|
|
|
bool verbose = false;
|
2020-08-11 11:43:25 +00:00
|
|
|
|
2022-04-02 15:48:05 +00:00
|
|
|
Vector<String> 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');
|
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) {
|
2022-01-23 17:09:30 +00:00
|
|
|
args_parser.print_usage(stderr, arguments.argv[0]);
|
2020-11-28 14:42:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
String 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);
|
2021-01-16 11:00:33 +00:00
|
|
|
combined_new_path = String::formatted("{}/{}", original_new_path, old_basename);
|
2020-11-28 14:42:09 +00:00
|
|
|
new_path = combined_new_path.characters();
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-05-06 17:28:18 +00:00
|
|
|
auto result = Core::File::copy_file_or_directory(
|
|
|
|
new_path, old_path,
|
|
|
|
Core::File::RecursionMode::Allowed,
|
|
|
|
Core::File::LinkMode::Disallowed,
|
|
|
|
Core::File::AddDuplicateFileMarker::No);
|
|
|
|
|
2021-02-21 00:54:37 +00:00
|
|
|
if (result.is_error()) {
|
2021-11-07 00:42:54 +00:00
|
|
|
warnln("mv: could not move '{}': {}", old_path, static_cast<Error const&>(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;
|
|
|
|
}
|