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 | |
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')
-rw-r--r-- | src/FileUtil.cpp | 80 | ||||
-rw-r--r-- | src/GlobalLib.cpp | 16 | ||||
-rw-r--r-- | src/curl.cpp | 18 | ||||
-rw-r--r-- | src/main.cpp | 1 |
4 files changed, 100 insertions, 15 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 diff --git a/src/GlobalLib.cpp b/src/GlobalLib.cpp index ce9426d..177f1b9 100644 --- a/src/GlobalLib.cpp +++ b/src/GlobalLib.cpp @@ -140,8 +140,11 @@ namespace sibs } else { - // TODO: Create build path if it doesn't exist string debugBuildPath = packageDir + "/build/debug"; + Result<bool> createBuildDirResult = createDirectoryRecursive(debugBuildPath.c_str()); + if(createBuildDirResult.isErr()) + return Result<string>::Err(createBuildDirResult); + Result<bool> buildFileResult = ninja.createBuildFile(sibsConfig.getPackageName(), sibsConfig.getDependencies(), debugBuildPath.c_str(), linkerFlagCallbackFunc); if (buildFileResult.isErr()) return Result<string>::Err(buildFileResult.getErrMsg()); @@ -167,23 +170,30 @@ namespace sibs url += dependency.version; url += ".tar.gz"; - // TODO: Create library path if it doesn't exist string libPath = getHomeDir(); libPath += "/.sibs/lib/"; libPath += dependency.name; libPath += "/"; libPath += dependency.version; - // TODO: Create archive directory if it doesn't exist string libArchivedFilePath = getHomeDir(); libArchivedFilePath += "/.sibs/archive/"; libArchivedFilePath += dependency.name; + Result<bool> createArchiveDirResult = createDirectoryRecursive(libArchivedFilePath.c_str()); + if(createArchiveDirResult.isErr()) + return createArchiveDirResult; + libArchivedFilePath += "/"; libArchivedFilePath += dependency.version; Result<bool> downloadResult = curl::downloadFile(url.c_str(), libArchivedFilePath.c_str()); if(downloadResult.isErr()) return downloadResult; + // Create build path. This is done here because we dont want to create it if download fails + Result<bool> createLibDirResult = createDirectoryRecursive(libPath.c_str()); + if(createLibDirResult.isErr()) + return createLibDirResult; + return Archive::extract(libArchivedFilePath.c_str(), libPath.c_str()); } }
\ No newline at end of file diff --git a/src/curl.cpp b/src/curl.cpp index f39cab8..e141e78 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -70,10 +70,24 @@ namespace sibs curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, file); printf("Downloading from url: %s\n", url); CURLcode curlResponse = curl_easy_perform(curl_handle); + + long httpCode = 0; + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl_handle); fclose(file); - if(curlResponse != CURLE_OK) + if(httpCode == 200 && curlResponse == CURLE_OK) + return Result<bool>::Ok(true); + + if(httpCode != 200) + { + string errMsg = "Failed to download file from url: "; + errMsg += url; + errMsg += "\nReason: Expected http response code 200 (OK), got: "; + errMsg += to_string(httpCode); + return Result<bool>::Err(errMsg); + } + else { string errMsg = "Failed to download file from url: "; errMsg += url; @@ -81,7 +95,5 @@ namespace sibs errMsg += curl_easy_strerror(curlResponse); return Result<bool>::Err(errMsg); } - - return Result<bool>::Ok(true); } }
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ce379c2..5c25414 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,7 +108,6 @@ void build(string projectPath) } }); - // TODO: Create build path if it doesn't exist string debugBuildPath = projectPath + "/build/debug"; Result<bool> buildFileResult = ninja.createBuildFile(sibsConfig.getPackageName(), sibsConfig.getDependencies(), debugBuildPath.c_str()); if(buildFileResult.isErr()) |