From c3811e003d2d57e6f9ea033e44501a41bc2e5ca8 Mon Sep 17 00:00:00 2001 From: Laurent Carlier Date: Fri, 9 Apr 2021 18:35:13 +0200 Subject: [PATCH] fix building on Ubuntu Bionic (18.04LTS) [GB.COMPRESS.ZSTD] * NEW: Fix building with Ubuntu Bionic (18.04LTS) (zstd > 1.3.2) --- .gitlab-ci.yml | 2 +- gb.compress.zstd/configure.ac | 2 +- gb.compress.zstd/src/main.c | 80 ++++++++++++++++++++++++++++++++--- 3 files changed, 76 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3caf17443..e8ea926d9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -171,7 +171,7 @@ build:ubuntu-bionic: script: - ./reconf-all - - GAMBAS_CONFIG_FAILURE=1 ./configure -C --disable-keyring --disable-zstd + - GAMBAS_CONFIG_FAILURE=1 ./configure -C --disable-keyring - make -j$(nproc) - make install diff --git a/gb.compress.zstd/configure.ac b/gb.compress.zstd/configure.ac index 229f4b29a..071b79c42 100644 --- a/gb.compress.zstd/configure.ac +++ b/gb.compress.zstd/configure.ac @@ -11,7 +11,7 @@ dnl ---- zstd compression driver GB_COMPONENT_PKG_CONFIG( zstd, ZSTD, gb.compress.zstd, [src], - 'libzstd >= 1.3.8') + 'libzstd >= 1.3.3') dnl ---- Create makefiles diff --git a/gb.compress.zstd/src/main.c b/gb.compress.zstd/src/main.c index e3d135341..20065d8c7 100644 --- a/gb.compress.zstd/src/main.c +++ b/gb.compress.zstd/src/main.c @@ -92,7 +92,11 @@ static int ZSTD_max_compression(void) static int ZSTD_min_compression(void) { + #if ZSTD_VERSION_NUMBER < 10308 // ubuntu 18.04 LTS - zstd v1.3.3 / debian stable is v1.3.8 + return 1; + #else return ZSTD_minCLevel(); + #endif } static int ZSTD_default_compression(void) @@ -158,16 +162,73 @@ static void ZSTD_c_File(char *source,char *target,int level) GB.Error("Error while compressing file: ZSTD_createCCtx() failed!"); goto error_cfile; } + + #if ZSTD_VERSION_NUMBER < 10308 // ubuntu 18.04 LTS - zstd v1.3.3 / debian stable is v1.3.8 + size_t const retValue = ZSTD_initCStream(cctx, level); + if (ZSTD_isError(retValue)) + { + GB.Error("Error while compressing file, ZSTD_initCStream() failed: &1", ZSTD_getErrorName(retValue)); + goto error_cfile; + } + + while(!feof(f_src)) + { + size_t len = fread(buffIn, 1, buffInSize, f_src); + if (len < buffInSize) + { + if (ferror(f_src)) + { + GB.Error("Error while reading data: &1", strerror(errno)); + goto error_cfile; + } + } + if (len > 0) + { + ZSTD_inBuffer input = { buffIn, len, 0 }; + int finished; + do { + ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; + size_t const remaining = ZSTD_compressStream(cctx, &output , &input); + + if (ZSTD_isError(remaining)) + { + GB.Error("Error while compressing file: &1", ZSTD_getErrorName(remaining)); + goto error_cfile; + } + if (fwrite(buffOut, 1, output.pos, f_dst) != output.pos) + { + GB.Error("Error while writing data: &1", strerror(errno)); + goto error_cfile; + } + finished = remaining; + } while (!finished); + + ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; + size_t const remaining = ZSTD_endStream(cctx, &output); + + if (remaining) + { + GB.Error("Error while compressing file: not fully flushed!"); + goto error_cfile; + } + if (fwrite(buffOut, 1, output.pos, f_dst) != output.pos) + { + GB.Error("Error while writing flushed data: &1", strerror(errno)); + goto error_cfile; + } + } + } + + #else // TODO: check errors ? ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level); ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1); - ZSTD_EndDirective mode = ZSTD_e_continue; + while(!feof(f_src)) { - size_t len = fread(buffIn, 1, buffInSize, f_src); - + size_t len = fread(buffIn, 1, buffInSize, f_src); if (len < buffInSize) { if (ferror(f_src)) @@ -177,7 +238,6 @@ static void ZSTD_c_File(char *source,char *target,int level) } mode = ZSTD_e_end; } - if (len > 0) { ZSTD_inBuffer input = { buffIn, len, 0 }; @@ -196,12 +256,12 @@ static void ZSTD_c_File(char *source,char *target,int level) GB.Error("Error while writing data: &1", strerror(errno)); goto error_cfile; } - finished = (len < buffInSize) ? (remaining == 0) : (input.pos == input.size); } while (!finished); } } - + #endif /* ZSTD_VERSION_NUMBER < 10308 */ + error_cfile: if (cctx != NULL) ZSTD_freeCCtx(cctx); @@ -288,6 +348,14 @@ static void ZSTD_u_File(char *source,char *target) goto error_ufile; } + #if ZSTD_VERSION_NUMBER < 10308 // ubuntu 18.04 LTS - zstd v1.3.3 / debian stable is v1.3.8 + size_t const retValue = ZSTD_initDStream(dctx); + if (ZSTD_isError(retValue)) + { + GB.Error("Error while decompressing file, ZSTD_initDStream() failed: &1", ZSTD_getErrorName(retValue)); + goto error_ufile; + } + #endif /* ZSTD_VERSION_NUMBER < 10308 */ while(!feof(f_src)) { size_t len = fread(buffIn, 1, buffInSize, f_src);