mktemp.cpp 3.9 KB

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