Add more image commands

This commit is contained in:
topjohnwu
2017-06-24 23:37:45 +09:00
parent 9430ed66cd
commit 04538372c6
6 changed files with 155 additions and 113 deletions
-72
View File
@@ -411,78 +411,6 @@ void get_client_cred(int fd, struct ucred *cred) {
PLOGE("getsockopt");
}
int create_img(const char *img, int size) {
unlink(img);
LOGI("Create %s with size %dM\n", img, size);
// Create a temp file with the file contexts
char file_contexts[] = "/magisk(/.*)? u:object_r:system_file:s0\n";
int fd = xopen("/dev/file_contexts_image", O_WRONLY | O_CREAT | O_TRUNC, 0644);
xwrite(fd, file_contexts, sizeof(file_contexts));
close(fd);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer),
"make_ext4fs -l %dM -a /magisk -S /dev/file_contexts_image %s; e2fsck -yf %s;", size, img, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status;
pid = run_command(0, NULL, "/system/bin/sh", command);
if (pid == -1)
return 1;
waitpid(pid, &status, 0);
unlink("/dev/file_contexts_image");
return WEXITSTATUS(status);
}
int get_img_size(const char *img, int *used, int *total) {
if (access(img, R_OK) == -1)
return 1;
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "e2fsck -yf %s", img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, fd = 0, status = 1;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd)) {
LOGD("magisk_img: %s", buffer);
if (strstr(buffer, img)) {
char *tok;
tok = strtok(buffer, ",");
while(tok != NULL) {
if (strstr(tok, "blocks")) {
status = 0;
break;
}
tok = strtok(NULL, ",");
}
if (status) continue;
sscanf(tok, "%d/%d", used, total);
*used = *used / 256 + 1;
*total /= 256;
break;
}
}
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int resize_img(const char *img, int size) {
LOGI("Resize %s to %dM\n", img, size);
char buffer[PATH_MAX];
snprintf(buffer, sizeof(buffer), "resize2fs %s %dM; e2fsck -yf %s;", img, size, img);
char *const command[] = { "sh", "-c", buffer, NULL };
int pid, status, fd = 0;
pid = run_command(1, &fd, "/system/bin/sh", command);
if (pid == -1)
return 1;
while (fdgets(buffer, sizeof(buffer), fd))
LOGD("magisk_img: %s", buffer);
close(fd);
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
int switch_mnt_ns(int pid) {
char mnt[32];
snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);