File::get -> File::descriptor.

This commit is contained in:
Victor Zverovich 2014-05-03 15:07:28 -07:00
parent c6d83b1a3b
commit ee793b35cf
3 changed files with 30 additions and 32 deletions

View File

@ -148,7 +148,7 @@ TEST(ErrorCodeTest, Ctor) {
TEST(FileTest, DefaultCtor) { TEST(FileTest, DefaultCtor) {
File f; File f;
EXPECT_EQ(-1, f.get()); EXPECT_EQ(-1, f.descriptor());
} }
// Checks if the file is open by reading one character from it. // Checks if the file is open by reading one character from it.
@ -172,7 +172,7 @@ bool IsClosedInternal(int fd) {
TEST(FileTest, OpenFileInCtor) { TEST(FileTest, OpenFileInCtor) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
ASSERT_TRUE(IsOpen(f.get())); ASSERT_TRUE(IsOpen(f.descriptor()));
} }
TEST(FileTest, OpenFileError) { TEST(FileTest, OpenFileError) {
@ -182,54 +182,54 @@ TEST(FileTest, OpenFileError) {
TEST(FileTest, MoveCtor) { TEST(FileTest, MoveCtor) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
int fd = f.get(); int fd = f.descriptor();
EXPECT_NE(-1, fd); EXPECT_NE(-1, fd);
File f2(std::move(f)); File f2(std::move(f));
EXPECT_EQ(fd, f2.get()); EXPECT_EQ(fd, f2.descriptor());
EXPECT_EQ(-1, f.get()); EXPECT_EQ(-1, f.descriptor());
} }
TEST(FileTest, MoveAssignment) { TEST(FileTest, MoveAssignment) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
int fd = f.get(); int fd = f.descriptor();
EXPECT_NE(-1, fd); EXPECT_NE(-1, fd);
File f2; File f2;
f2 = std::move(f); f2 = std::move(f);
EXPECT_EQ(fd, f2.get()); EXPECT_EQ(fd, f2.descriptor());
EXPECT_EQ(-1, f.get()); EXPECT_EQ(-1, f.descriptor());
} }
TEST(FileTest, MoveAssignmentClosesFile) { TEST(FileTest, MoveAssignmentClosesFile) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
File f2("CMakeLists.txt", File::RDONLY); File f2("CMakeLists.txt", File::RDONLY);
int old_fd = f2.get(); int old_fd = f2.descriptor();
f2 = std::move(f); f2 = std::move(f);
EXPECT_CLOSED(old_fd); EXPECT_CLOSED(old_fd);
} }
File OpenFile(int &fd) { File OpenFile(int &fd) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
fd = f.get(); fd = f.descriptor();
return std::move(f); return std::move(f);
} }
TEST(FileTest, MoveFromTemporaryInCtor) { TEST(FileTest, MoveFromTemporaryInCtor) {
int fd = 0xdeadbeef; int fd = 0xdeadbeef;
File f(OpenFile(fd)); File f(OpenFile(fd));
EXPECT_EQ(fd, f.get()); EXPECT_EQ(fd, f.descriptor());
} }
TEST(FileTest, MoveFromTemporaryInAssignment) { TEST(FileTest, MoveFromTemporaryInAssignment) {
int fd = 0xdeadbeef; int fd = 0xdeadbeef;
File f; File f;
f = OpenFile(fd); f = OpenFile(fd);
EXPECT_EQ(fd, f.get()); EXPECT_EQ(fd, f.descriptor());
} }
TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) { TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
int fd = 0xdeadbeef; int fd = 0xdeadbeef;
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
int old_fd = f.get(); int old_fd = f.descriptor();
f = OpenFile(fd); f = OpenFile(fd);
EXPECT_CLOSED(old_fd); EXPECT_CLOSED(old_fd);
} }
@ -238,7 +238,7 @@ TEST(FileTest, CloseFileInDtor) {
int fd = 0; int fd = 0;
{ {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
fd = f.get(); fd = f.descriptor();
} }
EXPECT_CLOSED(fd); EXPECT_CLOSED(fd);
} }
@ -248,15 +248,15 @@ TEST(FileTest, DtorCloseError) {
// The close function must be called inside EXPECT_STDERR, otherwise // The close function must be called inside EXPECT_STDERR, otherwise
// the system may allocate freed file descriptor when redirecting the // the system may allocate freed file descriptor when redirecting the
// output in EXPECT_STDERR. // output in EXPECT_STDERR.
EXPECT_STDERR(close(f->get()); delete f, EXPECT_STDERR(close(f->descriptor()); delete f,
FormatSystemErrorMessage(EBADF, "cannot close file") + "\n"); FormatSystemErrorMessage(EBADF, "cannot close file") + "\n");
} }
TEST(FileTest, Close) { TEST(FileTest, Close) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
int fd = f.get(); int fd = f.descriptor();
f.close(); f.close();
EXPECT_EQ(-1, f.get()); EXPECT_EQ(-1, f.descriptor());
EXPECT_CLOSED(fd); EXPECT_CLOSED(fd);
} }
@ -265,7 +265,7 @@ TEST(FileTest, CloseError) {
fmt::SystemError error("", 0); fmt::SystemError error("", 0);
std::string message = FormatSystemErrorMessage(EBADF, "cannot close file"); std::string message = FormatSystemErrorMessage(EBADF, "cannot close file");
EXPECT_STDERR( EXPECT_STDERR(
close(f->get()); close(f->descriptor());
try { f->close(); } catch (const fmt::SystemError &e) { error = e; } try { f->close(); } catch (const fmt::SystemError &e) { error = e; }
delete f, delete f,
message + "\n"); message + "\n");
@ -319,8 +319,8 @@ TEST(FileTest, WriteError) {
TEST(FileTest, Dup) { TEST(FileTest, Dup) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
File dup = File::dup(f.get()); File dup = File::dup(f.descriptor());
EXPECT_NE(f.get(), dup.get()); EXPECT_NE(f.descriptor(), dup.descriptor());
const char EXPECTED[] = "language: cpp"; const char EXPECTED[] = "language: cpp";
EXPECT_EQ(EXPECTED, Read(dup, sizeof(EXPECTED) - 1)); EXPECT_EQ(EXPECTED, Read(dup, sizeof(EXPECTED) - 1));
} }
@ -333,24 +333,24 @@ TEST(FileTest, DupError) {
TEST(FileTest, Dup2) { TEST(FileTest, Dup2) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
File dup("CMakeLists.txt", File::RDONLY); File dup("CMakeLists.txt", File::RDONLY);
f.dup2(dup.get()); f.dup2(dup.descriptor());
EXPECT_NE(f.get(), dup.get()); EXPECT_NE(f.descriptor(), dup.descriptor());
EXPECT_READ(dup, "language: cpp"); EXPECT_READ(dup, "language: cpp");
} }
TEST(FileTest, Dup2Error) { TEST(FileTest, Dup2Error) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
EXPECT_SYSTEM_ERROR(f.dup2(-1), EBADF, EXPECT_SYSTEM_ERROR(f.dup2(-1), EBADF,
fmt::Format("cannot duplicate file descriptor {} to -1") << f.get()); fmt::Format("cannot duplicate file descriptor {} to -1") << f.descriptor());
} }
TEST(FileTest, Dup2NoExcept) { TEST(FileTest, Dup2NoExcept) {
File f(".travis.yml", File::RDONLY); File f(".travis.yml", File::RDONLY);
File dup("CMakeLists.txt", File::RDONLY); File dup("CMakeLists.txt", File::RDONLY);
ErrorCode ec; ErrorCode ec;
f.dup2(dup.get(), ec); f.dup2(dup.descriptor(), ec);
EXPECT_EQ(0, ec.get()); EXPECT_EQ(0, ec.get());
EXPECT_NE(f.get(), dup.get()); EXPECT_NE(f.descriptor(), dup.descriptor());
EXPECT_READ(dup, "language: cpp"); EXPECT_READ(dup, "language: cpp");
} }
@ -364,15 +364,14 @@ TEST(FileTest, Dup2NoExceptError) {
TEST(FileTest, Pipe) { TEST(FileTest, Pipe) {
File read_end, write_end; File read_end, write_end;
File::pipe(read_end, write_end); File::pipe(read_end, write_end);
EXPECT_NE(-1, read_end.get()); EXPECT_NE(-1, read_end.descriptor());
EXPECT_NE(-1, write_end.get()); EXPECT_NE(-1, write_end.descriptor());
// TODO: try writing to write_end and reading from read_end // TODO: try writing to write_end and reading from read_end
} }
// TODO: test pipe // TODO: test pipe
// TODO: compile both with C++11 & C++98 mode // TODO: compile both with C++11 & C++98 mode
#endif #endif
// TODO: test OutputRedirector // TODO: test OutputRedirector

View File

@ -63,7 +63,7 @@ File::File(const char *path, int oflag) {
fmt::ThrowSystemError(errno, "cannot open file {}") << path; fmt::ThrowSystemError(errno, "cannot open file {}") << path;
} }
File::~File() { File::~File() FMT_NOEXCEPT(true) {
// Don't need to retry close in case of EINTR. // Don't need to retry close in case of EINTR.
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
if (fd_ != -1 && ::FMT_POSIX(close(fd_)) != 0) if (fd_ != -1 && ::FMT_POSIX(close(fd_)) != 0)

View File

@ -185,11 +185,10 @@ class File {
#endif #endif
// Destroys the object closing the file it represents if any. // Destroys the object closing the file it represents if any.
~File(); ~File() FMT_NOEXCEPT(true);
// Returns the file descriptor. // Returns the file descriptor.
// TODO: rename to descriptor int descriptor() const FMT_NOEXCEPT(true) { return fd_; }
int get() const FMT_NOEXCEPT(true) { return fd_; }
// Closes the file if its descriptor is not -1. // Closes the file if its descriptor is not -1.
void close(); void close();