Merge "Use more std::string in fastboot."

am: 17f3b1bad7

* commit '17f3b1bad76925c327a6572f6058b32651499d3c':
  Use more std::string in fastboot.

Change-Id: I154a39288554dc18df0848a2fe724e087501644b
This commit is contained in:
Elliott Hughes 2016-04-29 17:55:05 +00:00 committed by android-build-merger
commit 05ec41963d
7 changed files with 73 additions and 93 deletions

View file

@ -351,21 +351,21 @@ int fb_execute_queue(Transport* transport)
} }
if (a->op == OP_DOWNLOAD) { if (a->op == OP_DOWNLOAD) {
status = fb_download_data(transport, a->data, a->size); status = fb_download_data(transport, a->data, a->size);
status = a->func(a, status, status ? fb_get_error() : ""); status = a->func(a, status, status ? fb_get_error().c_str() : "");
if (status) break; if (status) break;
} else if (a->op == OP_COMMAND) { } else if (a->op == OP_COMMAND) {
status = fb_command(transport, a->cmd); status = fb_command(transport, a->cmd);
status = a->func(a, status, status ? fb_get_error() : ""); status = a->func(a, status, status ? fb_get_error().c_str() : "");
if (status) break; if (status) break;
} else if (a->op == OP_QUERY) { } else if (a->op == OP_QUERY) {
status = fb_command_response(transport, a->cmd, resp); status = fb_command_response(transport, a->cmd, resp);
status = a->func(a, status, status ? fb_get_error() : resp); status = a->func(a, status, status ? fb_get_error().c_str() : resp);
if (status) break; if (status) break;
} else if (a->op == OP_NOTICE) { } else if (a->op == OP_NOTICE) {
fprintf(stderr,"%s\n",(char*)a->data); fprintf(stderr,"%s\n",(char*)a->data);
} else if (a->op == OP_DOWNLOAD_SPARSE) { } else if (a->op == OP_DOWNLOAD_SPARSE) {
status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data)); status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data));
status = a->func(a, status, status ? fb_get_error() : ""); status = a->func(a, status, status ? fb_get_error().c_str() : "");
if (status) break; if (status) break;
} else if (a->op == OP_WAIT_FOR_DISCONNECT) { } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
transport->WaitForDisconnect(); transport->WaitForDisconnect();

View file

