コード例 #1
0
bool CFileHelpers::removeDir(const char *Dir)
{
	CFileHelpers* fh = CFileHelpers::getInstance();
	fh->clearDebugInfo();
	DIR *dir;
	struct dirent *entry;
	char path[PATH_MAX];

	dir = opendir(Dir);
	if (dir == NULL) {
		if (errno == ENOENT)
			return true;
		if (!fh->getConsoleQuiet())
			dprintf(DEBUG_NORMAL, "[CFileHelpers %s] remove directory %s: %s\n", __func__, Dir, strerror(errno));
		char buf[1024];
		memset(buf, '\0', sizeof(buf));
		snprintf(buf, sizeof(buf)-1, "remove directory %s: %s", Dir, strerror(errno));
		fh->setDebugInfo(buf, __path_file__, __func__, __LINE__);
		return false;
	}
	while ((entry = readdir(dir)) != NULL) {
		if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
			snprintf(path, (size_t) PATH_MAX, "%s/%s", Dir, entry->d_name);
			if (entry->d_type == DT_DIR)
				removeDir(path);
			else
				unlink(path);
		}
	}
	closedir(dir);
	rmdir(Dir);

	errno = 0;
	return true;
}
コード例 #2
0
// returns:	 true - success.
//		 false - errno is set
bool CFileHelpers::createDir(string& Dir, mode_t mode)
{
	CFileHelpers* fh = CFileHelpers::getInstance();
	fh->clearDebugInfo();
	int res = 0;
	for(string::iterator iter = Dir.begin() ; iter != Dir.end();) {
		string::iterator newIter = find(iter, Dir.end(), '/' );
		string newPath = string( Dir.begin(), newIter );
		if(!newPath.empty() && !file_exists(newPath.c_str())) {
			res = mkdir( newPath.c_str(), mode);
			if (res == -1) {
				if (errno == EEXIST) {
					res = 0;
				} else {
					// We can assume that if an error
					// occured, following will fail too,
					// so break here.
					if (!fh->getConsoleQuiet())
						dprintf(DEBUG_NORMAL, "[CFileHelpers %s] creating directory %s: %s\n", __func__, newPath.c_str(), strerror(errno));
					char buf[1024];
					memset(buf, '\0', sizeof(buf));
					snprintf(buf, sizeof(buf)-1, "creating directory %s: %s", newPath.c_str(), strerror(errno));
					fh->setDebugInfo(buf, __path_file__, __func__, __LINE__);
					break;
				}
			}
		}
		iter = newIter;
		if(newIter != Dir.end())
			++ iter;
	}

	return (res == 0 ? true : false);
}