Beispiel #1
0
void testStaticBuffer(
    Test * pTest)
{
    STATIC_BUFFER sbuffer;
    char *data1 = "Joshua";
    char *data2 = "Anna";
    char *data3 = "Christopher";
    char *data4 = "Mary";
    char data_buffer[480] = "";
    char test_data_buffer[480] = "";
    char *data;
    unsigned count;

    sbuf_init(&sbuffer, NULL, 0);
    ct_test(pTest, sbuf_empty(&sbuffer) == true);
    ct_test(pTest, sbuf_data(&sbuffer) == NULL);
    ct_test(pTest, sbuf_size(&sbuffer) == 0);
    ct_test(pTest, sbuf_count(&sbuffer) == 0);
    ct_test(pTest, sbuf_append(&sbuffer, data1, strlen(data1)) == false);

    sbuf_init(&sbuffer, data_buffer, sizeof(data_buffer));
    ct_test(pTest, sbuf_empty(&sbuffer) == true);
    ct_test(pTest, sbuf_data(&sbuffer) == data_buffer);
    ct_test(pTest, sbuf_size(&sbuffer) == sizeof(data_buffer));
    ct_test(pTest, sbuf_count(&sbuffer) == 0);

    ct_test(pTest, sbuf_append(&sbuffer, data1, strlen(data1)) == true);
    ct_test(pTest, sbuf_append(&sbuffer, data2, strlen(data2)) == true);
    ct_test(pTest, sbuf_append(&sbuffer, data3, strlen(data3)) == true);
    ct_test(pTest, sbuf_append(&sbuffer, data4, strlen(data4)) == true);
    strcat(test_data_buffer, data1);
    strcat(test_data_buffer, data2);
    strcat(test_data_buffer, data3);
    strcat(test_data_buffer, data4);
    ct_test(pTest, sbuf_count(&sbuffer) == strlen(test_data_buffer));

    data = sbuf_data(&sbuffer);
    count = sbuf_count(&sbuffer);
    ct_test(pTest, memcmp(data, test_data_buffer, count) == 0);
    ct_test(pTest, count == strlen(test_data_buffer));

    ct_test(pTest, sbuf_truncate(&sbuffer, 0) == true);
    ct_test(pTest, sbuf_count(&sbuffer) == 0);
    ct_test(pTest, sbuf_size(&sbuffer) == sizeof(data_buffer));
    ct_test(pTest, sbuf_append(&sbuffer, data4, strlen(data4)) == true);
    data = sbuf_data(&sbuffer);
    count = sbuf_count(&sbuffer);
    ct_test(pTest, memcmp(data, data4, count) == 0);
    ct_test(pTest, count == strlen(data4));

    return;
}
Beispiel #2
0
struct pkgdb_it *
pkgdb_rquery(struct pkgdb *db, const char *pattern, match_t match,
             const char *repo)
{
    sqlite3_stmt	*stmt = NULL;
    struct sbuf	*sql = NULL;
    const char	*reponame = NULL;
    const char	*comp = NULL;
    int		 ret;
    char		 basesql[BUFSIZ] = ""
                                   "SELECT id, origin, name, version, comment, "
                                   "prefix, desc, arch, maintainer, www, "
                                   "licenselogic, flatsize, pkgsize, "
                                   "cksum, manifestdigest, path AS repopath, '%1$s' AS dbname "
                                   "FROM '%1$s'.packages p";

    assert(db != NULL);
    assert(match == MATCH_ALL || (pattern != NULL && pattern[0] != '\0'));

    /*
     * If we have no remote repos loaded, we just return nothing instead of failing
     * an assert deep inside pkgdb_get_reponame
     */
    if (db->type != PKGDB_REMOTE)
        return (NULL);

    reponame = pkgdb_get_reponame(db, repo);

    sql = sbuf_new_auto();
    comp = pkgdb_get_pattern_query(pattern, match);
    if (comp && comp[0])
        strlcat(basesql, comp, sizeof(basesql));

    /*
     * Working on multiple remote repositories
     */
    if (reponame == NULL) {
        /* duplicate the query via UNION for all the attached
         * databases */

        ret = pkgdb_sql_all_attached(db->sqlite, sql,
                                     basesql, " UNION ALL ");
        if (ret != EPKG_OK) {
            sbuf_delete(sql);
            return (NULL);
        }
    } else
        sbuf_printf(sql, basesql, reponame, reponame);

    sbuf_cat(sql, " ORDER BY name;");
    sbuf_finish(sql);

    pkg_debug(4, "Pkgdb: running '%s'", sbuf_get(sql));
    ret = sqlite3_prepare_v2(db->sqlite, sbuf_get(sql), sbuf_size(sql), &stmt, NULL);
    if (ret != SQLITE_OK) {
        ERROR_SQLITE(db->sqlite);
        sbuf_delete(sql);
        return (NULL);
    }

    sbuf_delete(sql);

    if (match != MATCH_ALL && match != MATCH_CONDITION)
        sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT);

    return (pkgdb_it_new(db, stmt, PKG_REMOTE, PKGDB_IT_FLAG_ONCE));
}