summaryrefslogtreecommitdiffstats
path: root/updater
diff options
context:
space:
mode:
Diffstat (limited to 'updater')
-rw-r--r--updater/Android.mk20
-rwxr-xr-x[-rw-r--r--]updater/blockimg.cpp (renamed from updater/blockimg.c)524
-rw-r--r--updater/install.cpp (renamed from updater/install.c)174
-rw-r--r--updater/updater.cpp (renamed from updater/updater.c)2
4 files changed, 311 insertions, 409 deletions
diff --git a/updater/Android.mk b/updater/Android.mk
index a0ea06fa5..82fa7e265 100644
--- a/updater/Android.mk
+++ b/updater/Android.mk
@@ -1,11 +1,23 @@
# Copyright 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
LOCAL_PATH := $(call my-dir)
updater_src_files := \
- install.c \
- blockimg.c \
- updater.c
+ install.cpp \
+ blockimg.cpp \
+ updater.cpp
#
# Build a statically-linked binary to include in OTA packages
@@ -32,7 +44,7 @@ LOCAL_STATIC_LIBRARIES += \
endif
LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UPDATER_LIBS) $(TARGET_RECOVERY_UPDATER_EXTRA_LIBS)
-LOCAL_STATIC_LIBRARIES += libapplypatch libedify libmtdutils libminzip libz
+LOCAL_STATIC_LIBRARIES += libapplypatch libbase libedify libmtdutils libminzip libz
LOCAL_STATIC_LIBRARIES += libmincrypt libbz
LOCAL_STATIC_LIBRARIES += libcutils liblog libc
LOCAL_STATIC_LIBRARIES += libselinux
diff --git a/updater/blockimg.c b/updater/blockimg.cpp
index a6a389507..7da9adf5f 100644..100755
--- a/updater/blockimg.c
+++ b/updater/blockimg.cpp
@@ -19,6 +19,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <linux/fs.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
@@ -31,11 +32,17 @@
#include <time.h>
#include <unistd.h>
+#include <memory>
+#include <string>
+
+#include <base/strings.h>
+
#include "applypatch/applypatch.h"
#include "edify/expr.h"
#include "mincrypt/sha.h"
#include "minzip/Hash.h"
#include "updater.h"
+#include "print_sha1.h"
#define BLOCKSIZE 4096
@@ -44,20 +51,14 @@
// erase to mean fill the region with zeroes.
#define DEBUG_ERASE 0
-#ifndef BLKDISCARD
-#define BLKDISCARD _IO(0x12,119)
-#endif
-
#define STASH_DIRECTORY_BASE "/cache/recovery"
#define STASH_DIRECTORY_MODE 0700
#define STASH_FILE_MODE 0600
-char* PrintSha1(const uint8_t* digest);
-
typedef struct {
- int count;
- int size;
- int pos[0];
+ size_t count;
+ size_t size;
+ size_t pos[0]; // Actual limit is INT_MAX.
} RangeSet;
#define RANGESET_MAX_POINTS \
@@ -66,7 +67,7 @@ typedef struct {
static RangeSet* parse_range(char* text) {
char* save;
char* token;
- int i, num;
+ int num;
long int val;
RangeSet* out = NULL;
size_t bufsize;
@@ -81,18 +82,19 @@ static RangeSet* parse_range(char* text) {
goto err;
}
+ errno = 0;
val = strtol(token, NULL, 0);
- if (val < 2 || val > RANGESET_MAX_POINTS) {
+ if (errno != 0 || val < 2 || val > RANGESET_MAX_POINTS) {
goto err;
} else if (val % 2) {
goto err; // must be even
}
num = (int) val;
- bufsize = sizeof(RangeSet) + num * sizeof(int);
+ bufsize = sizeof(RangeSet) + num * sizeof(size_t);
- out = malloc(bufsize);
+ out = reinterpret_cast<RangeSet*>(malloc(bufsize));
if (!out) {
fprintf(stderr, "failed to allocate range of %zu bytes\n", bufsize);
@@ -102,41 +104,50 @@ static RangeSet* parse_range(char* text) {
out->count = num / 2;
out->size = 0;
- for (i = 0; i < num; ++i) {
+ for (int i = 0; i < num; i += 2) {
token = strtok_r(NULL, ",", &save);
if (!token) {
goto err;
}
+ errno = 0;
val = strtol(token, NULL, 0);
- if (val < 0 || val > INT_MAX) {
+ if (errno != 0 || val < 0 || val > INT_MAX) {
goto err;
}
- out->pos[i] = (int) val;
+ out->pos[i] = static_cast<size_t>(val);
- if (i % 2) {
- if (out->pos[i - 1] >= out->pos[i]) {
- goto err; // empty or negative range
- }
+ token = strtok_r(NULL, ",", &save);
- if (out->size > INT_MAX - out->pos[i]) {
- goto err; // overflow
- }
+ if (!token) {
+ goto err;
+ }
- out->size += out->pos[i];
- } else {
- if (out->size < 0) {
- goto err;
- }
+ errno = 0;
+ val = strtol(token, NULL, 0);
- out->size -= out->pos[i];
+ if (errno != 0 || val < 0 || val > INT_MAX) {
+ goto err;
}
+
+ out->pos[i+1] = static_cast<size_t>(val);
+
+ if (out->pos[i] >= out->pos[i+1]) {
+ goto err; // empty or negative range
+ }
+
+ size_t rs = out->pos[i+1] - out->pos[i];
+ if (out->size > SIZE_MAX - rs) {
+ goto err; // overflow
+ }
+
+ out->size += rs;
}
- if (out->size <= 0) {
+ if (out->size == 0) {
goto err;
}
@@ -147,28 +158,22 @@ err:
exit(1);
}
-static int range_overlaps(RangeSet* r1, RangeSet* r2) {
- int i, j, r1_0, r1_1, r2_0, r2_1;
-
- if (!r1 || !r2) {
- return 0;
- }
-
- for (i = 0; i < r1->count; ++i) {
- r1_0 = r1->pos[i * 2];
- r1_1 = r1->pos[i * 2 + 1];
+static bool range_overlaps(const RangeSet& r1, const RangeSet& r2) {
+ for (size_t i = 0; i < r1.count; ++i) {
+ size_t r1_0 = r1.pos[i * 2];
+ size_t r1_1 = r1.pos[i * 2 + 1];
- for (j = 0; j < r2->count; ++j) {
- r2_0 = r2->pos[j * 2];
- r2_1 = r2->pos[j * 2 + 1];
+ for (size_t j = 0; j < r2.count; ++j) {
+ size_t r2_0 = r2.pos[j * 2];
+ size_t r2_1 = r2.pos[j * 2 + 1];
if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) {
- return 1;
+ return true;
}
}
}
- return 0;
+ return false;
}
static int read_all(int fd, uint8_t* data, size_t size) {
@@ -195,11 +200,6 @@ static int write_all(int fd, const uint8_t* data, size_t size) {
written += w;
}
- if (fsync(fd) == -1) {
- fprintf(stderr, "fsync failed: %s\n", strerror(errno));
- return -1;
- }
-
return 0;
}
@@ -229,7 +229,7 @@ static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) {
typedef struct {
int fd;
RangeSet* tgt;
- int p_block;
+ size_t p_block;
size_t p_remain;
} RangeSinkState;
@@ -350,20 +350,18 @@ static void* unzip_new_data(void* cookie) {
}
static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
- int i;
size_t p = 0;
- size_t size;
if (!src || !buffer) {
return -1;
}
- for (i = 0; i < src->count; ++i) {
+ for (size_t i = 0; i < src->count; ++i) {
if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
return -1;
}
- size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
+ size_t size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE;
if (read_all(fd, buffer + p, size) == -1) {
return -1;
@@ -376,20 +374,18 @@ static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) {
}
static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
- int i;
size_t p = 0;
- size_t size;
if (!tgt || !buffer) {
return -1;
}
- for (i = 0; i < tgt->count; ++i) {
+ for (size_t i = 0; i < tgt->count; ++i) {
if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
return -1;
}
- size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
+ size_t size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE;
if (write_all(fd, buffer + p, size) == -1) {
return -1;
@@ -412,7 +408,7 @@ static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) {
// in *tgt, if tgt is non-NULL.
static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
- uint8_t** buffer, size_t* buffer_alloc, int fd) {
+ uint8_t** buffer, size_t* buffer_alloc, int fd) {
char* word;
int rc;
@@ -433,8 +429,7 @@ static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks,
}
static int VerifyBlocks(const char *expected, const uint8_t *buffer,
- size_t blocks, int printerror) {
- char* hexdigest = NULL;
+ size_t blocks, bool printerror) {
int rc = -1;
uint8_t digest[SHA_DIGEST_SIZE];
@@ -443,128 +438,75 @@ static int VerifyBlocks(const char *expected, const uint8_t *buffer,
}
SHA_hash(buffer, blocks * BLOCKSIZE, digest);
- hexdigest = PrintSha1(digest);
- if (hexdigest != NULL) {
- rc = strcmp(expected, hexdigest);
+ std::string hexdigest = print_sha1(digest);
- if (rc != 0 && printerror) {
- fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
- expected, hexdigest);
- }
+ rc = hexdigest != std::string(expected);
- free(hexdigest);
+ if (rc != 0 && printerror) {
+ fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n",
+ expected, hexdigest.c_str());
}
return rc;
}
-static char* GetStashFileName(const char* base, const char* id, const char* postfix) {
- char* fn;
- int len;
- int res;
-
- if (base == NULL) {
- return NULL;
- }
-
- if (id == NULL) {
- id = "";
- }
-
- if (postfix == NULL) {
- postfix = "";
- }
-
- len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1;
- fn = malloc(len);
-
- if (fn == NULL) {
- fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
- return NULL;
+static std::string GetStashFileName(const std::string& base, const std::string id,
+ const std::string postfix) {
+ if (base.empty()) {
+ return "";
}
- res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix);
-
- if (res < 0 || res >= len) {
- fprintf(stderr, "failed to format file name (return value %d)\n", res);
- free(fn);
- return NULL;
- }
+ std::string fn(STASH_DIRECTORY_BASE);
+ fn += "/" + base + "/" + id + postfix;
return fn;
}
-typedef void (*StashCallback)(const char*, void*);
+typedef void (*StashCallback)(const std::string&, void*);
// Does a best effort enumeration of stash files. Ignores possible non-file
// items in the stash directory and continues despite of errors. Calls the
// 'callback' function for each file and passes 'data' to the function as a
// parameter.
-static void EnumerateStash(const char* dirname, StashCallback callback, void* data) {
- char* fn;
- DIR* directory;
- int len;
- int res;
- struct dirent* item;
-
- if (dirname == NULL || callback == NULL) {
+static void EnumerateStash(const std::string& dirname, StashCallback callback, void* data) {
+ if (dirname.empty() || callback == NULL) {
return;
}
- directory = opendir(dirname);
+ std::unique_ptr<DIR, int(*)(DIR*)> directory(opendir(dirname.c_str()), closedir);
if (directory == NULL) {
if (errno != ENOENT) {
- fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno));
+ fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
}
return;
}
- while ((item = readdir(directory)) != NULL) {
+ struct dirent* item;
+ while ((item = readdir(directory.get())) != NULL) {
if (item->d_type != DT_REG) {
continue;
}
- len = strlen(dirname) + 1 + strlen(item->d_name) + 1;
- fn = malloc(len);
-
- if (fn == NULL) {
- fprintf(stderr, "failed to malloc %d bytes for fn\n", len);
- continue;
- }
-
- res = snprintf(fn, len, "%s/%s", dirname, item->d_name);
-
- if (res < 0 || res >= len) {
- fprintf(stderr, "failed to format file name (return value %d)\n", res);
- free(fn);
- continue;
- }
-
+ std::string fn = dirname + "/" + std::string(item->d_name);
callback(fn, data);
- free(fn);
- }
-
- if (closedir(directory) == -1) {
- fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno));
}
}
-static void UpdateFileSize(const char* fn, void* data) {
- int* size = (int*) data;
- struct stat st;
-
- if (!fn || !data) {
+static void UpdateFileSize(const std::string& fn, void* data) {
+ if (fn.empty() || !data) {
return;
}
- if (stat(fn, &st) == -1) {
- fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
+ struct stat st;
+ if (stat(fn.c_str(), &st) == -1) {
+ fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
return;
}
+ int* size = reinterpret_cast<int*>(data);
*size += st.st_size;
}
@@ -572,57 +514,49 @@ static void UpdateFileSize(const char* fn, void* data) {
// contains files. There is nothing we can do about unlikely, but possible
// errors, so they are merely logged.
-static void DeleteFile(const char* fn, void* data) {
- if (fn) {
- fprintf(stderr, "deleting %s\n", fn);
+static void DeleteFile(const std::string& fn, void* data) {
+ if (!fn.empty()) {
+ fprintf(stderr, "deleting %s\n", fn.c_str());
- if (unlink(fn) == -1 && errno != ENOENT) {
- fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno));
+ if (unlink(fn.c_str()) == -1 && errno != ENOENT) {
+ fprintf(stderr, "unlink \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
}
}
}
-static void DeletePartial(const char* fn, void* data) {
- if (fn && strstr(fn, ".partial") != NULL) {
+static void DeletePartial(const std::string& fn, void* data) {
+ if (android::base::EndsWith(fn, ".partial")) {
DeleteFile(fn, data);
}
}
-static void DeleteStash(const char* base) {
- char* dirname;
-
- if (base == NULL) {
+static void DeleteStash(const std::string& base) {
+ if (base.empty()) {
return;
}
- dirname = GetStashFileName(base, NULL, NULL);
+ fprintf(stderr, "deleting stash %s\n", base.c_str());
- if (dirname == NULL) {
- return;
- }
-
- fprintf(stderr, "deleting stash %s\n", base);
+ std::string dirname = GetStashFileName(base, "", "");
EnumerateStash(dirname, DeleteFile, NULL);
- if (rmdir(dirname) == -1) {
+ if (rmdir(dirname.c_str()) == -1) {
if (errno != ENOENT && errno != ENOTDIR) {
- fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno));
+ fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
}
}
-
- free(dirname);
}
-static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer,
- size_t* buffer_alloc, int printnoent) {
- char *fn = NULL;
+static int LoadStash(const std::string& base, const char* id, int verify, int* blocks,
+ uint8_t** buffer, size_t* buffer_alloc, bool printnoent) {
+ std::string fn;
int blockcount = 0;
int fd = -1;
int rc = -1;
int res;
struct stat st;
- if (!base || !id || !buffer || !buffer_alloc) {
+ if (base.empty() || !id || !buffer || !buffer_alloc) {
goto lsout;
}
@@ -630,32 +564,29 @@ static int LoadStash(const char* base, const char* id, int verify, int* blocks,
blocks = &blockcount;
}
- fn = GetStashFileName(base, id, NULL);
+ fn = GetStashFileName(base, std::string(id), "");
- if (fn == NULL) {
- goto lsout;
- }
-
- res = stat(fn, &st);
+ res = stat(fn.c_str(), &st);
if (res == -1) {
if (errno != ENOENT || printnoent) {
- fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno));
+ fprintf(stderr, "stat \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
}
goto lsout;
}
- fprintf(stderr, " loading %s\n", fn);
+ fprintf(stderr, " loading %s\n", fn.c_str());
if ((st.st_size % BLOCKSIZE) != 0) {
- fprintf(stderr, "%s size %zd not multiple of block size %d", fn, st.st_size, BLOCKSIZE);
+ fprintf(stderr, "%s size %" PRId64 " not multiple of block size %d",
+ fn.c_str(), static_cast<int64_t>(st.st_size), BLOCKSIZE);
goto lsout;
}
- fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY));
+ fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY));
if (fd == -1) {
- fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno));
+ fprintf(stderr, "open \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
goto lsout;
}
@@ -667,8 +598,8 @@ static int LoadStash(const char* base, const char* id, int verify, int* blocks,
*blocks = st.st_size / BLOCKSIZE;
- if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) {
- fprintf(stderr, "unexpected contents in %s\n", fn);
+ if (verify && VerifyBlocks(id, *buffer, *blocks, true) != 0) {
+ fprintf(stderr, "unexpected contents in %s\n", fn.c_str());
DeleteFile(fn, NULL);
goto lsout;
}
@@ -680,23 +611,21 @@ lsout:
close(fd);
}
- if (fn) {
- free(fn);
- }
-
return rc;
}
-static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer,
- int checkspace, int *exists) {
- char *fn = NULL;
- char *cn = NULL;
+static int WriteStash(const std::string& base, const char* id, int blocks,
+ uint8_t* buffer, bool checkspace, int *exists) {
+ std::string fn;
+ std::string cn;
+ std::string dname;
int fd = -1;
int rc = -1;
+ int dfd = -1;
int res;
struct stat st;
- if (base == NULL || buffer == NULL) {
+ if (base.empty() || buffer == NULL) {
goto wsout;
}
@@ -705,21 +634,17 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf
goto wsout;
}
- fn = GetStashFileName(base, id, ".partial");
- cn = GetStashFileName(base, id, NULL);
-
- if (fn == NULL || cn == NULL) {
- goto wsout;
- }
+ fn = GetStashFileName(base, std::string(id), ".partial");
+ cn = GetStashFileName(base, std::string(id), "");
if (exists) {
- res = stat(cn, &st);
+ res = stat(cn.c_str(), &st);
if (res == 0) {
// The file already exists and since the name is the hash of the contents,
// it's safe to assume the contents are identical (accidental hash collisions
// are unlikely)
- fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn);
+ fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn.c_str());
*exists = 1;
rc = 0;
goto wsout;
@@ -728,12 +653,12 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf
*exists = 0;
}
- fprintf(stderr, " writing %d blocks to %s\n", blocks, cn);
+ fprintf(stderr, " writing %d blocks to %s\n", blocks, cn.c_str());
- fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE));
+ fd = TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE));
if (fd == -1) {
- fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno));
+ fprintf(stderr, "failed to create \"%s\": %s\n", fn.c_str(), strerror(errno));
goto wsout;
}
@@ -742,12 +667,26 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf
}
if (fsync(fd) == -1) {
- fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno));
+ fprintf(stderr, "fsync \"%s\" failed: %s\n", fn.c_str(), strerror(errno));
goto wsout;
}
- if (rename(fn, cn) == -1) {
- fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno));
+ if (rename(fn.c_str(), cn.c_str()) == -1) {
+ fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn.c_str(), cn.c_str(),
+ strerror(errno));
+ goto wsout;
+ }
+
+ dname = GetStashFileName(base, "", "");
+ dfd = TEMP_FAILURE_RETRY(open(dname.c_str(), O_RDONLY | O_DIRECTORY));
+
+ if (dfd == -1) {
+ fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
+ goto wsout;
+ }
+
+ if (fsync(dfd) == -1) {
+ fprintf(stderr, "fsync \"%s\" failed: %s\n", dname.c_str(), strerror(errno));
goto wsout;
}
@@ -758,12 +697,8 @@ wsout:
close(fd);
}
- if (fn) {
- free(fn);
- }
-
- if (cn) {
- free(cn);
+ if (dfd != -1) {
+ close(dfd);
}
return rc;
@@ -773,104 +708,79 @@ wsout:
// hash enough space for the expected amount of blocks we need to store. Returns
// >0 if we created the directory, zero if it existed already, and <0 of failure.
-static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) {
- char* dirname = NULL;
- const uint8_t* digest;
- int rc = -1;
- int res;
- int size = 0;
- SHA_CTX ctx;
- struct stat st;
-
- if (blockdev == NULL || base == NULL) {
- goto csout;
+static int CreateStash(State* state, int maxblocks, const char* blockdev,
+ std::string& base) {
+ if (blockdev == NULL) {
+ return -1;
}
// Stash directory should be different for each partition to avoid conflicts
// when updating multiple partitions at the same time, so we use the hash of
// the block device name as the base directory
+ SHA_CTX ctx;
SHA_init(&ctx);
SHA_update(&ctx, blockdev, strlen(blockdev));
- digest = SHA_final(&ctx);
- *base = PrintSha1(digest);
-
- if (*base == NULL) {
- goto csout;
- }
-
- dirname = GetStashFileName(*base, NULL, NULL);
+ const uint8_t* digest = SHA_final(&ctx);
+ base = print_sha1(digest);
- if (dirname == NULL) {
- goto csout;
- }
-
- res = stat(dirname, &st);
+ std::string dirname = GetStashFileName(base, "", "");
+ struct stat st;
+ int res = stat(dirname.c_str(), &st);
if (res == -1 && errno != ENOENT) {
- ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno));
- goto csout;
+ ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
+ return -1;
} else if (res != 0) {
- fprintf(stderr, "creating stash %s\n", dirname);
- res = mkdir(dirname, STASH_DIRECTORY_MODE);
+ fprintf(stderr, "creating stash %s\n", dirname.c_str());
+ res = mkdir(dirname.c_str(), STASH_DIRECTORY_MODE);
if (res != 0) {
- ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno));
- goto csout;
+ ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname.c_str(), strerror(errno));
+ return -1;
}
if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) {
ErrorAbort(state, "not enough space for stash\n");
- goto csout;
+ return -1;
}
- rc = 1; // Created directory
- goto csout;
+ return 1; // Created directory
}
- fprintf(stderr, "using existing stash %s\n", dirname);
+ fprintf(stderr, "using existing stash %s\n", dirname.c_str());
// If the directory already exists, calculate the space already allocated to
// stash files and check if there's enough for all required blocks. Delete any
// partially completed stash files first.
EnumerateStash(dirname, DeletePartial, NULL);
+ int size = 0;
EnumerateStash(dirname, UpdateFileSize, &size);
size = (maxblocks * BLOCKSIZE) - size;
if (size > 0 && CacheSizeCheck(size) != 0) {
ErrorAbort(state, "not enough space for stash (%d more needed)\n", size);
- goto csout;
- }
-
- rc = 0; // Using existing directory
-
-csout:
- if (dirname) {
- free(dirname);
+ return -1;
}
- return rc;
+ return 0; // Using existing directory
}
-static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc,
- int fd, int usehash, int* isunresumable) {
- char *id = NULL;
- int res = -1;
- int blocks = 0;
-
- if (!wordsave || !buffer || !buffer_alloc || !isunresumable) {
+static int SaveStash(const std::string& base, char** wordsave, uint8_t** buffer,
+ size_t* buffer_alloc, int fd, bool usehash) {
+ if (!wordsave || !buffer || !buffer_alloc) {
return -1;
}
- id = strtok_r(NULL, " ", wordsave);
-
+ char *id = strtok_r(NULL, " ", wordsave);
if (id == NULL) {
fprintf(stderr, "missing id field in stash command\n");
return -1;
}
- if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) {
+ int blocks = 0;
+ if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, false) == 0) {
// Stash file already exists and has expected contents. Do not
// read from source again, as the source may have been already
// overwritten during a previous attempt.
@@ -881,7 +791,7 @@ static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t
return -1;
}
- if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) {
+ if (usehash && VerifyBlocks(id, *buffer, blocks, true) != 0) {
// Source blocks have unexpected contents. If we actually need this
// data later, this is an unrecoverable error. However, the command
// that uses the data may have already completed previously, so the
@@ -891,24 +801,17 @@ static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t
}
fprintf(stderr, "stashing %d blocks to %s\n", blocks, id);
- return WriteStash(base, id, blocks, *buffer, 0, NULL);
+ return WriteStash(base, id, blocks, *buffer, false, NULL);
}
-static int FreeStash(const char* base, const char* id) {
- char *fn = NULL;
-
- if (base == NULL || id == NULL) {
+static int FreeStash(const std::string& base, const char* id) {
+ if (base.empty() || id == NULL) {
return -1;
}
- fn = GetStashFileName(base, id, NULL);
-
- if (fn == NULL) {
- return -1;
- }
+ std::string fn = GetStashFileName(base, std::string(id), "");
DeleteFile(fn, NULL);
- free(fn);
return 0;
}
@@ -947,12 +850,11 @@ static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) {
// target RangeSet. Any stashes required are loaded using LoadStash.
static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
- uint8_t** buffer, size_t* buffer_alloc, int fd,
- const char* stashbase, int* overlap) {
+ uint8_t** buffer, size_t* buffer_alloc, int fd,
+ const std::string& stashbase, bool* overlap) {
char* word;
char* colonsave;
char* colon;
- int id;
int res;
RangeSet* locs;
size_t stashalloc = 0;
@@ -976,7 +878,7 @@ static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
res = ReadBlocks(src, *buffer, fd);
if (overlap && tgt) {
- *overlap = range_overlaps(src, *tgt);
+ *overlap = range_overlaps(*src, **tgt);
}
free(src);
@@ -1003,7 +905,7 @@ static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks,
colonsave = NULL;
colon = strtok_r(word, ":", &colonsave);
- res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1);
+ res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, true);
if (res == -1) {
// These source blocks will fail verification if used later, but we
@@ -1031,12 +933,12 @@ typedef struct {
char* cmdname;
char* cpos;
char* freestash;
- char* stashbase;
- int canwrite;
+ std::string stashbase;
+ bool canwrite;
int createdstash;
int fd;
int foundwrites;
- int isunresumable;
+ bool isunresumable;
int version;
int written;
NewThreadInfo nti;
@@ -1064,11 +966,10 @@ typedef struct {
// can be performed.
static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks,
- int onehash, int* overlap) {
+ int onehash, bool* overlap) {
char* srchash = NULL;
char* tgthash = NULL;
int stash_exists = 0;
- int overlap_blocks = 0;
int rc = -1;
uint8_t* tgtbuffer = NULL;
@@ -1110,20 +1011,20 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr
goto v3out;
}
- if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) {
+ if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, false) == 0) {
// Target blocks already have expected content, command should be skipped
rc = 1;
goto v3out;
}
- if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) {
+ if (VerifyBlocks(srchash, params->buffer, *src_blocks, true) == 0) {
// If source and target blocks overlap, stash the source blocks so we can
// resume from possible write errors
if (*overlap) {
fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks,
srchash);
- if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1,
+ if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, true,
&stash_exists) != 0) {
fprintf(stderr, "failed to stash overlapping source blocks\n");
goto v3out;
@@ -1141,7 +1042,7 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr
}
if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, &params->buffer,
- &params->bufsize, 1) == 0) {
+ &params->bufsize, true) == 0) {
// Overlapping source blocks were previously stashed, command can proceed.
// We are recovering from an interrupted command, so we don't know if the
// stash can safely be deleted after this command.
@@ -1151,7 +1052,7 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr
// Valid source data not available, update cannot be resumed
fprintf(stderr, "partition has unexpected contents\n");
- params->isunresumable = 1;
+ params->isunresumable = true;
v3out:
if (tgtbuffer) {
@@ -1163,7 +1064,7 @@ v3out:
static int PerformCommandMove(CommandParameters* params) {
int blocks = 0;
- int overlap = 0;
+ bool overlap = false;
int rc = -1;
int status = 0;
RangeSet* tgt = NULL;
@@ -1228,7 +1129,7 @@ static int PerformCommandStash(CommandParameters* params) {
}
return SaveStash(params->stashbase, &params->cpos, &params->buffer, &params->bufsize,
- params->fd, (params->version >= 3), &params->isunresumable);
+ params->fd, (params->version >= 3));
}
static int PerformCommandFree(CommandParameters* params) {
@@ -1245,8 +1146,6 @@ static int PerformCommandFree(CommandParameters* params) {
static int PerformCommandZero(CommandParameters* params) {
char* range = NULL;
- int i;
- int j;
int rc = -1;
RangeSet* tgt = NULL;
@@ -1269,12 +1168,12 @@ static int PerformCommandZero(CommandParameters* params) {
memset(params->buffer, 0, BLOCKSIZE);
if (params->canwrite) {
- for (i = 0; i < tgt->count; ++i) {
+ for (size_t i = 0; i < tgt->count; ++i) {
if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) {
goto pczout;
}
- for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
+ for (size_t j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) {
if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) {
goto pczout;
}
@@ -1354,7 +1253,7 @@ static int PerformCommandDiff(CommandParameters* params) {
char* logparams = NULL;
char* value = NULL;
int blocks = 0;
- int overlap = 0;
+ bool overlap = false;
int rc = -1;
int status = 0;
RangeSet* tgt = NULL;
@@ -1464,7 +1363,6 @@ pcdout:
static int PerformCommandErase(CommandParameters* params) {
char* range = NULL;
- int i;
int rc = -1;
RangeSet* tgt = NULL;
struct stat st;
@@ -1491,7 +1389,7 @@ static int PerformCommandErase(CommandParameters* params) {
range = strtok_r(NULL, " ", &params->cpos);
if (range == NULL) {
- fprintf(stderr, "missing target blocks for zero\n");
+ fprintf(stderr, "missing target blocks for erase\n");
goto pceout;
}
@@ -1500,7 +1398,7 @@ static int PerformCommandErase(CommandParameters* params) {
if (params->canwrite) {
fprintf(stderr, " erasing %d blocks\n", tgt->size);
- for (i = 0; i < tgt->count; ++i) {
+ for (size_t i = 0; i < tgt->count; ++i) {
// offset in bytes
blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE;
// length in bytes
@@ -1560,7 +1458,7 @@ static unsigned int HashString(const char *s) {
// - patch stream (filename within package.zip, must be uncompressed)
static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[],
- const Command* commands, int cmdcount, int dryrun) {
+ const Command* commands, int cmdcount, bool dryrun) {
char* line = NULL;
char* linesave = NULL;
@@ -1666,7 +1564,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc,
// The data in transfer_list_value is not necessarily null-terminated, so we need
// to copy it to a new buffer and add the null that strtok_r will need.
- transfer_list = malloc(transfer_list_value->size + 1);
+ transfer_list = reinterpret_cast<char*>(malloc(transfer_list_value->size + 1));
if (transfer_list == NULL) {
fprintf(stderr, "failed to allocate %zd bytes for transfer list\n",
@@ -1715,8 +1613,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc,
}
if (stash_max_blocks >= 0) {
- res = CreateStash(state, stash_max_blocks, blockdev_filename->data,
- &params.stashbase);
+ res = CreateStash(state, stash_max_blocks, blockdev_filename->data, params.stashbase);
if (res == -1) {
goto pbiudone;
@@ -1767,6 +1664,10 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc,
}
if (params.canwrite) {
+ if (fsync(params.fd) == -1) {
+ fprintf(stderr, "fsync failed: %s\n", strerror(errno));
+ goto pbiudone;
+ }
fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks);
fflush(cmd_pipe);
}
@@ -1833,10 +1734,6 @@ pbiudone:
DeleteStash(params.stashbase);
}
- if (params.stashbase) {
- free(params.stashbase);
- }
-
return StringValue(rc == 0 ? strdup("t") : strdup(""));
}
@@ -1908,7 +1805,7 @@ Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]
// Perform a dry run without writing to test if an update can proceed
return PerformBlockImageUpdate(name, state, argc, argv, commands,
- sizeof(commands) / sizeof(commands[0]), 1);
+ sizeof(commands) / sizeof(commands[0]), true);
}
Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1924,7 +1821,7 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]
};
return PerformBlockImageUpdate(name, state, argc, argv, commands,
- sizeof(commands) / sizeof(commands[0]), 0);
+ sizeof(commands) / sizeof(commands[0]), false);
}
Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
@@ -1944,27 +1841,28 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
goto done;
}
- int fd = open(blockdev_filename->data, O_RDWR);
+ int fd;
+ fd = open(blockdev_filename->data, O_RDWR);
if (fd < 0) {
ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno));
goto done;
}
- RangeSet* rs = parse_range(ranges->data);
+ RangeSet* rs;
+ rs = parse_range(ranges->data);
uint8_t buffer[BLOCKSIZE];
SHA_CTX ctx;
SHA_init(&ctx);
- int i, j;
- for (i = 0; i < rs->count; ++i) {
+ for (size_t i = 0; i < rs->count; ++i) {
if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) {
ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data,
strerror(errno));
goto done;
}
- for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
+ for (size_t j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) {
if (read_all(fd, buffer, BLOCKSIZE) == -1) {
ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data,
strerror(errno));
@@ -1983,7 +1881,7 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) {
if (digest == NULL) {
return StringValue(strdup(""));
} else {
- return StringValue(PrintSha1(digest));
+ return StringValue(strdup(print_sha1(digest).c_str()));
}
}
diff --git a/updater/install.c b/updater/install.cpp
index 4a0e064c3..422a1bb1e 100644
--- a/updater/install.c
+++ b/updater/install.cpp
@@ -80,9 +80,9 @@ void uiPrintf(State* state, const char* format, ...) {
// Take a sha-1 digest and return it as a newly-allocated hex string.
char* PrintSha1(const uint8_t* digest) {
- char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
- int i;
+ char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_SIZE*2 + 1));
const char* alphabet = "0123456789abcdef";
+ size_t i;
for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
buffer[i*2+1] = alphabet[digest[i] & 0xf];
@@ -138,18 +138,20 @@ Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
goto done;
}
- char *secontext = NULL;
+ {
+ char *secontext = NULL;
- if (sehandle) {
- selabel_lookup(sehandle, &secontext, mount_point, 0755);
- setfscreatecon(secontext);
- }
+ if (sehandle) {
+ selabel_lookup(sehandle, &secontext, mount_point, 0755);
+ setfscreatecon(secontext);
+ }
- mkdir(mount_point, 0755);
+ mkdir(mount_point, 0755);
- if (secontext) {
- freecon(secontext);
- setfscreatecon(NULL);
+ if (secontext) {
+ freecon(secontext);
+ setfscreatecon(NULL);
+ }
}
if (strcmp(partition_type, "MTD") == 0) {
@@ -207,11 +209,13 @@ Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
}
scan_mounted_volumes();
- const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
- if (vol == NULL) {
- result = strdup("");
- } else {
- result = mount_point;
+ {
+ const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
+ if (vol == NULL) {
+ result = strdup("");
+ } else {
+ result = mount_point;
+ }
}
done:
@@ -235,17 +239,19 @@ Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
}
scan_mounted_volumes();
- const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
- if (vol == NULL) {
- uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
- result = strdup("");
- } else {
- int ret = unmount_mounted_volume(vol);
- if (ret != 0) {
- uiPrintf(state, "unmount of %s failed (%d): %s\n",
- mount_point, ret, strerror(errno));
+ {
+ const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
+ if (vol == NULL) {
+ uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
+ result = strdup("");
+ } else {
+ int ret = unmount_mounted_volume(vol);
+ if (ret != 0) {
+ uiPrintf(state, "unmount of %s failed (%d): %s\n",
+ mount_point, ret, strerror(errno));
+ }
+ result = mount_point;
}
- result = mount_point;
}
done:
@@ -418,9 +424,8 @@ done:
}
Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
- char** paths = malloc(argc * sizeof(char*));
- int i;
- for (i = 0; i < argc; ++i) {
+ char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
+ for (int i = 0; i < argc; ++i) {
paths[i] = Evaluate(state, argv[i]);
if (paths[i] == NULL) {
int j;
@@ -435,7 +440,7 @@ Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
bool recursive = (strcmp(name, "delete_recursive") == 0);
int success = 0;
- for (i = 0; i < argc; ++i) {
+ for (int i = 0; i < argc; ++i) {
if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
++success;
free(paths[i]);
@@ -522,8 +527,6 @@ Value* PackageExtractFileFn(const char* name, State* state,
}
bool success = false;
- UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
-
if (argc == 2) {
// The two-argument version extracts to a file.
@@ -539,14 +542,16 @@ Value* PackageExtractFileFn(const char* name, State* state,
goto done2;
}
- FILE* f = fopen(dest_path, "wb");
- if (f == NULL) {
- printf("%s: can't open %s for write: %s\n",
- name, dest_path, strerror(errno));
- goto done2;
+ {
+ FILE* f = fopen(dest_path, "wb");
+ if (f == NULL) {
+ printf("%s: can't open %s for write: %s\n",
+ name, dest_path, strerror(errno));
+ goto done2;
+ }
+ success = mzExtractZipEntryToFile(za, entry, fileno(f));
+ fclose(f);
}
- success = mzExtractZipEntryToFile(za, entry, fileno(f));
- fclose(f);
done2:
free(zip_path);
@@ -557,7 +562,7 @@ Value* PackageExtractFileFn(const char* name, State* state,
// as the result.
char* zip_path;
- Value* v = malloc(sizeof(Value));
+ Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
v->type = VAL_BLOB;
v->size = -1;
v->data = NULL;
@@ -572,7 +577,7 @@ Value* PackageExtractFileFn(const char* name, State* state,
}
v->size = mzGetZipEntryUncompLen(entry);
- v->data = malloc(v->size);
+ v->data = reinterpret_cast<char*>(malloc(v->size));
if (v->data == NULL) {
printf("%s: failed to allocate %ld bytes for %s\n",
name, (long)v->size, zip_path);
@@ -871,17 +876,14 @@ static int do_SetMetadataRecursive(const char* filename, const struct stat *stat
}
static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
- int i;
int bad = 0;
- static int nwarnings = 0;
struct stat sb;
Value* result = NULL;
bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
if ((argc % 2) != 1) {
- return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
- name, argc);
+ return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
}
char** args = ReadVarArgs(state, argc, argv);
@@ -892,20 +894,22 @@ static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv
goto done;
}
- struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
+ {
+ struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
- if (recursive) {
- recursive_parsed_args = parsed;
- recursive_state = state;
- bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
- memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
- recursive_state = NULL;
- } else {
- bad += ApplyParsedPerms(state, args[0], &sb, parsed);
+ if (recursive) {
+ recursive_parsed_args = parsed;
+ recursive_state = state;
+ bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
+ memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
+ recursive_state = NULL;
+ } else {
+ bad += ApplyParsedPerms(state, args[0], &sb, parsed);
+ }
}
done:
- for (i = 0; i < argc; ++i) {
+ for (int i = 0; i < argc; ++i) {
free(args[i]);
}
free(args);
@@ -925,8 +929,7 @@ Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc != 1) {
return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
}
- char* key;
- key = Evaluate(state, argv[0]);
+ char* key = Evaluate(state, argv[0]);
if (key == NULL) return NULL;
char value[PROPERTY_VALUE_MAX];
@@ -953,29 +956,27 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
struct stat st;
if (stat(filename, &st) < 0) {
- ErrorAbort(state, "%s: failed to stat \"%s\": %s",
- name, filename, strerror(errno));
+ ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
goto done;
}
#define MAX_FILE_GETPROP_SIZE 65536
if (st.st_size > MAX_FILE_GETPROP_SIZE) {
- ErrorAbort(state, "%s too large for %s (max %d)",
- filename, name, MAX_FILE_GETPROP_SIZE);
+ ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
goto done;
}
- buffer = malloc(st.st_size+1);
+ buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
if (buffer == NULL) {
ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
goto done;
}
- FILE* f = fopen(filename, "rb");
+ FILE* f;
+ f = fopen(filename, "rb");
if (f == NULL) {
- ErrorAbort(state, "%s: failed to open %s: %s",
- name, filename, strerror(errno));
+ ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
goto done;
}
@@ -989,7 +990,8 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
fclose(f);
- char* line = strtok(buffer, "\n");
+ char* line;
+ line = strtok(buffer, "\n");
do {
// skip whitespace at start of line
while (*line && isspace(*line)) ++line;
@@ -1033,15 +1035,6 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
return StringValue(result);
}
-
-static bool write_raw_image_cb(const unsigned char* data,
- int data_len, void* ctx) {
- int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
- if (r == data_len) return true;
- printf("%s\n", strerror(errno));
- return false;
-}
-
// write_raw_image(filename_or_blob, partition)
Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
char* result = NULL;
@@ -1068,14 +1061,16 @@ Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
}
mtd_scan_partitions();
- const MtdPartition* mtd = mtd_find_partition_by_name(partition);
+ const MtdPartition* mtd;
+ mtd = mtd_find_partition_by_name(partition);
if (mtd == NULL) {
printf("%s: no mtd partition named \"%s\"\n", name, partition);
result = strdup("");
goto done;
}
- MtdWriteContext* ctx = mtd_write_partition(mtd);
+ MtdWriteContext* ctx;
+ ctx = mtd_write_partition(mtd);
if (ctx == NULL) {
printf("%s: can't write mtd partition \"%s\"\n",
name, partition);
@@ -1090,14 +1085,13 @@ Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
char* filename = contents->data;
FILE* f = fopen(filename, "rb");
if (f == NULL) {
- printf("%s: can't open %s: %s\n",
- name, filename, strerror(errno));
+ printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
result = strdup("");
goto done;
}
success = true;
- char* buffer = malloc(BUFSIZ);
+ char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
int read;
while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
int wrote = mtd_write_data(ctx, buffer, read);
@@ -1144,8 +1138,7 @@ Value* ApplyPatchSpaceFn(const char* name, State* state,
char* endptr;
size_t bytes = strtol(bytes_str, &endptr, 10);
if (bytes == 0 && endptr == bytes_str) {
- ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
- name, bytes_str);
+ ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
free(bytes_str);
return NULL;
}
@@ -1205,7 +1198,7 @@ Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
return NULL;
}
- char** patch_sha_str = malloc(patchcount * sizeof(char*));
+ char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*)));
for (i = 0; i < patchcount; ++i) {
patch_sha_str[i] = patches[i*2]->data;
patches[i*2]->data = NULL;
@@ -1264,7 +1257,7 @@ Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
for (i = 0; i < argc; ++i) {
size += strlen(args[i]);
}
- char* buffer = malloc(size+1);
+ char* buffer = reinterpret_cast<char*>(malloc(size+1));
size = 0;
for (i = 0; i < argc; ++i) {
strcpy(buffer+size, args[i]);
@@ -1294,7 +1287,7 @@ Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
return NULL;
}
- char** args2 = malloc(sizeof(char*) * (argc+1));
+ char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
memcpy(args2, args, sizeof(char*) * argc);
args2[argc] = NULL;
@@ -1361,7 +1354,7 @@ Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
}
int i;
- uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
+ uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE));
for (i = 1; i < argc; ++i) {
if (args[i]->type != VAL_STRING) {
printf("%s(): arg %d is not a string; skipping",
@@ -1397,7 +1390,7 @@ Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
char* filename;
if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
- Value* v = malloc(sizeof(Value));
+ Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
v->type = VAL_BLOB;
FileContents fc;
@@ -1555,15 +1548,14 @@ Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
return ErrorAbort(state, "%s() could not read args", name);
}
- int i;
- char** args2 = malloc(sizeof(char*) * (argc+1));
+ char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
// Tune2fs expects the program name as its args[0]
args2[0] = strdup(name);
- for (i = 0; i < argc; ++i) {
+ for (int i = 0; i < argc; ++i) {
args2[i + 1] = args[i];
}
int result = tune2fs_main(argc + 1, args2);
- for (i = 0; i < argc; ++i) {
+ for (int i = 0; i < argc; ++i) {
free(args[i]);
}
free(args);
diff --git a/updater/updater.c b/updater/updater.cpp
index 661f69587..0f22e6d04 100644
--- a/updater/updater.c
+++ b/updater/updater.cpp
@@ -89,7 +89,7 @@ int main(int argc, char** argv) {
return 4;
}
- char* script = malloc(script_entry->uncompLen+1);
+ char* script = reinterpret_cast<char*>(malloc(script_entry->uncompLen+1));
if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) {
printf("failed to read script from package\n");
return 5;