summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2018-09-05 18:16:46 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2018-09-05 18:16:46 +0000
commitf33d19ff211317017bf7be936b32dafebc286ef9 (patch)
tree82616f46bd85310f2663d39ad5300965fcb93608 /base
parent54ecb7f1b3bd3aa3adaedab80344be5214f97c8c (diff)
parenta3c868879c7cd7f52b206246d9839b60cc3dbeb9 (diff)
downloadsystem_core-f33d19ff211317017bf7be936b32dafebc286ef9.tar.gz
system_core-f33d19ff211317017bf7be936b32dafebc286ef9.tar.bz2
system_core-f33d19ff211317017bf7be936b32dafebc286ef9.zip
Merge "libbase: add Fdopen that takes a unique_fd."
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 c6936f137..71025adc2 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -22,6 +22,7 @@
#include <sys/socket.h>
#endif
+#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
@@ -199,6 +200,17 @@ inline bool Socketpair(int type, unique_fd_impl<Closer>* left, unique_fd_impl<Cl
return Socketpair(AF_UNIX, type, 0, left, right);
}
+// Using fdopen with unique_fd correctly is more annoying than it should be,
+// because fdopen doesn't close the file descriptor received upon failure.
+inline FILE* Fdopen(unique_fd&& ufd, const char* mode) {
+ int fd = ufd.release();
+ FILE* file = fdopen(fd, mode);
+ if (!file) {
+ close(fd);
+ }
+ return file;
+}
+
#endif // !defined(_WIN32)
} // namespace base