]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.sbin/pkg_install/create/perform.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / usr.sbin / pkg_install / create / perform.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * This is the main body of the create module.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "lib.h"
25 #include "create.h"
26
27 #include <err.h>
28 #include <libgen.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/syslimits.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36
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 *);
41
42 int
43 pkg_perform(char **pkgs)
44 {
45     static const char *home;
46     char *pkg = *pkgs;          /* Only one arg to create */
47     char *cp;
48     FILE *pkg_in, *fp;
49     Package plist;
50     int len;
51     const char *suf;
52
53     /* Preliminary setup */
54     if (InstalledPkg == NULL)
55         sanity_check();
56     if (Verbose && !PlistOnly)
57         printf("Creating package %s\n", pkg);
58
59     /* chop suffix off if already specified, remembering if we want to compress  */
60     len = strlen(pkg);
61     if (len > 4) {
62         if (!strcmp(&pkg[len - 4], ".tbz")) {
63             Zipper = BZIP2;
64             pkg[len - 4] = '\0';
65         }
66         else if (!strcmp(&pkg[len - 4], ".tgz")) {
67             Zipper = GZIP;
68             pkg[len - 4] = '\0';
69         }
70         else if (!strcmp(&pkg[len - 4], ".txz")) {
71             Zipper = XZ;
72             pkg[len - 4] = '\0';
73         }
74         else if (!strcmp(&pkg[len - 4], ".tar")) {
75             Zipper = NONE;
76             pkg[len - 4] = '\0';
77         }
78     }
79     if (Zipper == BZIP2) {
80         suf = "tbz";
81         setenv("BZIP2", "--best", 0);
82     } else if (Zipper == GZIP) {
83         suf = "tgz";
84         setenv("GZIP", "-9", 0);
85     } else if (Zipper == XZ) {
86         suf = "txz";
87     } else
88         suf = "tar";
89
90     if (InstalledPkg != NULL) {
91         char *pkgglob[] = { InstalledPkg, NULL };
92         char **matched, **pkgs;
93         int i, error;
94
95         pkgs = pkgglob;
96         if (MatchType != MATCH_EXACT) {
97                 matched = matchinstalled(MatchType, pkgs, &error);
98                 if (!error && matched != NULL)
99                         pkgs = matched;
100                 else if (MatchType != MATCH_GLOB)
101                         errx(1, "no packages match pattern");
102         }
103         /*
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.
108          */
109         if (pkgs[1] == NULL) {
110             if (pkg == InstalledPkg)
111                 pkg = *pkgs;
112             InstalledPkg = *pkgs;
113             if (!Recursive)
114                 return (create_from_installed(InstalledPkg, pkg, suf));
115             return (create_from_installed_recursive(pkg, suf));
116         }
117         for (i = 0; pkgs[i] != NULL; i++) {
118             InstalledPkg = pkg = pkgs[i];
119             if (!Recursive)
120                 create_from_installed(pkg, pkg, suf);
121             else
122                 create_from_installed_recursive(pkg, suf);
123         }
124         return TRUE;
125     }
126
127     get_dash_string(&Comment);
128     get_dash_string(&Desc);
129     if (!strcmp(Contents, "-"))
130         pkg_in = stdin;
131     else {
132         pkg_in = fopen(Contents, "r");
133         if (!pkg_in) {
134             cleanup(0);
135             errx(2, "%s: unable to open contents file '%s' for input",
136                 __func__, Contents);
137         }
138     }
139     plist.head = plist.tail = NULL;
140
141     /* Stick the dependencies, if any, at the top */
142     if (Pkgdeps) {
143         char **deps, *deporigin;
144         int i;
145         int ndeps = 0;
146
147         if (Verbose && !PlistOnly)
148             printf("Registering depends:");
149
150         /* Count number of dependencies */
151         for (cp = Pkgdeps; cp != NULL && *cp != '\0';
152                            cp = strpbrk(++cp, " \t\n")) {
153             ndeps++;
154         }
155
156         if (ndeps != 0) {
157             /* Create easy to use NULL-terminated list */
158             deps = alloca(sizeof(*deps) * ndeps + 1);
159             if (deps == NULL) {
160                 errx(2, "%s: alloca() failed", __func__);
161                 /* Not reached */
162             }
163             for (i = 0; Pkgdeps;) {
164                 cp = strsep(&Pkgdeps, " \t\n");
165                 if (*cp) {
166                     deps[i] = cp;
167                     i++;
168                 }
169             }
170             ndeps = i;
171             deps[ndeps] = NULL;
172
173             sortdeps(deps);
174             for (i = 0; i < ndeps; i++) {
175                 deporigin = strchr(deps[i], ':');
176                 if (deporigin != NULL) {
177                     *deporigin = '\0';
178                     add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin);
179                 }
180                 add_plist_top(&plist, PLIST_PKGDEP, deps[i]);
181                 if (Verbose && !PlistOnly)
182                     printf(" %s", deps[i]);
183             }
184         }
185
186         if (Verbose && !PlistOnly)
187             printf(".\n");
188     }
189
190     /* Put the conflicts directly after the dependencies, if any */
191     if (Conflicts) {
192         if (Verbose && !PlistOnly)
193             printf("Registering conflicts:");
194         while (Conflicts) {
195            cp = strsep(&Conflicts, " \t\n");
196            if (*cp) {
197                 add_plist(&plist, PLIST_CONFLICTS, cp);
198                 if (Verbose && !PlistOnly)
199                     printf(" %s", cp);
200            }
201         }
202         if (Verbose && !PlistOnly)
203             printf(".\n");
204     }
205
206     /* If a SrcDir override is set, add it now */
207     if (SrcDir) {
208         if (Verbose && !PlistOnly)
209             printf("Using SrcDir value of %s\n", SrcDir);
210         add_plist(&plist, PLIST_SRC, SrcDir);
211     }
212
213     /* Slurp in the packing list */
214     read_plist(&plist, pkg_in);
215
216     /* Prefix should add an @cwd to the packing list */
217     if (Prefix)
218         add_plist_top(&plist, PLIST_CWD, Prefix);
219
220     /* Add the origin if asked, at the top */
221     if (Origin)
222         add_plist_top(&plist, PLIST_ORIGIN, Origin);
223
224     /*
225      * Run down the list and see if we've named it, if not stick in a name
226      * at the top.
227      */
228     if (find_plist(&plist, PLIST_NAME) == NULL)
229         add_plist_top(&plist, PLIST_NAME, basename(pkg));
230
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__);
234     }
235     add_plist_top(&plist, PLIST_COMMENT, cp);
236     free(cp);
237
238     /*
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.
241      */
242     if (PlistOnly) {
243         check_list(home, &plist);
244         write_plist(&plist, stdout);
245         exit(0);
246     }
247
248     /* Make a directory to stomp around in */
249     home = make_playpen(PlayPen, 0);
250     signal(SIGINT, cleanup);
251     signal(SIGHUP, cleanup);
252
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.
258                          */
259     /* copy_plist(home, &plist); */
260     /* mark_plist(&plist); */
261
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);
272
273     if (Install) {
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);
278     }
279     if (PostInstall) {
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);
284     }
285     if (DeInstall) {
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);
290     }
291     if (PostDeInstall) {
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);
296     }
297     if (Require) {
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);
302     }
303     if (Display) {
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);
309     }
310     if (Mtree) {
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);
316     }
317
318     /* Finally, write out the packing list */
319     fp = fopen(CONTENTS_FNAME, "w");
320     if (!fp) {
321         cleanup(0);
322         errx(2, "%s: can't open file %s for writing",
323             __func__, CONTENTS_FNAME);
324     }
325     write_plist(&plist, fp);
326     if (fclose(fp)) {
327         cleanup(0);
328         errx(2, "%s: error while closing %s",
329             __func__, CONTENTS_FNAME);
330     }
331
332     /* And stick it into a tar ball */
333     make_dist(home, pkg, suf, &plist);
334
335     /* Cleanup */
336     free(Comment);
337     free(Desc);
338     free_plist(&plist);
339     leave_playpen();
340     return TRUE;        /* Success */
341 }
342
343 static void
344 make_dist(const char *homedir, const char *pkg, const char *suff, Package *plist)
345 {
346     struct stat sb;
347     char tball[FILENAME_MAX];
348     PackingList p;
349     int ret;
350     const char *args[50];       /* Much more than enough. */
351     int nargs = 0;
352     int pipefds[2];
353     FILE *totar;
354     pid_t pid;
355     const char *cname;
356     char *prefix = NULL;
357
358
359     args[nargs++] = "tar";      /* argv[0] */
360
361     if (*pkg == '/')
362         snprintf(tball, FILENAME_MAX, "%s.%s", pkg, suff);
363     else
364         snprintf(tball, FILENAME_MAX, "%s/%s.%s", homedir, pkg, suff);
365
366     /*
367      * If the package tarball exists already, and we are running in `no
368      * clobber' mode, skip this package.
369      */
370     if (stat(tball, &sb) == 0 && Regenerate == FALSE) {
371         if (Verbose)
372             printf("Skipping package '%s'.  It already exists.\n", tball);
373         return;
374     }
375
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";
382             cname = "bzip'd ";
383         }
384         else if (Zipper == XZ) {
385             args[nargs++] = "-J";
386             cname = "xz'd ";
387         }
388         else {
389             args[nargs++] = "-z";
390             cname = "gzip'd ";
391         }
392     } else {
393         cname = "";
394     }
395     if (Dereference)
396         args[nargs++] = "-h";
397     if (ExcludeFrom) {
398         args[nargs++] = "-X";
399         args[nargs++] = ExcludeFrom;
400     }
401     args[nargs++] = "-T";       /* Take filenames from file instead of args. */
402     args[nargs++] = "-";        /* Use stdin for the file. */
403     args[nargs] = NULL;
404
405     if (Verbose)
406         printf("Creating %star ball in '%s'\n", cname, tball);
407
408     /* Set up a pipe for passing the filenames, and fork off a tar process. */
409     if (pipe(pipefds) == -1) {
410         cleanup(0);
411         errx(2, "%s: cannot create pipe", __func__);
412     }
413     if ((pid = fork()) == -1) {
414         cleanup(0);
415         errx(2, "%s: cannot fork process for tar", __func__);
416     }
417     if (pid == 0) {     /* The child */
418         dup2(pipefds[0], 0);
419         close(pipefds[0]);
420         close(pipefds[1]);
421         execv("/usr/bin/tar", (char * const *)(uintptr_t)args);
422         cleanup(0);
423         errx(2, "%s: failed to execute tar command", __func__);
424     }
425
426     /* Meanwhile, back in the parent process ... */
427     close(pipefds[0]);
428     if ((totar = fdopen(pipefds[1], "w")) == NULL) {
429         cleanup(0);
430         errx(2, "%s: fdopen failed", __func__);
431     }
432
433     fprintf(totar, "%s\n", CONTENTS_FNAME);
434     fprintf(totar, "%s\n", COMMENT_FNAME);
435     fprintf(totar, "%s\n", DESC_FNAME);
436
437     if (Install)
438         fprintf(totar, "%s\n", INSTALL_FNAME);
439     if (PostInstall)
440         fprintf(totar, "%s\n", POST_INSTALL_FNAME);
441     if (DeInstall)
442         fprintf(totar, "%s\n", DEINSTALL_FNAME);
443     if (PostDeInstall)
444         fprintf(totar, "%s\n", POST_DEINSTALL_FNAME);
445     if (Require)
446         fprintf(totar, "%s\n", REQUIRE_FNAME);
447     if (Display)
448         fprintf(totar, "%s\n", DISPLAY_FNAME);
449     if (Mtree)
450         fprintf(totar, "%s\n", MTREE_FNAME);
451
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)
462              p = p->next;
463         if (p->type == PLIST_CWD && !prefix)
464             prefix = p->name;
465
466     }
467
468     fclose(totar);
469     wait(&ret);
470     /* assume either signal or bad exit is enough for us */
471     if (ret) {
472         cleanup(0);
473         errx(2, "%s: tar command failed with code %d", __func__, ret);
474     }
475 }
476
477 static void
478 sanity_check()
479 {
480     if (!Comment) {
481         cleanup(0);
482         errx(2, "%s: required package comment string is missing (-c comment)",
483             __func__);
484     }
485     if (!Desc) {
486         cleanup(0);
487         errx(2, "%s: required package description string is missing (-d desc)",
488             __func__);
489     }
490     if (!Contents) {
491         cleanup(0);
492         errx(2, "%s: required package contents list is missing (-f [-]file)",
493             __func__);
494     }
495 }
496
497
498 /* Clean up those things that would otherwise hang around */
499 void
500 cleanup(int sig)
501 {
502     int in_cleanup = 0;
503
504     if (!in_cleanup) {
505         in_cleanup = 1;
506         leave_playpen();
507     }
508     if (sig)
509         exit(1);
510 }
511
512 static int
513 create_from_installed_recursive(const char *pkg, const char *suf)
514 {
515     FILE *fp;
516     Package plist;
517     PackingList p;
518     char tmp[PATH_MAX];
519     int rval;
520
521     if (!create_from_installed(InstalledPkg, pkg, suf))
522         return FALSE;
523     snprintf(tmp, sizeof(tmp), "%s/%s/%s", LOG_DIR, InstalledPkg, CONTENTS_FNAME);
524     if (!fexists(tmp)) {
525         warnx("can't find package '%s' installed!", InstalledPkg);
526         return FALSE;
527     }
528     /* Suck in the contents list */
529     plist.head = plist.tail = NULL;
530     fp = fopen(tmp, "r");
531     if (!fp) {
532         warnx("unable to open %s file", tmp);
533         return FALSE;
534     }
535     read_plist(&plist, fp);
536     fclose(fp);
537     rval = TRUE;
538     for (p = plist.head; p ; p = p->next) {
539         if (p->type != PLIST_PKGDEP)
540             continue;
541         if (Verbose)
542             printf("Creating package %s\n", p->name);
543         if (!create_from_installed(p->name, p->name, suf)) {
544             rval = FALSE;
545             break;
546         }
547     }
548     free_plist(&plist);
549     return rval;
550 }
551
552 static int
553 create_from_installed(const char *ipkg, const char *pkg, const char *suf)
554 {
555     FILE *fp;
556     Package plist;
557     char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
558
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);
562         return FALSE;
563     }
564     getcwd(homedir, sizeof(homedir));
565     if (chdir(log_dir) == FAIL) {
566         warnx("can't change directory to '%s'!", log_dir);
567         return FALSE;
568     }
569     /* Suck in the contents list */
570     plist.head = plist.tail = NULL;
571     fp = fopen(CONTENTS_FNAME, "r");
572     if (!fp) {
573         warnx("unable to open %s file", CONTENTS_FNAME);
574         return FALSE;
575     }
576     read_plist(&plist, fp);
577     fclose(fp);
578
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;
588
589     make_dist(homedir, pkg, suf, &plist);
590
591     free_plist(&plist);
592     if (chdir(homedir) == FAIL) {
593         warnx("can't change directory to '%s'!", homedir);
594         return FALSE;
595     }
596     return TRUE;
597 }