Serenity Operating System
at master 112 lines 4.2 kB view raw
1/* 2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/LexicalPath.h> 8#include <AK/Random.h> 9#include <LibCore/ArgsParser.h> 10#include <LibCore/System.h> 11#include <LibMain/Main.h> 12#include <fcntl.h> 13#include <sys/stat.h> 14#include <unistd.h> 15 16constexpr StringView default_template = "tmp.XXXXXXXXXX"sv; 17 18static DeprecatedString generate_random_filename(DeprecatedString const& pattern) 19{ 20 StringBuilder new_filename { pattern.length() }; 21 22 constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789"; 23 for (size_t i = 0; i < pattern.length(); i++) { 24 if (pattern[i] == 'X') 25 new_filename.append(random_characters[(get_random<u32>() % (sizeof(random_characters) - 1))]); 26 else 27 new_filename.append(pattern[i]); 28 } 29 30 return new_filename.to_deprecated_string(); 31} 32 33static ErrorOr<DeprecatedString> make_temp(DeprecatedString const& pattern, bool directory, bool dry_run) 34{ 35 for (int i = 0; i < 100; ++i) { 36 auto path = generate_random_filename(pattern); 37 if (dry_run) { 38 auto stat_or_error = Core::System::lstat(path.view()); 39 if (stat_or_error.is_error() && stat_or_error.error().code() == ENOENT) 40 return path; 41 } else if (directory) { 42 TRY(Core::System::mkdir(path.view(), 0700)); 43 return path; 44 } else { 45 auto fd_or_error = Core::System::open(path.view(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 46 if (!fd_or_error.is_error()) { 47 TRY(Core::System::close(fd_or_error.value())); 48 return path; 49 } 50 } 51 } 52 return DeprecatedString {}; 53} 54 55ErrorOr<int> serenity_main(Main::Arguments arguments) 56{ 57 TRY(Core::System::pledge("stdio rpath wpath cpath")); 58 59 StringView file_template; 60 bool create_directory = false; 61 bool dry_run = false; 62 bool quiet = false; 63 StringView target_directory; 64 65 Core::ArgsParser args_parser; 66 args_parser.set_general_help("Create a temporary file or directory, safely, and print its name."); 67 args_parser.add_positional_argument(file_template, "The template must contain at least 3 consecutive 'X's", "template", Core::ArgsParser::Required::No); 68 args_parser.add_option(create_directory, "Create a temporary directory instead of a file", "directory", 'd'); 69 args_parser.add_option(dry_run, "Do not create anything, just print a unique name", "dry-run", 'u'); 70 args_parser.add_option(quiet, "Do not print diagnostics about file/directory creation failure", "quiet", 'q'); 71 args_parser.add_option(target_directory, "Create TEMPLATE relative to DIR", "tmpdir", 'p', "DIR"); 72 args_parser.parse(arguments); 73 74 if (target_directory.is_empty()) { 75 if (!file_template.is_empty()) { // If a custom template is specified we assume the target directory is the current directory 76 // FIXME: Get rid of this minor memory leak. 77 auto const* cwd_ptr = getcwd(nullptr, 0); 78 target_directory = StringView { cwd_ptr, strlen(cwd_ptr) }; 79 } else { 80 LexicalPath template_path(file_template); 81 char const* env_directory = getenv("TMPDIR"); 82 target_directory = env_directory && *env_directory ? StringView { env_directory, strlen(env_directory) } : "/tmp"sv; 83 } 84 } 85 86 if (file_template.is_empty()) { 87 file_template = default_template; 88 } 89 90 if (!file_template.find("XXX"sv).has_value()) { 91 if (!quiet) 92 warnln("Too few X's in template {}", file_template); 93 return 1; 94 } 95 96 auto target_path = LexicalPath::join(target_directory, file_template).string(); 97 98 auto final_path = TRY(make_temp(target_path, create_directory, dry_run)); 99 if (final_path.is_null()) { 100 if (!quiet) { 101 if (create_directory) 102 warnln("Failed to create directory via template {}", target_path.characters()); 103 else 104 warnln("Failed to create file via template {}", target_path.characters()); 105 } 106 return 1; 107 } 108 109 outln("{}", final_path); 110 111 return 0; 112}