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/syslimits.h>
35 static void sanity_check(void);
36 static void make_dist(const char *, const char *, const char *, Package *);
37 static int create_from_installed_recursive(const char *, const char *);
38 static int create_from_installed(const char *, const char *, const char *);
43 pkg_perform(char **pkgs)
45 char *pkg = *pkgs; /* Only one arg to create */
52 /* Preliminary setup */
53 if (InstalledPkg == NULL)
55 if (Verbose && !PlistOnly)
56 printf("Creating package %s\n", pkg);
58 /* chop suffix off if already specified, remembering if we want to compress */
61 if (!strcmp(&pkg[len - 4], ".tbz")) {
65 else if (!strcmp(&pkg[len - 4], ".tgz")) {
69 else if (!strcmp(&pkg[len - 4], ".tar")) {
74 if (Zipper == BZIP2) {
76 setenv("BZIP2", "--best", 0);
77 } else if (Zipper == GZIP) {
79 setenv("GZIP", "-9", 0);
83 if (InstalledPkg != NULL) {
84 char *pkgglob[] = { InstalledPkg, NULL };
85 char **matched, **pkgs;
89 if (MatchType != MATCH_EXACT) {
90 matched = matchinstalled(MatchType, pkgs, &error);
91 if (!error && matched != NULL)
93 else if (MatchType != MATCH_GLOB)
94 errx(1, "no packages match pattern");
97 * Is there is only one installed package matching the pattern,
98 * we need to respect the optional pkg-filename parameter. If,
99 * however, the pattern matches several packages, this parameter
100 * makes no sense and is ignored.
102 if (pkgs[1] == NULL) {
103 if (pkg == InstalledPkg)
105 InstalledPkg = *pkgs;
107 return (create_from_installed(InstalledPkg, pkg, suf));
108 return (create_from_installed_recursive(pkg, suf));
110 for (i = 0; pkgs[i] != NULL; i++) {
111 InstalledPkg = pkg = pkgs[i];
113 create_from_installed(pkg, pkg, suf);
115 create_from_installed_recursive(pkg, suf);
120 get_dash_string(&Comment);
121 get_dash_string(&Desc);
122 if (!strcmp(Contents, "-"))
125 pkg_in = fopen(Contents, "r");
128 errx(2, "%s: unable to open contents file '%s' for input",
132 plist.head = plist.tail = NULL;
134 /* Stick the dependencies, if any, at the top */
136 char **deps, *deporigin;
140 if (Verbose && !PlistOnly)
141 printf("Registering depends:");
143 /* Count number of dependencies */
144 for (cp = Pkgdeps; cp != NULL && *cp != '\0';
145 cp = strpbrk(++cp, " \t\n")) {
150 /* Create easy to use NULL-terminated list */
151 deps = alloca(sizeof(*deps) * ndeps + 1);
153 errx(2, "%s: alloca() failed", __func__);
156 for (i = 0; Pkgdeps;) {
157 cp = strsep(&Pkgdeps, " \t\n");
167 for (i = 0; i < ndeps; i++) {
168 deporigin = strchr(deps[i], ':');
169 if (deporigin != NULL) {
171 add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin);
173 add_plist_top(&plist, PLIST_PKGDEP, deps[i]);
174 if (Verbose && !PlistOnly)
175 printf(" %s", deps[i]);
179 if (Verbose && !PlistOnly)
183 /* Put the conflicts directly after the dependencies, if any */
185 if (Verbose && !PlistOnly)
186 printf("Registering conflicts:");
188 cp = strsep(&Conflicts, " \t\n");
190 add_plist(&plist, PLIST_CONFLICTS, cp);
191 if (Verbose && !PlistOnly)
195 if (Verbose && !PlistOnly)
199 /* If a SrcDir override is set, add it now */
201 if (Verbose && !PlistOnly)
202 printf("Using SrcDir value of %s\n", SrcDir);
203 add_plist(&plist, PLIST_SRC, SrcDir);
206 /* Slurp in the packing list */
207 read_plist(&plist, pkg_in);
209 /* Prefix should add an @cwd to the packing list */
211 add_plist_top(&plist, PLIST_CWD, Prefix);
213 /* Add the origin if asked, at the top */
215 add_plist_top(&plist, PLIST_ORIGIN, Origin);
218 * Run down the list and see if we've named it, if not stick in a name
221 if (find_plist(&plist, PLIST_NAME) == NULL)
222 add_plist_top(&plist, PLIST_NAME, basename(pkg));
224 if (asprintf(&cp, "PKG_FORMAT_REVISION:%d.%d", PLIST_FMT_VER_MAJOR,
225 PLIST_FMT_VER_MINOR) == -1) {
226 errx(2, "%s: asprintf() failed", __func__);
228 add_plist_top(&plist, PLIST_COMMENT, cp);
232 * We're just here for to dump out a revised plist for the FreeBSD ports
233 * hack. It's not a real create in progress.
236 check_list(home, &plist);
237 write_plist(&plist, stdout);
241 /* Make a directory to stomp around in */
242 home = make_playpen(PlayPen, 0);
243 signal(SIGINT, cleanup);
244 signal(SIGHUP, cleanup);
246 /* Make first "real contents" pass over it */
247 check_list(home, &plist);
248 (void) umask(022); /*
249 * Make sure gen'ed directories, files don't have
250 * group or other write bits.
252 /* copy_plist(home, &plist); */
253 /* mark_plist(&plist); */
255 /* Now put the release specific items in */
256 add_plist(&plist, PLIST_CWD, ".");
257 write_file(COMMENT_FNAME, Comment);
258 add_plist(&plist, PLIST_IGNORE, NULL);
259 add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
260 add_cksum(&plist, plist.tail, COMMENT_FNAME);
261 write_file(DESC_FNAME, Desc);
262 add_plist(&plist, PLIST_IGNORE, NULL);
263 add_plist(&plist, PLIST_FILE, DESC_FNAME);
264 add_cksum(&plist, plist.tail, DESC_FNAME);
267 copy_file(home, Install, INSTALL_FNAME);
268 add_plist(&plist, PLIST_IGNORE, NULL);
269 add_plist(&plist, PLIST_FILE, INSTALL_FNAME);
270 add_cksum(&plist, plist.tail, INSTALL_FNAME);
273 copy_file(home, PostInstall, POST_INSTALL_FNAME);
274 add_plist(&plist, PLIST_IGNORE, NULL);
275 add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME);
276 add_cksum(&plist, plist.tail, POST_INSTALL_FNAME);
279 copy_file(home, DeInstall, DEINSTALL_FNAME);
280 add_plist(&plist, PLIST_IGNORE, NULL);
281 add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME);
282 add_cksum(&plist, plist.tail, DEINSTALL_FNAME);
285 copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME);
286 add_plist(&plist, PLIST_IGNORE, NULL);
287 add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME);
288 add_cksum(&plist, plist.tail, POST_DEINSTALL_FNAME);
291 copy_file(home, Require, REQUIRE_FNAME);
292 add_plist(&plist, PLIST_IGNORE, NULL);
293 add_plist(&plist, PLIST_FILE, REQUIRE_FNAME);
294 add_cksum(&plist, plist.tail, REQUIRE_FNAME);
297 copy_file(home, Display, DISPLAY_FNAME);
298 add_plist(&plist, PLIST_IGNORE, NULL);
299 add_plist(&plist, PLIST_FILE, DISPLAY_FNAME);
300 add_cksum(&plist, plist.tail, DISPLAY_FNAME);
301 add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME);
304 copy_file(home, Mtree, MTREE_FNAME);
305 add_plist(&plist, PLIST_IGNORE, NULL);
306 add_plist(&plist, PLIST_FILE, MTREE_FNAME);
307 add_cksum(&plist, plist.tail, MTREE_FNAME);
308 add_plist(&plist, PLIST_MTREE, MTREE_FNAME);
311 /* Finally, write out the packing list */
312 fp = fopen(CONTENTS_FNAME, "w");
315 errx(2, "%s: can't open file %s for writing",
316 __func__, CONTENTS_FNAME);
318 write_plist(&plist, fp);
321 errx(2, "%s: error while closing %s",
322 __func__, CONTENTS_FNAME);
325 /* And stick it into a tar ball */
326 make_dist(home, pkg, suf, &plist);
333 return TRUE; /* Success */
337 make_dist(const char *homedir, const char *pkg, const char *suff, Package *plist)
339 char tball[FILENAME_MAX];
342 const char *args[50]; /* Much more than enough. */
351 args[nargs++] = "tar"; /* argv[0] */
354 snprintf(tball, FILENAME_MAX, "%s.%s", pkg, suff);
356 snprintf(tball, FILENAME_MAX, "%s/%s.%s", homedir, pkg, suff);
358 args[nargs++] = "-c";
359 args[nargs++] = "-f";
360 args[nargs++] = tball;
361 if (strchr(suff, 'z')) { /* Compress/gzip/bzip2? */
362 if (Zipper == BZIP2) {
363 args[nargs++] = "-j";
367 args[nargs++] = "-z";
374 args[nargs++] = "-h";
376 args[nargs++] = "-X";
377 args[nargs++] = ExcludeFrom;
379 args[nargs++] = "-T"; /* Take filenames from file instead of args. */
380 args[nargs++] = "-"; /* Use stdin for the file. */
384 printf("Creating %star ball in '%s'\n", cname, tball);
386 /* Set up a pipe for passing the filenames, and fork off a tar process. */
387 if (pipe(pipefds) == -1) {
389 errx(2, "%s: cannot create pipe", __func__);
391 if ((pid = fork()) == -1) {
393 errx(2, "%s: cannot fork process for tar", __func__);
395 if (pid == 0) { /* The child */
399 execv("/usr/bin/tar", (char * const *)(uintptr_t)args);
401 errx(2, "%s: failed to execute tar command", __func__);
404 /* Meanwhile, back in the parent process ... */
406 if ((totar = fdopen(pipefds[1], "w")) == NULL) {
408 errx(2, "%s: fdopen failed", __func__);
411 fprintf(totar, "%s\n", CONTENTS_FNAME);
412 fprintf(totar, "%s\n", COMMENT_FNAME);
413 fprintf(totar, "%s\n", DESC_FNAME);
416 fprintf(totar, "%s\n", INSTALL_FNAME);
418 fprintf(totar, "%s\n", POST_INSTALL_FNAME);
420 fprintf(totar, "%s\n", DEINSTALL_FNAME);
422 fprintf(totar, "%s\n", POST_DEINSTALL_FNAME);
424 fprintf(totar, "%s\n", REQUIRE_FNAME);
426 fprintf(totar, "%s\n", DISPLAY_FNAME);
428 fprintf(totar, "%s\n", MTREE_FNAME);
430 for (p = plist->head; p; p = p->next) {
431 if (p->type == PLIST_FILE)
432 fprintf(totar, "%s\n", p->name);
433 else if (p->type == PLIST_CWD && p->name == NULL)
434 fprintf(totar, "-C\n%s\n", prefix);
435 else if (p->type == PLIST_CWD && BaseDir && p->name && p->name[0] == '/')
436 fprintf(totar, "-C\n%s%s\n", BaseDir, p->name);
437 else if (p->type == PLIST_CWD || p->type == PLIST_SRC)
438 fprintf(totar, "-C\n%s\n", p->name);
439 else if (p->type == PLIST_IGNORE)
441 if (p->type == PLIST_CWD && !prefix)
448 /* assume either signal or bad exit is enough for us */
451 errx(2, "%s: tar command failed with code %d", __func__, ret);
460 errx(2, "%s: required package comment string is missing (-c comment)",
465 errx(2, "%s: required package description string is missing (-d desc)",
470 errx(2, "%s: required package contents list is missing (-f [-]file)",
476 /* Clean up those things that would otherwise hang around */
491 create_from_installed_recursive(const char *pkg, const char *suf)
499 if (!create_from_installed(InstalledPkg, pkg, suf))
501 snprintf(tmp, sizeof(tmp), "%s/%s/%s", LOG_DIR, InstalledPkg, CONTENTS_FNAME);
503 warnx("can't find package '%s' installed!", InstalledPkg);
506 /* Suck in the contents list */
507 plist.head = plist.tail = NULL;
508 fp = fopen(tmp, "r");
510 warnx("unable to open %s file", tmp);
513 read_plist(&plist, fp);
516 for (p = plist.head; p ; p = p->next) {
517 if (p->type != PLIST_PKGDEP)
520 printf("Creating package %s\n", p->name);
521 if (!create_from_installed(p->name, p->name, suf)) {
531 create_from_installed(const char *ipkg, const char *pkg, const char *suf)
535 char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
537 snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
538 if (!fexists(log_dir)) {
539 warnx("can't find package '%s' installed!", ipkg);
542 getcwd(homedir, sizeof(homedir));
543 if (chdir(log_dir) == FAIL) {
544 warnx("can't change directory to '%s'!", log_dir);
547 /* Suck in the contents list */
548 plist.head = plist.tail = NULL;
549 fp = fopen(CONTENTS_FNAME, "r");
551 warnx("unable to open %s file", CONTENTS_FNAME);
554 read_plist(&plist, fp);
557 Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
558 PostInstall = isfile(POST_INSTALL_FNAME) ?
559 (char *)POST_INSTALL_FNAME : NULL;
560 DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
561 PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
562 (char *)POST_DEINSTALL_FNAME : NULL;
563 Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
564 Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
565 Mtree = isfile(MTREE_FNAME) ? (char *)MTREE_FNAME : NULL;
567 make_dist(homedir, pkg, suf, &plist);
570 if (chdir(homedir) == FAIL) {
571 warnx("can't change directory to '%s'!", homedir);