mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:24:58 +00:00
mktemp: Ensure --tmpdir
and template paths are concatenated correctly
Previously, using the `--tempdir` option with a template containing a path didn't work correctly, as the template path would be discarded.
This commit is contained in:
parent
84252db976
commit
f3a68fc80d
1 changed files with 21 additions and 19 deletions
|
@ -56,11 +56,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
||||||
|
|
||||||
StringView file_template;
|
ByteString file_template;
|
||||||
bool create_directory = false;
|
bool create_directory = false;
|
||||||
bool dry_run = false;
|
bool dry_run = false;
|
||||||
bool quiet = false;
|
bool quiet = false;
|
||||||
StringView target_directory;
|
ByteString target_directory;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.set_general_help("Create a temporary file or directory, safely, and print its name.");
|
args_parser.set_general_help("Create a temporary file or directory, safely, and print its name.");
|
||||||
|
@ -71,33 +71,35 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_option(target_directory, "Create TEMPLATE relative to DIR", "tmpdir", 'p', "DIR");
|
args_parser.add_option(target_directory, "Create TEMPLATE relative to DIR", "tmpdir", 'p', "DIR");
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
Optional<ByteString> final_file_template;
|
if (file_template.is_empty()) {
|
||||||
ByteString final_target_directory = target_directory;
|
file_template = "tmp.XXXXXXXXXX"sv;
|
||||||
|
} else {
|
||||||
|
auto resolved_path = LexicalPath(file_template);
|
||||||
|
if (resolved_path.is_absolute()) {
|
||||||
|
if (!target_directory.is_empty()) {
|
||||||
|
warnln("mktemp: File template cannot be an absolute path if the --tmpdir option is used");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (target_directory.is_empty()) {
|
target_directory = resolved_path.dirname();
|
||||||
if (!file_template.is_empty()) {
|
file_template = resolved_path.basename();
|
||||||
auto resolved_path = LexicalPath(TRY(FileSystem::absolute_path(file_template)));
|
|
||||||
final_target_directory = resolved_path.dirname();
|
|
||||||
final_file_template = resolved_path.basename();
|
|
||||||
} else {
|
|
||||||
final_target_directory = "/tmp";
|
|
||||||
auto const* env_directory = getenv("TMPDIR");
|
|
||||||
if (env_directory != nullptr && *env_directory != 0)
|
|
||||||
final_target_directory = ByteString { env_directory, strlen(env_directory) };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!final_file_template.has_value()) {
|
if (target_directory.is_empty()) {
|
||||||
final_file_template = "tmp.XXXXXXXXXX";
|
target_directory = "/tmp";
|
||||||
|
auto const* env_directory = getenv("TMPDIR");
|
||||||
|
if (env_directory != nullptr && *env_directory != 0)
|
||||||
|
target_directory = ByteString(env_directory, strlen(env_directory));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!final_file_template->find("XXX"sv).has_value()) {
|
if (!file_template.find("XXX"sv).has_value()) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
warnln("Too few X's in template {}", final_file_template);
|
warnln("Too few X's in template {}", file_template);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto target_path = LexicalPath::join(final_target_directory, final_file_template.value()).string();
|
auto target_path = LexicalPath::join(target_directory, file_template).string();
|
||||||
|
|
||||||
auto final_path = TRY(make_temp(target_path, create_directory, dry_run));
|
auto final_path = TRY(make_temp(target_path, create_directory, dry_run));
|
||||||
if (!final_path.has_value()) {
|
if (!final_path.has_value()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue