Beispiel #1
0
ucp cr_xlat_big(FILE *in_file, FILE *out_file, cr_proc proc, ucp seed) {
  ucp perm;
  int is_a_out_buff;
  int a_length;
  int b_length;
  int length;
  int out_length;
  int in_length;
  perm = cr_clone(seed, 256);
  ucp a_buff256;
  ucp b_buff256;
  ucp in_buff;
  ucp out_buff;
  a_length = 0;
  b_length = 0;
  length = 0;
  out_length = 0;
  in_length = 0;
  a_buff256 = cr_m(256);
  b_buff256 = cr_m(256);
  is_a_out_buff = 0;
  in_buff = a_buff256;
  out_buff = NULL;
  a_length = cr_read(in_file, in_buff, 256);
  length = a_length;
  while (256 == length) {
    if (out_buff) {
      length = is_a_out_buff ? a_length : b_length;
      perm = cr_write_step(out_file, proc, perm, out_buff, length);
    }
    if (is_a_out_buff) {
      is_a_out_buff = 0;
      in_buff = a_buff256;
      out_buff = b_buff256;
    } else {
      is_a_out_buff = 1;
      in_buff = b_buff256;
      out_buff = a_buff256;
    }
    length = cr_read(in_file, in_buff, 256);
    if (is_a_out_buff) {
      b_length = length;
    } else {
      a_length = length;
    }
  }
  out_length = is_a_out_buff ? a_length : b_length;
  in_length = is_a_out_buff ? b_length : a_length;
  if (in_length < 256) {
    if (256 == out_length) {
      length = cr_last_bufs(out_buff, out_length, in_buff, in_length);
      out_length = length >> 8;
      in_length = length & 255;
      perm = cr_write_step(out_file, proc, perm, out_buff, out_length);
    }
    perm = cr_write_step(out_file, proc, perm, in_buff, in_length);
  } else {
Beispiel #2
0
// read the first line from the backend and skip it
static ssize_t rr_xclient_read(struct corerouter_peer *peer) {
	struct corerouter_session *cs = peer->session;
        struct rawrouter_session *rr = (struct rawrouter_session *) cs;
        ssize_t len = cr_read(peer, "rr_xclient_read()");
	if (!len) return 0;

	char *ptr = (peer->in->buf + peer->in->pos) - len;
	ssize_t i;
	for(i=0;i<len;i++) {
		if (rr->xclient_rn == 1) {
			if (ptr[i] != '\n') {
				return -1;
			}
			// banner received (skip it, will be sent later)
			size_t remains = len - (i+1);
			if (remains > 0) {
				peer->session->main_peer->out = peer->in;
				peer->session->main_peer->out_pos = (peer->in->pos - remains) ;
			}
			cr_write_to_backend(peer, rr_xclient_write);
			return len;
		}
		else if (ptr[i] == '\r') {
			rr->xclient_rn = 1;
		}
	}

	return len;
}
void
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, &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, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);
}
Beispiel #4
0
// read from client
static ssize_t rr_read(struct corerouter_peer *main_peer) {
	ssize_t len = cr_read(main_peer, "rr_read()");
	if (!len) return 0;

	main_peer->session->peers->out = main_peer->in;
	main_peer->session->peers->out_pos = 0;

	cr_write_to_backend(main_peer->session->peers, rr_instance_write);
	return len;
}
Beispiel #5
0
// read from backend
static ssize_t rr_instance_read(struct corerouter_peer *peer) {
	ssize_t len = cr_read(peer, "rr_instance_read()");
	if (!len) return 0;

	// set the input buffer as the main output one
	peer->session->main_peer->out = peer->in;
	peer->session->main_peer->out_pos = 0;

	cr_write_to_main(peer, rr_write);
	return len;
}
static void
test_cr_error_handling(void)
{
    GError *tmp_err = NULL;
    cr_CompressionType type;
    CR_FILE *f;

    type = cr_detect_compression("/filename/that/should/not/exists", &tmp_err);
    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE);
    g_error_free(tmp_err);
    tmp_err = NULL;

    type = cr_detect_compression("/", &tmp_err);
    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE);
    g_error_free(tmp_err);
    tmp_err = NULL;

    // Opening dir for writing

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_NO_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_IO);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_GZ_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_GZ);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_BZ2_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_IO);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_XZ_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_XZ);
    g_error_free(tmp_err);
    tmp_err = NULL;

    // Opening plain text file as compressed

    char buf[256];
    int ret;

    // gzread can read compressed as well as uncompressed, so this test
    // is useful.
    f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ,
                CR_CW_GZ_COMPRESSION, &tmp_err);
    g_assert(f);
    ret = cr_read(f, buf, 256, &tmp_err);
    g_assert_cmpint(ret, ==, FILE_COMPRESSED_1_CONTENT_LEN);
    g_assert(!tmp_err);
    ret = cr_close(f, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);

    f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ,
                CR_CW_BZ2_COMPRESSION, &tmp_err);
    g_assert(f);
    ret = cr_read(f, buf, 256, &tmp_err);
    g_assert_cmpint(ret, ==, -1);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_BZ2);
    g_error_free(tmp_err);
    tmp_err = NULL;
    ret = cr_close(f, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);

    f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ,
                CR_CW_XZ_COMPRESSION, &tmp_err);
    g_assert(f);
    ret = cr_read(f, buf, 256, &tmp_err);
    g_assert_cmpint(ret, ==, -1);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_XZ);
    g_error_free(tmp_err);
    tmp_err = NULL;
    ret = cr_close(f, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);
}
Beispiel #7
0
int
cr_repomd_record_compress_and_fill(cr_RepomdRecord *record,
                                   cr_RepomdRecord *crecord,
                                   cr_ChecksumType checksum_type,
                                   cr_CompressionType record_compression,
                                   GError **err)
{
    const char *suffix;
    gchar *path, *cpath;
    gchar *clocation_real, *clocation_href;
    gchar *checksum, *cchecksum;
    int readed;
    char buf[BUFFER_SIZE];
    CR_FILE *cw_plain;
    CR_FILE *cw_compressed;
    gint64 gf_size = -1, cgf_size = -1;
    gint64 gf_time = -1, cgf_time = -1;
    struct stat gf_stat, cgf_stat;
    const char *checksum_str = cr_checksum_name_str(checksum_type);
    GError *tmp_err = NULL;

    assert(record);
    assert(crecord);
    assert(!err || *err == NULL);

    if (!(record->location_real) || !strlen(record->location_real)) {
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_BADARG,
                    "Empty locations in repomd record object");
        return CRE_BADARG;
    }

    if (!g_file_test(record->location_real, G_FILE_TEST_IS_REGULAR)) {
        // File doesn't exists
        g_warning("%s: File %s doesn't exists", __func__, record->location_real);
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_NOFILE,
                    "File %s doesn't exists or not a regular file",
                    record->location_real);
        return CRE_NOFILE;;
    }

    // Paths

    suffix = cr_compression_suffix(record_compression);

    clocation_real = g_strconcat(record->location_real, suffix, NULL);
    clocation_href = g_strconcat(record->location_href, suffix, NULL);
    crecord->location_real = g_string_chunk_insert(crecord->chunk,
                                                   clocation_real);
    crecord->location_href = g_string_chunk_insert(crecord->chunk,
                                                   clocation_href);
    g_free(clocation_real);
    g_free(clocation_href);

    path  = record->location_real;
    cpath = crecord->location_real;


    // Compress file + get size of non compressed file

    cw_plain = cr_open(path,
                       CR_CW_MODE_READ,
                       CR_CW_NO_COMPRESSION,
                       &tmp_err);
    if (!cw_plain) {
        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,
                            &tmp_err);
    if (!cw_compressed) {
        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, &tmp_err)) > 0) {
        cr_write(cw_compressed, buf, (unsigned int) readed, &tmp_err);
        if (tmp_err)
            break;
    }

    cr_close(cw_plain, NULL);

    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;
    }


    // Compute checksums

    checksum  = cr_checksum_file(path, checksum_type, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err,
                                   "Error while checksum calculation:");
        return code;
    }

    cchecksum = cr_checksum_file(cpath, checksum_type, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err,
                                   "Error while checksum calculation:");
        return code;
    }


    // Get stats

    if (stat(path, &gf_stat)) {
        g_debug("%s: Error while stat() on %s", __func__, path);
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO,
                    "Cannot stat %s", path);
        return CRE_IO;
    } else {
        gf_size = gf_stat.st_size;
        gf_time = gf_stat.st_mtime;
    }

    if (stat(cpath, &cgf_stat)) {
        g_debug("%s: Error while stat() on %s", __func__, cpath);
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO,
                    "Cannot stat %s", cpath);
        return CRE_IO;
    } else {
        cgf_size = cgf_stat.st_size;
        cgf_time = cgf_stat.st_mtime;
    }


    // Results

    record->checksum = g_string_chunk_insert(record->chunk, checksum);
    record->checksum_type = g_string_chunk_insert(record->chunk, checksum_str);
    record->checksum_open = NULL;
    record->checksum_open_type = NULL;
    record->timestamp = gf_time;
    record->size = gf_size;
    record->size_open = -1;

    crecord->checksum = g_string_chunk_insert(crecord->chunk, cchecksum);
    crecord->checksum_type = g_string_chunk_insert(crecord->chunk, checksum_str);
    crecord->checksum_open = g_string_chunk_insert(record->chunk, checksum);
    crecord->checksum_open_type = g_string_chunk_insert(record->chunk, checksum_str);
    crecord->timestamp = cgf_time;
    crecord->size = cgf_size;
    crecord->size_open = gf_size;

    g_free(checksum);
    g_free(cchecksum);

    return CRE_OK;
}
Beispiel #8
0
contentStat *
cr_get_compressed_content_stat(const char *filename,
                               cr_ChecksumType checksum_type,
                               GError **err)
{
    GError *tmp_err = NULL;

    assert(filename);
    assert(!err || *err == NULL);

    if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_NOFILE,
                    "File %s doesn't exists or not a regular file", filename);
        return NULL;
    }


    // Open compressed 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;
    }


    // Read compressed file and calculate checksum and size

    cr_ChecksumCtx *checksum = cr_checksum_new(checksum_type, &tmp_err);
    if (tmp_err) {
        g_critical("%s: g_checksum_new() failed", __func__);
        g_propagate_prefixed_error(err, tmp_err,
                "Error while checksum calculation: ");
        return NULL;
    }

    gint64 size = 0;
    long readed;
    unsigned char buffer[BUFFER_SIZE];

    do {
        readed = cr_read(cwfile, (void *) buffer, BUFFER_SIZE, &tmp_err);
        if (readed == CR_CW_ERR) {
            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;
        }
        cr_checksum_update(checksum, buffer, readed, NULL);
        size += readed;
    } while (readed == BUFFER_SIZE);

    if (readed == CR_CW_ERR)
        return NULL;

    // Create result structure

    contentStat* result = g_malloc(sizeof(contentStat));
    if (result) {
        result->checksum = cr_checksum_final(checksum, NULL);
        result->size = size;
    } else {
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_MEMORY,
                    "Cannot allocate memory");
    }

    cr_close(cwfile, NULL);

    return result;
}
Beispiel #9
0
int
cr_xml_parser_generic(XML_Parser parser,
                      cr_ParserData *pd,
                      const char *path,
                      GError **err)
{
    /* Note: This function uses .err members of cr_ParserData! */

    int ret = CRE_OK;
    CR_FILE *f;
    GError *tmp_err = NULL;

    assert(parser);
    assert(pd);
    assert(path);
    assert(!err || *err == NULL);

    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;
    }

    while (1) {
        int len;
        void *buf = XML_GetBuffer(parser, XML_BUFFER_SIZE);
        if (!buf) {
            ret = CRE_MEMORY;
            g_set_error(err, ERR_DOMAIN, CRE_MEMORY,
                        "Out of memory: Cannot allocate buffer for xml parser '%s'",
                        path);
            break;
        }

        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': %s",
                       __func__, path, tmp_err->message);
            g_propagate_prefixed_error(err, tmp_err, "Read error: ");
            break;
        }

        if (!XML_ParseBuffer(parser, len, len == 0)) {
            ret = CRE_XMLPARSER;
            g_critical("%s: parsing error '%s': %s",
                       __func__,
                       path,
                       XML_ErrorString(XML_GetErrorCode(parser)));
            g_set_error(err, ERR_DOMAIN, CRE_XMLPARSER,
                        "Parse error '%s' at line: %d (%s)",
                        path,
                        (int) XML_GetCurrentLineNumber(parser),
                        (char *) XML_ErrorString(XML_GetErrorCode(parser)));
            break;
        }

        if (pd->err) {
            ret = pd->err->code;
            g_propagate_error(err, pd->err);
            break;
        }

        if (len == 0)
            break;
    }

    if (ret != CRE_OK) {
        // An error already encoutentered
        // just close the file without error checking
        cr_close(f, NULL);
    } else {
        // No error encountered yet
        cr_close(f, &tmp_err);
        if (tmp_err) {
            ret = tmp_err->code;
            g_propagate_prefixed_error(err, tmp_err, "Error while closing: ");
        }
    }

    return ret;
}