bool FileSystem::ForceDirectories(const char* path, CString& errmsg) { errmsg.Clear(); BString<1024> normPath = path; NormalizePathSeparators(normPath); int len = strlen(normPath); if (len > 3 && normPath[len - 1] == PATH_SEPARATOR) { normPath[len - 1] = '\0'; } if (DirectoryExists(normPath)) { return true; } if (FileExists(normPath)) { errmsg.Format("path %s is not a directory", *normPath); return false; } if (strlen(normPath) > 2) { BString<1024> parentPath = *normPath; char* p = (char*)strrchr(parentPath, PATH_SEPARATOR); if (p) { if (p - parentPath == 2 && parentPath[1] == ':' && strlen(parentPath) > 2) { parentPath[3] = '\0'; } else { *p = '\0'; } if (strlen(parentPath) != strlen(path) && !ForceDirectories(parentPath, errmsg)) { return false; } } if (_wmkdir(UtfPathToWidePath(normPath)) != 0 && errno != EEXIST) { errmsg.Format("could not create directory %s: %s", *normPath, *GetLastErrorMessage()); return false; } if (DirectoryExists(normPath)) { return true; } if (FileExists(normPath)) { errmsg.Format("path %s is not a directory", *normPath); return false; } } return false; }
bool Util::ForceDirectories(const char* szPath, char* szErrBuf, int iBufSize) { *szErrBuf = '\0'; char szSysErrStr[256]; char szNormPath[1024]; strncpy(szNormPath, szPath, 1024); szNormPath[1024-1] = '\0'; NormalizePathSeparators(szNormPath); int iLen = strlen(szNormPath); if ((iLen > 0) && szNormPath[iLen-1] == PATH_SEPARATOR #ifdef WIN32 && iLen > 3 #endif ) { szNormPath[iLen-1] = '\0'; } struct stat buffer; bool bOK = !stat(szNormPath, &buffer); if (!bOK && errno != ENOENT) { snprintf(szErrBuf, iBufSize, "could not read information for directory %s: errno %i, %s", szNormPath, errno, GetLastErrorMessage(szSysErrStr, sizeof(szSysErrStr))); szErrBuf[iBufSize-1] = 0; return false; } if (bOK && !S_ISDIR(buffer.st_mode)) { snprintf(szErrBuf, iBufSize, "path %s is not a directory", szNormPath); szErrBuf[iBufSize-1] = 0; return false; } if (!bOK #ifdef WIN32 && strlen(szNormPath) > 2 #endif ) { char szParentPath[1024]; strncpy(szParentPath, szNormPath, 1024); szParentPath[1024-1] = '\0'; char* p = (char*)strrchr(szParentPath, PATH_SEPARATOR); if (p) { #ifdef WIN32 if (p - szParentPath == 2 && szParentPath[1] == ':' && strlen(szParentPath) > 2) { szParentPath[3] = '\0'; } else #endif { *p = '\0'; } if (strlen(szParentPath) != strlen(szPath) && !ForceDirectories(szParentPath, szErrBuf, iBufSize)) { return false; } } if (mkdir(szNormPath, S_DIRMODE) != 0 && errno != EEXIST) { snprintf(szErrBuf, iBufSize, "could not create directory %s: %s", szNormPath, GetLastErrorMessage(szSysErrStr, sizeof(szSysErrStr))); szErrBuf[iBufSize-1] = 0; return false; } if (stat(szNormPath, &buffer) != 0) { snprintf(szErrBuf, iBufSize, "could not read information for directory %s: %s", szNormPath, GetLastErrorMessage(szSysErrStr, sizeof(szSysErrStr))); szErrBuf[iBufSize-1] = 0; return false; } if (!S_ISDIR(buffer.st_mode)) { snprintf(szErrBuf, iBufSize, "path %s is not a directory", szNormPath); szErrBuf[iBufSize-1] = 0; return false; } } return true; }
bool FileSystem::ForceDirectories(const char* path, CString& errmsg) { errmsg.Clear(); BString<1024> normPath = path; NormalizePathSeparators(normPath); int len = strlen(normPath); if ((len > 0) && normPath[len - 1] == PATH_SEPARATOR) { normPath[len - 1] = '\0'; } struct stat buffer; bool ok = !stat(normPath, &buffer); if (!ok && errno != ENOENT) { errmsg.Format("could not read information for directory %s: errno %i, %s", *normPath, errno, *GetLastErrorMessage()); return false; } if (ok && !S_ISDIR(buffer.st_mode)) { errmsg.Format("path %s is not a directory", *normPath); return false; } if (!ok) { BString<1024> parentPath = *normPath; char* p = (char*)strrchr(parentPath, PATH_SEPARATOR); if (p) { *p = '\0'; if (strlen(parentPath) != strlen(path) && !ForceDirectories(parentPath, errmsg)) { return false; } } if (mkdir(normPath, S_DIRMODE) != 0 && errno != EEXIST) { errmsg.Format("could not create directory %s: %s", *normPath, *GetLastErrorMessage()); return false; } if (stat(normPath, &buffer) != 0) { errmsg.Format("could not read information for directory %s: %s", *normPath, *GetLastErrorMessage()); return false; } if (!S_ISDIR(buffer.st_mode)) { errmsg.Format("path %s is not a directory", *normPath); return false; } } return true; }