コード例 #1
0
ファイル: cr.c プロジェクト: bardibardi/cheap_random
ucp cr_write_step(FILE *file, cr_proc proc, ucp perm, ucp s, int length) {
  ucp nextperm;
  nextperm = (*proc)(perm, s, length);
  cr_write(file, s, length);
  cr_f(perm);
  return nextperm;
}
コード例 #2
0
static void
test_contentstating_multiwrite(Outputtest *outputtest,
                               G_GNUC_UNUSED gconstpointer test_data)
{
    CR_FILE *f;
    int ret;
    cr_ContentStat *stat;
    GError *tmp_err = NULL;

    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_sopen(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);
    g_assert_cmpstr(stat->checksum, ==, content_sha256);
    cr_contentstat_free(stat, &tmp_err);
    g_assert(!tmp_err);
}
コード例 #3
0
ファイル: rawrouter.c プロジェクト: EasyPost/uwsgi
// write to client
static ssize_t rr_write(struct corerouter_peer *main_peer) {
	ssize_t len = cr_write(main_peer, "rr_write()");
	// end on empty write
	if (!len) return 0;

	// ok this response chunk is sent, let's start reading again
	if (cr_write_complete(main_peer)) {
		// reset the buffer
		main_peer->out->pos = 0;
                cr_reset_hooks(main_peer);
        } 

	return len;
}
コード例 #4
0
ファイル: rawrouter.c プロジェクト: EasyPost/uwsgi
// write to backend
static ssize_t rr_instance_write(struct corerouter_peer *peer) {
	ssize_t len = cr_write(peer, "rr_instance_write()");
	// end on empty write
	if (!len) return 0;

	// the chunk has been sent, start (again) reading from client and instances
	if (cr_write_complete(peer)) {
		// reset the buffer
		peer->out->pos = 0;
		cr_reset_hooks(peer);
	}

	return len;
}
コード例 #5
0
ファイル: fastrouter.c プロジェクト: CommerceRack/uwsgi
// send the uwsgi request header and vars
static ssize_t fr_instance_send_request(struct corerouter_peer *peer) {
	ssize_t len = cr_write(peer, "fr_instance_send_request()");
        // end on empty write
        if (!len) return 0;

        // the chunk has been sent, start (again) reading from client and instances
        if (cr_write_complete(peer)) {
                // reset the original read buffer
                peer->out->pos = 0;
		// start waiting for body
		peer->session->main_peer->last_hook_read = fr_read_body;
                cr_reset_hooks(peer);
        }

	return len;
}
コード例 #6
0
void
test_helper_cw_output(int type,
                      const char *filename,
                      cr_CompressionType ctype,
                      const char *content,
                      int len)
{
    int ret;
    CR_FILE *file;
    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, &tmp_err);
            g_assert_cmpint(ret, ==, len);
            g_assert(!tmp_err);
            break;

        case OUTPUT_TYPE_PUTS:
            ret = cr_puts(file, content, &tmp_err);
            g_assert_cmpint(ret, ==, len);
            g_assert(!tmp_err);
            break;

        case OUTPUT_TYPE_PRINTF:
            ret = cr_printf(&tmp_err, file, "%s", content);
            g_assert_cmpint(ret, ==, len);
            g_assert(!tmp_err);
            break;

        default:
            break;
    }

    ret = cr_close(file, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);

    // Read and compare

    test_helper_cw_input(filename, ctype, content, len);
}
コード例 #7
0
ファイル: cr.c プロジェクト: bardibardi/cheap_random
void cr_to_file(char *file_name, ucp buf, int length) {
  FILE *file;
  file = fopen(file_name, "wb");
  cr_write(file, buf, length);
  fclose(file);
}
コード例 #8
0
ファイル: repomd.c プロジェクト: janncker/createrepo_c
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;
}