Several small tweaks

This commit is contained in:
topjohnwu 2017-07-01 14:05:54 +08:00
parent 9e1aea33c3
commit 7cf4b819ae
3 changed files with 21 additions and 41 deletions

View File

@ -68,8 +68,8 @@ static int merge_img(const char *source, const char *target) {
// resize target to worst case // resize target to worst case
int s_used, s_total, t_used, t_total, n_total; int s_used, s_total, t_used, t_total, n_total;
if (get_img_size(source, &s_used, &s_total)) return 1; get_img_size(source, &s_used, &s_total);
if (get_img_size(target, &t_used, &t_total)) return 1; get_img_size(target, &t_used, &t_total);
n_total = round_size(s_used + t_used); n_total = round_size(s_used + t_used);
if (n_total != t_total) if (n_total != t_total)
resize_img(target, n_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, ".core") == 0 ||
strcmp(entry->d_name, "lost+found") == 0) strcmp(entry->d_name, "lost+found") == 0)
continue; continue;
// Cleanup old module // Cleanup old module if exists
snprintf(buf, PATH_MAX, "/dev/target/%s", entry->d_name); snprintf(buf, PATH_MAX, "%s/%s", TARGET_TMP, entry->d_name);
if (access(buf, F_OK) == 0) { if (access(buf, F_OK) == 0) {
LOGI("Upgrade module: %s\n", entry->d_name); LOGI("Upgrade module: %s\n", entry->d_name);
rm_rf(buf); rm_rf(buf);

View File

@ -300,11 +300,7 @@ int cp_afc(const char *source, const char *target) {
sfd = xopen(source, O_RDONLY); sfd = xopen(source, O_RDONLY);
tfd = xopen(target, O_WRONLY | O_CREAT | O_TRUNC); tfd = xopen(target, O_WRONLY | O_CREAT | O_TRUNC);
xsendfile(tfd, sfd, NULL, buf.st_size); xsendfile(tfd, sfd, NULL, buf.st_size);
fchmod(tfd, buf.st_mode & 0777); fclone_attr(sfd, tfd);
fchown(tfd, buf.st_uid, buf.st_gid);
fgetfilecon(sfd, &con);
fsetfilecon(tfd, con);
free(con);
close(sfd); close(sfd);
close(tfd); close(tfd);
} else if (S_ISLNK(buf.st_mode)) { } 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) { int rm_rf(const char *target) {
if (access(target, F_OK) == -1) if (access(target, F_OK) == -1)
return 0; return 0;
struct stat buf; // Use external rm command, saves a lot of headache and issues
xlstat(target, &buf); char command[PATH_MAX];
char *next; snprintf(command, sizeof(command), "rm -rf %s", target);
if (S_ISDIR(buf.st_mode)) { return system(command);
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;
} }
void clone_attr(const char *source, const char *target) { void clone_attr(const char *source, const char *target) {
@ -405,6 +373,17 @@ void clone_attr(const char *source, const char *target) {
free(con); 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) { void get_client_cred(int fd, struct ucred *cred) {
socklen_t ucred_length = sizeof(*cred); socklen_t ucred_length = sizeof(*cred);
if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length)) if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length))

View File

@ -89,6 +89,7 @@ int open_new(const char *filename);
int cp_afc(const char *source, const char *target); int cp_afc(const char *source, const char *target);
int clone_dir(const char *source, const char *target); int clone_dir(const char *source, const char *target);
int rm_rf(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 clone_attr(const char *source, const char *target);
void get_client_cred(int fd, struct ucred *cred); void get_client_cred(int fd, struct ucred *cred);
int switch_mnt_ns(int pid); int switch_mnt_ns(int pid);