From be66c108884ef3337071c7f66ab2c137df48d00b Mon Sep 17 00:00:00 2001 From: Tomek Sobkowiak Date: Tue, 26 Jan 2021 11:05:03 +0100 Subject: [PATCH] [EGD-5347] Replace filelength with file_size Replace use of filelenght with std::filesystem::file_size sqlite3vfs is using file descriptors so filelength is just renamed --- .../models/QuotesRepository.cpp | 2 +- module-db/Database/sqlite3vfs.cpp | 18 ++++++++-- module-gui/gui/core/FontManager.cpp | 4 +-- module-gui/gui/core/ImageManager.cpp | 4 +-- module-gui/gui/input/Profile.cpp | 2 +- .../endpoints/backup/BackupRestore.cpp | 10 +++--- .../endpoints/factoryReset/FactoryReset.cpp | 18 +++++----- .../endpoints/update/UpdateMuditaOS.cpp | 14 ++++---- module-utils/Utils.cpp | 14 +------- module-utils/Utils.hpp | 3 +- module-utils/i18n/i18n.cpp | 2 +- module-utils/test/unittest_utils.cpp | 34 ------------------- 12 files changed, 46 insertions(+), 79 deletions(-) diff --git a/module-apps/application-settings-new/models/QuotesRepository.cpp b/module-apps/application-settings-new/models/QuotesRepository.cpp index 077de115278e4b031a66731f4006cdf124c451c9..4c0726413fb2b631434cf2a83d73906fb218d1c0 100644 --- a/module-apps/application-settings-new/models/QuotesRepository.cpp +++ b/module-apps/application-settings-new/models/QuotesRepository.cpp @@ -86,7 +86,7 @@ namespace app return {}; } auto _ = gsl::finally([file] { std::fclose(file); }); - const auto length = utils::filesystem::filelength(file); + const auto length = std::filesystem::file_size(filename); if (length >= tar_buf) { LOG_ERROR("File %s length is too high!", filename.c_str()); diff --git a/module-db/Database/sqlite3vfs.cpp b/module-db/Database/sqlite3vfs.cpp index 68419bd4449b7239a5ba8ad8b4ba9e466a9c6c85..2270d4c203ade506749d3661cd0aaf505af1c143 100644 --- a/module-db/Database/sqlite3vfs.cpp +++ b/module-db/Database/sqlite3vfs.cpp @@ -159,6 +159,18 @@ struct EcophoneFile sqlite3_int64 iBufferOfst; /* Offset in file of zBuffer[0] */ }; +static std::uintmax_t file_size(std::FILE *file) noexcept +{ + if (file == nullptr) { + return 0; + } + const auto startPosition = std::ftell(file); + std::fseek(file, 0, SEEK_END); + const auto endPosition = std::ftell(file); + std::fseek(file, startPosition, SEEK_SET); + return endPosition; +} + /* ** Write directly to the file passed as the first argument. Even if the ** file has a write-buffer (EcophoneFile.aBuffer), ignore it. @@ -170,7 +182,7 @@ static int ecophoneDirectWrite(EcophoneFile *p, /* File handle */ ) { size_t nWrite; /* Return value from write() */ - const auto fileSize = utils::filesystem::filelength(p->fd); + const auto fileSize = file_size(p->fd); // vfs_fseek doesn't like offset to be > file size if (iOfst < fileSize) { if (std::fseek(p->fd, iOfst, SEEK_SET) != 0) { @@ -268,7 +280,7 @@ static int ecophoneRead(sqlite3_file *pFile, void *zBuf, int iAmt, sqlite_int64 return rc; } - auto fileSize = utils::filesystem::filelength(p->fd); + auto fileSize = file_size(p->fd); if (p->fd != nullptr) { if (iOfst >= fileSize) { @@ -393,7 +405,7 @@ static int ecophoneFileSize(sqlite3_file *pFile, sqlite_int64 *pSize) return rc; } - *pSize = utils::filesystem::filelength(p->fd); + *pSize = file_size(p->fd); return SQLITE_OK; } diff --git a/module-gui/gui/core/FontManager.cpp b/module-gui/gui/core/FontManager.cpp index 6907589cb7f9db82669df43244f2e9e72612d225..3e65ae0927291966c2371653f7c6edc3609374cf 100644 --- a/module-gui/gui/core/FontManager.cpp +++ b/module-gui/gui/core/FontManager.cpp @@ -46,7 +46,7 @@ namespace gui auto file = std::fopen(filename.c_str(), "rb"); - auto fileSize = utils::filesystem::filelength(file); + auto fileSize = std::filesystem::file_size(filename); std::rewind(file); char *fontData = new char[fileSize]; @@ -61,7 +61,7 @@ namespace gui // close file std::fclose(file); - if (static_cast(bytesRead) != fileSize) { + if (static_cast(bytesRead) != fileSize) { LOG_ERROR("Failed to read all file"); delete[] fontData; return nullptr; diff --git a/module-gui/gui/core/ImageManager.cpp b/module-gui/gui/core/ImageManager.cpp index 9ad42b0dce62aa56d517a484b7c8f1707de0cfa3..d278cf74b6936670144f8c29adb81bf5cda2c135 100644 --- a/module-gui/gui/core/ImageManager.cpp +++ b/module-gui/gui/core/ImageManager.cpp @@ -86,7 +86,7 @@ namespace gui auto file = std::fopen(filename.c_str(), "rb"); - auto fileSize = utils::filesystem::filelength(file); + auto fileSize = std::filesystem::file_size(filename); char *data = new char[fileSize]; if (data == nullptr) { @@ -130,7 +130,7 @@ namespace gui auto file = std::fopen(filename.c_str(), "rb"); - auto fileSize = utils::filesystem::filelength(file); + auto fileSize = std::filesystem::file_size(filename); char *data = new char[fileSize]; if (data == nullptr) { diff --git a/module-gui/gui/input/Profile.cpp b/module-gui/gui/input/Profile.cpp index 77c6985f14ef2822ee8c4a30b9a7b8f37c0a7454..854535a22e00729cf2b8e478b748f0045c6600f2 100644 --- a/module-gui/gui/input/Profile.cpp +++ b/module-gui/gui/input/Profile.cpp @@ -35,7 +35,7 @@ namespace gui return json11::Json(); } - uint32_t fsize = utils::filesystem::filelength(fd); + uint32_t fsize = std::filesystem::file_size(filepath); auto stream = std::make_unique(fsize + 1); diff --git a/module-services/service-desktop/endpoints/backup/BackupRestore.cpp b/module-services/service-desktop/endpoints/backup/BackupRestore.cpp index 39455eef9b3fa3da06cf619e60b8a6024e212eac..3b782788bd996c1c06f46cc09cfeed7015322ec2 100644 --- a/module-services/service-desktop/endpoints/backup/BackupRestore.cpp +++ b/module-services/service-desktop/endpoints/backup/BackupRestore.cpp @@ -149,8 +149,9 @@ bool BackupRestore::PackUserFiles() if ((direntry.path().string().compare(".") != 0) && (direntry.path().string().compare("..") != 0) && (direntry.path().string().compare("...") != 0)) { - LOG_INFO("PackUserFiles: archiving file %s...", (backupPathDB + direntry.path().string()).c_str()); - auto *file = std::fopen((backupPathDB + direntry.path().string()).c_str(), "r"); + auto path = backupPathDB + direntry.path().string().c_str(); + LOG_INFO("PackUserFiles: archiving file %s...", path.c_str()); + auto *file = std::fopen(path.c_str(), "r"); if (file == nullptr) { LOG_ERROR("PackUserFiles: archiving file %s failed, cannot open file, quitting...", @@ -172,12 +173,13 @@ bool BackupRestore::PackUserFiles() return false; } - uint32_t loopcount = (utils::filesystem::filelength(file) / tar_buf) + 1u; + uintmax_t filesize = std::filesystem::file_size(path); + uint32_t loopcount = (filesize / tar_buf) + 1u; uint32_t readsize = 0u; for (uint32_t i = 0u; i < loopcount; i++) { if (i + 1u == loopcount) { - readsize = utils::filesystem::filelength(file) % tar_buf; + readsize = filesize % tar_buf; } else { readsize = tar_buf; diff --git a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp index 8c2d17b5e3e4aed974e6ecc96d604222e34f14d0..3b961c5e7ed58dea1e1534b4b29b140ee92e20c0 100644 --- a/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp +++ b/module-services/service-desktop/endpoints/factoryReset/FactoryReset.cpp @@ -30,18 +30,18 @@ namespace FactoryReset static bool CopyFile(std::string sourcefile, std::string targetfile); - static int recurseDepth = 0; - static const int max_recurse_depth = 120; /* 120 is just an arbitrary value of max number of recursive calls. - * If more then error is assumed, not the real depth of directories." - */ + static int recurseDepth = 0; + static const int max_recurse_depth = 120; /* 120 is just an arbitrary value of max number of recursive calls. + * If more then error is assumed, not the real depth of directories." + */ static const int max_filepath_length = PATH_MAX; bool Run(sys::Service *ownerService) { LOG_INFO("FactoryReset: restoring factory state started..."); - recurseDepth = 0; - const auto factoryOSPath = purefs::dir::getFactoryOSPath(); + recurseDepth = 0; + const auto factoryOSPath = purefs::dir::getFactoryOSPath(); if (std::filesystem::is_directory(factoryOSPath.c_str()) && std::filesystem::is_empty(factoryOSPath.c_str())) { LOG_ERROR("FactoryReset: restoring factory state aborted"); @@ -114,7 +114,7 @@ namespace FactoryReset return false; } - const auto factoryOSPath = purefs::dir::getFactoryOSPath(); + const auto factoryOSPath = purefs::dir::getFactoryOSPath(); for (auto &direntry : std::filesystem::directory_iterator(sourcedir.c_str())) { if ((direntry.path().string().compare(".") == 0) || (direntry.path().string().compare("..") == 0) || @@ -182,12 +182,12 @@ namespace FactoryReset std::unique_ptr buffer(new unsigned char[copy_buf]); if (buffer.get() != nullptr) { - uint32_t loopcount = (utils::filesystem::filelength(sf.get()) / copy_buf) + 1u; + uint32_t loopcount = (std::filesystem::file_size(sourcefile) / copy_buf) + 1u; uint32_t readsize = copy_buf; for (uint32_t i = 0u; i < loopcount; i++) { if (i + 1u == loopcount) { - readsize = utils::filesystem::filelength(sf.get()) % copy_buf; + readsize = std::filesystem::file_size(sourcefile) % copy_buf; } if (std::fread(buffer.get(), 1, readsize, sf.get()) != readsize) { diff --git a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp index ee1216bbcef5a2a7213f059668e9588953e90571..114b7524fb29994849142262d7704685986d7092 100644 --- a/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp +++ b/module-services/service-desktop/endpoints/update/UpdateMuditaOS.cpp @@ -61,7 +61,7 @@ updateos::UpdateError UpdateMuditaOS::setUpdateFile(const std::filesystem::path if (std::filesystem::exists(updateFile)) { versionInformation = UpdateMuditaOS::getVersionInfoFromFile(updateFile); if (mtar_open(&updateTar, updateFile.c_str(), "r") == MTAR_ESUCCESS) { - totalBytes = utils::filesystem::filelength(updateTar.stream); + totalBytes = std::filesystem::file_size(updateFile); } else { return informError(updateos::UpdateError::CantOpenUpdateFile, @@ -204,7 +204,7 @@ std::string UpdateMuditaOS::readContent(const char *filename) noexcept updateos::UpdateError UpdateMuditaOS::verifyChecksums() { - status = updateos::UpdateState::ChecksumVerification; + status = updateos::UpdateState::ChecksumVerification; auto lineBuff = std::make_unique( boot::consts::tar_buf); // max line should be freertos max path + checksum, so this is enough fs::path checksumsFile = getUpdateTmpChild(updateos::file::checksums); @@ -377,7 +377,7 @@ updateos::UpdateError UpdateMuditaOS::prepareRoot() updateos::UpdateError UpdateMuditaOS::updateBootJSON() { - fs::path bootJSONAbsoulte = purefs::createPath(purefs::dir::getRootDiskPath(), purefs::file::boot_json); + fs::path bootJSONAbsoulte = purefs::createPath(purefs::dir::getRootDiskPath(), purefs::file::boot_json); informDebug("updateBootJSON %s", bootJSONAbsoulte.c_str()); auto *fp = std::fopen(bootJSONAbsoulte.c_str(), "r"); @@ -426,8 +426,8 @@ bool UpdateMuditaOS::unpackFileToTemp(mtar_header_t &h, unsigned long *crc32) return false; } - int errCode = MTAR_ESUCCESS; - auto *fp = std::fopen(fullPath.c_str(), "w+"); + int errCode = MTAR_ESUCCESS; + auto *fp = std::fopen(fullPath.c_str(), "w+"); if (fp == nullptr) { informError( updateos::UpdateError::CantWriteToFile, "unpackFileToTemp %s can't open for writing", fullPath.c_str()); @@ -579,7 +579,7 @@ updateos::UpdateError UpdateMuditaOS::writeBootloader(fs::path bootloaderFile) bootloaderFile.c_str()); } - unsigned long fileLen = utils::filesystem::filelength(fileHandler); + unsigned long fileLen = std::filesystem::file_size(bootloaderFile); auto fileBuf = std::make_unique(fileLen); if (fileBuf == nullptr) { std::fclose(fileHandler); @@ -670,7 +670,7 @@ bool UpdateMuditaOS::isUpgradeToCurrent(const std::string &versionToCompare) const fs::path UpdateMuditaOS::checkForUpdate() { - const auto updatesOSPath = purefs::dir::getUpdatesOSPath(); + const auto updatesOSPath = purefs::dir::getUpdatesOSPath(); for (auto &file : std::filesystem::directory_iterator(updatesOSPath.c_str())) { json11::Json versionInfo = UpdateMuditaOS::getVersionInfoFromFile(updatesOSPath / file.path()); if (versionInfo.is_null()) diff --git a/module-utils/Utils.cpp b/module-utils/Utils.cpp index 1b481374ca03d7f64e75aef57904f189e945efc2..2dde3e57830ebe2a70560056ef158c93a6b52226 100644 --- a/module-utils/Utils.cpp +++ b/module-utils/Utils.cpp @@ -12,19 +12,7 @@ namespace utils::filesystem inline constexpr auto crc_buf_len = 1024; } // namespace - long int filelength(std::FILE *file) noexcept - { - if (file == nullptr) { - return 0; - } - const auto startPosition = std::ftell(file); - std::fseek(file, 0, SEEK_END); - const auto endPosition = std::ftell(file); - std::fseek(file, startPosition, SEEK_SET); - return endPosition; - } - - unsigned long computeFileCRC32(::FILE *file) noexcept + unsigned long computeFileCRC32(std::FILE *file) noexcept { auto buf = std::make_unique(crc_buf_len); diff --git a/module-utils/Utils.hpp b/module-utils/Utils.hpp index 7e2cd2e3d3b6be41ba8ae4022950d65f7eda4821..28e9e7b5bae424c47fdfa3be0d048a4659a15f66 100644 --- a/module-utils/Utils.hpp +++ b/module-utils/Utils.hpp @@ -123,7 +123,7 @@ namespace utils } } - auto fractionalPart = static_cast(roundl(frac)); + auto fractionalPart = static_cast(roundl(frac)); auto fractionalPartLength = std::to_string(fractionalPart).length(); if (fractionalPartLength > precision) { base += 1; @@ -252,7 +252,6 @@ namespace utils namespace filesystem { - [[nodiscard]] long int filelength(std::FILE *file) noexcept; [[nodiscard]] unsigned long computeFileCRC32(std::FILE *file) noexcept; [[nodiscard]] std::string generateRandomId(std::size_t length = 0) noexcept; [[nodiscard]] std::string getline(std::FILE *stream, uint32_t length = 1024) noexcept; diff --git a/module-utils/i18n/i18n.cpp b/module-utils/i18n/i18n.cpp index 4a8631092125cc0afca150686386b33500636b1a..0b2f71f6288aa3011ce196bc7f66a49c100fad2a 100644 --- a/module-utils/i18n/i18n.cpp +++ b/module-utils/i18n/i18n.cpp @@ -27,7 +27,7 @@ namespace utils return json11::Json(); } - uint32_t fsize = utils::filesystem::filelength(fd); + uint32_t fsize = std::filesystem::file_size(path); auto stream = std::make_unique(fsize + 1); // +1 for NULL terminator diff --git a/module-utils/test/unittest_utils.cpp b/module-utils/test/unittest_utils.cpp index 156e952a919726bd43b621210a8de15bd2c559d7..f283872ccd6ce28563e42ac7dac455ac0630574f 100644 --- a/module-utils/test/unittest_utils.cpp +++ b/module-utils/test/unittest_utils.cpp @@ -13,7 +13,6 @@ #include "Utils.hpp" - TEST_CASE("Split tests") { std::string delimiter = "\r\n"; @@ -271,36 +270,3 @@ TEST_CASE("Fill leading digit in string") REQUIRE(utils::addLeadingZeros(test, 3) == "045"); REQUIRE(utils::addLeadingZeros(test, 4) == "0045"); } - -class ScopedDir -{ - public: - ScopedDir(const std::filesystem::path &dirPath) : dirPath{dirPath} - { - REQUIRE(std::filesystem::create_directory(dirPath)); - } - - ~ScopedDir() - { - REQUIRE(std::filesystem::remove(dirPath)); - } - - auto operator()(std::string file = "") -> std::filesystem::path - { - return dirPath.c_str() + file; - } - - private: - std::filesystem::path dirPath; -}; - -TEST_CASE("Read file length") -{ - ScopedDir dir("mytest"); - auto *file = std::fopen(dir("test.txt").c_str(), "w"); - REQUIRE(file != nullptr); - std::array v = {42, -1, 7}; - std::fwrite(v.data(), sizeof(v[0]), v.size(), file); - REQUIRE(utils::filesystem::filelength(file) == static_cast(sizeof(v[0]) * v.size())); - REQUIRE(std::fclose(file) == 0); -}