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