gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/util/temp_path.h (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GVISOR_TEST_UTIL_TEMP_PATH_H_ 16 #define GVISOR_TEST_UTIL_TEMP_PATH_H_ 17 18 #include <sys/stat.h> 19 20 #include <string> 21 #include <utility> 22 23 #include "absl/strings/str_cat.h" 24 #include "absl/strings/string_view.h" 25 #include "test/util/posix_error.h" 26 27 namespace gvisor { 28 namespace testing { 29 30 // Return a new temp filename, intended to be unique system-wide. 31 std::string NextTempBasename(); 32 33 // Returns an absolute path for a file in `dir` that does not yet exist. 34 // Distinct calls to NewTempAbsPathInDir from the same process, even from 35 // multiple threads, are guaranteed to return different paths. Distinct calls to 36 // NewTempAbsPathInDir from different processes are not synchronized. 37 std::string NewTempAbsPathInDir(absl::string_view const dir); 38 39 // Like NewTempAbsPathInDir, but the returned path is in the test's temporary 40 // directory, as provided by the testing framework. 41 std::string NewTempAbsPath(); 42 43 // Like NewTempAbsPathInDir, but the returned path is relative (to the current 44 // working directory). 45 std::string NewTempRelPath(); 46 47 // Returns the absolute path for the test temp dir. 48 std::string GetAbsoluteTestTmpdir(); 49 50 // Represents a temporary file or directory. 51 class TempPath { 52 public: 53 // Default creation mode for files. 54 static constexpr mode_t kDefaultFileMode = 0644; 55 56 // Default creation mode for directories. 57 static constexpr mode_t kDefaultDirMode = 0755; 58 59 // Creates a temporary file in directory `parent` with mode `mode` and 60 // contents `content`. 61 static PosixErrorOr<TempPath> CreateFileWith(absl::string_view parent, 62 absl::string_view content, 63 mode_t mode); 64 65 // Creates an empty temporary subdirectory in directory `parent` with mode 66 // `mode`. 67 static PosixErrorOr<TempPath> CreateDirWith(absl::string_view parent, 68 mode_t mode); 69 70 // Creates a temporary symlink in directory `parent` to destination `dest`. 71 static PosixErrorOr<TempPath> CreateSymlinkTo(absl::string_view parent, 72 std::string const& dest); 73 74 // Creates an empty temporary file in directory `parent` with mode 75 // kDefaultFileMode. 76 static PosixErrorOr<TempPath> CreateFileIn(absl::string_view parent); 77 78 // Creates an empty temporary subdirectory in directory `parent` with mode 79 // kDefaultDirMode. 80 static PosixErrorOr<TempPath> CreateDirIn(absl::string_view parent); 81 82 // Creates an empty temporary file in the test's temporary directory with mode 83 // `mode`. 84 static PosixErrorOr<TempPath> CreateFileMode(mode_t mode); 85 86 // Creates an empty temporary file in the test's temporary directory with 87 // mode kDefaultFileMode. 88 static PosixErrorOr<TempPath> CreateFile(); 89 90 // Creates an empty temporary subdirectory in the test's temporary directory 91 // with mode kDefaultDirMode. 92 static PosixErrorOr<TempPath> CreateDir(); 93 94 // Constructs a TempPath that represents nothing. 95 TempPath() = default; 96 97 // Constructs a TempPath that represents the given path, which will be deleted 98 // when the TempPath is destroyed. 99 explicit TempPath(std::string path) : path_(std::move(path)) {} 100 101 // Attempts to delete the represented temporary file or directory (in the 102 // latter case, also attempts to delete its contents). 103 ~TempPath(); 104 105 // Attempts to delete the represented temporary file or directory, then 106 // transfers ownership of the path represented by orig to this TempPath. 107 TempPath(TempPath&& orig); 108 TempPath& operator=(TempPath&& orig); 109 110 // Changes the path this TempPath represents. If the TempPath already 111 // represented a path, deletes and returns that path. Otherwise returns the 112 // empty string. 113 std::string reset(std::string newpath); 114 std::string reset() { return reset(""); } 115 116 // Forgets and returns the path this TempPath represents. The path is not 117 // deleted. 118 std::string release(); 119 120 // Returns the path this TempPath represents. 121 std::string const& path() const { return path_; } 122 123 private: 124 template <typename F> 125 static PosixErrorOr<TempPath> CreateIn(absl::string_view const parent, 126 F const& f) { 127 std::string path = NewTempAbsPathInDir(parent); 128 RETURN_IF_ERRNO(f(path)); 129 return TempPath(std::move(path)); 130 } 131 132 std::string path_; 133 }; 134 135 } // namespace testing 136 } // namespace gvisor 137 138 #endif // GVISOR_TEST_UTIL_TEMP_PATH_H_