@ -49,6 +49,7 @@
#include <android-base/parseint.h> #include <android-base/parseint.h>
#include <android-base/parsenetaddress.h> #include <android-base/parsenetaddress.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h> #include <android-base/strings.h>
#include <sparse/sparse.h> #include <sparse/sparse.h>
#include <ziparchive/zip_archive.h> #include <ziparchive/zip_archive.h>
@ -108,12 +109,9 @@ static struct {
{"vendor.img", "vendor.sig", "vendor", true}, {"vendor.img", "vendor.sig", "vendor", true},
}; };
static char* find_item(const char* item, const char* product) { static std::string find_item(const char* item, const char* product) {
char *dir;
const char *fn; const char *fn;
char path[PATH_MAX + 128]; if (!strcmp(item,"boot")) {
if(!strcmp(item,"boot")) {
fn = "boot.img"; fn = "boot.img";
} else if(!strcmp(item,"recovery")) { } else if(!strcmp(item,"recovery")) {
fn = "recovery.img"; fn = "recovery.img";
@ -129,24 +127,22 @@ static char* find_item(const char* item, const char* product) {
fn = "android-info.txt"; fn = "android-info.txt";
} else { } else {
fprintf(stderr,"unknown partition '%s'\n", item); fprintf(stderr,"unknown partition '%s'\n", item);
return 0; return "";
} }
if(product) { if (product) {
get_my_path(path); std::string path = get_my_path();
sprintf(path + strlen(path), path.erase(path.find_last_of('/'));
"../../../target/product/%s/%s", product, fn); return android::base::StringPrintf("%s/../../../target/product/%s/%s",
return strdup(path); path.c_str(), product, fn);
} }
dir = getenv("ANDROID_PRODUCT_OUT"); char* dir = getenv("ANDROID_PRODUCT_OUT");
if((dir == 0) || (dir[0] == 0)) { if (dir == nullptr || dir[0] == '\0') {
die("neither -p product specified nor ANDROID_PRODUCT_OUT set"); die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
return 0;
} }
sprintf(path, "%s/%s", dir, fn); return android::base::StringPrintf("%s/%s", dir, fn);
return strdup(path);
} }
static int64_t get_file_size(int fd) { static int64_t get_file_size(int fd) {
@ -179,8 +175,8 @@ oops:
return 0; return 0;
} }
static void* load_file(const char* fn, int64_t* sz) { static void* load_file(const std::string& path, int64_t* sz) {
int fd = open(fn, O_RDONLY | O_BINARY); int fd = open(path.c_str(), O_RDONLY | O_BINARY);
if (fd == -1) return nullptr; if (fd == -1) return nullptr;
return load_fd(fd, sz); return load_fd(fd, sz);
} }
@ -964,18 +960,19 @@ static void do_update(Transport* transport, const char* filename, const char* sl
CloseArchive(zip); CloseArchive(zip);
} }
static void do_send_signature(char* fn) { static void do_send_signature(const char* filename) {
char* xtn = strrchr(fn, '.'); if (android::base::EndsWith(filename, ".img") == false) {
if (!xtn) return; return;
}
if (strcmp(xtn, ".img")) return; std::string sig_path = filename;
sig_path.erase(sig_path.size() - 4);
strcpy(xtn, ".sig"); sig_path += ".sig";
int64_t sz; int64_t sz;
void* data = load_file(fn, &sz); void* data = load_file(sig_path, &sz);
strcpy(xtn, ".img");
if (data == nullptr) return; if (data == nullptr) return;
fb_queue_download("signature", data, sz); fb_queue_download("signature", data, sz);
fb_queue_command("signature", "installing signature"); fb_queue_command("signature", "installing signature");
} }
@ -985,8 +982,8 @@ static void do_flashall(Transport* transport, const char* slot_override, int era
fb_queue_query_save("product", cur_product, sizeof(cur_product)); fb_queue_query_save("product", cur_product, sizeof(cur_product));
char* fname = find_item("info", product); std::string fname = find_item("info", product);
if (fname == nullptr) die("cannot find android-info.txt"); if (fname.empty()) die("cannot find android-info.txt");
int64_t sz; int64_t sz;
void* data = load_file(fname, &sz); void* data = load_file(fname, &sz);
@ -997,14 +994,14 @@ static void do_flashall(Transport* transport, const char* slot_override, int era
for (size_t i = 0; i < ARRAY_SIZE(images); i++) { for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
fname = find_item(images[i].part_name, product); fname = find_item(images[i].part_name, product);
fastboot_buffer buf; fastboot_buffer buf;
if (load_buf(transport, fname, &buf)) { if (load_buf(transport, fname.c_str(), &buf)) {
if (images[i].is_optional) if (images[i].is_optional)
continue; continue;
die("could not load %s\n", images[i].img_name); die("could not load %s\n", images[i].img_name);
} }
auto flashall = [&](const std::string &partition) { auto flashall = [&](const std::string &partition) {
do_send_signature(fname); do_send_signature(fname.c_str());
if (erase_first && needs_erase(transport, partition.c_str())) { if (erase_first && needs_erase(transport, partition.c_str())) {
fb_queue_erase(partition.c_str()); fb_queue_erase(partition.c_str());
} }
@ -1180,7 +1177,7 @@ failed:
fprintf(stderr, "Erase successful, but not automatically formatting.\n"); fprintf(stderr, "Erase successful, but not automatically formatting.\n");
if (errMsg) fprintf(stderr, "%s", errMsg); if (errMsg) fprintf(stderr, "%s", errMsg);
} }
fprintf(stderr,"FAILED (%s)\n", fb_get_error()); fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -1441,8 +1438,8 @@ int main(int argc, char **argv)
fb_queue_download("boot.img", data, sz); fb_queue_download("boot.img", data, sz);
fb_queue_command("boot", "booting"); fb_queue_command("boot", "booting");
} else if(!strcmp(*argv, "flash")) { } else if(!strcmp(*argv, "flash")) {
char *pname = argv[1]; char* pname = argv[1];
char *fname = 0; std::string fname;
require(2); require(2);
if (argc > 2) { if (argc > 2) {
fname = argv[2]; fname = argv[2];
@ -1451,13 +1448,13 @@ int main(int argc, char **argv)
fname = find_item(pname, product); fname = find_item(pname, product);
skip(2); skip(2);
} }
if (fname == 0) die("cannot determine image filename for '%s'", pname); if (fname.empty()) die("cannot determine image filename for '%s'", pname);
auto flash = [&](const std::string &partition) { auto flash = [&](const std::string &partition) {
if (erase_first && needs_erase(transport, partition.c_str())) { if (erase_first && needs_erase(transport, partition.c_str())) {
fb_queue_erase(partition.c_str()); fb_queue_erase(partition.c_str());
} }
do_flash(transport, partition.c_str(), fname); do_flash(transport, partition.c_str(), fname.c_str());
}; };
do_for_partitions(transport, pname, slot_override.c_str(), flash, true); do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
} else if(!strcmp(*argv, "flash:raw")) { } else if(!strcmp(*argv, "flash:raw")) {

View file

@ -43,7 +43,7 @@ int fb_command(Transport* transport, const char* cmd);
int fb_command_response(Transport* transport, const char* cmd, char* response); int fb_command_response(Transport* transport, const char* cmd, char* response);
int fb_download_data(Transport* transport, const void* data, uint32_t size); int fb_download_data(Transport* transport, const void* data, uint32_t size);
int fb_download_data_sparse(Transport* transport, struct sparse_file* s); int fb_download_data_sparse(Transport* transport, struct sparse_file* s);
char *fb_get_error(void); const std::string fb_get_error();
#define FB_COMMAND_SZ 64 #define FB_COMMAND_SZ 64
#define FB_RESPONSE_SZ 64 #define FB_RESPONSE_SZ 64
@ -72,7 +72,7 @@ double now();
char *mkmsg(const char *fmt, ...); char *mkmsg(const char *fmt, ...);
__attribute__((__noreturn__)) void die(const char *fmt, ...); __attribute__((__noreturn__)) void die(const char *fmt, ...);
void get_my_path(char *path); std::string get_my_path();
/* Current product */ /* Current product */
extern char cur_product[FB_RESPONSE_SZ + 1]; extern char cur_product[FB_RESPONSE_SZ + 1];

View file

@ -36,16 +36,16 @@
#include <algorithm> #include <algorithm>
#include <android-base/stringprintf.h>
#include <sparse/sparse.h> #include <sparse/sparse.h>
#include "fastboot.h" #include "fastboot.h"
#include "transport.h" #include "transport.h"
static char ERROR[128]; static std::string g_error;
char *fb_get_error(void) const std::string fb_get_error() {
{ return g_error;
return ERROR;
} }
static int check_response(Transport* transport, uint32_t size, char* response) { static int check_response(Transport* transport, uint32_t size, char* response) {
@ -54,14 +54,14 @@ static int check_response(Transport* transport, uint32_t size, char* response) {
while (true) { while (true) {
int r = transport->Read(status, 64); int r = transport->Read(status, 64);
if (r < 0) { if (r < 0) {
snprintf(ERROR, sizeof(ERROR), "status read failed (%s)", strerror(errno)); g_error = android::base::StringPrintf("status read failed (%s)", strerror(errno));
transport->Close(); transport->Close();
return -1; return -1;
} }
status[r] = 0; status[r] = 0;
if (r < 4) { if (r < 4) {
snprintf(ERROR, sizeof(ERROR), "status malformed (%d bytes)", r); g_error = android::base::StringPrintf("status malformed (%d bytes)", r);
transport->Close(); transport->Close();
return -1; return -1;
} }
@ -80,9 +80,9 @@ static int check_response(Transport* transport, uint32_t size, char* response) {
if (!memcmp(status, "FAIL", 4)) { if (!memcmp(status, "FAIL", 4)) {
if (r > 4) { if (r > 4) {
snprintf(ERROR, sizeof(ERROR), "remote: %s", status + 4); g_error = android::base::StringPrintf("remote: %s", status + 4);
} else { } else {
strcpy(ERROR, "remote failure"); g_error = "remote failure";
} }
return -1; return -1;
} }
@ -90,14 +90,14 @@ static int check_response(Transport* transport, uint32_t size, char* response) {
if (!memcmp(status, "DATA", 4) && size > 0){ if (!memcmp(status, "DATA", 4) && size > 0){
uint32_t dsize = strtol(status + 4, 0, 16); uint32_t dsize = strtol(status + 4, 0, 16);
if (dsize > size) { if (dsize > size) {
strcpy(ERROR, "data size too large"); g_error = android::base::StringPrintf("data size too large (%d)", dsize);
transport->Close(); transport->Close();
return -1; return -1;
} }
return dsize; return dsize;
} }
strcpy(ERROR,"unknown status code"); g_error = "unknown status code";
transport->Close(); transport->Close();
break; break;
} }
@ -108,7 +108,7 @@ static int check_response(Transport* transport, uint32_t size, char* response) {
static int _command_start(Transport* transport, const char* cmd, uint32_t size, char* response) { static int _command_start(Transport* transport, const char* cmd, uint32_t size, char* response) {
size_t cmdsize = strlen(cmd); size_t cmdsize = strlen(cmd);
if (cmdsize > 64) { if (cmdsize > 64) {
snprintf(ERROR, sizeof(ERROR), "command too large"); g_error = android::base::StringPrintf("command too large (%zu)", cmdsize);
return -1; return -1;
} }
@ -117,7 +117,7 @@ static int _command_start(Transport* transport, const char* cmd, uint32_t size,
} }
if (transport->Write(cmd, cmdsize) != static_cast<int>(cmdsize)) { if (transport->Write(cmd, cmdsize) != static_cast<int>(cmdsize)) {
snprintf(ERROR, sizeof(ERROR), "command write failed (%s)", strerror(errno)); g_error = android::base::StringPrintf("command write failed (%s)", strerror(errno));
transport->Close(); transport->Close();
return -1; return -1;
} }
@ -128,12 +128,12 @@ static int _command_start(Transport* transport, const char* cmd, uint32_t size,
static int _command_data(Transport* transport, const void* data, uint32_t size) { static int _command_data(Transport* transport, const void* data, uint32_t size) {
int r = transport->Write(data, size); int r = transport->Write(data, size);
if (r < 0) { if (r < 0) {
snprintf(ERROR, sizeof(ERROR), "data transfer failure (%s)", strerror(errno)); g_error = android::base::StringPrintf("data transfer failure (%s)", strerror(errno));
transport->Close(); transport->Close();
return -1; return -1;
} }
if (r != ((int) size)) { if (r != ((int) size)) {
snprintf(ERROR, sizeof(ERROR), "data transfer failure (short transfer)"); g_error = "data transfer failure (short transfer)";
transport->Close(); transport->Close();
return -1; return -1;
} }
@ -216,7 +216,7 @@ static int fb_download_data_sparse_write(void *priv, const void *data, int len)
if (len > TRANSPORT_BUF_SIZE) { if (len > TRANSPORT_BUF_SIZE) {
if (transport_buf_len > 0) { if (transport_buf_len > 0) {
snprintf(ERROR, sizeof(ERROR), "internal error: transport_buf not empty\n"); g_error = "internal error: transport_buf not empty";
return -1; return -1;
} }
to_write = round_down(len, TRANSPORT_BUF_SIZE); to_write = round_down(len, TRANSPORT_BUF_SIZE);
@ -230,7 +230,7 @@ static int fb_download_data_sparse_write(void *priv, const void *data, int len)
if (len > 0) { if (len > 0) {
if (len > TRANSPORT_BUF_SIZE) { if (len > TRANSPORT_BUF_SIZE) {
snprintf(ERROR, sizeof(ERROR), "internal error: too much left for transport_buf\n"); g_error = "internal error: too much left for transport_buf";
return -1; return -1;
} }
memcpy(transport_buf, ptr, len); memcpy(transport_buf, ptr, len);

View file

@ -28,27 +28,16 @@
#include "fastboot.h" #include "fastboot.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
void get_my_path(char *path) #include <android-base/stringprintf.h>
{
char proc[64];
char *x;
sprintf(proc, "/proc/%d/exe", getpid()); std::string get_my_path() {
int err = readlink(proc, path, PATH_MAX - 1); std::string proc = android::base::StringPrintf("/proc/%d/exe", getpid());
char path[PATH_MAX + 1];
if(err <= 0) { int rc = readlink(proc.c_str(), path, sizeof(path) - 1);
path[0] = 0; if (rc == -1) return "";
} else { path[rc] = '\0';
path[err] = 0; return path;
x = strrchr(path,'/');
if(x) x[1] = 0;
}
} }

View file

@ -31,19 +31,15 @@
#import <Carbon/Carbon.h> #import <Carbon/Carbon.h>
#include <unistd.h> #include <unistd.h>
void get_my_path(char s[PATH_MAX]) std::string get_my_path() {
{
CFBundleRef mainBundle = CFBundleGetMainBundle(); CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle); CFURLRef executableURL = CFBundleCopyExecutableURL(mainBundle);
CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle); CFStringRef executablePathString = CFURLCopyFileSystemPath(executableURL, kCFURLPOSIXPathStyle);
CFRelease(executableURL); CFRelease(executableURL);
CFStringGetFileSystemRepresentation(executablePathString, s, PATH_MAX-1); char path[PATH_MAX + 1];
CFStringGetFileSystemRepresentation(executablePathString, path, sizeof(PATH_MAX)-1);
CFRelease(executablePathString); CFRelease(executablePathString);
char *x; return path;
x = strrchr(s, '/');
if(x) x[1] = 0;
} }

View file

@ -38,14 +38,12 @@
#include <windows.h> #include <windows.h>
void get_my_path(char exe[PATH_MAX]) std::string get_my_path() {
{ char path[PATH_MAX + 1];
char* r;
GetModuleFileName( NULL, exe, PATH_MAX-1 ); DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1);
exe[PATH_MAX-1] = 0; if (result == 0 || result == sizeof(path) - 1) return "";
r = strrchr( exe, '\\' ); path[PATH_MAX - 1] = 0;
if (r)
*r = 0; return path;
} }