]> 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 r154258,
[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[] = "hvIRfnrp: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
40 char    *Mode           = NULL;
41 char    *Owner          = NULL;
42 char    *Group          = NULL;
43 char    *PkgName        = NULL;
44 char    *PkgAddCmd      = NULL;
45 char    *Directory      = NULL;
46 char    FirstPen[FILENAME_MAX];
47 add_mode_t AddMode      = NORMAL;
48
49 #define MAX_PKGS        200
50 char    pkgnames[MAX_PKGS][MAXPATHLEN];
51 char    *pkgs[MAX_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         { 600000, 600099, "/packages-6.0-release" },
77         { 300000, 399000, "/packages-3-stable" },
78         { 400000, 499000, "/packages-4-stable" },
79         { 502100, 502128, "/packages-5-current" },
80         { 503100, 599000, "/packages-5-stable" },
81         { 600100, 699000, "/packages-6-stable" },
82         { 700000, 799000, "/packages-7-current" },
83         { 0, 9999999, "/packages-current" },
84         { 0, 0, NULL }
85 };
86
87 static char *getpackagesite(void);
88 int getosreldate(void);
89
90 static void usage __P((void));
91
92 int
93 main(int argc, char **argv)
94 {
95     int ch, error;
96     char **start;
97     char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
98     static char temppackageroot[MAXPATHLEN];
99     static char pkgaddpath[MAXPATHLEN];
100
101     if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
102         PkgAddCmd = realpath(argv[0], pkgaddpath);
103     else
104         PkgAddCmd = argv[0];
105
106     start = argv;
107     while ((ch = getopt(argc, argv, Options)) != -1) {
108         switch(ch) {
109         case 'v':
110             Verbose = TRUE;
111             break;
112
113         case 'p':
114             Prefix = optarg;
115             PrefixRecursive = FALSE;
116             break;
117
118         case 'P':
119             Prefix = optarg;
120             PrefixRecursive = TRUE;
121             break;
122
123         case 'I':
124             NoInstall = TRUE;
125             break;
126
127         case 'R':
128             NoRecord = TRUE;
129             break;
130
131         case 'f':
132             Force = TRUE;
133             break;
134
135         case 'K':
136             KeepPackage = TRUE;
137             break;
138
139         case 'n':
140             Fake = TRUE;
141             break;
142
143         case 'r':
144             Remote = TRUE;
145             break;
146
147         case 't':
148             if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
149                 errx(1, "-t Argument too long.");
150             break;
151
152         case 'S':
153             AddMode = SLAVE;
154             break;
155
156         case 'M':
157             AddMode = MASTER;
158             break;
159
160         case 'C':
161             Chroot = optarg;
162             break;
163
164         case 'h':
165         case '?':
166         default:
167             usage();
168             break;
169         }
170     }
171     argc -= optind;
172     argv += optind;
173
174     if (argc > MAX_PKGS) {
175         errx(1, "too many packages (max %d)", MAX_PKGS);
176     }
177
178     if (AddMode != SLAVE) {
179         for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
180
181         /* Get all the remaining package names, if any */
182         for (ch = 0; *argv; ch++, argv++) {
183             if (Remote) {
184                 if ((packagesite = getpackagesite()) == NULL)
185                     errx(1, "package name too long");
186                 if (strlcpy(temppackageroot, packagesite,
187                     sizeof(temppackageroot)) >= sizeof(temppackageroot))
188                     errx(1, "package name too long");
189                 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
190                     >= sizeof(temppackageroot))
191                     errx(1, "package name too long");
192                 remotepkg = temppackageroot;
193                 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 
194                         (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
195                         !ptr[4]))
196                     if (strlcat(remotepkg,
197 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
198                         ".tbz",
199 #else
200                         ".tgz",
201 #endif
202                         sizeof(temppackageroot)) >= sizeof(temppackageroot))
203                         errx(1, "package name too long");
204             }
205             if (!strcmp(*argv, "-"))    /* stdin? */
206                 pkgs[ch] = (char *)"-";
207             else if (isURL(*argv)) {    /* preserve URLs */
208                 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
209                     >= sizeof(pkgnames[ch]))
210                     errx(1, "package name too long");
211                 pkgs[ch] = pkgnames[ch];
212             }
213             else if ((Remote) && isURL(remotepkg)) {
214                 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
215                     >= sizeof(pkgnames[ch]))
216                     errx(1, "package name too long");
217                 pkgs[ch] = pkgnames[ch];
218             } else {                    /* expand all pathnames to fullnames */
219                 if (fexists(*argv)) /* refers to a file directly */
220                     pkgs[ch] = realpath(*argv, pkgnames[ch]);
221                 else {          /* look for the file in the expected places */
222                     if (!(cp = fileFindByPath(NULL, *argv))) {
223                         /* let pkg_do() fail later, so that error is reported */
224                         if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
225                             >= sizeof(pkgnames[ch]))
226                             errx(1, "package name too long");
227                         pkgs[ch] = pkgnames[ch];
228                     } else {
229                         if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
230                             >= sizeof(pkgnames[ch]))
231                             errx(1, "package name too long");
232                         pkgs[ch] = pkgnames[ch];
233                     }
234                 }
235             }
236             if (packagesite != NULL)
237                 packagesite[0] = '\0';
238         }
239     }
240     /* If no packages, yelp */
241     else if (!ch) {
242         warnx("missing package name(s)");
243         usage();
244     }
245     else if (ch > 1 && AddMode == MASTER) {
246         warnx("only one package name may be specified with master mode");
247         usage();
248     }
249     /* Perform chroot if requested */
250     if (Chroot != NULL) {
251         if (chroot(Chroot))
252             errx(1, "chroot to %s failed", Chroot);
253     }
254     /* Make sure the sub-execs we invoke get found */
255     setenv("PATH", 
256            "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
257            1);
258
259     /* Set a reasonable umask */
260     umask(022);
261
262     if ((error = pkg_perform(pkgs)) != 0) {
263         if (Verbose)
264             warnx("%d package addition(s) failed", error);
265         return error;
266     }
267     else
268         return 0;
269 }
270
271 static char *
272 getpackagesite(void)
273 {
274     int reldate, i;
275     static char sitepath[MAXPATHLEN];
276     struct utsname u;
277
278     if (getenv("PACKAGESITE")) {
279         if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
280             >= sizeof(sitepath))
281             return NULL;
282         return sitepath;
283     }
284
285     if (getenv("PACKAGEROOT")) {
286         if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
287             >= sizeof(sitepath))
288             return NULL;
289     } else {
290         if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
291             >= sizeof(sitepath))
292             return NULL;
293     }
294
295     if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
296         >= sizeof(sitepath))
297         return NULL;
298
299     uname(&u);
300     if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
301         return NULL;
302
303     reldate = getosreldate();
304     for(i = 0; releases[i].directory != NULL; i++) {
305         if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
306             if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
307                 >= sizeof(sitepath))
308                 return NULL;
309             break;
310         }
311     }
312
313     if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
314         return NULL;
315
316     return sitepath;
317
318 }
319
320 static void
321 usage()
322 {
323     fprintf(stderr, "%s\n%s\n",
324         "usage: pkg_add [-vInrfRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
325         "               pkg-name [pkg-name ...]");
326     exit(1);
327 }