github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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  // Returns an absolute path for a file in `dir` that does not yet exist.
    31  // Distinct calls to NewTempAbsPathInDir from the same process, even from
    32  // multiple threads, are guaranteed to return different paths. Distinct calls to
    33  // NewTempAbsPathInDir from different processes are not synchronized.
    34  std::string NewTempAbsPathInDir(absl::string_view const dir);
    35  
    36  // Like NewTempAbsPathInDir, but the returned path is in the test's temporary
    37  // directory, as provided by the testing framework.
    38  std::string NewTempAbsPath();
    39  
    40  // Like NewTempAbsPathInDir, but the returned path is relative (to the current
    41  // working directory).
    42  std::string NewTempRelPath();
    43  
    44  // Returns the absolute path for the test temp dir.
    45  std::string GetAbsoluteTestTmpdir();
    46  
    47  // Represents a temporary file or directory.
    48  class TempPath {
    49   public:
    50    // Default creation mode for files.
    51    static constexpr mode_t kDefaultFileMode = 0644;
    52  
    53    // Default creation mode for directories.
    54    static constexpr mode_t kDefaultDirMode = 0755;
    55  
    56    // Creates a temporary file in directory `parent` with mode `mode` and
    57    // contents `content`.
    58    static PosixErrorOr<TempPath> CreateFileWith(absl::string_view parent,
    59                                                 absl::string_view content,
    60                                                 mode_t mode);
    61  
    62    // Creates an empty temporary subdirectory in directory `parent` with mode
    63    // `mode`.
    64    static PosixErrorOr<TempPath> CreateDirWith(absl::string_view parent,
    65                                                mode_t mode);
    66  
    67    // Creates a temporary symlink in directory `parent` to destination `dest`.
    68    static PosixErrorOr<TempPath> CreateSymlinkTo(absl::string_view parent,
    69                                                  std::string const& dest);
    70  
    71    // Creates an empty temporary file in directory `parent` with mode
    72    // kDefaultFileMode.
    73    static PosixErrorOr<TempPath> CreateFileIn(absl::string_view parent);
    74  
    75    // Creates an empty temporary subdirectory in directory `parent` with mode
    76    // kDefaultDirMode.
    77    static PosixErrorOr<TempPath> CreateDirIn(absl::string_view parent);
    78  
    79    // Creates an empty temporary file in the test's temporary directory with mode
    80    // `mode`.
    81    static PosixErrorOr<TempPath> CreateFileMode(mode_t mode);
    82  
    83    // Creates an empty temporary file in the test's temporary directory with
    84    // mode kDefaultFileMode.
    85    static PosixErrorOr<TempPath> CreateFile();
    86  
    87    // Creates an empty temporary subdirectory in the test's temporary directory
    88    // with mode kDefaultDirMode.
    89    static PosixErrorOr<TempPath> CreateDir();
    90  
    91    // Constructs a TempPath that represents nothing.
    92    TempPath() = default;
    93  
    94    // Constructs a TempPath that represents the given path, which will be deleted
    95    // when the TempPath is destroyed.
    96    explicit TempPath(std::string path) : path_(std::move(path)) {}
    97  
    98    // Attempts to delete the represented temporary file or directory (in the
    99    // latter case, also attempts to delete its contents).
   100    ~TempPath();
   101  
   102    // Attempts to delete the represented temporary file or directory, then
   103    // transfers ownership of the path represented by orig to this TempPath.
   104    TempPath(TempPath&& orig);
   105    TempPath& operator=(TempPath&& orig);
   106  
   107    // Changes the path this TempPath represents. If the TempPath already
   108    // represented a path, deletes and returns that path. Otherwise returns the
   109    // empty string.
   110    std::string reset(std::string newpath);
   111    std::string reset() { return reset(""); }
   112  
   113    // Forgets and returns the path this TempPath represents. The path is not
   114    // deleted.
   115    std::string release();
   116  
   117    // Returns the path this TempPath represents.
   118    std::string path() const { return path_; }
   119  
   120   private:
   121    template <typename F>
   122    static PosixErrorOr<TempPath> CreateIn(absl::string_view const parent,
   123                                           F const& f) {
   124      std::string path = NewTempAbsPathInDir(parent);
   125      RETURN_IF_ERRNO(f(path));
   126      return TempPath(std::move(path));
   127    }
   128  
   129    std::string path_;
   130  };
   131  
   132  }  // namespace testing
   133  }  // namespace gvisor
   134  
   135  #endif  // GVISOR_TEST_UTIL_TEMP_PATH_H_