遍历目录
删除指定目录的其他文件(指定则文件保留)
/*******************************************************************************/
函数原型
功能
函数原型
函数原型
函数说明
#define STORAGE_ZERO_DIR "/age/storage/storage0/"
#define STORAGE_STARTAPP_DIR "/age/storage/storage0/startapp"
struct dirent* ent = NULL;
DIR *pDir;
char dir[64];
struct stat statbuf;
pid_t child_pid;
char* argv_list[5]={0};
char argv_list0[32],argv_list1[32],argv_list2[64]="",argv_list3[32]="";
int s32Status;
printf("file:%s,function:%s,law:%d.chenyouda\n",__FILE__,__FUNCTION__,__LINE__);
printf("size:%d \n",sizeof(name));
printf("len:%d \n",strlen(name));
if(NULL == name) return -1;
if(0==strlen(name))
{
strcpy(name,STORAGE_ZERO_DIR);
return 0;
}
printf("before\n");
if(0 != access(name,R_OK | W_OK)) return -1;
printf("strcmp into \n");
if(!strcmp(STORAGE_ZERO_DIR,name))
{
printf("enter one\n");
if(pDir = opendir(STORAGE_ZERO_DIR))
{ printf("enter two\n");
while((ent=readdir(pDir))!=NULL)
{ printf("enter three name:%s \n",ent->d_name);
memset(dir,0,sizeof(char)*64);
strcpy(dir,STORAGE_ZERO_DIR);
strcat(dir,ent->d_name);
printf("enter dir : %s \n",dir);
lstat(dir, &statbuf);//获取其属性
if (S_ISDIR(statbuf.st_mode)) //目录
{ printf("enter foure\n");
if(0 == strcmp(ent->d_name,".") ||\
0 ==strcmp(ent->d_name,".."))
continue;
if(strcmp(ent->d_name,"ServiceInfo") && strcmp(ent->d_name,"UserProperty") \
&& strcmp(ent->d_name,"startapp") )
{
//删除其他目录
printf("clean the dir %s\n",ent->d_name);
sprintf(argv_list2,"%s/%s",name,ent->d_name);
strcpy(argv_list0,"rm");
strcpy(argv_list1,"-rf");
argv_list[0]=argv_list0;
argv_list[1]=argv_list1;
argv_list[2]=argv_list2;
argv_list[3]=argv_list3;
child_pid = fork();
if( 0 == child_pid )
{
execvp((const char *)argv_list[0], (char * const*)argv_list);
printf("on error occurred in execvp!\r\n");
abort();
}
if(-1 == child_pid )
{
return -1;
}
waitpid(child_pid, &s32Status, 0);
if(WIFEXITED(s32Status))
{
printf("clean the dir have realize!!!\n");
continue;
}
}
}
else if(S_ISREG(statbuf.st_mode)) //普通文件
{
printf("enter file\n");
if(strcmp(ent->d_name,"LocalInformation.cfg") && strcmp(ent->d_name,"UpgradeVersion.json") \
&& strcmp(ent->d_name,"localNetwork.json") && strcmp(ent->d_name,"sys_setting.dat")\
&& strcmp(ent->d_name,"regionIndex.json"))
{
//删除其他文件
sprintf(argv_list2,"%s/%s",STORAGE_ZERO_DIR,ent->d_name);
strcpy(argv_list0,"rm");
strcpy(argv_list1,"-rf");
printf("clean the file %s\n",ent->d_name);
argv_list[0]=argv_list0;
argv_list[1]=argv_list1;
argv_list[2]=argv_list2;
argv_list[3]=argv_list3;
child_pid = fork();
if( 0 == child_pid )
{
execvp((const char *)argv_list[0], (char * const*)argv_list);
printf("on error occurred in execvp!\r\n");
abort();
}
if(-1 == child_pid )
{
return -1;
}
waitpid(child_pid, &s32Status, 0);
if(WIFEXITED(s32Status))
{
printf("clean the file have realize!!!\n");
continue;
}
}
}
else
printf("error \n");
}
if(0 != access(STORAGE_STARTAPP_DIR,R_OK | W_OK))
{
if(mkdir(STORAGE_STARTAPP_DIR,S_IRWXU | S_IRWXO))
{
printf("creat the file %s had faile!!!\n",STORAGE_STARTAPP_DIR);
}
printf("creat the file %s had success!!!\n",STORAGE_STARTAPP_DIR);
}
}
closedir(pDir);
}
return 0;
}