Merge "Move some small users of fstab to new C++ Fstab"

This commit is contained in:
Tom Cherry
2018-12-05 00:36:14 +00:00
committed by Gerrit Code Review
2 changed files with 27 additions and 35 deletions
+10 -9
View File
@@ -27,21 +27,22 @@
#include <android-base/properties.h> #include <android-base/properties.h>
#include <android-base/stringprintf.h> #include <android-base/stringprintf.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <fs_mgr.h> #include <fstab/fstab.h>
static std::string get_misc_blk_device(std::string* err) { static std::string get_misc_blk_device(std::string* err) {
std::unique_ptr<fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_default(), Fstab fstab;
fs_mgr_free_fstab); if (!ReadDefaultFstab(&fstab)) {
if (!fstab) {
*err = "failed to read default fstab"; *err = "failed to read default fstab";
return ""; return "";
} }
fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab.get(), "/misc"); for (const auto& entry : fstab) {
if (record == nullptr) { if (entry.mount_point == "/misc") {
*err = "failed to find /misc partition"; return entry.blk_device;
return ""; }
} }
return record->blk_device;
*err = "failed to find /misc partition";
return "";
} }
// In recovery mode, recovery can get started and try to access the misc // In recovery mode, recovery can get started and try to access the misc
+17 -26
View File
@@ -115,6 +115,7 @@
#include <cutils/android_reboot.h> #include <cutils/android_reboot.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
#include <fs_mgr.h> #include <fs_mgr.h>
#include <fstab/fstab.h>
#include "otautil/error_code.h" #include "otautil/error_code.h"
@@ -136,7 +137,7 @@ static const std::string UNCRYPT_PATH_FILE = "/cache/recovery/uncrypt_file";
static const std::string UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; static const std::string UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
static const std::string UNCRYPT_SOCKET = "uncrypt"; static const std::string UNCRYPT_SOCKET = "uncrypt";
static struct fstab* fstab = nullptr; static Fstab fstab;
static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) {
if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) {
@@ -162,18 +163,8 @@ static void add_block_to_ranges(std::vector<int>& ranges, int new_block) {
} }
} }
static struct fstab* read_fstab() { static std::string find_block_device(const char* path, bool* encryptable, bool* encrypted,
fstab = fs_mgr_read_fstab_default(); bool* f2fs_fs) {
if (!fstab) {
LOG(ERROR) << "failed to read default fstab";
return NULL;
}
return fstab;
}
static const char* find_block_device(const char* path, bool* encryptable,
bool* encrypted, bool* f2fs_fs) {
// Look for a volume whose mount point is the prefix of path and // Look for a volume whose mount point is the prefix of path and
// return its block device. Set encrypted if it's currently // return its block device. Set encrypted if it's currently
// encrypted. // encrypted.
@@ -181,30 +172,29 @@ static const char* find_block_device(const char* path, bool* encryptable,
// ensure f2fs_fs is set to false first. // ensure f2fs_fs is set to false first.
*f2fs_fs = false; *f2fs_fs = false;
for (int i = 0; i < fstab->num_entries; ++i) { for (const auto& entry : fstab) {
struct fstab_rec* v = &fstab->recs[i]; if (entry.mount_point.empty()) {
if (!v->mount_point) {
continue; continue;
} }
int len = strlen(v->mount_point); auto len = entry.mount_point.size();
if (strncmp(path, v->mount_point, len) == 0 && if (android::base::StartsWith(path, entry.mount_point) &&
(path[len] == '/' || path[len] == 0)) { (path[len] == '/' || path[len] == 0)) {
*encrypted = false; *encrypted = false;
*encryptable = false; *encryptable = false;
if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) { if (entry.is_encryptable() || entry.fs_mgr_flags.file_encryption) {
*encryptable = true; *encryptable = true;
if (android::base::GetProperty("ro.crypto.state", "") == "encrypted") { if (android::base::GetProperty("ro.crypto.state", "") == "encrypted") {
*encrypted = true; *encrypted = true;
} }
} }
if (strcmp(v->fs_type, "f2fs") == 0) { if (entry.fs_type == "f2fs") {
*f2fs_fs = true; *f2fs_fs = true;
} }
return v->blk_device; return entry.blk_device;
} }
} }
return NULL; return "";
} }
static bool write_status_to_socket(int status, int socket) { static bool write_status_to_socket(int status, int socket) {
@@ -493,8 +483,8 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke
bool encryptable; bool encryptable;
bool encrypted; bool encrypted;
bool f2fs_fs; bool f2fs_fs;
const char* blk_dev = find_block_device(path, &encryptable, &encrypted, &f2fs_fs); const std::string blk_dev = find_block_device(path, &encryptable, &encrypted, &f2fs_fs);
if (blk_dev == nullptr) { if (blk_dev.empty()) {
LOG(ERROR) << "failed to find block device for " << path; LOG(ERROR) << "failed to find block device for " << path;
return kUncryptBlockDeviceFindError; return kUncryptBlockDeviceFindError;
} }
@@ -514,7 +504,7 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke
// and /sdcard we leave the file alone. // and /sdcard we leave the file alone.
if (strncmp(path, "/data/", 6) == 0) { if (strncmp(path, "/data/", 6) == 0) {
LOG(INFO) << "writing block map " << map_file; LOG(INFO) << "writing block map " << map_file;
return produce_block_map(path, map_file, blk_dev, encrypted, f2fs_fs, socket); return produce_block_map(path, map_file, blk_dev.c_str(), encrypted, f2fs_fs, socket);
} }
return 0; return 0;
@@ -654,7 +644,8 @@ int main(int argc, char** argv) {
return 2; return 2;
} }
if ((fstab = read_fstab()) == nullptr) { if (!ReadDefaultFstab(&fstab)) {
LOG(ERROR) << "failed to read default fstab";
log_uncrypt_error_code(kUncryptFstabReadError); log_uncrypt_error_code(kUncryptFstabReadError);
return 1; return 1;
} }