int pkg_delete_dirs(__unused struct pkgdb *db, struct pkg *pkg, bool force) { struct pkg_dir *dir = NULL; ucl_object_t *obj; char fpath[MAXPATHLEN]; while (pkg_dirs(pkg, &dir) == EPKG_OK) { if (dir->keep == 1) continue; obj = pkg_annotation_lookup(pkg, "relocated"); snprintf(fpath, sizeof(fpath), "%s%s", obj ? pkg_object_string(obj) : "" , pkg_dir_path(dir) ); if (pkg_dir_try(dir)) { if (rmdir(fpath) == -1 && errno != ENOTEMPTY && errno != EBUSY && !force) pkg_emit_errno("rmdir", fpath); } else { if (rmdir(fpath) == -1 && !force) pkg_emit_errno("rmdir", fpath); } } return (EPKG_OK); }
int pkg_delete_dirs(struct pkgdb *db, struct pkg *pkg, int force) { struct pkg_dir *dir = NULL; int64_t nbpackage; while (pkg_dirs(pkg, &dir) == EPKG_OK) { nbpackage = 0; if (pkgdb_is_dir_used(db, pkg_dir_path(dir), &nbpackage) != EPKG_OK) return (EPKG_FATAL); if (nbpackage > 1) continue; if (pkg_dir_try(dir)) { if (rmdir(pkg_dir_path(dir)) == -1 && errno != ENOTEMPTY && force != 1) pkg_emit_errno("rmdir", pkg_dir_path(dir)); } else { if (rmdir(pkg_dir_path(dir)) == -1 && force != 1) pkg_emit_errno("rmdir", pkg_dir_path(dir)); } } return (EPKG_OK); }
int pkg_delete_dirs(__unused struct pkgdb *db, struct pkg *pkg) { struct pkg_dir *dir = NULL; while (pkg_dirs(pkg, &dir) == EPKG_OK) { if (dir->keep == 1) continue; pkg_delete_dir(pkg, dir); } return (EPKG_OK); }
int pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir, const char *md_dir, char *plist) { struct pkg *pkg = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct packing *pkg_archive = NULL; int ret = EPKG_FATAL; pkg_debug(1, "Creating package from stage directory: '%s'", rootdir); if ((ret = pkg_new(&pkg, PKG_FILE)) != EPKG_OK) goto cleanup; if ((ret = pkg_load_metadata(pkg, NULL, md_dir, plist, rootdir, false)) != EPKG_OK) goto cleanup; /* Create the archive */ pkg_archive = pkg_create_archive(outdir, pkg, format, 0); if (pkg_archive == NULL) { ret = EPKG_FATAL; /* XXX do better */ goto cleanup; } /* XXX: autoplist support doesn't work right with meta-ports */ if (0 && pkg_files(pkg, &file) != EPKG_OK && pkg_dirs(pkg, &dir) != EPKG_OK) { /* Now traverse the file directories, adding to the archive */ packing_append_tree(pkg_archive, md_dir, NULL); packing_append_tree(pkg_archive, rootdir, "/"); ret = EPKG_OK; } else { ret = pkg_create_from_dir(pkg, rootdir, pkg_archive); } cleanup: free(pkg); packing_finish(pkg_archive); return (ret); }
int pkg_delete_dirs(__unused struct pkgdb *db, struct pkg *pkg, bool force) { struct pkg_dir *dir = NULL; while (pkg_dirs(pkg, &dir) == EPKG_OK) { if (dir->keep == 1) continue; if (pkg_dir_try(dir)) { if (rmdir(pkg_dir_path(dir)) == -1 && errno != ENOTEMPTY && errno != EBUSY && !force) pkg_emit_errno("rmdir", pkg_dir_path(dir)); } else { if (rmdir(pkg_dir_path(dir)) == -1 && !force) pkg_emit_errno("rmdir", pkg_dir_path(dir)); } } return (EPKG_OK); }
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; } } }
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_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); }
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); }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; int ret; struct stat st; int64_t flatsize = 0; int64_t nfiles; const char *relocation; hardlinks_t *hardlinks; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } relocation = pkg_kv_get(&pkg->annotations, "relocated"); if (relocation == NULL) relocation = ""; if (pkg_rootdir != NULL) relocation = pkg_rootdir; /* * Get / compute size / checksum if not provided in the manifest */ nfiles = kh_count(pkg->filehash); counter_init("file sizes/checksums", nfiles); hardlinks = kh_init_hardlinks(); while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); if (lstat(fpath, &st) == -1) { pkg_emit_error("file '%s' is missing", fpath); return (EPKG_FATAL); } if (file->size == 0) file->size = (int64_t)st.st_size; if (st.st_nlink == 1 || !check_for_hardlink(hardlinks, &st)) { flatsize += file->size; } file->sum = pkg_checksum_generate_file(fpath, PKG_HASH_TYPE_SHA256_HEX); if (file->sum == NULL) return (EPKG_FATAL); counter_count(); } kh_destroy_hardlinks(hardlinks); counter_end(); pkg->flatsize = flatsize; if (pkg->type == PKG_OLD_FILE) { pkg_emit_error("Cannot create an old format package"); return (EPKG_FATAL); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_analyse_files(NULL, pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } counter_init("packing files", nfiles); while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); ret = packing_append_file_attr(pkg_archive, fpath, file->path, file->uname, file->gname, file->perm, file->fflags); if (developer_mode && ret != EPKG_OK) return (ret); counter_count(); } counter_end(); nfiles = kh_count(pkg->dirhash); counter_init("packing directories", nfiles); while (pkg_dirs(pkg, &dir) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, dir->path); ret = packing_append_file_attr(pkg_archive, fpath, dir->path, dir->uname, dir->gname, dir->perm, dir->fflags); if (developer_mode && ret != EPKG_OK) return (ret); counter_count(); } counter_end(); return (EPKG_OK); }
int pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir, const char *md_dir, char *plist) { struct pkg *pkg = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct packing *pkg_archive = NULL; char *manifest = NULL; char arch[BUFSIZ]; int ret = ENOMEM; int i, mfd; regex_t preg; regmatch_t pmatch[2]; size_t size; struct pkg_manifest_key *keys = NULL; pkg_debug(1, "Creating package from stage directory: '%s'", rootdir); if ((mfd = open(md_dir, O_DIRECTORY)) == -1) { pkg_emit_errno("open", md_dir); goto cleanup; } if(pkg_new(&pkg, PKG_FILE) != EPKG_OK) { ret = EPKG_FATAL; goto cleanup; } pkg_manifest_keys_new(&keys); /* Load the manifest from the metadata directory */ if ((ret = pkg_parse_manifest_fileat(mfd, pkg, "+MANIFEST", keys)) != EPKG_OK) { ret = EPKG_FATAL; goto cleanup; } /* if no descriptions provided then try to get it from a file */ if (pkg->desc == NULL) pkg_load_from_file(mfd, pkg, PKG_DESC, "+DESC"); /* if no message try to get it from a file */ if (pkg->message == NULL) { /* Try ucl version first */ if (pkg_load_message_from_file(mfd, pkg, "+DISPLAY.ucl", true) != EPKG_OK) { pkg_load_message_from_file(mfd, pkg, "+DISPLAY", false); } } /* if no arch autodetermine it */ if (pkg->abi == NULL) { pkg_get_myarch(arch, BUFSIZ); pkg->abi = strdup(arch); } for (i = 0; scripts[i] != NULL; i++) { if (faccessat(mfd, scripts[i], F_OK, 0) == 0) pkg_addscript_fileat(mfd, pkg, scripts[i]); } if (plist != NULL && ports_parse_plist(pkg, plist, rootdir) != EPKG_OK) { ret = EPKG_FATAL; goto cleanup; } if (pkg->www == NULL) { if (pkg->desc == NULL) { pkg_emit_error("No www or desc defined in manifest"); ret = EPKG_FATAL; goto cleanup; } regcomp(&preg, "^WWW:[[:space:]]*(.*)$", REG_EXTENDED|REG_ICASE|REG_NEWLINE); if (regexec(&preg, pkg->desc, 2, pmatch, 0) == 0) { size = pmatch[1].rm_eo - pmatch[1].rm_so; pkg->www = strndup(&pkg->desc[pmatch[1].rm_so], size); } else { pkg->www = strdup("UNKNOWN"); } regfree(&preg); } /* Create the archive */ pkg_archive = pkg_create_archive(outdir, pkg, format, 0); if (pkg_archive == NULL) { ret = EPKG_FATAL; /* XXX do better */ goto cleanup; } /* XXX: autoplist support doesn't work right with meta-ports */ if (0 && pkg_files(pkg, &file) != EPKG_OK && pkg_dirs(pkg, &dir) != EPKG_OK) { /* Now traverse the file directories, adding to the archive */ packing_append_tree(pkg_archive, md_dir, NULL); packing_append_tree(pkg_archive, rootdir, "/"); ret = EPKG_OK; } else { ret = pkg_create_from_dir(pkg, rootdir, pkg_archive); } cleanup: if (mfd != -1) close(mfd); free(pkg); free(manifest); pkg_manifest_keys_free(keys); packing_finish(pkg_archive); return (ret); }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN + 1]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; char *m; int ret; const char *mtree; bool developer; struct stat st; char sha256[SHA256_DIGEST_LENGTH * 2 + 1]; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } /* * if the checksum is not provided in the manifest recompute it */ while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); const char *pkg_sum = pkg_file_cksum(file); if (root != NULL) snprintf(fpath, sizeof(fpath), "%s%s", root, pkg_path); else strlcpy(fpath, pkg_path, sizeof(fpath)); if ((pkg_sum == NULL || pkg_sum[0] == '\0') && lstat(fpath, &st) == 0 && !S_ISLNK(st.st_mode)) { if (sha256_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); strlcpy(file->sum, sha256, sizeof(file->sum)); } } /* * Register shared libraries used by the package if SHLIBS * enabled in conf. Deletes shlib info if not. */ pkg_register_shlibs(pkg); pkg_emit_manifest(pkg, &m); packing_append_buffer(pkg_archive, m, "+MANIFEST", strlen(m)); free(m); pkg_get(pkg, PKG_MTREE, &mtree); if (mtree != NULL) packing_append_buffer(pkg_archive, mtree, "+MTREE_DIRS", strlen(mtree)); while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); if (root != NULL) snprintf(fpath, sizeof(fpath), "%s%s", root, pkg_path); else strlcpy(fpath, pkg_path, sizeof(fpath)); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, file->uname, file->gname, file->perm); pkg_config_bool(PKG_CONFIG_DEVELOPER_MODE, &developer); if (developer && ret != EPKG_OK) return (ret); } while (pkg_dirs(pkg, &dir) == EPKG_OK) { const char *pkg_path = pkg_dir_path(dir); if (root != NULL) snprintf(fpath, sizeof(fpath), "%s%s", root, pkg_path); else strlcpy(fpath, pkg_dir_path(dir), sizeof(fpath)); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, dir->uname, dir->gname, dir->perm); pkg_config_bool(PKG_CONFIG_DEVELOPER_MODE, &developer); if (developer && ret != EPKG_OK) return (ret); } return (EPKG_OK); }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; char *m; int ret; bool developer; struct stat st; char sha256[SHA256_DIGEST_LENGTH * 2 + 1]; int64_t flatsize = 0; const char *relocation; struct hardlinks *hardlinks = NULL; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } relocation = pkg_kv_get(&pkg->annotations, "relocated"); if (relocation == NULL) relocation = ""; /* * Get / compute size / checksum if not provided in the manifest */ while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); if (lstat(fpath, &st) == -1) { pkg_emit_error("file '%s' is missing", fpath); return (EPKG_FATAL); } if (file->size == 0) file->size = (int64_t)st.st_size; if (st.st_nlink == 1 || !check_for_hardlink(&hardlinks, &st)) { flatsize += file->size; } if (S_ISLNK(st.st_mode)) { if (file->sum[0] == '\0') { if (pkg_symlink_cksum(fpath, root, sha256) == EPKG_OK) strlcpy(file->sum, sha256, sizeof(file->sum)); else return (EPKG_FATAL); } } else { if (file->sum[0] == '\0') { if (pkg->type == PKG_OLD_FILE) { if (md5_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } else { if (sha256_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } strlcpy(file->sum, sha256, sizeof(file->sum)); } } } pkg->flatsize = flatsize; HASH_FREE(hardlinks, free); if (pkg->type == PKG_OLD_FILE) { char oldcomment[BUFSIZ]; pkg_old_emit_content(pkg, &m); packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m)); free(m); packing_append_buffer(pkg_archive, pkg->desc, "+DESC", strlen(pkg->desc)); packing_append_buffer(pkg_archive, pkg->message, "+DISPLAY", strlen(pkg->message)); pkg_snprintf(oldcomment, sizeof(oldcomment), "%c\n", pkg); packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment)); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_analyse_files(NULL, pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } while (pkg_files(pkg, &file) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, file->path); ret = packing_append_file_attr(pkg_archive, fpath, file->path, file->uname, file->gname, file->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } while (pkg_dirs(pkg, &dir) == EPKG_OK) { snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", relocation, dir->path); ret = packing_append_file_attr(pkg_archive, fpath, dir->path, dir->uname, dir->gname, dir->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } return (EPKG_OK); }
int pkg_create_staged(const char *outdir, pkg_formats format, const char *rootdir, const char *md_dir, char *plist, bool old) { struct pkg *pkg = NULL; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; struct packing *pkg_archive = NULL; char *manifest = NULL; char path[MAXPATHLEN]; char arch[BUFSIZ]; int ret = ENOMEM; char *buf; int i; regex_t preg; regmatch_t pmatch[2]; size_t size; char *www = NULL; struct pkg_manifest_key *keys = NULL; pkg_debug(1, "Creating package from stage directory: '%s'", rootdir); /* Load the manifest from the metadata directory */ if (snprintf(path, sizeof(path), "%s/+MANIFEST", md_dir) == -1) goto cleanup; if(pkg_new(&pkg, old ? PKG_OLD_FILE : PKG_FILE) != EPKG_OK) { ret = EPKG_FATAL; goto cleanup; } pkg_manifest_keys_new(&keys); if ((ret = pkg_parse_manifest_file(pkg, path, keys)) != EPKG_OK) { ret = EPKG_FATAL; goto cleanup; } /* if no descriptions provided then try to get it from a file */ pkg_get(pkg, PKG_DESC, &buf); if (buf == NULL) { if (snprintf(path, sizeof(path), "%s/+DESC", md_dir) == -1) goto cleanup; if (access(path, F_OK) == 0) { pkg_debug(1, "Taking description from: '%s'", path); pkg_set_from_file(pkg, PKG_DESC, path, false); } } /* if no message try to get it from a file */ pkg_get(pkg, PKG_MESSAGE, &buf); if (buf == NULL) { ret = snprintf(path, sizeof(path), "%s/+DISPLAY", md_dir); if (ret == -1) goto cleanup; if (access(path, F_OK) == 0) { pkg_debug(1, "Taking message from: '%s'", path); pkg_set_from_file(pkg, PKG_MESSAGE, path, false); } } /* if no arch autodetermine it */ pkg_get(pkg, PKG_ARCH, &buf); if (buf == NULL) { pkg_get_myarch(arch, BUFSIZ); pkg_set(pkg, PKG_ARCH, arch); } /* if no mtree try to get it from a file */ pkg_get(pkg, PKG_MTREE, &buf); if (buf == NULL) { ret = snprintf(path, sizeof(path), "%s/+MTREE_DIRS", md_dir); if (ret == -1) goto cleanup; if (access(path, F_OK) == 0) { pkg_debug(1, "Taking mtree definition from: '%s'", path); pkg_set_from_file(pkg, PKG_MTREE, path, false); } } for (i = 0; scripts[i] != NULL; i++) { snprintf(path, sizeof(path), "%s/%s", md_dir, scripts[i]); if (access(path, F_OK) == 0) pkg_addscript_file(pkg, path); } if (plist != NULL && ports_parse_plist(pkg, plist, rootdir) != EPKG_OK) { ret = EPKG_FATAL; goto cleanup; } /* if www is not given then try to determine it from description */ if (www != NULL) { pkg_set(pkg, PKG_WWW, www); free(www); } pkg_get(pkg, PKG_WWW, &www); if (www == NULL) { pkg_get(pkg, PKG_DESC, &buf); if (buf == NULL) { pkg_emit_error("No www or desc defined in manifest"); ret = EPKG_FATAL; goto cleanup; } regcomp(&preg, "^WWW:[[:space:]]*(.*)$", REG_EXTENDED|REG_ICASE|REG_NEWLINE); if (regexec(&preg, buf, 2, pmatch, 0) == 0) { size = pmatch[1].rm_eo - pmatch[1].rm_so; www = strndup(&buf[pmatch[1].rm_so], size); pkg_set(pkg, PKG_WWW, www); free(www); } else { pkg_set(pkg, PKG_WWW, "UNKNOWN"); } regfree(&preg); } /* Create the archive */ pkg_archive = pkg_create_archive(outdir, pkg, format, 0); if (pkg_archive == NULL) { ret = EPKG_FATAL; /* XXX do better */ goto cleanup; } /* XXX: autoplist support doesn't work right with meta-ports */ if (0 && pkg_files(pkg, &file) != EPKG_OK && pkg_dirs(pkg, &dir) != EPKG_OK) { /* Now traverse the file directories, adding to the archive */ packing_append_tree(pkg_archive, md_dir, NULL); packing_append_tree(pkg_archive, rootdir, "/"); } else { pkg_create_from_dir(pkg, rootdir, pkg_archive); } ret = EPKG_OK; cleanup: free(pkg); free(manifest); pkg_manifest_keys_free(keys); if (ret == EPKG_OK) ret = packing_finish(pkg_archive); return ret; }
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); }
static int pkg_create_from_dir(struct pkg *pkg, const char *root, struct packing *pkg_archive) { char fpath[MAXPATHLEN]; struct pkg_file *file = NULL; struct pkg_dir *dir = NULL; char *m; int ret; const char *mtree; bool developer; struct stat st; char sha256[SHA256_DIGEST_LENGTH * 2 + 1]; int64_t flatsize = 0; ucl_object_t *obj; if (pkg_is_valid(pkg) != EPKG_OK) { pkg_emit_error("the package is not valid"); return (EPKG_FATAL); } obj = pkg_annotation_lookup(pkg, "relocated"); /* * Get / compute size / checksum if not provided in the manifest */ while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); const char *pkg_sum = pkg_file_cksum(file); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); if (lstat(fpath, &st) != 0 || S_ISLNK(st.st_mode)) continue; if (file->size == 0) file->size = (int64_t)st.st_size; flatsize += file->size; if (pkg_sum == NULL || pkg_sum[0] == '\0') { if (pkg->type == PKG_OLD_FILE) { if (md5_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } else { if (sha256_file(fpath, sha256) != EPKG_OK) return (EPKG_FATAL); } strlcpy(file->sum, sha256, sizeof(file->sum)); } } pkg_set(pkg, PKG_FLATSIZE, flatsize); if (pkg->type == PKG_OLD_FILE) { const char *desc, *display, *comment; char oldcomment[BUFSIZ]; pkg_old_emit_content(pkg, &m); packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m)); free(m); pkg_get(pkg, PKG_DESC, &desc, PKG_MESSAGE, &display, PKG_COMMENT, &comment); packing_append_buffer(pkg_archive, desc, "+DESC", strlen(desc)); packing_append_buffer(pkg_archive, display, "+DISPLAY", strlen(display)); snprintf(oldcomment, sizeof(oldcomment), "%s\n", comment); packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment)); } else { /* * Register shared libraries used by the package if * SHLIBS enabled in conf. Deletes shlib info if not. */ struct sbuf *b = sbuf_new_auto(); pkg_register_shlibs(pkg, root); pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL); packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b)); sbuf_clear(b); pkg_emit_manifest_sbuf(pkg, b, 0, NULL); sbuf_finish(b); packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b)); sbuf_delete(b); } pkg_get(pkg, PKG_MTREE, &mtree); if (mtree != NULL) packing_append_buffer(pkg_archive, mtree, "+MTREE_DIRS", strlen(mtree)); while (pkg_files(pkg, &file) == EPKG_OK) { const char *pkg_path = pkg_file_path(file); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, file->uname, file->gname, file->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } while (pkg_dirs(pkg, &dir) == EPKG_OK) { const char *pkg_path = pkg_dir_path(dir); snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "", obj ? pkg_object_string(obj) : "", pkg_path); ret = packing_append_file_attr(pkg_archive, fpath, pkg_path, dir->uname, dir->gname, dir->perm); developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE")); if (developer && ret != EPKG_OK) return (ret); } return (EPKG_OK); }
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); }