tempfile.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <c10/macros/Export.h>
  3. #include <optional>
  4. #include <string>
  5. #include <string_view>
  6. #include <utility>
  7. namespace c10 {
  8. struct C10_API TempFile {
  9. TempFile(std::string_view name, int fd = -1) noexcept : fd(fd), name(name) {}
  10. TempFile(const TempFile&) = delete;
  11. TempFile(TempFile&& other) noexcept
  12. : fd(other.fd), name(std::move(other.name)) {
  13. other.fd = -1;
  14. }
  15. TempFile& operator=(const TempFile&) = delete;
  16. TempFile& operator=(TempFile&& other) noexcept {
  17. fd = other.fd;
  18. name = std::move(other.name);
  19. other.fd = -1;
  20. return *this;
  21. }
  22. #if defined(_WIN32)
  23. bool open();
  24. #endif
  25. ~TempFile();
  26. int fd;
  27. std::string name;
  28. };
  29. struct C10_API TempDir {
  30. TempDir() = delete;
  31. explicit TempDir(std::string_view name) noexcept : name(name) {}
  32. TempDir(const TempDir&) = delete;
  33. TempDir(TempDir&& other) noexcept : name(std::move(other.name)) {
  34. other.name.clear();
  35. }
  36. TempDir& operator=(const TempDir&) = delete;
  37. TempDir& operator=(TempDir&& other) noexcept {
  38. name = std::move(other.name);
  39. return *this;
  40. }
  41. ~TempDir();
  42. std::string name;
  43. };
  44. /// Attempts to return a temporary file or returns `nullopt` if an error
  45. /// occurred.
  46. ///
  47. /// The file returned follows the pattern
  48. /// `<tmp-dir>/<name-prefix><random-pattern>`, where `<tmp-dir>` is the value of
  49. /// the `"TMPDIR"`, `"TMP"`, `"TEMP"` or
  50. /// `"TEMPDIR"` environment variable if any is set, or otherwise `/tmp`;
  51. /// `<name-prefix>` is the value supplied to this function, and
  52. /// `<random-pattern>` is a random sequence of numbers.
  53. /// On Windows, `name_prefix` is ignored and `tmpnam_s` is used,
  54. /// and no temporary file is opened.
  55. C10_API std::optional<TempFile> try_make_tempfile(
  56. std::string_view name_prefix = "torch-file-");
  57. /// Like `try_make_tempfile`, but throws an exception if a temporary file could
  58. /// not be returned.
  59. C10_API TempFile make_tempfile(std::string_view name_prefix = "torch-file-");
  60. /// Attempts to return a temporary directory or returns `nullopt` if an error
  61. /// occurred.
  62. ///
  63. /// The directory returned follows the pattern
  64. /// `<tmp-dir>/<name-prefix><random-pattern>/`, where `<tmp-dir>` is the value
  65. /// of the `"TMPDIR"`, `"TMP"`, `"TEMP"` or
  66. /// `"TEMPDIR"` environment variable if any is set, or otherwise `/tmp`;
  67. /// `<name-prefix>` is the value supplied to this function, and
  68. /// `<random-pattern>` is a random sequence of numbers.
  69. /// On Windows, `name_prefix` is ignored.
  70. C10_API std::optional<TempDir> try_make_tempdir(
  71. std::string_view name_prefix = "torch-dir-");
  72. /// Like `try_make_tempdir`, but throws an exception if a temporary directory
  73. /// could not be returned.
  74. C10_API TempDir make_tempdir(std::string_view name_prefix = "torch-dir-");
  75. } // namespace c10