mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
mktemp: Port to LibMain
This commit is contained in:
parent
b4d55f2a55
commit
eeb74e2578
2 changed files with 34 additions and 39 deletions
|
@ -131,6 +131,7 @@ target_link_libraries(md LibMarkdown)
|
||||||
target_link_libraries(mkdir LibMain)
|
target_link_libraries(mkdir LibMain)
|
||||||
target_link_libraries(mkfifo LibMain)
|
target_link_libraries(mkfifo LibMain)
|
||||||
target_link_libraries(mknod LibMain)
|
target_link_libraries(mknod LibMain)
|
||||||
|
target_link_libraries(mktemp LibMain)
|
||||||
target_link_libraries(nc LibMain)
|
target_link_libraries(nc LibMain)
|
||||||
target_link_libraries(netstat LibMain)
|
target_link_libraries(netstat LibMain)
|
||||||
target_link_libraries(notify LibGUI)
|
target_link_libraries(notify LibGUI)
|
||||||
|
|
|
@ -7,66 +7,60 @@
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/Random.h>
|
#include <AK/Random.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <errno.h>
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
constexpr const char* default_template = "tmp.XXXXXXXXXX";
|
constexpr StringView default_template = "tmp.XXXXXXXXXX";
|
||||||
|
|
||||||
static char* generate_random_filename(const char* pattern)
|
static String generate_random_filename(String const& pattern)
|
||||||
{
|
{
|
||||||
char* new_filename = strdup(pattern);
|
StringBuilder new_filename { pattern.length() };
|
||||||
int pattern_length = strlen(pattern);
|
|
||||||
|
|
||||||
constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
for (auto i = pattern_length - 1; i >= 0; --i) {
|
for (size_t i = 0; i < pattern.length(); i++) {
|
||||||
if (pattern[i] != 'X')
|
if (pattern[i] == 'X')
|
||||||
break;
|
new_filename.append(random_characters[(get_random<u32>() % (sizeof(random_characters) - 1))]);
|
||||||
new_filename[i] = random_characters[(get_random<u32>() % (sizeof(random_characters) - 1))];
|
else
|
||||||
|
new_filename.append(pattern[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_filename;
|
return new_filename.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
static char* make_temp(const char* pattern, bool directory, bool dry_run)
|
static ErrorOr<String> make_temp(String const& pattern, bool directory, bool dry_run)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 100; ++i) {
|
for (int i = 0; i < 100; ++i) {
|
||||||
char* path = generate_random_filename(pattern);
|
auto path = generate_random_filename(pattern);
|
||||||
if (dry_run) {
|
if (dry_run) {
|
||||||
struct stat stat;
|
auto stat_or_error = Core::System::lstat(path.view());
|
||||||
auto rc = lstat(path, &stat);
|
if (stat_or_error.is_error() && stat_or_error.error().code() == ENOENT)
|
||||||
if (rc < 0 && errno == ENOENT)
|
|
||||||
return path;
|
return path;
|
||||||
} else if (directory) {
|
} else if (directory) {
|
||||||
if (!mkdir(path, 0700))
|
TRY(Core::System::mkdir(path.view(), 0700));
|
||||||
return path;
|
return path;
|
||||||
} else {
|
} else {
|
||||||
auto fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
auto fd_or_error = Core::System::open(path.view(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
||||||
if (fd >= 0) {
|
if (!fd_or_error.is_error()) {
|
||||||
close(fd);
|
TRY(Core::System::close(fd_or_error.value()));
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(path);
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return String {};
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath wpath cpath", nullptr)) {
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* file_template = nullptr;
|
StringView file_template;
|
||||||
bool create_directory = false;
|
bool create_directory = false;
|
||||||
bool dry_run = false;
|
bool dry_run = false;
|
||||||
bool quiet = false;
|
bool quiet = false;
|
||||||
const char* target_directory = nullptr;
|
StringView 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.");
|
||||||
|
@ -75,10 +69,10 @@ int main(int argc, char** argv)
|
||||||
args_parser.add_option(dry_run, "Do not create anything, just print a unique name", "dry-run", 'u');
|
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(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");
|
args_parser.add_option(target_directory, "Create TEMPLATE relative to DIR", "tmpdir", 'p', "DIR");
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
if (!target_directory) {
|
if (target_directory.is_empty()) {
|
||||||
if (file_template) { // If a custom template is specified we assume the target directory is the current directory
|
if (!file_template.is_empty()) { // If a custom template is specified we assume the target directory is the current directory
|
||||||
target_directory = getcwd(nullptr, 0);
|
target_directory = getcwd(nullptr, 0);
|
||||||
} else {
|
} else {
|
||||||
LexicalPath template_path(file_template);
|
LexicalPath template_path(file_template);
|
||||||
|
@ -87,11 +81,11 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file_template) {
|
if (file_template.is_empty()) {
|
||||||
file_template = default_template;
|
file_template = default_template;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!String(file_template).find("XXX").has_value()) {
|
if (!file_template.find("XXX").has_value()) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
warnln("Too few X's in template {}", file_template);
|
warnln("Too few X's in template {}", file_template);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -99,8 +93,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
auto target_path = LexicalPath::join(target_directory, file_template).string();
|
auto target_path = LexicalPath::join(target_directory, file_template).string();
|
||||||
|
|
||||||
char* final_path = make_temp(target_path.characters(), create_directory, dry_run);
|
auto final_path = TRY(make_temp(target_path, create_directory, dry_run));
|
||||||
if (!final_path) {
|
if (final_path.is_null()) {
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
if (create_directory)
|
if (create_directory)
|
||||||
warnln("Failed to create directory via template {}", target_path.characters());
|
warnln("Failed to create directory via template {}", target_path.characters());
|
||||||
|
@ -111,6 +105,6 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
outln("{}", final_path);
|
outln("{}", final_path);
|
||||||
free(final_path);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue