4

I'm having some trouble with the 'if(S_IFDIR(stbuf.st_mode))' line. Is this the correct way to test for a directory to recurse into? The function at the moment seems to do it right for 1 or 2 loops and then fails and segmentation faults.

I've tried the following and probably more as the condition.

S_ISDIR(st_mode)
((st_mode & ST_IFMT) == S_IFDIR)
S_IFDIR(stbuf.st_mode)

I've included the whole function because I'm concerned the problem might be elsewhere.

void getFolderContents(char *source, int temp){
    struct stat stbuf;
    int isDir;
    dirPnt = opendir(source);
    if(dirPnt != NULL){
        while(entry = readdir(dirPnt)){
            char *c = entry->d_name;
            if(strcmp(entry->d_name, cwd) == 0 || strcmp(entry->d_name, parent) == 0){
            }
            else{
                stat(entry->d_name, &stbuf);
                printf("%i %i ", S_IFMT, stbuf.st_mode);
                if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                printf("DIR: %s\n", entry->d_name);
                getFolderContents(entry->d_name, 0);
            }
            printf("FILE: %s\n", entry->d_name);
        }
    }
    closedir(dirPnt);
}
Preview
  • 35,317
  • 10
  • 92
  • 112
cheesysam
  • 1,109
  • 5
  • 15
  • 32
  • For the correct answer look [here](http://stackoverflow.com/questions/7267295/how-can-i-copy-a-file-from-one-directory-to-another-in-c-c/38588376#38588376). – te7 Jul 26 '16 at 11:17

2 Answers2

3

Yes, that's correct. But since you never change into the directory, you will not find it.

Consider the following directory hierarchy:

 a
 |
 +- b
 |  |
 |  +- c
 ...

Your code will scan its current directory, and find "a". It will determine that it is a directory, and call itself recursively, and open "a" for reading. This works. That scan will find a directory called "b", but trying to open it using the entry name only will fail, since the path is now "a/b".

I recommend changing into the directory (with chdir()) before opening it. That means you can just opendir("."). Store the old path, and chdir() out again when recursing that level is done (not before doing a recursive call to go deeper).

unwind
  • 391,730
  • 64
  • 469
  • 606
  • hum, that would not explain the SEGFAULT though ? – Bahbar Oct 09 '09 at 09:30
  • When the entry structure is tested for and found it is a directory, I'm calling the function again recursively which will then open that directory path. – cheesysam Oct 09 '09 at 09:32
  • Do you mean then I need to include the full relative path to directory I started with? – cheesysam Oct 09 '09 at 10:16
  • Your process has exactly one "current directory". All non-absolute file operations are done using that as that the base. If you do opendir() on "a", then opendir on "b", there is nothing that makes that mean "a/b". You need to call chdir() to make the scanned directory current, then opendir(".") and recurse. – unwind Oct 09 '09 at 11:09
1

Where is entry defined ? is it a local variable ? I can't see why it would segfault, but may be you should make it a local variable. One example where it will bite you is here :

                    if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                            printf("DIR: %s\n", entry->d_name);
                            getFolderContents(entry->d_name, 0);
                    }
                    printf("FILE: %s\n", entry->d_name);

The printf is gonna print the wrong name, so you should probably add an else here.

The same is true with dirpnt. When you go out of getFolderContents inside the while loop, you end up calling readdir on a closed dirpoint, which should get you out of the loop.

But as stated by bahbar : You can't recurse and store temporary variable in global variable

shodanex
  • 14,975
  • 11
  • 57
  • 91