2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * This is the main body of the create module.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
33 #include <sys/syslimits.h>
37 static void sanity_check(void);
38 static void make_dist(const char *, const char *, const char *, Package *);
39 static int create_from_installed_recursive(const char *, const char *);
40 static int create_from_installed(const char *, const char *, const char *);
43 pkg_perform(char **pkgs)
45 static const char *home;
46 char *pkg = *pkgs; /* Only one arg to create */
53 /* Preliminary setup */
54 if (InstalledPkg == NULL)
56 if (Verbose && !PlistOnly)
57 printf("Creating package %s\n", pkg);
59 /* chop suffix off if already specified, remembering if we want to compress */
62 if (!strcmp(&pkg[len - 4], ".tbz")) {
66 else if (!strcmp(&pkg[len - 4], ".tgz")) {
70 else if (!strcmp(&pkg[len - 4], ".txz")) {
74 else if (!strcmp(&pkg[len - 4], ".tar")) {
79 if (Zipper == BZIP2) {
81 setenv("BZIP2", "--best", 0);
82 } else if (Zipper == GZIP) {
84 setenv("GZIP", "-9", 0);
85 } else if (Zipper == XZ) {
90 if (InstalledPkg != NULL) {
91 char *pkgglob[] = { InstalledPkg, NULL };
92 char **matched, **pkgs;
96 if (MatchType != MATCH_EXACT) {
97 matched = matchinstalled(MatchType, pkgs, &error);
98 if (!error && matched != NULL)
100 else if (MatchType != MATCH_GLOB)
101 errx(1, "no packages match pattern");
104 * Is there is only one installed package matching the pattern,
105 * we need to respect the optional pkg-filename parameter. If,
106 * however, the pattern matches several packages, this parameter
107 * makes no sense and is ignored.
109 if (pkgs[1] == NULL) {
110 if (pkg == InstalledPkg)
112 InstalledPkg = *pkgs;
114 return (create_from_installed(InstalledPkg, pkg, suf));
115 return (create_from_installed_recursive(pkg, suf));
117 for (i = 0; pkgs[i] != NULL; i++) {
118 InstalledPkg = pkg = pkgs[i];
120 create_from_installed(pkg, pkg, suf);
122 create_from_installed_recursive(pkg, suf);
127 get_dash_string(&Comment);
128 get_dash_string(&Desc);
129 if (!strcmp(Contents, "-"))
132 pkg_in = fopen(Contents, "r");
135 errx(2, "%s: unable to open contents file '%s' for input",
139 plist.head = plist.tail = NULL;
141 /* Stick the dependencies, if any, at the top */
143 char **deps, *deporigin;
147 if (Verbose && !PlistOnly)
148 printf("Registering depends:");
150 /* Count number of dependencies */
151 for (cp = Pkgdeps; cp != NULL && *cp != '\0';
152 cp = strpbrk(++cp, " \t\n")) {
157 /* Create easy to use NULL-terminated list */
158 deps = alloca(sizeof(*deps) * ndeps + 1);
160 errx(2, "%s: alloca() failed", __func__);
163 for (i = 0; Pkgdeps;) {
164 cp = strsep(&Pkgdeps, " \t\n");
174 for (i = 0; i < ndeps; i++) {
175 deporigin = strchr(deps[i], ':');
176 if (deporigin != NULL) {
178 add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin);
180 add_plist_top(&plist, PLIST_PKGDEP, deps[i]);
181 if (Verbose && !PlistOnly)
182 printf(" %s", deps[i]);
186 if (Verbose && !PlistOnly)
190 /* Put the conflicts directly after the dependencies, if any */
192 if (Verbose && !PlistOnly)
193 printf("Registering conflicts:");
195 cp = strsep(&Conflicts, " \t\n");
197 add_plist(&plist, PLIST_CONFLICTS, cp);
198 if (Verbose && !PlistOnly)
202 if (Verbose && !PlistOnly)
206 /* If a SrcDir override is set, add it now */
208 if (Verbose && !PlistOnly)
209 printf("Using SrcDir value of %s\n", SrcDir);
210 add_plist(&plist, PLIST_SRC, SrcDir);
213 /* Slurp in the packing list */
214 read_plist(&plist, pkg_in);
216 /* Prefix should add an @cwd to the packing list */
218 add_plist_top(&plist, PLIST_CWD, Prefix);
220 /* Add the origin if asked, at the top */
222 add_plist_top(&plist, PLIST_ORIGIN, Origin);
225 * Run down the list and see if we've named it, if not stick in a name
228 if (find_plist(&plist, PLIST_NAME) == NULL)
229 add_plist_top(&plist, PLIST_NAME, basename(pkg));
231 if (asprintf(&cp, "PKG_FORMAT_REVISION:%d.%d", PLIST_FMT_VER_MAJOR,
232 PLIST_FMT_VER_MINOR) == -1) {
233 errx(2, "%s: asprintf() failed", __func__);
235 add_plist_top(&plist, PLIST_COMMENT, cp);
239 * We're just here for to dump out a revised plist for the FreeBSD ports
240 * hack. It's not a real create in progress.
243 check_list(home, &plist);
244 write_plist(&plist, stdout);
248 /* Make a directory to stomp around in */
249 home = make_playpen(PlayPen, 0);
250 signal(SIGINT, cleanup);
251 signal(SIGHUP, cleanup);
253 /* Make first "real contents" pass over it */
254 check_list(home, &plist);
255 (void) umask(022); /*
256 * Make sure gen'ed directories, files don't have
257 * group or other write bits.
259 /* copy_plist(home, &plist); */
260 /* mark_plist(&plist); */
262 /* Now put the release specific items in */
263 add_plist(&plist, PLIST_CWD, ".");
264 write_file(COMMENT_FNAME, Comment);
265 add_plist(&plist, PLIST_IGNORE, NULL);
266 add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
267 add_cksum(&plist, plist.tail, COMMENT_FNAME);
268 write_file(DESC_FNAME, Desc);
269 add_plist(&plist, PLIST_IGNORE, NULL);
270 add_plist(&plist, PLIST_FILE, DESC_FNAME);
271 add_cksum(&plist, plist.tail, DESC_FNAME);
274 copy_file(home, Install, INSTALL_FNAME);
275 add_plist(&plist, PLIST_IGNORE, NULL);
276 add_plist(&plist, PLIST_FILE, INSTALL_FNAME);
277 add_cksum(&plist, plist.tail, INSTALL_FNAME);
280 copy_file(home, PostInstall, POST_INSTALL_FNAME);
281 add_plist(&plist, PLIST_IGNORE, NULL);
282 add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME);
283 add_cksum(&plist, plist.tail, POST_INSTALL_FNAME);
286 copy_file(home, DeInstall, DEINSTALL_FNAME);
287 add_plist(&plist, PLIST_IGNORE, NULL);
288 add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME);
289 add_cksum(&plist, plist.tail, DEINSTALL_FNAME);
292 copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME);
293 add_plist(&plist, PLIST_IGNORE, NULL);
294 add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME);
295 add_cksum(&plist, plist.tail, POST_DEINSTALL_FNAME);
298 copy_file(home, Require, REQUIRE_FNAME);
299 add_plist(&plist, PLIST_IGNORE, NULL);
300 add_plist(&plist, PLIST_FILE, REQUIRE_FNAME);
301 add_cksum(&plist, plist.tail, REQUIRE_FNAME);
304 copy_file(home, Display, DISPLAY_FNAME);
305 add_plist(&plist, PLIST_IGNORE, NULL);
306 add_plist(&plist, PLIST_FILE, DISPLAY_FNAME);
307 add_cksum(&plist, plist.tail, DISPLAY_FNAME);
308 add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME);
311 copy_file(home, Mtree, MTREE_FNAME);
312 add_plist(&plist, PLIST_IGNORE, NULL);
313 add_plist(&plist, PLIST_FILE, MTREE_FNAME);
314 add_cksum(&plist, plist.tail, MTREE_FNAME);
315 add_plist(&plist, PLIST_MTREE, MTREE_FNAME);
318 /* Finally, write out the packing list */
319 fp = fopen(CONTENTS_FNAME, "w");
322 errx(2, "%s: can't open file %s for writing",
323 __func__, CONTENTS_FNAME);
325 write_plist(&plist, fp);
328 errx(2, "%s: error while closing %s",
329 __func__, CONTENTS_FNAME);
332 /* And stick it into a tar ball */
333 make_dist(home, pkg, suf, &plist);
340 return TRUE; /* Success */
344 make_dist(const char *homedir, const char *pkg, const char *suff, Package *plist)
347 char tball[FILENAME_MAX];
350 const char *args[50]; /* Much more than enough. */
359 args[nargs++] = "tar"; /* argv[0] */
362 snprintf(tball, FILENAME_MAX, "%s.%s", pkg, suff);
364 snprintf(tball, FILENAME_MAX, "%s/%s.%s", homedir, pkg, suff);
367 * If the package tarball exists already, and we are running in `no
368 * clobber' mode, skip this package.
370 if (stat(tball, &sb) == 0 && Regenerate == FALSE) {
372 printf("Skipping package '%s'. It already exists.\n", tball);
376 args[nargs++] = "-c";
377 args[nargs++] = "-f";
378 args[nargs++] = tball;
379 if (strchr(suff, 'z')) { /* Compress/gzip/bzip2? */
380 if (Zipper == BZIP2) {
381 args[nargs++] = "-j";
384 else if (Zipper == XZ) {
385 args[nargs++] = "-J";
389 args[nargs++] = "-z";
396 args[nargs++] = "-h";
398 args[nargs++] = "-X";
399 args[nargs++] = ExcludeFrom;
401 args[nargs++] = "-T"; /* Take filenames from file instead of args. */
402 args[nargs++] = "-"; /* Use stdin for the file. */
406 printf("Creating %star ball in '%s'\n", cname, tball);
408 /* Set up a pipe for passing the filenames, and fork off a tar process. */
409 if (pipe(pipefds) == -1) {
411 errx(2, "%s: cannot create pipe", __func__);
413 if ((pid = fork()) == -1) {
415 errx(2, "%s: cannot fork process for tar", __func__);
417 if (pid == 0) { /* The child */
421 execv("/usr/bin/tar", (char * const *)(uintptr_t)args);
423 errx(2, "%s: failed to execute tar command", __func__);
426 /* Meanwhile, back in the parent process ... */
428 if ((totar = fdopen(pipefds[1], "w")) == NULL) {
430 errx(2, "%s: fdopen failed", __func__);
433 fprintf(totar, "%s\n", CONTENTS_FNAME);
434 fprintf(totar, "%s\n", COMMENT_FNAME);
435 fprintf(totar, "%s\n", DESC_FNAME);
438 fprintf(totar, "%s\n", INSTALL_FNAME);
440 fprintf(totar, "%s\n", POST_INSTALL_FNAME);
442 fprintf(totar, "%s\n", DEINSTALL_FNAME);
444 fprintf(totar, "%s\n", POST_DEINSTALL_FNAME);
446 fprintf(totar, "%s\n", REQUIRE_FNAME);
448 fprintf(totar, "%s\n", DISPLAY_FNAME);
450 fprintf(totar, "%s\n", MTREE_FNAME);
452 for (p = plist->head; p; p = p->next) {
453 if (p->type == PLIST_FILE)
454 fprintf(totar, "%s\n", p->name);
455 else if (p->type == PLIST_CWD && p->name == NULL)
456 fprintf(totar, "-C\n%s\n", prefix);
457 else if (p->type == PLIST_CWD && BaseDir && p->name && p->name[0] == '/')
458 fprintf(totar, "-C\n%s%s\n", BaseDir, p->name);
459 else if (p->type == PLIST_CWD || p->type == PLIST_SRC)
460 fprintf(totar, "-C\n%s\n", p->name);
461 else if (p->type == PLIST_IGNORE)
463 if (p->type == PLIST_CWD && !prefix)
470 /* assume either signal or bad exit is enough for us */
473 errx(2, "%s: tar command failed with code %d", __func__, ret);
482 errx(2, "%s: required package comment string is missing (-c comment)",
487 errx(2, "%s: required package description string is missing (-d desc)",
492 errx(2, "%s: required package contents list is missing (-f [-]file)",
498 /* Clean up those things that would otherwise hang around */
513 create_from_installed_recursive(const char *pkg, const char *suf)
521 if (!create_from_installed(InstalledPkg, pkg, suf))
523 snprintf(tmp, sizeof(tmp), "%s/%s/%s", LOG_DIR, InstalledPkg, CONTENTS_FNAME);
525 warnx("can't find package '%s' installed!", InstalledPkg);
528 /* Suck in the contents list */
529 plist.head = plist.tail = NULL;
530 fp = fopen(tmp, "r");
532 warnx("unable to open %s file", tmp);
535 read_plist(&plist, fp);
538 for (p = plist.head; p ; p = p->next) {
539 if (p->type != PLIST_PKGDEP)
542 printf("Creating package %s\n", p->name);
543 if (!create_from_installed(p->name, p->name, suf)) {
553 create_from_installed(const char *ipkg, const char *pkg, const char *suf)
557 char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
559 snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
560 if (!fexists(log_dir)) {
561 warnx("can't find package '%s' installed!", ipkg);
564 getcwd(homedir, sizeof(homedir));
565 if (chdir(log_dir) == FAIL) {
566 warnx("can't change directory to '%s'!", log_dir);
569 /* Suck in the contents list */
570 plist.head = plist.tail = NULL;
571 fp = fopen(CONTENTS_FNAME, "r");
573 warnx("unable to open %s file", CONTENTS_FNAME);
576 read_plist(&plist, fp);
579 Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
580 PostInstall = isfile(POST_INSTALL_FNAME) ?
581 (char *)POST_INSTALL_FNAME : NULL;
582 DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
583 PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
584 (char *)POST_DEINSTALL_FNAME : NULL;
585 Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
586 Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
587 Mtree = isfile(MTREE_FNAME) ? (char *)MTREE_FNAME : NULL;
589 make_dist(homedir, pkg, suf, &plist);
592 if (chdir(homedir) == FAIL) {
593 warnx("can't change directory to '%s'!", homedir);