#include <folly/experimental/TestUtil.h>
-#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <boost/regex.hpp>
-#include <folly/Conv.h>
+
#include <folly/Exception.h>
#include <folly/File.h>
#include <folly/FileUtil.h>
#include <folly/Memory.h>
#include <folly/String.h>
-#include <folly/portability/Environment.h>
#include <folly/portability/Fcntl.h>
-#include <folly/portability/Unistd.h>
#ifdef _WIN32
-#include <crtdbg.h>
+#include <crtdbg.h> // @manual
#endif
namespace folly {
return path;
}
-} // namespace
+} // namespace
TemporaryFile::TemporaryFile(StringPiece namePrefix,
fs::path dir,
}
}
+void TemporaryFile::close() {
+ if (::close(fd_) == -1) {
+ PLOG(ERROR) << "close failed";
+ }
+ fd_ = -1;
+}
+
const fs::path& TemporaryFile::path() const {
CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
DCHECK(!path_.empty());
return path_;
}
-TemporaryFile::~TemporaryFile() {
+void TemporaryFile::reset() {
if (fd_ != -1 && closeOnDestruction_) {
- if (close(fd_) == -1) {
- PLOG(ERROR) << "close failed";
+ if (::close(fd_) == -1) {
+ PLOG(ERROR) << "close failed (fd = " << fd_ << "): ";
}
}
}
}
+TemporaryFile::~TemporaryFile() {
+ reset();
+}
+
TemporaryDirectory::TemporaryDirectory(
StringPiece namePrefix,
fs::path dir,
Scope scope)
: scope_(scope),
- path_(folly::make_unique<fs::path>(
+ path_(std::make_unique<fs::path>(
generateUniquePath(std::move(dir), namePrefix))) {
fs::create_directory(path());
}
return !hasPCREPatternMatch(pattern, target);
}
-} // namespace detail
+} // namespace detail
CaptureFD::CaptureFD(int fd, ChunkCob chunk_cob)
: chunkCob_(std::move(chunk_cob)), fd_(fd), readOffset_(0) {
return std::string(buf.get(), size);
}
-static std::map<std::string, std::string> getEnvVarMap() {
- std::map<std::string, std::string> data;
- for (auto it = environ; *it != nullptr; ++it) {
- std::string key, value;
- split("=", *it, key, value);
- if (key.empty()) {
- continue;
- }
- CHECK(!data.count(key)) << "already contains: " << key;
- data.emplace(move(key), move(value));
- }
- return data;
-}
-
-EnvVarSaver::EnvVarSaver() {
- saved_ = getEnvVarMap();
-}
-
-EnvVarSaver::~EnvVarSaver() {
- for (const auto& kvp : getEnvVarMap()) {
- if (saved_.count(kvp.first)) {
- continue;
- }
- PCHECK(0 == unsetenv(kvp.first.c_str()));
- }
- for (const auto& kvp : saved_) {
- PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
- }
-}
-
-} // namespace test
-} // namespace folly
+} // namespace test
+} // namespace folly