int pkg_perform(char **pkgs) { char **matched, **rb, **rbtmp; int errcode, i, j; int err_cnt = 0; struct reqr_by_entry *rb_entry; struct reqr_by_head *rb_list; if (MatchType != MATCH_EXACT) { matched = matchinstalled(MatchType, pkgs, &errcode); if (errcode != 0) return 1; /* Not reached */ /* * Copy matched[] into pkgs[], because we'll need to use * matchinstalled() later on. */ if (matched != NULL) { pkgs = NULL; for (i = 0; matched[i] != NULL; i++) { pkgs = realloc(pkgs, sizeof(*pkgs) * (i + 2)); pkgs[i] = strdup(matched[i]); } pkgs[i] = NULL; } else switch (MatchType) { case MATCH_GLOB: break; case MATCH_ALL: warnx("no packages installed"); return 0; case MATCH_EREGEX: case MATCH_REGEX: warnx("no packages match pattern(s)"); return 1; default: break; } } err_cnt += sortdeps(pkgs); for (i = 0; pkgs[i]; i++) { if (Recursive == TRUE) { errcode = requiredby(pkgs[i], &rb_list, FALSE, TRUE); if (errcode < 0) { err_cnt++; } else if (errcode > 0) { /* * Copy values from the rb_list queue into argv-like NULL * terminated list because requiredby() uses some static * storage, while pkg_do() below will call this function, * thus blowing our rb_list away. */ rbtmp = rb = alloca((errcode + 1) * sizeof(*rb)); if (rb == NULL) { warnx("%s(): alloca() failed", __func__); err_cnt++; continue; } STAILQ_FOREACH(rb_entry, rb_list, link) { *rbtmp = alloca(strlen(rb_entry->pkgname) + 1); if (*rbtmp == NULL) { warnx("%s(): alloca() failed", __func__); err_cnt++; continue; } strcpy(*rbtmp, rb_entry->pkgname); rbtmp++; } *rbtmp = NULL; err_cnt += sortdeps(rb); for (j = 0; rb[j]; j++) err_cnt += pkg_do(rb[j]); }
int pkg_perform(char **pkgs) { static const char *home; char *pkg = *pkgs; /* Only one arg to create */ char *cp; FILE *pkg_in, *fp; Package plist; int len; const char *suf; /* Preliminary setup */ if (InstalledPkg == NULL) sanity_check(); if (Verbose && !PlistOnly) printf("Creating package %s\n", pkg); /* chop suffix off if already specified, remembering if we want to compress */ len = strlen(pkg); if (len > 4) { if (!strcmp(&pkg[len - 4], ".tbz")) { Zipper = BZIP2; pkg[len - 4] = '\0'; } else if (!strcmp(&pkg[len - 4], ".tgz")) { Zipper = GZIP; pkg[len - 4] = '\0'; } else if (!strcmp(&pkg[len - 4], ".txz")) { Zipper = XZ; pkg[len - 4] = '\0'; } else if (!strcmp(&pkg[len - 4], ".tar")) { Zipper = NONE; pkg[len - 4] = '\0'; } } if (Zipper == BZIP2) { suf = "tbz"; setenv("BZIP2", "--best", 0); } else if (Zipper == GZIP) { suf = "tgz"; setenv("GZIP", "-9", 0); } else if (Zipper == XZ) { suf = "txz"; } else suf = "tar"; if (InstalledPkg != NULL) { char *pkgglob[] = { InstalledPkg, NULL }; char **matched, **pkgs; int i, error; pkgs = pkgglob; if (MatchType != MATCH_EXACT) { matched = matchinstalled(MatchType, pkgs, &error); if (!error && matched != NULL) pkgs = matched; else if (MatchType != MATCH_GLOB) errx(1, "no packages match pattern"); } /* * Is there is only one installed package matching the pattern, * we need to respect the optional pkg-filename parameter. If, * however, the pattern matches several packages, this parameter * makes no sense and is ignored. */ if (pkgs[1] == NULL) { if (pkg == InstalledPkg) pkg = *pkgs; InstalledPkg = *pkgs; if (!Recursive) return (create_from_installed(InstalledPkg, pkg, suf)); return (create_from_installed_recursive(pkg, suf)); } for (i = 0; pkgs[i] != NULL; i++) { InstalledPkg = pkg = pkgs[i]; if (!Recursive) create_from_installed(pkg, pkg, suf); else create_from_installed_recursive(pkg, suf); } return TRUE; } get_dash_string(&Comment); get_dash_string(&Desc); if (!strcmp(Contents, "-")) pkg_in = stdin; else { pkg_in = fopen(Contents, "r"); if (!pkg_in) { cleanup(0); errx(2, "%s: unable to open contents file '%s' for input", __func__, Contents); } } plist.head = plist.tail = NULL; /* Stick the dependencies, if any, at the top */ if (Pkgdeps) { char **deps, *deporigin; int i; int ndeps = 0; if (Verbose && !PlistOnly) printf("Registering depends:"); /* Count number of dependencies */ for (cp = Pkgdeps; cp != NULL && *cp != '\0'; cp = strpbrk(++cp, " \t\n")) { ndeps++; } if (ndeps != 0) { /* Create easy to use NULL-terminated list */ deps = alloca(sizeof(*deps) * ndeps + 1); if (deps == NULL) { errx(2, "%s: alloca() failed", __func__); /* Not reached */ } for (i = 0; Pkgdeps;) { cp = strsep(&Pkgdeps, " \t\n"); if (*cp) { deps[i] = cp; i++; } } ndeps = i; deps[ndeps] = NULL; sortdeps(deps); for (i = 0; i < ndeps; i++) { deporigin = strchr(deps[i], ':'); if (deporigin != NULL) { *deporigin = '\0'; add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin); } add_plist_top(&plist, PLIST_PKGDEP, deps[i]); if (Verbose && !PlistOnly) printf(" %s", deps[i]); } } if (Verbose && !PlistOnly) printf(".\n"); } /* Put the conflicts directly after the dependencies, if any */ if (Conflicts) { if (Verbose && !PlistOnly) printf("Registering conflicts:"); while (Conflicts) { cp = strsep(&Conflicts, " \t\n"); if (*cp) { add_plist(&plist, PLIST_CONFLICTS, cp); if (Verbose && !PlistOnly) printf(" %s", cp); } } if (Verbose && !PlistOnly) printf(".\n"); } /* If a SrcDir override is set, add it now */ if (SrcDir) { if (Verbose && !PlistOnly) printf("Using SrcDir value of %s\n", SrcDir); add_plist(&plist, PLIST_SRC, SrcDir); } /* Slurp in the packing list */ read_plist(&plist, pkg_in); /* Prefix should add an @cwd to the packing list */ if (Prefix) { char resolved_prefix[PATH_MAX]; if (realpath(Prefix, resolved_prefix) == NULL) err(EXIT_FAILURE, "couldn't resolve path for prefix: %s", Prefix); add_plist_top(&plist, PLIST_CWD, resolved_prefix); } /* Add the origin if asked, at the top */ if (Origin) add_plist_top(&plist, PLIST_ORIGIN, Origin); /* * Run down the list and see if we've named it, if not stick in a name * at the top. */ if (find_plist(&plist, PLIST_NAME) == NULL) add_plist_top(&plist, PLIST_NAME, basename(pkg)); if (asprintf(&cp, "PKG_FORMAT_REVISION:%d.%d", PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR) == -1) { errx(2, "%s: asprintf() failed", __func__); } add_plist_top(&plist, PLIST_COMMENT, cp); free(cp); /* * We're just here for to dump out a revised plist for the FreeBSD ports * hack. It's not a real create in progress. */ if (PlistOnly) { check_list(home, &plist); write_plist(&plist, stdout); exit(0); } /* Make a directory to stomp around in */ home = make_playpen(PlayPen, 0); signal(SIGINT, cleanup); signal(SIGHUP, cleanup); /* Make first "real contents" pass over it */ check_list(home, &plist); (void) umask(022); /* * Make sure gen'ed directories, files don't have * group or other write bits. */ /* copy_plist(home, &plist); */ /* mark_plist(&plist); */ /* Now put the release specific items in */ if (!Prefix) { add_plist(&plist, PLIST_CWD, "."); } write_file(COMMENT_FNAME, Comment); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, COMMENT_FNAME); add_cksum(&plist, plist.tail, COMMENT_FNAME); write_file(DESC_FNAME, Desc); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, DESC_FNAME); add_cksum(&plist, plist.tail, DESC_FNAME); if (Install) { copy_file(home, Install, INSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, INSTALL_FNAME); add_cksum(&plist, plist.tail, INSTALL_FNAME); } if (PostInstall) { copy_file(home, PostInstall, POST_INSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME); add_cksum(&plist, plist.tail, POST_INSTALL_FNAME); } if (DeInstall) { copy_file(home, DeInstall, DEINSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME); add_cksum(&plist, plist.tail, DEINSTALL_FNAME); } if (PostDeInstall) { copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME); add_cksum(&plist, plist.tail, POST_DEINSTALL_FNAME); } if (Require) { copy_file(home, Require, REQUIRE_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, REQUIRE_FNAME); add_cksum(&plist, plist.tail, REQUIRE_FNAME); } if (Display) { copy_file(home, Display, DISPLAY_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, DISPLAY_FNAME); add_cksum(&plist, plist.tail, DISPLAY_FNAME); add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME); } if (Mtree) { copy_file(home, Mtree, MTREE_FNAME); add_plist(&plist, PLIST_IGNORE, NULL); add_plist(&plist, PLIST_FILE, MTREE_FNAME); add_cksum(&plist, plist.tail, MTREE_FNAME); add_plist(&plist, PLIST_MTREE, MTREE_FNAME); } /* Finally, write out the packing list */ fp = fopen(CONTENTS_FNAME, "w"); if (!fp) { cleanup(0); errx(2, "%s: can't open file %s for writing", __func__, CONTENTS_FNAME); } write_plist(&plist, fp); if (fclose(fp)) { cleanup(0); errx(2, "%s: error while closing %s", __func__, CONTENTS_FNAME); } /* And stick it into a tar ball */ make_dist(home, pkg, suf, &plist); /* Cleanup */ free(Comment); free(Desc); free_plist(&plist); leave_playpen(); return TRUE; /* Success */ }