summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorMathieu Chartier <mathieuc@google.com>2018-11-02 08:05:31 -0700
committerMathieu Chartier <mathieuc@google.com>2018-11-02 08:24:38 -0700
commit1666bafa7074a6fa3a745229931a8292da1f41bd (patch)
tree66574b7a4bd1a39724d773d86a9820a2a7c5c6a2 /base
parent6d7c8fcc92c4914dc01de4f82ac3b39abebff264 (diff)
downloadsystem_core-1666bafa7074a6fa3a745229931a8292da1f41bd.tar.gz
system_core-1666bafa7074a6fa3a745229931a8292da1f41bd.tar.bz2
system_core-1666bafa7074a6fa3a745229931a8292da1f41bd.zip
libbase: add Fdopendir that takes a unique_fd.
Using fdopendir with unique_fd correctly is more annoying than it should be, because fdopendir doesn't close the file descriptor received upon failure. Add an android::base::Fdopendir that does that handles the failure case. Bug: 118818285 Test: treehugger Change-Id: I5dbbe0eb82242bc7716f84735ffc7644febcfd65
Diffstat (limited to 'base')
-rw-r--r--base/include/android-base/unique_fd.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index cd2dc0416..4e3879b99 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -19,6 +19,7 @@
#include <fcntl.h>
#if !defined(_WIN32)
+#include <dirent.h>
#include <sys/socket.h>
#endif
@@ -211,6 +212,17 @@ inline FILE* Fdopen(unique_fd&& ufd, const char* mode) {
return file;
}
+// Using fdopendir with unique_fd correctly is more annoying than it should be,
+// because fdopen doesn't close the file descriptor received upon failure.
+inline DIR* Fdopendir(unique_fd&& ufd) {
+ int fd = ufd.release();
+ DIR* dir = fdopendir(fd);
+ if (dir == nullptr) {
+ close(fd);
+ }
+ return dir;
+}
+
#endif // !defined(_WIN32)
} // namespace base