aboutsummaryrefslogtreecommitdiff
path: root/src/FileUtil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/FileUtil.cpp')
-rw-r--r--src/FileUtil.cpp154
1 files changed, 58 insertions, 96 deletions
diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp
index 77669e5..e29ed36 100644
--- a/src/FileUtil.cpp
+++ b/src/FileUtil.cpp
@@ -16,6 +16,19 @@
using namespace std;
+#if OS_FAMILY == OS_FAMILY_POSIX
+static int makedir(const _tinydir_char_t *dir)
+{
+ return mkdir(dir, S_IRWXU);
+}
+#elif OS_FAMILY == OS_FAMILY_WINDOWS
+
+static int makedir(const _tinydir_char_t *dir)
+{
+ return _wmkdir(dir);
+}
+#endif
+
namespace sibs
{
#if OS_FAMILY == OS_FAMILY_POSIX
@@ -263,45 +276,21 @@ namespace sibs
fclose(file);
return Result<bool>::Ok(true);
}
-#if OS_FAMILY == OS_FAMILY_POSIX
- Result<FileString> getHomeDir()
- {
- const char *homeDir = getenv("HOME");
- if(!homeDir)
- {
- passwd *pw = getpwuid(getuid());
- homeDir = pw->pw_dir;
- }
- return Result<FileString>::Ok(homeDir);
- }
-
- Result<FileString> getCwd()
- {
- FileString cwd;
- cwd.resize(_TINYDIR_PATH_MAX);
- if(getcwd(&cwd[0], _TINYDIR_PATH_MAX) != 0)
- {
- if(cwd.empty()) cwd = ".";
- cwd.resize(_tinydir_strlen(cwd.c_str()));
- return Result<FileString>::Ok(cwd);
- }
- return Result<FileString>::Err(strerror(errno));
- }
Result<bool> createDirectory(const _tinydir_char_t *path)
{
- size_t pathLength = strlen(path);
- if(pathLength > _TINYDIR_PATH_MAX - 1)
+ size_t pathLength = _tinydir_strlen(path);
+ if (pathLength > _TINYDIR_PATH_MAX - 1)
{
string errMsg = "Directory path too long: ";
- errMsg += string(path, pathLength);
+ errMsg += toUtf8(FileString(path, pathLength));
return Result<bool>::Err(errMsg, ENAMETOOLONG);
}
- if(mkdir(path, S_IRWXU) != 0)
+ if (makedir(path) != 0)
{
int error = errno;
- if(error != EEXIST)
+ if (error != EEXIST)
{
string errMsg = "Failed to create directory: ";
errMsg += toUtf8(path);
@@ -315,47 +304,46 @@ namespace sibs
Result<bool> createDirectoryRecursive(const _tinydir_char_t *path)
{
- char pathBuffer[_TINYDIR_PATH_MAX];
- size_t pathLength = strlen(path);
- if(pathLength > sizeof(pathBuffer) - 1)
+ _tinydir_char_t pathBuffer[_TINYDIR_PATH_MAX];
+ size_t pathLength = _tinydir_strlen(path);
+ if (pathLength > sizeof(pathBuffer) - 1)
{
string errMsg = "Directory path too long: ";
- errMsg += string(path, pathLength);
+ errMsg += toUtf8(FileString(path, pathLength));
return Result<bool>::Err(errMsg, ENAMETOOLONG);
}
- strcpy(pathBuffer, path);
+ _tinydir_strcpy(pathBuffer, path);
- char *p = pathBuffer;
- for(size_t i = 0; i < pathLength; ++i)
+ _tinydir_char_t *p = pathBuffer;
+ for (size_t i = 0; i < pathLength; ++i)
{
- char c = *p;
- if(i > 0 && (c == '/' || c == '\\'))
+ if (i > 0 && *p == '/')
{
*p = '\0';
- if(mkdir(pathBuffer, S_IRWXU) != 0)
+ if (makedir(pathBuffer) != 0)
{
int error = errno;
- if(error != EEXIST)
+ if (error != EEXIST)
{
string errMsg = "Failed to create directory: ";
- errMsg += pathBuffer;
+ errMsg += toUtf8(pathBuffer);
errMsg += "; reason: ";
errMsg += strerror(error);
return Result<bool>::Err(errMsg, error);
}
}
- *p = c;
+ *p = '/';
}
++p;
}
- if(mkdir(pathBuffer, S_IRWXU) != 0)
+ if (makedir(pathBuffer) != 0)
{
int error = errno;
- if(error != EEXIST)
+ if (error != EEXIST)
{
string errMsg = "Failed to create directory: ";
- errMsg += pathBuffer;
+ errMsg += toUtf8(pathBuffer);
errMsg += "; reason: ";
errMsg += strerror(error);
return Result<bool>::Err(errMsg, error);
@@ -365,6 +353,31 @@ namespace sibs
return Result<bool>::Ok(true);
}
+#if OS_FAMILY == OS_FAMILY_POSIX
+ Result<FileString> getHomeDir()
+ {
+ const char *homeDir = getenv("HOME");
+ if(!homeDir)
+ {
+ passwd *pw = getpwuid(getuid());
+ homeDir = pw->pw_dir;
+ }
+ return Result<FileString>::Ok(homeDir);
+ }
+
+ Result<FileString> getCwd()
+ {
+ FileString cwd;
+ cwd.resize(_TINYDIR_PATH_MAX);
+ if(getcwd(&cwd[0], _TINYDIR_PATH_MAX) != 0)
+ {
+ if(cwd.empty()) cwd = ".";
+ cwd.resize(_tinydir_strlen(cwd.c_str()));
+ return Result<FileString>::Ok(cwd);
+ }
+ return Result<FileString>::Err(strerror(errno));
+ }
+
Result<FileString> getRealPath(const _tinydir_char_t *path)
{
// TODO: Verify NULL can be passed as 'resolved' argument with different compilers and operating systems (clang, freebsd etc)
@@ -420,57 +433,6 @@ namespace sibs
return Result<FileString>::Ok(cwd);
}
- Result<bool> createDirectoryRecursive(const _tinydir_char_t *path)
- {
- _tinydir_char_t pathBuffer[_TINYDIR_PATH_MAX];
- size_t pathLength = _tinydir_strlen(path);
- if (pathLength > sizeof(pathBuffer) - 1)
- {
- string errMsg = "Directory path too long: ";
- errMsg += toUtf8(FileString(path, pathLength));
- return Result<bool>::Err(errMsg, ENAMETOOLONG);
- }
- _tinydir_strcpy(pathBuffer, path);
-
- _tinydir_char_t *p = pathBuffer;
- for (size_t i = 0; i < pathLength; ++i)
- {
- if (i > 0 && *p == '/')
- {
- *p = '\0';
- if (_wmkdir(pathBuffer) != 0)
- {
- int error = errno;
- if (error != EEXIST)
- {
- string errMsg = "Failed to create directory: ";
- errMsg += toUtf8(pathBuffer);
- errMsg += "; reason: ";
- errMsg += strerror(error);
- return Result<bool>::Err(errMsg, error);
- }
- }
- *p = '/';
- }
- ++p;
- }
-
- if (_wmkdir(pathBuffer) != 0)
- {
- int error = errno;
- if (error != EEXIST)
- {
- string errMsg = "Failed to create directory: ";
- errMsg += toUtf8(pathBuffer);
- errMsg += "; reason: ";
- errMsg += strerror(error);
- return Result<bool>::Err(errMsg, error);
- }
- }
-
- return Result<bool>::Ok(true);
- }
-
Result<FileString> getRealPath(const _tinydir_char_t *path)
{
FileString fullPath;