user2541163
user2541163

Reputation: 737

Checking if file path is readable and writable

I am trying to list all the files and sub directories recursively given a file path. This works, until when I try to add the code to check if the file path is readable/writeable(which I commented the lines out). It now does not go into the recursive loop. This is my code

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

Upvotes: 1

Views: 11415

Answers (1)

Duck
Duck

Reputation: 27572

This example uses access to replace your use of fopen to test the file permissions.

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

Upvotes: 4

Related Questions