2013-12-18 99 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

相關問題