From f1c219b6322427fd2d5d97df17fe684fbfe45afa Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 4 Jul 2020 20:21:45 +0200 Subject: Return a string for a getFileData --- include/FileUtil.hpp | 2 +- src/Conf.cpp | 13 +++++++------ src/FileUtil.cpp | 19 ++++++------------- src/GlobalLib.cpp | 20 +++++++++++++++++++- src/main.cpp | 16 +++++++--------- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/include/FileUtil.hpp b/include/FileUtil.hpp index 0108b24..f2a1799 100644 --- a/include/FileUtil.hpp +++ b/include/FileUtil.hpp @@ -61,7 +61,7 @@ namespace sibs void walkDirFiles(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc); bool walkDirFilesRecursive(const _tinydir_char_t *directory, FileWalkCallbackFunc callbackFunc); bool walkDirFilesRecursiveSortTimestamp(const _tinydir_char_t *directory, SortedFileWalkCallbackFunc callbackFunc); - Result getFileContent(const _tinydir_char_t *filepath); + Result getFileContent(const _tinydir_char_t *filepath); Result fileWrite(const _tinydir_char_t *filepath, StringView data); Result fileOverwrite(const _tinydir_char_t *filepath, StringView data); Result getHomeDir(); diff --git a/src/Conf.cpp b/src/Conf.cpp index dc9c406..74c311c 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -463,19 +463,20 @@ namespace sibs Result Config::readFromFile(const _tinydir_char_t *filepath, SibsConfig &config) { - Result fileContentResult = getFileContent(filepath); + Result fileContentResult = getFileContent(filepath); if(fileContentResult.isErr()) return Result::Err(fileContentResult.getErrMsg()); - const char *code = fileContentResult.unwrap().data; - if(!utf8::is_valid(code, code + fileContentResult.unwrap().size)) + const std::string &code = fileContentResult.unwrap(); + if(!utf8::is_valid(code.data(), code.data() + code.size())) return Result::Err("File is not in valid utf8 format"); - if(fileContentResult.unwrap().size >= 3 && utf8::is_bom(code)) - code += 3; + const char *code_ptr = code.data(); + if(code.size() >= 3 && utf8::is_bom(code_ptr)) + code_ptr += 3; // Do not free file content (fileContentResult) on purpose, since we are using the data and sibs is short lived - Result parseResult = Parser::parse(code, config); + Result parseResult = Parser::parse(code_ptr, config); if(!parseResult) { string errMsg = "Failed while parsing project.conf for project "; diff --git a/src/FileUtil.cpp b/src/FileUtil.cpp index 1fd86d6..e1142ec 100644 --- a/src/FileUtil.cpp +++ b/src/FileUtil.cpp @@ -301,7 +301,7 @@ namespace sibs return true; } - Result getFileContent(const _tinydir_char_t *filepath) + Result getFileContent(const _tinydir_char_t *filepath) { #if OS_FAMILY == OS_FAMILY_POSIX FILE *file = fopen(filepath, "rb"); @@ -315,25 +315,18 @@ namespace sibs errMsg += toUtf8(filepath); errMsg += "; reason: "; errMsg += strerror(error); - return Result::Err(errMsg); + return Result::Err(errMsg); } fseek(file, 0, SEEK_END); size_t fileSize = ftell(file); fseek(file, 0, SEEK_SET); - // TODO: Change this to string so it can be deallocated and use std::move to prevent copies - char *result = (char*)malloc(fileSize + 1); - if(!result) - { - std::string errMsg = "Failed to load file content from file: "; - errMsg += toUtf8(filepath); - throw std::runtime_error(errMsg); - } - result[fileSize] = '\0'; - fread(result, 1, fileSize, file); + std::string result; + result.resize(fileSize); + fread(&result[0], 1, fileSize, file); fclose(file); - return Result::Ok(StringView(result, fileSize)); + return Result::Ok(std::move(result)); } Result fileWrite(const _tinydir_char_t *filepath, StringView data) diff --git a/src/GlobalLib.cpp b/src/GlobalLib.cpp index df66727..8346a83 100644 --- a/src/GlobalLib.cpp +++ b/src/GlobalLib.cpp @@ -60,7 +60,22 @@ namespace sibs FileString libArchivedFilePath = libPathResult.unwrap(); libArchivedFilePath += TINYDIR_STRING("/.cache/sibs/archive/"); libArchivedFilePath += toFileString(name); - if(getFileType(libArchivedFilePath.c_str()) != FileType::FILE_NOT_FOUND) + FileType archive_path_file_type = getFileType(libArchivedFilePath.c_str()); + + if(archive_path_file_type == FileType::FILE_NOT_FOUND) + return Result::Ok(true); + + if(archive_path_file_type != FileType::DIRECTORY) + return Result::Err("A previous download of package is corrupt, attempting to redownload..."); + + bool isEmpty = true; + walkDir(libArchivedFilePath.c_str(), [&isEmpty](tinydir_file *file) + { + isEmpty = false; + return false; + }); + + if(!isEmpty) return Result::Err("A previous download of package is corrupt, attempting to redownload..."); return Result::Ok(true); @@ -314,6 +329,7 @@ namespace sibs if(!createArchiveDirResult) return createArchiveDirResult; + FileString libArchivedDir = libArchivedFilePath; libArchivedFilePath += TINYDIR_STRING("/"); libArchivedFilePath += toFileString(package.version.toString()); Result downloadResult = curl::downloadFile(package.urls[0].c_str(), libArchivedFilePath.c_str()); @@ -329,8 +345,10 @@ namespace sibs // We have extracted the archive, we dont need to cache it. If remove fails, it doesn't really matter, user can remove it himself #if OS_FAMILY == OS_FAMILY_POSIX remove(libArchivedFilePath.c_str()); + remove(libArchivedDir.c_str()); #else _wremove(libArchivedFilePath.c_str()); + _wremove(libArchivedDir.c_str()); #endif return archiveExtractResult; } diff --git a/src/main.cpp b/src/main.cpp index 3d11fd5..0cb55e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -793,26 +793,24 @@ static Result gitInitProject(const FileString &projectPath) static bool gitIgnoreContainsSibs(const FileString &gitIgnoreFilePath) { - Result fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); + Result fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); if(!fileContentResult) return false; - StringView fileContent = fileContentResult.unwrap(); - const char *fileContentEnd = fileContent.data + fileContent.size; - auto it = std::search(fileContent.data, fileContentEnd, SIBS_GITIGNORE_HEADER.begin(), SIBS_GITIGNORE_HEADER.end()); + const std::string &fileContent = fileContentResult.unwrap(); + const char *fileContentEnd = fileContent.data() + fileContent.size(); + auto it = std::search(fileContent.data(), fileContentEnd, SIBS_GITIGNORE_HEADER.begin(), SIBS_GITIGNORE_HEADER.end()); bool containsSibs = it != fileContentEnd; - free((void*)fileContent.data); return containsSibs; } static void gitIgnoreAppendSibs(const FileString &gitIgnoreFilePath) { - Result fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); + Result fileContentResult = getFileContent(gitIgnoreFilePath.c_str()); string fileContentNew; if(fileContentResult) { - StringView fileContent = fileContentResult.unwrap(); - fileContentNew.append(fileContent.data, fileContent.data + fileContent.size); + const std::string &fileContent = fileContentResult.unwrap(); + fileContentNew += fileContent; fileContentNew += "\n\n"; - free((void*)fileContent.data); } fileContentNew += SIBS_GITIGNORE_HEADER; fileContentNew += "\n"; -- cgit v1.2.3