void test_manifest(void) { struct pkg *p = NULL; struct pkg_dep *dep = NULL; struct pkg_conflict *conflict = NULL; struct pkg_option *option = NULL; struct pkg_category *category = NULL; struct pkg_file *file = NULL; struct pkg_manifest_key *keys = NULL; const char *pkg_str; int64_t pkg_int; int i; pkg_manifest_keys_new(&keys); ATF_REQUIRE(keys != NULL); ATF_REQUIRE_EQ(EPKG_OK, pkg_new(&p, PKG_FILE)); ATF_REQUIRE(p != NULL); ATF_REQUIRE_EQ(EPKG_OK, pkg_parse_manifest(p, manifest, strlen(manifest), keys)); pkg_manifest_keys_free(keys); ATF_REQUIRE(pkg_get(p, PKG_NAME, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "foobar") == 0); ATF_REQUIRE(pkg_get(p, PKG_VERSION, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "0.3") == 0); ATF_REQUIRE(pkg_get(p, PKG_ORIGIN, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "foo/bar") == 0); ATF_REQUIRE(pkg_get(p, PKG_COMMENT, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "A dummy manifest") == 0); ATF_REQUIRE(pkg_get(p, PKG_ARCH, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "amd64") == 0); ATF_REQUIRE(pkg_get(p, PKG_WWW, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "http://www.foobar.com") == 0); ATF_REQUIRE(pkg_get(p, PKG_MAINTAINER, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "*****@*****.**") == 0); ATF_REQUIRE(pkg_get(p, PKG_PREFIX, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "/opt/prefix") == 0); ATF_REQUIRE(pkg_get(p, PKG_DESC, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "port description") == 0); ATF_REQUIRE(pkg_get(p, PKG_MESSAGE, &pkg_str) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_str, "pkg message") == 0); ATF_REQUIRE(pkg_get(p, PKG_FLATSIZE, &pkg_int) == EPKG_OK); ATF_REQUIRE(pkg_int == 10000); i = 0; while (pkg_deps(p, &dep) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_dep_name(dep), "depfoo") == 0); ATF_REQUIRE(strcmp(pkg_dep_origin(dep), "dep/foo") == 0); ATF_REQUIRE(strcmp(pkg_dep_version(dep), "1.2") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_dep_name(dep), "depbar") == 0); ATF_REQUIRE(strcmp(pkg_dep_origin(dep), "dep/bar") == 0); ATF_REQUIRE(strcmp(pkg_dep_version(dep), "3.4") == 0); } i++; } ATF_REQUIRE(i == 2); i = 0; #if 0 while (pkg_conflicts(p, &conflict) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_conflict_glob(conflict), "foo-*") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_conflict_glob(conflict), "bar-*") == 0); } i++; } ATF_REQUIRE(i == 2); #endif i = 0; while (pkg_options(p, &option) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_option_opt(option), "foo") == 0); ATF_REQUIRE(strcmp(pkg_option_value(option), "true") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_option_opt(option), "bar") == 0); ATF_REQUIRE(strcmp(pkg_option_value(option), "false") == 0); } i++; } ATF_REQUIRE(i == 2); i = 0; while (pkg_categories(p, &category) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_category_name(category), "foo") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_category_name(category), "bar") == 0); } i++; } ATF_REQUIRE(i == 2); ATF_REQUIRE(pkg_files(p, &file) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_file_path(file), "/usr/local/bin/foo") == 0); #if 0 ATF_REQUIRE(strcmp(pkg_file_sha256(file), "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b") == 0); #endif pkg_free(p); /* p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest1) == EPKG_FATAL); pkg_free(p); p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest2) == EPKG_FATAL); pkg_free(p); p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest3) == EPKG_FATAL); pkg_free(p); p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest4) == EPKG_FATAL); pkg_free(p); */ }
static int pkg_repo_binary_add_pkg(struct pkg *pkg, const char *pkg_path, sqlite3 *sqlite, bool forced) { const char *name, *version, *origin, *comment, *desc; const char *arch, *maintainer, *www, *prefix, *sum, *rpath; const char *olddigest, *manifestdigest; int64_t flatsize, pkgsize; int64_t licenselogic; int ret; struct pkg_dep *dep = NULL; struct pkg_option *option = NULL; struct pkg_shlib *shlib = NULL; const pkg_object *obj, *licenses, *categories, *annotations; pkg_iter it; int64_t package_id; pkg_get(pkg, PKG_ORIGIN, &origin, PKG_NAME, &name, PKG_VERSION, &version, PKG_COMMENT, &comment, PKG_DESC, &desc, PKG_ARCH, &arch, PKG_MAINTAINER, &maintainer, PKG_WWW, &www, PKG_PREFIX, &prefix, PKG_FLATSIZE, &flatsize, PKG_LICENSE_LOGIC, &licenselogic, PKG_CKSUM, &sum, PKG_PKGSIZE, &pkgsize, PKG_REPOPATH, &rpath, PKG_LICENSES, &licenses, PKG_CATEGORIES, &categories, PKG_ANNOTATIONS, &annotations, PKG_OLD_DIGEST, &olddigest, PKG_DIGEST, &manifestdigest); try_again: if ((ret = pkg_repo_binary_run_prstatement(PKG, origin, name, version, comment, desc, arch, maintainer, www, prefix, pkgsize, flatsize, (int64_t)licenselogic, sum, rpath, manifestdigest, olddigest)) != SQLITE_DONE) { if (ret == SQLITE_CONSTRAINT) { switch(pkg_repo_binary_delete_conflicting(origin, version, pkg_path, forced)) { case EPKG_FATAL: /* sqlite error */ ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(PKG)); return (EPKG_FATAL); break; case EPKG_END: /* repo already has newer */ return (EPKG_END); break; default: /* conflict cleared, try again */ goto try_again; break; } } else { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(PKG)); return (EPKG_FATAL); } } package_id = sqlite3_last_insert_rowid(sqlite); if (pkg_repo_binary_run_prstatement (FTS_APPEND, package_id, name, version, origin) != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(FTS_APPEND)); return (EPKG_FATAL); } dep = NULL; while (pkg_deps(pkg, &dep) == EPKG_OK) { if (pkg_repo_binary_run_prstatement(DEPS, pkg_dep_origin(dep), pkg_dep_name(dep), pkg_dep_version(dep), package_id) != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(DEPS)); return (EPKG_FATAL); } } it = NULL; while ((obj = pkg_object_iterate(categories, &it))) { ret = pkg_repo_binary_run_prstatement(CAT1, pkg_object_string(obj)); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(CAT2, package_id, pkg_object_string(obj)); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(CAT2)); return (EPKG_FATAL); } } it = NULL; while ((obj = pkg_object_iterate(licenses, &it))) { ret = pkg_repo_binary_run_prstatement(LIC1, pkg_object_string(obj)); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(LIC2, package_id, pkg_object_string(obj)); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(LIC2)); return (EPKG_FATAL); } } option = NULL; while (pkg_options(pkg, &option) == EPKG_OK) { ret = pkg_repo_binary_run_prstatement(OPT1, pkg_option_opt(option)); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(OPT2, pkg_option_opt(option), pkg_option_value(option), package_id); if(ret != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(OPT2)); return (EPKG_FATAL); } } shlib = NULL; while (pkg_shlibs_required(pkg, &shlib) == EPKG_OK) { const char *shlib_name = pkg_shlib_name(shlib); ret = pkg_repo_binary_run_prstatement(SHLIB1, shlib_name); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(SHLIB_REQD, package_id, shlib_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(SHLIB_REQD)); return (EPKG_FATAL); } } shlib = NULL; while (pkg_shlibs_provided(pkg, &shlib) == EPKG_OK) { const char *shlib_name = pkg_shlib_name(shlib); ret = pkg_repo_binary_run_prstatement(SHLIB1, shlib_name); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(SHLIB_PROV, package_id, shlib_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(SHLIB_PROV)); return (EPKG_FATAL); } } it = NULL; while ((obj = pkg_object_iterate(annotations, &it))) { const char *note_tag = pkg_object_key(obj); const char *note_val = pkg_object_string(obj); ret = pkg_repo_binary_run_prstatement(ANNOTATE1, note_tag); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(ANNOTATE1, note_val); if (ret == SQLITE_DONE) ret = pkg_repo_binary_run_prstatement(ANNOTATE2, package_id, note_tag, note_val); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite, pkg_repo_binary_sql_prstatement(ANNOTATE2)); return (EPKG_FATAL); } } return (EPKG_OK); }
static void print_query(struct pkg *pkg, char *qstr, char multiline) { struct sbuf *output = sbuf_new_auto(); struct pkg_dep *dep = NULL; struct pkg_category *cat = NULL; struct pkg_option *option = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct pkg_license *lic = NULL; struct pkg_user *user = NULL; struct pkg_group *group = NULL; struct pkg_script *scripts = NULL; switch (multiline) { case 'd': while (pkg_deps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_get(output)); break; } case 'r': while (pkg_rdeps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_get(output)); } break; case 'C': while (pkg_categories(pkg, &cat) == EPKG_OK) { format_str(pkg, output, qstr, cat); printf("%s\n", sbuf_get(output)); } break; case 'O': while (pkg_options(pkg, &option) == EPKG_OK) { format_str(pkg, output, qstr, option); printf("%s\n", sbuf_get(output)); } break; case 'F': while (pkg_files(pkg, &file) == EPKG_OK) { format_str(pkg, output, qstr, file); printf("%s\n", sbuf_get(output)); } break; case 'D': while (pkg_dirs(pkg, &dir) == EPKG_OK) { format_str(pkg, output, qstr, dir); printf("%s\n", sbuf_get(output)); } break; case 'L': while (pkg_licenses(pkg, &lic) == EPKG_OK) { format_str(pkg, output, qstr, lic); printf("%s\n", sbuf_get(output)); } break; case 'U': while (pkg_users(pkg, &user) == EPKG_OK) { format_str(pkg, output, qstr, user); printf("%s\n", sbuf_get(output)); } break; case 'G': while (pkg_users(pkg, &user) == EPKG_OK) { format_str(pkg, output, qstr, group); printf("%s\n", sbuf_get(output)); } break; case 'S': while (pkg_scripts(pkg, &scripts) == EPKG_OK) { format_str(pkg, output, qstr, scripts); printf("%s\n", sbuf_get(output)); } break; default: format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_get(output)); break; } sbuf_delete(output); }
void print_info(struct pkg * const pkg, unsigned int options) { struct pkg_category *cat = NULL; struct pkg_dep *dep = NULL; struct pkg_dir *dir = NULL; struct pkg_file *file = NULL; struct pkg_group *group = NULL; struct pkg_license *lic = NULL; struct pkg_option *option = NULL; struct pkg_shlib *shlib = NULL; struct pkg_user *user = NULL; bool multirepos_enabled = false; bool print_tag = false; bool show_locks = false; char size[7]; const char *name, *version, *prefix, *origin, *reponame, *repourl; const char *maintainer, *www, *comment, *desc, *message, *arch; const char *repopath; const char *tab; char *m; unsigned opt; int64_t flatsize, newflatsize, newpkgsize; lic_t licenselogic; bool locked; int cout = 0; /* Number of characters output */ int info_num; /* Number of different data items to print */ pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multirepos_enabled); pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version, PKG_PREFIX, &prefix, PKG_ORIGIN, &origin, PKG_REPONAME, &reponame, PKG_REPOURL, &repourl, PKG_MAINTAINER, &maintainer, PKG_WWW, &www, PKG_COMMENT, &comment, PKG_DESC, &desc, PKG_FLATSIZE, &flatsize, PKG_NEW_FLATSIZE, &newflatsize, PKG_NEW_PKGSIZE, &newpkgsize, PKG_LICENSE_LOGIC, &licenselogic, PKG_MESSAGE, &message, PKG_ARCH, &arch, PKG_REPOPATH, &repopath, PKG_LOCKED, &locked); if (!multirepos_enabled) pkg_config_string(PKG_CONFIG_REPO, &repourl); if (options & INFO_RAW) { /* Not for remote packages */ if (pkg_type(pkg) != PKG_REMOTE) { pkg_emit_manifest(pkg, &m); printf("%s\n", m); free(m); } return; } /* Show locking status when requested to display it and the package is locally installed */ if (pkg_type(pkg) == PKG_INSTALLED && (options & INFO_LOCKED) != 0) show_locks = true; if (!quiet) { /* Print a tag-line identifying the package -- either NAMEVER, ORIGIN or NAME (in that order of preference). This may be the only output from this function */ if (options & INFO_TAG_NAMEVER) cout = printf("%s-%s", name, version); else if (options & INFO_TAG_ORIGIN) cout = printf("%s", origin); else if (options & INFO_TAG_NAME) cout = printf("%s", name); } /* Don't display a tab if quiet, retains compatibility. */ tab = quiet ? "" : "\t"; /* If we printed a tag, and there are no other items to print, then just return now. If there's only one single-line item to print, show it at column 32 on the same line. If there's one multi-line item to print, start a new line. If there is more than one item to print per pkg, use 'key : value' style to show on a new line. */ info_num = 0; for (opt = 0x1U; opt <= INFO_LASTFIELD; opt <<= 1) if ((opt & options) != 0) info_num++; if (info_num == 0 && cout > 0) { printf("\n"); return; } if (info_num == 1) { /* Only one item to print */ print_tag = false; if (!quiet) { if (options & INFO_MULTILINE) printf(":\n"); else { if (cout < 31) cout = 31 - cout; else cout = 1; printf("%*s", cout, " "); } } } else { /* Several items to print */ print_tag = true; if (!quiet) printf("\n"); } for (opt = 0x1; opt <= INFO_LASTFIELD; opt <<= 1) { if ((opt & options) == 0) continue; switch (opt) { case INFO_NAME: if (print_tag) printf("%-15s: ", "Name"); printf("%s\n", name); break; case INFO_VERSION: if (print_tag) printf("%-15s: ", "Version"); printf("%s\n", version); break; case INFO_ORIGIN: if (print_tag) printf("%-15s: ", "Origin"); printf("%s\n", origin); break; case INFO_PREFIX: if (print_tag) printf("%-15s: ", "Prefix"); printf("%s\n", prefix); break; case INFO_REPOSITORY: if (pkg_type(pkg) == PKG_REMOTE && repourl != NULL && repourl[0] != '\0') { if (print_tag) printf("%-15s: ", "Repository"); printf("%s [%s]\n", reponame, repourl); } else if (!print_tag) printf("\n"); break; case INFO_CATEGORIES: if (pkg_list_count(pkg, PKG_CATEGORIES) > 0) { if (print_tag) printf("%-15s: ", "Categories"); if (pkg_categories(pkg, &cat) == EPKG_OK) printf("%s", pkg_category_name(cat)); while (pkg_categories(pkg, &cat) == EPKG_OK) printf(" %s", pkg_category_name(cat)); printf("\n"); } else if (!print_tag) printf("\n"); break; case INFO_LICENSES: if (pkg_list_count(pkg, PKG_LICENSES) > 0) { if (print_tag) printf("%-15s: ", "Licenses"); if (pkg_licenses(pkg, &lic) == EPKG_OK) printf("%s", pkg_license_name(lic)); while (pkg_licenses(pkg, &lic) == EPKG_OK) { if (licenselogic != 1) printf(" %c", licenselogic); printf(" %s", pkg_license_name(lic)); } printf("\n"); } else if (!print_tag) printf("\n"); break; case INFO_MAINTAINER: if (print_tag) printf("%-15s: ", "Maintainer"); printf("%s\n", maintainer); break; case INFO_WWW: if (print_tag) printf("%-15s: ", "WWW"); printf("%s\n", www); break; case INFO_COMMENT: if (print_tag) printf("%-15s: ", "Comment"); printf("%s\n", comment); break; case INFO_OPTIONS: if (pkg_list_count(pkg, PKG_OPTIONS) > 0) { if (print_tag) printf("%-15s:\n", "Options"); while (pkg_options(pkg, &option) == EPKG_OK) printf("%s%-15s: %s\n", tab, pkg_option_opt(option), pkg_option_value(option)); } break; case INFO_SHLIBS_REQUIRED: if (pkg_list_count(pkg, PKG_SHLIBS_REQUIRED) > 0) { if (print_tag) printf("%-15s:\n", "Shared Libs required"); while (pkg_shlibs_required(pkg, &shlib) == EPKG_OK) printf("%s%s\n", tab, pkg_shlib_name(shlib)); } break; case INFO_SHLIBS_PROVIDED: if (pkg_list_count(pkg, PKG_SHLIBS_PROVIDED) > 0) { if (print_tag) printf("%-15s:\n", "Shared Libs provided"); while (pkg_shlibs_provided(pkg, &shlib) == EPKG_OK) printf("%s%s\n", tab, pkg_shlib_name(shlib)); } break; case INFO_FLATSIZE: if (pkg_type(pkg) == PKG_INSTALLED || pkg_type(pkg) == PKG_FILE) humanize_number(size, sizeof(size), flatsize,"B", HN_AUTOSCALE, 0); else humanize_number(size, sizeof(size), newflatsize,"B", HN_AUTOSCALE, 0); if (print_tag) printf("%-15s: ", "Flat size"); printf("%s\n", size); break; case INFO_PKGSIZE: /* Remote pkgs only */ if (pkg_type(pkg) == PKG_REMOTE) { humanize_number(size, sizeof(size), newpkgsize,"B", HN_AUTOSCALE, 0); if (print_tag) printf("%-15s: ", "Pkg size"); printf("%s\n", size); } else if (!print_tag) printf("\n"); break; case INFO_DESCR: if (print_tag) printf("%-15s:\n", "Description"); printf("%s\n", desc); break; case INFO_MESSAGE: if (message) { if (print_tag) printf("%-15s:\n", "Message"); printf("%s\n", message); } break; case INFO_DEPS: if (pkg_list_count(pkg, PKG_DEPS) > 0) { if (print_tag) printf("%-15s:\n", "Depends on"); while (pkg_deps(pkg, &dep) == EPKG_OK) { printf("%s%s-%s", tab, pkg_dep_name(dep), pkg_dep_version(dep)); if (show_locks && pkg_dep_is_locked(dep)) printf(" (*)"); printf("\n"); } } break; case INFO_RDEPS: if (pkg_list_count(pkg, PKG_RDEPS) > 0) { if (print_tag) printf("%-15s:\n", "Required by"); while (pkg_rdeps(pkg, &dep) == EPKG_OK) { printf("%s%s-%s", tab, pkg_dep_name(dep), pkg_dep_version(dep)); if (show_locks && pkg_dep_is_locked(dep)) printf(" (*)"); printf("\n"); } } break; case INFO_FILES: /* Installed pkgs only */ if (pkg_type(pkg) != PKG_REMOTE && pkg_list_count(pkg, PKG_FILES) > 0) { if (print_tag) printf("%-15s:\n", "Files"); while (pkg_files(pkg, &file) == EPKG_OK) printf("%s%s\n", tab, pkg_file_path(file)); } break; case INFO_DIRS: /* Installed pkgs only */ if (pkg_type(pkg) != PKG_REMOTE && pkg_list_count(pkg, PKG_DIRS) > 0) { if (print_tag) printf("%-15s:\n", "Directories"); while (pkg_dirs(pkg, &dir) == EPKG_OK) printf("%s%s\n", tab, pkg_dir_path(dir)); } break; case INFO_USERS: /* Installed pkgs only */ if (pkg_type(pkg) != PKG_REMOTE && pkg_list_count(pkg, PKG_USERS) > 0) { if (print_tag) printf("%-15s: ", "Users"); if (pkg_users(pkg, &user) == EPKG_OK) printf("%s", pkg_user_name(user)); while (pkg_users(pkg, &user) == EPKG_OK) printf(" %s", pkg_user_name(user)); printf("\n"); } break; case INFO_GROUPS: /* Installed pkgs only */ if (pkg_type(pkg) != PKG_REMOTE && pkg_list_count(pkg, PKG_GROUPS) > 0) { if (print_tag) printf("%-15s: ", "Groups"); if (pkg_groups(pkg, &group) == EPKG_OK) printf("%s", pkg_group_name(group)); while (pkg_groups(pkg, &group) == EPKG_OK) printf(" %s", pkg_group_name(group)); printf("\n"); } break; case INFO_ARCH: if (print_tag) printf("%-15s: ", "Architecture"); printf("%s\n", arch); break; case INFO_REPOURL: if (pkg_type(pkg) == PKG_REMOTE && repourl != NULL && repourl[0] != '\0') { if (print_tag) printf("%-15s: ", "Pkg URL"); if (repourl[strlen(repourl) -1] == '/') printf("%s%s\n", repourl, repopath); else printf("%s/%s\n", repourl, repopath); } else if (!print_tag) printf("\n"); break; case INFO_LOCKED: if (print_tag) printf("%-15s: ", "Locked"); printf("%s\n", locked ? "yes" : "no"); break; } } }
void print_query(struct pkg *pkg, char *qstr, char multiline) { struct sbuf *output = sbuf_new_auto(); struct pkg_dep *dep = NULL; struct pkg_option *option = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; char *buf; struct pkg_kv *kv; switch (multiline) { case 'd': while (pkg_deps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'r': while (pkg_rdeps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'C': buf = NULL; while (pkg_categories(pkg, &buf) == EPKG_OK) { format_str(pkg, output, qstr, buf); printf("%s\n", sbuf_data(output)); } break; case 'O': while (pkg_options(pkg, &option) == EPKG_OK) { format_str(pkg, output, qstr, option); printf("%s\n", sbuf_data(output)); } break; case 'F': while (pkg_files(pkg, &file) == EPKG_OK) { format_str(pkg, output, qstr, file); printf("%s\n", sbuf_data(output)); } break; case 'D': while (pkg_dirs(pkg, &dir) == EPKG_OK) { format_str(pkg, output, qstr, dir); printf("%s\n", sbuf_data(output)); } break; case 'L': buf = NULL; while (pkg_licenses(pkg, &buf) == EPKG_OK) { format_str(pkg, output, qstr, buf); printf("%s\n", sbuf_data(output)); } break; case 'U': buf = NULL; while (pkg_users(pkg, &buf) == EPKG_OK) { format_str(pkg, output, qstr, buf); printf("%s\n", sbuf_data(output)); } break; case 'G': buf = NULL; while (pkg_groups(pkg, &buf) == EPKG_OK) { format_str(pkg, output, qstr, buf); printf("%s\n", sbuf_data(output)); } break; case 'B': buf = NULL; while (pkg_shlibs_required(pkg, &buf) == EPKG_OK) { format_str(pkg, output, qstr, buf); printf("%s\n", sbuf_data(output)); } break; case 'b': buf = NULL; while (pkg_shlibs_provided(pkg, &buf) == EPKG_OK) { format_str(pkg, output, qstr, buf); printf("%s\n", sbuf_data(output)); } break; case 'A': pkg_get(pkg, PKG_ANNOTATIONS, &kv); while (kv != NULL) { format_str(pkg, output, qstr, kv); printf("%s\n", sbuf_data(output)); kv = kv->next; } break; default: format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); break; } sbuf_delete(output); }
void test_manifest(void) { struct pkg *p = NULL; struct pkg_dep *dep = NULL; struct pkg_conflict *conflict = NULL; struct pkg_option *option = NULL; struct pkg_file *file = NULL; int i; ATF_REQUIRE_EQ(EPKG_OK, pkg_new(&p, PKG_FILE)); ATF_REQUIRE(p != NULL); ATF_REQUIRE_EQ(EPKG_OK, pkg_parse_manifest(p, manifest)); #if 0 ATF_REQUIRE(strcmp(pkg_get(p, PKG_NAME), "foobar") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_VERSION), "0.3") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_ORIGIN), "foo/bar") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_COMMENT), "A dummy manifest") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_ARCH), "amd64") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_VERSION), "800500") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_WWW), "http://www.foobar.com") == 0); ATF_REQUIRE(strcmp(pkg_get(p, PKG_MAINTAINER), "*****@*****.**") == 0); #endif i = 0; while (pkg_deps(p, &dep) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_dep_name(dep), "depfoo") == 0); ATF_REQUIRE(strcmp(pkg_dep_origin(dep), "dep/foo") == 0); ATF_REQUIRE(strcmp(pkg_dep_version(dep), "1.2") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_dep_name(dep), "depbar") == 0); ATF_REQUIRE(strcmp(pkg_dep_origin(dep), "dep/bar") == 0); ATF_REQUIRE(strcmp(pkg_dep_version(dep), "3.4") == 0); } i++; } ATF_REQUIRE(i == 2); i = 0; #if 0 while (pkg_conflicts(p, &conflict) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_conflict_glob(conflict), "foo-*") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_conflict_glob(conflict), "bar-*") == 0); } i++; } ATF_REQUIRE(i == 2); #endif i = 0; while (pkg_options(p, &option) == EPKG_OK) { if (i == 0) { ATF_REQUIRE(strcmp(pkg_option_opt(option), "foo") == 0); ATF_REQUIRE(strcmp(pkg_option_value(option), "true") == 0); } else if (i == 1) { ATF_REQUIRE(strcmp(pkg_option_opt(option), "bar") == 0); ATF_REQUIRE(strcmp(pkg_option_value(option), "false") == 0); } i++; } ATF_REQUIRE(i == 2); ATF_REQUIRE(pkg_files(p, &file) == EPKG_OK); ATF_REQUIRE(strcmp(pkg_file_path(file), "/usr/local/bin/foo") == 0); #if 0 ATF_REQUIRE(strcmp(pkg_file_sha256(file), "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b") == 0); #endif pkg_free(p); /* p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest1) == EPKG_FATAL); pkg_free(p); p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest2) == EPKG_FATAL); pkg_free(p); p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest3) == EPKG_FATAL); pkg_free(p); p = NULL; ATF_REQUIRE(pkg_new(&p, PKG_FILE) == EPKG_OK); ATF_REQUIRE(pkg_parse_manifest(p, wrong_manifest4) == EPKG_FATAL); pkg_free(p); */ }
int pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *data) { FTS *fts = NULL; FTSENT *ent = NULL; struct stat st; struct pkg *pkg = NULL; struct pkg_dep *dep = NULL; struct pkg_category *category = NULL; struct pkg_license *license = NULL; struct pkg_option *option = NULL; struct sbuf *manifest = sbuf_new_auto(); char *ext = NULL; sqlite3 *sqlite = NULL; sqlite3_stmt *stmt_deps = NULL; sqlite3_stmt *stmt_pkg = NULL; sqlite3_stmt *stmt_lic1 = NULL; sqlite3_stmt *stmt_lic2 = NULL; sqlite3_stmt *stmt_cat1 = NULL; sqlite3_stmt *stmt_cat2 = NULL; sqlite3_stmt *stmt_opts = NULL; int64_t package_id; char *errmsg = NULL; int retcode = EPKG_OK; char *pkg_path; char cksum[SHA256_DIGEST_LENGTH * 2 +1]; char *repopath[2]; char repodb[MAXPATHLEN + 1]; const char initsql[] = "" "CREATE TABLE packages (" "id INTEGER PRIMARY KEY," "origin TEXT UNIQUE," "name TEXT NOT NULL," "version TEXT NOT NULL," "comment TEXT NOT NULL," "desc TEXT NOT NULL," "arch TEXT NOT NULL," "osversion TEXT NOT NULL," "maintainer TEXT NOT NULL," "www TEXT," "prefix TEXT NOT NULL," "pkgsize INTEGER NOT NULL," "flatsize INTEGER NOT NULL," "licenselogic INTEGER NOT NULL," "cksum TEXT NOT NULL," "path TEXT NOT NULL," /* relative path to the package in the repository */ "pkg_format_version INTEGER" ");" "CREATE TABLE deps (" "origin TEXT," "name TEXT," "version TEXT," "package_id INTEGER REFERENCES packages(id)," "UNIQUE(package_id, origin)" ");" "CREATE TABLE categories (" "id INTEGER PRIMARY KEY, " "name TEXT NOT NULL UNIQUE " ");" "CREATE TABLE pkg_categories (" "package_id INTEGER REFERENCES packages(id), " "category_id INTEGER REFERENCES categories(id), " "UNIQUE(package_id, category_id)" ");" "CREATE TABLE licenses (" "id INTEGER PRIMARY KEY," "name TEXT NOT NULL UNIQUE" ");" "CREATE TABLE pkg_licenses (" "package_id INTEGER REFERENCES packages(id), " "license_id INTEGER REFERENCES licenses(id), " "UNIQUE(package_id, license_id)" ");" "CREATE TABLE options (" "package_id INTEGER REFERENCES packages(id), " "option TEXT," "value TEXT," "UNIQUE (package_id, option)" ");" "PRAGMA user_version=2;" ; const char pkgsql[] = "" "INSERT INTO packages (" "origin, name, version, comment, desc, arch, osversion, " "maintainer, www, prefix, pkgsize, flatsize, licenselogic, cksum, path" ")" "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15);"; const char depssql[] = "" "INSERT INTO deps (origin, name, version, package_id) " "VALUES (?1, ?2, ?3, ?4);"; const char licsql[] = "INSERT OR IGNORE INTO licenses(name) VALUES(?1);"; const char addlicsql[] = "INSERT OR ROLLBACK INTO pkg_licenses(package_id, license_id) " "VALUES (?1, (SELECT id FROM licenses WHERE name = ?2));"; const char catsql[] = "INSERT OR IGNORE INTO categories(name) VALUES(?1);"; const char addcatsql[] = "INSERT OR ROLLBACK INTO pkg_categories(package_id, category_id) " "VALUES (?1, (SELECT id FROM categories WHERE name = ?2));"; const char addoption[] = "INSERT OR ROLLBACK INTO options (option, value, package_id) " "VALUES (?1, ?2, ?3);"; if (!is_dir(path)) { pkg_emit_error("%s is not a directory", path); return EPKG_FATAL; } repopath[0] = path; repopath[1] = NULL; snprintf(repodb, sizeof(repodb), "%s/repo.sqlite", path); if (stat(repodb, &st) != -1) if (unlink(repodb) != 0) { pkg_emit_errno("unlink", path); return EPKG_FATAL; } sqlite3_initialize(); if (sqlite3_open(repodb, &sqlite) != SQLITE_OK) { sqlite3_shutdown(); return (EPKG_FATAL); } if ((retcode = sql_exec(sqlite, initsql)) != EPKG_OK) goto cleanup; if ((retcode = sql_exec(sqlite, "BEGIN TRANSACTION;")) != EPKG_OK) goto cleanup; if (sqlite3_prepare_v2(sqlite, pkgsql, -1, &stmt_pkg, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_prepare_v2(sqlite, depssql, -1, &stmt_deps, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_prepare_v2(sqlite, licsql, -1, &stmt_lic1, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_prepare_v2(sqlite, addlicsql, -1, &stmt_lic2, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_prepare_v2(sqlite, addoption, -1, &stmt_opts, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if ((fts = fts_open(repopath, FTS_PHYSICAL, NULL)) == NULL) { pkg_emit_errno("fts_open", path); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_prepare_v2(sqlite, catsql, -1, &stmt_cat1, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_prepare_v2(sqlite, addcatsql, -1, &stmt_cat2, NULL) != SQLITE_OK) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } while ((ent = fts_read(fts)) != NULL) { cksum[0] = '\0'; /* skip everything that is not a file */ if (ent->fts_info != FTS_F) continue; ext = strrchr(ent->fts_name, '.'); if (ext == NULL) continue; if (strcmp(ext, ".tgz") != 0 && strcmp(ext, ".tbz") != 0 && strcmp(ext, ".txz") != 0 && strcmp(ext, ".tar") != 0) continue; pkg_path = ent->fts_path; pkg_path += strlen(path); while (pkg_path[0] == '/' ) pkg_path++; if (pkg_open(&pkg, ent->fts_accpath, manifest) != EPKG_OK) { retcode = EPKG_WARN; continue; } if (progress != NULL) progress(pkg, data); sqlite3_bind_text(stmt_pkg, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 2, pkg_get(pkg, PKG_NAME), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 3, pkg_get(pkg, PKG_VERSION), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 4, pkg_get(pkg, PKG_COMMENT), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 5, pkg_get(pkg, PKG_DESC), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 6, pkg_get(pkg, PKG_ARCH), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 7, pkg_get(pkg, PKG_OSVERSION), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 8, pkg_get(pkg, PKG_MAINTAINER), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 9, pkg_get(pkg, PKG_WWW), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 10, pkg_get(pkg, PKG_PREFIX), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_pkg, 11, ent->fts_statp->st_size); sqlite3_bind_int64(stmt_pkg, 12, pkg_flatsize(pkg)); sha256_file(ent->fts_accpath, cksum); sqlite3_bind_int64(stmt_pkg, 13, pkg_licenselogic(pkg)); sqlite3_bind_text(stmt_pkg, 14, cksum, -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 15, pkg_path, -1, SQLITE_STATIC); if (sqlite3_step(stmt_pkg) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } sqlite3_reset(stmt_pkg); package_id = sqlite3_last_insert_rowid(sqlite); dep = NULL; while (pkg_deps(pkg, &dep) == EPKG_OK) { sqlite3_bind_text(stmt_deps, 1, pkg_dep_origin(dep), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_deps, 2, pkg_dep_name(dep), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_deps, 3, pkg_dep_version(dep), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_deps, 4, package_id); if (sqlite3_step(stmt_deps) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } sqlite3_reset(stmt_deps); } category = NULL; while (pkg_categories(pkg, &category) == EPKG_OK) { sqlite3_bind_text(stmt_cat1, 1, pkg_category_name(category), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_cat2, 1, package_id); sqlite3_bind_text(stmt_cat2, 2, pkg_category_name(category), -1, SQLITE_STATIC); if (sqlite3_step(stmt_cat1) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_step(stmt_cat2) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } sqlite3_reset(stmt_cat1); sqlite3_reset(stmt_cat2); } license = NULL; while (pkg_licenses(pkg, &license) == EPKG_OK) { sqlite3_bind_text(stmt_lic1, 1, pkg_license_name(license), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_lic2, 1, package_id); sqlite3_bind_text(stmt_lic2, 2, pkg_license_name(license), -1, SQLITE_STATIC); if (sqlite3_step(stmt_lic1) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } if (sqlite3_step(stmt_lic2) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } sqlite3_reset(stmt_lic1); sqlite3_reset(stmt_lic2); } option = NULL; while (pkg_options(pkg, &option) == EPKG_OK) { sqlite3_bind_text(stmt_opts, 1, pkg_option_opt(option), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_opts, 2, pkg_option_value(option), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_opts, 3, package_id); if (sqlite3_step(stmt_opts) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } sqlite3_reset(stmt_opts); } } if (sqlite3_exec(sqlite, "COMMIT;", NULL, NULL, &errmsg) != SQLITE_OK) { pkg_emit_error("sqlite: %s", errmsg); retcode = EPKG_FATAL; } cleanup: if (fts != NULL) fts_close(fts); if (pkg != NULL) pkg_free(pkg); if (stmt_pkg != NULL) sqlite3_finalize(stmt_pkg); if (stmt_deps != NULL) sqlite3_finalize(stmt_deps); if (stmt_cat1 != NULL) sqlite3_finalize(stmt_cat1); if (stmt_cat2 != NULL) sqlite3_finalize(stmt_cat2); if (stmt_lic1 != NULL) sqlite3_finalize(stmt_lic1); if (stmt_lic2 != NULL) sqlite3_finalize(stmt_lic2); if (stmt_opts != NULL) sqlite3_finalize(stmt_opts); if (sqlite != NULL) sqlite3_close(sqlite); if (errmsg != NULL) sqlite3_free(errmsg); sbuf_delete(manifest); sqlite3_shutdown(); return (retcode); }
int pkg_create_repo(char *path, bool force, void (progress)(struct pkg *pkg, void *data), void *data) { FTS *fts = NULL; struct thd_data thd_data; int num_workers; size_t len; pthread_t *tids = NULL; struct pkg_dep *dep = NULL; struct pkg_category *category = NULL; struct pkg_license *license = NULL; struct pkg_option *option = NULL; struct pkg_shlib *shlib = NULL; sqlite3 *sqlite = NULL; int64_t package_id; char *errmsg = NULL; int retcode = EPKG_OK; int ret; char *repopath[2]; char repodb[MAXPATHLEN + 1]; char repopack[MAXPATHLEN + 1]; struct archive *a = NULL; struct archive_entry *ae = NULL; if (!is_dir(path)) { pkg_emit_error("%s is not a directory", path); return (EPKG_FATAL); } repopath[0] = path; repopath[1] = NULL; len = sizeof(num_workers); if (sysctlbyname("hw.ncpu", &num_workers, &len, NULL, 0) == -1) num_workers = 6; if ((fts = fts_open(repopath, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) { pkg_emit_errno("fts_open", path); retcode = EPKG_FATAL; goto cleanup; } snprintf(repodb, sizeof(repodb), "%s/repo.sqlite", path); snprintf(repopack, sizeof(repopack), "%s/repo.txz", path); if (access(repopack, F_OK) == 0) { a = archive_read_new(); archive_read_support_compression_all(a); archive_read_support_format_tar(a); ret = archive_read_open_filename(a, repopack, 4096); if (ret != ARCHIVE_OK) { /* if we can't unpack it it won't be useful for us */ unlink(repopack); } else { while (archive_read_next_header(a, &ae) == ARCHIVE_OK) { if (!strcmp(archive_entry_pathname(ae), "repo.sqlite")) { archive_entry_set_pathname(ae, repodb); archive_read_extract(a, ae, EXTRACT_ARCHIVE_FLAGS); break; } } } if (a != NULL) archive_read_finish(a); } if ((retcode = initialize_repo(repodb, force, &sqlite)) != EPKG_OK) goto cleanup; if ((retcode = initialize_prepared_statements(sqlite)) != EPKG_OK) goto cleanup; thd_data.root_path = path; thd_data.max_results = num_workers; thd_data.num_results = 0; thd_data.stop = false; thd_data.fts = fts; pthread_mutex_init(&thd_data.fts_m, NULL); STAILQ_INIT(&thd_data.results); thd_data.thd_finished = 0; pthread_mutex_init(&thd_data.results_m, NULL); pthread_cond_init(&thd_data.has_result, NULL); pthread_cond_init(&thd_data.has_room, NULL); /* Launch workers */ tids = calloc(num_workers, sizeof(pthread_t)); for (int i = 0; i < num_workers; i++) { pthread_create(&tids[i], NULL, (void *)&read_pkg_file, &thd_data); } for (;;) { struct pkg_result *r; const char *name, *version, *origin, *comment, *desc; const char *arch, *maintainer, *www, *prefix; int64_t flatsize; lic_t licenselogic; pthread_mutex_lock(&thd_data.results_m); while ((r = STAILQ_FIRST(&thd_data.results)) == NULL) { if (thd_data.thd_finished == num_workers) { break; } pthread_cond_wait(&thd_data.has_result, &thd_data.results_m); } if (r != NULL) { STAILQ_REMOVE_HEAD(&thd_data.results, next); thd_data.num_results--; pthread_cond_signal(&thd_data.has_room); } pthread_mutex_unlock(&thd_data.results_m); if (r == NULL) { break; } if (r->retcode != EPKG_OK) { continue; } /* do not add if package if already in repodb (possibly at a different pkg_path) */ if (run_prepared_statement(EXISTS, r->cksum) != SQLITE_ROW) { ERROR_SQLITE(sqlite); goto cleanup; } if (sqlite3_column_int(STMT(EXISTS), 0) > 0) { continue; } if (progress != NULL) progress(r->pkg, data); pkg_get(r->pkg, PKG_ORIGIN, &origin, PKG_NAME, &name, PKG_VERSION, &version, PKG_COMMENT, &comment, PKG_DESC, &desc, PKG_ARCH, &arch, PKG_MAINTAINER, &maintainer, PKG_WWW, &www, PKG_PREFIX, &prefix, PKG_FLATSIZE, &flatsize, PKG_LICENSE_LOGIC, &licenselogic); try_again: if ((ret = run_prepared_statement(PKG, origin, name, version, comment, desc, arch, maintainer, www, prefix, r->size, flatsize, (int64_t)licenselogic, r->cksum, r->path)) != SQLITE_DONE) { if (ret == SQLITE_CONSTRAINT) { switch(maybe_delete_conflicting(origin, version, r->path)) { case EPKG_FATAL: /* sqlite error */ ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; break; case EPKG_END: /* repo already has newer */ continue; break; default: /* conflict cleared, try again */ goto try_again; break; } } else { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } } package_id = sqlite3_last_insert_rowid(sqlite); dep = NULL; while (pkg_deps(r->pkg, &dep) == EPKG_OK) { if (run_prepared_statement(DEPS, pkg_dep_origin(dep), pkg_dep_name(dep), pkg_dep_version(dep), package_id) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } } category = NULL; while (pkg_categories(r->pkg, &category) == EPKG_OK) { const char *cat_name = pkg_category_name(category); ret = run_prepared_statement(CAT1, cat_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(CAT2, package_id, cat_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } } license = NULL; while (pkg_licenses(r->pkg, &license) == EPKG_OK) { const char *lic_name = pkg_license_name(license); ret = run_prepared_statement(LIC1, lic_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(LIC2, package_id, lic_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } } option = NULL; while (pkg_options(r->pkg, &option) == EPKG_OK) { if (run_prepared_statement(OPTS, pkg_option_opt(option), pkg_option_value(option), package_id) != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } } shlib = NULL; while (pkg_shlibs(r->pkg, &shlib) == EPKG_OK) { const char *shlib_name = pkg_shlib_name(shlib); ret = run_prepared_statement(SHLIB1, shlib_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(SHLIB2, package_id, shlib_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); retcode = EPKG_FATAL; goto cleanup; } } pkg_free(r->pkg); free(r); } if (pkgdb_transaction_commit(sqlite, NULL) != SQLITE_OK) retcode = EPKG_FATAL; cleanup: if (tids != NULL) { // Cancel running threads if (retcode != EPKG_OK) { pthread_mutex_lock(&thd_data.fts_m); thd_data.stop = true; pthread_mutex_unlock(&thd_data.fts_m); } // Join on threads to release thread IDs for (int i = 0; i < num_workers; i++) { pthread_join(tids[i], NULL); } free(tids); } if (fts != NULL) fts_close(fts); finalize_prepared_statements(); if (sqlite != NULL) sqlite3_close(sqlite); if (errmsg != NULL) sqlite3_free(errmsg); sqlite3_shutdown(); return (retcode); }
int pkg_old_emit_content(struct pkg *pkg, char **dest) { struct sbuf *content = sbuf_new_auto(); struct pkg_dep *dep = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct pkg_option *option = NULL; char option_type = 0; pkg_sbuf_printf(content, "@comment PKG_FORMAT_REVISION:1.1\n" "@name %n-%v\n" "@comment ORIGIN:%o\n" "@cwd %p\n" /* hack because we can recreate the prefix split or origin */ "@cwd /\n", pkg, pkg, pkg, pkg); while (pkg_deps(pkg, &dep) == EPKG_OK) { sbuf_printf(content, "@pkgdep %s-%s\n" "@comment DEPORIGIN:%s\n", pkg_dep_name(dep), pkg_dep_version(dep), pkg_dep_origin(dep)); } while (pkg_files(pkg, &file) == EPKG_OK) { sbuf_printf(content, "%s\n" "@comment MD5:%s\n", file->path + 1, file->sum); } while (pkg_dirs(pkg, &dir) == EPKG_OK) { sbuf_printf(content, "@unexec /sbin/rmdir \"%s\" 2>/dev/null\n", dir->path); } sbuf_printf(content, "@comment OPTIONS:"); while (pkg_options(pkg, &option) == EPKG_OK) { /* Add space for previous option, if not the first. */ if (option_type != 0) sbuf_cat(content, " "); if (strcmp(pkg_option_value(option), "on") == 0) option_type = '+'; else option_type = '-'; sbuf_printf(content, "%c%s", option_type, pkg_option_opt(option)); } sbuf_printf(content, "\n"); sbuf_finish(content); *dest = strdup(sbuf_get(content)); sbuf_delete(content); return (EPKG_OK); }
int print_info(struct pkg * const pkg, unsigned int opt) { struct pkg_dep *dep = NULL; struct pkg_file *file = NULL; struct pkg_category *cat = NULL; struct pkg_license *lic = NULL; struct pkg_option *option = NULL; bool multirepos_enabled = false; char *m; char size[7]; const char *name, *version, *prefix, *origin, *reponame, *repourl; const char *maintainer, *www, *comment, *desc; int64_t flatsize, newflatsize, newpkgsize; lic_t licenselogic; pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multirepos_enabled); pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version, PKG_PREFIX, &prefix, PKG_ORIGIN, &origin, PKG_REPONAME, &reponame, PKG_REPOURL, &repourl, PKG_MAINTAINER, &maintainer, PKG_WWW, &www, PKG_COMMENT, &comment, PKG_DESC, &desc, PKG_FLATSIZE, &flatsize, PKG_NEW_FLATSIZE, &newflatsize, PKG_NEW_PKGSIZE, &newpkgsize, PKG_LICENSE_LOGIC, &licenselogic); if (opt & INFO_RAW) { pkg_emit_manifest(pkg, &m); printf("%s\n", m); free(m); } else if (opt & INFO_FULL) { printf("%-15s: %s\n", "Name", name); printf("%-15s: %s\n", "Version", version); printf("%-15s: %s\n", "Origin", origin); printf("%-15s: %s\n", "Prefix", prefix); if ((pkg_type(pkg) == PKG_REMOTE) && multirepos_enabled) printf("%-15s: %s [%s]\n", "Repository", reponame, repourl); if (!pkg_list_is_empty(pkg, PKG_CATEGORIES)) { printf("%-15s:", "Categories"); while (pkg_categories(pkg, &cat) == EPKG_OK) printf(" %s", pkg_category_name(cat)); printf("\n"); } if (!pkg_list_is_empty(pkg, PKG_LICENSES)) { printf("%-15s:", "Licenses"); while (pkg_licenses(pkg, &lic) == EPKG_OK) { printf(" %s", pkg_license_name(lic)); if (licenselogic != 1) printf(" %c", licenselogic); else printf(" "); } printf("\b \n"); } printf("%-15s: %s\n", "Maintainer", maintainer); printf("%-15s: %s\n", "WWW", www); printf("%-15s: %s\n", "Comment", comment); if (!pkg_list_is_empty(pkg, PKG_OPTIONS)) { printf("%-15s: \n", "Options"); while (pkg_options(pkg, &option) == EPKG_OK) printf("\t%s: %s\n", pkg_option_opt(option), pkg_option_value(option)); } if (pkg_type(pkg) == PKG_INSTALLED || pkg_type(pkg) == PKG_FILE) { humanize_number(size, sizeof(size), flatsize, "B", HN_AUTOSCALE, 0); printf("%-15s: %s\n", "Flat size", size); } else { humanize_number(size, sizeof(size), newflatsize, "B", HN_AUTOSCALE, 0); printf("%-15s: %s\n", "Flat size", size); humanize_number(size, sizeof(size), newpkgsize, "B", HN_AUTOSCALE, 0); printf("%-15s: %s\n", "Pkg size", size); } printf("%-15s: \n%s\n", "Description", desc); printf("\n"); } else if (opt & INFO_PRINT_DEP) { if (!(opt & INFO_QUIET)) printf("%s-%s depends on:\n", name, version); while (pkg_deps(pkg, &dep) == EPKG_OK) { printf("%s-%s\n", pkg_dep_get(dep, PKG_DEP_NAME), pkg_dep_get(dep, PKG_DEP_VERSION)); } if (!(opt & INFO_QUIET)) printf("\n"); } else if (opt & INFO_PRINT_RDEP) { if (!(opt & INFO_QUIET)) printf("%s-%s is required by:\n", name, version); while (pkg_rdeps(pkg, &dep) == EPKG_OK) { printf("%s-%s\n", pkg_dep_get(dep, PKG_DEP_NAME), pkg_dep_get(dep, PKG_DEP_VERSION)); } if (!(opt & INFO_QUIET)) printf("\n"); } else if (opt & INFO_LIST_FILES) { if (!(opt & INFO_QUIET)) printf("%s-%s owns the following files:\n", name, version); while (pkg_files(pkg, &file) == EPKG_OK) { printf("%s\n", pkg_file_get(file, PKG_FILE_PATH)); } if (!(opt & INFO_QUIET)) printf("\n"); } else if (opt & INFO_SIZE) { if (pkg_type(pkg) == PKG_INSTALLED) { humanize_number(size, sizeof(size), flatsize, "B", HN_AUTOSCALE, 0); printf("%s-%s size is: %s\n", name, version, size); } else { humanize_number(size, sizeof(size), newflatsize, "B", HN_AUTOSCALE, 0); printf("%s-%s flat size is: %s\n", name, version, size); humanize_number(size, sizeof(size), newpkgsize, "B", HN_AUTOSCALE, 0); printf("%s-%s package size is: %s\n", name, version, size); } } else if (opt & INFO_ORIGIN) { if (opt & INFO_QUIET) printf("%s\n", origin); else printf("%s-%s: %s\n", name, version, origin); } else if (opt & INFO_PREFIX) { if (opt & INFO_QUIET) printf("%s\n", prefix); else printf("%s-%s: %s\n", name, version, prefix); } else { if (opt & INFO_QUIET) printf("%s-%s\n", name, version); else { if ((pkg_type(pkg) == PKG_REMOTE) && multirepos_enabled) printf("%s-%s [repository: %s]: %s\n", name, version, reponame, comment); else printf("%s-%s: %s\n", name, version, comment); } } return (0); }
void print_query(struct pkg *pkg, char *qstr, char multiline) { struct sbuf *output = sbuf_new_auto(); struct pkg_dep *dep = NULL; struct pkg_category *cat = NULL; struct pkg_option *option = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct pkg_license *lic = NULL; struct pkg_user *user = NULL; struct pkg_group *group = NULL; struct pkg_shlib *shlib = NULL; struct pkg_note *note = NULL; switch (multiline) { case 'd': while (pkg_deps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'r': while (pkg_rdeps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'C': while (pkg_categories(pkg, &cat) == EPKG_OK) { format_str(pkg, output, qstr, cat); printf("%s\n", sbuf_data(output)); } break; case 'O': while (pkg_options(pkg, &option) == EPKG_OK) { format_str(pkg, output, qstr, option); printf("%s\n", sbuf_data(output)); } break; case 'F': while (pkg_files(pkg, &file) == EPKG_OK) { format_str(pkg, output, qstr, file); printf("%s\n", sbuf_data(output)); } break; case 'D': while (pkg_dirs(pkg, &dir) == EPKG_OK) { format_str(pkg, output, qstr, dir); printf("%s\n", sbuf_data(output)); } break; case 'L': while (pkg_licenses(pkg, &lic) == EPKG_OK) { format_str(pkg, output, qstr, lic); printf("%s\n", sbuf_data(output)); } break; case 'U': while (pkg_users(pkg, &user) == EPKG_OK) { format_str(pkg, output, qstr, user); printf("%s\n", sbuf_data(output)); } break; case 'G': while (pkg_groups(pkg, &group) == EPKG_OK) { format_str(pkg, output, qstr, group); printf("%s\n", sbuf_data(output)); } break; case 'B': while (pkg_shlibs_required(pkg, &shlib) == EPKG_OK) { format_str(pkg, output, qstr, shlib); printf("%s\n", sbuf_data(output)); } break; case 'b': while (pkg_shlibs_provided(pkg, &shlib) == EPKG_OK) { format_str(pkg, output, qstr, shlib); printf("%s\n", sbuf_data(output)); } break; case 'A': while (pkg_annotations(pkg, ¬e) == EPKG_OK) { format_str(pkg, output, qstr, note); printf("%s\n", sbuf_data(output)); } break; default: format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); break; } sbuf_delete(output); }
int pkgdb_repo_add_package(struct pkg *pkg, const char *pkg_path, sqlite3 *sqlite, const char *manifest_digest, bool forced) { const char *name, *version, *origin, *comment, *desc; const char *arch, *maintainer, *www, *prefix, *sum, *rpath; int64_t flatsize, pkgsize; lic_t licenselogic; int ret; struct pkg_dep *dep = NULL; struct pkg_category *category = NULL; struct pkg_license *license = NULL; struct pkg_option *option = NULL; struct pkg_shlib *shlib = NULL; struct pkg_note *note = NULL; int64_t package_id; pkg_get(pkg, PKG_ORIGIN, &origin, PKG_NAME, &name, PKG_VERSION, &version, PKG_COMMENT, &comment, PKG_DESC, &desc, PKG_ARCH, &arch, PKG_MAINTAINER, &maintainer, PKG_WWW, &www, PKG_PREFIX, &prefix, PKG_FLATSIZE, &flatsize, PKG_LICENSE_LOGIC, &licenselogic, PKG_CKSUM, &sum, PKG_PKGSIZE, &pkgsize, PKG_REPOPATH, &rpath); try_again: if ((ret = run_prepared_statement(PKG, origin, name, version, comment, desc, arch, maintainer, www, prefix, pkgsize, flatsize, (int64_t)licenselogic, sum, rpath, manifest_digest)) != SQLITE_DONE) { if (ret == SQLITE_CONSTRAINT) { switch(maybe_delete_conflicting(origin, version, pkg_path, forced)) { case EPKG_FATAL: /* sqlite error */ ERROR_SQLITE(sqlite); return (EPKG_FATAL); break; case EPKG_END: /* repo already has newer */ return (EPKG_END); break; default: /* conflict cleared, try again */ goto try_again; break; } } else { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } package_id = sqlite3_last_insert_rowid(sqlite); dep = NULL; while (pkg_deps(pkg, &dep) == EPKG_OK) { if (run_prepared_statement(DEPS, pkg_dep_origin(dep), pkg_dep_name(dep), pkg_dep_version(dep), package_id) != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } category = NULL; while (pkg_categories(pkg, &category) == EPKG_OK) { const char *cat_name = pkg_category_name(category); ret = run_prepared_statement(CAT1, cat_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(CAT2, package_id, cat_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } license = NULL; while (pkg_licenses(pkg, &license) == EPKG_OK) { const char *lic_name = pkg_license_name(license); ret = run_prepared_statement(LIC1, lic_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(LIC2, package_id, lic_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } option = NULL; while (pkg_options(pkg, &option) == EPKG_OK) { ret = run_prepared_statement(OPT1, pkg_option_opt(option)); if (ret == SQLITE_DONE) ret = run_prepared_statement(OPT2, pkg_option_opt(option), pkg_option_value(option), package_id); if(ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } shlib = NULL; while (pkg_shlibs_required(pkg, &shlib) == EPKG_OK) { const char *shlib_name = pkg_shlib_name(shlib); ret = run_prepared_statement(SHLIB1, shlib_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(SHLIB_REQD, package_id, shlib_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } shlib = NULL; while (pkg_shlibs_provided(pkg, &shlib) == EPKG_OK) { const char *shlib_name = pkg_shlib_name(shlib); ret = run_prepared_statement(SHLIB1, shlib_name); if (ret == SQLITE_DONE) ret = run_prepared_statement(SHLIB_PROV, package_id, shlib_name); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } note = NULL; while (pkg_annotations(pkg, ¬e) == EPKG_OK) { const char *note_tag = pkg_annotation_tag(note); const char *note_val = pkg_annotation_value(note); ret = run_prepared_statement(ANNOTATE1, note_tag); if (ret == SQLITE_DONE) ret = run_prepared_statement(ANNOTATE1, note_val); if (ret == SQLITE_DONE) ret = run_prepared_statement(ANNOTATE2, package_id, note_tag, note_val); if (ret != SQLITE_DONE) { ERROR_SQLITE(sqlite); return (EPKG_FATAL); } } return (EPKG_OK); }
int pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg) { struct pkg **deps; struct pkg_file **files; struct pkg_conflict **conflicts; struct pkg_script **scripts; struct pkg_option **options; sqlite3 *s; sqlite3_stmt *stmt_pkg = NULL; sqlite3_stmt *stmt_sel_pkg = NULL; sqlite3_stmt *stmt_dep = NULL; sqlite3_stmt *stmt_conflict = NULL; sqlite3_stmt *stmt_file = NULL; sqlite3_stmt *stmt_script = NULL; sqlite3_stmt *stmt_option = NULL; sqlite3_stmt *stmt_dirs = NULL; int i; int ret; int retcode = EPKG_OK; const char *path; int64_t package_id; char *errmsg; const char sql_begin[] = "BEGIN TRANSACTION;"; const char sql_pkg[] = "" "INSERT INTO pkg_mtree( " "origin, name, version, comment, desc, mtree, message, arch, " "osversion, maintainer, www, prefix, flatsize) " "VALUES( ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13);"; const char sql_sel_pkg[] = "" "SELECT id FROM packages " "WHERE origin = ?1;"; const char sql_dep[] = "" "INSERT INTO deps (origin, name, version, package_id) " "VALUES (?1, ?2, ?3, ?4);"; const char sql_conflict[] = "" "INSERT INTO conflicts (name, package_id) " "VALUES (?1, ?2);"; const char sql_file[] = "" "INSERT INTO files (path, sha256, package_id) " "VALUES (?1, ?2, ?3);"; const char sql_script[] = "" "INSERT INTO scripts (script, type, package_id) " "VALUES (?1, ?2, ?3);"; const char sql_option[] = "" "INSERT INTO options (option, value, package_id) " "VALUES (?1, ?2, ?3);"; const char sql_dir[] = "" "INSERT INTO pkg_dirs(origin, path) " "VALUES (?1, ?2);"; if (pkgdb_has_flag(db, PKGDB_FLAG_IN_FLIGHT)) { pkg_error_set(EPKG_FATAL, "tried to register a package with an in-flight SQL command"); return (EPKG_FATAL); } s = db->sqlite; if (sqlite3_exec(s, sql_begin, NULL, NULL, &errmsg) != SQLITE_OK) { pkg_error_set(EPKG_FATAL, "sqlite: %s", errmsg); sqlite3_free(errmsg); return (EPKG_FATAL); } PKGDB_SET_FLAG(db, PKGDB_FLAG_IN_FLIGHT); /* * Insert package record */ if (sqlite3_prepare_v2(s, sql_pkg, -1, &stmt_pkg, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_bind_text(stmt_pkg, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 2, pkg_get(pkg, PKG_NAME), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 3, pkg_get(pkg, PKG_VERSION), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 4, pkg_get(pkg, PKG_COMMENT), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 5, pkg_get(pkg, PKG_DESC), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 6, pkg_get(pkg, PKG_MTREE), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 7, pkg_get(pkg, PKG_MESSAGE), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 8, pkg_get(pkg, PKG_ARCH), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 9, pkg_get(pkg, PKG_OSVERSION), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 10, pkg_get(pkg, PKG_MAINTAINER), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 11, pkg_get(pkg, PKG_WWW), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_pkg, 12, pkg_get(pkg, PKG_PREFIX), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_pkg, 13, pkg_flatsize(pkg)); if ((ret = sqlite3_step(stmt_pkg)) != SQLITE_DONE) { if ( ret == SQLITE_CONSTRAINT) retcode = pkg_error_set(EPKG_FATAL, "constraint violation on " "pkg with %s", pkg_get(pkg, PKG_ORIGIN)); else retcode = ERROR_SQLITE(s); goto cleanup; } /* * Get the generated package_id */ if (sqlite3_prepare_v2(s, sql_sel_pkg, -1, &stmt_sel_pkg, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_bind_text(stmt_sel_pkg, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC); ret = sqlite3_step(stmt_sel_pkg); if (ret == SQLITE_ROW) { package_id = sqlite3_column_int64(stmt_sel_pkg, 0); ret = SQLITE_DONE; } else { retcode = ERROR_SQLITE(s); goto cleanup; } /* * Insert dependencies list */ if (sqlite3_prepare_v2(s, sql_dep, -1, &stmt_dep, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } deps = pkg_deps(pkg); for (i = 0; deps[i] != NULL; i++) { sqlite3_bind_text(stmt_dep, 1, pkg_get(deps[i], PKG_ORIGIN), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_dep, 2, pkg_get(deps[i], PKG_NAME), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_dep, 3, pkg_get(deps[i], PKG_VERSION), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_dep, 4, package_id); if ((ret = sqlite3_step(stmt_dep)) != SQLITE_DONE) { if ( ret == SQLITE_CONSTRAINT) retcode = pkg_error_set(EPKG_FATAL, "constraint violation on " "deps with %s", pkg_get(deps[i], PKG_ORIGIN)); else retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_reset(stmt_dep); } /* * Insert conflicts list */ if (sqlite3_prepare_v2(s, sql_conflict, -1, &stmt_conflict, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } conflicts = pkg_conflicts(pkg); for (i = 0; conflicts[i] != NULL; i++) { sqlite3_bind_text(stmt_conflict, 1, pkg_conflict_glob(conflicts[i]), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_conflict, 2, package_id); if ((ret = sqlite3_step(stmt_conflict)) != SQLITE_DONE) { if ( ret == SQLITE_CONSTRAINT) retcode = pkg_error_set(EPKG_FATAL, "constraint violation on " "conflicts with %s", pkg_conflict_glob(conflicts[i])); else retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_reset(stmt_conflict); } /* * Insert file * and dirs */ if (sqlite3_prepare_v2(s, sql_file, -1, &stmt_file, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } if (sqlite3_prepare_v2(s, sql_dir, -1, &stmt_dirs, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } files = pkg_files(pkg); for (i = 0; files[i] != NULL; i++) { path = pkg_file_path(files[i]); if (path[strlen(path) - 1 ] != '/') { sqlite3_bind_text(stmt_file, 1, pkg_file_path(files[i]), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_file, 2, pkg_file_sha256(files[i]), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_file, 3, package_id); if ((ret = sqlite3_step(stmt_file)) != SQLITE_DONE) { if ( ret == SQLITE_CONSTRAINT) retcode = pkg_error_set(EPKG_FATAL, "constraint violation on " "path with %s", pkg_file_path(files[i])); else retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_reset(stmt_file); } else { sqlite3_bind_text(stmt_dirs, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_dirs, 2, pkg_file_path(files[i]), -1, SQLITE_STATIC); if ((ret = sqlite3_step(stmt_dirs)) != SQLITE_DONE) { if ( ret == SQLITE_CONSTRAINT) retcode = pkg_error_set(EPKG_FATAL, "constraint violation on " "dirs with %s", pkg_file_path(files[i])); else retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_reset(stmt_dirs); } } /* * Insert scripts */ if (sqlite3_prepare_v2(s, sql_script, -1, &stmt_script, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } scripts = pkg_scripts(pkg); for (i = 0; scripts[i] != NULL; i++) { sqlite3_bind_text(stmt_script, 1, pkg_script_data(scripts[i]), -1, SQLITE_STATIC); sqlite3_bind_int(stmt_script, 2, pkg_script_type(scripts[i])); sqlite3_bind_int64(stmt_script, 3, package_id); if (sqlite3_step(stmt_script) != SQLITE_DONE) { if ( ret == SQLITE_CONSTRAINT) retcode = pkg_error_set(EPKG_FATAL, "constraint violation on " "scripts with %s", pkg_script_data(scripts[i])); else retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_reset(stmt_script); } /* * Insert options */ options = pkg_options(pkg); if (sqlite3_prepare_v2(s, sql_option, -1, &stmt_option, NULL) != SQLITE_OK) { retcode = ERROR_SQLITE(s); goto cleanup; } for (i = 0; options[i] != NULL; i++) { sqlite3_bind_text(stmt_option, 1, pkg_option_opt(options[i]), -1, SQLITE_STATIC); sqlite3_bind_text(stmt_option, 2, pkg_option_value(options[i]), -1, SQLITE_STATIC); sqlite3_bind_int64(stmt_option, 3, package_id); if (sqlite3_step(stmt_option) != SQLITE_DONE) { retcode = ERROR_SQLITE(s); goto cleanup; } sqlite3_reset(stmt_option); } cleanup: if (stmt_pkg != NULL) sqlite3_finalize(stmt_pkg); if (stmt_sel_pkg != NULL) sqlite3_finalize(stmt_sel_pkg); if (stmt_dep != NULL) sqlite3_finalize(stmt_dep); if (stmt_conflict != NULL) sqlite3_finalize(stmt_conflict); if (stmt_file != NULL) sqlite3_finalize(stmt_file); if (stmt_script != NULL) sqlite3_finalize(stmt_script); if (stmt_option != NULL) sqlite3_finalize(stmt_option); if (stmt_dirs != NULL) sqlite3_finalize(stmt_dirs); return (retcode); }
void print_query(struct pkg *pkg, char *qstr, char multiline) { struct sbuf *output = sbuf_new_auto(); struct pkg_dep *dep = NULL; struct pkg_option *option = NULL; struct pkg_file *file = NULL; struct pkg_kv *kv; switch (multiline) { case 'd': while (pkg_deps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'r': while (pkg_rdeps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'C': pkg_printf("%C%{%Cn\n%|%}", pkg); break; case 'O': while (pkg_options(pkg, &option) == EPKG_OK) { format_str(pkg, output, qstr, option); printf("%s\n", sbuf_data(output)); } break; case 'F': while (pkg_files(pkg, &file) == EPKG_OK) { format_str(pkg, output, qstr, file); printf("%s\n", sbuf_data(output)); } break; case 'D': pkg_printf("%D", pkg); break; case 'L': pkg_printf("%L%{%Ln\n%|%}", pkg); break; case 'U': pkg_printf("%U", pkg); break; case 'G': pkg_printf("%G", pkg); break; case 'B': pkg_printf("%B", pkg); break; case 'b': pkg_printf("%b", pkg); break; case 'A': pkg_get(pkg, PKG_ANNOTATIONS, &kv); while (kv != NULL) { format_str(pkg, output, qstr, kv); printf("%s\n", sbuf_data(output)); kv = kv->next; } break; default: format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); break; } sbuf_delete(output); }
void print_query(struct pkg *pkg, char *qstr, char multiline) { struct sbuf *output = sbuf_new_auto(); struct pkg_dep *dep = NULL; struct pkg_option *option = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct pkg_user *user = NULL; struct pkg_group *group = NULL; struct pkg_shlib *shlib = NULL; const pkg_object *o, *list; pkg_iter it; switch (multiline) { case 'd': while (pkg_deps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'r': while (pkg_rdeps(pkg, &dep) == EPKG_OK) { format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); } break; case 'C': it = NULL; pkg_get(pkg, PKG_CATEGORIES, &list); while ((o = pkg_object_iterate(list, &it))) { format_str(pkg, output, qstr, o); printf("%s\n", sbuf_data(output)); } break; case 'O': while (pkg_options(pkg, &option) == EPKG_OK) { format_str(pkg, output, qstr, option); printf("%s\n", sbuf_data(output)); } break; case 'F': while (pkg_files(pkg, &file) == EPKG_OK) { format_str(pkg, output, qstr, file); printf("%s\n", sbuf_data(output)); } break; case 'D': while (pkg_dirs(pkg, &dir) == EPKG_OK) { format_str(pkg, output, qstr, dir); printf("%s\n", sbuf_data(output)); } break; case 'L': it = NULL; pkg_get(pkg, PKG_LICENSES, &list); while ((o = pkg_object_iterate(list, &it))) { format_str(pkg, output, qstr, o); printf("%s\n", sbuf_data(output)); } break; case 'U': while (pkg_users(pkg, &user) == EPKG_OK) { format_str(pkg, output, qstr, user); printf("%s\n", sbuf_data(output)); } break; case 'G': while (pkg_groups(pkg, &group) == EPKG_OK) { format_str(pkg, output, qstr, group); printf("%s\n", sbuf_data(output)); } break; case 'B': while (pkg_shlibs_required(pkg, &shlib) == EPKG_OK) { format_str(pkg, output, qstr, shlib); printf("%s\n", sbuf_data(output)); } break; case 'b': while (pkg_shlibs_provided(pkg, &shlib) == EPKG_OK) { format_str(pkg, output, qstr, shlib); printf("%s\n", sbuf_data(output)); } break; case 'A': it = NULL; pkg_get(pkg, PKG_ANNOTATIONS, &list); while ((o = pkg_object_iterate(list, &it))) { format_str(pkg, output, qstr, o); printf("%s\n", sbuf_data(output)); } break; default: format_str(pkg, output, qstr, dep); printf("%s\n", sbuf_data(output)); break; } sbuf_delete(output); }