static void register_depends(package_t *plist, char *deps, int build_only) { char *cp; if (Verbose && !PlistOnly) { if (build_only) printf("Registering build depends:"); else printf("Registering depends:"); } while (deps) { cp = strsep(&deps, " \t\n"); if (*cp) { char *best_installed; best_installed = find_best_matching_installed_pkg(cp); if (best_installed != NULL) { add_plist(plist, PLIST_BLDDEP, best_installed); if (Verbose && !PlistOnly && build_only) printf(" %s", cp); } else warnx("No matching package installed for %s", cp); free(best_installed); if (!build_only) { add_plist(plist, PLIST_PKGDEP, cp); if (Verbose && !PlistOnly) printf(" %s", cp); } } } if (Verbose && !PlistOnly) printf(".\n"); }
int pkg_build(const char *pkg, const char *full_pkg, const char *suffix, package_t *plist) { char *plist_buf; size_t plist_len; /* Now put the release specific items in */ add_plist(plist, PLIST_CWD, "."); comment_file = make_and_add(plist, COMMENT_FNAME, Comment, 0444); desc_file = make_and_add(plist, DESC_FNAME, Desc, 0444); if (Install) { install_file = load_and_add(plist, Install, INSTALL_FNAME, 0555); } if (DeInstall) { deinstall_file = load_and_add(plist, DeInstall, DEINSTALL_FNAME, 0555); } if (Display) { display_file = load_and_add(plist, Display, DISPLAY_FNAME, 0444); add_plist(plist, PLIST_DISPLAY, DISPLAY_FNAME); } if (BuildVersion) { build_version_file = load_and_add(plist, BuildVersion, BUILD_VERSION_FNAME, 0444); } if (BuildInfo) { build_info_file = load_and_add(plist, BuildInfo, BUILD_INFO_FNAME, 0444); } if (SizePkg) { size_pkg_file = load_and_add(plist, SizePkg, SIZE_PKG_FNAME, 0444); } if (SizeAll) { size_all_file = load_and_add(plist, SizeAll, SIZE_ALL_FNAME, 0444); } if (Preserve) { preserve_file = load_and_add(plist, Preserve, PRESERVE_FNAME, 0444); } if (create_views) views_file = make_and_add(plist, VIEWS_FNAME, xstrdup(""), 0444); /* Finally, write out the packing list */ stringify_plist(plist, &plist_buf, &plist_len, realprefix); contents_file = make_memory_file(CONTENTS_FNAME, plist_buf, plist_len, DefaultOwner, DefaultGroup, 0644); /* And stick it into a tar ball */ make_dist(pkg, suffix, plist); return TRUE; /* Success */ }
static struct memory_file * make_and_add(package_t *plist, const char *target_name, char *content, mode_t perm) { struct memory_file *file; file = make_memory_file(target_name, content, strlen(content), DefaultOwner, DefaultGroup, perm); add_plist(plist, PLIST_IGNORE, NULL); add_plist(plist, PLIST_FILE, target_name); return file; }
static struct memory_file * load_and_add(package_t *plist, const char *input_name, const char *target_name, mode_t perm) { struct memory_file *file; file = load_memory_file(input_name, target_name, DefaultOwner, DefaultGroup, perm); add_plist(plist, PLIST_IGNORE, NULL); add_plist(plist, PLIST_FILE, target_name); return file; }
/* Read a packing list from a file */ void read_plist(Package *pkg, FILE *fp) { char *cp, pline[FILENAME_MAX]; int cmd, major, minor; pkg->fmtver_maj = 1; pkg->fmtver_mnr = 0; pkg->origin = NULL; while (fgets(pline, FILENAME_MAX, fp)) { int len = strlen(pline); while (len && isspace(pline[len - 1])) pline[--len] = '\0'; if (!len) continue; cp = pline; if (pline[0] != CMD_CHAR) { cmd = PLIST_FILE; goto bottom; } cmd = plist_cmd(pline + 1, &cp); if (cmd == FAIL) { warnx("%s: unknown command '%s' (package tools out of date?)", __func__, pline); goto bottom; } if (*cp == '\0') { cp = NULL; if (cmd == PLIST_PKGDEP) { warnx("corrupted record for package %s (pkgdep line without " "argument), ignoring", pkg->name); cmd = FAIL; } goto bottom; } if (cmd == PLIST_COMMENT && sscanf(cp, "PKG_FORMAT_REVISION:%d.%d\n", &major, &minor) == 2) { pkg->fmtver_maj = major; pkg->fmtver_mnr = minor; if (verscmp(pkg, PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR) <= 0) goto bottom; warnx("plist format revision (%d.%d) is higher than supported" "(%d.%d)", pkg->fmtver_maj, pkg->fmtver_mnr, PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR); if (pkg->fmtver_maj > PLIST_FMT_VER_MAJOR) { cleanup(0); exit(2); } } bottom: add_plist(pkg, cmd, cp); } }
/* * Parse a packaging list from a memory buffer. */ void parse_plist(package_t *pkg, const char *buf) { int cmd; char *line, *cp; const char *eol, *next; size_t len; pkg->head = NULL; pkg->tail = NULL; for (; *buf; buf = next) { /* Until add_plist can deal with trailing whitespace. */ if ((eol = strchr(buf, '\n')) != NULL) { next = eol + 1; len = eol - buf; } else { len = strlen(buf); next = buf + len; } while (len && isspace((unsigned char)buf[len - 1])) --len; if (len == 0) continue; line = xmalloc(len + 1); memcpy(line, buf, len); line[len] = '\0'; if (*(cp = line) == CMD_CHAR) { if ((cmd = plist_cmd(line + 1, &cp)) == FAIL) { warnx("Unrecognised PLIST command `%s'", line); continue; } if (*cp == '\0') { free(cp); cp = NULL; } } else { cmd = PLIST_FILE; } add_plist(pkg, cmd, cp); free(cp); } }
/* * Read a packing list from a file */ void append_plist(package_t *pkg, FILE * fp) { char pline[MaxPathSize]; char *cp; int cmd; int len; int free_cp; while (fgets(pline, MaxPathSize, fp) != (char *) NULL) { for (len = strlen(pline); len && isspace((unsigned char) pline[len - 1]);) { pline[--len] = '\0'; } if (len == 0) { continue; } free_cp = 0; if (*(cp = pline) == CMD_CHAR) { if ((cmd = plist_cmd(pline + 1, &cp)) == FAIL) { warnx("Unrecognised PLIST command `%s'", pline); continue; } if (*cp == '\0') { free(cp); cp = NULL; } free_cp = 1; } else { cmd = PLIST_FILE; } add_plist(pkg, cmd, cp); if (free_cp) free(cp); } }
int pkg_perform(const char *pkg) { char *cp; FILE *pkg_in; package_t plist; const char *full_pkg, *suffix; char *allocated_pkg; int retval; /* Break the package name into base and desired suffix (if any) */ if ((cp = strrchr(pkg, '.')) != NULL) { allocated_pkg = xmalloc(cp - pkg + 1); memcpy(allocated_pkg, pkg, cp - pkg); allocated_pkg[cp - pkg] = '\0'; suffix = cp + 1; full_pkg = pkg; pkg = allocated_pkg; } else { allocated_pkg = NULL; full_pkg = pkg; suffix = "tgz"; } /* Preliminary setup */ sanity_check(); if (Verbose && !PlistOnly) printf("Creating package %s\n", pkg); get_dash_string(&Comment); get_dash_string(&Desc); if (IS_STDIN(Contents)) pkg_in = stdin; else { pkg_in = fopen(Contents, "r"); if (!pkg_in) errx(2, "unable to open contents file '%s' for input", Contents); } plist.head = plist.tail = NULL; /* Stick the dependencies, if any, at the top */ if (Pkgdeps) register_depends(&plist, Pkgdeps, 0); /* * Put the build dependencies after the dependencies. * This works due to the evaluation order in pkg_add. */ if (BuildPkgdeps) register_depends(&plist, BuildPkgdeps, 1); /* Put the conflicts directly after the dependencies, if any */ if (Pkgcfl) { if (Verbose && !PlistOnly) printf("Registering conflicts:"); while (Pkgcfl) { cp = strsep(&Pkgcfl, " \t\n"); if (*cp) { add_plist(&plist, PLIST_PKGCFL, cp); if (Verbose && !PlistOnly) printf(" %s", cp); } } if (Verbose && !PlistOnly) printf(".\n"); } /* Slurp in the packing list */ append_plist(&plist, pkg_in); if (pkg_in != stdin) fclose(pkg_in); /* Prefix should override the packing list */ if (Prefix) { delete_plist(&plist, FALSE, PLIST_CWD, NULL); add_plist_top(&plist, PLIST_CWD, Prefix); } /* * 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_of(pkg)); } /* Make first "real contents" pass over it */ check_list(&plist, basename_of(pkg)); /* * 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) { write_plist(&plist, stdout, realprefix); retval = TRUE; } else { #ifdef BOOTSTRAP warnx("Package building is not supported in bootstrap mode"); retval = FALSE; #else retval = pkg_build(pkg, full_pkg, suffix, &plist); #endif } /* Cleanup */ free(Comment); free(Desc); free_plist(&plist); free(allocated_pkg); return retval; }
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 */ }