aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/ninja/Ninja.cpp18
-rw-r--r--include/Archive.hpp1
-rw-r--r--include/FileUtil.hpp4
-rw-r--r--src/FileUtil.cpp80
-rw-r--r--src/GlobalLib.cpp16
-rw-r--r--src/curl.cpp18
-rw-r--r--src/main.cpp1
7 files changed, 114 insertions, 24 deletions
diff --git a/backend/ninja/Ninja.cpp b/backend/ninja/Ninja.cpp
index 72c1e80..7fb07bf 100644
--- a/backend/ninja/Ninja.cpp
+++ b/backend/ninja/Ninja.cpp
@@ -85,9 +85,11 @@ namespace backend
{
if(dependencies.empty()) return Result<string>::Ok("");
- // TODO: Global library dir should be created during sibs installation
string globalLibDir = getHomeDir();
globalLibDir += "/.sibs/lib";
+ Result<bool> createGlobalLibDirResult = createDirectoryRecursive(globalLibDir.c_str());
+ if(createGlobalLibDirResult.isErr())
+ return Result<string>::Err(createGlobalLibDirResult);
string globalLibLinkerFlags;
vector<string> pkgConfigDependencies;
@@ -149,6 +151,10 @@ namespace backend
if(sourceFiles.empty())
return Result<bool>::Err("No source files provided");
+ Result<bool> createBuildDirResult = createDirectoryRecursive(savePath);
+ if(createBuildDirResult.isErr())
+ return createBuildDirResult;
+
string ninjaBuildFilePath = savePath;
ninjaBuildFilePath += "/build.ninja";
@@ -253,13 +259,9 @@ namespace backend
return Result<bool>::Err("NOT IMPLEMENTED YET!");
}
- bool fileOverwritten = sibs::fileOverwrite(ninjaBuildFilePath.c_str(), sibs::StringView(result.data(), result.size()));
- if(!fileOverwritten)
- {
- string errMsg = "Failed to overwrite ninja build file: ";
- errMsg += ninjaBuildFilePath;
- return Result<bool>::Err(errMsg);
- }
+ Result<bool> fileOverwriteResult = sibs::fileOverwrite(ninjaBuildFilePath.c_str(), sibs::StringView(result.data(), result.size()));
+ if(fileOverwriteResult.isErr())
+ return fileOverwriteResult;
printf("Created ninja build file: %s\n", ninjaBuildFilePath.c_str());
return Result<bool>::Ok(true);
diff --git a/include/Archive.hpp b/include/Archive.hpp
index 6d6a55d..2545f77 100644
--- a/include/Archive.hpp
+++ b/include/Archive.hpp
@@ -8,6 +8,7 @@ namespace sibs
class Archive
{
public:
+ // Note: renames root directory in archive to @destination
static Result<bool> extract(const char *source, const char *destination);
};
}
diff --git a/include/FileUtil.hpp b/include/FileUtil.hpp
index 8407a36..5b91aad 100644
--- a/include/FileUtil.hpp
+++ b/include/FileUtil.hpp
@@ -22,9 +22,11 @@ namespace sibs
void walkDirFiles(const char *directory, FileWalkCallbackFunc callbackFunc);
void walkDirFilesRecursive(const char *directory, FileWalkCallbackFunc callbackFunc);
Result<StringView> getFileContent(const char *filepath);
- bool fileOverwrite(const char *filepath, StringView data);
+ Result<bool> fileOverwrite(const char *filepath, StringView data);
const char* getHomeDir();
Result<std::string> getCwd();
+ // Note: Will not delete created directories if this operation fails for some reason
+ Result<bool> createDirectoryRecursive(const char *path);
}
#endif //SIBS_FILEUTIL_HPP
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())