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 *);
45 pkg_perform(char **pkgs)
47 char *pkg = *pkgs; /* Only one arg to create */
54 /* Preliminary setup */
55 if (InstalledPkg == NULL)
57 if (Verbose && !PlistOnly)
58 printf("Creating package %s\n", pkg);
60 /* chop suffix off if already specified, remembering if we want to compress */
63 if (!strcmp(&pkg[len - 4], ".tbz")) {
67 else if (!strcmp(&pkg[len - 4], ".tgz")) {
71 else if (!strcmp(&pkg[len - 4], ".tar")) {
76 if (Zipper == BZIP2) {
78 setenv("BZIP2", "--best", 0);
79 } else if (Zipper == GZIP) {
81 setenv("GZIP", "-9", 0);
85 if (InstalledPkg != NULL) {
86 char *pkgglob[] = { InstalledPkg, NULL };
87 char **matched, **pkgs;
91 if (MatchType != MATCH_EXACT) {
92 matched = matchinstalled(MatchType, pkgs, &error);
93 if (!error && matched != NULL)
95 else if (MatchType != MATCH_GLOB)
96 errx(1, "no packages match pattern");
99 * Is there is only one installed package matching the pattern,
100 * we need to respect the optional pkg-filename parameter. If,
101 * however, the pattern matches several packages, this parameter
102 * makes no sense and is ignored.
104 if (pkgs[1] == NULL) {
105 if (pkg == InstalledPkg)
107 InstalledPkg = *pkgs;
109 return (create_from_installed(InstalledPkg, pkg, suf));
110 return (create_from_installed_recursive(pkg, suf));
112 for (i = 0; pkgs[i] != NULL; i++) {
113 InstalledPkg = pkg = pkgs[i];
115 create_from_installed(pkg, pkg, suf);
117 create_from_installed_recursive(pkg, suf);
122 get_dash_string(&Comment);
123 get_dash_string(&Desc);
124 if (!strcmp(Contents, "-"))
127 pkg_in = fopen(Contents, "r");
130 errx(2, "%s: unable to open contents file '%s' for input",
134 plist.head = plist.tail = NULL;
136 /* Stick the dependencies, if any, at the top */
138 char **deps, *deporigin;
142 if (Verbose && !PlistOnly)
143 printf("Registering depends:");
145 /* Count number of dependencies */
146 for (cp = Pkgdeps; cp != NULL && *cp != '\0';
147 cp = strpbrk(++cp, " \t\n")) {
152 /* Create easy to use NULL-terminated list */
153 deps = alloca(sizeof(*deps) * ndeps + 1);
155 errx(2, "%s: alloca() failed", __func__);
158 for (i = 0; Pkgdeps;) {
159 cp = strsep(&Pkgdeps, " \t\n");
169 for (i = 0; i < ndeps; i++) {
170 deporigin = strchr(deps[i], ':');
171 if (deporigin != NULL) {
173 add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin);
175 add_plist_top(&plist, PLIST_PKGDEP, deps[i]);
176 if (Verbose && !PlistOnly)
177 printf(" %s", deps[i]);
181 if (Verbose && !PlistOnly)
185 /* Put the conflicts directly after the dependencies, if any */
187 if (Verbose && !PlistOnly)
188 printf("Registering conflicts:");
190 cp = strsep(&Conflicts, " \t\n");
192 add_plist(&plist, PLIST_CONFLICTS, cp);
193 if (Verbose && !PlistOnly)
197 if (Verbose && !PlistOnly)
201 /* If a SrcDir override is set, add it now */
203 if (Verbose && !PlistOnly)
204 printf("Using SrcDir value of %s\n", SrcDir);
205 add_plist(&plist, PLIST_SRC, SrcDir);
208 /* Slurp in the packing list */
209 read_plist(&plist, pkg_in);
211 /* Prefix should add an @cwd to the packing list */
213 add_plist_top(&plist, PLIST_CWD, Prefix);
215 /* Add the origin if asked, at the top */
217 add_plist_top(&plist, PLIST_ORIGIN, Origin);
220 * Run down the list and see if we've named it, if not stick in a name
223 if (find_plist(&plist, PLIST_NAME) == NULL)
224 add_plist_top(&plist, PLIST_NAME, basename(pkg));
226 if (asprintf(&cp, "PKG_FORMAT_REVISION:%d.%d", PLIST_FMT_VER_MAJOR,
227 PLIST_FMT_VER_MINOR) == -1) {
228 errx(2, "%s: asprintf() failed", __func__);
230 add_plist_top(&plist, PLIST_COMMENT, cp);
234 * We're just here for to dump out a revised plist for the FreeBSD ports
235 * hack. It's not a real create in progress.
238 check_list(home, &plist);
239 write_plist(&plist, stdout);
243 /* Make a directory to stomp around in */
244 home = make_playpen(PlayPen, 0);
245 signal(SIGINT, cleanup);
246 signal(SIGHUP, cleanup);
248 /* Make first "real contents" pass over it */
249 check_list(home, &plist);
250 (void) umask(022); /*
251 * Make sure gen'ed directories, files don't have
252 * group or other write bits.
254 /* copy_plist(home, &plist); */
255 /* mark_plist(&plist); */
257 /* Now put the release specific items in */
258 add_plist(&plist, PLIST_CWD, ".");
259 write_file(COMMENT_FNAME, Comment);
260 add_plist(&plist, PLIST_IGNORE, NULL);
261 add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
262 add_cksum(&plist, plist.tail, COMMENT_FNAME);
263 write_file(DESC_FNAME, Desc);
264 add_plist(&plist, PLIST_IGNORE, NULL);
265 add_plist(&plist, PLIST_FILE, DESC_FNAME);
266 add_cksum(&plist, plist.tail, DESC_FNAME);
269 copy_file(home, Install, INSTALL_FNAME);
270 add_plist(&plist, PLIST_IGNORE, NULL);
271 add_plist(&plist, PLIST_FILE, INSTALL_FNAME);
272 add_cksum(&plist, plist.tail, INSTALL_FNAME);
275 copy_file(home, PostInstall, POST_INSTALL_FNAME);
276 add_plist(&plist, PLIST_IGNORE, NULL);
277 add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME);
278 add_cksum(&plist, plist.tail, POST_INSTALL_FNAME);
281 copy_file(home, DeInstall, DEINSTALL_FNAME);
282 add_plist(&plist, PLIST_IGNORE, NULL);
283 add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME);
284 add_cksum(&plist, plist.tail, DEINSTALL_FNAME);
287 copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME);
288 add_plist(&plist, PLIST_IGNORE, NULL);
289 add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME);
290 add_cksum(&plist, plist.tail, POST_DEINSTALL_FNAME);
293 copy_file(home, Require, REQUIRE_FNAME);
294 add_plist(&plist, PLIST_IGNORE, NULL);
295 add_plist(&plist, PLIST_FILE, REQUIRE_FNAME);
296 add_cksum(&plist, plist.tail, REQUIRE_FNAME);
299 copy_file(home, Display, DISPLAY_FNAME);
300 add_plist(&plist, PLIST_IGNORE, NULL);
301 add_plist(&plist, PLIST_FILE, DISPLAY_FNAME);
302 add_cksum(&plist, plist.tail, DISPLAY_FNAME);
303 add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME);
306 copy_file(home, Mtree, MTREE_FNAME);
307 add_plist(&plist, PLIST_IGNORE, NULL);
308 add_plist(&plist, PLIST_FILE, MTREE_FNAME);
309 add_cksum(&plist, plist.tail, MTREE_FNAME);
310 add_plist(&plist, PLIST_MTREE, MTREE_FNAME);
313 /* Finally, write out the packing list */
314 fp = fopen(CONTENTS_FNAME, "w");
317 errx(2, "%s: can't open file %s for writing",
318 __func__, CONTENTS_FNAME);
320 write_plist(&plist, fp);
323 errx(2, "%s: error while closing %s",
324 __func__, CONTENTS_FNAME);
327 /* And stick it into a tar ball */
328 make_dist(home, pkg, suf, &plist);
335 return TRUE; /* Success */
339 make_dist(const char *homedir, const char *pkg, const char *suff, Package *plist)
342 char tball[FILENAME_MAX];
345 const char *args[50]; /* Much more than enough. */
354 args[nargs++] = "tar"; /* argv[0] */
357 snprintf(tball, FILENAME_MAX, "%s.%s", pkg, suff);
359 snprintf(tball, FILENAME_MAX, "%s/%s.%s", homedir, pkg, suff);
362 * If the package tarball exists already, and we are running in `no
363 * clobber' mode, skip this package.
365 if (stat(tball, &sb) == 0 && Regenerate == FALSE) {
367 printf("Skipping package '%s'. It already exists.\n", tball);
371 args[nargs++] = "-c";
372 args[nargs++] = "-f";
373 args[nargs++] = tball;
374 if (strchr(suff, 'z')) { /* Compress/gzip/bzip2? */
375 if (Zipper == BZIP2) {
376 args[nargs++] = "-j";
380 args[nargs++] = "-z";
387 args[nargs++] = "-h";
389 args[nargs++] = "-X";
390 args[nargs++] = ExcludeFrom;
392 args[nargs++] = "-T"; /* Take filenames from file instead of args. */
393 args[nargs++] = "-"; /* Use stdin for the file. */
397 printf("Creating %star ball in '%s'\n", cname, tball);
399 /* Set up a pipe for passing the filenames, and fork off a tar process. */
400 if (pipe(pipefds) == -1) {
402 errx(2, "%s: cannot create pipe", __func__);
404 if ((pid = fork()) == -1) {
406 errx(2, "%s: cannot fork process for tar", __func__);
408 if (pid == 0) { /* The child */
412 execv("/usr/bin/tar", (char * const *)(uintptr_t)args);
414 errx(2, "%s: failed to execute tar command", __func__);
417 /* Meanwhile, back in the parent process ... */
419 if ((totar = fdopen(pipefds[1], "w")) == NULL) {
421 errx(2, "%s: fdopen failed", __func__);
424 fprintf(totar, "%s\n", CONTENTS_FNAME);
425 fprintf(totar, "%s\n", COMMENT_FNAME);
426 fprintf(totar, "%s\n", DESC_FNAME);
429 fprintf(totar, "%s\n", INSTALL_FNAME);
431 fprintf(totar, "%s\n", POST_INSTALL_FNAME);
433 fprintf(totar, "%s\n", DEINSTALL_FNAME);
435 fprintf(totar, "%s\n", POST_DEINSTALL_FNAME);
437 fprintf(totar, "%s\n", REQUIRE_FNAME);
439 fprintf(totar, "%s\n", DISPLAY_FNAME);
441 fprintf(totar, "%s\n", MTREE_FNAME);
443 for (p = plist->head; p; p = p->next) {
444 if (p->type == PLIST_FILE)
445 fprintf(totar, "%s\n", p->name);
446 else if (p->type == PLIST_CWD && p->name == NULL)
447 fprintf(totar, "-C\n%s\n", prefix);
448 else if (p->type == PLIST_CWD && BaseDir && p->name && p->name[0] == '/')
449 fprintf(totar, "-C\n%s%s\n", BaseDir, p->name);
450 else if (p->type == PLIST_CWD || p->type == PLIST_SRC)
451 fprintf(totar, "-C\n%s\n", p->name);
452 else if (p->type == PLIST_IGNORE)
454 if (p->type == PLIST_CWD && !prefix)
461 /* assume either signal or bad exit is enough for us */
464 errx(2, "%s: tar command failed with code %d", __func__, ret);
473 errx(2, "%s: required package comment string is missing (-c comment)",
478 errx(2, "%s: required package description string is missing (-d desc)",
483 errx(2, "%s: required package contents list is missing (-f [-]file)",
489 /* Clean up those things that would otherwise hang around */
504 create_from_installed_recursive(const char *pkg, const char *suf)
512 if (!create_from_installed(InstalledPkg, pkg, suf))
514 snprintf(tmp, sizeof(tmp), "%s/%s/%s", LOG_DIR, InstalledPkg, CONTENTS_FNAME);
516 warnx("can't find package '%s' installed!", InstalledPkg);
519 /* Suck in the contents list */
520 plist.head = plist.tail = NULL;
521 fp = fopen(tmp, "r");
523 warnx("unable to open %s file", tmp);
526 read_plist(&plist, fp);
529 for (p = plist.head; p ; p = p->next) {
530 if (p->type != PLIST_PKGDEP)
533 printf("Creating package %s\n", p->name);
534 if (!create_from_installed(p->name, p->name, suf)) {
544 create_from_installed(const char *ipkg, const char *pkg, const char *suf)
548 char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
550 snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
551 if (!fexists(log_dir)) {
552 warnx("can't find package '%s' installed!", ipkg);
555 getcwd(homedir, sizeof(homedir));
556 if (chdir(log_dir) == FAIL) {
557 warnx("can't change directory to '%s'!", log_dir);
560 /* Suck in the contents list */
561 plist.head = plist.tail = NULL;
562 fp = fopen(CONTENTS_FNAME, "r");
564 warnx("unable to open %s file", CONTENTS_FNAME);
567 read_plist(&plist, fp);
570 Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
571 PostInstall = isfile(POST_INSTALL_FNAME) ?
572 (char *)POST_INSTALL_FNAME : NULL;
573 DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
574 PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
575 (char *)POST_DEINSTALL_FNAME : NULL;
576 Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
577 Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
578 Mtree = isfile(MTREE_FNAME) ? (char *)MTREE_FNAME : NULL;
580 make_dist(homedir, pkg, suf, &plist);
583 if (chdir(homedir) == FAIL) {
584 warnx("can't change directory to '%s'!", homedir);