客户预置了一些资源在内置的/data/media/0/目录下,但是恢复出厂设置会把data给清掉,需要在恢复出厂设置时不删除掉/data/media/0/的特定文件:
大致有两种思路:
1.将需要保留的目录在删除前复制到另外一个分区,比如cache分区,格式化完data分区再移动回来。
2.恢复出厂设置的操作不要执行格式化,而是执行命令rm,只删除需要删除的目录,其他目录保留。
由于abc目录文件较大,差不多2G,思路1由于找不到分区暂存,故现采用思路2。
修改bootable/recovery/recovery.c里面对data分区清除的代码:
static bool wipe_data(int should_confirm, Device* device) {
if (should_confirm && !yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!")) {
return false;
}
modified_flash = true;
ui->Print("\n-- Wiping data...\n");
bool success =
device->PreWipeData() &&
erase_volume("/data") &&
erase_volume("/cache") &&
device->PostWipeData();
ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
return success;
}
static bool erase_volume(const char* volume) {
bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
...
ensure_path_unmounted(volume);
result = format_volume(volume);改成:
int result = 0;
if(is_cache) {
ensure_path_unmounted(volume);
result = format_volume(volume);
} else {
ensure_path_mounted(volume);
system("cd /data/media/; busybox mv 0/abc/ ./; busybox rm -rf `busybox ls -a | busybox egrep -v abc`");//reserve abc
//system("cd /data/media/; busybox rm -rf `busybox ls -a | busybox egrep -v 0`");//reserve 0
system("cd /data/; busybox rm -rf `busybox ls -a | busybox egrep -v media`");//reserve media
}备注:
刚开始代码是这样写的:
int result = 0;
if(is_cache) {
ensure_path_unmounted(volume);
result = format_volume(volume);
} else {
ensure_path_mounted(volume);
system("cd /data/media/0/; busybox rm -rf `busybox ls -a | busybox egrep -v abc`");//reserve abc
system("cd /data/media/; busybox rm -rf `busybox ls -a | busybox egrep -v 0`");//reserve 0
system("cd /data/; busybox rm -rf `busybox ls -a | busybox egrep -v media`");//reserve media
}后来发现系统起来后会自动把/data/media/0/abc变成/data/media/0/0/abc,也就是多了一层0,后来改成了:
int result = 0;
if(is_cache) {
ensure_path_unmounted(volume);
result = format_volume(volume);
} else {
ensure_path_mounted(volume);
system("cd /data/media/; busybox mv 0/abc/ ./; busybox rm -rf `busybox ls -a | busybox egrep -v abc`");//reserve abc
//system("cd /data/media/; busybox rm -rf `busybox ls -a | busybox egrep -v 0`");//reserve 0
system("cd /data/; busybox rm -rf `busybox ls -a | busybox egrep -v media`");//reserve media
}
测试又发现system函数在recovery里面好像执行不成功,查了一下system函数,有以下介绍:
The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:
execl("/bin/sh", "sh", "-c", command, (char *) 0);
应该是由于recovery模式下没有/bin/sh这个指令导致的,所以又改成以下方式:
ensure_path_mounted(volume);
//system("cd /data/media/; busybox mv 0/abc/ ./; busybox rm -rf `busybox ls -a | busybox egrep -v abc`");//reserve abc
//system("cd /data/; busybox rm -rf `busybox ls -a | busybox egrep -v media`");//reserve media
result = rename("/data/media/0/abc", "/data/media/abc");
if(result) {
ui->Print("rename : %s", strerror(errno));
result = 0;
}
remove_files("/data/media", "abc");
remove_files("/data", "media");
其中remove_files函数如下:
void remove_files(const char *dir, const char *except_dir)
{
DIR *dp;
struct dirent *entry;
struct stat s;
// check the parameter !
if( NULL == dir )
{
ui->Print(" dir is null ! ");
return;
}
// check if dir_name is a valid dir
lstat( dir , &s );
if( ! S_ISDIR( s.st_mode ) )
{
ui->Print("dir is not a valid directory !");
return;
}
//ui->Print("%s", dir);
if((dp = opendir(dir)) == NULL)
{
ui->Print("can't open dir.");
return;
}
chdir (dir);
while((entry = readdir(dp)) != NULL)
{
//ui->Print("%s : %d", entry->d_name, entry->d_type);
if(strcmp(entry->d_name,".")==0 || strcmp(entry->d_name,"..")==0)
continue;
if(strcmp(entry->d_name, except_dir) == 0)
continue;
//ui->Print("%s : %d", entry->d_name, entry->d_type);
if(remove(entry->d_name))
ui->Print("remove %s : %s.\n", entry->d_name, strerror(errno));
}
chdir("..");
closedir(dp);
}
又发现remove函数无法删除非空目录,最后代码又做了调整:
void remove_files(const char *dir, const char *except_dir)
{
DIR *dp;
struct dirent *entry;
struct stat s;
pid_t pid;
//char cmd[128];
// check the parameter !
if( NULL == dir )
{
ui->Print(" dir is null ! ");
return;
}
// check if dir_name is a valid dir
lstat( dir , &s );
if( ! S_ISDIR( s.st_mode ) )
{
ui->Print("dir is not a valid directory !");
return;
}
//ui->Print("%s", dir);
if((dp = opendir(dir)) == NULL)
{
ui->Print("can't open dir.");
return;
}
chdir (dir);
while((entry = readdir(dp)) != NULL)
{
//ui->Print("%s : %d", entry->d_name, entry->d_type);
if(strcmp(entry->d_name,".")==0 || strcmp(entry->d_name,"..")==0)
continue;
if(strcmp(entry->d_name, except_dir) == 0)
continue;
//ui->Print("%s : %d", entry->d_name, entry->d_type);
if(remove(entry->d_name))
{
ui->Print("remove %s : %s.\n", entry->d_name, strerror(errno));
//snprintf(cmd, sizeof(cmd), "rm -rf %s", entry->d_name);
//printf("cmd: %s\n", buf);
if ((pid = fork()) < 0) {
printf("fork error\n");
} else if (pid == 0) {
execl("/sbin/busybox", "rm", "-rf", entry->d_name, (char *)0);
exit(127);
}
}
}
chdir("..");
closedir(dp);
}
实测OK。