tempfile.h 2.9 KB

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