#include <dirent.h>
void recovery_backend() { DIR * pdir ; struct dirent * pdirent; struct stat f_ftime; char full_path[PATH_MAX] = {0}; char buf[PATH_MAX] = {0}; char cmd[256] = {0}; pdir = opendir(cups_backend); if(pdir == NULL) return ; for(pdirent = readdir(pdir);pdirent != NULL;pdirent = readdir(pdir)) { if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0) continue; memset(full_path,0,sizeof(full_path)); snprintf(full_path,sizeof(full_path),"%s%s",cups_backend,pdirent->d_name); if(stat(full_path,&f_ftime) != 0) if(S_ISDIR(f_ftime.st_mode)) continue; /*子目錄跳過*/ if(f_ftime.st_mode & S_IFDIR) continue; memset(buf,0,sizeof(buf)); readlink(full_path,buf,sizeof(buf)); if(strcmp(full_hook_backend,buf) == 0) { remove(full_path); memset(cmd,0,sizeof(cmd)); snprintf(cmd,sizeof(cmd),"cp -P %s%s %s",bk_backend,pdirent->d_name,cups_backend); system(cmd); } } closedir(pdir); }
遍歷目錄
void CConfigfile::GerConfigFile(const char *strpath) { char dir[MAX_PATH] = {0}; char childpath[MAX_PATH] = {0}; DIR *dp; // 定義子目錄流指針 struct dirent *entry; // 定義dirent結構指針保存后續目錄 struct stat statbuf; // 定義statbuf結構保存文件屬性 strcpy(dir, strpath); if((dp = opendir(dir)) == NULL) // 打開目錄,獲取子目錄流指針,判斷操作是否成功 { puts("can't open dir."); return; } //chdir (dir); // 切換到當前目錄 while((entry = readdir(dp)) != NULL) // 獲取下一級目錄信息,如果未否則循環 { lstat(entry->d_name, &statbuf); // 獲取下一級成員屬性 if(S_IFDIR &statbuf.st_mode) // 判斷下一級成員是否是目錄 { if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) continue; sprintf(childpath,"%s/%s",strpath,entry->d_name); printf("path:%s\n",childpath); GerConfigFile(childpath); //printf("%*s%s/\n", depth, "", entry->d_name); // 輸出目錄名稱 //scan_dir(entry->d_name, depth+4); // 遞歸調用自身,掃描下一級目錄的內容 } else { } //printf("%*s%s\n", depth, "", entry->d_name); // 輸出屬性不是目錄的成員 } //chdir(".."); // 回到上級目錄 closedir(dp); // 關閉子目錄流 return; }