StdFile: Replace CreateDirectory

Removed CreateDirectory, replaced with CreatePath, which creates parent
directories recursively if needed
Nicolas Hake 2009-07-10 21:33:55 +02:00
parent 2a38d0056d
commit d6337554bc
8 changed files with 61 additions and 26 deletions

View File

@ -651,8 +651,7 @@ void C4ConfigGeneral::DeterminePaths(BOOL forceWorkingDirectory)
AppendBackslash(ScreenshotPath); AppendBackslash(ScreenshotPath);
} }
// Create user path if it doesn't already exist // Create user path if it doesn't already exist
if (!DirectoryExists(UserDataPath)) CreatePath(UserDataPath);
CreateDirectory(UserDataPath, NULL);
} }
static bool GrabOldPlayerFile(const char *fn) static bool GrabOldPlayerFile(const char *fn)
@ -746,7 +745,7 @@ const char *C4Config::AtScreenshotPath(const char *szFilename)
if(len = SLen(AtPathFilename)) if(len = SLen(AtPathFilename))
if(AtPathFilename[len-1] == DirectorySeparator) if(AtPathFilename[len-1] == DirectorySeparator)
AtPathFilename[len-1] = '\0'; AtPathFilename[len-1] = '\0';
if (!DirectoryExists(AtPathFilename) && !CreateDirectory(AtPathFilename, NULL)) if (!CreatePath(AtPathFilename))
{ {
SCopy(General.ExePath, General.ScreenshotPath, CFG_MaxString-1); SCopy(General.ExePath, General.ScreenshotPath, CFG_MaxString-1);
SCopy(General.ScreenshotPath,AtPathFilename,_MAX_PATH); SCopy(General.ScreenshotPath,AtPathFilename,_MAX_PATH);
@ -761,9 +760,8 @@ const char *C4Config::AtScreenshotPath(const char *szFilename)
BOOL C4ConfigGeneral::CreateSaveFolder(const char *strDirectory, const char *strLanguageTitle) BOOL C4ConfigGeneral::CreateSaveFolder(const char *strDirectory, const char *strLanguageTitle)
{ {
// Create directory if needed // Create directory if needed
if (!DirectoryExists(strDirectory)) if (!CreatePath(strDirectory))
if (!CreateDirectory(strDirectory, NULL)) return FALSE;
return FALSE;
// Create title component if needed // Create title component if needed
char lang[3]; SCopy(Config.General.Language, lang, 2); char lang[3]; SCopy(Config.General.Language, lang, 2);
StdStrBuf strTitleFile; strTitleFile.Format("%s%c%s", strDirectory, DirectorySeparator, C4CFN_WriteTitle); StdStrBuf strTitleFile; strTitleFile.Format("%s%c%s", strDirectory, DirectorySeparator, C4CFN_WriteTitle);

View File

@ -378,7 +378,7 @@ BOOL C4Group_UnpackDirectory(const char *szFilename)
char szFoldername[_MAX_PATH+1]; char szFoldername[_MAX_PATH+1];
SCopy(szFilename,szFoldername,_MAX_PATH); SCopy(szFilename,szFoldername,_MAX_PATH);
MakeTempFilename(szFoldername); MakeTempFilename(szFoldername);
if (!CreateDirectory(szFoldername,NULL)) { hGroup.Close(); return FALSE; } if (!CreatePath(szFoldername)) { hGroup.Close(); return FALSE; }
// Extract files to folder // Extract files to folder
if (!hGroup.Extract("*",szFoldername)) { hGroup.Close(); return FALSE; } if (!hGroup.Extract("*",szFoldername)) { hGroup.Close(); return FALSE; }

View File

@ -1706,7 +1706,7 @@ bool C4Network2ResList::CreateNetworkFolder()
// does not exist? // does not exist?
if(access(szNetworkPath, 00)) if(access(szNetworkPath, 00))
{ {
if(!CreateDirectory(szNetworkPath, 0)) if(!CreatePath(szNetworkPath))
{ LogFatal("Network: could not create network path!"); return false; } { LogFatal("Network: could not create network path!"); return false; }
return true; return true;
} }

View File

@ -727,7 +727,7 @@ bool C4ScenarioSection::EnsureTempStore(bool fExtractLandscape, bool fExtractObj
// main section: extract section files from main scenario group (create group as open dir) // main section: extract section files from main scenario group (create group as open dir)
if (!szFilename) if (!szFilename)
{ {
if (!CreateDirectory(szTmp, NULL)) return false; if (!CreatePath(szTmp)) return false;
C4Group hGroup; C4Group hGroup;
if (!hGroup.Open(szTmp, TRUE)) { EraseItem(szTmp); return false; } if (!hGroup.Open(szTmp, TRUE)) { EraseItem(szTmp); return false; }
// extract all desired section files // extract all desired section files

View File

@ -434,7 +434,7 @@ void C4StartupMainDlg::HandleIncomingKeyfile(const char *strIncomingKey)
if (GetScreen()->ShowMessageModal(strMessage.getData(), LoadResStr("IDS_DLG_REGISTRATION"), C4GUI::MessageDialog::btnYesNo, C4GUI::Ico_Confirm)) if (GetScreen()->ShowMessageModal(strMessage.getData(), LoadResStr("IDS_DLG_REGISTRATION"), C4GUI::MessageDialog::btnYesNo, C4GUI::Ico_Confirm))
{ {
// Create key path if it doesn't already exist // Create key path if it doesn't already exist
if (!DirectoryExists(Config.GetKeyPath())) CreateDirectory(Config.GetKeyPath(), NULL); CreatePath(Config.GetKeyPath());
// Move key into key path // Move key into key path
StdStrBuf strTarget; strTarget.Format("%s%s", Config.GetKeyPath(), GetFilename(strKeyFilename.getData())); StdStrBuf strTarget; strTarget.Format("%s%s", Config.GetKeyPath(), GetFilename(strKeyFilename.getData()));
if (!MoveItem(strKeyFilename.getData(), strTarget.getData())) if (!MoveItem(strKeyFilename.getData(), strTarget.getData()))
@ -451,7 +451,7 @@ void C4StartupMainDlg::HandleIncomingKeyfile(const char *strIncomingKey)
// Say thank you // Say thank you
GetScreen()->ShowMessageModal(LoadResStr("IDS_CTL_REGISTERED"), LoadResStr("IDS_DLG_REGISTRATION"), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Notify); GetScreen()->ShowMessageModal(LoadResStr("IDS_CTL_REGISTERED"), LoadResStr("IDS_DLG_REGISTRATION"), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Notify);
// Create key path if it doesn't already exist // Create key path if it doesn't already exist
if (!DirectoryExists(Config.GetKeyPath())) CreateDirectory(Config.GetKeyPath(), NULL); CreatePath(Config.GetKeyPath());
// Now try to copy it into the key path (preferred) // Now try to copy it into the key path (preferred)
StdStrBuf strTarget; strTarget.Format("%s%s", Config.GetKeyPath(), GetFilename(strKeyFilename.getData())); StdStrBuf strTarget; strTarget.Format("%s%s", Config.GetKeyPath(), GetFilename(strKeyFilename.getData()));
if (!CopyItem(strKeyFilename.getData(), strTarget.getData())) if (!CopyItem(strKeyFilename.getData(), strTarget.getData()))

View File

@ -328,7 +328,7 @@ BOOL C4UpdatePackage::Execute(C4Group *pGroup)
// GrpUpdate -> file must exist // GrpUpdate -> file must exist
if(GrpUpdate) return FALSE; if(GrpUpdate) return FALSE;
// create dir // create dir
CreateDirectory(strTarget, NULL); CreatePath(strTarget);
} }
*p = '\\'; lp = p + 1; *p = '\\'; lp = p + 1;
} }

View File

@ -27,6 +27,7 @@
#include <Standard.h> #include <Standard.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#ifdef _WIN32 #ifdef _WIN32
#include <io.h> #include <io.h>
@ -38,19 +39,27 @@
#define _MAX_PATH PATH_MAX #define _MAX_PATH PATH_MAX
#define _MAX_FNAME NAME_MAX #define _MAX_FNAME NAME_MAX
bool CreateDirectory(const char * pathname, void* = 0);
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists); bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists);
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
static const char *DirectorySeparators = "/\\";
#define DirectorySeparator '\\' #define DirectorySeparator '\\'
#define AltDirectorySeparator '/' #define AltDirectorySeparator '/'
#else #else
static const char *DirectorySeparators = "/";
#define DirectorySeparator '/' #define DirectorySeparator '/'
#define AltDirectorySeparator '\\' #define AltDirectorySeparator '\\'
#define DIRECTORYSEPARATORS "/"
#endif #endif
#define Wildcard '*' #define Wildcard '*'
/** Create a directory and all of its parents.
* \p[in] path Directory to create
* \returns true on success, false otherwise.
*/
bool CreatePath(const std::string &path);
const char *GetWorkingDirectory(); const char *GetWorkingDirectory();
bool SetWorkingDirectory(const char *szPath); bool SetWorkingDirectory(const char *szPath);
char *GetFilename(char *path); char *GetFilename(char *path);

View File

@ -43,6 +43,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <zlib.h> #include <zlib.h>
#include <string>
/* Path & Filename */ /* Path & Filename */
@ -560,26 +561,53 @@ bool SetWorkingDirectory(const char *path)
#endif #endif
} }
#ifndef _WIN32 bool CreatePath(const std::string &path)
// CreateDirectory: true on success {
bool CreateDirectory(const char * pathname, void*) assert(!path.empty());
#ifdef _WIN32
if (!CreateDirectory(path.c_str(), NULL))
{ {
int r = mkdir(pathname,S_IREAD | S_IWRITE | S_IEXEC); DWORD err = GetLastError();
if (r && errno == ENOENT) switch(err)
{ {
StdCopyStrBuf parent(pathname); case ERROR_PATH_NOT_FOUND:
char * slash = strrchr(parent.getMData(), '/'); break;
if (slash) case ERROR_ALREADY_EXISTS:
return true;
default:
// Something major has happened: Log
{ {
*slash = 0; LPSTR str;
CreateDirectory(parent.getData()); if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
return !mkdir(pathname,S_IREAD | S_IWRITE | S_IEXEC); NULL, err, 0, (LPSTR)&str, 0, NULL))
{
LogF("CreateDirectory failed: %s", str);
LocalFree(str);
}
return false;
} }
} }
// mkdir: 0 on success }
return !r; #else
if (!mkdir(path.c_str(), S_IREAD | S_IWRITE | S_IEXEC))
return true;
switch(errno)
{
case ENOENT:
break;
case EEXIST:
// FIXME: Check whether the path is blocked by a non-directory
return true;
default:
return false;
} }
#endif #endif
// Recursively create parent path
std::string::size_type slash = path.find_last_of(DirectorySeparators);
if (slash == 0 || slash == std::string::npos)
return false;
return CreatePath(path.substr(0, slash)) && CreatePath(path);
}
bool DirectoryExists(const char *szFilename) bool DirectoryExists(const char *szFilename)
{ {