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], ".tar")) {
75 if (Zipper == BZIP2) {
77 setenv("BZIP2", "--best", 0);
78 } else if (Zipper == GZIP) {
80 setenv("GZIP", "-9", 0);
84 if (InstalledPkg != NULL) {
85 char *pkgglob[] = { InstalledPkg, NULL };
86 char **matched, **pkgs;
90 if (MatchType != MATCH_EXACT) {
91 matched = matchinstalled(MatchType, pkgs, &error);
92 if (!error && matched != NULL)
94 else if (MatchType != MATCH_GLOB)
95 errx(1, "no packages match pattern");
98 * Is there is only one installed package matching the pattern,
99 * we need to respect the optional pkg-filename parameter. If,
100 * however, the pattern matches several packages, this parameter
101 * makes no sense and is ignored.
103 if (pkgs[1] == NULL) {
104 if (pkg == InstalledPkg)
106 InstalledPkg = *pkgs;
108 return (create_from_installed(InstalledPkg, pkg, suf));
109 return (create_from_installed_recursive(pkg, suf));
111 for (i = 0; pkgs[i] != NULL; i++) {
112 InstalledPkg = pkg = pkgs[i];
114 create_from_installed(pkg, pkg, suf);
116 create_from_installed_recursive(pkg, suf);
121 get_dash_string(&Comment);
122 get_dash_string(&Desc);
123 if (!strcmp(Contents, "-"))
126 pkg_in = fopen(Contents, "r");
129 errx(2, "%s: unable to open contents file '%s' for input",
133 plist.head = plist.tail = NULL;
135 /* Stick the dependencies, if any, at the top */
137 char **deps, *deporigin;
141 if (Verbose && !PlistOnly)
142 printf("Registering depends:");
144 /* Count number of dependencies */
145 for (cp = Pkgdeps; cp != NULL && *cp != '\0';
146 cp = strpbrk(++cp, " \t\n")) {
151 /* Create easy to use NULL-terminated list */
152 deps = alloca(sizeof(*deps) * ndeps + 1);
154 errx(2, "%s: alloca() failed", __func__);
157 for (i = 0; Pkgdeps;) {
158 cp = strsep(&Pkgdeps, " \t\n");
168 for (i = 0; i < ndeps; i++) {
169 deporigin = strchr(deps[i], ':');
170 if (deporigin != NULL) {
172 add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin);
174 add_plist_top(&plist, PLIST_PKGDEP, deps[i]);
175 if (Verbose && !PlistOnly)
176 printf(" %s", deps[i]);
180 if (Verbose && !PlistOnly)
184 /* Put the conflicts directly after the dependencies, if any */
186 if (Verbose && !PlistOnly)
187 printf("Registering conflicts:");
189 cp = strsep(&Conflicts, " \t\n");
191 add_plist(&plist, PLIST_CONFLICTS, cp);
192 if (Verbose && !PlistOnly)
196 if (Verbose && !PlistOnly)
200 /* If a SrcDir override is set, add it now */
202 if (Verbose && !PlistOnly)
203 printf("Using SrcDir value of %s\n", SrcDir);
204 add_plist(&plist, PLIST_SRC, SrcDir);
207 /* Slurp in the packing list */
208 read_plist(&plist, pkg_in);
210 /* Prefix should add an @cwd to the packing list */
212 add_plist_top(&plist, PLIST_CWD, Prefix);
214 /* Add the origin if asked, at the top */
216 add_plist_top(&plist, PLIST_ORIGIN, Origin);
219 * Run down the list and see if we've named it, if not stick in a name
222 if (find_plist(&plist, PLIST_NAME) == NULL)
223 add_plist_top(&plist, PLIST_NAME, basename(pkg));
225 if (asprintf(&cp, "PKG_FORMAT_REVISION:%d.%d", PLIST_FMT_VER_MAJOR,
226 PLIST_FMT_VER_MINOR) == -1) {
227 errx(2, "%s: asprintf() failed", __func__);
229 add_plist_top(&plist, PLIST_COMMENT, cp);
233 * We're just here for to dump out a revised plist for the FreeBSD ports
234 * hack. It's not a real create in progress.
237 check_list(home, &plist);
238 write_plist(&plist, stdout);
242 /* Make a directory to stomp around in */
243 home = make_playpen(PlayPen, 0);
244 signal(SIGINT, cleanup);
245 signal(SIGHUP, cleanup);
247 /* Make first "real contents" pass over it */
248 check_list(home, &plist);
249 (void) umask(022); /*
250 * Make sure gen'ed directories, files don't have
251 * group or other write bits.
253 /* copy_plist(home, &plist); */
254 /* mark_plist(&plist); */
256 /* Now put the release specific items in */
257 add_plist(&plist, PLIST_CWD, ".");
258 write_file(COMMENT_FNAME, Comment);
259 add_plist(&plist, PLIST_IGNORE, NULL);
260 add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
261 add_cksum(&plist, plist.tail, COMMENT_FNAME);
262 write_file(DESC_FNAME, Desc);
263 add_plist(&plist, PLIST_IGNORE, NULL);
264 add_plist(&plist, PLIST_FILE, DESC_FNAME);
265 add_cksum(&plist, plist.tail, DESC_FNAME);
268 copy_file(home, Install, INSTALL_FNAME);
269 add_plist(&plist, PLIST_IGNORE, NULL);
270 add_plist(&plist, PLIST_FILE, INSTALL_FNAME);
271 add_cksum(&plist, plist.tail, INSTALL_FNAME);
274 copy_file(home, PostInstall, POST_INSTALL_FNAME);
275 add_plist(&plist, PLIST_IGNORE, NULL);
276 add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME);
277 add_cksum(&plist, plist.tail, POST_INSTALL_FNAME);
280 copy_file(home, DeInstall, DEINSTALL_FNAME);
281 add_plist(&plist, PLIST_IGNORE, NULL);
282 add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME);
283 add_cksum(&plist, plist.tail, DEINSTALL_FNAME);
286 copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME);
287 add_plist(&plist, PLIST_IGNORE, NULL);
288 add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME);
289 add_cksum(&plist, plist.tail, POST_DEINSTALL_FNAME);
292 copy_file(home, Require, REQUIRE_FNAME);
293 add_plist(&plist, PLIST_IGNORE, NULL);
294 add_plist(&plist, PLIST_FILE, REQUIRE_FNAME);
295 add_cksum(&plist, plist.tail, REQUIRE_FNAME);
298 copy_file(home, Display, DISPLAY_FNAME);
299 add_plist(&plist, PLIST_IGNORE, NULL);
300 add_plist(&plist, PLIST_FILE, DISPLAY_FNAME);
301 add_cksum(&plist, plist.tail, DISPLAY_FNAME);
302 add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME);
305 copy_file(home, Mtree, MTREE_FNAME);
306 add_plist(&plist, PLIST_IGNORE, NULL);
307 add_plist(&plist, PLIST_FILE, MTREE_FNAME);
308 add_cksum(&plist, plist.tail, MTREE_FNAME);
309 add_plist(&plist, PLIST_MTREE, MTREE_FNAME);
312 /* Finally, write out the packing list */
313 fp = fopen(CONTENTS_FNAME, "w");
316 errx(2, "%s: can't open file %s for writing",
317 __func__, CONTENTS_FNAME);
319 write_plist(&plist, fp);
322 errx(2, "%s: error while closing %s",
323 __func__, CONTENTS_FNAME);
326 /* And stick it into a tar ball */
327 make_dist(home, pkg, suf, &plist);
334 return TRUE; /* Success */
338 make_dist(const char *homedir, const char *pkg, const char *suff, Package *plist)
341 char tball[FILENAME_MAX];
344 const char *args[50]; /* Much more than enough. */
353 args[nargs++] = "tar"; /* argv[0] */
356 snprintf(tball, FILENAME_MAX, "%s.%s", pkg, suff);
358 snprintf(tball, FILENAME_MAX, "%s/%s.%s", homedir, pkg, suff);
361 * If the package tarball exists already, and we are running in `no
362 * clobber' mode, skip this package.
364 if (stat(tball, &sb) == 0 && Regenerate == FALSE) {
366 printf("Skipping package '%s'. It already exists.\n", tball);
370 args[nargs++] = "-c";
371 args[nargs++] = "-f";
372 args[nargs++] = tball;
373 if (strchr(suff, 'z')) { /* Compress/gzip/bzip2? */
374 if (Zipper == BZIP2) {
375 args[nargs++] = "-j";
379 args[nargs++] = "-z";
386 args[nargs++] = "-h";
388 args[nargs++] = "-X";
389 args[nargs++] = ExcludeFrom;
391 args[nargs++] = "-T"; /* Take filenames from file instead of args. */
392 args[nargs++] = "-"; /* Use stdin for the file. */
396 printf("Creating %star ball in '%s'\n", cname, tball);
398 /* Set up a pipe for passing the filenames, and fork off a tar process. */
399 if (pipe(pipefds) == -1) {
401 errx(2, "%s: cannot create pipe", __func__);
403 if ((pid = fork()) == -1) {
405 errx(2, "%s: cannot fork process for tar", __func__);
407 if (pid == 0) { /* The child */
411 execv("/usr/bin/tar", (char * const *)(uintptr_t)args);
413 errx(2, "%s: failed to execute tar command", __func__);
416 /* Meanwhile, back in the parent process ... */
418 if ((totar = fdopen(pipefds[1], "w")) == NULL) {
420 errx(2, "%s: fdopen failed", __func__);
423 fprintf(totar, "%s\n", CONTENTS_FNAME);
424 fprintf(totar, "%s\n", COMMENT_FNAME);
425 fprintf(totar, "%s\n", DESC_FNAME);
428 fprintf(totar, "%s\n", INSTALL_FNAME);
430 fprintf(totar, "%s\n", POST_INSTALL_FNAME);
432 fprintf(totar, "%s\n", DEINSTALL_FNAME);
434 fprintf(totar, "%s\n", POST_DEINSTALL_FNAME);
436 fprintf(totar, "%s\n", REQUIRE_FNAME);
438 fprintf(totar, "%s\n", DISPLAY_FNAME);
440 fprintf(totar, "%s\n", MTREE_FNAME);
442 for (p = plist->head; p; p = p->next) {
443 if (p->type == PLIST_FILE)
444 fprintf(totar, "%s\n", p->name);
445 else if (p->type == PLIST_CWD && p->name == NULL)
446 fprintf(totar, "-C\n%s\n", prefix);
447 else if (p->type == PLIST_CWD && BaseDir && p->name && p->name[0] == '/')
448 fprintf(totar, "-C\n%s%s\n", BaseDir, p->name);
449 else if (p->type == PLIST_CWD || p->type == PLIST_SRC)
450 fprintf(totar, "-C\n%s\n", p->name);
451 else if (p->type == PLIST_IGNORE)
453 if (p->type == PLIST_CWD && !prefix)
460 /* assume either signal or bad exit is enough for us */
463 errx(2, "%s: tar command failed with code %d", __func__, ret);
472 errx(2, "%s: required package comment string is missing (-c comment)",
477 errx(2, "%s: required package description string is missing (-d desc)",
482 errx(2, "%s: required package contents list is missing (-f [-]file)",
488 /* Clean up those things that would otherwise hang around */
503 create_from_installed_recursive(const char *pkg, const char *suf)
511 if (!create_from_installed(InstalledPkg, pkg, suf))
513 snprintf(tmp, sizeof(tmp), "%s/%s/%s", LOG_DIR, InstalledPkg, CONTENTS_FNAME);
515 warnx("can't find package '%s' installed!", InstalledPkg);
518 /* Suck in the contents list */
519 plist.head = plist.tail = NULL;
520 fp = fopen(tmp, "r");
522 warnx("unable to open %s file", tmp);
525 read_plist(&plist, fp);
528 for (p = plist.head; p ; p = p->next) {
529 if (p->type != PLIST_PKGDEP)
532 printf("Creating package %s\n", p->name);
533 if (!create_from_installed(p->name, p->name, suf)) {
543 create_from_installed(const char *ipkg, const char *pkg, const char *suf)
547 char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
549 snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
550 if (!fexists(log_dir)) {
551 warnx("can't find package '%s' installed!", ipkg);
554 getcwd(homedir, sizeof(homedir));
555 if (chdir(log_dir) == FAIL) {
556 warnx("can't change directory to '%s'!", log_dir);
559 /* Suck in the contents list */
560 plist.head = plist.tail = NULL;
561 fp = fopen(CONTENTS_FNAME, "r");
563 warnx("unable to open %s file", CONTENTS_FNAME);
566 read_plist(&plist, fp);
569 Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
570 PostInstall = isfile(POST_INSTALL_FNAME) ?
571 (char *)POST_INSTALL_FNAME : NULL;
572 DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
573 PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
574 (char *)POST_DEINSTALL_FNAME : NULL;
575 Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
576 Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
577 Mtree = isfile(MTREE_FNAME) ? (char *)MTREE_FNAME : NULL;
579 make_dist(homedir, pkg, suf, &plist);
582 if (chdir(homedir) == FAIL) {
583 warnx("can't change directory to '%s'!", homedir);