From f2c70dfaba8d6481e86646080c51b6874d95f14e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 12 Dec 2017 19:46:57 +0100 Subject: 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 --- src/FileUtil.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/GlobalLib.cpp | 16 ++++++++--- src/curl.cpp | 18 ++++++++++--- src/main.cpp | 1 - 4 files changed, 100 insertions(+), 15 deletions(-) (limited to 'src') 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 #include #include +#include #endif using namespace std; @@ -84,10 +85,14 @@ namespace sibs Result getFileContent(const char *filepath) { FILE *file = fopen(filepath, "rb"); - if(!file || errno != 0) + if(!file) { - perror(filepath); - return Result::Err("Failed to open file"); + int error = errno; + string errMsg = "Failed to open file: "; + errMsg += filepath; + errMsg += "; reason: "; + errMsg += strerror(error); + return Result::Err(errMsg); } fseek(file, 0, SEEK_END); @@ -108,17 +113,21 @@ namespace sibs return Result::Ok(StringView(result, fileSize)); } - bool fileOverwrite(const char *filepath, StringView data) + Result 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::Err(errMsg); } fwrite(data.data, 1, data.size, file); fclose(file); - return true; + return Result::Ok(true); } const char* getHomeDir() @@ -144,4 +153,59 @@ namespace sibs return Result::Err(strerror(errno)); } + +#if OS_FAMILY == OS_FAMILY_POSIX + Result 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::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::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::Err(errMsg, error); + } + } + + return Result::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 createBuildDirResult = createDirectoryRecursive(debugBuildPath.c_str()); + if(createBuildDirResult.isErr()) + return Result::Err(createBuildDirResult); + Result buildFileResult = ninja.createBuildFile(sibsConfig.getPackageName(), sibsConfig.getDependencies(), debugBuildPath.c_str(), linkerFlagCallbackFunc); if (buildFileResult.isErr()) return Result::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 createArchiveDirResult = createDirectoryRecursive(libArchivedFilePath.c_str()); + if(createArchiveDirResult.isErr()) + return createArchiveDirResult; + libArchivedFilePath += "/"; libArchivedFilePath += dependency.version; Result 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 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::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::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::Err(errMsg); } - - return Result::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 buildFileResult = ninja.createBuildFile(sibsConfig.getPackageName(), sibsConfig.getDependencies(), debugBuildPath.c_str()); if(buildFileResult.isErr()) -- cgit v1.2.3