adb: win32: Unicode path names, env vars, some console support
Initial support for Unicode file/dir names. Unicode paths can be passed
on the command line, directory enumeration can enumerate Unicode paths,
Unicode paths are used for file access, and Unicode paths can be output
on the console correctly.
Also Unicode environment variable access.
Initial support for Unicode output from adb shell (which uses
adb_fwrite()). This is partial because the corner case of an
adb_fwrite() call with an incomplete UTF-8 multi-byte sequence does not
output correctly, but this should be uncommon, is better than what we
had before (*always* incorrect UTF-8 multi-byte sequences) and can be
fixed in the future.
Calls to Windows APIs with char strings were changed to pass wchar_t
strings to the FooW() variants.
For more details, see the giant comment in sysdeps_win32.cpp.
https://code.google.com/p/android/issues/detail?id=8185
Change-Id: I7ebf6713bb635638b986ccee97b354428837c9c5
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index 2b174cd..6b48621 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -82,21 +82,22 @@
static std::string GetLogFilePath() {
const char log_name[] = "adb.log";
- char temp_path[MAX_PATH - sizeof(log_name) + 1];
+ WCHAR temp_path[MAX_PATH];
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364992%28v=vs.85%29.aspx
- DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
- CHECK_LE(nchars, sizeof(temp_path));
- if (nchars == 0) {
- // TODO(danalbert): Log the error message from FormatError().
- // Windows unfortunately has two errnos, errno and GetLastError(), so
- // I'm not sure what to do about PLOG here. Probably better to just
- // ignore it and add a simplified version of FormatError() for use in
- // log messages.
+ DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
+ if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
+ // If string truncation or some other error.
+ // TODO(danalbert): Log the error message from
+ // FormatMessage(GetLastError()). Pure Windows APIs only touch
+ // GetLastError(), C Runtime APIs touch errno, so maybe there should be
+ // WPLOG or PLOGW (which would read GetLastError() instead of errno),
+ // in addition to PLOG, or maybe better to just ignore it and add a
+ // simplified version of FormatMessage() for use in log messages.
LOG(ERROR) << "Error creating log file";
}
- return std::string(temp_path) + log_name;
+ return narrow(temp_path) + log_name;
}
#else
static const char kNullFileName[] = "/dev/null";
@@ -189,9 +190,35 @@
return 0;
}
+#ifdef _WIN32
+static bool _argv_is_utf8 = false;
+#endif
+
int main(int argc, char** argv) {
+#ifdef _WIN32
+ if (!_argv_is_utf8) {
+ fatal("_argv_is_utf8 is not set, suggesting that wmain was not "
+ "called. Did you forget to link with -municode?");
+ }
+#endif
+
adb_sysdeps_init();
adb_trace_init(argv);
D("Handling commandline()\n");
return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
}
+
+#ifdef _WIN32
+
+extern "C"
+int wmain(int argc, wchar_t **argv) {
+ // Set diagnostic flag to try to detect if the build system was not
+ // configured to call wmain.
+ _argv_is_utf8 = true;
+
+ // Convert args from UTF-16 to UTF-8 and pass that to main().
+ NarrowArgs narrow_args(argc, argv);
+ return main(argc, narrow_args.data());
+}
+
+#endif