cr_contentstat_new(cr_ChecksumType type, GError **err)
{
cr_ContentStat *cstat;
- cr_ChecksumCtx *checksum;
- GError *tmp_err = NULL;
assert(!err || *err == NULL);
- if (type == CR_CHECKSUM_UNKNOWN) {
- checksum = NULL;
- } else {
- checksum = cr_checksum_new(type, &tmp_err);
- if (tmp_err) {
- g_propagate_error(err, tmp_err);
- return NULL;
- }
- }
-
cstat = g_malloc0(sizeof(cr_ContentStat));
- cstat->checksum = checksum;
+ cstat->checksum_type = type;
return cstat;
}
-char *
+void
cr_contentstat_free(cr_ContentStat *cstat, GError **err)
{
- char *checksum_str = NULL;
- GError *tmp_err = NULL;
-
assert(!err || *err == NULL);
if (!cstat)
- return NULL;
-
- if (cstat->checksum) {
- checksum_str = cr_checksum_final(cstat->checksum, &tmp_err);
- if (tmp_err)
- g_propagate_error(err, tmp_err);
- }
+ return;
+ g_free(cstat->checksum);
g_free(cstat);
- return checksum_str;
}
typedef struct {
return NULL;
}
- file->stat = stat;
+ if (stat) {
+ file->stat = stat;
+
+ if (stat->checksum_type == CR_CHECKSUM_UNKNOWN) {
+ file->checksum_ctx = NULL;
+ } else {
+ file->checksum_ctx = cr_checksum_new(stat->checksum_type,
+ &tmp_err);
+ if (tmp_err) {
+ g_propagate_error(err, tmp_err);
+ cr_close(file, NULL);
+ return NULL;
+ }
+ }
+ }
assert(!err || (!file && *err != NULL) || (file && *err == NULL));
break;
}
+ if (cr_file->stat)
+ cr_file->stat->checksum = cr_checksum_final(cr_file->checksum_ctx,
+ NULL);
+
g_free(cr_file);
assert(!err || (ret != CRE_OK && *err != NULL)
}
if (cr_file->stat) {
- // Do open content stat
-
- cr_ContentStat *stat = cr_file->stat;
- GError *tmp_err = NULL;
-
- stat->size += len;
- if (stat->checksum)
- cr_checksum_update(stat->checksum, buffer, len, &tmp_err);
-
- if (tmp_err) {
- g_propagate_error(err, tmp_err);
- return CR_CW_ERR;
+ cr_file->stat->size += len;
+ if (cr_file->checksum_ctx) {
+ GError *tmp_err = NULL;
+ cr_checksum_update(cr_file->checksum_ctx, buffer, len, &tmp_err);
+ if (tmp_err) {
+ g_propagate_error(err, tmp_err);
+ return CR_CW_ERR;
+ }
}
}
/** Stat build about open content during compression (writting).
*/
typedef struct {
- gint64 size; /*!< Size of content */
- cr_ChecksumCtx *checksum; /*!< Checksum context */
+ gint64 size; /*!< Size of content */
+ cr_ChecksumType checksum_type; /*!< Checksum type */
+ char *checksum; /*!< Checksum */
} cr_ContentStat;
/** Creates new cr_ContentStat object
/** Frees cr_ContentStat object.
* @param cstat cr_ContentStat object
* @param err GError **
- * @return checksum or NULL on error or if checksum calculation
- * was disabled (CR_CHECKSUM_UNKNOWN was used)
*/
-char *cr_contentstat_free(cr_ContentStat *cstat, GError **err);
+void cr_contentstat_free(cr_ContentStat *cstat, GError **err);
/** Structure represents a compressed file.
*/
typedef struct {
- cr_CompressionType type; /*!< Type of compression */
- void *FILE; /*!< Pointer to gzFile, BZFILE or plain FILE */
- cr_OpenMode mode; /*!< Mode */
- cr_ContentStat *stat; /*!< Content stats */
+ cr_CompressionType type; /*!< Type of compression */
+ void *FILE; /*!< Pointer to gzFile, BZFILE, ... */
+ cr_OpenMode mode; /*!< Mode */
+ cr_ContentStat *stat; /*!< Content stats */
+ cr_ChecksumCtx *checksum_ctx; /*!< Checksum contenxt */
} CR_FILE;
#define CR_CW_ERR -1 /*!< Return value - Error */
* @param filename filename
* @param mode open mode
* @param comtype type of compression
- * @param stat cr_ContentStat object or NULL
+ * @param stat pointer to cr_ContentStat or NULL
* @param err GError **
* @return pointer to a CR_FILE or NULL
*/
* compression suffix is used.
* If dst is NULL, src + compression suffix is used)
* @param comtype type of compression
- * @param stat cr_ContentStat object or NULL
+ * @param stat pointer to cr_ContentStat or NULL
* @param err GError **
* @return cr_Error return code
*/
static void
test_cr_contentstat(void)
{
- char *checksum;
cr_ContentStat *cs = NULL;
GError *tmp_err = NULL;
cs = cr_contentstat_new(CR_CHECKSUM_UNKNOWN, NULL);
g_assert(cs);
- checksum = cr_contentstat_free(cs, NULL);
- g_assert(!checksum);
+ g_assert(!cs->checksum);
+ cr_contentstat_free(cs, NULL);
cs = NULL;
cs = cr_contentstat_new(CR_CHECKSUM_UNKNOWN, &tmp_err);
g_assert(cs);
g_assert(!tmp_err);
- checksum = cr_contentstat_free(cs, &tmp_err);
- g_assert(!checksum);
+ g_assert(!cs->checksum);
+ cr_contentstat_free(cs, &tmp_err);
g_assert(!tmp_err);
cs = NULL;
-
- cs = cr_contentstat_new(CR_CHECKSUM_SHA256, NULL);
- g_assert(cs);
- checksum = cr_contentstat_free(cs, NULL);
- g_assert(checksum);
- g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649"
- "b934ca495991b7852b855");
- g_free(checksum);
- cs = NULL;
- checksum = NULL;
-
- cs = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
- g_assert(cs);
- g_assert(!tmp_err);
- checksum = cr_contentstat_free(cs, &tmp_err);
- g_assert(!tmp_err);
- g_assert(checksum);
- g_assert_cmpstr(checksum, ==, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649"
- "b934ca495991b7852b855");
- g_free(checksum);
- cs = NULL;
- checksum = NULL;
}
static void
CR_FILE *f;
int ret;
cr_ContentStat *stat;
- char *checksum;
GError *tmp_err = NULL;
CR_UNUSED(test_data);
g_assert(!tmp_err);
g_assert_cmpint(stat->size, ==, content_len);
- checksum = cr_contentstat_free(stat, &tmp_err);
- g_assert(checksum);
+ g_assert_cmpstr(stat->checksum, ==, content_sha256);
+ cr_contentstat_free(stat, &tmp_err);
g_assert(!tmp_err);
- g_assert_cmpstr(checksum, ==, content_sha256);
- g_free(checksum);
- checksum = NULL;
// Gz compression
g_assert(!tmp_err);
g_assert_cmpint(stat->size, ==, content_len);
- checksum = cr_contentstat_free(stat, &tmp_err);
- g_assert(checksum);
+ g_assert_cmpstr(stat->checksum, ==, content_sha256);
+ cr_contentstat_free(stat, &tmp_err);
g_assert(!tmp_err);
- g_assert_cmpstr(checksum, ==, content_sha256);
- g_free(checksum);
- checksum = NULL;
// Bz2 compression
g_assert(!tmp_err);
g_assert_cmpint(stat->size, ==, content_len);
- checksum = cr_contentstat_free(stat, &tmp_err);
- g_assert(checksum);
+ g_assert_cmpstr(stat->checksum, ==, content_sha256);
+ cr_contentstat_free(stat, &tmp_err);
g_assert(!tmp_err);
- g_assert_cmpstr(checksum, ==, content_sha256);
- g_free(checksum);
- checksum = NULL;
// Xz compression
g_assert(!tmp_err);
g_assert_cmpint(stat->size, ==, content_len);
- checksum = cr_contentstat_free(stat, &tmp_err);
- g_assert(checksum);
+ g_assert_cmpstr(stat->checksum, ==, content_sha256);
+ cr_contentstat_free(stat, &tmp_err);
g_assert(!tmp_err);
- g_assert_cmpstr(checksum, ==, content_sha256);
- g_free(checksum);
- checksum = NULL;
}
static void
CR_FILE *f;
int ret;
cr_ContentStat *stat;
- char *checksum;
GError *tmp_err = NULL;
CR_UNUSED(test_data);
g_assert(!tmp_err);
g_assert_cmpint(stat->size, ==, content_len);
- checksum = cr_contentstat_free(stat, &tmp_err);
- g_assert(checksum);
+ g_assert_cmpstr(stat->checksum, ==, content_sha256);
+ cr_contentstat_free(stat, &tmp_err);
g_assert(!tmp_err);
- g_assert_cmpstr(checksum, ==, content_sha256);
- g_free(checksum);
- checksum = NULL;
}
{
CR_UNUSED(test_data);
int ret;
- char *checksum, *checksum_;
+ char *checksum;
cr_ContentStat *stat;
GError *tmp_err = NULL;
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);
+ ret = cr_compress_file_with_stat(TEST_TEXT_FILE, copyfiletest->dst_file,
+ CR_CW_GZ_COMPRESSION, stat, &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);
+ checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA256, NULL);
+ g_assert_cmpstr(stat->checksum, ==, checksum);
+ cr_contentstat_free(stat, &tmp_err);
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