lundi 27 juin 2016

DIR Functions returning false after a few days

I have a daemon (running on Ubuntu Server 16.04, compiled with g++ -std=c++11) that relies on two functions to know if a directory Exists and if it's empty:

       bool DirectoryExists ( const char* path ){

        if( path == NULL )return false;
        DIR *d;
        d = opendir(path);
        if (d){
            closedir(d);
            return true;
        }
        return false;
    }

    bool isEmpty(const char* path) {
      int n = 0;
      //Directory scan
        DIR           *d;
        struct dirent *dir;
        d = opendir(path);
        if (d){
            while ((dir = readdir(d)) != NULL){
                if(dir->d_name[0] == '.')continue;
                if(++n > 0) break;
            }
            closedir(d);
        }
        else{
            return false;
        } 
        if (n == 0) //Directory Empty
            return true;
        else
            return false;
}

The problem is that after a day or two of the daemon working these functions start constantly returning FALSE (both of them) when they should return true. I have the suspicion that the DIR * pointer is not closing correctly but i couldn't manage to fix it.

What am i doing wrong here?

Thank you very much!

Aucun commentaire:

Enregistrer un commentaire