1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:34:59 +00:00

Utilities: Make mktemp follow the spec directory path resolution

Previously an input like this:

mktemp -d /tmp/dirtest_-XXXXX

Would result in an error when it should have created a temp directory
This commit is contained in:
Cameron Youell 2023-04-29 01:01:14 +10:00 committed by Andrew Kaster
parent 9630fe3ebc
commit d9ff37e1b1
2 changed files with 19 additions and 13 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,13 +9,12 @@
#include <AK/Random.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
#include <LibMain/Main.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
constexpr StringView default_template = "tmp.XXXXXXXXXX"sv;
static DeprecatedString generate_random_filename(DeprecatedString const& pattern)
{
StringBuilder new_filename { pattern.length() };
@ -71,28 +71,33 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(target_directory, "Create TEMPLATE relative to DIR", "tmpdir", 'p', "DIR");
args_parser.parse(arguments);
Optional<String> final_file_template;
Optional<String> final_target_directory;
if (target_directory.is_empty()) {
if (!file_template.is_empty()) { // If a custom template is specified we assume the target directory is the current directory
// FIXME: Get rid of this minor memory leak.
auto const* cwd_ptr = getcwd(nullptr, 0);
target_directory = StringView { cwd_ptr, strlen(cwd_ptr) };
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()));
} else {
char const* env_directory = getenv("TMPDIR");
target_directory = env_directory && *env_directory ? StringView { env_directory, strlen(env_directory) } : "/tmp"sv;
final_target_directory = "/tmp"_short_string;
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) }));
}
}
if (file_template.is_empty()) {
file_template = default_template;
if (!final_file_template.has_value()) {
final_file_template = TRY("tmp.XXXXXXXXXX"_string);
}
if (!file_template.find("XXX"sv).has_value()) {
if (!final_file_template->find_byte_offset("XXX"sv).has_value()) {
if (!quiet)
warnln("Too few X's in template {}", file_template);
warnln("Too few X's in template {}", final_file_template);
return 1;
}
auto target_path = LexicalPath::join(target_directory, file_template).string();
auto target_path = LexicalPath::join(final_target_directory->to_deprecated_string(), final_file_template->to_deprecated_string()).string();
auto final_path = TRY(make_temp(target_path, create_directory, dry_run));
if (final_path.is_null()) {