Commit e20eed09 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Sara Golemon

File ctor should take StringPiece.

Summary: [Folly] File ctor should take StringPiece.

This way we can use it with `std::string` and `folly::fbstring` a touch more easily.

Reviewed By: @luciang

Differential Revision: D2235870
parent 48749665
......@@ -53,6 +53,12 @@ File::File(const char* name, int flags, mode_t mode)
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_) {
......
......@@ -17,12 +17,15 @@
#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 {
......@@ -46,6 +49,9 @@ class File {
* 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();
......
......@@ -55,6 +55,15 @@ TEST(File, Simple) {
}
}
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment