mirror of
https://github.com/libretro/scummvm.git
synced 2025-02-01 08:23:15 +00:00
POSIX: Move assureDirectoryExists to posix-fs{.h,.cpp}.
This commit is contained in:
parent
dde89c36f5
commit
2622cded72
@ -38,6 +38,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef __OS2__
|
||||
#define INCL_DOS
|
||||
@ -251,4 +252,67 @@ Common::WriteStream *POSIXFilesystemNode::createWriteStream() {
|
||||
return StdioStream::makeFromPath(getPath(), true);
|
||||
}
|
||||
|
||||
namespace Posix {
|
||||
|
||||
bool assureDirectoryExists(const Common::String &dir, const char *prefix) {
|
||||
struct stat sb;
|
||||
|
||||
// Check whether the prefix exists if one is supplied.
|
||||
if (prefix) {
|
||||
if (stat(prefix, &sb) != 0) {
|
||||
return false;
|
||||
} else if (!S_ISDIR(sb.st_mode)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtain absolute path.
|
||||
Common::String path;
|
||||
if (prefix) {
|
||||
path = prefix;
|
||||
path += '/';
|
||||
path += dir;
|
||||
} else {
|
||||
path = dir;
|
||||
}
|
||||
|
||||
path = Common::normalizePath(path, '/');
|
||||
|
||||
const Common::String::iterator end = path.end();
|
||||
Common::String::iterator cur = path.begin();
|
||||
if (*cur == '/')
|
||||
++cur;
|
||||
|
||||
do {
|
||||
if (cur + 1 != end) {
|
||||
if (*cur != '/') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// It is kind of ugly and against the purpose of Common::String to
|
||||
// insert 0s inside, but this is just for a local string and
|
||||
// simplifies the code a lot.
|
||||
*cur = '\0';
|
||||
}
|
||||
|
||||
if (mkdir(path.c_str(), 0755) != 0) {
|
||||
if (errno == EEXIST) {
|
||||
if (stat(path.c_str(), &sb) != 0) {
|
||||
return false;
|
||||
} else if (!S_ISDIR(sb.st_mode)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*cur = '/';
|
||||
} while (cur++ != end);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // End of namespace Posix
|
||||
|
||||
#endif //#if defined(POSIX)
|
||||
|
@ -81,4 +81,18 @@ private:
|
||||
virtual void setFlags();
|
||||
};
|
||||
|
||||
namespace Posix {
|
||||
|
||||
/**
|
||||
* Assure that a directory path exists.
|
||||
*
|
||||
* @param dir The path which is required to exist.
|
||||
* @param prefix An (optional) prefix which should not be created if non existent.
|
||||
* prefix is prepended to dir if supplied.
|
||||
* @return true in case the directoy exists (or was created), false otherwise.
|
||||
*/
|
||||
bool assureDirectoryExists(const Common::String &dir, const char *prefix = nullptr);
|
||||
|
||||
} // End of namespace Posix
|
||||
|
||||
#endif
|
||||
|
@ -33,82 +33,13 @@
|
||||
#include "backends/platform/sdl/posix/posix.h"
|
||||
#include "backends/saves/posix/posix-saves.h"
|
||||
#include "backends/fs/posix/posix-fs-factory.h"
|
||||
#include "backends/fs/posix/posix-fs.h"
|
||||
#include "backends/taskbar/unity/unity-taskbar.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
/**
|
||||
* Assure that a directory path exists.
|
||||
*
|
||||
* @param dir The path which is required to exist.
|
||||
* @param prefix An (optional) prefix which should not be created if non existent.
|
||||
* prefix is prepended to dir if supplied.
|
||||
* @return true in case the directoy exists (or was created), false otherwise.
|
||||
*/
|
||||
bool assureDirectoryExists(const Common::String &dir, const char *prefix = nullptr) {
|
||||
struct stat sb;
|
||||
|
||||
// Check whether the prefix exists if one is supplied.
|
||||
if (prefix) {
|
||||
if (stat(prefix, &sb) != 0) {
|
||||
return false;
|
||||
} else if (!S_ISDIR(sb.st_mode)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Obtain absolute path.
|
||||
Common::String path;
|
||||
if (prefix) {
|
||||
path = prefix;
|
||||
path += '/';
|
||||
path += dir;
|
||||
} else {
|
||||
path = dir;
|
||||
}
|
||||
|
||||
path = Common::normalizePath(path, '/');
|
||||
|
||||
const Common::String::iterator end = path.end();
|
||||
Common::String::iterator cur = path.begin();
|
||||
if (*cur == '/')
|
||||
++cur;
|
||||
|
||||
do {
|
||||
if (cur + 1 != end) {
|
||||
if (*cur != '/') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// It is kind of ugly and against the purpose of Common::String to
|
||||
// insert 0s inside, but this is just for a local string and
|
||||
// simplifies the code a lot.
|
||||
*cur = '\0';
|
||||
}
|
||||
|
||||
if (mkdir(path.c_str(), 0755) != 0) {
|
||||
if (errno == EEXIST) {
|
||||
if (stat(path.c_str(), &sb) != 0) {
|
||||
return false;
|
||||
} else if (!S_ISDIR(sb.st_mode)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
*cur = '/';
|
||||
} while (cur++ != end);
|
||||
|
||||
return true;
|
||||
}
|
||||
} // End of anonymous namespace
|
||||
|
||||
OSystem_POSIX::OSystem_POSIX(Common::String baseConfigName)
|
||||
:
|
||||
_baseConfigName(baseConfigName) {
|
||||
@ -181,7 +112,7 @@ Common::String OSystem_POSIX::getDefaultConfigFileName() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (assureDirectoryExists(".config", envVar)) {
|
||||
if (Posix::assureDirectoryExists(".config", envVar)) {
|
||||
prefix = envVar;
|
||||
prefix += "/.config";
|
||||
}
|
||||
@ -189,7 +120,7 @@ Common::String OSystem_POSIX::getDefaultConfigFileName() {
|
||||
prefix = envVar;
|
||||
}
|
||||
|
||||
if (!prefix.empty() && assureDirectoryExists("scummvm", prefix.c_str())) {
|
||||
if (!prefix.empty() && Posix::assureDirectoryExists("scummvm", prefix.c_str())) {
|
||||
prefix += "/scummvm";
|
||||
}
|
||||
#endif
|
||||
@ -239,7 +170,7 @@ Common::WriteStream *OSystem_POSIX::createLogFile() {
|
||||
logFile += "scummvm/logs";
|
||||
#endif
|
||||
|
||||
if (!assureDirectoryExists(logFile, prefix)) {
|
||||
if (!Posix::assureDirectoryExists(logFile, prefix)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user