temp_dir.ncdi 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. include_guard "temp_dir"
  2. template temp_dir {
  3. objref_arg(_arg0) log;
  4. alias(@_arg1) temp_base_dir;
  5. alias(@_arg2) mktemp;
  6. alias(@_arg3) rm;
  7. # Create a temporary directory.
  8. var("") created_dir;
  9. Do {
  10. # Build the path template.
  11. value(@concat(temp_base_dir, "/build-")) temp_prefix;
  12. concat(temp_prefix, "XXXXXXXXXXXXXXXX") temp_template;
  13. # Call mktemp.
  14. call(@run_process_output, {{mktemp, "-d", temp_template}}) mktemp_res;
  15. # Check mktemp success.
  16. If (@not(mktemp_res.succeeded)) {
  17. log->call(@error, "mktemp failed");
  18. _do->break();
  19. };
  20. # Remove a trailing newline from the output.
  21. objref(^mktemp_res.output) output;
  22. If (@num_equal(output.length, "0")) {
  23. log->call(@error, "mktemp result is empty");
  24. _do->break();
  25. };
  26. var(@num_subtract(output.length, "1")) len_minus_one;
  27. output->substr(len_minus_one) last_char;
  28. If (@val_different(last_char, "\n")) {
  29. log->call(@error, "last char of mktemp result is not a newline");
  30. _do->break();
  31. };
  32. output->substr("0", len_minus_one) without_newline;
  33. output->reset(without_newline);
  34. # Check created dir prefix.
  35. output->substr("0", temp_prefix.length) created_prefix;
  36. If (@val_different(created_prefix, temp_prefix)) {
  37. log->call(@error, "mktemp result prefix incorrect");
  38. _do->break();
  39. };
  40. # Remember it.
  41. created_dir->set(output);
  42. } Interrupt {
  43. # Do not interrupt the directory creation, or we might
  44. # end up with creating but not removing a directory.
  45. if(@false);
  46. } dir_creation;
  47. # Expose success status.
  48. var(dir_creation.succeeded) succeeded;
  49. # Arrange for removing the temp dir.
  50. If (succeeded) {
  51. run({}, {rm, "-rf", created_dir});
  52. };
  53. }