views:

63

answers:

1

okay, so I'm trying to get a directory of folders and sub folders, but it just goes into a infinite loop. What is a better way to create a directory of folders and sub-folders? Cause I really have no idea.

this is my code so far:

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct Directory{
  public:
        int indent;
        vector<string> files;
        vector<string> folders;
        string dir;
        Directory(string mydir){ dir = mydir;}
};

int getfolders (string dir, vector<string> &folders)
{
    DIR *dp;
    struct stat st;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        stat(dirp->d_name, &st);

        if(S_ISDIR(st.st_mode)){
            if(dirp->d_name[0] != '.')
                folders.push_back(string(dirp->d_name));
        }
    }
    closedir(dp);
    return 0;
}

/*function... might want it in some class?*/
int getfiles (string dir, vector<string> &files)
{

    DIR *dp;
    struct stat st;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        stat(dirp->d_name, &st);

        if(!S_ISDIR(st.st_mode)){
            files.push_back(string(dirp->d_name));
        }
    }
    closedir(dp);
    return 0;
}

int main()
{
    struct Directory root = Directory(".");


    vector<string> display = vector<string>();

    cout << "hello\n";

    getfiles(root.dir,root.files);
    getfolders(root.dir,root.folders);

    cout << "hello\n";

    vector<Directory> dirs = vector<Directory>();

    for(int i = 0; i < (int) root.folders.size(); i++){
        dirs.push_back(Directory(root.folders[i]));

       getfiles(dirs[i].dir,dirs[i].files);
        getfolders(dirs[i].dir,dirs[i].folders);


        //infinate loop start
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            dirs.push_back(Directory(dirs[i].folders[j]));

            getfiles(dirs[i].dir,dirs[i].files);
            getfolders(dirs[i].dir,dirs[i].folders);
        }
        //infinate loop end
    }

    cout << "hello\n";

    for (int i = 0; i < (int) root.folders.size();i++) {
        cout << root.folders[i] << endl;
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            cout << dirs[i].folders[j] << endl;
        }
    }
    return 0;
}
+2  A: 

I think you have to handle symbolic links to directories differently. There could be the source for your endless loop:

Say /tmp/foo is a symbolic link to /tmp, then I think your program will go into endless loop when .==/tmp

fschmitt
Thank you for your reply. Yes, that is the cause of the endless loop, but I can't think of another way to handle the sub-folders, which is the question I am asking. ^_^
William
I would handle symbolic links to directories as files instead of directories, I think. Because otherwise, you may leave the source directory and enter a completely different part of the file system. If you have to de-reference symbolic links, keep a list of directory names you have already visited in a global array.
fschmitt