Clear old fs::path when moving a TemporaryDirectory
[folly.git] / folly / experimental / TestUtil.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/experimental/TestUtil.h>
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include <boost/regex.hpp>
25 #include <folly/Conv.h>
26 #include <folly/Exception.h>
27 #include <folly/File.h>
28 #include <folly/FileUtil.h>
29 #include <folly/String.h>
30 #include <folly/portability/Environment.h>
31
32 namespace folly {
33 namespace test {
34
35 namespace {
36
37 fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
38   if (path.empty()) {
39     path = fs::temp_directory_path();
40   }
41   if (namePrefix.empty()) {
42     path /= fs::unique_path();
43   } else {
44     path /= fs::unique_path(
45         to<std::string>(namePrefix, ".%%%%-%%%%-%%%%-%%%%"));
46   }
47   return path;
48 }
49
50 }  // namespace
51
52 TemporaryFile::TemporaryFile(StringPiece namePrefix,
53                              fs::path dir,
54                              Scope scope,
55                              bool closeOnDestruction)
56   : scope_(scope),
57     closeOnDestruction_(closeOnDestruction),
58     fd_(-1),
59     path_(generateUniquePath(std::move(dir), namePrefix)) {
60   fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
61   checkUnixError(fd_, "open failed");
62
63   if (scope_ == Scope::UNLINK_IMMEDIATELY) {
64     boost::system::error_code ec;
65     fs::remove(path_, ec);
66     if (ec) {
67       LOG(WARNING) << "unlink on construction failed: " << ec;
68     } else {
69       path_.clear();
70     }
71   }
72 }
73
74 const fs::path& TemporaryFile::path() const {
75   CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
76   DCHECK(!path_.empty());
77   return path_;
78 }
79
80 TemporaryFile::~TemporaryFile() {
81   if (fd_ != -1 && closeOnDestruction_) {
82     if (close(fd_) == -1) {
83       PLOG(ERROR) << "close failed";
84     }
85   }
86
87   // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
88   // try again here.
89   if (scope_ != Scope::PERMANENT && !path_.empty()) {
90     boost::system::error_code ec;
91     fs::remove(path_, ec);
92     if (ec) {
93       LOG(WARNING) << "unlink on destruction failed: " << ec;
94     }
95   }
96 }
97
98 TemporaryDirectory::TemporaryDirectory(
99     StringPiece namePrefix,
100     fs::path dir,
101     Scope scope)
102     : scope_(scope),
103       path_(std::make_unique<fs::path>(
104           generateUniquePath(std::move(dir), namePrefix))) {
105   fs::create_directory(path());
106 }
107
108 TemporaryDirectory::~TemporaryDirectory() {
109   if (scope_ == Scope::DELETE_ON_DESTRUCTION && path_ != nullptr) {
110     boost::system::error_code ec;
111     fs::remove_all(path(), ec);
112     if (ec) {
113       LOG(WARNING) << "recursive delete on destruction failed: " << ec;
114     }
115   }
116 }
117
118 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
119   std::string p = dir_.path().string();
120   ::chdir(p.c_str());
121 }
122
123 ChangeToTempDir::~ChangeToTempDir() {
124   std::string p = initialPath_.string();
125   ::chdir(p.c_str());
126 }
127
128 namespace detail {
129
130 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
131   return boost::regex_match(
132     target.begin(),
133     target.end(),
134     boost::regex(pattern.begin(), pattern.end())
135   );
136 }
137
138 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
139   return !hasPCREPatternMatch(pattern, target);
140 }
141
142 }  // namespace detail
143
144 CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
145     : chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
146   oldFDCopy_ = dup(fd_);
147   PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
148
149   int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
150   PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
151     << " with " << file_fd;
152   PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
153 }
154
155 void CaptureFD::release() {
156   if (oldFDCopy_ != fd_) {
157     readIncremental();  // Feed chunkCob_
158     PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
159       << oldFDCopy_ << " into " << fd_;
160     PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
161     oldFDCopy_ = fd_;  // Make this call idempotent
162   }
163 }
164
165 CaptureFD::~CaptureFD() {
166   release();
167 }
168
169 std::string CaptureFD::read() const {
170   std::string contents;
171   std::string filename = file_.path().string();
172   PCHECK(folly::readFile(filename.c_str(), contents));
173   return contents;
174 }
175
176 std::string CaptureFD::readIncremental() {
177   std::string filename = file_.path().string();
178   // Yes, I know that I could just keep the file open instead. So sue me.
179   folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
180   auto size = lseek(f.fd(), 0, SEEK_END) - readOffset_;
181   std::unique_ptr<char[]> buf(new char[size]);
182   auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
183   PCHECK(size == bytes_read);
184   readOffset_ += size;
185   chunkCob_(StringPiece(buf.get(), buf.get() + size));
186   return std::string(buf.get(), size);
187 }
188
189 static std::map<std::string, std::string> getEnvVarMap() {
190   std::map<std::string, std::string> data;
191   for (auto it = environ; *it != nullptr; ++it) {
192     std::string key, value;
193     split("=", *it, key, value);
194     if (key.empty()) {
195       continue;
196     }
197     CHECK(!data.count(key)) << "already contains: " << key;
198     data.emplace(move(key), move(value));
199   }
200   return data;
201 }
202
203 EnvVarSaver::EnvVarSaver() {
204   saved_ = getEnvVarMap();
205 }
206
207 EnvVarSaver::~EnvVarSaver() {
208   for (const auto& kvp : getEnvVarMap()) {
209     if (saved_.count(kvp.first)) {
210       continue;
211     }
212     PCHECK(0 == unsetenv(kvp.first.c_str()));
213   }
214   for (const auto& kvp : saved_) {
215     PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
216   }
217 }
218
219 }  // namespace test
220 }  // namespace folly