From 7cf4b819aeac9c2f92d4e4513eac1e9c44e91cdc Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 1 Jul 2017 14:05:54 +0800 Subject: [PATCH] Several small tweaks --- jni/daemon/bootstages.c | 8 +++---- jni/utils/misc.c | 53 +++++++++++++---------------------------- jni/utils/utils.h | 1 + 3 files changed, 21 insertions(+), 41 deletions(-) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index 38bf5b66b..33f81ed45 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -68,8 +68,8 @@ static int merge_img(const char *source, const char *target) { // resize target to worst case int s_used, s_total, t_used, t_total, n_total; - if (get_img_size(source, &s_used, &s_total)) return 1; - if (get_img_size(target, &t_used, &t_total)) return 1; + get_img_size(source, &s_used, &s_total); + get_img_size(target, &t_used, &t_total); n_total = round_size(s_used + t_used); if (n_total != t_total) resize_img(target, n_total); @@ -93,8 +93,8 @@ static int merge_img(const char *source, const char *target) { strcmp(entry->d_name, ".core") == 0 || strcmp(entry->d_name, "lost+found") == 0) continue; - // Cleanup old module - snprintf(buf, PATH_MAX, "/dev/target/%s", entry->d_name); + // Cleanup old module if exists + snprintf(buf, PATH_MAX, "%s/%s", TARGET_TMP, entry->d_name); if (access(buf, F_OK) == 0) { LOGI("Upgrade module: %s\n", entry->d_name); rm_rf(buf); diff --git a/jni/utils/misc.c b/jni/utils/misc.c index a1ff6aa69..2e8dccfa8 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -300,11 +300,7 @@ int cp_afc(const char *source, const char *target) { sfd = xopen(source, O_RDONLY); tfd = xopen(target, O_WRONLY | O_CREAT | O_TRUNC); xsendfile(tfd, sfd, NULL, buf.st_size); - fchmod(tfd, buf.st_mode & 0777); - fchown(tfd, buf.st_uid, buf.st_gid); - fgetfilecon(sfd, &con); - fsetfilecon(tfd, con); - free(con); + fclone_attr(sfd, tfd); close(sfd); close(tfd); } else if (S_ISLNK(buf.st_mode)) { @@ -360,38 +356,10 @@ int clone_dir(const char *source, const char *target) { int rm_rf(const char *target) { if (access(target, F_OK) == -1) return 0; - struct stat buf; - xlstat(target, &buf); - char *next; - if (S_ISDIR(buf.st_mode)) { - DIR *dir; - struct dirent *entry; - if (!(dir = xopendir(target))) - return 1; - next = xmalloc(PATH_MAX); - while ((entry = xreaddir(dir))) { - if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) - continue; - snprintf(next, PATH_MAX, "%s/%s", target, entry->d_name); - switch (entry->d_type) { - case DT_DIR: - rm_rf(next); - break; - case DT_REG: - case DT_LNK: - unlink(next); - break; - } - } - free(next); - closedir(dir); - rmdir(target); - } else if (S_ISREG(buf.st_mode) || S_ISLNK(buf.st_mode)) { - unlink(target); - } else { - return 1; - } - return 0; + // Use external rm command, saves a lot of headache and issues + char command[PATH_MAX]; + snprintf(command, sizeof(command), "rm -rf %s", target); + return system(command); } void clone_attr(const char *source, const char *target) { @@ -405,6 +373,17 @@ void clone_attr(const char *source, const char *target) { free(con); } +void fclone_attr(const int sourcefd, const int targetfd) { + struct stat buf; + fstat(sourcefd, &buf); + fchmod(targetfd, buf.st_mode & 0777); + fchown(targetfd, buf.st_uid, buf.st_gid); + char *con; + fgetfilecon(sourcefd, &con); + fsetfilecon(sourcefd, con); + free(con); +} + void get_client_cred(int fd, struct ucred *cred) { socklen_t ucred_length = sizeof(*cred); if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length)) diff --git a/jni/utils/utils.h b/jni/utils/utils.h index 6d60b70cc..02c6b9127 100644 --- a/jni/utils/utils.h +++ b/jni/utils/utils.h @@ -89,6 +89,7 @@ int open_new(const char *filename); int cp_afc(const char *source, const char *target); int clone_dir(const char *source, const char *target); int rm_rf(const char *target); +void fclone_attr(const int sourcefd, const int targetfd); void clone_attr(const char *source, const char *target); void get_client_cred(int fd, struct ucred *cred); int switch_mnt_ns(int pid);