检查文件路径是否可读写

时间:2013-12-18 02:47:14

标签: c

我试图在给定文件路径的情况下递归列出所有文件和子目录。这是有效的,直到我尝试添加代码以检查文件路径是否可读/可写(我将这些行注释掉)。它现在不进入递归循环。这是我的代码

#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;
}

1 个答案:

答案 0 :(得分: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);
}