From 6e652271f4d075f1ff7383434b855cc0258fc27f Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Wed, 20 Jun 2012 09:32:46 +0200 Subject: [PATCH] Add compress_file function into the misc module --- src/misc.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/misc.h | 10 +++++++ tests/testmisc.c | 16 ++++++++++++ 3 files changed, 105 insertions(+) diff --git a/src/misc.c b/src/misc.c index a0fb46f..5792015 100644 --- a/src/misc.c +++ b/src/misc.c @@ -465,6 +465,85 @@ int copy_file(const char *src, const char *in_dst) +int compress_file(const char *src, const char *in_dst, CompressionType compression) +{ + int readed; + char buf[BUFFER_SIZE]; + + FILE *orig; + CW_FILE *new; + + if (!src) { + g_debug(MODULE"%s: File name cannot be NULL", __func__); + return CR_COPY_ERR; + } + + if (compression == AUTO_DETECT_COMPRESSION || + compression == UNKNOWN_COMPRESSION) { + g_debug(MODULE"%s: Bad compression type", __func__); + return CR_COPY_ERR; + } + + // Src must be a file NOT a directory + if (g_str_has_suffix(src, "/") || + !g_file_test(src, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) + { + g_debug(MODULE"%s: Source (%s) must be directory!", __func__, src); + return CR_COPY_ERR; + } + + gchar *dst = (gchar *) in_dst; + if (!dst) { + // If destination is NULL, use src + compression suffix + const gchar *suffix = get_suffix(compression); + dst = g_strconcat(src, suffix, NULL); + } else { + // If destination is dir use filename from src + compression suffix + if (g_str_has_suffix(in_dst, "/")) { + const gchar *suffix = get_suffix(compression); + dst = g_strconcat(in_dst, get_filename(src), suffix, NULL); + } + } + + if ((orig = fopen(src, "r")) == NULL) { + g_debug(MODULE"%s: Cannot open source file %s (%s)", __func__, src, strerror(errno)); + return CR_COPY_ERR; + } + + if ((new = cw_open(dst, CW_MODE_WRITE, compression)) == NULL) { + g_debug(MODULE"%s: Cannot open destination file %s", __func__, dst); + fclose(orig); + return CR_COPY_ERR; + } + + while ((readed = fread(buf, 1, BUFFER_SIZE, orig)) > 0) { + if (cw_write(new, buf, readed) != readed) { + g_debug(MODULE"%s: Error while copy %s -> %s", __func__, src, dst); + cw_close(new); + fclose(orig); + return CR_COPY_ERR; + } + + if (readed != BUFFER_SIZE && ferror(orig)) { + g_debug(MODULE"%s: Error while copy %s -> %s (%s)", __func__, src, dst, strerror(errno)); + cw_close(new); + fclose(orig); + return CR_COPY_ERR; + } + } + + if (dst != in_dst) { + g_free(dst); + } + + cw_close(new); + fclose(orig); + + return CR_COPY_OK; +} + + + void download(CURL *handle, const char *url, const char *in_dst, char **error) { CURLcode rcode; diff --git a/src/misc.h b/src/misc.h index 5936c83..a242db6 100644 --- a/src/misc.h +++ b/src/misc.h @@ -21,6 +21,7 @@ #include #include +#include "compression_wrapper.h" #include "constants.h" /** \defgroup misc Miscellaneous useful functions and macros. @@ -138,6 +139,15 @@ void download(CURL *handle, const char *url, const char *destination, char **err int copy_file(const char *src, const char *dst); /** \ingroup misc + * Compress file. + * @param src source filename + * @param dst destination (If dst is dir, filename of src + compression suffix is used. + * If dst is NULL, src + compression suffix is used) + * @return CR_COPY_OK or CR_COPY_ERR on error + */ +int compress_file(const char *src, const char *dst, CompressionType compression); + +/** \ingroup misc * Better copy file. Source (src) could be remote address ("http://" or "ftp://"). * @param src source filename * @param dst destination (if dst is dir, filename of src is used) diff --git a/tests/testmisc.c b/tests/testmisc.c index e3d692a..7c70781 100644 --- a/tests/testmisc.c +++ b/tests/testmisc.c @@ -579,6 +579,21 @@ static void copyfiletest_test_corner_cases(Copyfiletest *copyfiletest, gconstpoi } +static void compressfile_test_text_file(Copyfiletest *copyfiletest, gconstpointer test_data) +{ + UNUSED(test_data); + int ret; + char *checksum; + + g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); + ret = compress_file(TEXT_FILE, copyfiletest->dst_file, GZ_COMPRESSION); + g_assert_cmpint(ret, ==, CR_COPY_OK); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + checksum = compute_file_checksum(copyfiletest->dst_file, PKG_CHECKSUM_SHA256); + g_assert_cmpstr(checksum, ==, "8909fde88a5747d800fd2562b0f22945f014aa7df64cf1c15c7933ae54b72ab6"); + g_free(checksum); +} + static void test_download_valid_url_1(Copyfiletest *copyfiletest, gconstpointer test_data) { @@ -942,6 +957,7 @@ int main(int argc, char *argv[]) g_test_add("/misc/copyfiletest_test_binary_file", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_binary_file, copyfiletest_teardown); g_test_add("/misc/copyfiletest_test_rewrite", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_rewrite, copyfiletest_teardown); g_test_add("/misc/copyfiletest_test_corner_cases", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_corner_cases, copyfiletest_teardown); + g_test_add("/misc/compressfile_test_text_file", Copyfiletest, NULL, copyfiletest_setup, compressfile_test_text_file, copyfiletest_teardown); g_test_add("/misc/test_download_valid_url_1", Copyfiletest, NULL, copyfiletest_setup, test_download_valid_url_1, copyfiletest_teardown); g_test_add("/misc/test_download_valid_url_2", Copyfiletest, NULL, copyfiletest_setup, test_download_valid_url_2, copyfiletest_teardown); g_test_add("/misc/test_download_invalid_url", Copyfiletest, NULL, copyfiletest_setup, test_download_invalid_url, copyfiletest_teardown); -- 2.7.4