From 0f29cbc7500311264ee8e6f551e7ca10b58d0d1a Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Wed, 12 Dec 2018 16:12:28 -0800 Subject: [PATCH] adb: switch unix_open to string_view. Test: test_adb.py Test: test_device.py Test: $ANDROID_HOST_OUT/nativetest64/adb_test/adb_test Test: adb shell /data/nativetest64/adbd_test/adbd_test Change-Id: Ieecc9b1b7f2111f4da45d4bbd1b7703535fe7d4d --- adb/adb.cpp | 2 +- adb/adb_trace.cpp | 3 +-- adb/client/main.cpp | 2 +- adb/client/usb_linux.cpp | 6 +++--- adb/daemon/remount_service.cpp | 2 +- adb/sysdeps.h | 28 +++++++++++++--------------- adb/sysdeps_win32.cpp | 7 ++++--- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/adb/adb.cpp b/adb/adb.cpp index 9c0eecafc..c78b96b6d 100644 --- a/adb/adb.cpp +++ b/adb/adb.cpp @@ -600,7 +600,7 @@ static void ReportServerStartupFailure(pid_t pid) { fprintf(stderr, "Full server startup log: %s\n", GetLogFilePath().c_str()); fprintf(stderr, "Server had pid: %d\n", pid); - android::base::unique_fd fd(unix_open(GetLogFilePath().c_str(), O_RDONLY)); + android::base::unique_fd fd(unix_open(GetLogFilePath(), O_RDONLY)); if (fd == -1) return; // Let's not show more than 128KiB of log... diff --git a/adb/adb_trace.cpp b/adb/adb_trace.cpp index a024a89bf..2bd6a3ea1 100644 --- a/adb/adb_trace.cpp +++ b/adb/adb_trace.cpp @@ -72,8 +72,7 @@ static std::string get_log_file_name() { } void start_device_log(void) { - int fd = unix_open(get_log_file_name().c_str(), - O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640); + int fd = unix_open(get_log_file_name(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640); if (fd == -1) { return; } diff --git a/adb/client/main.cpp b/adb/client/main.cpp index fb581a6a6..2ee81a994 100644 --- a/adb/client/main.cpp +++ b/adb/client/main.cpp @@ -40,7 +40,7 @@ static void setup_daemon_logging() { const std::string log_file_path(GetLogFilePath()); - int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640); + int fd = unix_open(log_file_path, O_WRONLY | O_CREAT | O_APPEND, 0640); if (fd == -1) { PLOG(FATAL) << "cannot open " << log_file_path; } diff --git a/adb/client/usb_linux.cpp b/adb/client/usb_linux.cpp index f1bf559a9..6b6e3e3f2 100644 --- a/adb/client/usb_linux.cpp +++ b/adb/client/usb_linux.cpp @@ -156,7 +156,7 @@ static void find_usb_device(const std::string& base, continue; } - int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC); + int fd = unix_open(dev_name, O_RDONLY | O_CLOEXEC); if (fd == -1) { continue; } @@ -535,10 +535,10 @@ static void register_device(const char* dev_name, const char* dev_path, unsigned // Initialize mark so we don't get garbage collected after the device scan. usb->mark = true; - usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC); + usb->fd = unix_open(usb->path, O_RDWR | O_CLOEXEC); if (usb->fd == -1) { // Opening RW failed, so see if we have RO access. - usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC); + usb->fd = unix_open(usb->path, O_RDONLY | O_CLOEXEC); if (usb->fd == -1) { D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno)); return; diff --git a/adb/daemon/remount_service.cpp b/adb/daemon/remount_service.cpp index 1a923173d..80b3e06e6 100644 --- a/adb/daemon/remount_service.cpp +++ b/adb/daemon/remount_service.cpp @@ -103,7 +103,7 @@ bool dev_is_overlayfs(const std::string& dev) { bool make_block_device_writable(const std::string& dev) { if (dev_is_overlayfs(dev)) return true; - int fd = unix_open(dev.c_str(), O_RDONLY | O_CLOEXEC); + int fd = unix_open(dev, O_RDONLY | O_CLOEXEC); if (fd == -1) { return false; } diff --git a/adb/sysdeps.h b/adb/sysdeps.h index b8d7e06b0..15247e722 100644 --- a/adb/sysdeps.h +++ b/adb/sysdeps.h @@ -27,6 +27,7 @@ #include #include +#include #include // Include this before open/close/unlink are defined as macros below. @@ -139,7 +140,7 @@ static __inline__ int adb_open_mode(const char* path, int options, int mode) } // See the comments for the !defined(_WIN32) version of unix_open(). -extern int unix_open(const char* path, int options, ...); +extern int unix_open(std::string_view path, int options, ...); #define open ___xxx_unix_open // Checks if |fd| corresponds to a console. @@ -357,20 +358,17 @@ static __inline__ void close_on_exec(int fd) // by unix_read(), unix_write(), unix_close()). Also, the C Runtime has // configurable CR/LF translation which defaults to text mode, but is settable // with _setmode(). -static __inline__ int unix_open(const char* path, int options,...) -{ - if ((options & O_CREAT) == 0) - { - return TEMP_FAILURE_RETRY( open(path, options) ); - } - else - { - int mode; - va_list args; - va_start( args, options ); - mode = va_arg( args, int ); - va_end( args ); - return TEMP_FAILURE_RETRY( open( path, options, mode ) ); +static __inline__ int unix_open(std::string_view path, int options, ...) { + std::string zero_terminated(path.begin(), path.end()); + if ((options & O_CREAT) == 0) { + return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options)); + } else { + int mode; + va_list args; + va_start(args, options); + mode = va_arg(args, int); + va_end(args); + return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode)); } } diff --git a/adb/sysdeps_win32.cpp b/adb/sysdeps_win32.cpp index 8a6541d60..dbc892064 100644 --- a/adb/sysdeps_win32.cpp +++ b/adb/sysdeps_win32.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -2203,15 +2204,15 @@ NarrowArgs::~NarrowArgs() { } } -int unix_open(const char* path, int options, ...) { +int unix_open(std::string_view path, int options, ...) { std::wstring path_wide; - if (!android::base::UTF8ToWide(path, &path_wide)) { + if (!android::base::UTF8ToWide(path.data(), path.size(), &path_wide)) { return -1; } if ((options & O_CREAT) == 0) { return _wopen(path_wide.c_str(), options); } else { - int mode; + int mode; va_list args; va_start(args, options); mode = va_arg(args, int);