CR_FILE *
-cr_open_with_stats(const char *filename,
- cr_OpenMode mode,
- cr_CompressionType comtype,
- cr_ContentStat *stat,
- GError **err)
+cr_open_with_stat(const char *filename,
+ cr_OpenMode mode,
+ cr_CompressionType comtype,
+ cr_ContentStat *stat,
+ GError **err)
{
CR_FILE *file = NULL;
cr_CompressionType type = comtype;
return NULL;
}
+ file->stat = stat;
+
assert(!err || (!file && *err != NULL) || (file && *err == NULL));
return file;
return ret;
}
+ 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;
+ }
+ }
+
switch (cr_file->type) {
case (CR_CW_NO_COMPRESSION): // ---------------------------------------
switch (cr_file->type) {
case (CR_CW_NO_COMPRESSION): // ---------------------------------------
- if (fputs(str, (FILE *) cr_file->FILE) != EOF) {
- ret = 0;
- } 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 = 0;
- } else {
- 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, err);
- if (ret == (int) len)
- ret = 0;
- else
+ if (ret != (int) len)
ret = CR_CW_ERR;
break;
switch (cr_file->type) {
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): // --------------------------------------
case (CR_CW_XZ_COMPRESSION): // ---------------------------------------
tmp_ret = cr_write(cr_file, buf, ret, err);
- if (tmp_ret != (int) ret) {
+ if (tmp_ret != (int) ret)
ret = CR_CW_ERR;
- }
break;
default: // -----------------------------------------------------------
case OUTPUT_TYPE_PUTS:
ret = cr_puts(file, content, &tmp_err);
- g_assert_cmpint(ret, ==, CRE_OK);
+ g_assert_cmpint(ret, ==, len);
g_assert(!tmp_err);
break;
}
+static void
+test_contentstating_singlewrite(Outputtest *outputtest, gconstpointer test_data)
+{
+ CR_FILE *f;
+ int ret;
+ cr_ContentStat *stat;
+ char *checksum;
+ GError *tmp_err = NULL;
+
+ CR_UNUSED(test_data);
+
+ const char *content = "sdlkjowykjnhsadyhfsoaf\nasoiuyseahlndsf\n";
+ const int content_len = 39;
+ const char *content_sha256 = "c9d112f052ab86270bfb484817a513d6ce188133ddc0"
+ "7c0fc1ac32018b6da6c7";
+
+ // No compression
+
+ stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+ g_assert(stat);
+ g_assert(!tmp_err);
+
+ f = cr_open_with_stat(outputtest->tmp_filename,
+ CR_CW_MODE_WRITE,
+ CR_CW_NO_COMPRESSION,
+ stat,
+ &tmp_err);
+ g_assert(f);
+ g_assert(!tmp_err);
+
+ ret = cr_write(f, content, content_len, &tmp_err);
+ g_assert_cmpint(ret, ==, content_len);
+ g_assert(!tmp_err);
+
+ cr_close(f, &tmp_err);
+ g_assert(!tmp_err);
+
+ g_assert_cmpint(stat->size, ==, content_len);
+ checksum = cr_contentstat_free(stat, &tmp_err);
+ g_assert(checksum);
+ g_assert(!tmp_err);
+ g_assert_cmpstr(checksum, ==, content_sha256);
+ g_free(checksum);
+ checksum = NULL;
+
+ // Gz compression
+
+ stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+ g_assert(stat);
+ g_assert(!tmp_err);
+
+ f = cr_open_with_stat(outputtest->tmp_filename,
+ CR_CW_MODE_WRITE,
+ CR_CW_GZ_COMPRESSION,
+ stat,
+ &tmp_err);
+ g_assert(f);
+ g_assert(!tmp_err);
+
+ ret = cr_write(f, content, content_len, &tmp_err);
+ g_assert_cmpint(ret, ==, content_len);
+ g_assert(!tmp_err);
+
+ cr_close(f, &tmp_err);
+ g_assert(!tmp_err);
+
+ g_assert_cmpint(stat->size, ==, content_len);
+ checksum = cr_contentstat_free(stat, &tmp_err);
+ g_assert(checksum);
+ g_assert(!tmp_err);
+ g_assert_cmpstr(checksum, ==, content_sha256);
+ g_free(checksum);
+ checksum = NULL;
+
+ // Bz2 compression
+
+ stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+ g_assert(stat);
+ g_assert(!tmp_err);
+
+ f = cr_open_with_stat(outputtest->tmp_filename,
+ CR_CW_MODE_WRITE,
+ CR_CW_BZ2_COMPRESSION,
+ stat,
+ &tmp_err);
+ g_assert(f);
+ g_assert(!tmp_err);
+
+ ret = cr_write(f, content, content_len, &tmp_err);
+ g_assert_cmpint(ret, ==, content_len);
+ g_assert(!tmp_err);
+
+ cr_close(f, &tmp_err);
+ g_assert(!tmp_err);
+
+ g_assert_cmpint(stat->size, ==, content_len);
+ checksum = cr_contentstat_free(stat, &tmp_err);
+ g_assert(checksum);
+ g_assert(!tmp_err);
+ g_assert_cmpstr(checksum, ==, content_sha256);
+ g_free(checksum);
+ checksum = NULL;
+
+ // Xz compression
+
+ stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+ g_assert(stat);
+ g_assert(!tmp_err);
+
+ f = cr_open_with_stat(outputtest->tmp_filename,
+ CR_CW_MODE_WRITE,
+ CR_CW_XZ_COMPRESSION,
+ stat,
+ &tmp_err);
+ g_assert(f);
+ g_assert(!tmp_err);
+
+ ret = cr_write(f, content, content_len, &tmp_err);
+ g_assert_cmpint(ret, ==, content_len);
+ g_assert(!tmp_err);
+
+ cr_close(f, &tmp_err);
+ g_assert(!tmp_err);
+
+ g_assert_cmpint(stat->size, ==, content_len);
+ checksum = cr_contentstat_free(stat, &tmp_err);
+ g_assert(checksum);
+ g_assert(!tmp_err);
+ g_assert_cmpstr(checksum, ==, content_sha256);
+ g_free(checksum);
+ checksum = NULL;
+}
+
+static void
+test_contentstating_multiwrite(Outputtest *outputtest, gconstpointer test_data)
+{
+ CR_FILE *f;
+ int ret;
+ cr_ContentStat *stat;
+ char *checksum;
+ GError *tmp_err = NULL;
+
+ CR_UNUSED(test_data);
+
+ const char *content = "sdlkjowykjnhsadyhfsoaf\nasoiuyseahlndsf\n";
+ const int content_len = 39;
+ const char *content_sha256 = "c9d112f052ab86270bfb484817a513d6ce188133ddc0"
+ "7c0fc1ac32018b6da6c7";
+
+ // Gz compression
+
+ stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
+ g_assert(stat);
+ g_assert(!tmp_err);
+
+ f = cr_open_with_stat(outputtest->tmp_filename,
+ CR_CW_MODE_WRITE,
+ CR_CW_GZ_COMPRESSION,
+ stat,
+ &tmp_err);
+ g_assert(f);
+ g_assert(!tmp_err);
+
+ ret = cr_write(f, content, 10, &tmp_err);
+ g_assert_cmpint(ret, ==, 10);
+ g_assert(!tmp_err);
+
+ ret = cr_write(f, content+10, 29, &tmp_err);
+ g_assert_cmpint(ret, ==, 29);
+ g_assert(!tmp_err);
+
+ cr_close(f, &tmp_err);
+ g_assert(!tmp_err);
+
+ g_assert_cmpint(stat->size, ==, content_len);
+ checksum = cr_contentstat_free(stat, &tmp_err);
+ g_assert(checksum);
+ g_assert(!tmp_err);
+ g_assert_cmpstr(checksum, ==, content_sha256);
+ g_free(checksum);
+ checksum = NULL;
+}
+
+
int
main(int argc, char *argv[])
{
outputtest_setup, outputtest_cw_output, outputtest_teardown);
g_test_add_func("/compression_wrapper/test_cr_error_handling",
test_cr_error_handling);
+ g_test_add("/compression_wrapper/test_contentstating_singlewrite",
+ Outputtest, NULL, outputtest_setup,
+ test_contentstating_singlewrite, outputtest_teardown);
+ g_test_add("/compression_wrapper/test_contentstating_multiwrite",
+ Outputtest, NULL, outputtest_setup,
+ test_contentstating_multiwrite, outputtest_teardown);
return g_test_run();
}