+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;
#include <glib.h>
#include <curl/curl.h>
+#include "compression_wrapper.h"
#include "constants.h"
/** \defgroup misc Miscellaneous useful functions and macros.
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)
}
+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)
{
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);