From e129810ac18b44a0a11a2c25708e41b6083332e4 Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Wed, 5 Jun 2013 17:02:31 +0200 Subject: [PATCH] misc: Add cr_compress_file_with_stat function. --- src/compression_wrapper.h | 14 ++--- src/misc.c | 11 ++-- src/misc.h | 24 +++++++-- tests/test_misc.c | 129 ++++++++++++++++++++++++++++++++++------------ 4 files changed, 129 insertions(+), 49 deletions(-) diff --git a/src/compression_wrapper.h b/src/compression_wrapper.h index cbef901..632ae4f 100644 --- a/src/compression_wrapper.h +++ b/src/compression_wrapper.h @@ -99,14 +99,14 @@ const char *cr_compression_suffix(cr_CompressionType comtype); cr_CompressionType cr_detect_compression(const char* filename, GError **err); /** Open/Create the specified file. - * @param filename filename - * @param mode open mode - * @param comtype type of compression - * @param err GError ** + * @param FILENAME filename + * @param MODE open mode + * @param COMTYPE type of compression + * @param ERR GError ** * @return pointer to a CR_FILE or NULL */ -#define cr_open(FILENAME, MODE, COMPRESSION, ERR) \ - cr_open_with_stat(FILENAME, MODE, COMPRESSION, NULL, ERR) +#define cr_open(FILENAME, MODE, COMTYPE, ERR) \ + cr_open_with_stat(FILENAME, MODE, COMTYPE, NULL, ERR) /** Open/Create the specified file. For writting is possible pass * a cr_ContentStat object and after cr_close() get stats of @@ -114,7 +114,7 @@ cr_CompressionType cr_detect_compression(const char* filename, GError **err); * @param filename filename * @param mode open mode * @param comtype type of compression - * @param stat cr_ContentStat object + * @param stat cr_ContentStat object or NULL * @param err GError ** * @return pointer to a CR_FILE or NULL */ diff --git a/src/misc.c b/src/misc.c index 5d0986b..6210dd5 100644 --- a/src/misc.c +++ b/src/misc.c @@ -420,10 +420,11 @@ copy_file_cleanup: int -cr_compress_file(const char *src, - const char *in_dst, - cr_CompressionType compression, - GError **err) +cr_compress_file_with_stat(const char *src, + const char *in_dst, + cr_CompressionType compression, + cr_ContentStat *stat, + GError **err) { int ret = CRE_OK; int readed; @@ -467,7 +468,7 @@ cr_compress_file(const char *src, goto compress_file_cleanup; } - new = cr_open(dst, CR_CW_MODE_WRITE, compression, &tmp_err); + new = cr_open_with_stat(dst, CR_CW_MODE_WRITE, compression, stat, &tmp_err); if (tmp_err) { g_debug("%s: Cannot open destination file %s", __func__, dst); g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", dst); diff --git a/src/misc.h b/src/misc.h index c05cdf4..f381823 100644 --- a/src/misc.h +++ b/src/misc.h @@ -148,18 +148,32 @@ int cr_copy_file(const char *src, GError **err); /** 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) + * @param COMTYPE type of compression + * @param ERR GError ** + * @return cr_Error return code + */ +#define cr_compress_file(SRC, DST, COMTYPE, ERR) \ + cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR) + +/** 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) - * @param compression type of compression + * @param comtype type of compression + * @param stat cr_ContentStat object or NULL * @param err GError ** * @return cr_Error return code */ -int cr_compress_file(const char *src, - const char *dst, - cr_CompressionType compression, - GError **err); +int cr_compress_file_with_stat(const char *src, + const char *dst, + cr_CompressionType comtype, + cr_ContentStat *stat, + GError **err); /** Better copy file. Source (src) could be remote address (http:// or ftp://). * @param src source filename diff --git a/tests/test_misc.c b/tests/test_misc.c index b5d91ac..7375cba 100644 --- a/tests/test_misc.c +++ b/tests/test_misc.c @@ -449,7 +449,7 @@ copyfiletest_test_empty_file(Copyfiletest *copyfiletest, gconstpointer test_data ret = cr_copy_file(TEST_EMPTY_FILE, copyfiletest->dst_file, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); g_free(checksum); @@ -466,7 +466,7 @@ copyfiletest_test_text_file(Copyfiletest *copyfiletest, gconstpointer test_data) g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); ret = cr_copy_file(TEST_TEXT_FILE, copyfiletest->dst_file, NULL); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "2f395bdfa2750978965e4781ddf224c89646c7d7a1569b7ebb023b170f7bd8bb"); g_free(checksum); @@ -485,7 +485,7 @@ copyfiletest_test_binary_file(Copyfiletest *copyfiletest, gconstpointer test_dat ret = cr_copy_file(TEST_BINARY_FILE, copyfiletest->dst_file, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); g_free(checksum); @@ -503,7 +503,7 @@ copyfiletest_test_rewrite(Copyfiletest *copyfiletest, gconstpointer test_data) g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); ret = cr_copy_file(TEST_BINARY_FILE, copyfiletest->dst_file, NULL); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); g_free(checksum); @@ -511,7 +511,7 @@ copyfiletest_test_rewrite(Copyfiletest *copyfiletest, gconstpointer test_data) ret = cr_copy_file(TEST_TEXT_FILE, copyfiletest->dst_file, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "2f395bdfa2750978965e4781ddf224c89646c7d7a1569b7ebb023b170f7bd8bb"); g_free(checksum); @@ -550,15 +550,44 @@ compressfile_test_text_file(Copyfiletest *copyfiletest, gconstpointer test_data) GError *tmp_err = NULL; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file, CR_CW_GZ_COMPRESSION, &tmp_err); + ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file, + CR_CW_GZ_COMPRESSION, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); - g_assert_cmpstr(checksum, ==, "8909fde88a5747d800fd2562b0f22945f014aa7df64cf1c15c7933ae54b72ab6"); + g_assert_cmpstr(checksum, ==, "8909fde88a5747d800fd2562b0f22945f014aa7df64" + "cf1c15c7933ae54b72ab6"); g_free(checksum); } +static void +compressfile_with_stat_test_text_file(Copyfiletest *copyfiletest, gconstpointer test_data) +{ + CR_UNUSED(test_data); + int ret; + char *checksum, *checksum_; + cr_ContentStat *stat; + GError *tmp_err = NULL; + + stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err); + g_assert(stat); + g_assert(!tmp_err); + + g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); + ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file, + CR_CW_GZ_COMPRESSION, &tmp_err); + g_assert(!tmp_err); + g_assert_cmpint(ret, ==, CRE_OK); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); + checksum = cr_contentstat_free(stat, &tmp_err); + g_assert(checksum); + g_assert(!tmp_err); + checksum_ = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA256, NULL); + g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649" + "b934ca495991b7852b855"); + g_free(checksum); +} static void test_cr_download_valid_url_1(Copyfiletest *copyfiletest, gconstpointer test_data) @@ -624,7 +653,7 @@ test_cr_better_copy_file_local(Copyfiletest *copyfiletest, gconstpointer test_da ret = cr_better_copy_file(TEST_BINARY_FILE, copyfiletest->dst_file, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(copyfiletest->dst_file, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(checksum, ==, "bf68e32ad78cea8287be0f35b74fa3fecd0eaa91770b48f1a7282b015d6d883e"); g_free(checksum); @@ -644,7 +673,7 @@ test_cr_better_copy_file_url(Copyfiletest *copyfiletest, gconstpointer test_data ret = cr_better_copy_file(VALID_URL_01, copyfiletest->dst_file, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); } @@ -1011,28 +1040,64 @@ main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); - g_test_add_func("/misc/test_cr_str_to_evr", test_cr_str_to_evr); - g_test_add_func("/misc/test_cr_str_to_evr_with_chunk", test_cr_str_to_evr_with_chunk); - g_test_add_func("/misc/test_cr_is_primary", test_cr_is_primary); - g_test_add_func("/misc/test_cr_get_header_byte_range", test_cr_get_header_byte_range); - g_test_add_func("/misc/test_cr_get_filename", test_cr_get_filename); - g_test_add("/misc/copyfiletest_test_empty_file", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_empty_file, copyfiletest_teardown); - g_test_add("/misc/copyfiletest_test_text_file", Copyfiletest, NULL, copyfiletest_setup, copyfiletest_test_text_file, copyfiletest_teardown); - 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_cr_download_valid_url_1", Copyfiletest, NULL, copyfiletest_setup, test_cr_download_valid_url_1, copyfiletest_teardown); -// g_test_add("/misc/test_cr_download_valid_url_2", Copyfiletest, NULL, copyfiletest_setup, test_cr_download_valid_url_2, copyfiletest_teardown); -// g_test_add("/misc/test_cr_download_invalid_url", Copyfiletest, NULL, copyfiletest_setup, test_cr_download_invalid_url, copyfiletest_teardown); - g_test_add("/misc/test_cr_better_copy_file_local", Copyfiletest, NULL, copyfiletest_setup, test_cr_better_copy_file_local, copyfiletest_teardown); -// g_test_add("/misc/test_cr_better_copy_file_url", Copyfiletest, NULL, copyfiletest_setup, test_cr_better_copy_file_url, copyfiletest_teardown); - g_test_add_func("/misc/test_cr_normalize_dir_path", test_cr_normalize_dir_path); - g_test_add_func("/misc/test_cr_remove_dir", test_cr_remove_dir); - g_test_add_func("/misc/test_cr_str_to_version", test_cr_str_to_version); - g_test_add_func("/misc/test_cr_cmp_version_str", test_cr_cmp_version_str); - g_test_add_func("/misc/test_cr_split_rpm_filename", test_cr_split_rpm_filename); - g_test_add_func("/misc/test_cr_cmp_evr", test_cr_cmp_evr); + g_test_add_func("/misc/test_cr_str_to_evr", + test_cr_str_to_evr); + g_test_add_func("/misc/test_cr_str_to_evr_with_chunk", + test_cr_str_to_evr_with_chunk); + g_test_add_func("/misc/test_cr_is_primary", + test_cr_is_primary); + g_test_add_func("/misc/test_cr_get_header_byte_range", + test_cr_get_header_byte_range); + g_test_add_func("/misc/test_cr_get_filename", + test_cr_get_filename); + g_test_add("/misc/copyfiletest_test_empty_file", + Copyfiletest, NULL, copyfiletest_setup, + copyfiletest_test_empty_file, copyfiletest_teardown); + g_test_add("/misc/copyfiletest_test_text_file", + Copyfiletest, NULL, copyfiletest_setup, + copyfiletest_test_text_file, copyfiletest_teardown); + 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/compressfile_with_stat_test_text_file", + Copyfiletest, NULL, copyfiletest_setup, + compressfile_with_stat_test_text_file, copyfiletest_teardown); +// g_test_add("/misc/test_cr_download_valid_url_1", +// Copyfiletest, NULL, copyfiletest_setup, +// test_cr_download_valid_url_1, copyfiletest_teardown); +// g_test_add("/misc/test_cr_download_valid_url_2", +// Copyfiletest, NULL, copyfiletest_setup, +// test_cr_download_valid_url_2, copyfiletest_teardown); +// g_test_add("/misc/test_cr_download_invalid_url", +// Copyfiletest, NULL, copyfiletest_setup, +// test_cr_download_invalid_url, copyfiletest_teardown); + g_test_add("/misc/test_cr_better_copy_file_local", + Copyfiletest, NULL, copyfiletest_setup, + test_cr_better_copy_file_local, copyfiletest_teardown); +// g_test_add("/misc/test_cr_better_copy_file_url", +// Copyfiletest, NULL, copyfiletest_setup, +// test_cr_better_copy_file_url, copyfiletest_teardown); + g_test_add_func("/misc/test_cr_normalize_dir_path", + test_cr_normalize_dir_path); + g_test_add_func("/misc/test_cr_remove_dir", + test_cr_remove_dir); + g_test_add_func("/misc/test_cr_str_to_version", + test_cr_str_to_version); + g_test_add_func("/misc/test_cr_cmp_version_str", + test_cr_cmp_version_str); + g_test_add_func("/misc/test_cr_split_rpm_filename", + test_cr_split_rpm_filename); + g_test_add_func("/misc/test_cr_cmp_evr", + test_cr_cmp_evr); return g_test_run(); } -- 2.7.4