2013-12-18 64 views
0

我想列出递归给出文件路径的所有文件和子目录。这工作,直到当我尝试添加代码来检查文件路径是可读/可写的(我评论了线)。它现在不会进入递归循环。这是我的代码检查文件路径是否可读可写

#include <unistd.h> 
#include <sys/types.h> 
#include <dirent.h> 
#include <stdio.h> 

void listDir(char *name, FILE *fp) 
{ 
    DIR *dir; 
    struct dirent *entry; 

    if (!(dir = opendir(name))) 
     return; 
    if (!(entry = readdir(dir))) 
     return; 

    do { 
     FILE *fileCopy; 
     char read[50]; 
     char write[50]; 
     char path[1024]; 
     int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); 
     path[len] = 0; 
     if (entry->d_type == DT_DIR) 
     { 
      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) 
       continue; 
      // if((fileCopy = fopen(path, "rb")) == NULL){ 
      //  strcpy(read,"Not Readable"); 
      // } 
      // else{ 
      //  strcpy(read,"Readable"); 
      // } 
      // if((fileCopy = fopen(path, "wb")) == NULL){ 
      //  strcpy(write,"Not Writable"); 
      // } 
      // else{ 
      //  strcpy(write,"Writable"); 
      // } 
      fprintf(fp,"[D]%s - %s,%s\n", path,read,write); 
      listDir(path ,fp); 
     } 
     else 
     { 
      // if((fileCopy = fopen(path, "rb")) == NULL){ 
      //  strcpy(read,"Not Readable"); 
      // } 
      // else{ 
      //  strcpy(read,"Readable"); 
      // } 
      // if((fileCopy = fopen(path, "wb")) == NULL){ 
      //  strcpy(write,"Not Writable"); 
      // } 
      // else{ 
      // strcpy(write,"Writable"); 
      // } 
      fprintf(fp,"[F]%s - %s,%s\n", path,read,write); 
     } 
    } while ((entry = readdir(dir))); 
    closedir(dir); 

} 

int main(void) 
{ 
    FILE *fp; 
     fp = fopen("/var/mobile/Applications/FileIOAccess.txt", "w"); 
    listDir("/var",fp); 
    fclose(fp); 
    return 0; 
} 
+0

'(进入== READDIR(DIR)'???? – Recker

+0

我相信它不是错误,但ANW试了一下,现在有文件 – user2541163

+0

你被分配值进入这是在没有输出错误的'while'循环结构... [This](http://linux.die.net/man/3/readdir)可能会有帮助...尝试根据“返回值”部分中的信息更改条件。 .. – Recker

回答

2

此示例使用access更换您的fopen用来测试文件的权限。

void listDir(char *name, FILE *fp) 
{ 
    DIR *dir; 
    struct dirent *entry; 

    if (!(dir = opendir(name))) 
     return; 

    if (!(entry = readdir(dir))) 
     return; 

    do 
    { 
     char readString[50] = {0}; 
     char writeString[50] = {0}; 
     char path[1024]; 
     char filetype; 

     snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); 

     if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) 
      continue; 

     if (access(path, R_OK) == 0) 
      strcpy(readString, "Readable"); 
     else 
      strcpy(readString, "Not Readable"); 

     if (access(path, W_OK) == 0) 
      strcpy(writeString, "Writable"); 
     else 
      strcpy(writeString, "Not Writable"); 

     switch (entry->d_type) 
     { 
      case DT_UNKNOWN: filetype = '?'; break; 
      case DT_FIFO: filetype = 'P'; break; 
      case DT_CHR:  filetype = 'C'; break; 
      case DT_DIR:  filetype = 'D'; break; 
      case DT_BLK:  filetype = 'B'; break; 
      case DT_REG:  filetype = 'F'; break; 
      case DT_LNK:  filetype = 'L'; break; 
      case DT_SOCK: filetype = 'S'; break; 
      case DT_WHT:  filetype = 'W'; break; 
      default:   filetype = '?'; break; 
     } 

     fprintf(fp,"[%c]%s - %s,%s\n", filetype, path, readString, writeString); 

     if (entry->d_type == DT_DIR) 
      listDir(path, fp); 

    } while ((entry = readdir(dir))); 

    closedir(dir); 
} 
+0

只是1个问题,不知道你是否熟悉这一点。可以说我正在为越狱iphone编写一个控制台应用程序。使用stat(),access()或fopen()有什么区别?显然我尝试使用fopen()和access(),并且大多数iphone系统路径给我可读和可写,这我不相信。 – user2541163

+0

正如我上面提到的,主要区别在于'access'使用真实的用户/组ID来测试文件权限。使用'stat',您也可以选择对有效用户/组进行测试权限。我对iphone或监狱破坏不了解太多。也许越狱过程改变了权限或提升了你的用户权限? – Duck

+0

好的谢谢。你对我的帮助很大。 – user2541163

相关问题