diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-12 19:46:57 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-12 19:49:29 +0100 |
commit | f2c70dfaba8d6481e86646080c51b6874d95f14e (patch) | |
tree | 1ee19bdd5f951b86f2b481c94f969a473a5e57fc /src/FileUtil.cpp | |
parent | f3b7b7d34b3bf2b1be18914577c96b66dead379a (diff) |
Lazily create directories that are needed
Directories such as:
~/.sibs
~/.sibs/archive
~/.sibs/lib
And directories for each specific library.
Also fix bug in getFileContent and fileOverwrite if file already exists
Diffstat (limited to 'src/FileUtil.cpp')
-rw-r--r-- | src/FileUtil.cpp | 80 |
1 files changed, 72 insertions, 8 deletions
diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp index d815ebf..9a15938 100644 --- a/src/FileUtil.cpp +++ b/src/FileUtil.cpp @@ -6,6 +6,7 @@ #include <unistd.h> #include <sys/types.h> #include <pwd.h> +#include <fcntl.h> #endif using namespace std; @@ -84,10 +85,14 @@ namespace sibs Result<StringView> getFileContent(const char *filepath) { FILE *file = fopen(filepath, "rb"); - if(!file || errno != 0) + if(!file) { - perror(filepath); - return Result<StringView>::Err("Failed to open file"); + int error = errno; + string errMsg = "Failed to open file: "; + errMsg += filepath; + errMsg += "; reason: "; + errMsg += strerror(error); + return Result<StringView>::Err(errMsg); } fseek(file, 0, SEEK_END); @@ -108,17 +113,21 @@ namespace sibs return Result<StringView>::Ok(StringView(result, fileSize)); } - bool fileOverwrite(const char *filepath, StringView data) + Result<bool> fileOverwrite(const char *filepath, StringView data) { FILE *file = fopen(filepath, "wb"); - if(!file || errno != 0) + if(!file) { - perror(filepath); - return false; + int error = errno; + string errMsg = "Failed to overwrite file: "; + errMsg += filepath; + errMsg += "; reason: "; + errMsg += strerror(error); + return Result<bool>::Err(errMsg); } fwrite(data.data, 1, data.size, file); fclose(file); - return true; + return Result<bool>::Ok(true); } const char* getHomeDir() @@ -144,4 +153,59 @@ namespace sibs return Result<string>::Err(strerror(errno)); } + +#if OS_FAMILY == OS_FAMILY_POSIX + Result<bool> createDirectoryRecursive(const char *path) + { + char pathBuffer[PATH_MAX]; + size_t pathLength = strlen(path); + if(pathLength > sizeof(pathBuffer) - 1) + { + string errMsg = "Directory path too long: "; + errMsg += string(path, pathLength); + return Result<bool>::Err(errMsg, ENAMETOOLONG); + } + strcpy(pathBuffer, path); + + char *p = pathBuffer; + for(size_t i = 0; i < pathLength; ++i) + { + if(i > 0 && *p == '/') + { + *p = '\0'; + if(mkdir(pathBuffer, S_IRWXU) != 0) + { + int error = errno; + if(error != EEXIST) + { + string errMsg = "Failed to create directory: "; + errMsg += pathBuffer; + errMsg += "; reason: "; + errMsg += strerror(error); + return Result<bool>::Err(errMsg, error); + } + } + *p = '/'; + } + ++p; + } + + if(mkdir(pathBuffer, S_IRWXU) != 0) + { + int error = errno; + if(error != EEXIST) + { + string errMsg = "Failed to create directory: "; + errMsg += pathBuffer; + errMsg += "; reason: "; + errMsg += strerror(error); + return Result<bool>::Err(errMsg, error); + } + } + + return Result<bool>::Ok(true); + } +#else +#error "TODO: Implement createDirectoryRecursive on windows" +#endif }
\ No newline at end of file |