]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/pkg_install/add/main.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / pkg_install / add / main.c
1 /*
2  *
3  * FreeBSD install - a package for the installation and maintainance
4  * of non-core utilities.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * Jordan K. Hubbard
16  * 18 July 1993
17  *
18  * This is the add module.
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include <sys/param.h>
25 #include <sys/utsname.h>
26 #include <err.h>
27 #include <getopt.h>
28
29 #include "lib.h"
30 #include "add.h"
31
32 char    *Prefix         = NULL;
33 Boolean PrefixRecursive = FALSE;
34 char    *Chroot         = NULL;
35 Boolean NoInstall       = FALSE;
36 Boolean NoRecord        = FALSE;
37 Boolean Remote          = FALSE;
38 Boolean KeepPackage     = FALSE;
39 Boolean FailOnAlreadyInstalled  = TRUE;
40 Boolean IgnoreDeps      = FALSE;
41
42 char    *Mode           = NULL;
43 char    *Owner          = NULL;
44 char    *Group          = NULL;
45 char    *PkgName        = NULL;
46 char    *PkgAddCmd      = NULL;
47 char    *Directory      = NULL;
48 char    FirstPen[FILENAME_MAX];
49 add_mode_t AddMode      = NORMAL;
50
51 char    **pkgs;
52
53 struct {
54         int lowver;     /* Lowest version number to match */
55         int hiver;      /* Highest version number to match */
56         const char *directory;  /* Directory it lives in */
57 } releases[] = {
58         { 410000, 410000, "/packages-4.1-release" },
59         { 420000, 420000, "/packages-4.2-release" },
60         { 430000, 430000, "/packages-4.3-release" },
61         { 440000, 440000, "/packages-4.4-release" },
62         { 450000, 450000, "/packages-4.5-release" },
63         { 460000, 460001, "/packages-4.6-release" },
64         { 460002, 460099, "/packages-4.6.2-release" },
65         { 470000, 470099, "/packages-4.7-release" },
66         { 480000, 480099, "/packages-4.8-release" },
67         { 490000, 490099, "/packages-4.9-release" },
68         { 491000, 491099, "/packages-4.10-release" },
69         { 492000, 492099, "/packages-4.11-release" },
70         { 500000, 500099, "/packages-5.0-release" },
71         { 501000, 501099, "/packages-5.1-release" },
72         { 502000, 502009, "/packages-5.2-release" },
73         { 502010, 502099, "/packages-5.2.1-release" },
74         { 503000, 503099, "/packages-5.3-release" },
75         { 504000, 504099, "/packages-5.4-release" },
76         { 505000, 505099, "/packages-5.5-release" },
77         { 600000, 600099, "/packages-6.0-release" },
78         { 601000, 601099, "/packages-6.1-release" },
79         { 602000, 602099, "/packages-6.2-release" },
80         { 603000, 603099, "/packages-6.3-release" },
81         { 700000, 700099, "/packages-7.0-release" },
82         { 701000, 701099, "/packages-7.1-release" },
83         { 300000, 399000, "/packages-3-stable" },
84         { 400000, 499000, "/packages-4-stable" },
85         { 502100, 502128, "/packages-5-current" },
86         { 503100, 599000, "/packages-5-stable" },
87         { 600100, 699000, "/packages-6-stable" },
88         { 700100, 799000, "/packages-7-stable" },
89         { 800000, 899000, "/packages-8-current" },
90         { 0, 9999999, "/packages-current" },
91         { 0, 0, NULL }
92 };
93
94 static char *getpackagesite(void);
95 int getosreldate(void);
96
97 static void usage(void);
98
99 static char opts[] = "hviIRfFnrp:P:SMt:C:K";
100 static struct option longopts[] = {
101         { "chroot",     required_argument,      NULL,           'C' },
102         { "dry-run",    no_argument,            NULL,           'n' },
103         { "force",      no_argument,            NULL,           'f' },
104         { "help",       no_argument,            NULL,           'h' },
105         { "keep",       no_argument,            NULL,           'K' },
106         { "master",     no_argument,            NULL,           'M' },
107         { "no-deps",    no_argument,            NULL,           'i' },
108         { "no-record",  no_argument,            NULL,           'R' },
109         { "no-script",  no_argument,            NULL,           'I' },
110         { "prefix",     required_argument,      NULL,           'p' },
111         { "remote",     no_argument,            NULL,           'r' },
112         { "template",   required_argument,      NULL,           't' },
113         { "slave",      no_argument,            NULL,           'S' },
114         { "verbose",    no_argument,            NULL,           'v' },
115         { NULL,         0,                      NULL,           0 }
116 };
117
118 int
119 main(int argc, char **argv)
120 {
121     int ch, error;
122     char **start;
123     char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
124     static char temppackageroot[MAXPATHLEN];
125     static char pkgaddpath[MAXPATHLEN];
126
127     if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
128         PkgAddCmd = realpath(argv[0], pkgaddpath);
129     else
130         PkgAddCmd = argv[0];
131
132     start = argv;
133     while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
134         switch(ch) {
135         case 'v':
136             Verbose++;
137             break;
138
139         case 'p':
140             Prefix = optarg;
141             PrefixRecursive = FALSE;
142             break;
143
144         case 'P':
145             Prefix = optarg;
146             PrefixRecursive = TRUE;
147             break;
148
149         case 'I':
150             NoInstall = TRUE;
151             break;
152
153         case 'R':
154             NoRecord = TRUE;
155             break;
156
157         case 'f':
158             Force = TRUE;
159             break;
160
161         case 'F':
162             FailOnAlreadyInstalled = FALSE;
163             break;
164
165         case 'K':
166             KeepPackage = TRUE;
167             break;
168
169         case 'n':
170             Fake = TRUE;
171             break;
172
173         case 'r':
174             Remote = TRUE;
175             break;
176
177         case 't':
178             if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
179                 errx(1, "-t Argument too long.");
180             break;
181
182         case 'S':
183             AddMode = SLAVE;
184             break;
185
186         case 'M':
187             AddMode = MASTER;
188             break;
189
190         case 'C':
191             Chroot = optarg;
192             break;
193
194         case 'i':
195             IgnoreDeps = TRUE;
196             break;
197
198         case 'h':
199         default:
200             usage();
201             break;
202         }
203     }
204     argc -= optind;
205     argv += optind;
206
207     if (AddMode != SLAVE) {
208         pkgs = (char **)malloc((argc+1) * sizeof(char *));
209         for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ;
210
211         /* Get all the remaining package names, if any */
212         for (ch = 0; *argv; ch++, argv++) {
213             char temp[MAXPATHLEN];
214             if (Remote) {
215                 if ((packagesite = getpackagesite()) == NULL)
216                     errx(1, "package name too long");
217                 if (strlcpy(temppackageroot, packagesite,
218                     sizeof(temppackageroot)) >= sizeof(temppackageroot))
219                     errx(1, "package name too long");
220                 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
221                     >= sizeof(temppackageroot))
222                     errx(1, "package name too long");
223                 remotepkg = temppackageroot;
224                 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 
225                         (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
226                         !ptr[4]))
227                     if (strlcat(remotepkg, ".tbz",
228                         sizeof(temppackageroot)) >= sizeof(temppackageroot))
229                         errx(1, "package name too long");
230             }
231             if (!strcmp(*argv, "-"))    /* stdin? */
232                 pkgs[ch] = (char *)"-";
233             else if (isURL(*argv)) {    /* preserve URLs */
234                 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
235                     errx(1, "package name too long");
236                 pkgs[ch] = strdup(temp);
237             }
238             else if ((Remote) && isURL(remotepkg)) {
239                 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp))
240                     errx(1, "package name too long");
241                 pkgs[ch] = strdup(temp);
242             } else {                    /* expand all pathnames to fullnames */
243                 if (fexists(*argv)) /* refers to a file directly */
244                     pkgs[ch] = strdup(realpath(*argv, temp));
245                 else {          /* look for the file in the expected places */
246                     if (!(cp = fileFindByPath(NULL, *argv))) {
247                         /* let pkg_do() fail later, so that error is reported */
248                         if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
249                             errx(1, "package name too long");
250                         pkgs[ch] = strdup(temp);
251                     } else {
252                         if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp))
253                             errx(1, "package name too long");
254                         pkgs[ch] = strdup(temp);
255                     }
256                 }
257             }
258             if (packagesite != NULL)
259                 packagesite[0] = '\0';
260         }
261     }
262     /* If no packages, yelp */
263     if (!ch) {
264         warnx("missing package name(s)");
265         usage();
266     }
267     else if (ch > 1 && AddMode == MASTER) {
268         warnx("only one package name may be specified with master mode");
269         usage();
270     }
271     /* Perform chroot if requested */
272     if (Chroot != NULL) {
273         if (chroot(Chroot))
274             errx(1, "chroot to %s failed", Chroot);
275     }
276     /* Make sure the sub-execs we invoke get found */
277     setenv("PATH", 
278            "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
279            1);
280
281     /* Set a reasonable umask */
282     umask(022);
283
284     if ((error = pkg_perform(pkgs)) != 0) {
285         if (Verbose)
286             warnx("%d package addition(s) failed", error);
287         return error;
288     }
289     else
290         return 0;
291 }
292
293 static char *
294 getpackagesite(void)
295 {
296     int reldate, i;
297     static char sitepath[MAXPATHLEN];
298     struct utsname u;
299
300     if (getenv("PACKAGESITE")) {
301         if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
302             >= sizeof(sitepath))
303             return NULL;
304         return sitepath;
305     }
306
307     if (getenv("PACKAGEROOT")) {
308         if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
309             >= sizeof(sitepath))
310             return NULL;
311     } else {
312         if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
313             >= sizeof(sitepath))
314             return NULL;
315     }
316
317     if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
318         >= sizeof(sitepath))
319         return NULL;
320
321     uname(&u);
322     if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
323         return NULL;
324
325     reldate = getosreldate();
326     for(i = 0; releases[i].directory != NULL; i++) {
327         if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
328             if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
329                 >= sizeof(sitepath))
330                 return NULL;
331             break;
332         }
333     }
334
335     if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
336         return NULL;
337
338     return sitepath;
339
340 }
341
342 static void
343 usage()
344 {
345     fprintf(stderr, "%s\n%s\n",
346         "usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
347         "               pkg-name [pkg-name ...]");
348     exit(1);
349 }