bool CrashHandlerImpl::deleteOldReports(size_t nbReportsToKeep)
{
	Autolock autoLock(&m_Lock);

    if(strlen(m_pCrashInfo->crashReportFolder) == 0) {
		LogError << "Report location has not been specified";
		return false;
	}
	
	// Exit if there is no crash report folder yet...
	fs::path location(m_pCrashInfo->crashReportFolder);
	if(!fs::is_directory(location))
		return true;

	typedef std::map<std::string, fs::path> CrashReportMap;
	CrashReportMap oldCrashes;

	for(fs::directory_iterator it(location); !it.end(); ++it) {
		fs::path folderPath = location / it.name();
		if(fs::is_directory(folderPath)) {
			// Ensure this directory really contains a crash report...
			fs::path crashManifestPath = folderPath / "crash.xml";
			if(fs::is_regular_file(crashManifestPath)) {
				oldCrashes[it.name()] = folderPath;
			}
		}
	}

	// Nothing to delete
	if(nbReportsToKeep >= oldCrashes.size())
		return true;

	int nbReportsToDelete = oldCrashes.size() - nbReportsToKeep; 

	// std::map will return the oldest reports first as folders are named "yyyy.MM.dd hh.mm.ss"
	CrashReportMap::const_iterator it = oldCrashes.begin();
	for(int i = 0; i < nbReportsToDelete; ++i, ++it) {
		fs::remove_all(it->second);
	}
	
	return true;
}
Exemplo n.º 2
0
bool CrashHandlerImpl::deleteOldReports(size_t nbReportsToKeep) {
	
	Autolock autoLock(&m_Lock);
	
	if(strlen(m_pCrashInfo->crashReportFolder) == 0) {
		LogError << "Report location has not been specified";
		return false;
	}
	
	// Exit if there is no crash report folder yet...
	fs::path location(m_pCrashInfo->crashReportFolder);
	if(!fs::is_directory(location)) {
		return true;
	}
	
	typedef std::multimap<std::time_t, fs::path> CrashReportMap;
	CrashReportMap oldCrashes;
	
	for(fs::directory_iterator it(location); !it.end(); ++it) {
		fs::path path = location / it.name();
		if(fs::is_directory(path)) {
			oldCrashes.insert(CrashReportMap::value_type(fs::last_write_time(path), path));
		} else {
			fs::remove(path);
		}
	}
	
	// Nothing to delete
	if(nbReportsToKeep >= oldCrashes.size()) {
		return true;
	}
	
	int nbReportsToDelete = oldCrashes.size() - nbReportsToKeep; 
	
	CrashReportMap::const_iterator it = oldCrashes.begin();
	for(int i = 0; i < nbReportsToDelete; ++i, ++it) {
		fs::remove_all(it->second);
	}
	
	return true;
}