Create defaults/uuids and adjust the build to copy the file to /etc/defaults
[dragonfly/vkernel-mp.git] / usr.sbin / pkg_install / add / main.c
blob0e061cb9d08008fee82202dc994758d7aa272fca
1 /*
3 * FreeBSD install - a package for the installation and maintainance
4 * of non-core utilities.
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.
15 * Jordan K. Hubbard
16 * 18 July 1993
18 * This is the add module.
20 * $FreeBSD: src/usr.sbin/pkg_install/add/main.c,v 1.60 2004/10/17 14:36:30 obrien Exp $
21 * $DragonFly: src/usr.sbin/pkg_install/add/main.c,v 1.11 2005/08/12 17:57:22 drhodus Exp $
24 #include <err.h>
25 #include <sys/param.h>
26 #include <sys/utsname.h>
27 #include "lib.h"
28 #include "add.h"
30 static char Options[] = "hvIRfnrp:SMt:C:";
32 char *Prefix = NULL;
33 char *Chroot = NULL;
34 Boolean NoInstall = FALSE;
35 Boolean NoRecord = FALSE;
36 Boolean Remote = FALSE;
38 char *Mode = NULL;
39 char *Owner = NULL;
40 char *Group = NULL;
41 char *PkgName = NULL;
42 char *PkgAddCmd = NULL;
43 char *Directory = NULL;
44 char FirstPen[FILENAME_MAX];
45 add_mode_t AddMode = NORMAL;
47 #define MAX_PKGS 200
48 char pkgnames[MAX_PKGS][MAXPATHLEN];
49 char *pkgs[MAX_PKGS];
51 struct {
52 int lowver; /* Lowest version number to match */
53 int hiver; /* Highest version number to match */
54 const char *directory; /* Directory it lives in */
55 } releases[] = {
56 { 410000, 410000, "/packages-4.1-release" },
57 { 420000, 420000, "/packages-4.2-release" },
58 { 430000, 430000, "/packages-4.3-release" },
59 { 440000, 440000, "/packages-4.4-release" },
60 { 450000, 450000, "/packages-4.5-release" },
61 { 460000, 460001, "/packages-4.6-release" },
62 { 460002, 460099, "/packages-4.6.2-release" },
63 { 470000, 470099, "/packages-4.7-release" },
64 { 480000, 480099, "/packages-4.8-release" },
65 { 490000, 490099, "/packages-4.9-release" },
66 { 491000, 491099, "/packages-4.10-release" },
67 { 492000, 492099, "/packages-4.11-release" },
68 { 500000, 500099, "/packages-5.0-release" },
69 { 501000, 501099, "/packages-5.1-release" },
70 { 502000, 502009, "/packages-5.2-release" },
71 { 502010, 502099, "/packages-5.2.1-release" },
72 { 503000, 503099, "/packages-5.3-release" },
73 { 300000, 399000, "/packages-3-stable" },
74 { 400000, 499000, "/packages-4-stable" },
75 { 502100, 502128, "/packages-5-current" },
76 { 503000, 599000, "/packages-5-stable" },
77 { 600000, 699000, "/packages-6-current" },
78 { 0, 9999999, "/" },
79 { 0, 0, NULL }
82 static char *getpackagesite(void);
83 int getosreldate(void);
85 static void usage(void);
87 int
88 main(int argc, char **argv)
90 int ch, error;
91 char **start;
92 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
93 static char temppackageroot[MAXPATHLEN];
94 static char pkgaddpath[MAXPATHLEN];
96 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
97 PkgAddCmd = realpath(argv[0], pkgaddpath);
98 else
99 PkgAddCmd = argv[0];
101 start = argv;
102 while ((ch = getopt(argc, argv, Options)) != -1) {
103 switch(ch) {
104 case 'v':
105 Verbose = TRUE;
106 break;
108 case 'p':
109 Prefix = optarg;
110 break;
112 case 'I':
113 NoInstall = TRUE;
114 break;
116 case 'R':
117 NoRecord = TRUE;
118 break;
120 case 'f':
121 Force = TRUE;
122 break;
124 case 'n':
125 Fake = TRUE;
126 Verbose = TRUE;
127 break;
129 case 'r':
130 Remote = TRUE;
131 break;
133 case 't':
134 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
135 errx(1, "-t Argument too long.");
136 break;
138 case 'S':
139 AddMode = SLAVE;
140 break;
142 case 'M':
143 AddMode = MASTER;
144 break;
146 case 'C':
147 Chroot = optarg;
148 break;
150 case 'h':
151 case '?':
152 default:
153 usage();
154 break;
157 argc -= optind;
158 argv += optind;
160 if (argc > MAX_PKGS) {
161 errx(1, "too many packages (max %d)", MAX_PKGS);
164 if (AddMode != SLAVE) {
165 for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
167 /* Get all the remaining package names, if any */
168 for (ch = 0; *argv; ch++, argv++) {
169 if (Remote) {
170 if ((packagesite = getpackagesite()) == NULL)
171 errx(1, "package name too long");
172 if (strlcpy(temppackageroot, packagesite,
173 sizeof(temppackageroot)) >= sizeof(temppackageroot))
174 errx(1, "package name too long");
175 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
176 >= sizeof(temppackageroot))
177 errx(1, "package name too long");
178 remotepkg = temppackageroot;
179 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
180 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
181 !ptr[4]))
182 if (strlcat(remotepkg,
183 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
184 ".tbz",
185 #else
186 ".tgz",
187 #endif
188 sizeof(temppackageroot)) >= sizeof(temppackageroot))
189 errx(1, "package name too long");
191 if (!strcmp(*argv, "-")) /* stdin? */
192 pkgs[ch] = (char *)"-";
193 else if (isURL(*argv)) { /* preserve URLs */
194 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
195 >= sizeof(pkgnames[ch]))
196 errx(1, "package name too long");
197 pkgs[ch] = pkgnames[ch];
199 else if ((Remote) && isURL(remotepkg)) {
200 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
201 >= sizeof(pkgnames[ch]))
202 errx(1, "package name too long");
203 pkgs[ch] = pkgnames[ch];
204 } else { /* expand all pathnames to fullnames */
205 if (fexists(*argv)) /* refers to a file directly */
206 pkgs[ch] = realpath(*argv, pkgnames[ch]);
207 else { /* look for the file in the expected places */
208 if (!(cp = fileFindByPath(NULL, *argv))) {
209 /* let pkg_do() fail later, so that error is reported */
210 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
211 >= sizeof(pkgnames[ch]))
212 errx(1, "package name too long");
213 pkgs[ch] = pkgnames[ch];
214 } else {
215 if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
216 >= sizeof(pkgnames[ch]))
217 errx(1, "package name too long");
218 pkgs[ch] = pkgnames[ch];
222 if (packagesite != NULL)
223 packagesite[0] = '\0';
226 /* If no packages, yelp */
227 else if (!ch) {
228 warnx("missing package name(s)");
229 usage();
231 else if (ch > 1 && AddMode == MASTER) {
232 warnx("only one package name may be specified with master mode");
233 usage();
235 /* Perform chroot if requested */
236 if (Chroot != NULL) {
237 if (chroot(Chroot))
238 errx(1, "chroot to %s failed", Chroot);
240 /* Make sure the sub-execs we invoke get found */
241 setenv("PATH",
242 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
245 /* Set a reasonable umask */
246 umask(022);
248 if ((error = pkg_perform(pkgs)) != 0) {
249 if (Verbose)
250 warnx("%d package addition(s) failed", error);
251 return error;
253 else
254 return 0;
257 static char *
258 getpackagesite(void)
260 int reldate, i;
261 static char sitepath[MAXPATHLEN];
262 struct utsname u;
264 if (getenv("PACKAGESITE")) {
265 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
266 >= sizeof(sitepath))
267 return NULL;
268 return sitepath;
271 if (getenv("PACKAGEROOT")) {
272 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
273 >= sizeof(sitepath))
274 return NULL;
275 } else {
276 if (strlcat(sitepath, "http://fireflybsd.com", sizeof(sitepath))
277 >= sizeof(sitepath))
278 return NULL;
281 if (strlcat(sitepath, "/packages", sizeof(sitepath))
282 >= sizeof(sitepath))
283 return NULL;
285 #if 0
286 uname(&u);
287 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
288 return NULL;
289 #endif
291 reldate = getosreldate();
292 for(i = 0; releases[i].directory != NULL; i++) {
293 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
294 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
295 >= sizeof(sitepath))
296 return NULL;
297 break;
301 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
302 return NULL;
304 return sitepath;
308 static void
309 usage()
311 fprintf(stderr, "%s\n%s\n",
312 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix] [-C chrootdir]",
313 " pkg-name [pkg-name ...]");
314 exit(1);