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>
19 #include <sys/types.h>
22 #include <boost/regex.hpp>
23 #include <folly/Exception.h>
24 #include <folly/File.h>
25 #include <folly/FileUtil.h>
26 #include <folly/Memory.h>
27 #include <folly/String.h>
28 #include <folly/portability/Fcntl.h>
39 fs::path generateUniquePath(fs::path path, StringPiece namePrefix) {
41 path = fs::temp_directory_path();
43 if (namePrefix.empty()) {
44 path /= fs::unique_path();
46 path /= fs::unique_path(
47 to<std::string>(namePrefix, ".%%%%-%%%%-%%%%-%%%%"));
54 TemporaryFile::TemporaryFile(StringPiece namePrefix,
57 bool closeOnDestruction)
59 closeOnDestruction_(closeOnDestruction),
61 path_(generateUniquePath(std::move(dir), namePrefix)) {
62 fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
63 checkUnixError(fd_, "open failed");
65 if (scope_ == Scope::UNLINK_IMMEDIATELY) {
66 boost::system::error_code ec;
67 fs::remove(path_, ec);
69 LOG(WARNING) << "unlink on construction failed: " << ec;
76 const fs::path& TemporaryFile::path() const {
77 CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
78 DCHECK(!path_.empty());
82 TemporaryFile::~TemporaryFile() {
83 if (fd_ != -1 && closeOnDestruction_) {
84 if (close(fd_) == -1) {
85 PLOG(ERROR) << "close failed";
89 // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
91 if (scope_ != Scope::PERMANENT && !path_.empty()) {
92 boost::system::error_code ec;
93 fs::remove(path_, ec);
95 LOG(WARNING) << "unlink on destruction failed: " << ec;
100 TemporaryDirectory::TemporaryDirectory(
101 StringPiece namePrefix,
105 path_(folly::make_unique<fs::path>(
106 generateUniquePath(std::move(dir), namePrefix))) {
107 fs::create_directory(path());
110 TemporaryDirectory::~TemporaryDirectory() {
111 if (scope_ == Scope::DELETE_ON_DESTRUCTION && path_ != nullptr) {
112 boost::system::error_code ec;
113 fs::remove_all(path(), ec);
115 LOG(WARNING) << "recursive delete on destruction failed: " << ec;
120 ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
121 std::string p = dir_.path().string();
125 ChangeToTempDir::~ChangeToTempDir() {
126 std::string p = initialPath_.string();
132 SavedState disableInvalidParameters() {
135 ret.previousThreadLocalHandler = _set_thread_local_invalid_parameter_handler(
141 ret.previousCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
149 void enableInvalidParameters(SavedState state) {
150 _set_thread_local_invalid_parameter_handler(
151 (_invalid_parameter_handler)state.previousThreadLocalHandler);
152 _CrtSetReportMode(_CRT_ASSERT, state.previousCrtReportMode);
155 void enableInvalidParameters(SavedState) {}
158 bool hasPCREPatternMatch(StringPiece pattern, StringPiece target) {
159 return boost::regex_match(
162 boost::regex(pattern.begin(), pattern.end())
166 bool hasNoPCREPatternMatch(StringPiece pattern, StringPiece target) {
167 return !hasPCREPatternMatch(pattern, target);
170 } // namespace detail
172 CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
173 : chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
174 oldFDCopy_ = dup(fd_);
175 PCHECK(oldFDCopy_ != -1) << "Could not copy FD " << fd_;
177 int file_fd = open(file_.path().string().c_str(), O_WRONLY|O_CREAT, 0600);
178 PCHECK(dup2(file_fd, fd_) != -1) << "Could not replace FD " << fd_
179 << " with " << file_fd;
180 PCHECK(close(file_fd) != -1) << "Could not close " << file_fd;
183 void CaptureFD::release() {
184 if (oldFDCopy_ != fd_) {
185 readIncremental(); // Feed chunkCob_
186 PCHECK(dup2(oldFDCopy_, fd_) != -1) << "Could not restore old FD "
187 << oldFDCopy_ << " into " << fd_;
188 PCHECK(close(oldFDCopy_) != -1) << "Could not close " << oldFDCopy_;
189 oldFDCopy_ = fd_; // Make this call idempotent
193 CaptureFD::~CaptureFD() {
197 std::string CaptureFD::read() const {
198 std::string contents;
199 std::string filename = file_.path().string();
200 PCHECK(folly::readFile(filename.c_str(), contents));
204 std::string CaptureFD::readIncremental() {
205 std::string filename = file_.path().string();
206 // Yes, I know that I could just keep the file open instead. So sue me.
207 folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
208 auto size = size_t(lseek(f.fd(), 0, SEEK_END) - readOffset_);
209 std::unique_ptr<char[]> buf(new char[size]);
210 auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
211 PCHECK(ssize_t(size) == bytes_read);
212 readOffset_ += off_t(size);
213 chunkCob_(StringPiece(buf.get(), buf.get() + size));
214 return std::string(buf.get(), size);