/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2012-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/experimental/TestUtil.h>
-#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <boost/regex.hpp>
+
#include <folly/Exception.h>
#include <folly/File.h>
#include <folly/FileUtil.h>
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,
}
}
-ChangeToTempDir::ChangeToTempDir() : initialPath_(fs::current_path()) {
- std::string p = dir_.path().string();
- ::chdir(p.c_str());
+ChangeToTempDir::ChangeToTempDir() {
+ orig_ = fs::current_path();
+ fs::current_path(path());
}
ChangeToTempDir::~ChangeToTempDir() {
- std::string p = initialPath_.string();
- ::chdir(p.c_str());
+ if (!orig_.empty()) {
+ fs::current_path(orig_);
+ }
}
namespace detail {
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);
}
-} // namespace test
-} // namespace folly
+} // namespace test
+} // namespace folly