/** Record where a breach of the OIFITS standard has occurred. */ static void set_result(oi_check_result *pResult, oi_breach_level level, const char *description, const char *location) { if(level > pResult->level) pResult->level = level; if(pResult->description == NULL) pResult->description = g_string_chunk_insert(pResult->chunk, description); if(++pResult->numBreach < MAX_REPORT) { pResult->location[pResult->numBreach-1] = g_string_chunk_insert(pResult->chunk, location); } else if (pResult->numBreach == MAX_REPORT) { pResult->location[MAX_REPORT-1] = g_string_chunk_insert(pResult->chunk, "[List truncated]"); } }
cr_RepomdRecord * cr_repomd_record_new(const char *type, const char *path) { cr_RepomdRecord *md = g_malloc0(sizeof(*md)); md->chunk = g_string_chunk_new(128); md->type = cr_safe_string_chunk_insert(md->chunk, type); if (path) { gchar *filename = cr_get_filename(path); gchar *location_href = g_strconcat(LOCATION_HREF_PREFIX, filename, NULL); md->location_real = g_string_chunk_insert(md->chunk, path); md->location_href = g_string_chunk_insert(md->chunk, location_href); g_free(location_href); } return md; }
static int set_str(_PackageObject *self, PyObject *value, void *member_offset) { if (check_PackageStatus(self)) return -1; if (!PyString_Check(value) && value != Py_None) { PyErr_SetString(PyExc_TypeError, "String or None expected!"); return -1; } cr_Package *pkg = self->package; if (value == Py_None) { // If value is None exist right now (avoid possibly // creation of a string chunk) *((char **) ((size_t) pkg + (size_t) member_offset)) = NULL; return 0; } // Check if chunk exits // If it doesn't - this is package from loaded metadata and all its // strings are in a metadata common chunk (cr_Metadata->chunk). // In this case, we have to create a chunk for this package before // inserting a new string. if (!pkg->chunk) pkg->chunk = g_string_chunk_new(0); char *str = g_string_chunk_insert(pkg->chunk, PyString_AsString(value)); *((char **) ((size_t) pkg + (size_t) member_offset)) = str; return 0; }
/* this implementation should work for _all_ locales. */ static char* mu_str_normalize_in_place_generic (char *str, gboolean downcase, GStringChunk *strchunk) { char *norm; size_t len; /* FIXME: add accent-folding etc. */ if (!downcase) return str; /* nothing to do */ len = strlen (str); norm = g_utf8_strdown (str, len); if (strlen (norm) > len) { /* this case is rare, but does happen */ char *copy; if (!strchunk) return norm; copy = g_string_chunk_insert (strchunk, norm); g_free (norm); return copy; } memcpy (str, norm, len); return str; }
/* add a new playlist entry into the table (number, name and random/not random) */ void irmpc_playlist_add (unsigned int number, const char *name, bool random) { if (name == NULL) return; if (playlist_name_storage == NULL) { playlist_name_storage = g_string_chunk_new (64); if (playlist_name_storage == NULL) return; } gchar *entry_name = g_string_chunk_insert (playlist_name_storage, name); if (playlist_table == NULL) { playlist_table = g_tree_new (irmpc_playlist_table_cmp_func); if (playlist_table == NULL) return; } struct playlist_info *entry; entry = (struct playlist_info *) malloc (sizeof (struct playlist_info)); if (entry == NULL) return; entry->name = entry_name; entry->random = random; g_tree_insert (playlist_table, GINT_TO_POINTER(number), entry); }
static Group* simple_add_group (XfceRcSimple *simple, const gchar *name) { Group *group; for (group = simple->gfirst; group != NULL; group = group->next) if (str_is_equal (group->name, name)) return group; group = g_slice_new (Group); group->name = g_string_chunk_insert (simple->string_chunk, name); group->efirst = NULL; group->elast = NULL; if (G_UNLIKELY (simple->gfirst == NULL)) { group->next = group->prev = NULL; simple->gfirst = simple->glast = group; } else { group->next = NULL; group->prev = simple->glast; simple->glast->next = group; simple->glast = group; } return group; }
static void lr_yum_repomd_add_repo_tag(LrYumRepoMd *repomd, char *tag) { assert(repomd); if (!tag) return; repomd->repo_tags = g_slist_append(repomd->repo_tags, g_string_chunk_insert(repomd->chunk, tag)); }
static void parse_version_info(const char **attrs, Package *p) { int i; const char *attr; const char *value; for (i = 0; attrs && attrs[i]; i++) { attr = attrs[i]; value = attrs[++i]; if (!strcmp (attr, "epoch")) p->epoch = g_string_chunk_insert (p->chunk, value); else if (!strcmp (attr, "ver")) p->version = g_string_chunk_insert (p->chunk, value); else if (!strcmp (attr, "rel")) p->release = g_string_chunk_insert (p->chunk, value); } }
static void parse_package (const char **attrs, Package *p) { int i; const char *attr; const char *value; for (i = 0; attrs && attrs[i]; i++) { attr = attrs[i]; value = attrs[++i]; if (!strcmp (attr, "pkgid")) p->pkgId = g_string_chunk_insert (p->chunk, value); if (!strcmp (attr, "name")) p->name = g_string_chunk_insert (p->chunk, value); else if (!strcmp (attr, "arch")) p->arch = g_string_chunk_insert (p->chunk, value); } }
static void lr_yum_repomd_add_distro_tag(LrYumRepoMd *repomd, const char *cpeid, const char *tag) { assert(repomd); if (!tag) return; LrYumDistroTag *distrotag = lr_malloc0(sizeof(*distrotag)); distrotag->cpeid = lr_string_chunk_insert(repomd->chunk, cpeid); distrotag->tag = g_string_chunk_insert(repomd->chunk, tag); repomd->distro_tags = g_slist_append(repomd->distro_tags, distrotag); }
static RmNode *rm_node_new(RmTrie *trie, const char *elem) { RmNode *self = g_slice_alloc0(sizeof(RmNode)); if(elem != NULL) { /* Note: We could use g_string_chunk_insert_const here. * That would keep a hash table with all strings internally though, * but would sort out storing duplicate path elements. In normal * setups this will not happen that much though I guess. */ self->basename = g_string_chunk_insert(trie->chunks, elem); } return self; }
XfceRcSimple* _xfce_rc_simple_new (XfceRcSimple *shared, const gchar *filename, gboolean readonly) { XfceRcSimple *simple; simple = g_new0 (XfceRcSimple, 1); _xfce_rc_init (XFCE_RC (simple)); /* attach callbacks */ simple->__parent__.close = _xfce_rc_simple_close; simple->__parent__.get_groups = _xfce_rc_simple_get_groups; simple->__parent__.get_entries = _xfce_rc_simple_get_entries; simple->__parent__.delete_group = _xfce_rc_simple_delete_group; simple->__parent__.get_group = _xfce_rc_simple_get_group; simple->__parent__.has_group = _xfce_rc_simple_has_group; simple->__parent__.set_group = _xfce_rc_simple_set_group; simple->__parent__.delete_entry = _xfce_rc_simple_delete_entry; simple->__parent__.has_entry = _xfce_rc_simple_has_entry; simple->__parent__.read_entry = _xfce_rc_simple_read_entry; if (!readonly) { simple->__parent__.flush = _xfce_rc_simple_flush; simple->__parent__.rollback = _xfce_rc_simple_rollback; simple->__parent__.is_dirty = _xfce_rc_simple_is_dirty; simple->__parent__.is_readonly = _xfce_rc_simple_is_readonly; simple->__parent__.write_entry = _xfce_rc_simple_write_entry; } if (shared != NULL) { simple->shared_chunks = TRUE; simple->string_chunk = shared->string_chunk; } else { simple->shared_chunks = FALSE; simple->string_chunk = g_string_chunk_new (4096); } simple->filename = g_string_chunk_insert (simple->string_chunk, filename); simple->readonly = readonly; /* add NULL_GROUP */ simple->group = simple_add_group (simple, NULL_GROUP); return simple; }
LrDownloadTarget * lr_downloadtarget_new(LrHandle *handle, const char *path, const char *baseurl, int fd, const char *fn, GSList *possiblechecksums, gint64 expectedsize, gboolean resume, LrProgressCb progresscb, void *cbdata, LrEndCb endcb, LrMirrorFailureCb mirrorfailurecb, void *userdata, gint64 byterangestart, gint64 byterangeend) { LrDownloadTarget *target; assert(path); assert((fd > 0 && !fn) || (fd < 0 && fn)); target = lr_malloc0(sizeof(*target)); if (byterangestart && resume) { g_debug("%s: Cannot specify byterangestart and set resume to TRUE " "at the same time", __func__); free(target); return NULL; } target->handle = handle; target->chunk = g_string_chunk_new(0); target->path = g_string_chunk_insert(target->chunk, path); target->baseurl = lr_string_chunk_insert(target->chunk, baseurl); target->fd = fd; target->fn = lr_string_chunk_insert(target->chunk, fn); target->checksums = possiblechecksums; target->expectedsize = expectedsize; target->resume = resume; target->progresscb = progresscb; target->cbdata = cbdata; target->endcb = endcb; target->mirrorfailurecb = mirrorfailurecb; target->rcode = LRE_UNFINISHED; target->userdata = userdata; target->byterangestart = byterangestart; target->byterangeend = byterangeend; return target; }
char* mu_str_normalize (const char *str, gboolean downcase, GStringChunk *strchunk) { char *mystr; g_return_val_if_fail (str, NULL); if (strchunk) mystr = g_string_chunk_insert (strchunk, str); else mystr = g_strdup (str); return mu_str_normalize_in_place_try (mystr, downcase, strchunk); }
char* mu_str_xapian_escape (const char *query, gboolean esc_space, GStringChunk *strchunk) { char *mystr; g_return_val_if_fail (query, NULL); if (strchunk) mystr = g_string_chunk_insert (strchunk, query); else mystr = g_strdup (query); return mu_str_xapian_escape_in_place_try (mystr, esc_space, strchunk); }
Sequence * new_Sequence_StaticSeqHolder(StaticSeqHolder * ssh,Sequence * seq) { char * str; assert(seq); assert(ssh); assert(ssh->gstring_chunk); str = g_string_chunk_insert(ssh->gstring_chunk,seq->seq); ckfree(seq->seq); seq->seq = str; seq->dynamite_hard_link++; return seq; }
/** * Gets a pointer to a constant string value with a string equal to str. * _string_lookup_val will always return the same value for the same string. * * @param s4 The database to look for the string in * @param str The string to find the constant string of * @return A pointer to a string value */ const s4_val_t *_string_lookup_val (s4_t *s4, const char *str) { s4_val_t *ret; g_mutex_lock (&s4->const_data->strings_lock); ret = g_hash_table_lookup (s4->const_data->strings_table, str); if (ret == NULL) { str = g_string_chunk_insert (s4->const_data->strings, str); ret = s4_val_new_internal_string (str, s4); g_hash_table_insert (s4->const_data->strings_table, (void*)str, ret); } g_mutex_unlock (&s4->const_data->strings_lock); return ret; }
gchar* g_string_chunk_insert_const (GStringChunk *chunk, const gchar *string) { char* lookup; g_return_val_if_fail (chunk != NULL, NULL); if (!chunk->const_table) chunk->const_table = g_hash_table_new (g_str_hash, g_str_equal); lookup = (char*) g_hash_table_lookup (chunk->const_table, (gchar *)string); if (!lookup) { lookup = g_string_chunk_insert (chunk, string); g_hash_table_insert (chunk->const_table, lookup, lookup); } return lookup; }
ASTNode * ASTNode_new(NodeKind kind, gchar * text, gpointer cx) { ASTNode * node = g_slice_new0(ASTNode); node->id = ASTNode_ID++; node->kind = kind; node->type = NULL; if(text) node->text = g_string_chunk_insert (MiluStringPool, text) ; else node->text = g_strdup(""); //node->text = text ? g_string_chunk_insert (MiluStringPool, text) : NULL ; node->children = NULL; node->next_sibling = NULL; node->parent = NULL; node->prev_sibling = NULL; node->cx = cx; node->is_mutant = FALSE; node->ori_text = NULL; node->ext[0] = 0; node->ext[1] = 0; node->ext[2] = 0; return node; }
static Entry* simple_add_entry (XfceRcSimple *simple, const gchar *key, const gchar *value, const gchar *locale) { LEntry *lentry_before; LEntry *lentry; Entry *entry; gint result; for (entry = simple->group->efirst; entry != NULL; entry = entry->next) if (str_is_equal (entry->key, key)) break; if (G_UNLIKELY (entry == NULL)) { entry = g_slice_new (Entry); entry->key = g_string_chunk_insert (simple->string_chunk, key); entry->value = g_string_chunk_insert (simple->string_chunk, value); entry->lfirst = NULL; entry->llast = NULL; if (simple->group->efirst == NULL) { entry->next = entry->prev = NULL; simple->group->efirst = simple->group->elast = entry; } else { entry->next = NULL; entry->prev = simple->group->elast; simple->group->elast->next = entry; simple->group->elast = entry; } if (locale == NULL) return entry; } if (G_UNLIKELY (locale == NULL)) { /* overwrite existing value */ if (!str_is_equal (entry->value, value)) entry->value = g_string_chunk_insert (simple->string_chunk, value); } else { /* Add the localized value. Note the optimization used within here: We store * the locales in alphabetic order (e.g. de, fr, ...). And we start the search * at the end of the locales list, since locales are usually sorted in the * rc files. Once we notice that the locale in question is less than the * current list locale, we cancel the search and remember the list element as * the element before the new list element (remember the sorting order). This * makes parsing a usual resource config file with lots of locales amazingly * fast. */ lentry_before = NULL; for (lentry = entry->llast; lentry != NULL; lentry = lentry->prev) { result = strcmp (lentry->locale, locale); if (result == 0) break; else if (result < 0) { lentry_before = lentry; lentry = NULL; break; } } if (G_LIKELY (lentry == NULL)) { /* create new localized entry */ lentry = g_slice_new (LEntry); lentry->locale = g_string_chunk_insert (simple->string_chunk, locale); lentry->value = g_string_chunk_insert (simple->string_chunk, value); if (G_UNLIKELY (entry->lfirst == NULL)) { lentry->next = lentry->prev = NULL; entry->lfirst = entry->llast = lentry; } else if (lentry_before != NULL) { lentry->next = lentry_before->next; lentry->prev = lentry_before; if (G_UNLIKELY (lentry_before->next != NULL)) lentry_before->next->prev = lentry; else entry->llast = lentry; lentry_before->next = lentry; } else { lentry->next = NULL; lentry->prev = entry->llast; entry->llast->next = lentry; entry->llast = lentry; } } else { /* overwrite value in existing localized entry */ if (G_LIKELY (!str_is_equal (lentry->value, value))) lentry->value = g_string_chunk_insert (simple->string_chunk, value); } } return entry; }
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; }
int main (int argc, char *argv[]) { GList *list, *t; GSList *slist, *st; GHashTable *hash_table; GMemChunk *mem_chunk; GStringChunk *string_chunk; GTimer *timer; gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6}; gchar *string; gchar *mem[10000], *tmp_string = NULL, *tmp_string_2; gint i, j; GArray *garray; GPtrArray *gparray; GByteArray *gbarray; GString *string1, *string2; GTree *tree; char chars[62]; GRelation *relation; GTuples *tuples; gint data [1024]; struct { gchar *filename; gchar *dirname; } dirname_checks[] = { #ifndef NATIVE_WIN32 { "/", "/" }, { "////", "/" }, { ".////", "." }, { ".", "." }, { "..", "." }, { "../", ".." }, { "..////", ".." }, { "", "." }, { "a/b", "a" }, { "a/b/", "a/b" }, { "c///", "c" }, #else { "\\", "\\" }, { ".\\\\\\\\", "." }, { ".", "." }, { "..", "." }, { "..\\", ".." }, { "..\\\\\\\\", ".." }, { "", "." }, { "a\\b", "a" }, { "a\\b\\", "a\\b" }, { "c\\\\\\", "c" }, #endif }; guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]); guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U; guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U; #ifdef G_HAVE_GINT64 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U), gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU); #endif g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n", glib_major_version, glib_minor_version, glib_micro_version, glib_interface_age, glib_binary_age); string = g_get_current_dir (); g_print ("cwd: %s\n", string); g_free (string); g_print ("user: %s\n", g_get_user_name ()); g_print ("real: %s\n", g_get_real_name ()); g_print ("home: %s\n", g_get_home_dir ()); g_print ("tmp-dir: %s\n", g_get_tmp_dir ()); /* type sizes */ g_print ("checking size of gint8: %d", (int)sizeof (gint8)); TEST (NULL, sizeof (gint8) == 1); g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16)); TEST (NULL, sizeof (gint16) == 2); g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32)); TEST (NULL, sizeof (gint32) == 4); #ifdef G_HAVE_GINT64 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64)); TEST (NULL, sizeof (gint64) == 8); #endif /* G_HAVE_GINT64 */ g_print ("\n"); g_print ("checking g_dirname()..."); for (i = 0; i < n_dirname_checks; i++) { gchar *dirname; dirname = g_dirname (dirname_checks[i].filename); if (strcmp (dirname, dirname_checks[i].dirname) != 0) { g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n", dirname_checks[i].filename, dirname_checks[i].dirname, dirname); n_dirname_checks = 0; } g_free (dirname); } if (n_dirname_checks) g_print ("ok\n"); g_print ("checking doubly linked lists..."); list = NULL; for (i = 0; i < 10; i++) list = g_list_append (list, &nums[i]); list = g_list_reverse (list); for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != (9 - i)) g_error ("Regular insert failed"); } for (i = 0; i < 10; i++) if(g_list_position(list, g_list_nth (list, i)) != i) g_error("g_list_position does not seem to be the inverse of g_list_nth\n"); g_list_free (list); list = NULL; for (i = 0; i < 10; i++) list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one); /* g_print("\n"); g_list_foreach (list, my_list_print, NULL); */ for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != i) g_error ("Sorted insert failed"); } g_list_free (list); list = NULL; for (i = 0; i < 10; i++) list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two); /* g_print("\n"); g_list_foreach (list, my_list_print, NULL); */ for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != (9 - i)) g_error ("Sorted insert failed"); } g_list_free (list); list = NULL; for (i = 0; i < 10; i++) list = g_list_prepend (list, &morenums[i]); list = g_list_sort (list, my_list_compare_two); /* g_print("\n"); g_list_foreach (list, my_list_print, NULL); */ for (i = 0; i < 10; i++) { t = g_list_nth (list, i); if (*((gint*) t->data) != (9 - i)) g_error ("Merge sort failed"); } g_list_free (list); g_print ("ok\n"); g_print ("checking singly linked lists..."); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_append (slist, &nums[i]); slist = g_slist_reverse (slist); for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != (9 - i)) g_error ("failed"); } g_slist_free (slist); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one); /* g_print("\n"); g_slist_foreach (slist, my_list_print, NULL); */ for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != i) g_error ("Sorted insert failed"); } g_slist_free(slist); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two); /* g_print("\n"); g_slist_foreach (slist, my_list_print, NULL); */ for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != (9 - i)) g_error("Sorted insert failed"); } g_slist_free(slist); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_prepend (slist, &morenums[i]); slist = g_slist_sort (slist, my_list_compare_two); /* g_print("\n"); g_slist_foreach (slist, my_list_print, NULL); */ for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); if (*((gint*) st->data) != (9 - i)) g_error("Sorted insert failed"); } g_slist_free(slist); g_print ("ok\n"); g_print ("checking binary trees...\n"); tree = g_tree_new (my_compare); i = 0; for (j = 0; j < 10; j++, i++) { chars[i] = '0' + j; g_tree_insert (tree, &chars[i], &chars[i]); } for (j = 0; j < 26; j++, i++) { chars[i] = 'A' + j; g_tree_insert (tree, &chars[i], &chars[i]); } for (j = 0; j < 26; j++, i++) { chars[i] = 'a' + j; g_tree_insert (tree, &chars[i], &chars[i]); } g_print ("tree height: %d\n", g_tree_height (tree)); g_print ("tree nnodes: %d\n", g_tree_nnodes (tree)); g_print ("tree: "); g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL); g_print ("\n"); for (i = 0; i < 10; i++) g_tree_remove (tree, &chars[i]); g_print ("tree height: %d\n", g_tree_height (tree)); g_print ("tree nnodes: %d\n", g_tree_nnodes (tree)); g_print ("tree: "); g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL); g_print ("\n"); g_print ("ok\n"); /* check n-way trees */ g_node_test (); g_print ("checking mem chunks..."); mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE); for (i = 0; i < 10000; i++) { mem[i] = g_chunk_new (gchar, mem_chunk); for (j = 0; j < 50; j++) mem[i][j] = i * j; } for (i = 0; i < 10000; i++) { g_mem_chunk_free (mem_chunk, mem[i]); } g_print ("ok\n"); g_print ("checking hash tables..."); hash_table = g_hash_table_new (my_hash, my_hash_compare); for (i = 0; i < 10000; i++) { array[i] = i; g_hash_table_insert (hash_table, &array[i], &array[i]); } g_hash_table_foreach (hash_table, my_hash_callback, NULL); for (i = 0; i < 10000; i++) if (array[i] == 0) g_print ("%d\n", i); for (i = 0; i < 10000; i++) g_hash_table_remove (hash_table, &array[i]); for (i = 0; i < 10000; i++) { array[i] = i; g_hash_table_insert (hash_table, &array[i], &array[i]); } if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 || g_hash_table_size (hash_table) != 5000) g_print ("bad!\n"); g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL); g_hash_table_destroy (hash_table); g_print ("ok\n"); g_print ("checking string chunks..."); string_chunk = g_string_chunk_new (1024); for (i = 0; i < 100000; i ++) { tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); if (strcmp ("hi pete", tmp_string) != 0) g_error ("string chunks are broken.\n"); } tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 != tmp_string && strcmp(tmp_string_2, tmp_string) == 0); tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 == tmp_string); g_string_chunk_free (string_chunk); g_print ("ok\n"); g_print ("checking arrays..."); garray = g_array_new (FALSE, FALSE, sizeof (gint)); for (i = 0; i < 10000; i++) g_array_append_val (garray, i); for (i = 0; i < 10000; i++) if (g_array_index (garray, gint, i) != i) g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i); g_array_free (garray, TRUE); garray = g_array_new (FALSE, FALSE, sizeof (gint)); for (i = 0; i < 100; i++) g_array_prepend_val (garray, i); for (i = 0; i < 100; i++) if (g_array_index (garray, gint, i) != (100 - i - 1)) g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1); g_array_free (garray, TRUE); g_print ("ok\n"); g_print ("checking strings..."); string1 = g_string_new ("hi pete!"); string2 = g_string_new (""); g_assert (strcmp ("hi pete!", string1->str) == 0); for (i = 0; i < 10000; i++) g_string_append_c (string1, 'a'+(i%26)); #if !(defined (_MSC_VER) || defined (__LCC__)) /* MSVC and LCC use the same run-time C library, which doesn't like the %10000.10000f format... */ g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #else g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #endif g_print ("string2 length = %d...\n", string2->len); string2->str[70] = '\0'; g_print ("first 70 chars:\n%s\n", string2->str); string2->str[141] = '\0'; g_print ("next 70 chars:\n%s\n", string2->str+71); string2->str[212] = '\0'; g_print ("and next 70:\n%s\n", string2->str+142); g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70); g_print ("ok\n"); g_print ("checking timers...\n"); timer = g_timer_new (); g_print (" spinning for 3 seconds...\n"); g_timer_start (timer); while (g_timer_elapsed (timer, NULL) < 3) ; g_timer_stop (timer); g_timer_destroy (timer); g_print ("ok\n"); g_print ("checking g_strcasecmp..."); g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0); g_assert (g_strcasecmp ("frobozz", "frobozz") == 0); g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0); g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0); g_assert (g_strcasecmp ("", "") == 0); g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0); g_assert (g_strcasecmp ("a", "b") < 0); g_assert (g_strcasecmp ("a", "B") < 0); g_assert (g_strcasecmp ("A", "b") < 0); g_assert (g_strcasecmp ("A", "B") < 0); g_assert (g_strcasecmp ("b", "a") > 0); g_assert (g_strcasecmp ("b", "A") > 0); g_assert (g_strcasecmp ("B", "a") > 0); g_assert (g_strcasecmp ("B", "A") > 0); g_print ("ok\n"); g_print ("checking g_strdup..."); g_assert(g_strdup(NULL) == NULL); string = g_strdup(GLIB_TEST_STRING); g_assert(string != NULL); g_assert(strcmp(string, GLIB_TEST_STRING) == 0); g_free(string); g_print ("ok\n"); g_print ("checking g_strconcat..."); string = g_strconcat(GLIB_TEST_STRING, NULL); g_assert(string != NULL); g_assert(strcmp(string, GLIB_TEST_STRING) == 0); g_free(string); string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, GLIB_TEST_STRING, NULL); g_assert(string != NULL); g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING GLIB_TEST_STRING) == 0); g_free(string); g_print ("ok\n"); g_print ("checking g_strdup_printf..."); string = g_strdup_printf ("%05d %-5s", 21, "test"); g_assert (string != NULL); g_assert (strcmp(string, "00021 test ") == 0); g_free (string); g_print ("ok\n"); /* g_debug (argv[0]); */ /* Relation tests */ g_print ("checking relations..."); relation = g_relation_new (2); g_relation_index (relation, 0, g_int_hash, g_int_equal); g_relation_index (relation, 1, g_int_hash, g_int_equal); for (i = 0; i < 1024; i += 1) data[i] = i; for (i = 1; i < 1023; i += 1) { g_relation_insert (relation, data + i, data + i + 1); g_relation_insert (relation, data + i, data + i - 1); } for (i = 2; i < 1022; i += 1) { g_assert (! g_relation_exists (relation, data + i, data + i)); g_assert (! g_relation_exists (relation, data + i, data + i + 2)); g_assert (! g_relation_exists (relation, data + i, data + i - 2)); } for (i = 1; i < 1023; i += 1) { g_assert (g_relation_exists (relation, data + i, data + i + 1)); g_assert (g_relation_exists (relation, data + i, data + i - 1)); } for (i = 2; i < 1022; i += 1) { g_assert (g_relation_count (relation, data + i, 0) == 2); g_assert (g_relation_count (relation, data + i, 1) == 2); } g_assert (g_relation_count (relation, data, 0) == 0); g_assert (g_relation_count (relation, data + 42, 0) == 2); g_assert (g_relation_count (relation, data + 43, 1) == 2); g_assert (g_relation_count (relation, data + 41, 1) == 2); g_relation_delete (relation, data + 42, 0); g_assert (g_relation_count (relation, data + 42, 0) == 0); g_assert (g_relation_count (relation, data + 43, 1) == 1); g_assert (g_relation_count (relation, data + 41, 1) == 1); tuples = g_relation_select (relation, data + 200, 0); g_assert (tuples->len == 2); #if 0 for (i = 0; i < tuples->len; i += 1) { printf ("%d %d\n", *(gint*) g_tuples_index (tuples, i, 0), *(gint*) g_tuples_index (tuples, i, 1)); } #endif g_assert (g_relation_exists (relation, data + 300, data + 301)); g_relation_delete (relation, data + 300, 0); g_assert (!g_relation_exists (relation, data + 300, data + 301)); g_tuples_destroy (tuples); g_relation_destroy (relation); relation = NULL; g_print ("ok\n"); g_print ("checking pointer arrays..."); gparray = g_ptr_array_new (); for (i = 0; i < 10000; i++) g_ptr_array_add (gparray, GINT_TO_POINTER (i)); for (i = 0; i < 10000; i++) if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i)) g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i)); g_ptr_array_free (gparray, TRUE); g_print ("ok\n"); g_print ("checking byte arrays..."); gbarray = g_byte_array_new (); for (i = 0; i < 10000; i++) g_byte_array_append (gbarray, (guint8*) "abcd", 4); for (i = 0; i < 10000; i++) { g_assert (gbarray->data[4*i] == 'a'); g_assert (gbarray->data[4*i+1] == 'b'); g_assert (gbarray->data[4*i+2] == 'c'); g_assert (gbarray->data[4*i+3] == 'd'); } g_byte_array_free (gbarray, TRUE); g_print ("ok\n"); g_printerr ("g_log tests:"); g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345); g_message ("the next warning is a test:"); string = NULL; g_print (string); g_print ("checking endian macros (host is "); #if G_BYTE_ORDER == G_BIG_ENDIAN g_print ("big endian)..."); #else g_print ("little endian)..."); #endif g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2); g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2); #ifdef G_HAVE_GINT64 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2); #endif g_print ("ok\n"); return 0; }
int cr_repomd_record_rename_file(cr_RepomdRecord *md, GError **err) { int x, len; gchar *location_prefix = NULL; const gchar *location_filename = NULL; gchar *new_location_href; gchar *new_location_real; assert(!err || *err == NULL); if (!md) return CRE_OK; if (!(md->location_real) || !strlen(md->location_real)) { g_debug("Empty locations in repomd record object"); g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_BADARG, "Empty locations in repomd record object"); return CRE_BADARG; } if (!md->checksum) { g_debug("Record doesn't contain checksum"); g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_BADARG, "Record doesn't contain checksum"); return CRE_BADARG; } location_filename = md->location_real; x = strlen(md->location_real); for (; x > 0; x--) { if (md->location_real[x] == '/') { location_prefix = g_strndup(md->location_real, x+1); location_filename = cr_get_filename(md->location_real+x+1); break; } } if (!location_prefix) // In case that the location_real doesn't contain '/' location_prefix = g_strdup(""); // Check if the rename is necessary // During update with --keep-all-metadata some files (groupfile, // updateinfo, ..) could already have checksum in filenames if (g_str_has_prefix(location_filename, md->checksum)) { // The filename constains valid checksum g_free(location_prefix); return CRE_OK; } // Skip existing obsolete checksum in the name if there is any len = strlen(location_filename); if (len > 32) { // The filename is long -> it could contains a checksum for (x = 0; x < len; x++) { if (location_filename[x] == '-' && ( x == 32 // Prefix is MD5 checksum || x == 40 // Prefix is SHA1 checksum || x == 64 // Prefix is SHA256 checksum || x == 128 // Prefix is SHA512 checksum )) { location_filename = location_filename + x + 1; break; } } } // Prepare new name new_location_real = g_strconcat(location_prefix, md->checksum, "-", location_filename, NULL); g_free(location_prefix); // Rename file if (g_file_test (new_location_real, G_FILE_TEST_EXISTS)) { if (remove(new_location_real)) { g_critical("%s: Cannot delete old %s", __func__, new_location_real); g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, "File with name %s already exists and cannot be deleted", new_location_real); g_free(new_location_real); return CRE_IO; } } if (rename(md->location_real, new_location_real)) { g_critical("%s: Cannot rename %s to %s", __func__, md->location_real, new_location_real); g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO, "Cannot rename %s to %s", md->location_real, new_location_real); g_free(new_location_real); return CRE_IO; } // Update locations in repomd record md->location_real = g_string_chunk_insert(md->chunk, new_location_real); new_location_href = g_strconcat(LOCATION_HREF_PREFIX, md->checksum, "-", location_filename, NULL); md->location_href = g_string_chunk_insert(md->chunk, new_location_href); g_free(new_location_real); g_free(new_location_href); return CRE_OK; }
static void XMLCALL lr_start_handler(void *pdata, const char *element, const char **attr) { LrParserData *pd = pdata; LrStatesSwitch *sw; if (pd->err) return; // There was an error -> do nothing if (pd->depth != pd->statedepth) { // We are inside of unknown element pd->depth++; return; } pd->depth++; if (!pd->swtab[pd->state]) { // Current element should not have any sub elements return; } // Find current state by its name for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) if (!strcmp(element, sw->ename)) break; if (sw->from != pd->state) { // No state for current element (unknown element) lr_xml_parser_warning(pd, LR_XML_WARNING_UNKNOWNTAG, "Unknown element \"%s\"", element); return; } // Update parser data pd->state = sw->to; pd->docontent = sw->docontent; pd->statedepth = pd->depth; pd->lcontent = 0; pd->content[0] = '\0'; const char *val; switch(pd->state) { case STATE_START: break; case STATE_REPOMD: pd->repomdfound = TRUE; break; case STATE_REVISION: case STATE_TAGS: case STATE_REPO: case STATE_CONTENT: break; case STATE_REPOID: assert(pd->repomd); assert(!pd->repomdrecord); val = lr_find_attr("type", attr); if (val) pd->repomd->repoid_type = g_string_chunk_insert(pd->repomd->chunk, val); break; case STATE_DISTRO: assert(pd->repomd); assert(!pd->repomdrecord); val = lr_find_attr("cpeid", attr); if (val) pd->cpeid = g_strdup(val); break; case STATE_DATA: assert(pd->repomd); assert(!pd->repomdrecord); val = lr_find_attr("type", attr); if (!val) { lr_xml_parser_warning(pd, LR_XML_WARNING_MISSINGATTR, "Missing attribute \"type\" of a data element"); val = "unknown"; } pd->repomdrecord = lr_yum_repomdrecord_init(val); lr_yum_repomd_set_record(pd->repomd, pd->repomdrecord); break; case STATE_LOCATION: assert(pd->repomd); assert(pd->repomdrecord); val = lr_find_attr("href", attr); if (val) pd->repomdrecord->location_href = g_string_chunk_insert( pd->repomdrecord->chunk, val); else lr_xml_parser_warning(pd, LR_XML_WARNING_MISSINGATTR, "Missing attribute \"href\" of a location element"); val = lr_find_attr("xml:base", attr); if (val) pd->repomdrecord->location_base = g_string_chunk_insert( pd->repomdrecord->chunk, val); break; case STATE_CHECKSUM: assert(pd->repomd); assert(pd->repomdrecord); val = lr_find_attr("type", attr); if (!val) { lr_xml_parser_warning(pd, LR_XML_WARNING_MISSINGATTR, "Missing attribute \"type\" of a checksum element"); break; } pd->repomdrecord->checksum_type = g_string_chunk_insert( pd->repomdrecord->chunk, val); break; case STATE_OPENCHECKSUM: assert(pd->repomd); assert(pd->repomdrecord); val = lr_find_attr("type", attr); if (!val) { lr_xml_parser_warning(pd, LR_XML_WARNING_MISSINGATTR, "Missing attribute \"type\" of an open checksum element"); break; } pd->repomdrecord->checksum_open_type = g_string_chunk_insert( pd->repomdrecord->chunk, val); break; case STATE_TIMESTAMP: case STATE_SIZE: case STATE_OPENSIZE: case STATE_DBVERSION: default: break; } }
static void XMLCALL lr_end_handler(void *pdata, G_GNUC_UNUSED const char *element) { LrParserData *pd = pdata; unsigned int state = pd->state; if (pd->err) return; // There was an error -> do nothing if (pd->depth != pd->statedepth) { // Back from the unknown state pd->depth--; return; } pd->depth--; pd->statedepth--; pd->state = pd->sbtab[pd->state]; pd->docontent = 0; switch (state) { case STATE_START: case STATE_REPOMD: break; case STATE_REVISION: assert(pd->repomd); assert(!pd->repomdrecord); if (pd->lcontent == 0) { lr_xml_parser_warning(pd, LR_XML_WARNING_MISSINGVAL, "Missing value of a revision element"); break; } lr_yum_repomd_set_revision(pd->repomd, pd->content); break; case STATE_REPOID: assert(pd->repomd); assert(!pd->repomdrecord); pd->repomd->repoid = g_string_chunk_insert(pd->repomd->chunk, pd->content); break; case STATE_TAGS: break; case STATE_REPO: assert(pd->repomd); assert(!pd->repomdrecord); lr_yum_repomd_add_repo_tag(pd->repomd, pd->content); break; case STATE_CONTENT: assert(pd->repomd); assert(!pd->repomdrecord); lr_yum_repomd_add_content_tag(pd->repomd, pd->content); break; case STATE_DISTRO: assert(pd->repomd); assert(!pd->repomdrecord); lr_yum_repomd_add_distro_tag(pd->repomd, pd->cpeid, pd->content); if (pd->cpeid) { g_free(pd->cpeid); pd->cpeid = NULL; } break; case STATE_DATA: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord = NULL; break; case STATE_LOCATION: break; case STATE_CHECKSUM: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord->checksum = lr_string_chunk_insert( pd->repomdrecord->chunk, pd->content); break; case STATE_OPENCHECKSUM: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord->checksum_open = lr_string_chunk_insert( pd->repomdrecord->chunk, pd->content); break; case STATE_TIMESTAMP: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord->timestamp = lr_xml_parser_strtoll(pd, pd->content, 0); break; case STATE_SIZE: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord->size = lr_xml_parser_strtoll(pd, pd->content, 0); break; case STATE_OPENSIZE: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord->size_open = lr_xml_parser_strtoll(pd, pd->content, 0); break; case STATE_DBVERSION: assert(pd->repomd); assert(pd->repomdrecord); pd->repomdrecord->db_version = (int) lr_xml_parser_strtoll(pd, pd->content, 0); break; default: break; } }
int main (int argc, char *argv[]) { GStringChunk *string_chunk; gchar *tmp_string = NULL, *tmp_string_2; gint i; GString *string1, *string2; string_chunk = g_string_chunk_new (1024); for (i = 0; i < 100000; i ++) { tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); if (strcmp ("hi pete", tmp_string) != 0) g_error ("string chunks are broken.\n"); } tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 != tmp_string && strcmp(tmp_string_2, tmp_string) == 0); tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 == tmp_string); g_string_chunk_free (string_chunk); string1 = g_string_new ("hi pete!"); string2 = g_string_new (""); g_assert (string1 != NULL); g_assert (string2 != NULL); g_assert (strlen (string1->str) == string1->len); g_assert (strlen (string2->str) == string2->len); g_assert (string2->len == 0); g_assert (strcmp ("hi pete!", string1->str) == 0); g_assert (strcmp ("", string2->str) == 0); for (i = 0; i < 10000; i++) g_string_append_c (string1, 'a'+(i%26)); g_assert((strlen("hi pete!") + 10000) == string1->len); g_assert((strlen("hi pete!") + 10000) == strlen(string1->str)); #ifndef G_OS_WIN32 /* MSVC and mingw32 use the same run-time C library, which doesn't like the %10000.10000f format... */ g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #else g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #endif g_string_free (string1, TRUE); g_string_free (string2, TRUE); /* append */ string1 = g_string_new ("firsthalf"); g_string_append (string1, "lasthalf"); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* append_len */ string1 = g_string_new ("firsthalf"); g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf")); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* prepend */ string1 = g_string_new ("lasthalf"); g_string_prepend (string1, "firsthalf"); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* prepend_len */ string1 = g_string_new ("lasthalf"); g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf")); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* insert */ string1 = g_string_new ("firstlast"); g_string_insert (string1, 5, "middle"); g_assert (strcmp (string1->str, "firstmiddlelast") == 0); g_string_free (string1, TRUE); /* insert with pos == end of the string */ string1 = g_string_new ("firstmiddle"); g_string_insert (string1, strlen ("firstmiddle"), "last"); g_assert (strcmp (string1->str, "firstmiddlelast") == 0); g_string_free (string1, TRUE); /* insert_len */ string1 = g_string_new ("firstlast"); g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle")); g_assert (strcmp (string1->str, "firstmiddlelast") == 0); g_string_free (string1, TRUE); /* insert_len with magic -1 pos for append */ string1 = g_string_new ("first"); g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last")); g_assert (strcmp (string1->str, "firstlast") == 0); g_string_free (string1, TRUE); /* insert_len with magic -1 len for strlen-the-string */ string1 = g_string_new ("first"); g_string_insert_len (string1, 5, "last", -1); g_assert (strcmp (string1->str, "firstlast") == 0); g_string_free (string1, TRUE); /* g_string_equal */ string1 = g_string_new ("test"); string2 = g_string_new ("te"); g_assert (! g_string_equal(string1, string2)); g_string_append (string2, "st"); g_assert (g_string_equal(string1, string2)); g_string_free (string1, TRUE); g_string_free (string2, TRUE); /* Check handling of embedded ASCII 0 (NUL) characters in GString. */ string1 = g_string_new ("fiddle"); string2 = g_string_new ("fiddle"); g_assert (g_string_equal(string1, string2)); g_string_append_c(string1, '\0'); g_assert (! g_string_equal(string1, string2)); g_string_append_c(string2, '\0'); g_assert (g_string_equal(string1, string2)); g_string_append_c(string1, 'x'); g_string_append_c(string2, 'y'); g_assert (! g_string_equal(string1, string2)); g_assert (string1->len == 8); g_string_append(string1, "yzzy"); g_assert (string1->len == 12); g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0); g_string_insert(string1, 1, "QED"); g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0); g_string_free (string1, TRUE); g_string_free (string2, TRUE); return 0; }
void tag_label(metafile_t *mf, unsigned long t, const char *str) { tag_t *tag = tag_get(mf, t); tag->label = g_string_chunk_insert(mf->gsc, str); }
gboolean lr_download_packages(GSList *targets, LrPackageDownloadFlag flags, GError **err) { gboolean ret; gboolean failfast = flags & LR_PACKAGEDOWNLOAD_FAILFAST; struct sigaction old_sigact; GSList *downloadtargets = NULL; gboolean interruptible = FALSE; assert(!err || *err == NULL); if (!targets) return TRUE; // Check targets for (GSList *elem = targets; elem; elem = g_slist_next(elem)) { LrPackageTarget *packagetarget = elem->data; if (!packagetarget->handle) { continue; /* g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_BADFUNCARG, "Package target %s doesn't have specified a handle", packagetarget->relative_url); return FALSE; */ } if (packagetarget->handle->interruptible) interruptible = TRUE; // Check repotype // Note: Checked because lr_handle_prepare_internal_mirrorlist // support only LR_YUMREPO yet if (packagetarget->handle->repotype != LR_YUMREPO) { g_debug("%s: Bad repo type", __func__); g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_BADFUNCARG, "Bad repo type"); return FALSE; } } // Setup sighandler if (interruptible) { struct sigaction sigact; g_debug("%s: Using own SIGINT handler", __func__); memset(&sigact, 0, sizeof(old_sigact)); memset(&sigact, 0, sizeof(sigact)); sigemptyset(&sigact.sa_mask); sigact.sa_handler = lr_sigint_handler; sigaddset(&sigact.sa_mask, SIGINT); sigact.sa_flags = SA_RESTART; if (sigaction(SIGINT, &sigact, &old_sigact) == -1) { g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_SIGACTION, "Cannot set Librepo SIGINT handler"); return FALSE; } } // List of handles for fastest mirror resolving GSList *fmr_handles = NULL; // Prepare targets for (GSList *elem = targets; elem; elem = g_slist_next(elem)) { gchar *local_path; LrPackageTarget *packagetarget = elem->data; LrDownloadTarget *downloadtarget; gint64 realsize = -1; gboolean doresume = packagetarget->resume; // Reset output attributes of the handle lr_packagetarget_reset(packagetarget); // Prepare destination filename if (packagetarget->dest) { if (g_file_test(packagetarget->dest, G_FILE_TEST_IS_DIR)) { // Dir specified gchar *file_basename = g_path_get_basename(packagetarget->relative_url); local_path = g_build_filename(packagetarget->dest, file_basename, NULL); g_free(file_basename); } else { local_path = g_strdup(packagetarget->dest); } } else { // No destination path specified local_path = g_path_get_basename(packagetarget->relative_url); } packagetarget->local_path = g_string_chunk_insert(packagetarget->chunk, local_path); g_free(local_path); // Check expected size and real size if the file exists if (doresume && g_access(packagetarget->local_path, R_OK) == 0 && packagetarget->expectedsize > 0) { struct stat buf; if (stat(packagetarget->local_path, &buf)) { g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_IO, "Cannot stat %s: %s", packagetarget->local_path, g_strerror(errno)); return FALSE; } realsize = buf.st_size; if (packagetarget->expectedsize < realsize) // Existing file is bigger then the one that is expected, // disable resuming doresume = FALSE; } if (g_access(packagetarget->local_path, R_OK) == 0 && packagetarget->checksum && packagetarget->checksum_type != LR_CHECKSUM_UNKNOWN) { /* If the file exists and checksum is ok, then is pointless to * download the file again. * Moreover, if the resume is enabled and the file is already * completely downloaded, then the download is going to fail. */ int fd_r = open(packagetarget->local_path, O_RDONLY); if (fd_r != -1) { gboolean matches; ret = lr_checksum_fd_cmp(packagetarget->checksum_type, fd_r, packagetarget->checksum, 1, &matches, NULL); close(fd_r); if (ret && matches) { // Checksum calculation was ok and checksum matches g_debug("%s: Package %s is already downloaded (checksum matches)", __func__, packagetarget->local_path); packagetarget->err = g_string_chunk_insert( packagetarget->chunk, "Already downloaded"); // Call end callback LrEndCb end_cb = packagetarget->endcb; if (end_cb) end_cb(packagetarget->cbdata, LR_TRANSFER_ALREADYEXISTS, "Already downloaded"); continue; } else if (ret) { // Checksum calculation was ok but checksum doesn't match if (realsize != -1 && realsize == packagetarget->expectedsize) // File size is the same as the expected one // Don't try to resume doresume = FALSE; } } } if (doresume && realsize != -1 && realsize == packagetarget->expectedsize) { // File's size matches the expected one, the resume is enabled and // no checksum is known => expect that the file is // the one the user wants g_debug("%s: Package %s is already downloaded (size matches)", __func__, packagetarget->local_path); packagetarget->err = g_string_chunk_insert( packagetarget->chunk, "Already downloaded"); // Call end callback LrEndCb end_cb = packagetarget->endcb; if (end_cb) end_cb(packagetarget->cbdata, LR_TRANSFER_ALREADYEXISTS, "Already downloaded"); continue; } if (packagetarget->handle) { ret = lr_handle_prepare_internal_mirrorlist(packagetarget->handle, FALSE, err); if (!ret) goto cleanup; if (packagetarget->handle->fastestmirror) { if (!g_slist_find(fmr_handles, packagetarget->handle)) fmr_handles = g_slist_prepend(fmr_handles, packagetarget->handle); } } GSList *checksums = NULL; LrDownloadTargetChecksum *checksum; checksum = lr_downloadtargetchecksum_new(packagetarget->checksum_type, packagetarget->checksum); checksums = g_slist_prepend(checksums, checksum); downloadtarget = lr_downloadtarget_new(packagetarget->handle, packagetarget->relative_url, packagetarget->base_url, -1, packagetarget->local_path, checksums, packagetarget->expectedsize, doresume, packagetarget->progresscb, packagetarget->cbdata, packagetarget->endcb, packagetarget->mirrorfailurecb, packagetarget, packagetarget->byterangestart, packagetarget->byterangeend); downloadtargets = g_slist_append(downloadtargets, downloadtarget); } // Do Fastest Mirror resolving for all handles in one shot if (fmr_handles) { fmr_handles = g_slist_reverse(fmr_handles); ret = lr_fastestmirror_sort_internalmirrorlists(fmr_handles, err); g_slist_free(fmr_handles); if (!ret) { return FALSE; } } // Start downloading ret = lr_download(downloadtargets, failfast, err); cleanup: // Copy download statuses from downloadtargets to targets for (GSList *elem = downloadtargets; elem; elem = g_slist_next(elem)) { LrDownloadTarget *downloadtarget = elem->data; LrPackageTarget *packagetarget = downloadtarget->userdata; if (downloadtarget->err) packagetarget->err = g_string_chunk_insert(packagetarget->chunk, downloadtarget->err); } // Free downloadtargets list g_slist_free_full(downloadtargets, (GDestroyNotify)lr_downloadtarget_free); // Restore original signal handler if (interruptible) { g_debug("%s: Restoring an old SIGINT handler", __func__); sigaction(SIGINT, &old_sigact, NULL); if (lr_interrupt) { if (err && *err != NULL) g_clear_error(err); g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_INTERRUPTED, "Insterupted by a SIGINT signal"); return FALSE; } } return ret; }
gboolean lr_check_packages(GSList *targets, LrPackageCheckFlag flags, GError **err) { gboolean ret = TRUE; gboolean failfast = flags & LR_PACKAGECHECK_FAILFAST; struct sigaction old_sigact; gboolean interruptible = FALSE; assert(!err || *err == NULL); if (!targets) return TRUE; // Check targets for (GSList *elem = targets; elem; elem = g_slist_next(elem)) { LrPackageTarget *packagetarget = elem->data; if (packagetarget->handle->interruptible) interruptible = TRUE; if (!packagetarget->checksum || packagetarget->checksum_type == LR_CHECKSUM_UNKNOWN) { g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_BADOPTARG, "Target %s doesn't have specified " "checksum value or checksum type!", packagetarget->relative_url); return FALSE; } } // Setup sighandler if (interruptible) { g_debug("%s: Using own SIGINT handler", __func__); struct sigaction sigact; sigact.sa_handler = lr_sigint_handler; sigaddset(&sigact.sa_mask, SIGINT); sigact.sa_flags = SA_RESTART; if (sigaction(SIGINT, &sigact, &old_sigact) == -1) { g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_SIGACTION, "Cannot set Librepo SIGINT handler"); return FALSE; } } for (GSList *elem = targets; elem; elem = g_slist_next(elem)) { gchar *local_path; LrPackageTarget *packagetarget = elem->data; // Prepare destination filename if (packagetarget->dest) { if (g_file_test(packagetarget->dest, G_FILE_TEST_IS_DIR)) { // Dir specified gchar *file_basename = g_path_get_basename(packagetarget->relative_url); local_path = g_build_filename(packagetarget->dest, file_basename, NULL); g_free(file_basename); } else { local_path = g_strdup(packagetarget->dest); } } else { // No destination path specified local_path = g_path_get_basename(packagetarget->relative_url); } packagetarget->local_path = g_string_chunk_insert(packagetarget->chunk, local_path); if (g_access(packagetarget->local_path, R_OK) == 0) { // If the file exists check its checksum int fd_r = open(packagetarget->local_path, O_RDONLY); if (fd_r != -1) { // File was successfully opened gboolean matches; ret = lr_checksum_fd_cmp(packagetarget->checksum_type, fd_r, packagetarget->checksum, 1, &matches, NULL); close(fd_r); if (ret && matches) { // Checksum is ok packagetarget->err = NULL; g_debug("%s: Package %s is already downloaded (checksum matches)", __func__, packagetarget->local_path); } else { // Checksum doesn't match or checksuming error packagetarget->err = g_string_chunk_insert( packagetarget->chunk, "Checksum of doesn't match"); if (failfast) { ret = FALSE; g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_BADCHECKSUM, "File with nonmatching checksum found"); break; } } } else { // Cannot open the file packagetarget->err = g_string_chunk_insert(packagetarget->chunk, "Cannot be opened"); if (failfast) { ret = FALSE; g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_IO, "Cannot open %s", packagetarget->local_path); break; } } } else { // File doesn't exists packagetarget->err = g_string_chunk_insert(packagetarget->chunk, "Doesn't exist"); if (failfast) { ret = FALSE; g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_IO, "File %s doesn't exists", packagetarget->local_path); break; } } } // Restore original signal handler if (interruptible) { g_debug("%s: Restoring an old SIGINT handler", __func__); sigaction(SIGINT, &old_sigact, NULL); if (lr_interrupt) { if (err && *err != NULL) g_clear_error(err); g_set_error(err, LR_PACKAGE_DOWNLOADER_ERROR, LRE_INTERRUPTED, "Insterupted by a SIGINT signal"); return FALSE; } } return ret; }
static void nm_cmd_line_add_string (NMCmdLine *cmd, const char *str) { g_ptr_array_add (cmd->array, g_string_chunk_insert (cmd->chunk, str)); }