From c89e2569454fc0f6f7002923cd1be828e271293e Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Thu, 29 Dec 2022 14:28:41 +0200 Subject: [PATCH] Remove ubuntu 18.04 and cleanup code IB-7398 Signed-off-by: Raul Metsma --- .github/workflows/build.yml | 14 ++--- src/CMakeLists.txt | 4 +- src/DataFile.cpp | 10 ++-- src/DataFile_p.h | 20 ++++--- src/digidoc-tool.cpp | 37 +++++-------- src/util/File.cpp | 101 ++++-------------------------------- src/util/File.h | 3 -- 7 files changed, 38 insertions(+), 151 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 26df273e5..333322611 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -104,24 +104,18 @@ jobs: container: ${{ matrix.container }} strategy: matrix: - container: ['ubuntu:18.04', 'ubuntu:20.04', 'ubuntu:22.04', 'ubuntu:22.10'] + container: ['ubuntu:20.04', 'ubuntu:22.04', 'ubuntu:22.10'] env: DEBIAN_FRONTEND: noninteractive DEBFULLNAME: github-actions DEBEMAIL: github-actions@github.com steps: + - name: Install dependencies + run: apt update -qq && apt install --no-install-recommends -y git lsb-release fakeroot build-essential devscripts cdbs cmake xxd xsdcxx libxml-security-c-dev zlib1g-dev doxygen swig openjdk-8-jdk-headless libpython3-dev python3-distutils libboost-test-dev lintian - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v3 with: submodules: recursive - - name: Add open-eid repo - if: matrix.container == 'ubuntu:18.04' - run: | - apt update -qq && apt install --no-install-recommends -y curl ca-certificates gnupg2 lsb-release - curl https://installer.id.ee/media/install-scripts/C6C83D68.pub | gpg --dearmor | tee /etc/apt/trusted.gpg.d/ria-repository.gpg > /dev/null - echo "deb https://installer.id.ee/media/ubuntu/ $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/ria-repository.list - - name: Install dependencies - run: apt update -qq && apt install --no-install-recommends -y lsb-release fakeroot build-essential devscripts cdbs cmake xxd xsdcxx libxml-security-c-dev zlib1g-dev doxygen swig openjdk-8-jdk-headless libpython3-dev python3-distutils libboost-test-dev lintian - name: Setup changelog run: | export VERSION=$(grep project CMakeLists.txt | egrep -o "([0-9]{1,}\.)+[0-9]{1,}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e1581be78..0530f32bf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -242,9 +242,7 @@ target_link_libraries(digidocpp PRIVATE ${CMAKE_DL_LIBS} minizip digidocpp_priv) if( BUILD_TOOLS ) add_executable(digidoc-tool digidoc-tool.rc digidoc-tool.cpp) - target_link_libraries(digidoc-tool digidocpp digidocpp_priv Threads::Threads - "$<$,$,9.0>>:-lstdc++fs>" - ) + target_link_libraries(digidoc-tool digidocpp digidocpp_priv Threads::Threads) configure_file( digidoc-tool.1.cmake digidoc-tool.1 ) endif() diff --git a/src/DataFile.cpp b/src/DataFile.cpp index f12066261..6393f6059 100644 --- a/src/DataFile.cpp +++ b/src/DataFile.cpp @@ -23,6 +23,7 @@ #include "util/File.h" #include "util/log.h" +#include #include using namespace digidoc; @@ -86,13 +87,11 @@ DataFile::DataFile() = default; DataFile::~DataFile() = default; -DataFilePrivate::DataFilePrivate(unique_ptr is, string filename, string mediatype, - string id, vector digestValue) +DataFilePrivate::DataFilePrivate(unique_ptr &&is, string filename, string mediatype, string id) : m_is(move(is)) , m_id(move(id)) , m_filename(move(filename)) , m_mediatype(move(mediatype)) - , m_digestValue(move(digestValue)) { m_is->seekg(0, istream::end); istream::pos_type pos = m_is->tellg(); @@ -101,8 +100,6 @@ DataFilePrivate::DataFilePrivate(unique_ptr is, string filename, string vector DataFilePrivate::calcDigest(const string &method) const { - if(!m_digestValue.empty()) - return m_digestValue; Digest calc(method); calcDigest(&calc); return calc.result(); @@ -110,7 +107,7 @@ vector DataFilePrivate::calcDigest(const string &method) const void DataFilePrivate::calcDigest(Digest *digest) const { - vector buf(10240, 0); + array buf{}; m_is->clear(); m_is->seekg(0); while(*m_is) @@ -125,7 +122,6 @@ void DataFilePrivate::saveAs(const string& path) const { ofstream ofs(File::encodeName(path).c_str(), ofstream::binary); saveAs(ofs); - ofs.close(); } void DataFilePrivate::saveAs(ostream &os) const diff --git a/src/DataFile_p.h b/src/DataFile_p.h index f20dbd021..d03e9a295 100644 --- a/src/DataFile_p.h +++ b/src/DataFile_p.h @@ -28,25 +28,23 @@ namespace digidoc { class Digest; -class DataFilePrivate: public DataFile +class DataFilePrivate final: public DataFile { public: - DataFilePrivate(std::unique_ptr is, std::string filename, std::string mediatype, std::string id = {}, - std::vector digestValue = {}); + DataFilePrivate(std::unique_ptr &&is, std::string filename, std::string mediatype, std::string id = {}); - std::string id() const override { return m_id.empty() ? m_filename : m_id; } - std::string fileName() const override { return m_filename; } - unsigned long fileSize() const override { return m_size; } - std::string mediaType() const override { return m_mediatype; } + std::string id() const final { return m_id.empty() ? m_filename : m_id; } + std::string fileName() const final { return m_filename; } + unsigned long fileSize() const final { return m_size; } + std::string mediaType() const final { return m_mediatype; } - std::vector calcDigest(const std::string &method) const override; + std::vector calcDigest(const std::string &method) const final; void calcDigest(Digest *method) const; - void saveAs(std::ostream &os) const override; - void saveAs(const std::string& path) const override; + void saveAs(std::ostream &os) const final; + void saveAs(const std::string& path) const final; std::unique_ptr m_is; std::string m_id, m_filename, m_mediatype; - std::vector m_digestValue; unsigned long m_size; }; } diff --git a/src/digidoc-tool.cpp b/src/digidoc-tool.cpp index 47257eb59..a841e6da0 100644 --- a/src/digidoc-tool.cpp +++ b/src/digidoc-tool.cpp @@ -32,11 +32,8 @@ #include "util/log.h" #include -#include -#include -#include +#include #include -#include #include #include @@ -51,13 +48,7 @@ using namespace digidoc; using namespace digidoc::util; using namespace std; -#if __has_include() -#include -namespace fs = std::filesystem; -#else -#include -namespace fs = std::experimental::filesystem; -#endif +namespace fs = filesystem; namespace std { @@ -128,11 +119,6 @@ static ostream &operator<<(ostream &os, Signature::Validator::Status status) } return os; } - -static string& operator+(string_view lhs, string rhs) -{ - return rhs.insert(0, lhs); -} } /** @@ -495,9 +481,8 @@ unique_ptr ToolConfig::getSigner(bool getwebsigner) const win->setThumbprint(thumbprint); signer = unique_ptr(win.release()); } - else #endif - if(!pkcs12.empty()) + else if(!pkcs12.empty()) signer = make_unique(pkcs12, pin); else signer = make_unique(pkcs11, pin); @@ -556,7 +541,7 @@ static int open(int argc, char* argv[]) { ToolConfig::Warning reportwarnings = ToolConfig::WWarning; string path, policy; - string_view extractPath; + fs::path extractPath; bool validateOnExtract = false; int returnCode = EXIT_SUCCESS; @@ -573,11 +558,13 @@ static int open(int argc, char* argv[]) } else if(arg.find("--extractAll") == 0) { - extractPath = "."; - size_t pos = arg.find('='); - if(pos != string::npos) - extractPath = arg.substr(pos + 1); - if(!fs::is_directory(fs::u8path(extractPath))) + extractPath = fs::current_path(); + if(auto pos = arg.find('='); pos != string::npos) + { + fs::path newPath = fs::u8path(arg.substr(pos + 1)); + extractPath = newPath.is_relative() ? extractPath / newPath : newPath; + } + if(!fs::is_directory(extractPath)) THROW("Path is not directory"); } else if(arg == "--validateOnExtract") @@ -605,7 +592,7 @@ static int open(int argc, char* argv[]) for(const DataFile *file: doc->dataFiles()) { try { - string dst = extractPath + "/" + File::fileName(file->fileName()); + string dst = (extractPath / fs::u8path(file->fileName()).filename()).u8string(); file->saveAs(dst); cout << " Document(" << file->mediaType() << ") extracted to " << dst << " (" << file->fileSize() << " bytes)" << endl; } catch(const Exception &e) { diff --git a/src/util/File.cpp b/src/util/File.cpp index 1ce175510..d85ce1584 100644 --- a/src/util/File.cpp +++ b/src/util/File.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -45,6 +46,7 @@ using namespace digidoc; using namespace digidoc::util; using namespace std; +namespace fs = filesystem; #ifdef _WIN32 #define f_stat _wstat64 @@ -58,77 +60,6 @@ using f_statbuf = struct stat; using f_utimbuf = struct utimbuf; #endif -#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__ANDROID__) -#include -#include -#include -#include -#include - -/** - * Helper method for converting from non-UTF-8 encoded strings to UTF-8. - * Supported LANG values for Linux: see /usr/share/i18n/SUPPORTED. - * Supported encodings for libiconv: see iconv --list . - * - * Note! If non-ASCII characters are used we assume a proper LANG value!!! - * - * @param str_in The string to be converted. - * @return Returns the input string in UTF-8. - */ -string File::convertUTF8(string_view str_in, bool to_UTF) -{ - string charset = nl_langinfo(CODESET); - // no conversion needed for UTF-8 - if(charset == "UTF-8" || charset == "utf-8") - return string(str_in); - - iconv_t ic_descr = iconv_t(-1); - try - { - ic_descr = to_UTF ? iconv_open("UTF-8", charset.c_str()) : iconv_open(charset.c_str(), "UTF-8"); - } - catch(exception &) {} - - if(ic_descr == iconv_t(-1)) - return string(str_in); - - char* inptr = (char*)str_in.data(); - size_t inleft = str_in.size(); - - string out; - char outbuf[64]; - char* outptr; - size_t outleft; - - while(inleft > 0) - { - outbuf[0] = '\0'; - outptr = (char *)outbuf; - outleft = sizeof(outbuf) - sizeof(outbuf[0]); - - size_t result = iconv(ic_descr, &inptr, &inleft, &outptr, &outleft); - if(result == size_t(-1)) - { - switch(errno) - { - case E2BIG: break; - case EILSEQ: - case EINVAL: - default: - iconv_close(ic_descr); - return string(str_in); - break; - } - } - *outptr = '\0'; - out += outbuf; - } - iconv_close(ic_descr); - - return out; -} -#endif - stack File::tempFiles; string File::confPath() @@ -167,23 +98,15 @@ File::f_string File::encodeName(string_view fileName) { if(fileName.empty()) return {}; -#if defined(_WIN32) - int len = MultiByteToWideChar(CP_UTF8, 0, fileName.data(), int(fileName.size()), nullptr, 0); - f_string out(size_t(len), 0); - len = MultiByteToWideChar(CP_UTF8, 0, fileName.data(), int(fileName.size()), out.data(), len); -#elif defined(__APPLE__) - CFMutableStringRef ref = CFStringCreateMutable(nullptr, 0); - CFStringAppendCString(ref, fileName.data(), kCFStringEncodingUTF8); - CFStringNormalize(ref, kCFStringNormalizationFormD); - +#ifdef __APPLE__ + CFStringRef ref = CFStringCreateWithBytesNoCopy({}, (UInt8 *)fileName.data(), + CFIndex(fileName.size()), kCFStringEncodingUTF8, FALSE, kCFAllocatorNull); string out(fileName.size() * 2, 0); - CFStringGetCString(ref, out.data(), CFIndex(out.size()), kCFStringEncodingUTF8); + CFStringGetFileSystemRepresentation(ref, out.data(), CFIndex(out.size())); CFRelease(ref); out.resize(strlen(out.c_str())); -#elif defined(__ANDROID__) - f_string out = string(fileName); #else - f_string out = convertUTF8(fileName,false); + f_string out = fs::u8path(fileName); #endif return out; } @@ -197,11 +120,7 @@ string File::decodeName(const f_string_view &localFileName) { if(localFileName.empty()) return {}; -#if defined(_WIN32) - int len = WideCharToMultiByte(CP_UTF8, 0, localFileName.data(), int(localFileName.size()), nullptr, 0, nullptr, nullptr); - string out(size_t(len), 0); - WideCharToMultiByte(CP_UTF8, 0, localFileName.data(), int(localFileName.size()), out.data(), len, nullptr, nullptr); -#elif defined(__APPLE__) +#ifdef __APPLE__ CFMutableStringRef ref = CFStringCreateMutable(nullptr, 0); CFStringAppendCString(ref, localFileName.data(), kCFStringEncodingUTF8); CFStringNormalize(ref, kCFStringNormalizationFormC); @@ -210,10 +129,8 @@ string File::decodeName(const f_string_view &localFileName) CFStringGetCString(ref, out.data(), CFIndex(out.size()), kCFStringEncodingUTF8); CFRelease(ref); out.resize(strlen(out.c_str())); -#elif defined(__ANDROID__) - string out = string(localFileName); #else - string out = convertUTF8(localFileName,true); + string out = fs::path(localFileName).u8string(); #endif return out; } diff --git a/src/util/File.h b/src/util/File.h index 41bbaeab6..683114a90 100644 --- a/src/util/File.h +++ b/src/util/File.h @@ -71,9 +71,6 @@ namespace digidoc #endif private: -#if !defined(_WIN32) && !defined(__APPLE__) - static std::string convertUTF8(std::string_view str_in, bool to_UTF); -#endif static std::stack tempFiles; };