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);
}
// Create user path if it doesn't already exist
if (!DirectoryExists(UserDataPath))
CreateDirectory(UserDataPath, NULL);
CreatePath(UserDataPath);
}
static bool GrabOldPlayerFile(const char *fn)
@ -746,7 +745,7 @@ const char *C4Config::AtScreenshotPath(const char *szFilename)
if(len = SLen(AtPathFilename))
if(AtPathFilename[len-1] == DirectorySeparator)
AtPathFilename[len-1] = '\0';
if (!DirectoryExists(AtPathFilename) && !CreateDirectory(AtPathFilename, NULL))
if (!CreatePath(AtPathFilename))
{
SCopy(General.ExePath, General.ScreenshotPath, CFG_MaxString-1);
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)
{
// Create directory if needed
if (!DirectoryExists(strDirectory))
if (!CreateDirectory(strDirectory, NULL))
return FALSE;
if (!CreatePath(strDirectory))
return FALSE;
// Create title component if needed
char lang[3]; SCopy(Config.General.Language, lang, 2);
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];
SCopy(szFilename,szFoldername,_MAX_PATH);
MakeTempFilename(szFoldername);
if (!CreateDirectory(szFoldername,NULL)) { hGroup.Close(); return FALSE; }
if (!CreatePath(szFoldername)) { hGroup.Close(); return FALSE; }
// Extract files to folder
if (!hGroup.Extract("*",szFoldername)) { hGroup.Close(); return FALSE; }

View File

@ -1706,7 +1706,7 @@ bool C4Network2ResList::CreateNetworkFolder()
// does not exist?
if(access(szNetworkPath, 00))
{
if(!CreateDirectory(szNetworkPath, 0))
if(!CreatePath(szNetworkPath))
{ LogFatal("Network: could not create network path!"); return false; }
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)
if (!szFilename)
{
if (!CreateDirectory(szTmp, NULL)) return false;
if (!CreatePath(szTmp)) return false;
C4Group hGroup;
if (!hGroup.Open(szTmp, TRUE)) { EraseItem(szTmp); return false; }
// 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))
{
// 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
StdStrBuf strTarget; strTarget.Format("%s%s", Config.GetKeyPath(), GetFilename(strKeyFilename.getData()));
if (!MoveItem(strKeyFilename.getData(), strTarget.getData()))
@ -451,7 +451,7 @@ void C4StartupMainDlg::HandleIncomingKeyfile(const char *strIncomingKey)
// Say thank you
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
if (!DirectoryExists(Config.GetKeyPath())) CreateDirectory(Config.GetKeyPath(), NULL);
CreatePath(Config.GetKeyPath());
// Now try to copy it into the key path (preferred)
StdStrBuf strTarget; strTarget.Format("%s%s", Config.GetKeyPath(), GetFilename(strKeyFilename.getData()));
if (!CopyItem(strKeyFilename.getData(), strTarget.getData()))

View File

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

View File

@ -27,6 +27,7 @@
#include <Standard.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#ifdef _WIN32
#include <io.h>
@ -38,19 +39,27 @@
#define _MAX_PATH PATH_MAX
#define _MAX_FNAME NAME_MAX
bool CreateDirectory(const char * pathname, void* = 0);
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists);
#endif
#ifdef _WIN32
static const char *DirectorySeparators = "/\\";
#define DirectorySeparator '\\'
#define AltDirectorySeparator '/'
#else
static const char *DirectorySeparators = "/";
#define DirectorySeparator '/'
#define AltDirectorySeparator '\\'
#define DIRECTORYSEPARATORS "/"
#endif
#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();
bool SetWorkingDirectory(const char *szPath);
char *GetFilename(char *path);

View File

@ -43,6 +43,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <zlib.h>
#include <string>
/* Path & Filename */
@ -560,26 +561,53 @@ bool SetWorkingDirectory(const char *path)
#endif
}
#ifndef _WIN32
// CreateDirectory: true on success
bool CreateDirectory(const char * pathname, void*)
bool CreatePath(const std::string &path)
{
assert(!path.empty());
#ifdef _WIN32
if (!CreateDirectory(path.c_str(), NULL))
{
int r = mkdir(pathname,S_IREAD | S_IWRITE | S_IEXEC);
if (r && errno == ENOENT)
DWORD err = GetLastError();
switch(err)
{
StdCopyStrBuf parent(pathname);
char * slash = strrchr(parent.getMData(), '/');
if (slash)
case ERROR_PATH_NOT_FOUND:
break;
case ERROR_ALREADY_EXISTS:
return true;
default:
// Something major has happened: Log
{
*slash = 0;
CreateDirectory(parent.getData());
return !mkdir(pathname,S_IREAD | S_IWRITE | S_IEXEC);
LPSTR str;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
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
// 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)
{