122-fs-add-mkdtemp-method-for-creating-temporary-directo.patch 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. From: Felix Fietkau <[email protected]>
  2. Date: Thu, 9 Oct 2025 14:11:55 +0200
  3. Subject: [PATCH] fs: add mkdtemp() method for creating temporary directories
  4. Returns the directory path as a string, following the same template
  5. handling pattern as mkstemp().
  6. Signed-off-by: Felix Fietkau <[email protected]>
  7. ---
  8. --- a/lib/fs.c
  9. +++ b/lib/fs.c
  10. @@ -2636,6 +2636,86 @@ uc_fs_mkstemp(uc_vm_t *vm, size_t nargs)
  11. }
  12. /**
  13. + * Creates a unique temporary directory based on the given template.
  14. + *
  15. + * If the template argument is given and contains a relative path, the created
  16. + * directory will be placed relative to the current working directory.
  17. + *
  18. + * If the template argument is given and contains an absolute path, the created
  19. + * directory will be placed relative to the given directory.
  20. + *
  21. + * If the template argument is given but does not contain a directory separator,
  22. + * the directory will be placed in `/tmp/`.
  23. + *
  24. + * If no template argument is given, the default `/tmp/XXXXXX` is used.
  25. + *
  26. + * The template argument must end with six consecutive X characters (`XXXXXX`),
  27. + * which will be replaced with a random string to create the unique directory name.
  28. + * If the template does not end with `XXXXXX`, it will be automatically appended.
  29. + *
  30. + * Returns a string containing the path of the created directory on success.
  31. + *
  32. + * Returns `null` if an error occurred, e.g. on insufficient permissions or
  33. + * inaccessible directory.
  34. + *
  35. + * @function module:fs#mkdtemp
  36. + *
  37. + * @param {string} [template="/tmp/XXXXXX"]
  38. + * The path template to use when forming the temporary directory name.
  39. + *
  40. + * @returns {?string}
  41. + *
  42. + * @example
  43. + * // Create a unique temporary directory in the current working directory
  44. + * const tempDir = mkdtemp('./data-XXXXXX');
  45. + */
  46. +static uc_value_t *
  47. +uc_fs_mkdtemp(uc_vm_t *vm, size_t nargs)
  48. +{
  49. + uc_value_t *template = uc_fn_arg(0);
  50. + bool ends_with_template = false;
  51. + char *path, *t, *result;
  52. + uc_value_t *rv;
  53. + size_t l;
  54. +
  55. + if (template && ucv_type(template) != UC_STRING)
  56. + err_return(EINVAL);
  57. +
  58. + t = ucv_string_get(template);
  59. + l = ucv_string_length(template);
  60. +
  61. + ends_with_template = (l >= 6 && strcmp(&t[l - 6], "XXXXXX") == 0);
  62. +
  63. + if (t && strchr(t, '/')) {
  64. + if (ends_with_template)
  65. + xasprintf(&path, "%s", t);
  66. + else
  67. + xasprintf(&path, "%s.XXXXXX", t);
  68. + }
  69. + else if (t) {
  70. + if (ends_with_template)
  71. + xasprintf(&path, "/tmp/%s", t);
  72. + else
  73. + xasprintf(&path, "/tmp/%s.XXXXXX", t);
  74. + }
  75. + else {
  76. + xasprintf(&path, "/tmp/XXXXXX");
  77. + }
  78. +
  79. + result = mkdtemp(path);
  80. +
  81. + if (!result) {
  82. + free(path);
  83. + err_return(errno);
  84. + }
  85. +
  86. + rv = ucv_string_new(result);
  87. + free(path);
  88. +
  89. + return rv;
  90. +}
  91. +
  92. +/**
  93. * Checks the accessibility of a file or directory.
  94. *
  95. * The optional modes argument specifies the access modes which should be
  96. @@ -3069,6 +3149,7 @@ static const uc_function_list_t global_f
  97. { "basename", uc_fs_basename },
  98. { "lsdir", uc_fs_lsdir },
  99. { "mkstemp", uc_fs_mkstemp },
  100. + { "mkdtemp", uc_fs_mkdtemp },
  101. { "access", uc_fs_access },
  102. { "readfile", uc_fs_readfile },
  103. { "writefile", uc_fs_writefile },