From a6d9225ae10a8ef46c5b2673111080cd9fa72911 Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Mon, 27 May 2013 15:05:06 +0200 Subject: [PATCH] compression_wrapper: Add GError based error reporting support. --- VERSION.cmake | 4 +- src/compression_wrapper.c | 403 ++++++++++++++++++++++++++++++-------- src/compression_wrapper.h | 25 ++- src/error.c | 6 + src/error.h | 40 ++-- src/load_metadata.c | 28 +-- src/misc.c | 13 +- src/repomd.c | 91 ++++++--- src/xml_file.c | 46 +++-- src/xml_parser_filelists.c | 30 +-- tests/test_compression_wrapper.c | 235 +++++++++++++++------- tests/test_xml_file.c | 10 +- tests/test_xml_parser_filelists.c | 1 + 13 files changed, 664 insertions(+), 268 deletions(-) diff --git a/VERSION.cmake b/VERSION.cmake index ae678c5..e5eb169 100644 --- a/VERSION.cmake +++ b/VERSION.cmake @@ -1,3 +1,3 @@ SET(CR_MAJOR "0") -SET(CR_MINOR "1") -SET(CR_PATCH "17") +SET(CR_MINOR "2") +SET(CR_PATCH "0") diff --git a/src/compression_wrapper.c b/src/compression_wrapper.c index 8285150..10c0ffc 100644 --- a/src/compression_wrapper.c +++ b/src/compression_wrapper.c @@ -22,12 +22,14 @@ #include #include #include +#include #include #include #include #include #include #include "logging.h" +#include "error.h" #include "compression_wrapper.h" /* @@ -92,13 +94,18 @@ typedef struct { cr_CompressionType -cr_detect_compression(const char *filename) +cr_detect_compression(const char *filename, GError **err) { cr_CompressionType type = CR_CW_UNKNOWN_COMPRESSION; + assert(filename); + assert(!err || *err == NULL); + if (!g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) { g_debug("%s: File %s doesn't exists or it's not a regular file", __func__, filename); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_NOFILE, + "File %s doesn't exists or it's not a regular file.", filename); return type; } @@ -124,11 +131,23 @@ cr_detect_compression(const char *filename) // No success? Let's get hardcore... (Use magic bytes) - //magic_t myt = magic_open(MAGIC_MIME_TYPE); magic_t myt = magic_open(MAGIC_MIME); - magic_load(myt, NULL); + if (myt == NULL) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_MAGIC, + "magic_open() failed: Cannot allocate the magic cookie"); + return type; + } + + if (magic_load(myt, NULL) == -1) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_MAGIC, + "magic_load() failed: %s", magic_error(myt)); + return type; + } + if (magic_check(myt, NULL) == -1) { - g_critical("%s: magic_check() failed", __func__); + g_critical("%s: magic_check() failed: %s", __func__, magic_error(myt)); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_MAGIC, + "magic_check() failed: %s", magic_error(myt)); return type; } @@ -174,16 +193,17 @@ cr_detect_compression(const char *filename) type = CR_CW_NO_COMPRESSION; } } else { - g_debug("%s: Mime type not detected! (%s)", __func__, filename); + g_debug("%s: Mime type not detected! (%s): %s", __func__, filename, + magic_error(myt)); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_MAGIC, + "mime_type() detection failed: %s", magic_error(myt)); } // Xml detection if (type == CR_CW_UNKNOWN_COMPRESSION && g_str_has_suffix(filename, ".xml")) - { type = CR_CW_NO_COMPRESSION; - } magic_close(myt); @@ -210,46 +230,64 @@ cr_compression_suffix(cr_CompressionType comtype) } +static char * +cr_gz_strerror(gzFile f) +{ + char *msg = ""; + int errnum = Z_ERRNO; + + msg = (char *) gzerror(f, &errnum); + if (errnum == Z_ERRNO) + msg = strerror(errno); + return msg; +} + + CR_FILE * -cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) +cr_open(const char *filename, + cr_OpenMode mode, + cr_CompressionType comtype, + GError **err) { CR_FILE *file = NULL; - cr_CompressionType type; + cr_CompressionType type = comtype; + GError *tmp_err = NULL; + assert(filename); + assert(mode == CR_CW_MODE_READ || mode == CR_CW_MODE_WRITE); assert(mode < CR_CW_MODE_SENTINEL); assert(comtype < CR_CW_COMPRESSION_SENTINEL); - - if (!filename || (mode != CR_CW_MODE_READ && mode != CR_CW_MODE_WRITE)) { - g_debug("%s: Filename is NULL or bad mode value", __func__); - return NULL; - } - - - // Compression type detection + assert(!err || *err == NULL); if (mode == CR_CW_MODE_WRITE) { if (comtype == CR_CW_AUTO_DETECT_COMPRESSION) { g_debug("%s: CR_CW_AUTO_DETECT_COMPRESSION cannot be used if " "mode is CR_CW_MODE_WRITE", __func__); - return NULL; + assert(0); } if (comtype == CR_CW_UNKNOWN_COMPRESSION) { g_debug("%s: CR_CW_UNKNOWN_COMPRESSION cannot be used if mode" " is CR_CW_MODE_WRITE", __func__); - return NULL; + assert(0); } } - if (comtype != CR_CW_AUTO_DETECT_COMPRESSION) { - type = comtype; - } else { - type = cr_detect_compression(filename); + if (comtype == CR_CW_AUTO_DETECT_COMPRESSION) + type = cr_detect_compression(filename, &tmp_err); + + if (tmp_err) { + // Error while detection + g_propagate_error(err, tmp_err); + return NULL; } if (type == CR_CW_UNKNOWN_COMPRESSION) { + // Detection without error but compression type is unknown g_debug("%s: Cannot detect compression type", __func__); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_UNKNOWNCOMPRESSION, + "Cannot detect compression type"); return NULL; } @@ -263,11 +301,15 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) case (CR_CW_NO_COMPRESSION): // ---------------------------------------- file->type = CR_CW_NO_COMPRESSION; - if (mode == CR_CW_MODE_WRITE) { + if (mode == CR_CW_MODE_WRITE) file->FILE = (void *) fopen(filename, "w"); - } else { + else file->FILE = (void *) fopen(filename, "r"); - } + + if (!file->FILE) + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fopen(): %s", strerror(errno)); + break; case (CR_CW_GZ_COMPRESSION): // ---------------------------------------- @@ -281,18 +323,28 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) file->FILE = (void *) gzopen(filename, "rb"); } + if (!file->FILE) + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fopen(): %s", strerror(errno)); + if (gzbuffer((gzFile) file->FILE, GZ_BUFFER_SIZE) == -1) { g_debug("%s: gzbuffer() call failed", __func__); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ, + "gzbuffer() call failed"); } + break; case (CR_CW_BZ2_COMPRESSION): // --------------------------------------- file->type = CR_CW_BZ2_COMPRESSION; FILE *f; + int bzerror = BZ_OK; + if (mode == CR_CW_MODE_WRITE) { f = fopen(filename, "wb"); if (f) { - file->FILE = (void *) BZ2_bzWriteOpen(NULL, f, + file->FILE = (void *) BZ2_bzWriteOpen(&bzerror, + f, BZ2_BLOCKSIZE100K, BZ2_VERBOSITY, BZ2_WORK_FACTOR); @@ -300,12 +352,41 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) } else { f = fopen(filename, "rb"); if (f) { - file->FILE = (void *) BZ2_bzReadOpen(NULL, f, + file->FILE = (void *) BZ2_bzReadOpen(&bzerror, + f, BZ2_VERBOSITY, BZ2_USE_LESS_MEMORY, NULL, 0); } } + + if (!f) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fopen(): %s", strerror(errno)); + } else if (bzerror != BZ_OK) { + const char *err_msg; + + switch (bzerror) { + case BZ_CONFIG_ERROR: + err_msg = "library has been mis-compiled"; + break; + case BZ_PARAM_ERROR: + err_msg = "bad function params"; + break; + case BZ_IO_ERROR: + err_msg = "ferror(f) is nonzero"; + break; + case BZ_MEM_ERROR: + err_msg = "insufficient memory is available"; + break; + default: + err_msg = "other error"; + } + + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BZ2, + "Bz2 error: %s", err_msg); + } + break; case (CR_CW_XZ_COMPRESSION): // ---------------------------------------- @@ -333,23 +414,25 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) XZ_DECODER_FLAGS); if (ret != LZMA_OK) { + const char *err_msg; + switch (ret) { case LZMA_MEM_ERROR: - g_debug("%s: XZ: Cannot allocate memory", - __func__); + err_msg = "Cannot allocate memory"; break; case LZMA_OPTIONS_ERROR: - g_debug("%s: XZ: Unsupported flags (options)", - __func__); + err_msg = "Unsupported flags (options)"; break; case LZMA_PROG_ERROR: - g_debug("%s: XZ: One or more of the parameters " - "have values that will never be valid.", - __func__); + err_msg = "One or more of the parameters " + "have values that will never be valid."; break; default: - g_debug("%s: XZ: Unknown error", __func__); + err_msg = "Unknown error"; } + + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ error: %s", err_msg); g_free((void *) xz_file); break; } @@ -362,6 +445,9 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) xz_file->file = fopen(filename, "rb"); if (!(xz_file->file)) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "fopen(): %s", strerror(errno)); + lzma_end(&(xz_file->stream)); g_free((void *) xz_file); } @@ -373,55 +459,81 @@ cr_open(const char *filename, cr_OpenMode mode, cr_CompressionType comtype) break; } - if (!file->FILE) { // File is not open -> cleanup g_free(file); - file = NULL; + if (err && *err == NULL) + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "Unknown error while opening: %s", filename); + return NULL; } + assert(!err || (!file && *err != NULL) || (file && *err == NULL)); + return file; } int -cr_close(CR_FILE *cr_file) +cr_close(CR_FILE *cr_file, GError **err) { - if (!cr_file) { - return CR_CW_ERR; - } - - int ret; int cw_ret = CR_CW_ERR; + assert(!err || *err == NULL); + + if (!cr_file) + return CRE_OK; + switch (cr_file->type) { case (CR_CW_NO_COMPRESSION): // ---------------------------------------- ret = fclose((FILE *) cr_file->FILE); - if (ret == 0) { + if (ret == 0) cw_ret = CR_CW_OK; - } + else + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fclose(): %s", strerror(errno)); + break; case (CR_CW_GZ_COMPRESSION): // ---------------------------------------- ret = gzclose((gzFile) cr_file->FILE); if (ret == Z_OK) { cw_ret = CR_CW_OK; + } else { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ, + "gzclose(): %s", cr_gz_strerror((gzFile) cr_file->FILE)); } break; case (CR_CW_BZ2_COMPRESSION): // --------------------------------------- - if (cr_file->mode == CR_CW_MODE_READ) { + if (cr_file->mode == CR_CW_MODE_READ) BZ2_bzReadClose(&ret, (BZFILE *) cr_file->FILE); - } else { + else BZ2_bzWriteClose(&ret, (BZFILE *) cr_file->FILE, BZ2_SKIP_FFLUSH, NULL, NULL); - } if (ret == BZ_OK) { cw_ret = CR_CW_OK; + } else { + const char *err_msg; + + switch (ret) { + case BZ_SEQUENCE_ERROR: + // This really shoud not happen + err_msg = "file was opened with BZ2_bzReadOpen"; + break; + case BZ_IO_ERROR: + err_msg = "error writing the compressed file"; + break; + default: + err_msg = "other error"; + } + + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BZ2, + "Bz2 error: %s", err_msg); } break; @@ -439,12 +551,16 @@ cr_close(CR_FILE *cr_file) if(ret != LZMA_OK && ret != LZMA_STREAM_END) { // Error while coding + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ: lzma_code() error"); break; } size_t out_len = XZ_BUFFER_SIZE - stream->avail_out; if(fwrite(xz_file->buffer, 1, out_len, xz_file->file) != out_len) { // Error while writing + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ: fwrite() error: %s", strerror(errno)); break; } @@ -465,27 +581,36 @@ cr_close(CR_FILE *cr_file) } default: // ------------------------------------------------------------ + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "Bad compressed file type"); break; } g_free(cr_file); + assert(!err || (cw_ret == CR_CW_ERR && *err != NULL) + || (cw_ret != CR_CW_ERR && *err == NULL)); + return cw_ret; } int -cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) +cr_read(CR_FILE *cr_file, void *buffer, unsigned int len, GError **err) { int bzerror; int ret; assert(cr_file); assert(buffer); + assert(!err || *err == NULL); - if (cr_file->mode != CR_CW_MODE_READ) + if (cr_file->mode != CR_CW_MODE_READ) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "File is not opened in read mode"); return CR_CW_ERR; + } switch (cr_file->type) { @@ -493,11 +618,18 @@ cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) ret = fread(buffer, 1, len, (FILE *) cr_file->FILE); if ((ret != (int) len) && !feof((FILE *) cr_file->FILE)) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fread(): %s", strerror(errno)); } break; case (CR_CW_GZ_COMPRESSION): // ---------------------------------------- - return gzread((gzFile) cr_file->FILE, buffer, len); + ret = gzread((gzFile) cr_file->FILE, buffer, len); + if (ret == -1) { + ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ, + "fread(): %s", cr_gz_strerror((gzFile) cr_file->FILE)); + } break; case (CR_CW_BZ2_COMPRESSION): // --------------------------------------- @@ -505,8 +637,45 @@ cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) if (!ret && bzerror == BZ_SEQUENCE_ERROR) // Next read after BZ_STREAM_END (EOF) return 0; + if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) { - return CR_CW_ERR; + const char *err_msg; + ret = CR_CW_ERR; + + switch (bzerror) { + case BZ_PARAM_ERROR: + // This shoud not happend + err_msg = "bad function params!"; + break; + case BZ_SEQUENCE_ERROR: + // This shoud not happend + err_msg = "file was opened with BZ2_bzWriteOpen"; + break; + case BZ_IO_ERROR: + err_msg = "error while reading from the compressed file"; + break; + case BZ_UNEXPECTED_EOF: + err_msg = "the compressed file ended before " + "the logical end-of-stream was detected"; + break; + case BZ_DATA_ERROR: + err_msg = "data integrity error was detected in " + "the compressed stream"; + break; + case BZ_DATA_ERROR_MAGIC: + err_msg = "the stream does not begin with " + "the requisite header bytes (ie, is not " + "a bzip2 data file)."; + break; + case BZ_MEM_ERROR: + err_msg = "insufficient memory was available"; + break; + default: + err_msg = "other error"; + } + + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BZ2, + "Bz2 error: %s", err_msg); } break; @@ -524,6 +693,8 @@ cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) if (stream->avail_in == 0) { if ((lret = fread(xz_file->buffer, 1, XZ_BUFFER_SIZE, xz_file->file)) < 0) { g_debug("%s: XZ: Error while fread", __func__); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ: fread(): %s", strerror(errno)); return CR_CW_ERR; // Error while reading input file } else if (lret == 0) { g_debug("%s: EOF", __func__); @@ -537,11 +708,13 @@ cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) lret = lzma_code(stream, LZMA_RUN); if (lret != LZMA_OK && lret != LZMA_STREAM_END) { g_debug("%s: XZ: Error while decoding (%d)", __func__, lret); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ: Error while deconding(%d)", lret); return CR_CW_ERR; // Error while decoding } - if (lret == LZMA_STREAM_END) { + + if (lret == LZMA_STREAM_END) break; - } } ret = len - stream->avail_out; @@ -550,31 +723,42 @@ cr_read(CR_FILE *cr_file, void *buffer, unsigned int len) default: // ------------------------------------------------------------ ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "Bad compressed file type"); break; } + assert(!err || (ret == CR_CW_ERR && *err != NULL) + || (ret != CR_CW_ERR && *err == NULL)); + return ret; } int -cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) +cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len, GError **err) { int bzerror; int ret = CR_CW_ERR; assert(cr_file); assert(buffer); + assert(!err || *err == NULL); - if (cr_file->mode != CR_CW_MODE_WRITE) + if (cr_file->mode != CR_CW_MODE_WRITE) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "File is not opened in read mode"); return ret; + } switch (cr_file->type) { case (CR_CW_NO_COMPRESSION): // ---------------------------------------- if ((ret = (int) fwrite(buffer, 1, len, (FILE *) cr_file->FILE)) != (int) len) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fwrite(): %s", strerror(errno)); } break; @@ -583,8 +767,11 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) ret = 0; break; } + if ((ret = gzwrite((gzFile) cr_file->FILE, buffer, len)) == 0) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ, + "gzwrite(): %s", cr_gz_strerror((gzFile) cr_file->FILE)); } break; @@ -593,7 +780,27 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) if (bzerror == BZ_OK) { ret = len; } else { + const char *err_msg; ret = CR_CW_ERR; + + switch (bzerror) { + case BZ_PARAM_ERROR: + // This shoud not happend + err_msg = "bad function params!"; + break; + case BZ_SEQUENCE_ERROR: + // This shoud not happend + err_msg = "file was opened with BZ2_bzReadOpen"; + break; + case BZ_IO_ERROR: + err_msg = "error while reading from the compressed file"; + break; + default: + err_msg = "other error"; + } + + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BZ2, + "Bz2 error: %s", err_msg); } break; @@ -612,12 +819,16 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) lret = lzma_code(stream, LZMA_RUN); if (lret != LZMA_OK) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ: lzma_code() error"); break; // Error while coding } size_t out_len = XZ_BUFFER_SIZE - stream->avail_out; if ((fwrite(xz_file->buffer, 1, out_len, xz_file->file)) != out_len) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_XZ, + "XZ: fwrite(): %s", strerror(errno)); break; // Error while writing } } @@ -626,90 +837,109 @@ cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len) } default: // ------------------------------------------------------------ + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "Bad compressed file type"); break; } + assert(!err || (ret == CR_CW_ERR && *err != NULL) + || (ret != CR_CW_ERR && *err == NULL)); + return ret; } int -cr_puts(CR_FILE *cr_file, const char *str) +cr_puts(CR_FILE *cr_file, const char *str, GError **err) { size_t len; - int bzerror; int ret = CR_CW_ERR; assert(cr_file); + assert(!err || *err == NULL); + + if (!str) + return CR_CW_OK; - if (!str || cr_file->mode != CR_CW_MODE_WRITE) + if (cr_file->mode != CR_CW_MODE_WRITE) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "File is not opened in write mode"); return CR_CW_ERR; + } switch (cr_file->type) { case (CR_CW_NO_COMPRESSION): // ---------------------------------------- if (fputs(str, (FILE *) cr_file->FILE) != EOF) { ret = CR_CW_OK; + } else { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fputs(): %s", strerror(errno)); } break; case (CR_CW_GZ_COMPRESSION): // ---------------------------------------- if (gzputs((gzFile) cr_file->FILE, str) != -1) { ret = CR_CW_OK; - } - break; - - case (CR_CW_BZ2_COMPRESSION): // --------------------------------------- - len = strlen(str); - BZ2_bzWrite(&bzerror, (BZFILE *) cr_file->FILE, (void *) str, len); - if (bzerror == BZ_OK) { - ret = CR_CW_OK; } else { - ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ, + "gzputs(): %s", cr_gz_strerror((gzFile) cr_file->FILE)); } break; + case (CR_CW_BZ2_COMPRESSION): // --------------------------------------- case (CR_CW_XZ_COMPRESSION): // ---------------------------------------- len = strlen(str); - ret = cr_write(cr_file, str, len); - if (ret == (int) len) { + ret = cr_write(cr_file, str, len, err); + if (ret == (int) len) ret = CR_CW_OK; - } else { + else ret = CR_CW_ERR; - } break; default: // ------------------------------------------------------------ + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "Bad compressed file type"); break; } + assert(!err || (ret == CR_CW_ERR && *err != NULL) + || (ret != CR_CW_ERR && *err == NULL)); + return ret; } int -cr_printf(CR_FILE *cr_file, const char *format, ...) +cr_printf(GError **err, CR_FILE *cr_file, const char *format, ...) { va_list vl; int ret; gchar *buf = NULL; assert(cr_file); + assert(!err || *err == NULL); - if (!format || cr_file->mode != CR_CW_MODE_WRITE) - return CR_CW_ERR; + if (!format) + return 0; - va_start(vl, format); + if (cr_file->mode != CR_CW_MODE_WRITE) { + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "File is not opened in write mode"); + return CR_CW_ERR; + } // Fill format string + va_start(vl, format); ret = g_vasprintf(&buf, format, vl); - va_end(vl); if (ret < 0) { g_debug("%s: vasprintf() call failed", __func__); + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_MEMORY, + "vasprintf() call failed"); return CR_CW_ERR; } @@ -722,24 +952,22 @@ cr_printf(CR_FILE *cr_file, const char *format, ...) case (CR_CW_NO_COMPRESSION): // ---------------------------------------- if ((ret = fwrite(buf, 1, ret, cr_file->FILE)) < 0) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_IO, + "fwrite(): %s", strerror(errno)); } break; case (CR_CW_GZ_COMPRESSION): // ---------------------------------------- if (gzputs((gzFile) cr_file->FILE, buf) == -1) { ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_GZ, + "gzputs(): %s", cr_gz_strerror((gzFile) cr_file->FILE)); } break; case (CR_CW_BZ2_COMPRESSION): // --------------------------------------- - BZ2_bzWrite(&tmp_ret, (BZFILE *) cr_file->FILE, buf, ret); - if (tmp_ret != BZ_OK) { - ret = CR_CW_ERR; - } - break; - case (CR_CW_XZ_COMPRESSION): // ---------------------------------------- - tmp_ret = cr_write(cr_file, buf, ret); + tmp_ret = cr_write(cr_file, buf, ret, err); if (tmp_ret != (int) ret) { ret = CR_CW_ERR; } @@ -747,10 +975,15 @@ cr_printf(CR_FILE *cr_file, const char *format, ...) default: // ------------------------------------------------------------ ret = CR_CW_ERR; + g_set_error(err, CR_COMPRESSION_WRAPPER_ERROR, CRE_BADARG, + "Bad compressed file type"); break; } g_free(buf); + assert(!err || (ret == CR_CW_ERR && *err != NULL) + || (ret != CR_CW_ERR && *err == NULL)); + return ret; } diff --git a/src/compression_wrapper.h b/src/compression_wrapper.h index 7a21058..26b1c3a 100644 --- a/src/compression_wrapper.h +++ b/src/compression_wrapper.h @@ -69,57 +69,68 @@ const char *cr_compression_suffix(cr_CompressionType comtype); /** Detect a compression type of the specified file. * @param filename filename + * @param err GError ** * @return detected type of compression */ -cr_CompressionType cr_detect_compression(const char* filename); +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 ** * @return pointer to a CR_FILE or NULL */ CR_FILE *cr_open(const char *filename, cr_OpenMode mode, - cr_CompressionType comtype); + cr_CompressionType comtype, + GError **err); /** Reads an array of len bytes from the CR_FILE. * @param cr_file CR_FILE pointer * @param buffer target buffer * @param len number of bytes to read + * @param err GError ** * @return number of readed bytes or CR_CW_ERR */ -int cr_read(CR_FILE *cr_file, void *buffer, unsigned int len); +int cr_read(CR_FILE *cr_file, void *buffer, unsigned int len, GError **err); /** Writes the array of len bytes from buffer to the cr_file. * @param cr_file CR_FILE pointer * @param buffer source buffer * @param len number of bytes to read + * @param err GError ** * @return number of uncompressed bytes readed (0 = EOF) * or CR_CW_ERR */ -int cr_write(CR_FILE *cr_file, const void *buffer, unsigned int len); +int cr_write(CR_FILE *cr_file, + const void *buffer, + unsigned int len, + GError **err); /** Writes the string pointed by str into the cr_file. * @param cr_file CR_FILE pointer * @param str null terminated ('\0') string + * @param err GError ** * @return number of uncompressed bytes writed or CR_CW_ERR */ -int cr_puts(CR_FILE *cr_file, const char *str); +int cr_puts(CR_FILE *cr_file, const char *str, GError **err); /** Writes a formatted string into the cr_file. + * @param err GError ** * @param cr_file CR_FILE pointer * @param format format string * @param ... list of additional arguments as specified in format * @return CR_CW_OK or CR_CW_ERR */ -int cr_printf(CR_FILE *cr_file, const char *format, ...); +int cr_printf(GError **err, CR_FILE *cr_file, const char *format, ...); /** Closes the CR_FILE. * @param cr_file CR_FILE pointer + * @param err GError ** * @return CR_CW_OK or CR_CW_ERR */ -int cr_close(CR_FILE *cr_file); +int cr_close(CR_FILE *cr_file, GError **err); /** @} */ diff --git a/src/error.c b/src/error.c index 4650d7d..b021527 100644 --- a/src/error.c +++ b/src/error.c @@ -71,6 +71,12 @@ cr_checksum_error_quark(void) } GQuark +cr_compression_wrapper_error_quark(void) +{ + return g_quark_from_static_string("cr_compression_wrapper_error"); +} + +GQuark cr_db_error_quark(void) { return g_quark_from_static_string("cr_db_error"); diff --git a/src/error.h b/src/error.h index 4c20bc4..b5ad55a 100644 --- a/src/error.h +++ b/src/error.h @@ -60,6 +60,16 @@ typedef enum { (16) Bad filelists.xml file */ CRE_BADXMLOTHER, /*!< (17) Bad filelists.xml file */ + CRE_BADXMLREPOMD, /*!< + (18) Bad repomd.xml file */ + CRE_MAGIC, /*!< + (19) Magic Number Recognition Library (libmagic) error */ + CRE_GZ, /*!< + (20) Gzip library related error */ + CRE_BZ2, /*!< + (21) Bzip2 library related error */ + CRE_XZ, /*!< + (22) Xz (lzma) library related error */ } cr_Error; /** Converts cr_Error return code to error string. @@ -69,22 +79,24 @@ typedef enum { const char *cr_strerror(cr_Error rc); /* Error domains */ -#define CR_CHECKSUM_ERROR cr_checksum_error_quark() -#define CR_DB_ERROR cr_db_error_quark() -#define CR_LOAD_METADATA_ERROR cr_load_metadata_error_quark() -#define CR_LOCATE_METADATA_ERROR cr_locate_metadata_error_quark() -#define CR_PARSEPKG_ERROR cr_parsepkg_error_quark() -#define CR_REPOMD_ERROR cr_repomd_error_quark() -#define CR_REPOMD_RECORD_ERROR cr_repomd_record_error_quark() -#define CR_XML_DUMP_FILELISTS_ERROR cr_xml_dump_filelists_error_quark() -#define CR_XML_DUMP_OTHER_ERROR cr_xml_dump_other_error_quark() -#define CR_XML_DUMP_PRIMARY_ERROR cr_xml_dump_primary_error_quark() -#define CR_XML_FILE_ERROR cr_xml_file_error_quark() -#define CR_XML_PARSER_FIL_ERROR cr_xml_parser_fil_error_quark() -#define CR_XML_PARSER_OTH_ERROR cr_xml_parser_oth_error_quark() -#define CR_XML_PARSER_PRI_ERROR cr_xml_parser_pri_error_quark() +#define CR_CHECKSUM_ERROR cr_checksum_error_quark() +#define CR_COMPRESSION_WRAPPER_ERROR cr_compression_wrapper_error_quark() +#define CR_DB_ERROR cr_db_error_quark() +#define CR_LOAD_METADATA_ERROR cr_load_metadata_error_quark() +#define CR_LOCATE_METADATA_ERROR cr_locate_metadata_error_quark() +#define CR_PARSEPKG_ERROR cr_parsepkg_error_quark() +#define CR_REPOMD_ERROR cr_repomd_error_quark() +#define CR_REPOMD_RECORD_ERROR cr_repomd_record_error_quark() +#define CR_XML_DUMP_FILELISTS_ERROR cr_xml_dump_filelists_error_quark() +#define CR_XML_DUMP_OTHER_ERROR cr_xml_dump_other_error_quark() +#define CR_XML_DUMP_PRIMARY_ERROR cr_xml_dump_primary_error_quark() +#define CR_XML_FILE_ERROR cr_xml_file_error_quark() +#define CR_XML_PARSER_FIL_ERROR cr_xml_parser_fil_error_quark() +#define CR_XML_PARSER_OTH_ERROR cr_xml_parser_oth_error_quark() +#define CR_XML_PARSER_PRI_ERROR cr_xml_parser_pri_error_quark() GQuark cr_checksum_error_quark(void); +GQuark cr_compression_wrapper_error_quark(void); GQuark cr_db_error_quark(void); GQuark cr_load_metadata_error_quark(void); GQuark cr_locate_metadata_error_quark(void); diff --git a/src/load_metadata.c b/src/load_metadata.c index 164661b..7e2013f 100644 --- a/src/load_metadata.c +++ b/src/load_metadata.c @@ -1063,37 +1063,39 @@ cr_load_xml_files(GHashTable *hashtable, CR_FILE *pri_xml_cwfile, *fil_xml_cwfile, *oth_xml_cwfile; XML_Parser pri_p, fil_p, oth_p; struct ParserData parser_data; + GError *tmp_err = NULL; assert(!err || *err == NULL); // Detect compression type - compression_type = cr_detect_compression(primary_xml_path); + compression_type = cr_detect_compression(primary_xml_path, &tmp_err); if (compression_type == CR_CW_UNKNOWN_COMPRESSION) { + ret = tmp_err->code; g_debug("%s: Unknown compression", __func__); - g_set_error(err, CR_LOAD_METADATA_ERROR, CRE_UNKNOWNCOMPRESSION, - "Unknown compression of %s", primary_xml_path); - return CRE_UNKNOWNCOMPRESSION; + g_propagate_prefixed_error(err, tmp_err, + "Compression detection of %s: ", primary_xml_path); + return ret; } // Open files - if (!(pri_xml_cwfile = cr_open(primary_xml_path, CR_CW_MODE_READ, compression_type))) { + if (!(pri_xml_cwfile = cr_open(primary_xml_path, CR_CW_MODE_READ, compression_type, NULL))) { g_debug("%s: Cannot open file: %s", __func__, primary_xml_path); g_set_error(err, CR_LOAD_METADATA_ERROR, CRE_IO, "Cannot open %s", primary_xml_path); return CRE_IO; } - if (!(fil_xml_cwfile = cr_open(filelists_xml_path, CR_CW_MODE_READ, compression_type))) { + if (!(fil_xml_cwfile = cr_open(filelists_xml_path, CR_CW_MODE_READ, compression_type, NULL))) { g_debug("%s: Cannot open file: %s", __func__, filelists_xml_path); g_set_error(err, CR_LOAD_METADATA_ERROR, CRE_IO, "Cannot open %s", primary_xml_path); return CRE_IO; } - if (!(oth_xml_cwfile = cr_open(other_xml_path, CR_CW_MODE_READ, compression_type))) { + if (!(oth_xml_cwfile = cr_open(other_xml_path, CR_CW_MODE_READ, compression_type, NULL))) { g_debug("%s: Cannot open file: %s", __func__, other_xml_path); g_set_error(err, CR_LOAD_METADATA_ERROR, CRE_IO, "Cannot open %s", primary_xml_path); @@ -1144,7 +1146,7 @@ cr_load_xml_files(GHashTable *hashtable, break; } - pri_len = cr_read(pri_xml_cwfile, (void *) pri_buff, CHUNK_SIZE); + pri_len = cr_read(pri_xml_cwfile, (void *) pri_buff, CHUNK_SIZE, NULL); if (pri_len < 0) { g_critical("%s: Read error", __func__); ret = CRE_IO; @@ -1190,7 +1192,7 @@ cr_load_xml_files(GHashTable *hashtable, break; } - fil_len = cr_read(fil_xml_cwfile, (void *) fil_buff, CHUNK_SIZE); + fil_len = cr_read(fil_xml_cwfile, (void *) fil_buff, CHUNK_SIZE, NULL); if (fil_len < 0) { g_critical("%s: Read error", __func__); ret = CRE_IO; @@ -1236,7 +1238,7 @@ cr_load_xml_files(GHashTable *hashtable, break; } - oth_len = cr_read(oth_xml_cwfile, (void *) oth_buff, CHUNK_SIZE); + oth_len = cr_read(oth_xml_cwfile, (void *) oth_buff, CHUNK_SIZE, NULL); if (oth_len < 0) { g_critical("%s: Read error", __func__); ret = CRE_IO; @@ -1276,9 +1278,9 @@ cleanup: XML_ParserFree(pri_p); XML_ParserFree(fil_p); XML_ParserFree(oth_p); - cr_close(pri_xml_cwfile); - cr_close(fil_xml_cwfile); - cr_close(oth_xml_cwfile); + cr_close(pri_xml_cwfile, NULL); + cr_close(fil_xml_cwfile, NULL); + cr_close(oth_xml_cwfile, NULL); g_string_free(parser_data.current_string, TRUE); diff --git a/src/misc.c b/src/misc.c index db08eae..d8dbda0 100644 --- a/src/misc.c +++ b/src/misc.c @@ -552,16 +552,16 @@ cr_compress_file(const char *src, return CR_COPY_ERR; } - if ((new = cr_open(dst, CR_CW_MODE_WRITE, compression)) == NULL) { + if ((new = cr_open(dst, CR_CW_MODE_WRITE, compression, NULL)) == NULL) { g_debug("%s: Cannot open destination file %s", __func__, dst); fclose(orig); return CR_COPY_ERR; } while ((readed = fread(buf, 1, BUFFER_SIZE, orig)) > 0) { - if (cr_write(new, buf, readed) != readed) { + if (cr_write(new, buf, readed, NULL) != readed) { g_debug("%s: Error while copy %s -> %s", __func__, src, dst); - cr_close(new); + cr_close(new, NULL); fclose(orig); return CR_COPY_ERR; } @@ -569,17 +569,16 @@ cr_compress_file(const char *src, if (readed != BUFFER_SIZE && ferror(orig)) { g_debug("%s: Error while copy %s -> %s (%s)", __func__, src, dst, strerror(errno)); - cr_close(new); + cr_close(new, NULL); fclose(orig); return CR_COPY_ERR; } } - if (dst != in_dst) { + if (dst != in_dst) g_free(dst); - } - cr_close(new); + cr_close(new, NULL); fclose(orig); return CR_COPY_OK; diff --git a/src/repomd.c b/src/repomd.c index 3bce528..8e5448d 100644 --- a/src/repomd.c +++ b/src/repomd.c @@ -109,6 +109,8 @@ cr_get_compressed_content_stat(const char *filename, cr_ChecksumType checksum_type, GError **err) { + GError *tmp_err = NULL; + assert(filename); assert(!err || *err == NULL); @@ -121,10 +123,13 @@ cr_get_compressed_content_stat(const char *filename, // Open compressed file - CR_FILE *cwfile; - if (!(cwfile = cr_open(filename, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION))) { - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, - "Cannot open a file"); + CR_FILE *cwfile = cr_open(filename, + CR_CW_MODE_READ, + CR_CW_AUTO_DETECT_COMPRESSION, + &tmp_err); + if (!cwfile) { + g_propagate_prefixed_error(err, tmp_err, + "Cannot open a file %s: ", filename); return NULL; } @@ -165,12 +170,13 @@ cr_get_compressed_content_stat(const char *filename, unsigned char buffer[BUFFER_SIZE]; do { - readed = cr_read(cwfile, (void *) buffer, BUFFER_SIZE); + readed = cr_read(cwfile, (void *) buffer, BUFFER_SIZE, &tmp_err); if (readed == CR_CW_ERR) { - g_debug("%s: Error while read compressed file: %s", - __func__, filename); - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, - "Error while read compressed file"); + g_debug("%s: Error while read compressed file %s: %s", + __func__, filename, tmp_err->message); + g_propagate_prefixed_error(err, tmp_err, + "Error while read compressed file %s: ", + filename); break; } g_checksum_update (checksum, buffer, readed); @@ -195,7 +201,7 @@ cr_get_compressed_content_stat(const char *filename, // Clean up g_checksum_free(checksum); - cr_close(cwfile); + cr_close(cwfile, NULL); return result; } @@ -257,8 +263,16 @@ cr_repomd_record_fill(cr_RepomdRecord md, // Compute checksum of non compressed content and its size if (!md->checksum_open_type || !md->checksum_open || !md->size_open) { - if (cr_detect_compression(path) != CR_CW_UNKNOWN_COMPRESSION && - cr_detect_compression(path) != CR_CW_NO_COMPRESSION) + cr_CompressionType com_type = cr_detect_compression(path, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, + "Cannot detect compression type of %s: ", path); + return code; + } + + if (com_type != CR_CW_UNKNOWN_COMPRESSION && + com_type != CR_CW_NO_COMPRESSION) { // File compressed by supported algorithm contentStat *open_stat = NULL; @@ -377,35 +391,50 @@ cr_repomd_record_compress_and_fill(cr_RepomdRecord record, // Compress file + get size of non compressed file - cw_plain = cr_open(path, CR_CW_MODE_READ, CR_CW_NO_COMPRESSION); + cw_plain = cr_open(path, + CR_CW_MODE_READ, + CR_CW_NO_COMPRESSION, + &tmp_err); if (!cw_plain) { - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, - "Cannot open %s", path); - return CRE_IO; + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", path); + return code; } - cw_compressed = cr_open(cpath, CR_CW_MODE_WRITE, record_compression); + cw_compressed = cr_open(cpath, + CR_CW_MODE_WRITE, + record_compression, + &tmp_err); if (!cw_compressed) { - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, - "Cannot open %s", path); - return CRE_IO; + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", cpath); + return code; } - while ((readed = cr_read(cw_plain, buf, BUFFER_SIZE)) > 0) { - if (cr_write(cw_compressed, buf, (unsigned int) readed) == CR_CW_ERR) { - g_debug("%s: Error while record compression", __func__); + while ((readed = cr_read(cw_plain, buf, BUFFER_SIZE, &tmp_err)) > 0) { + cr_write(cw_compressed, buf, (unsigned int) readed, &tmp_err); + if (tmp_err) break; - } } - cr_close(cw_compressed); - cr_close(cw_plain); + cr_close(cw_plain, NULL); - if (readed == CR_CW_ERR) { - g_debug("%s: Error while record compression", __func__); - g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, - "Error while compression"); - return CRE_IO; + if (tmp_err) { + int code = tmp_err->code; + cr_close(cw_compressed, NULL); + g_debug("%s: Error while repomd record compression: %s", __func__, + tmp_err->message); + g_propagate_prefixed_error(err, tmp_err, + "Error while compression %s -> %s:", path, cpath); + return code; + } + + cr_close(cw_compressed, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, + "Error while closing %s: ", path); + return code; } diff --git a/src/xml_file.c b/src/xml_file.c index be0eea6..6aa3953 100644 --- a/src/xml_file.c +++ b/src/xml_file.c @@ -45,6 +45,7 @@ cr_xmlfile_open(const char *filename, GError **err) { cr_XmlFile *f; + GError *tmp_err = NULL; assert(filename); assert(type < CR_XMLFILE_SENTINEL); @@ -57,10 +58,9 @@ cr_xmlfile_open(const char *filename, return NULL; } - CR_FILE *cr_f = cr_open(filename, CR_CW_MODE_WRITE, comtype); - if (cr_f == NULL) { - g_set_error(err, CR_XML_FILE_ERROR, CRE_IO, - "Cannot open file"); + CR_FILE *cr_f = cr_open(filename, CR_CW_MODE_WRITE, comtype, &tmp_err); + if (tmp_err) { + g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", filename); return NULL; } @@ -90,6 +90,7 @@ int cr_xmlfile_write_xml_header(cr_XmlFile *f, GError **err) { const char *xml_header; + GError *tmp_err = NULL; assert(f); assert(!err || *err == NULL); @@ -109,10 +110,10 @@ cr_xmlfile_write_xml_header(cr_XmlFile *f, GError **err) assert(0); } - if (cr_printf(f->f, xml_header, f->pkgs) == CR_CW_ERR) { - g_set_error(err, CR_XML_FILE_ERROR, CRE_IO, - "Cannot write XML header"); - return CRE_IO; + if (cr_printf(&tmp_err, f->f, xml_header, f->pkgs) == CR_CW_ERR) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Cannot write XML header: "); + return code; } f->header = 1; @@ -124,6 +125,7 @@ int cr_xmlfile_write_xml_footer(cr_XmlFile *f, GError **err) { const char *xml_footer; + GError *tmp_err = NULL; assert(f); assert(!err || *err == NULL); @@ -143,10 +145,11 @@ cr_xmlfile_write_xml_footer(cr_XmlFile *f, GError **err) assert(0); } - if (cr_puts(f->f, xml_footer) < 0) { - g_set_error(err, CR_XML_FILE_ERROR, CRE_IO, - "Cannot write XML footer"); - return CRE_IO; + cr_puts(f->f, xml_footer, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Cannot write XML footer: "); + return code; } f->footer = 1; @@ -220,10 +223,11 @@ cr_xmlfile_add_chunk(cr_XmlFile *f, const char* chunk, GError **err) } } - if (cr_puts(f->f, chunk) < 0) { - g_set_error(err, CR_XML_FILE_ERROR, CRE_IO, - "Error while write operation"); - return CRE_IO; + cr_puts(f->f, chunk, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Error while write: "); + return code; } return CRE_OK; @@ -257,10 +261,12 @@ cr_xmlfile_close(cr_XmlFile *f, GError **err) } } - if (cr_close(f->f) == CR_CW_ERR) { - g_set_error(err, CR_XML_FILE_ERROR, CRE_IO, - "Error while closing file"); - return CRE_IO; + cr_close(f->f, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, + "Error while closing a file: "); + return code; } g_free(f); diff --git a/src/xml_parser_filelists.c b/src/xml_parser_filelists.c index dee3d39..037eee6 100644 --- a/src/xml_parser_filelists.c +++ b/src/xml_parser_filelists.c @@ -270,6 +270,7 @@ cr_xml_parse_filelists(const char *path, cr_ParserData *pd; XML_Parser parser; char *msgs; + GError *tmp_err = NULL; assert(path); assert(newpkgcb || pkgcb); @@ -279,10 +280,11 @@ cr_xml_parse_filelists(const char *path, if (!newpkgcb) // Use default newpkgcb newpkgcb = cr_newpkgcb; - f = cr_open(path, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION); - if (!f) { - g_set_error(err, CR_XML_PARSER_FIL_ERROR, CRE_IO, "Cannot open %s", path); - return CRE_IO; + f = cr_open(path, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", path); + return code; } parser = XML_ParserCreate(NULL); @@ -314,13 +316,12 @@ cr_xml_parse_filelists(const char *path, break; } - len = cr_read(f, buf, XML_BUFFER_SIZE); - if (len < 0) { - ret = CRE_IO; - g_critical("%s: Cannot read for parsing : %s\n", - __func__, strerror(errno)); - g_set_error(err, CR_XML_PARSER_FIL_ERROR, CRE_IO, - "Error while reading xml"); + len = cr_read(f, buf, XML_BUFFER_SIZE, &tmp_err); + if (tmp_err) { + ret = tmp_err->code; + g_critical("%s: Error while reading xml : %s\n", + __func__, tmp_err->message); + g_propagate_prefixed_error(err, tmp_err, "Read error: "); break; } @@ -359,7 +360,12 @@ cr_xml_parse_filelists(const char *path, msgs = cr_xml_parser_data_free(pd); XML_ParserFree(parser); - cr_close(f); + cr_close(f, &tmp_err); + if (tmp_err) { + int code = tmp_err->code; + g_propagate_prefixed_error(err, tmp_err, "Error while closing: "); + return code; + } if (messages) *messages = msgs; diff --git a/tests/test_compression_wrapper.c b/tests/test_compression_wrapper.c index 88b8e30..ec74028 100644 --- a/tests/test_compression_wrapper.c +++ b/tests/test_compression_wrapper.c @@ -83,34 +83,43 @@ static void test_cr_detect_compression(void) { cr_CompressionType ret; + GError *tmp_err = NULL; // Plain - ret = cr_detect_compression(FILE_COMPRESSED_0_PLAIN); + ret = cr_detect_compression(FILE_COMPRESSED_0_PLAIN, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_NO_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_PLAIN); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_PLAIN, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_NO_COMPRESSION); + g_assert(!tmp_err); // Gz - ret = cr_detect_compression(FILE_COMPRESSED_0_GZ); + ret = cr_detect_compression(FILE_COMPRESSED_0_GZ, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_GZ_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_GZ); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_GZ, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_GZ_COMPRESSION); + g_assert(!tmp_err); // Bz2 - ret = cr_detect_compression(FILE_COMPRESSED_0_BZ2); + ret = cr_detect_compression(FILE_COMPRESSED_0_BZ2, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_BZ2_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_BZ2); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_BZ2, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_BZ2_COMPRESSION); + g_assert(!tmp_err); // Xz - ret = cr_detect_compression(FILE_COMPRESSED_0_XZ); + ret = cr_detect_compression(FILE_COMPRESSED_0_XZ, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_XZ_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_XZ); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_XZ, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_XZ_COMPRESSION); + g_assert(!tmp_err); } @@ -118,52 +127,70 @@ static void test_cr_detect_compression_bad_suffix(void) { cr_CompressionType ret; + GError *tmp_err = NULL; // Plain - ret = cr_detect_compression(FILE_COMPRESSED_0_PLAIN_BAD_SUFFIX); + ret = cr_detect_compression(FILE_COMPRESSED_0_PLAIN_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_NO_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_PLAIN_BAD_SUFFIX); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_PLAIN_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_NO_COMPRESSION); + g_assert(!tmp_err); // Gz - ret = cr_detect_compression(FILE_COMPRESSED_0_GZ_BAD_SUFFIX); + ret = cr_detect_compression(FILE_COMPRESSED_0_GZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_GZ_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_GZ_BAD_SUFFIX); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_GZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_GZ_COMPRESSION); + g_assert(!tmp_err); // Bz2 - ret = cr_detect_compression(FILE_COMPRESSED_0_BZ2_BAD_SUFFIX); + ret = cr_detect_compression(FILE_COMPRESSED_0_BZ2_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_BZ2_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_BZ2_BAD_SUFFIX); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_BZ2_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_BZ2_COMPRESSION); + g_assert(!tmp_err); // Xz - ret = cr_detect_compression(FILE_COMPRESSED_0_XZ_BAD_SUFFIX); + ret = cr_detect_compression(FILE_COMPRESSED_0_XZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_XZ_COMPRESSION); - ret = cr_detect_compression(FILE_COMPRESSED_1_XZ_BAD_SUFFIX); + g_assert(!tmp_err); + ret = cr_detect_compression(FILE_COMPRESSED_1_XZ_BAD_SUFFIX, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_XZ_COMPRESSION); + g_assert(!tmp_err); } void -test_helper_cw_input(const char *filename, cr_CompressionType ctype, const char *content, int len) +test_helper_cw_input(const char *filename, + cr_CompressionType ctype, + const char *content, + int len) { int ret; CR_FILE *file; char buffer[COMPRESSED_BUFFER_LEN+1]; + GError *tmp_err = NULL; - file = cr_open(filename, CR_CW_MODE_READ, ctype); - g_assert(file != NULL); - ret = cr_read(file, buffer, COMPRESSED_BUFFER_LEN); + file = cr_open(filename, CR_CW_MODE_READ, ctype, &tmp_err); + g_assert(file); + g_assert(!tmp_err); + + ret = cr_read(file, buffer, COMPRESSED_BUFFER_LEN, &tmp_err); g_assert_cmpint(ret, ==, len); + g_assert(!tmp_err); + buffer[ret] = '\0'; g_assert_cmpstr(buffer, ==, content); - ret = cr_close(file); + ret = cr_close(file, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_OK); + g_assert(!tmp_err); } @@ -172,23 +199,31 @@ test_cr_read_with_autodetection(void) { // Plain - test_helper_cw_input(FILE_COMPRESSED_0_PLAIN, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_input(FILE_COMPRESSED_1_PLAIN, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_0_PLAIN, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_1_PLAIN, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); // Gz - test_helper_cw_input(FILE_COMPRESSED_0_GZ, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_input(FILE_COMPRESSED_1_GZ, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_0_GZ, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_1_GZ, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); // Bzip2 - test_helper_cw_input(FILE_COMPRESSED_0_BZ2, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_input(FILE_COMPRESSED_1_BZ2, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_0_BZ2, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_1_BZ2, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); // Xz - test_helper_cw_input(FILE_COMPRESSED_0_XZ, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_input(FILE_COMPRESSED_1_XZ, CR_CW_AUTO_DETECT_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_0_XZ, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_input(FILE_COMPRESSED_1_XZ, CR_CW_AUTO_DETECT_COMPRESSION, + FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); } @@ -225,36 +260,45 @@ outputtest_teardown(Outputtest *outputtest, gconstpointer test_data) #define OUTPUT_TYPE_PRINTF 2 void -test_helper_cw_output(int type, const char *filename, cr_CompressionType ctype, const char *content, int len) +test_helper_cw_output(int type, + const char *filename, + cr_CompressionType ctype, + const char *content, + int len) { int ret; CR_FILE *file; - - file = cr_open(filename, CR_CW_MODE_WRITE, ctype); + GError *tmp_err = NULL; + file = cr_open(filename, CR_CW_MODE_WRITE, ctype, &tmp_err); g_assert(file); + g_assert(!tmp_err); switch(type) { case OUTPUT_TYPE_WRITE: - ret = cr_write(file, content, len); + ret = cr_write(file, content, len, &tmp_err); g_assert_cmpint(ret, ==, len); + g_assert(!tmp_err); break; case OUTPUT_TYPE_PUTS: - ret = cr_puts(file, content); + ret = cr_puts(file, content, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_OK); + g_assert(!tmp_err); break; case OUTPUT_TYPE_PRINTF: - ret = cr_printf(file, "%s", content); // XXX + ret = cr_printf(&tmp_err, file, "%s", content); g_assert_cmpint(ret, ==, len); + g_assert(!tmp_err); break; default: break; } - ret = cr_close(file); + ret = cr_close(file, &tmp_err); g_assert_cmpint(ret, ==, CR_CW_OK); + g_assert(!tmp_err); // Read and compare @@ -267,61 +311,101 @@ outputtest_cw_output(Outputtest *outputtest, gconstpointer test_data) { CR_UNUSED(test_data); - CR_FILE *file; - - file = cr_open(outputtest->tmp_filename, CR_CW_MODE_WRITE, CR_CW_AUTO_DETECT_COMPRESSION); - g_assert(!file); - - file = cr_open(outputtest->tmp_filename, CR_CW_MODE_WRITE, CR_CW_UNKNOWN_COMPRESSION); - g_assert(!file); - // Plain printf("Testing - plain\nwrite()\n"); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("puts()\n"); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("printf()\n"); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_NO_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_NO_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); // Gz printf("Testing - gz\nwrite()\n"); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("puts()\n"); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("printf()\n"); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_GZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); // Bz2 printf("Testing - bz2\nwrite()\n"); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("puts()\n"); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("printf()\n"); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_BZ2_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); // Xz printf("Testing - xz\nwrite()\n"); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_WRITE, outputtest->tmp_filename, + CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("puts()\n"); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PUTS, outputtest->tmp_filename, + CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); printf("printf()\n"); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, FILE_COMPRESSED_0_CONTENT_LEN); - test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, FILE_COMPRESSED_1_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_0_CONTENT, + FILE_COMPRESSED_0_CONTENT_LEN); + test_helper_cw_output(OUTPUT_TYPE_PRINTF, outputtest->tmp_filename, + CR_CW_XZ_COMPRESSION, FILE_COMPRESSED_1_CONTENT, + FILE_COMPRESSED_1_CONTENT_LEN); } @@ -331,11 +415,16 @@ main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); - g_test_add_func("/compression_wrapper/test_cr_compression_suffix", test_cr_compression_suffix); - g_test_add_func("/compression_wrapper/test_cr_detect_compression", test_cr_detect_compression); - g_test_add_func("/compression_wrapper/test_cr_detect_compression_bad_suffix", test_cr_detect_compression_bad_suffix); - g_test_add_func("/compression_wrapper/test_cr_read_with_autodetection", test_cr_read_with_autodetection); - g_test_add("/compression_wrapper/outputtest_cw_output", Outputtest, NULL, outputtest_setup, outputtest_cw_output, outputtest_teardown); + g_test_add_func("/compression_wrapper/test_cr_compression_suffix", + test_cr_compression_suffix); + g_test_add_func("/compression_wrapper/test_cr_detect_compression", + test_cr_detect_compression); + g_test_add_func("/compression_wrapper/test_cr_detect_compression_bad_suffix", + test_cr_detect_compression_bad_suffix); + g_test_add_func("/compression_wrapper/test_cr_read_with_autodetection", + test_cr_read_with_autodetection); + g_test_add("/compression_wrapper/outputtest_cw_output", Outputtest, NULL, + outputtest_setup, outputtest_cw_output, outputtest_teardown); return g_test_run(); } diff --git a/tests/test_xml_file.c b/tests/test_xml_file.c index c864d1c..afe9d85 100644 --- a/tests/test_xml_file.c +++ b/tests/test_xml_file.c @@ -75,12 +75,15 @@ test_no_packages(TestFixtures *fixtures, gconstpointer test_data) g_assert(err == NULL); cr_xmlfile_close(f, &err); - CR_FILE *crf = cr_open(path, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION); + CR_FILE *crf = cr_open(path, + CR_CW_MODE_READ, + CR_CW_AUTO_DETECT_COMPRESSION, + NULL); g_assert(crf); - ret = cr_read(crf, &contents, 2047); + ret = cr_read(crf, &contents, 2047, NULL); g_assert(ret != CR_CW_ERR); contents[ret] = '\0'; - cr_close(crf); + cr_close(crf, NULL); g_assert_cmpstr(contents, ==, "\n" "