void TestFixture::TemporaryDirectoryCreator::Delete() { // change the directory back to the original directory if (!fOldWorkingDirectory.empty()) { chdir(fOldWorkingDirectory.c_str()); fOldWorkingDirectory.clear(); } // remove the temporary directory if (!fTemporaryDirectory.empty()) { std::string directory = fTemporaryDirectory; fTemporaryDirectory.clear(); RemoveRecursively(directory); } }
BOOL PathNameEx::RemoveRecursively(const String_256& rPath) { PORTNOTETRACE("other","PathNameEx::RemoveRecursively - do nothing"); #ifndef EXCLUDE_FROM_XARALX String_256 strFilename(rPath); strFilename.toLower(); // See if the path points to a file (the easy case) or a directory if (strFilename[strFilename.Length() - 1] == chPathSep) { strFilename.Remove(strFilename.Length() - 1, 1); goto DIRECTORY; } struct _stat fileData; if (_stat((TCHAR*) strFilename, &fileData)) { if (errno == ENOENT) { ERROR3("Filename or path not found"); } else { ERROR3("_stat() failed with an unknown error"); } return FALSE; } if (fileData.st_mode & _S_IFDIR) // directory { DIRECTORY: // Make sure the directory is not the current one TCHAR tchbuff[_MAX_PATH]; if (_getcwd(tchbuff, _MAX_PATH) == NULL) { ERROR3("Can't get working directory"); return FALSE; } if (strstr(_strlwr(tchbuff), (TCHAR*) strFilename)) { // change to upper dir (we should never attempt to delete the root directory!) PathName path(strFilename); if (_chdir((TCHAR*) String_256(path.GetLocation(FALSE)))) { ERROR3("Can't change directory"); return FALSE; } } // Try to remove it in the hope that it's empty if (_rmdir((TCHAR*) strFilename) == -1) { if (errno == ENOTEMPTY || errno == EACCES) { _finddata_t findData; String_256 strSearchPattern(strFilename); strSearchPattern += chPathSep; strSearchPattern += _T("*"); // add wildcard INT32 hSearch = _findfirst(strSearchPattern, &findData); if (hSearch == -1) return FALSE; do { if (!(strcmp(findData.name, _T(".")) && strcmp(findData.name, _T("..")))) continue; // skip this directory (.) or its parent (..) String_256 strFoundFile(strFilename); strFoundFile += chPathSep; strFoundFile += findData.name; RemoveRecursively(strFoundFile); } while (_findnext(hSearch, &findData) == 0); _findclose(hSearch); return (_rmdir((TCHAR*) strFilename) != -1); } else { return FALSE; // probably invalid path } } else return TRUE; // succedded } else if (fileData.st_mode & _S_IFREG) // file return (remove((TCHAR*) strFilename) != -1); else #endif return FALSE; }