ownsFd_ = true;
}
+File::File(const std::string& name, int flags, mode_t mode)
+ : File(name.c_str(), flags, mode) {}
+
+File::File(StringPiece name, int flags, mode_t mode)
+ : File(name.str(), flags, mode) {}
+
File::File(File&& other) noexcept
: fd_(other.fd_)
, ownsFd_(other.ownsFd_) {
#ifndef FOLLY_FILE_H_
#define FOLLY_FILE_H_
+#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
-#include <fcntl.h>
#include <unistd.h>
+#include <string>
+
#include <folly/Portability.h>
+#include <folly/Range.h>
namespace folly {
* Open and create a file object. Throws on error.
*/
explicit File(const char* name, int flags = O_RDONLY, mode_t mode = 0666);
+ explicit File(
+ const std::string& name, int flags = O_RDONLY, mode_t mode = 0666);
+ explicit File(StringPiece name, int flags = O_RDONLY, mode_t mode = 0666);
~File();
}
}
+TEST(File, SimpleStringPiece) {
+ char buf = 'x';
+ File f(StringPiece("/etc/hosts"));
+ EXPECT_NE(-1, f.fd());
+ EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
+ f.close();
+ EXPECT_EQ(-1, f.fd());
+}
+
TEST(File, OwnsFd) {
// Wrap a file descriptor, make sure that ownsFd works
// We'll test that the file descriptor is closed by closing the writing