2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/TestUtil.h>
19 #include <sys/types.h>
22 #include <boost/regex.hpp>
23 #include <folly/Conv.h>
24 #include <folly/Exception.h>
25 #include <folly/File.h>
26 #include <folly/FileUtil.h>
27 #include <folly/Memory.h>
28 #include <folly/String.h>
29 #include <folly/portability/Environment.h>
30 #include <folly/portability/Fcntl.h>
31 #include <folly/portability/Unistd.h>
42 fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
44 path = fs::temp_directory_path();
46 if (namePrefix.empty()) {
47 path /= fs::unique_path();
49 path /= fs::unique_path(
50 to<std::string>(namePrefix, ".%%%%-%%%%-%%%%-%%%%"));
57 TemporaryFile::TemporaryFile(StringPiece namePrefix,
60 bool closeOnDestruction)
62 closeOnDestruction_(closeOnDestruction),
64 path_(generateUniquePath(std::move(dir), namePrefix)) {
65 fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
66 checkUnixError(fd_, "open failed");
68 if (scope_ == Scope::UNLINK_IMMEDIATELY) {
69 boost::system::error_code ec;
70 fs::remove(path_, ec);
72 LOG(WARNING) << "unlink on construction failed: " << ec;
79 const fs::path& TemporaryFile::path() const {
80 CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
81 DCHECK(!path_.empty());
85 TemporaryFile::~TemporaryFile() {
86 if (fd_ != -1 && closeOnDestruction_) {
87 if (close(fd_) == -1) {
88 PLOG(ERROR) << "close failed";
92 // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
94 if (scope_ != Scope::PERMANENT && !path_.empty()) {
95 boost::system::error_code ec;
96 fs::remove(path_, ec);
98 LOG(WARNING) << "unlink on destruction failed: " << ec;
103 TemporaryDirectory::TemporaryDirectory(
104 StringPiece namePrefix,
108 path_(folly::make_unique<fs::path>(
109 generateUniquePath(std::move(dir), namePrefix))) {
110 fs::create_directory(path());
113 TemporaryDirectory::~TemporaryDirectory() {
114 if (scope_ == Scope::DELETE_ON_DESTRUCTION && path_ != nullptr) {
115 boost::system::error_code ec;
116 fs::remove_all(path(), ec);
118 LOG(WARNING) << "recursive delete on destruction failed: " << ec;
123 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
124 std::string p = dir_.path().string();
128 ChangeToTempDir::~ChangeToTempDir() {
129 std::string p = initialPath_.string();
135 SavedState disableInvalidParameters() {
138 ret.previousThreadLocalHandler = _set_thread_local_invalid_parameter_handler(
144 ret.previousCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
152 void enableInvalidParameters(SavedState state) {
153 _set_thread_local_invalid_parameter_handler(
154 (_invalid_parameter_handler)state.previousThreadLocalHandler);
155 _CrtSetReportMode(_CRT_ASSERT, state.previousCrtReportMode);
158 void enableInvalidParameters(SavedState) {}
161 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
162 return boost::regex_match(
165 boost::regex(pattern.begin(), pattern.end())
169 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
170 return !hasPCREPatternMatch(pattern, target);
173 } // namespace detail
175 CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
176 : chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
177 oldFDCopy_ = dup(fd_);
178 PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
180 int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
181 PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
182 << " with " << file_fd;
183 PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
186 void CaptureFD::release() {
187 if (oldFDCopy_ != fd_) {
188 readIncremental(); // Feed chunkCob_
189 PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
190 << oldFDCopy_ << " into " << fd_;
191 PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
192 oldFDCopy_ = fd_; // Make this call idempotent
196 CaptureFD::~CaptureFD() {
200 std::string CaptureFD::read() const {
201 std::string contents;
202 std::string filename = file_.path().string();
203 PCHECK(folly::readFile(filename.c_str(), contents));
207 std::string CaptureFD::readIncremental() {
208 std::string filename = file_.path().string();
209 // Yes, I know that I could just keep the file open instead. So sue me.
210 folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
211 auto size = lseek(f.fd(), 0, SEEK_END) - readOffset_;
212 std::unique_ptr<char[]> buf(new char[size]);
213 auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
214 PCHECK(size == bytes_read);
216 chunkCob_(StringPiece(buf.get(), buf.get() + size));
217 return std::string(buf.get(), size);
220 static std::map<std::string, std::string> getEnvVarMap() {
221 std::map<std::string, std::string> data;
222 for (auto it = environ; *it != nullptr; ++it) {
223 std::string key, value;
224 split("=", *it, key, value);
228 CHECK(!data.count(key)) << "already contains: " << key;
229 data.emplace(move(key), move(value));
234 EnvVarSaver::EnvVarSaver() {
235 saved_ = getEnvVarMap();
238 EnvVarSaver::~EnvVarSaver() {
239 for (const auto& kvp : getEnvVarMap()) {
240 if (saved_.count(kvp.first)) {
243 PCHECK(0 == unsetenv(kvp.first.c_str()));
245 for (const auto& kvp : saved_) {
246 PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));