当有内置SD卡,且不打开shared sdcard功能的情况下,需要在SD卡中预置一些客户资源。
这些资源end user可见,也可删除。在资源被user删除之后,执行恢复出厂设置功能,需要恢复这些预置资源。
1. 预置同样的资源到user不可见的分区,如/system下。
在alps\vendor\mediatek\project_name\artifacts\out\target\product\your_project_name\system\ 下创建目录,
将预制资源放于此目录下,重新build new之后,system image中就带有预制资源。
2. recovery结束,拷贝资源到SD卡上。
在recovery main函数的结尾,也就是恢复出厂设置,或者SD卡升级之后,通过客制化函数,将特定的文件copy到SD卡下。
int
main(int argc, char **argv) {
...
//添加这个客制化函数用来恢复特定文件夹下的所有文件,包括子目录,这里是案例:/data/app/ ---> /sdcard/app/
recovery_restore_special("/sdcard/app", "/data/app");
// Otherwise, get ready to boot the main system...
finish_recovery(send_intent);
ui->Print("Rebooting...\n");
property_set(ANDROID_RB_PROPERTY, "reboot,");
return EXIT_SUCCESS;
}
int recovery_restore_special(char *des_dir_path, char *src_dir_path)
{
int ret= 0;
printf("\n\n++ %s ++\n\n", __func__);
printf("src dir:%s\n", src_dir_path);
printf("des dir:%s\n", des_dir_path);
//mount path
ret = ensure_path_mounted(src_dir_path);
if(ret < 0) {
printf("mount src dir error:%s\n", src_dir_path);
return -1;
}
printf("mount:%s ret=%d\n", src_dir_path, ret);
ret = ensure_path_mounted(des_dir_path);
if(ret < 0) {
printf("mount des dir error:%s\n", des_dir_path);
ensure_path_unmounted(src_dir_path);
return -1;
}
printf("mount:%s ret=%d\n", des_dir_path, ret);
//restore dir and file
ret = restore_dir_file(des_dir_path, src_dir_path);
printf("restore ret=%d\n", ret);
//unmount
ensure_path_unmounted(src_dir_path);
ensure_path_unmounted(des_dir_path);
printf("\n\n-- %s --\n\n", __func__);
return 0;
}
static int restore_dir_file(char *des_dir_path, char *src_dir_path)
{
int ret= 0;
DIR* dir_des = NULL;
DIR* dir_src = NULL;
struct dirent* de_des = NULL;
struct dirent* de_src = NULL;
FILE * fdes = NULL;
FILE * fsrc = NULL;
char src_file_path[256] = {0};
char des_file_path[256] = {0};
char buf[256] = {0};
int name_len = 0;
int size = 0, file_len = 0;
printf("\n\nenter, %s \n\n", __func__);
printf("src dir:%s\n", src_dir_path);
printf("des dir:%s\n", des_dir_path);
//open dir
dir_src = opendir(src_dir_path);
if(dir_src == NULL) {
printf("open src dir error:%s\n", src_dir_path);
return -1;
}
dir_des = opendir(des_dir_path);
if(dir_des == NULL) {
ret = -1;
if(errno == ENOENT) {
printf("des dir not exist:%s\n", des_dir_path);
ret = mkdir(des_dir_path, (S_IRWXU | S_IRWXG | S_IRWXO));
}
if(ret < 0) {
closedir(dir_src);
printf("open des dir error:%s\n", des_dir_path);
return -1;
}
else {
printf("mkdir des dir:%s\n", des_dir_path);
dir_des = opendir(des_dir_path);
}
}
printf("dir_src=%d, dir_des=%d\n", dir_src, dir_des);
while ((de_src = readdir(dir_src)) != NULL) {
printf("d_name: %s\n", de_src->d_name);
if (de_src->d_type == DT_DIR) {
name_len = strlen(de_src->d_name);
// skip "." and ".." entries
if ((name_len == 1 && de_src->d_name[0] == ‘.‘) ||
(name_len == 2 && de_src->d_name[0] == ‘.‘ && de_src->d_name[1] == ‘.‘)) {
continue;
}
else {
//src dir path append the folder‘s d_name
sprintf(src_file_path, "%s/%s", src_dir_path, de_src->d_name);
printf("src dir path: %s\n", src_file_path);
//des dir path append the folder‘s d_name
sprintf(des_file_path, "%s/%s", des_dir_path, de_src->d_name);
printf("des dir path: %s\n", des_file_path);
printf("---entry sub dir to restore\n");
ret = restore_dir_file(des_file_path, src_file_path);
printf("sub dir restore ret=%d\n", ret);
}
}
else if (de_src->d_type == DT_REG) {
//open src file
sprintf(src_file_path, "%s/%s", src_dir_path, de_src->d_name);
printf("src_file_path: %s\n", src_file_path);
fsrc = fopen(src_file_path, "r");
printf("fsrc=%d\n", fsrc);
//create des file
sprintf(des_file_path, "%s/%s", des_dir_path, de_src->d_name);
printf("des_file_path: %s\n", des_file_path);
fdes = fopen_path(des_file_path, "w");
printf("fdes=%d\n", fdes);
//read from src file, write to des file
file_len= 0;
while (!feof(fsrc)) {
size = fread(buf, sizeof(char), sizeof(buf), fsrc);
fwrite(buf, sizeof(char), size, fdes);
memset(buf, 0, sizeof(buf));
file_len += size;
}
fflush(fdes);
printf("%s: file_len=%d\n", de_src->d_name, file_len);
//close file
fclose(fsrc);
fclose(fdes);
}
}
//close dir
closedir(dir_src);
closedir(dir_des);
printf("\n\nexit, %s \n\n", __func__);
return 0;
}