am 7a677807: Merge "updater: Fix the line breaks in ui_print commands."

* commit '7a6778078b2525c245869061366eb1f92942542a':
  updater: Fix the line breaks in ui_print commands.
This commit is contained in:
Tao Bao
2015-09-10 17:31:11 +00:00
committed by Android Git Automerger
+33 -27
View File
@@ -34,6 +34,9 @@
#include <linux/xattr.h> #include <linux/xattr.h>
#include <inttypes.h> #include <inttypes.h>
#include <base/strings.h>
#include <base/stringprintf.h>
#include "bootloader.h" #include "bootloader.h"
#include "applypatch/applypatch.h" #include "applypatch/applypatch.h"
#include "cutils/android_reboot.h" #include "cutils/android_reboot.h"
@@ -53,28 +56,35 @@
#include "wipe.h" #include "wipe.h"
#endif #endif
void uiPrint(State* state, char* buffer) { // Send over the buffer to recovery though the command pipe.
char* line = strtok(buffer, "\n"); static void uiPrint(State* state, const std::string& buffer) {
UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
while (line) {
fprintf(ui->cmd_pipe, "ui_print %s\n", line);
line = strtok(NULL, "\n");
}
fprintf(ui->cmd_pipe, "ui_print\n");
// The recovery will only print the contents to screen for pipe command // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
// ui_print. We need to dump the contents to stderr (which has been // So skip sending empty strings to UI.
// redirected to the log file) directly. std::vector<std::string> lines = android::base::Split(buffer, "\n");
fprintf(stderr, "%s", buffer); for (auto& line: lines) {
if (!line.empty()) {
fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
fprintf(ui->cmd_pipe, "ui_print\n");
}
}
// On the updater side, we need to dump the contents to stderr (which has
// been redirected to the log file). Because the recovery will only print
// the contents to screen when processing pipe command ui_print.
fprintf(stderr, "%s", buffer.c_str());
} }
__attribute__((__format__(printf, 2, 3))) __nonnull((2)) __attribute__((__format__(printf, 2, 3))) __nonnull((2))
void uiPrintf(State* state, const char* format, ...) { void uiPrintf(State* state, const char* format, ...) {
char error_msg[1024]; std::string error_msg;
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vsnprintf(error_msg, sizeof(error_msg), format, ap); android::base::StringAppendV(&error_msg, format, ap);
va_end(ap); va_end(ap);
uiPrint(state, error_msg); uiPrint(state, error_msg);
} }
@@ -159,7 +169,7 @@ Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
const MtdPartition* mtd; const MtdPartition* mtd;
mtd = mtd_find_partition_by_name(location); mtd = mtd_find_partition_by_name(location);
if (mtd == NULL) { if (mtd == NULL) {
uiPrintf(state, "%s: no mtd partition named \"%s\"", uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
name, location); name, location);
result = strdup(""); result = strdup("");
goto done; goto done;
@@ -1246,28 +1256,24 @@ Value* ApplyPatchCheckFn(const char* name, State* state,
return StringValue(strdup(result == 0 ? "t" : "")); return StringValue(strdup(result == 0 ? "t" : ""));
} }
// This is the updater side handler for ui_print() in edify script. Contents
// will be sent over to the recovery side for on-screen display.
Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
char** args = ReadVarArgs(state, argc, argv); char** args = ReadVarArgs(state, argc, argv);
if (args == NULL) { if (args == NULL) {
return NULL; return NULL;
} }
int size = 0; std::string buffer;
int i; for (int i = 0; i < argc; ++i) {
for (i = 0; i < argc; ++i) { buffer += args[i];
size += strlen(args[i]);
}
char* buffer = reinterpret_cast<char*>(malloc(size+1));
size = 0;
for (i = 0; i < argc; ++i) {
strcpy(buffer+size, args[i]);
size += strlen(args[i]);
free(args[i]); free(args[i]);
} }
free(args); free(args);
buffer[size] = '\0';
buffer += "\n";
uiPrint(state, buffer); uiPrint(state, buffer);
return StringValue(buffer); return StringValue(strdup(buffer.c_str()));
} }
Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {