2 * Copyright 2017 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>
20 #include <sys/types.h>
22 #include <boost/regex.hpp>
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/Fcntl.h>
32 #include <crtdbg.h> // @manual
40 fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
42 path = fs::temp_directory_path();
44 if (namePrefix.empty()) {
45 path /= fs::unique_path();
47 path /= fs::unique_path(
48 to<std::string>(namePrefix, ".%%%%-%%%%-%%%%-%%%%"));
55 TemporaryFile::TemporaryFile(StringPiece namePrefix,
58 bool closeOnDestruction)
60 closeOnDestruction_(closeOnDestruction),
62 path_(generateUniquePath(std::move(dir), namePrefix)) {
63 fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
64 checkUnixError(fd_, "open failed");
66 if (scope_ == Scope::UNLINK_IMMEDIATELY) {
67 boost::system::error_code ec;
68 fs::remove(path_, ec);
70 LOG(WARNING) << "unlink on construction failed: " << ec;
77 void TemporaryFile::close() {
78 if (::close(fd_) == -1) {
79 PLOG(ERROR) << "close failed";
84 const fs::path& TemporaryFile::path() const {
85 CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
86 DCHECK(!path_.empty());
90 void TemporaryFile::reset() {
91 if (fd_ != -1 && closeOnDestruction_) {
92 if (::close(fd_) == -1) {
93 PLOG(ERROR) << "close failed (fd = " << fd_ << "): ";
97 // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
99 if (scope_ != Scope::PERMANENT && !path_.empty()) {
100 boost::system::error_code ec;
101 fs::remove(path_, ec);
103 LOG(WARNING) << "unlink on destruction failed: " << ec;
108 TemporaryFile::~TemporaryFile() {
112 TemporaryDirectory::TemporaryDirectory(
113 StringPiece namePrefix,
117 path_(std::make_unique<fs::path>(
118 generateUniquePath(std::move(dir), namePrefix))) {
119 fs::create_directory(path());
122 TemporaryDirectory::~TemporaryDirectory() {
123 if (scope_ == Scope::DELETE_ON_DESTRUCTION && path_ != nullptr) {
124 boost::system::error_code ec;
125 fs::remove_all(path(), ec);
127 LOG(WARNING) << "recursive delete on destruction failed: " << ec;
132 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
133 std::string p = dir_.path().string();
137 ChangeToTempDir::~ChangeToTempDir() {
138 std::string p = initialPath_.string();
144 SavedState disableInvalidParameters() {
147 ret.previousThreadLocalHandler = _set_thread_local_invalid_parameter_handler(
153 ret.previousCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
161 void enableInvalidParameters(SavedState state) {
162 _set_thread_local_invalid_parameter_handler(
163 (_invalid_parameter_handler)state.previousThreadLocalHandler);
164 _CrtSetReportMode(_CRT_ASSERT, state.previousCrtReportMode);
167 void enableInvalidParameters(SavedState) {}
170 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
171 return boost::regex_match(
174 boost::regex(pattern.begin(), pattern.end())
178 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
179 return !hasPCREPatternMatch(pattern, target);
182 } // namespace detail
184 CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
185 : chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
186 oldFDCopy_ = dup(fd_);
187 PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
189 int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
190 PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
191 << " with " << file_fd;
192 PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
195 void CaptureFD::release() {
196 if (oldFDCopy_ != fd_) {
197 readIncremental(); // Feed chunkCob_
198 PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
199 << oldFDCopy_ << " into " << fd_;
200 PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
201 oldFDCopy_ = fd_; // Make this call idempotent
205 CaptureFD::~CaptureFD() {
209 std::string CaptureFD::read() const {
210 std::string contents;
211 std::string filename = file_.path().string();
212 PCHECK(folly::readFile(filename.c_str(), contents));
216 std::string CaptureFD::readIncremental() {
217 std::string filename = file_.path().string();
218 // Yes, I know that I could just keep the file open instead. So sue me.
219 folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
220 auto size = size_t(lseek(f.fd(), 0, SEEK_END) - readOffset_);
221 std::unique_ptr<char[]> buf(new char[size]);
222 auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
223 PCHECK(ssize_t(size) == bytes_read);
224 readOffset_ += off_t(size);
225 chunkCob_(StringPiece(buf.get(), buf.get() + size));
226 return std::string(buf.get(), size);