Merge "Blacklist some vector functions for CFI."
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index 3de7be6..4979eef 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -782,9 +782,16 @@
return RemoteShell(use_shell_protocol, shell_type_arg, escape_char, command);
}
-static int adb_download_buffer(const char *service, const char *fn, const void* data, unsigned sz,
- bool show_progress)
-{
+static int adb_download_buffer(const char* service, const char* filename) {
+ std::string content;
+ if (!android::base::ReadFileToString(filename, &content)) {
+ fprintf(stderr, "error: couldn't read %s: %s\n", filename, strerror(errno));
+ return -1;
+ }
+
+ const uint8_t* data = reinterpret_cast<const uint8_t*>(content.data());
+ unsigned sz = content.size();
+
std::string error;
int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error);
if (fd < 0) {
@@ -798,10 +805,8 @@
unsigned total = sz;
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data);
- if (show_progress) {
- const char* x = strrchr(service, ':');
- if (x) service = x + 1;
- }
+ const char* x = strrchr(service, ':');
+ if (x) service = x + 1;
while (sz > 0) {
unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
@@ -814,14 +819,10 @@
}
sz -= xfer;
ptr += xfer;
- if (show_progress) {
- printf("sending: '%s' %4d%% \r", fn, (int)(100LL - ((100LL * sz) / (total))));
- fflush(stdout);
- }
+ printf("sending: '%s' %4d%% \r", filename, (int)(100LL - ((100LL * sz) / (total))));
+ fflush(stdout);
}
- if (show_progress) {
- printf("\n");
- }
+ printf("\n");
if (!adb_status(fd, &error)) {
fprintf(stderr,"* error response '%s' *\n", error.c_str());
@@ -854,38 +855,40 @@
* - When the other side sends "DONEDONE" instead of a block number,
* we hang up.
*/
-static int adb_sideload_host(const char* fn) {
- fprintf(stderr, "loading: '%s'...\n", fn);
-
- std::string content;
- if (!android::base::ReadFileToString(fn, &content)) {
- fprintf(stderr, "failed: %s\n", strerror(errno));
+static int adb_sideload_host(const char* filename) {
+ fprintf(stderr, "opening '%s'...\n", filename);
+ struct stat sb;
+ if (stat(filename, &sb) == -1) {
+ fprintf(stderr, "failed to stat file %s: %s\n", filename, strerror(errno));
+ return -1;
+ }
+ unique_fd package_fd(adb_open(filename, O_RDONLY));
+ if (package_fd == -1) {
+ fprintf(stderr, "failed to open file %s: %s\n", filename, strerror(errno));
return -1;
}
- const uint8_t* data = reinterpret_cast<const uint8_t*>(content.data());
- unsigned sz = content.size();
-
fprintf(stderr, "connecting...\n");
- std::string service =
- android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
+ std::string service = android::base::StringPrintf(
+ "sideload-host:%d:%d", static_cast<int>(sb.st_size), SIDELOAD_HOST_BLOCK_SIZE);
std::string error;
- unique_fd fd(adb_connect(service, &error));
- if (fd < 0) {
- // Try falling back to the older sideload method. Maybe this
+ unique_fd device_fd(adb_connect(service, &error));
+ if (device_fd < 0) {
+ // Try falling back to the older (<= K) sideload method. Maybe this
// is an older device that doesn't support sideload-host.
fprintf(stderr, "falling back to older sideload method...\n");
- return adb_download_buffer("sideload", fn, data, sz, true);
+ return adb_download_buffer("sideload", filename);
}
int opt = SIDELOAD_HOST_BLOCK_SIZE;
- adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
+ adb_setsockopt(device_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
+
+ char buf[SIDELOAD_HOST_BLOCK_SIZE];
size_t xfer = 0;
int last_percent = -1;
while (true) {
- char buf[9];
- if (!ReadFdExactly(fd, buf, 8)) {
+ if (!ReadFdExactly(device_fd, buf, 8)) {
fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
return -1;
}
@@ -893,26 +896,35 @@
if (strcmp("DONEDONE", buf) == 0) {
printf("\rTotal xfer: %.2fx%*s\n",
- (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
+ static_cast<double>(xfer) / (sb.st_size ? sb.st_size : 1),
+ static_cast<int>(strlen(filename) + 10), "");
return 0;
}
int block = strtol(buf, NULL, 10);
size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
- if (offset >= sz) {
+ if (offset >= static_cast<size_t>(sb.st_size)) {
fprintf(stderr, "* attempt to read block %d past end\n", block);
return -1;
}
- const uint8_t* start = data + offset;
- size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
+
size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
- if (offset_end > sz) {
- to_write = sz - offset;
+ if ((offset + SIDELOAD_HOST_BLOCK_SIZE) > static_cast<size_t>(sb.st_size)) {
+ to_write = sb.st_size - offset;
}
- if (!WriteFdExactly(fd, start, to_write)) {
- adb_status(fd, &error);
+ if (adb_lseek(package_fd, offset, SEEK_SET) != static_cast<int>(offset)) {
+ fprintf(stderr, "* failed to seek to package block: %s\n", strerror(errno));
+ return -1;
+ }
+ if (!ReadFdExactly(package_fd, buf, to_write)) {
+ fprintf(stderr, "* failed to read package block: %s\n", strerror(errno));
+ return -1;
+ }
+
+ if (!WriteFdExactly(device_fd, buf, to_write)) {
+ adb_status(device_fd, &error);
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
return -1;
}
@@ -924,9 +936,9 @@
// extra access to things like the zip central directory).
// This estimate of the completion becomes 100% when we've
// transferred ~2.13 (=100/47) times the package size.
- int percent = (int)(xfer * 47LL / (sz ? sz : 1));
+ int percent = static_cast<int>(xfer * 47LL / (sb.st_size ? sb.st_size : 1));
if (percent != last_percent) {
- printf("\rserving: '%s' (~%d%%) ", fn, percent);
+ printf("\rserving: '%s' (~%d%%) ", filename, percent);
fflush(stdout);
last_percent = percent;
}
diff --git a/debuggerd/libdebuggerd/test/tombstone_test.cpp b/debuggerd/libdebuggerd/test/tombstone_test.cpp
index 5ee07cd..325210d 100644
--- a/debuggerd/libdebuggerd/test/tombstone_test.cpp
+++ b/debuggerd/libdebuggerd/test/tombstone_test.cpp
@@ -77,7 +77,9 @@
resetLogs();
elf_set_fake_build_id("");
siginfo_t si;
+ memset(&si, 0, sizeof(si));
si.si_signo = SIGABRT;
+ si.si_code = SI_KERNEL;
ptrace_set_fake_getsiginfo(si);
}
@@ -292,7 +294,9 @@
map_mock_->AddMap(map);
siginfo_t si;
+ memset(&si, 0, sizeof(si));
si.si_signo = SIGBUS;
+ si.si_code = SI_KERNEL;
si.si_addr = reinterpret_cast<void*>(0x1000);
ptrace_set_fake_getsiginfo(si);
dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100);
@@ -348,7 +352,9 @@
map_mock_->AddMap(map);
siginfo_t si;
+ memset(&si, 0, sizeof(si));
si.si_signo = SIGBUS;
+ si.si_code = SI_KERNEL;
si.si_addr = reinterpret_cast<void*>(0xa533000);
ptrace_set_fake_getsiginfo(si);
dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100);
@@ -404,7 +410,9 @@
map_mock_->AddMap(map);
siginfo_t si;
+ memset(&si, 0, sizeof(si));
si.si_signo = SIGBUS;
+ si.si_code = SI_KERNEL;
si.si_addr = reinterpret_cast<void*>(0xa534040);
ptrace_set_fake_getsiginfo(si);
dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100);
@@ -458,7 +466,9 @@
map_mock_->AddMap(map);
siginfo_t si;
+ memset(&si, 0, sizeof(si));
si.si_signo = SIGBUS;
+ si.si_code = SI_KERNEL;
#if defined(__LP64__)
si.si_addr = reinterpret_cast<void*>(0x12345a534040UL);
#else
@@ -503,7 +513,7 @@
map_mock_->AddMap(map);
siginfo_t si;
- si.si_signo = 0;
+ memset(&si, 0, sizeof(si));
ptrace_set_fake_getsiginfo(si);
dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100);
@@ -539,7 +549,9 @@
ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
siginfo_t si;
+ memset(&si, 0, sizeof(si));
si.si_signo = i;
+ si.si_code = SI_KERNEL;
si.si_addr = reinterpret_cast<void*>(0x1000);
ptrace_set_fake_getsiginfo(si);
dump_all_maps(backtrace_mock_.get(), map_mock_.get(), &log_, 100);
@@ -592,7 +604,7 @@
TEST_F(TombstoneTest, dump_signal_info_error) {
siginfo_t si;
- si.si_signo = 0;
+ memset(&si, 0, sizeof(si));
ptrace_set_fake_getsiginfo(si);
dump_signal_info(&log_, 123);
diff --git a/fs_mgr/fs_mgr_slotselect.cpp b/fs_mgr/fs_mgr_slotselect.cpp
index f3bba7b..a536b22 100644
--- a/fs_mgr/fs_mgr_slotselect.cpp
+++ b/fs_mgr/fs_mgr_slotselect.cpp
@@ -35,10 +35,11 @@
return -1;
}
got_suffix = 1;
+ // remove below line when bootloaders fix androidboot.slot_suffix param
+ if (suffix[0] == '_') suffix.erase(suffix.begin());
}
- if (asprintf(&tmp, "%s%s", fstab->recs[n].blk_device,
- suffix.c_str()) > 0) {
+ if (asprintf(&tmp, "%s_%s", fstab->recs[n].blk_device, suffix.c_str()) > 0) {
free(fstab->recs[n].blk_device);
fstab->recs[n].blk_device = tmp;
} else {
diff --git a/include/ziparchive/zip_writer.h b/include/ziparchive/zip_writer.h
index 0b6ede4..41ca2e1 100644
--- a/include/ziparchive/zip_writer.h
+++ b/include/ziparchive/zip_writer.h
@@ -17,15 +17,16 @@
#ifndef LIBZIPARCHIVE_ZIPWRITER_H_
#define LIBZIPARCHIVE_ZIPWRITER_H_
-#include "android-base/macros.h"
-#include <utils/Compat.h>
-
#include <cstdio>
#include <ctime>
+#include <zlib.h>
+
#include <memory>
#include <string>
#include <vector>
-#include <zlib.h>
+
+#include "android-base/macros.h"
+#include "utils/Compat.h"
/**
* Writes a Zip file via a stateful interface.
@@ -63,6 +64,20 @@
kAlign32 = 0x02,
};
+ /**
+ * A struct representing a zip file entry.
+ */
+ struct FileEntry {
+ std::string path;
+ uint16_t compression_method;
+ uint32_t crc32;
+ uint32_t compressed_size;
+ uint32_t uncompressed_size;
+ uint16_t last_mod_time;
+ uint16_t last_mod_date;
+ uint32_t local_file_header_offset;
+ };
+
static const char* ErrorCodeString(int32_t error_code);
/**
@@ -122,6 +137,19 @@
int32_t FinishEntry();
/**
+ * Discards the last-written entry. Can only be called after an entry has been written using
+ * FinishEntry().
+ * Returns 0 on success, and an error value < 0 on failure.
+ */
+ int32_t DiscardLastEntry();
+
+ /**
+ * Sets `out_entry` to the last entry written after a call to FinishEntry().
+ * Returns 0 on success, and an error value < 0 if no entries have been written.
+ */
+ int32_t GetLastEntry(FileEntry* out_entry);
+
+ /**
* Writes the Central Directory Headers and flushes the zip file stream.
* Returns 0 on success, and an error value < 0 on failure.
*/
@@ -130,22 +158,11 @@
private:
DISALLOW_COPY_AND_ASSIGN(ZipWriter);
- struct FileInfo {
- std::string path;
- uint16_t compression_method;
- uint32_t crc32;
- uint32_t compressed_size;
- uint32_t uncompressed_size;
- uint16_t last_mod_time;
- uint16_t last_mod_date;
- uint32_t local_file_header_offset;
- };
-
int32_t HandleError(int32_t error_code);
int32_t PrepareDeflate();
- int32_t StoreBytes(FileInfo* file, const void* data, size_t len);
- int32_t CompressBytes(FileInfo* file, const void* data, size_t len);
- int32_t FlushCompressedBytes(FileInfo* file);
+ int32_t StoreBytes(FileEntry* file, const void* data, size_t len);
+ int32_t CompressBytes(FileEntry* file, const void* data, size_t len);
+ int32_t FlushCompressedBytes(FileEntry* file);
enum class State {
kWritingZip,
@@ -157,7 +174,8 @@
FILE* file_;
off64_t current_offset_;
State state_;
- std::vector<FileInfo> files_;
+ std::vector<FileEntry> files_;
+ FileEntry current_file_entry_;
std::unique_ptr<z_stream, void(*)(z_stream*)> z_stream_;
std::vector<uint8_t> buffer_;
diff --git a/libcutils/fs_config.c b/libcutils/fs_config.c
index 68412a0..50b44cb 100644
--- a/libcutils/fs_config.c
+++ b/libcutils/fs_config.c
@@ -116,16 +116,21 @@
* although the developer is advised to restrict the scope to the /vendor or
* oem/ file-system since the intent is to provide support for customized
* portions of a separate vendor.img or oem.img. Has to remain open so that
- * customization can also land on /system/vendor or /system/orm. We expect
- * build-time checking or filtering when constructing the associated
- * fs_config_* files.
+ * customization can also land on /system/vendor, /system/oem or /system/odm.
+ * We expect build-time checking or filtering when constructing the associated
+ * fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
*/
static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
static const char oem_conf_file[] = "/oem/etc/fs_config_files";
+static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
+static const char odm_conf_file[] = "/odm/etc/fs_config_files";
static const char* conf[][2] = {
- {sys_conf_file, sys_conf_dir}, {ven_conf_file, ven_conf_dir}, {oem_conf_file, oem_conf_dir},
+ {sys_conf_file, sys_conf_dir},
+ {ven_conf_file, ven_conf_dir},
+ {oem_conf_file, oem_conf_dir},
+ {odm_conf_file, odm_conf_dir},
};
static const struct fs_path_config android_files[] = {
@@ -142,6 +147,8 @@
{ 00600, AID_ROOT, AID_ROOT, 0, "default.prop" },
{ 00600, AID_ROOT, AID_ROOT, 0, "odm/build.prop" },
{ 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" },
+ { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
+ { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
{ 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
{ 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
diff --git a/libutils/include/utils/Compat.h b/libutils/include/utils/Compat.h
index 2709e3b..dee577e 100644
--- a/libutils/include/utils/Compat.h
+++ b/libutils/include/utils/Compat.h
@@ -37,6 +37,10 @@
return pwrite(fd, buf, nbytes, offset);
}
+static inline int ftruncate64(int fd, off64_t length) {
+ return ftruncate(fd, length);
+}
+
#endif /* __APPLE__ */
#if defined(_WIN32)
diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc
index b72ed7f..7600528 100644
--- a/libziparchive/zip_writer.cc
+++ b/libziparchive/zip_writer.cc
@@ -14,21 +14,23 @@
* limitations under the License.
*/
-#include "entry_name_utils-inl.h"
-#include "zip_archive_common.h"
#include "ziparchive/zip_writer.h"
-#include <utils/Log.h>
-
-#include <sys/param.h>
-
-#include <cassert>
#include <cstdio>
-#include <memory>
-#include <vector>
+#include <sys/param.h>
#include <zlib.h>
#define DEF_MEM_LEVEL 8 // normally in zutil.h?
+#include <memory>
+#include <vector>
+
+#include "android-base/logging.h"
+#include "utils/Compat.h"
+#include "utils/Log.h"
+
+#include "entry_name_utils-inl.h"
+#include "zip_archive_common.h"
+
#if !defined(powerof2)
#define powerof2(x) ((((x)-1)&(x))==0)
#endif
@@ -171,12 +173,12 @@
return kInvalidAlignment;
}
- FileInfo fileInfo = {};
- fileInfo.path = std::string(path);
- fileInfo.local_file_header_offset = current_offset_;
+ current_file_entry_ = {};
+ current_file_entry_.path = path;
+ current_file_entry_.local_file_header_offset = current_offset_;
- if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(fileInfo.path.data()),
- fileInfo.path.size())) {
+ if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(current_file_entry_.path.data()),
+ current_file_entry_.path.size())) {
return kInvalidEntryName;
}
@@ -188,24 +190,24 @@
header.gpb_flags |= kGPBDDFlagMask;
if (flags & ZipWriter::kCompress) {
- fileInfo.compression_method = kCompressDeflated;
+ current_file_entry_.compression_method = kCompressDeflated;
int32_t result = PrepareDeflate();
if (result != kNoError) {
return result;
}
} else {
- fileInfo.compression_method = kCompressStored;
+ current_file_entry_.compression_method = kCompressStored;
}
- header.compression_method = fileInfo.compression_method;
+ header.compression_method = current_file_entry_.compression_method;
- ExtractTimeAndDate(time, &fileInfo.last_mod_time, &fileInfo.last_mod_date);
- header.last_mod_time = fileInfo.last_mod_time;
- header.last_mod_date = fileInfo.last_mod_date;
+ ExtractTimeAndDate(time, ¤t_file_entry_.last_mod_time, ¤t_file_entry_.last_mod_date);
+ header.last_mod_time = current_file_entry_.last_mod_time;
+ header.last_mod_date = current_file_entry_.last_mod_date;
- header.file_name_length = fileInfo.path.size();
+ header.file_name_length = current_file_entry_.path.size();
- off64_t offset = current_offset_ + sizeof(header) + fileInfo.path.size();
+ off64_t offset = current_offset_ + sizeof(header) + current_file_entry_.path.size();
std::vector<char> zero_padding;
if (alignment != 0 && (offset & (alignment - 1))) {
// Pad the extra field so the data will be aligned.
@@ -220,7 +222,8 @@
return HandleError(kIoError);
}
- if (fwrite(path, sizeof(*path), fileInfo.path.size(), file_) != fileInfo.path.size()) {
+ if (fwrite(path, sizeof(*path), current_file_entry_.path.size(), file_)
+ != current_file_entry_.path.size()) {
return HandleError(kIoError);
}
@@ -230,15 +233,37 @@
return HandleError(kIoError);
}
- files_.emplace_back(std::move(fileInfo));
-
current_offset_ = offset;
state_ = State::kWritingEntry;
return kNoError;
}
+int32_t ZipWriter::DiscardLastEntry() {
+ if (state_ != State::kWritingZip || files_.empty()) {
+ return kInvalidState;
+ }
+
+ FileEntry& last_entry = files_.back();
+ current_offset_ = last_entry.local_file_header_offset;
+ if (fseeko(file_, current_offset_, SEEK_SET) != 0) {
+ return HandleError(kIoError);
+ }
+ files_.pop_back();
+ return kNoError;
+}
+
+int32_t ZipWriter::GetLastEntry(FileEntry* out_entry) {
+ CHECK(out_entry != nullptr);
+
+ if (files_.empty()) {
+ return kInvalidState;
+ }
+ *out_entry = files_.back();
+ return kNoError;
+}
+
int32_t ZipWriter::PrepareDeflate() {
- assert(state_ == State::kWritingZip);
+ CHECK(state_ == State::kWritingZip);
// Initialize the z_stream for compression.
z_stream_ = std::unique_ptr<z_stream, void(*)(z_stream*)>(new z_stream(), DeleteZStream);
@@ -269,25 +294,25 @@
return HandleError(kInvalidState);
}
- FileInfo& currentFile = files_.back();
int32_t result = kNoError;
- if (currentFile.compression_method & kCompressDeflated) {
- result = CompressBytes(¤tFile, data, len);
+ if (current_file_entry_.compression_method & kCompressDeflated) {
+ result = CompressBytes(¤t_file_entry_, data, len);
} else {
- result = StoreBytes(¤tFile, data, len);
+ result = StoreBytes(¤t_file_entry_, data, len);
}
if (result != kNoError) {
return result;
}
- currentFile.crc32 = crc32(currentFile.crc32, reinterpret_cast<const Bytef*>(data), len);
- currentFile.uncompressed_size += len;
+ current_file_entry_.crc32 = crc32(current_file_entry_.crc32,
+ reinterpret_cast<const Bytef*>(data), len);
+ current_file_entry_.uncompressed_size += len;
return kNoError;
}
-int32_t ZipWriter::StoreBytes(FileInfo* file, const void* data, size_t len) {
- assert(state_ == State::kWritingEntry);
+int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) {
+ CHECK(state_ == State::kWritingEntry);
if (fwrite(data, 1, len, file_) != len) {
return HandleError(kIoError);
@@ -297,11 +322,11 @@
return kNoError;
}
-int32_t ZipWriter::CompressBytes(FileInfo* file, const void* data, size_t len) {
- assert(state_ == State::kWritingEntry);
- assert(z_stream_);
- assert(z_stream_->next_out != nullptr);
- assert(z_stream_->avail_out != 0);
+int32_t ZipWriter::CompressBytes(FileEntry* file, const void* data, size_t len) {
+ CHECK(state_ == State::kWritingEntry);
+ CHECK(z_stream_);
+ CHECK(z_stream_->next_out != nullptr);
+ CHECK(z_stream_->avail_out != 0);
// Prepare the input.
z_stream_->next_in = reinterpret_cast<const uint8_t*>(data);
@@ -331,17 +356,17 @@
return kNoError;
}
-int32_t ZipWriter::FlushCompressedBytes(FileInfo* file) {
- assert(state_ == State::kWritingEntry);
- assert(z_stream_);
- assert(z_stream_->next_out != nullptr);
- assert(z_stream_->avail_out != 0);
+int32_t ZipWriter::FlushCompressedBytes(FileEntry* file) {
+ CHECK(state_ == State::kWritingEntry);
+ CHECK(z_stream_);
+ CHECK(z_stream_->next_out != nullptr);
+ CHECK(z_stream_->avail_out != 0);
// Keep deflating while there isn't enough space in the buffer to
// to complete the compress.
int zerr;
while ((zerr = deflate(z_stream_.get(), Z_FINISH)) == Z_OK) {
- assert(z_stream_->avail_out == 0);
+ CHECK(z_stream_->avail_out == 0);
size_t write_bytes = z_stream_->next_out - buffer_.data();
if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
return HandleError(kIoError);
@@ -373,9 +398,8 @@
return kInvalidState;
}
- FileInfo& currentFile = files_.back();
- if (currentFile.compression_method & kCompressDeflated) {
- int32_t result = FlushCompressedBytes(¤tFile);
+ if (current_file_entry_.compression_method & kCompressDeflated) {
+ int32_t result = FlushCompressedBytes(¤t_file_entry_);
if (result != kNoError) {
return result;
}
@@ -388,13 +412,15 @@
}
DataDescriptor dd = {};
- dd.crc32 = currentFile.crc32;
- dd.compressed_size = currentFile.compressed_size;
- dd.uncompressed_size = currentFile.uncompressed_size;
+ dd.crc32 = current_file_entry_.crc32;
+ dd.compressed_size = current_file_entry_.compressed_size;
+ dd.uncompressed_size = current_file_entry_.uncompressed_size;
if (fwrite(&dd, sizeof(dd), 1, file_) != 1) {
return HandleError(kIoError);
}
+ files_.emplace_back(std::move(current_file_entry_));
+
current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd);
state_ = State::kWritingZip;
return kNoError;
@@ -406,7 +432,7 @@
}
off64_t startOfCdr = current_offset_;
- for (FileInfo& file : files_) {
+ for (FileEntry& file : files_) {
CentralDirectoryRecord cdr = {};
cdr.record_signature = CentralDirectoryRecord::kSignature;
cdr.gpb_flags |= kGPBDDFlagMask;
@@ -442,11 +468,19 @@
return HandleError(kIoError);
}
+ current_offset_ += sizeof(er);
+
+ // Since we can BackUp() and potentially finish writing at an offset less than one we had
+ // already written at, we must truncate the file.
+
+ if (ftruncate64(fileno(file_), current_offset_) != 0) {
+ return HandleError(kIoError);
+ }
+
if (fflush(file_) != 0) {
return HandleError(kIoError);
}
- current_offset_ += sizeof(er);
state_ = State::kDone;
return kNoError;
}
diff --git a/libziparchive/zip_writer_test.cc b/libziparchive/zip_writer_test.cc
index 16a574d..30f4950 100644
--- a/libziparchive/zip_writer_test.cc
+++ b/libziparchive/zip_writer_test.cc
@@ -23,6 +23,10 @@
#include <memory>
#include <vector>
+static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& expected,
+ ZipArchiveHandle handle,
+ ZipEntry* zip_entry);
+
struct zipwriter : public ::testing::Test {
TemporaryFile* temp_file_;
int fd_;
@@ -59,16 +63,10 @@
ZipEntry data;
ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
- EXPECT_EQ(strlen(expected), data.compressed_length);
- EXPECT_EQ(strlen(expected), data.uncompressed_length);
EXPECT_EQ(kCompressStored, data.method);
-
- char buffer[6];
- EXPECT_EQ(0,
- ExtractToMemory(handle, &data, reinterpret_cast<uint8_t*>(&buffer), sizeof(buffer)));
- buffer[5] = 0;
-
- EXPECT_STREQ(expected, buffer);
+ EXPECT_EQ(strlen(expected), data.compressed_length);
+ ASSERT_EQ(strlen(expected), data.uncompressed_length);
+ ASSERT_TRUE(AssertFileEntryContentsEq(expected, handle, &data));
CloseArchive(handle);
}
@@ -94,26 +92,19 @@
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
- char buffer[4];
ZipEntry data;
ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
EXPECT_EQ(kCompressStored, data.method);
EXPECT_EQ(2u, data.compressed_length);
- EXPECT_EQ(2u, data.uncompressed_length);
- ASSERT_EQ(0,
- ExtractToMemory(handle, &data, reinterpret_cast<uint8_t*>(buffer), arraysize(buffer)));
- buffer[2] = 0;
- EXPECT_STREQ("he", buffer);
+ ASSERT_EQ(2u, data.uncompressed_length);
+ ASSERT_TRUE(AssertFileEntryContentsEq("he", handle, &data));
ASSERT_EQ(0, FindEntry(handle, ZipString("file/file.txt"), &data));
EXPECT_EQ(kCompressStored, data.method);
EXPECT_EQ(3u, data.compressed_length);
- EXPECT_EQ(3u, data.uncompressed_length);
- ASSERT_EQ(0,
- ExtractToMemory(handle, &data, reinterpret_cast<uint8_t*>(buffer), arraysize(buffer)));
- buffer[3] = 0;
- EXPECT_STREQ("llo", buffer);
+ ASSERT_EQ(3u, data.uncompressed_length);
+ ASSERT_TRUE(AssertFileEntryContentsEq("llo", handle, &data));
ASSERT_EQ(0, FindEntry(handle, ZipString("file/file2.txt"), &data));
EXPECT_EQ(kCompressStored, data.method);
@@ -143,7 +134,7 @@
CloseArchive(handle);
}
-void ConvertZipTimeToTm(uint32_t& zip_time, struct tm* tm) {
+static void ConvertZipTimeToTm(uint32_t& zip_time, struct tm* tm) {
memset(tm, 0, sizeof(struct tm));
tm->tm_hour = (zip_time >> 11) & 0x1f;
tm->tm_min = (zip_time >> 5) & 0x3f;
@@ -264,14 +255,8 @@
ZipEntry data;
ASSERT_EQ(0, FindEntry(handle, ZipString("file.txt"), &data));
EXPECT_EQ(kCompressDeflated, data.method);
- EXPECT_EQ(4u, data.uncompressed_length);
-
- char buffer[5];
- ASSERT_EQ(0,
- ExtractToMemory(handle, &data, reinterpret_cast<uint8_t*>(buffer), arraysize(buffer)));
- buffer[4] = 0;
-
- EXPECT_STREQ("helo", buffer);
+ ASSERT_EQ(4u, data.uncompressed_length);
+ ASSERT_TRUE(AssertFileEntryContentsEq("helo", handle, &data));
CloseArchive(handle);
}
@@ -319,3 +304,111 @@
ASSERT_EQ(-5, writer.StartAlignedEntry("align.txt", ZipWriter::kAlign32, 4096));
ASSERT_EQ(-6, writer.StartAlignedEntry("align.txt", 0, 3));
}
+
+TEST_F(zipwriter, BackupRemovesTheLastFile) {
+ ZipWriter writer(file_);
+
+ const char* kKeepThis = "keep this";
+ const char* kDropThis = "drop this";
+ const char* kReplaceWithThis = "replace with this";
+
+ ZipWriter::FileEntry entry;
+ EXPECT_LT(writer.GetLastEntry(&entry), 0);
+
+ ASSERT_EQ(0, writer.StartEntry("keep.txt", 0));
+ ASSERT_EQ(0, writer.WriteBytes(kKeepThis, strlen(kKeepThis)));
+ ASSERT_EQ(0, writer.FinishEntry());
+
+ ASSERT_EQ(0, writer.GetLastEntry(&entry));
+ EXPECT_EQ("keep.txt", entry.path);
+
+ ASSERT_EQ(0, writer.StartEntry("drop.txt", 0));
+ ASSERT_EQ(0, writer.WriteBytes(kDropThis, strlen(kDropThis)));
+ ASSERT_EQ(0, writer.FinishEntry());
+
+ ASSERT_EQ(0, writer.GetLastEntry(&entry));
+ EXPECT_EQ("drop.txt", entry.path);
+
+ ASSERT_EQ(0, writer.DiscardLastEntry());
+
+ ASSERT_EQ(0, writer.GetLastEntry(&entry));
+ EXPECT_EQ("keep.txt", entry.path);
+
+ ASSERT_EQ(0, writer.StartEntry("replace.txt", 0));
+ ASSERT_EQ(0, writer.WriteBytes(kReplaceWithThis, strlen(kReplaceWithThis)));
+ ASSERT_EQ(0, writer.FinishEntry());
+
+ ASSERT_EQ(0, writer.GetLastEntry(&entry));
+ EXPECT_EQ("replace.txt", entry.path);
+
+ ASSERT_EQ(0, writer.Finish());
+
+ // Verify that "drop.txt" does not exist.
+
+ ASSERT_GE(0, lseek(fd_, 0, SEEK_SET));
+
+ ZipArchiveHandle handle;
+ ASSERT_EQ(0, OpenArchiveFd(fd_, "temp", &handle, false));
+
+ ZipEntry data;
+ ASSERT_EQ(0, FindEntry(handle, ZipString("keep.txt"), &data));
+ ASSERT_TRUE(AssertFileEntryContentsEq(kKeepThis, handle, &data));
+
+ ASSERT_NE(0, FindEntry(handle, ZipString("drop.txt"), &data));
+
+ ASSERT_EQ(0, FindEntry(handle, ZipString("replace.txt"), &data));
+ ASSERT_TRUE(AssertFileEntryContentsEq(kReplaceWithThis, handle, &data));
+
+ CloseArchive(handle);
+}
+
+TEST_F(zipwriter, TruncateFileAfterBackup) {
+ ZipWriter writer(file_);
+
+ const char* kSmall = "small";
+
+ ASSERT_EQ(0, writer.StartEntry("small.txt", 0));
+ ASSERT_EQ(0, writer.WriteBytes(kSmall, strlen(kSmall)));
+ ASSERT_EQ(0, writer.FinishEntry());
+
+ ASSERT_EQ(0, writer.StartEntry("large.txt", 0));
+ std::vector<uint8_t> data;
+ data.resize(1024*1024, 0xef);
+ ASSERT_EQ(0, writer.WriteBytes(data.data(), data.size()));
+ ASSERT_EQ(0, writer.FinishEntry());
+
+ off_t before_len = ftello(file_);
+
+ ZipWriter::FileEntry entry;
+ ASSERT_EQ(0, writer.GetLastEntry(&entry));
+ ASSERT_EQ(0, writer.DiscardLastEntry());
+
+ ASSERT_EQ(0, writer.Finish());
+
+ off_t after_len = ftello(file_);
+
+ ASSERT_GT(before_len, after_len);
+}
+
+static ::testing::AssertionResult AssertFileEntryContentsEq(const std::string& expected,
+ ZipArchiveHandle handle,
+ ZipEntry* zip_entry) {
+ if (expected.size() != zip_entry->uncompressed_length) {
+ return ::testing::AssertionFailure() << "uncompressed entry size "
+ << zip_entry->uncompressed_length << " does not match expected size " << expected.size();
+ }
+
+ std::string actual;
+ actual.resize(expected.size());
+
+ uint8_t* buffer = reinterpret_cast<uint8_t*>(&*actual.begin());
+ if (ExtractToMemory(handle, zip_entry, buffer, actual.size()) != 0) {
+ return ::testing::AssertionFailure() << "failed to extract entry";
+ }
+
+ if (expected != actual) {
+ return ::testing::AssertionFailure() << "actual zip_entry data '" << actual
+ << "' does not match expected '" << expected << "'";
+ }
+ return ::testing::AssertionSuccess();
+}
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 366f082..59724a5 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -412,6 +412,9 @@
mkdir /data/misc/profman 0770 system shell
mkdir /data/misc/gcov 0770 root root
+ mkdir /data/vendor 0771 root root
+ mkdir /data/vendor/hardware 0771 root root
+
# For security reasons, /data/local/tmp should always be empty.
# Do not place files or directories in /data/local/tmp
mkdir /data/local/tmp 0771 shell shell