Merge "Switch fs_mgr/*.c to C++."

This commit is contained in:
Treehugger Robot 2017-01-26 02:24:21 +00:00 committed by Gerrit Code Review
commit 8c0cf30a6b
6 changed files with 44 additions and 36 deletions

View file

@ -18,11 +18,11 @@ include $(CLEAR_VARS)
LOCAL_CLANG := true LOCAL_CLANG := true
LOCAL_SANITIZE := integer LOCAL_SANITIZE := integer
LOCAL_SRC_FILES:= \ LOCAL_SRC_FILES:= \
fs_mgr.c \ fs_mgr.cpp \
fs_mgr_dm_ioctl.cpp \ fs_mgr_dm_ioctl.cpp \
fs_mgr_format.c \ fs_mgr_format.cpp \
fs_mgr_fstab.c \ fs_mgr_fstab.cpp \
fs_mgr_slotselect.c \ fs_mgr_slotselect.cpp \
fs_mgr_verity.cpp \ fs_mgr_verity.cpp \
fs_mgr_avb.cpp \ fs_mgr_avb.cpp \
fs_mgr_avb_ops.cpp fs_mgr_avb_ops.cpp
@ -48,7 +48,7 @@ include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_CLANG := true LOCAL_CLANG := true
LOCAL_SANITIZE := integer LOCAL_SANITIZE := integer
LOCAL_SRC_FILES:= fs_mgr_main.c LOCAL_SRC_FILES:= fs_mgr_main.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_MODULE:= fs_mgr LOCAL_MODULE:= fs_mgr
LOCAL_MODULE_TAGS := optional LOCAL_MODULE_TAGS := optional

View file

