static unsigned int file_counter = 0;
static void get_file_list(char *path,FILE_LIST_T *list, int max_list)
{
DIR *dirp;
struct dirent *direntp;
char buf[150];
char mtd_dir[100];
struct stat statbuf;
dirp=opendir(path);
if(dirp==NULL)
{
printf("open directory %s error\n",path);
return;
}
while((direntp=readdir(dirp))!=NULL)
{
if(strcmp(direntp->d_name,".")==0 || strcmp(direntp->d_name,"..")==0)
continue;
snprintf(buf,sizeof(buf),"%s%s",path,direntp->d_name);
if(stat(buf,&statbuf)==-1)
{
printf("stat failed on %s\n",buf);
continue;
}
/*here we process only directory®ular file items,which means that link/device/socket/fifo files are ignored*/
if(S_ISDIR(statbuf.st_mode))/*the item is a directory */
{
snprintf(mtd_dir,sizeof(mtd_dir),"%s%s","mkdir /flash/",buf+strlen(LS_PATH));
system(mtd_dir);/*this will makes the system unstable!*/
strcat(buf,"/");
get_file_list(buf,list,max_list);
}
else if(S_ISREG(statbuf.st_mode))/*the item is a regular file*/
{
list[file_counter].name = (char *)malloc(LS_FILE_NAME_LEN);
if (list[file_counter].name == NULL)
{
printf("ls malloc failed\n");
return;
}
snprintf(list[file_counter].name,LS_FILE_NAME_LEN,"%s%s",path+strlen(LS_PATH),direntp->d_name);
list[file_counter].file_size = statbuf.st_size;
fs_total_size+= list[file_counter].file_size;
if(++file_counter>=max_list)
break;
}
}
closedir(dirp);
}
类似的操作可以参考busybox中的recursive_action.c recursive_action()。
阅读(1859) | 评论(0) | 转发(0) |