2021-02-28 00:25:55 +00:00
|
|
|
/*
|
2021-04-22 20:40:43 +00:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
2023-04-28 15:01:14 +00:00
|
|
|
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
2021-02-28 00:25:55 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-28 00:25:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/LexicalPath.h>
|
2021-05-14 15:27:38 +00:00
|
|
|
#include <AK/Random.h>
|
2021-02-28 00:25:55 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-14 18:00:22 +00:00
|
|
|
#include <LibCore/System.h>
|
2023-04-28 15:01:14 +00:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-01-14 18:00:22 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-04-11 03:53:37 +00:00
|
|
|
#include <fcntl.h>
|
2021-02-28 00:25:55 +00:00
|
|
|
#include <sys/stat.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2021-02-28 00:25:55 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString generate_random_filename(DeprecatedString const& pattern)
|
2021-02-28 00:25:55 +00:00
|
|
|
{
|
2022-01-14 18:00:22 +00:00
|
|
|
StringBuilder new_filename { pattern.length() };
|
2021-02-28 00:25:55 +00:00
|
|
|
|
2021-05-17 22:52:33 +00:00
|
|
|
constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
2022-01-14 18:00:22 +00:00
|
|
|
for (size_t i = 0; i < pattern.length(); i++) {
|
|
|
|
if (pattern[i] == 'X')
|
|
|
|
new_filename.append(random_characters[(get_random<u32>() % (sizeof(random_characters) - 1))]);
|
|
|
|
else
|
|
|
|
new_filename.append(pattern[i]);
|
2021-02-28 00:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
return new_filename.to_deprecated_string();
|
2021-02-28 00:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 11:30:58 +00:00
|
|
|
static ErrorOr<Optional<DeprecatedString>> make_temp(DeprecatedString const& pattern, bool directory, bool dry_run)
|
2021-02-28 00:25:55 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
2022-01-14 18:00:22 +00:00
|
|
|
auto path = generate_random_filename(pattern);
|
2021-02-28 00:25:55 +00:00
|
|
|
if (dry_run) {
|
2022-01-14 18:00:22 +00:00
|
|
|
auto stat_or_error = Core::System::lstat(path.view());
|
|
|
|
if (stat_or_error.is_error() && stat_or_error.error().code() == ENOENT)
|
2021-02-28 00:25:55 +00:00
|
|
|
return path;
|
|
|
|
} else if (directory) {
|
2022-01-14 18:00:22 +00:00
|
|
|
TRY(Core::System::mkdir(path.view(), 0700));
|
|
|
|
return path;
|
2021-02-28 00:25:55 +00:00
|
|
|
} else {
|
2022-01-14 18:00:22 +00:00
|
|
|
auto fd_or_error = Core::System::open(path.view(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
|
|
|
if (!fd_or_error.is_error()) {
|
|
|
|
TRY(Core::System::close(fd_or_error.value()));
|
2021-02-28 00:25:55 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-10 11:30:58 +00:00
|
|
|
return OptionalNone {};
|
2021-02-28 00:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 18:00:22 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-02-28 00:25:55 +00:00
|
|
|
{
|
2022-01-14 18:00:22 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
2021-02-28 00:25:55 +00:00
|
|
|
|
2022-01-14 18:00:22 +00:00
|
|
|
StringView file_template;
|
2021-02-28 00:25:55 +00:00
|
|
|
bool create_directory = false;
|
|
|
|
bool dry_run = false;
|
|
|
|
bool quiet = false;
|
2022-01-14 18:00:22 +00:00
|
|
|
StringView target_directory;
|
2021-02-28 00:25:55 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Create a temporary file or directory, safely, and print its name.");
|
|
|
|
args_parser.add_positional_argument(file_template, "The template must contain at least 3 consecutive 'X's", "template", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.add_option(create_directory, "Create a temporary directory instead of a file", "directory", 'd');
|
|
|
|
args_parser.add_option(dry_run, "Do not create anything, just print a unique name", "dry-run", 'u');
|
|
|
|
args_parser.add_option(quiet, "Do not print diagnostics about file/directory creation failure", "quiet", 'q');
|
|
|
|
args_parser.add_option(target_directory, "Create TEMPLATE relative to DIR", "tmpdir", 'p', "DIR");
|
2022-01-14 18:00:22 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-02-28 00:25:55 +00:00
|
|
|
|
2023-04-28 15:01:14 +00:00
|
|
|
Optional<String> final_file_template;
|
|
|
|
Optional<String> final_target_directory;
|
|
|
|
|
2022-01-14 18:00:22 +00:00
|
|
|
if (target_directory.is_empty()) {
|
2023-04-28 15:01:14 +00:00
|
|
|
if (!file_template.is_empty()) {
|
|
|
|
auto resolved_path = LexicalPath(TRY(FileSystem::absolute_path(file_template)).to_deprecated_string());
|
|
|
|
final_target_directory = TRY(String::from_utf8(resolved_path.dirname()));
|
|
|
|
final_file_template = TRY(String::from_utf8(resolved_path.basename()));
|
2021-02-28 00:25:55 +00:00
|
|
|
} else {
|
2023-08-08 02:26:17 +00:00
|
|
|
final_target_directory = "/tmp"_string;
|
2023-04-28 15:01:14 +00:00
|
|
|
auto const* env_directory = getenv("TMPDIR");
|
|
|
|
if (env_directory != nullptr && *env_directory != 0)
|
|
|
|
final_target_directory = TRY(String::from_utf8({ env_directory, strlen(env_directory) }));
|
2021-02-28 00:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 15:01:14 +00:00
|
|
|
if (!final_file_template.has_value()) {
|
2023-08-07 09:12:38 +00:00
|
|
|
final_file_template = "tmp.XXXXXXXXXX"_string;
|
2021-02-28 00:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 15:01:14 +00:00
|
|
|
if (!final_file_template->find_byte_offset("XXX"sv).has_value()) {
|
2021-02-28 00:25:55 +00:00
|
|
|
if (!quiet)
|
2023-04-28 15:01:14 +00:00
|
|
|
warnln("Too few X's in template {}", final_file_template);
|
2021-02-28 00:25:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-04-28 15:01:14 +00:00
|
|
|
auto target_path = LexicalPath::join(final_target_directory->to_deprecated_string(), final_file_template->to_deprecated_string()).string();
|
2021-02-28 00:25:55 +00:00
|
|
|
|
2022-01-14 18:00:22 +00:00
|
|
|
auto final_path = TRY(make_temp(target_path, create_directory, dry_run));
|
2023-10-10 11:30:58 +00:00
|
|
|
if (!final_path.has_value()) {
|
2021-02-28 00:25:55 +00:00
|
|
|
if (!quiet) {
|
|
|
|
if (create_directory)
|
2021-06-29 11:11:03 +00:00
|
|
|
warnln("Failed to create directory via template {}", target_path.characters());
|
2021-02-28 00:25:55 +00:00
|
|
|
else
|
2021-06-29 11:11:03 +00:00
|
|
|
warnln("Failed to create file via template {}", target_path.characters());
|
2021-02-28 00:25:55 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-10 11:30:58 +00:00
|
|
|
outln("{}", *final_path);
|
2022-01-14 18:00:22 +00:00
|
|
|
|
2021-02-28 00:25:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|