adb: make ScopedFd universally accessible and useful.
Change-Id: I707ffbd10958e7449b4c95dff48638480c746939
diff --git a/adb/adb_utils.h b/adb/adb_utils.h
index f1149b3..89fcd66 100644
--- a/adb/adb_utils.h
+++ b/adb/adb_utils.h
@@ -19,6 +19,8 @@
#include <string>
+#include <android-base/macros.h>
+
void close_stdin();
bool getcwd(std::string* cwd);
@@ -39,4 +41,45 @@
bool set_file_block_mode(int fd, bool block);
+extern int adb_close(int fd);
+
+// Helper to automatically close an FD when it goes out of scope.
+class ScopedFd {
+ public:
+ ScopedFd() {
+ }
+
+ ~ScopedFd() {
+ Reset();
+ }
+
+ void Reset(int fd = -1) {
+ if (fd != fd_) {
+ if (valid()) {
+ adb_close(fd_);
+ }
+ fd_ = fd;
+ }
+ }
+
+ int Release() {
+ int temp = fd_;
+ fd_ = -1;
+ return temp;
+ }
+
+ bool valid() const {
+ return fd_ >= 0;
+ }
+
+ int fd() const {
+ return fd_;
+ }
+
+ private:
+ int fd_ = -1;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedFd);
+};
+
#endif