Hi Edward_Lii,
Last time I have fixed some bugs under Windows in FileManager and commited it, but today after I update the working copy I found my changes are lost Maybe you forget update before commiting changes?
acme_pjz {l Wrote}:Hi Edward_Lii,
Last time I have fixed some bugs under Windows in FileManager and commited it, but today after I update the working copy I found my changes are lost Maybe you forget update before commiting changes?
bool removeDirectory(const char *path){
#ifdef WIN32
WIN32_FIND_DATAA f;
HANDLE h = FindFirstFileA((string(path)+"\\*").c_str(),&f);
#else
//Open the directory that needs to be removed.
DIR* d=opendir(path);
#endif
//Get the path length
size_t path_len = strlen(path);
//Boolean if the directory is emptied.
//True: succes False: failure
//Default is false because if the directory couldn't be opened we can return r(false).
bool r = false;
#ifdef WIN32
if(h!=NULL && h!=INVALID_HANDLE_VALUE) {
#else
//Check if the directory exists.
if(d) {
//Pointer to an entry of the directory.
struct dirent* p;
#endif
#ifdef WIN32
do{
#else
//Loop the entries of the directory that needs to be removed.
while(!r && (p=readdir(d))) {
#endif
//Boolean if the entry is deleted.
//True: succes False: failure
bool r2 = false;
char* buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
#ifdef WIN32
if (!strcmp(f.cFileName, ".") || !strcmp(f.cFileName, "..")) {
#else
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) {
#endif
//The filename is . or .. so we continue to the next entry.
continue;
} else {
#ifdef WIN32
//Get the length of the path + the directory entry name.
len = path_len + strlen(f.cFileName) + 2;
#else
//Get the length of the path + the directory entry name.
len = path_len + strlen(p->d_name) + 2;
#endif
buf = (char*) malloc(len);
if(buf) {
#ifdef WIN32
_snprintf(buf, len, "%s\\%s", path, f.cFileName);
if(f.dwFileAttributes &; FILE_ATTRIBUTE_DIRECTORY){
r2 = removeDirectory(buf);
}else{
r2 = true; //Remove file here? <-----
}
#else
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if(!stat(buf, &statbuf)){
//Check if the entry is a directory or a file.
if (S_ISDIR(statbuf.st_mode)){
r2 = removeDirectory(buf);
}else{
r2 = unlink(buf)!=0;
}
}
#endif
//Free the buf.
free(buf);
}
//We set r to r2 since r2 contains the status of the latest deletion.
// FIXME: should be r && r2 (??)
r = r2;
}
#ifdef WIN32
}while(FindNextFileA(h,&f));
FindClose(h);
#else
}
//Close the directory.
closedir(d);
#endif
}
//The while loop has ended, meaning we (tried) cleared the directory.
//If r is true, meaning no errors we can delete the directory.
// FIXME: should be 'r', not '!r' (??)
if(/*!*/r){
// FIXME: the return value of rmdir is 0 means successful (??)
r = rmdir(path)==0;
}
//Return the status.
return r;
}
acme_pjz {l Wrote}:1. In Linux code you don't have code for removing existing files either, if I read the code correctly. So in Windows I don't write code for removing files...
acme_pjz {l Wrote}:2. I haven't see the notes that the return value is "false" means successful, and "true" means failed How confusing So I'll check the Windows code soon...
acme_pjz {l Wrote}:3. Does the Linux code work now?
Users browsing this forum: No registered users and 1 guest