@ -31,6 +31,7 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <android-base/unique_fd.h>
#include <cutils/android_reboot.h> #include <cutils/android_reboot.h>
#include <cutils/partition_utils.h> #include <cutils/partition_utils.h>
#include <cutils/properties.h> #include <cutils/properties.h>
@ -94,13 +95,13 @@ static int wait_for_file(const char *filename, int timeout)
return ret; return ret;
} }
static void check_fs(char *blk_device, char *fs_type, char *target) static void check_fs(const char *blk_device, char *fs_type, char *target)
{ {
int status; int status;
int ret; int ret;
long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
char tmpmnt_opts[64] = "errors=remount-ro"; char tmpmnt_opts[64] = "errors=remount-ro";
char *e2fsck_argv[] = { const char *e2fsck_argv[] = {
E2FSCK_BIN, E2FSCK_BIN,
#ifndef TARGET_USES_MKE2FS // "-f" only for old ext4 generation tool #ifndef TARGET_USES_MKE2FS // "-f" only for old ext4 generation tool
"-f", "-f",
@ -157,9 +158,12 @@ static void check_fs(char *blk_device, char *fs_type, char *target)
} else { } else {
INFO("Running %s on %s\n", E2FSCK_BIN, blk_device); INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv),
const_cast<char **>(e2fsck_argv),
&status, true, LOG_KLOG | LOG_FILE, &status, true, LOG_KLOG | LOG_FILE,
true, FSCK_LOG_FILE, NULL, 0); true,
const_cast<char *>(FSCK_LOG_FILE),
NULL, 0);
if (ret < 0) { if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */ /* No need to check for error in fork, we can't really handle it now */
@ -167,16 +171,18 @@ static void check_fs(char *blk_device, char *fs_type, char *target)
} }
} }
} else if (!strcmp(fs_type, "f2fs")) { } else if (!strcmp(fs_type, "f2fs")) {
char *f2fs_fsck_argv[] = { const char *f2fs_fsck_argv[] = {
F2FS_FSCK_BIN, F2FS_FSCK_BIN,
"-a", "-a",
blk_device blk_device
}; };
INFO("Running %s -a %s\n", F2FS_FSCK_BIN, blk_device); INFO("Running %s -a %s\n", F2FS_FSCK_BIN, blk_device);
ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv, ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv),
const_cast<char **>(f2fs_fsck_argv),
&status, true, LOG_KLOG | LOG_FILE, &status, true, LOG_KLOG | LOG_FILE,
true, FSCK_LOG_FILE, NULL, 0); true, const_cast<char *>(FSCK_LOG_FILE),
NULL, 0);
if (ret < 0) { if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */ /* No need to check for error in fork, we can't really handle it now */
ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN); ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN);
@ -228,17 +234,18 @@ static int do_quota(char *blk_device, char *fs_type, struct fstab_rec *rec)
ERROR("Not running %s on %s (executable not in system image)\n", ERROR("Not running %s on %s (executable not in system image)\n",
TUNE2FS_BIN, blk_device); TUNE2FS_BIN, blk_device);
} else { } else {
char* arg1 = NULL; const char* arg1 = nullptr;
char* arg2 = NULL; const char* arg2 = nullptr;
int status = 0; int status = 0;
int ret = 0; int ret = 0;
int fd = TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC)); android::base::unique_fd fd(
TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC)));
if (fd >= 0) { if (fd >= 0) {
struct ext4_super_block sb; struct ext4_super_block sb;
ret = read_super_block(fd, &sb); ret = read_super_block(fd, &sb);
if (ret < 0) { if (ret < 0) {
ERROR("Can't read '%s' super block: %s\n", blk_device, strerror(errno)); ERROR("Can't read '%s' super block: %s\n", blk_device, strerror(errno));
goto out; return force_check;
} }
int has_quota = (sb.s_feature_ro_compat int has_quota = (sb.s_feature_ro_compat
@ -247,7 +254,7 @@ static int do_quota(char *blk_device, char *fs_type, struct fstab_rec *rec)
if (has_quota == want_quota) { if (has_quota == want_quota) {
INFO("Requested quota status is match on %s\n", blk_device); INFO("Requested quota status is match on %s\n", blk_device);
goto out; return force_check;
} else if (want_quota) { } else if (want_quota) {
INFO("Enabling quota on %s\n", blk_device); INFO("Enabling quota on %s\n", blk_device);
arg1 = "-Oquota"; arg1 = "-Oquota";
@ -263,21 +270,20 @@ static int do_quota(char *blk_device, char *fs_type, struct fstab_rec *rec)
return force_check; return force_check;
} }
char *tune2fs_argv[] = { const char *tune2fs_argv[] = {
TUNE2FS_BIN, TUNE2FS_BIN,
arg1, arg1,
arg2, arg2,
blk_device, blk_device,
}; };
ret = android_fork_execvp_ext(ARRAY_SIZE(tune2fs_argv), tune2fs_argv, ret = android_fork_execvp_ext(ARRAY_SIZE(tune2fs_argv),
const_cast<char **>(tune2fs_argv),
&status, true, LOG_KLOG | LOG_FILE, &status, true, LOG_KLOG | LOG_FILE,
true, NULL, NULL, 0); true, NULL, NULL, 0);
if (ret < 0) { if (ret < 0) {
/* No need to check for error in fork, we can't really handle it now */ /* No need to check for error in fork, we can't really handle it now */
ERROR("Failed trying to run %s\n", TUNE2FS_BIN); ERROR("Failed trying to run %s\n", TUNE2FS_BIN);
} }
out:
close(fd);
} }
} }
return force_check; return force_check;
@ -300,13 +306,14 @@ static void do_reserved_size(char *blk_device, char *fs_type, struct fstab_rec *
int status = 0; int status = 0;
int ret = 0; int ret = 0;
unsigned long reserved_blocks = 0; unsigned long reserved_blocks = 0;
int fd = TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC)); android::base::unique_fd fd(
TEMP_FAILURE_RETRY(open(blk_device, O_RDONLY | O_CLOEXEC)));
if (fd >= 0) { if (fd >= 0) {
struct ext4_super_block sb; struct ext4_super_block sb;
ret = read_super_block(fd, &sb); ret = read_super_block(fd, &sb);
if (ret < 0) { if (ret < 0) {
ERROR("Can't read '%s' super block: %s\n", blk_device, strerror(errno)); ERROR("Can't read '%s' super block: %s\n", blk_device, strerror(errno));
goto out; return;
} }
reserved_blocks = rec->reserved_size / EXT4_BLOCK_SIZE(&sb); reserved_blocks = rec->reserved_size / EXT4_BLOCK_SIZE(&sb);
unsigned long reserved_threshold = ext4_blocks_count(&sb) * 0.02; unsigned long reserved_threshold = ext4_blocks_count(&sb) * 0.02;
@ -317,7 +324,7 @@ static void do_reserved_size(char *blk_device, char *fs_type, struct fstab_rec *
if (ext4_r_blocks_count(&sb) == reserved_blocks) { if (ext4_r_blocks_count(&sb) == reserved_blocks) {
INFO("Have reserved same blocks\n"); INFO("Have reserved same blocks\n");
goto out; return;
} }
} else { } else {
ERROR("Failed to open '%s': %s\n", blk_device, strerror(errno)); ERROR("Failed to open '%s': %s\n", blk_device, strerror(errno));
@ -326,13 +333,14 @@ static void do_reserved_size(char *blk_device, char *fs_type, struct fstab_rec *
char buf[16] = {0}; char buf[16] = {0};
snprintf(buf, sizeof (buf), "-r %lu", reserved_blocks); snprintf(buf, sizeof (buf), "-r %lu", reserved_blocks);
char *tune2fs_argv[] = { const char *tune2fs_argv[] = {
TUNE2FS_BIN, TUNE2FS_BIN,
buf, buf,
blk_device, blk_device,
}; };
ret = android_fork_execvp_ext(ARRAY_SIZE(tune2fs_argv), tune2fs_argv, ret = android_fork_execvp_ext(ARRAY_SIZE(tune2fs_argv),
const_cast<char **>(tune2fs_argv),
&status, true, LOG_KLOG | LOG_FILE, &status, true, LOG_KLOG | LOG_FILE,
true, NULL, NULL, 0); true, NULL, NULL, 0);
@ -340,8 +348,6 @@ static void do_reserved_size(char *blk_device, char *fs_type, struct fstab_rec *
/* No need to check for error in fork, we can't really handle it now */ /* No need to check for error in fork, we can't really handle it now */
ERROR("Failed trying to run %s\n", TUNE2FS_BIN); ERROR("Failed trying to run %s\n", TUNE2FS_BIN);
} }
out:
close(fd);
} }
} }
} }
@ -1024,9 +1030,9 @@ int fs_mgr_swapon_all(struct fstab *fstab)
int err = 0; int err = 0;
int ret = 0; int ret = 0;
int status; int status;
char *mkswap_argv[2] = { const char *mkswap_argv[2] = {
MKSWAP_BIN, MKSWAP_BIN,
NULL nullptr
}; };
if (!fstab) { if (!fstab) {
@ -1075,7 +1081,8 @@ int fs_mgr_swapon_all(struct fstab *fstab)
/* Initialize the swap area */ /* Initialize the swap area */
mkswap_argv[1] = fstab->recs[i].blk_device; mkswap_argv[1] = fstab->recs[i].blk_device;
err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv, err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv),
const_cast<char **>(mkswap_argv),
&status, true, LOG_KLOG, false, NULL, &status, true, LOG_KLOG, false, NULL,
NULL, 0); NULL, 0);
if (err) { if (err) {

View file

@ -34,8 +34,10 @@
#include "fs_mgr_priv.h" #include "fs_mgr_priv.h"
#include "cryptfs.h" #include "cryptfs.h"
extern "C" {
extern struct fs_info info; /* magic global from ext4_utils */ extern struct fs_info info; /* magic global from ext4_utils */
extern void reset_ext4fs_info(); extern void reset_ext4fs_info();
}
static int format_ext4(char *fs_blkdev, char *fs_mnt_point, bool crypt_footer) static int format_ext4(char *fs_blkdev, char *fs_mnt_point, bool crypt_footer)
{ {

View file

@ -326,9 +326,10 @@ struct fstab *fs_mgr_read_fstab_file(FILE *fstab_file)
} }
/* Allocate and init the fstab structure */ /* Allocate and init the fstab structure */
fstab = calloc(1, sizeof(struct fstab)); fstab = static_cast<struct fstab *>(calloc(1, sizeof(struct fstab)));
fstab->num_entries = entries; fstab->num_entries = entries;
fstab->recs = calloc(fstab->num_entries, sizeof(struct fstab_rec)); fstab->recs = static_cast<struct fstab_rec *>(
calloc(fstab->num_entries, sizeof(struct fstab_rec)));
fseek(fstab_file, 0, SEEK_SET); fseek(fstab_file, 0, SEEK_SET);

View file

@ -14,8 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -25,7 +23,7 @@
#warning "libgen.h must not be included" #warning "libgen.h must not be included"
#endif #endif
char *me = ""; char *me = nullptr;
static void usage(void) static void usage(void)
{ {