cyb70289 commented on code in PR #13354:
URL: https://github.com/apache/arrow/pull/13354#discussion_r894088698


##########
cpp/src/arrow/util/io_util.h:
##########
@@ -124,14 +125,46 @@ Result<bool> DeleteFile(const PlatformFilename& 
file_path, bool allow_not_found
 ARROW_EXPORT
 Result<bool> FileExists(const PlatformFilename& path);
 
+// TODO expose this more publicly to make it available from io/file.h?
+/// A RAII wrapper for a file descriptor.

Review Comment:
   nit nit: `An RAII`?



##########
cpp/src/arrow/util/io_util_test.cc:
##########
@@ -159,6 +170,282 @@ TEST(WinErrorFromStatus, Basics) {
 }
 #endif
 
+class TestFileDescriptor : public ::testing::Test {
+ public:
+  Result<int> NewFileDescriptor() {
+    // Make a new fd by dup'ing C stdout (why not?)
+    int new_fd = dup(1);
+    if (new_fd < 0) {
+      return IOErrorFromErrno(errno, "Failed to dup() C stdout");
+    }
+    return new_fd;
+  }
+
+  void AssertValidFileDescriptor(int fd) {
+    ASSERT_FALSE(FileIsClosed(fd)) << "Not a valid file descriptor: " << fd;
+  }
+
+  void AssertInvalidFileDescriptor(int fd) {
+    ASSERT_TRUE(FileIsClosed(fd)) << "Unexpectedly valid file descriptor: " << 
fd;
+  }
+};
+
+TEST_F(TestFileDescriptor, Basics) {
+  int new_fd, new_fd2;
+
+  // Default initialization
+  FileDescriptor a;
+  ASSERT_EQ(a.fd(), -1);
+  ASSERT_TRUE(a.closed());
+  ASSERT_OK(a.Close());
+  ASSERT_OK(a.Close());
+
+  // Assignment
+  ASSERT_OK_AND_ASSIGN(new_fd, NewFileDescriptor());
+  AssertValidFileDescriptor(new_fd);
+  a = FileDescriptor(new_fd);
+  ASSERT_FALSE(a.closed());
+  ASSERT_GT(a.fd(), 2);

Review Comment:
   Will it fail if `stdin` or `stderr` is closed? Though I don't think it will 
happen.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to