Support paths containing spaces on Windows

Quote the arguments of "adb push" to support paths which contain spaces
on Windows.

Fixes <https://github.com/Genymobile/scrcpy/issues/288>.
This commit is contained in:
Romain Vimont 2018-10-04 20:47:53 +02:00
parent ff4430b2a3
commit 8875955921
3 changed files with 57 additions and 10 deletions

View file

@ -6,6 +6,7 @@
#include "common.h" #include "common.h"
#include "log.h" #include "log.h"
#include "str_util.h"
static const char *adb_command; static const char *adb_command;
@ -89,23 +90,49 @@ process_t adb_reverse_remove(const char *serial, const char *device_socket_name)
} }
process_t adb_push(const char *serial, const char *local, const char *remote) { process_t adb_push(const char *serial, const char *local, const char *remote) {
#ifdef __WINDOWS__
// Windows will parse the string, so the paths must be quoted
// (see sys/win/command.c)
local = strquote(local);
if (!local) {
return PROCESS_NONE;
}
remote = strquote(remote);
if (!remote) {
free((void *) local);
return PROCESS_NONE;
}
#endif
const char *const adb_cmd[] = {"push", local, remote}; const char *const adb_cmd[] = {"push", local, remote};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
#ifdef __WINDOWS__
free((void *) remote);
free((void *) local);
#endif
return proc;
} }
process_t adb_install(const char *serial, const char *local) { process_t adb_install(const char *serial, const char *local) {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
// Windows will parse the string, so the local name must be quoted (see sys/win/command.c) // Windows will parse the string, so the local name must be quoted
size_t len = strlen(local); // (see sys/win/command.c)
char quoted[len + 3]; local = strquote(local);
memcpy(&quoted[1], local, len); if (!local) {
quoted[0] = '"'; return PROCESS_NONE;
quoted[len + 1] = '"'; }
quoted[len + 2] = '\0';
local = quoted;
#endif #endif
const char *const adb_cmd[] = {"install", "-r", local}; const char *const adb_cmd[] = {"install", "-r", local};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd)); process_t proc = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
#ifdef __WINDOWS__
free((void *) local);
#endif
return proc;
} }
process_t adb_remove_path(const char *serial, const char *path) { process_t adb_remove_path(const char *serial, const char *path) {

View file

@ -1,5 +1,8 @@
#include "str_util.h" #include "str_util.h"
#include <stdlib.h>
#include <string.h>
size_t xstrncpy(char *dest, const char *src, size_t n) { size_t xstrncpy(char *dest, const char *src, size_t n) {
size_t i; size_t i;
for (i = 0; i < n - 1 && src[i] != '\0'; ++i) for (i = 0; i < n - 1 && src[i] != '\0'; ++i)
@ -31,3 +34,16 @@ truncated:
dst[n - 1] = '\0'; dst[n - 1] = '\0';
return n; return n;
} }
char *strquote(const char *src) {
size_t len = strlen(src);
char *quoted = malloc(len + 3);
if (!quoted) {
return NULL;
}
memcpy(&quoted[1], src, len);
quoted[0] = '"';
quoted[len + 1] = '"';
quoted[len + 2] = '\0';
return quoted;
}

View file

@ -16,4 +16,8 @@ size_t xstrncpy(char *dest, const char *src, size_t n);
// occurred, or n if truncated // occurred, or n if truncated
size_t xstrjoin(char *dst, const char *const tokens[], char sep, size_t n); size_t xstrjoin(char *dst, const char *const tokens[], char sep, size_t n);
// quote a string
// returns the new allocated string, to be freed by the caller
char *strquote(const char *src);
#endif #endif