[C++] Recursief directory del

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Preau
  • Registratie: Oktober 2010
  • Niet online
Ik ben met C++ een programma aan het schrijven dat onder andere recursief een directory moet kunnen legen en dan weggooien.

Op Windows lukt het zolang er geen subdirectories zijn goed, als er een subdirectory is dan lukt het niet omdat de diepste subdirectory pas verwijderd wordt als ik het programma afsluit.
Dit komt omdat de directory dan nog ergens open staat maar ik heb hem volgens mij overal netjes weer gesloten, maar toch ga ik ergens de fout in.

De recursieve delete methode:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Delete a DIR, recursive
void Filehandler::deleteDir(std::string filePath) {
    char charPath[FILENAME_MAX];
    strcpy(charPath, filePath.c_str());
    //Make sure it's a dir
    if(!Filehandler::isFile(charPath) && Filehandler::fileAvailable(charPath)) {
        //Init vars
        DIR *dir = opendir(charPath);
        struct dirent *drnt;
        //Read directory
        if(dir != 0) {
            while(drnt = readdir(dir)) {
                if(strcmp(drnt->d_name, ".") == 0 || strcmp(drnt->d_name, "..") == 0) {
                    //Used for navigating in windows only, no need to delete
                    continue;
                }

                //Create the new path
                std::string currentPath(filePath);
                currentPath += SEPARATOR;
                currentPath.append(drnt->d_name);

                if(Filehandler::isFile(currentPath)) {
                    //Is File, delete it
                    std::remove(currentPath.c_str());
                } else {
                    //Is Directory, run method again for directory
                    Filehandler::deleteDir(currentPath);
                }
            }
            closedir(dir);
            //Delete self
            rmdir(filePath.c_str());
        }
    }
}


En hier de 2 eigen methodes die gebruikt worden: isFile en fileAvailable:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Check if file or directory
bool Filehandler::isFile(std::string filePath) {
    struct stat statbuf;
    stat((char *) filePath.c_str(), &statbuf);
    if(S_ISDIR(statbuf.st_mode)) {
        //is directory
        return false;
    } else {
        //is probably file, always checked if really exists fileAvailable
        return true;
    }
}

//Check if a file or directory exists
bool Filehandler::fileAvailable(char *filePath) {
    if(Filehandler::isFile(std::string(filePath))) {
        //File found, check if able to be opened
        ifstream file(filePath, ios::in|ios::binary|ios::ate);
        if(file.is_open()) {
            //File was able to be openend
            file.close();
            return true;
        } else {
            file.close();
            return false;
        }
    } else {
        //Check if dir exists
        DIR *dir = opendir(filePath);
        if(dir != 0) {
            return true;
        }
        closedir(dir);
    }
    //File not found or unable to be opened
    return false;
}


PS dit programma moet ook op UNIX gaan werken, daarom gebruik ik dirent voor het doorlopen van mappen en rmdir voor het verwijderen.

Acties:
  • 0 Henk 'm!

  • Preau
  • Registratie: Oktober 2010
  • Niet online
Je zal het altijd zien, meteen na het posten zie ik de fout.

Bij de fileAvailable staat voor de return op regel 31 geen closedir en dus wordt de dir nooit goed gesloten.

[ Voor 5% gewijzigd door Preau op 12-01-2013 12:12 ]


Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...