Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / usr.sbin / pkg_install / add / extract.c
blob732a13fea441e0184deb92ee5d4111d1f4f65bac
1 /*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
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.
14 * Jordan K. Hubbard
15 * 18 July 1993
17 * This is the package extraction code for the add module.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include <ctype.h>
25 #include <err.h>
26 #include "lib.h"
27 #include "add.h"
30 #define STARTSTRING "/usr/bin/tar cf -"
31 #define TOOBIG(str) \
32 (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) ||\
33 ((int)strlen(str) + FILENAME_MAX + perm_count > maxargs))
35 #define PUSHOUT(todir) /* push out string */ \
36 if (where_count > (int)sizeof(STARTSTRING)-1) { \
37 strcat(where_args, "|/usr/bin/tar --unlink -xpPf - -C "); \
38 strcat(where_args, todir); \
39 if (system(where_args)) { \
40 cleanup(0); \
41 errx(2, "%s: can not invoke %ld byte tar pipeline: %s", \
42 __func__, (long)strlen(where_args), where_args); \
43 } \
44 strcpy(where_args, STARTSTRING); \
45 where_count = sizeof(STARTSTRING)-1; \
46 } \
47 if (perm_count) { \
48 apply_perms(todir, perm_args); \
49 perm_args[0] = 0;\
50 perm_count = 0; \
53 static void
54 rollback(const char *name, const char *home, PackingList start, PackingList stop)
56 PackingList q;
57 char try[FILENAME_MAX], bup[FILENAME_MAX];
58 const char *dir;
59 char *prefix = NULL;
61 dir = home;
62 for (q = start; q != stop; q = q->next) {
63 if (q->type == PLIST_FILE) {
64 snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
65 if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
66 (void)chflags(try, 0);
67 (void)unlink(try);
68 if (rename(bup, try))
69 warnx("rollback: unable to rename %s back to %s", bup, try);
72 else if (q->type == PLIST_CWD) {
73 if (!prefix)
74 prefix = q->name;
75 if (q->name == NULL)
76 q->name = prefix;
77 else if (strcmp(q->name, "."))
78 dir = q->name;
79 else
80 dir = home;
85 #define add_char(buf, len, pos, ch) do {\
86 if ((pos) < (len)) { \
87 buf[(pos)] = (ch); \
88 buf[(pos) + 1] = '\0'; \
89 } \
90 ++(pos); \
91 } while (0)
93 static int
94 add_arg(char *buf, int len, const char *str)
96 int i = 0;
98 add_char(buf, len, i, ' ');
99 for (; *str != '\0'; ++str) {
100 if (!isalnum(*str) && *str != '/' && *str != '.' && *str != '-')
101 add_char(buf, len, i, '\\');
102 add_char(buf, len, i, *str);
104 return (i);
107 void
108 extract_plist(const char *home, Package *pkg)
110 PackingList p = pkg->head;
111 char *last_file, *prefix = NULL;
112 char *where_args, *perm_args, *last_chdir;
113 int maxargs, where_count = 0, perm_count = 0, add_count;
114 Boolean preserve;
116 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */
117 where_args = alloca(maxargs);
118 if (!where_args) {
119 cleanup(0);
120 errx(2, "%s: can't get argument list space", __func__);
122 perm_args = alloca(maxargs);
123 if (!perm_args) {
124 cleanup(0);
125 errx(2, "%s: can't get argument list space", __func__);
128 strcpy(where_args, STARTSTRING);
129 where_count = sizeof(STARTSTRING)-1;
130 perm_args[0] = 0;
132 last_chdir = 0;
133 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
135 /* Reset the world */
136 Owner = NULL;
137 Group = NULL;
138 Mode = NULL;
139 last_file = NULL;
140 Directory = (char *)home;
142 /* Do it */
143 while (p) {
144 char cmd[FILENAME_MAX];
146 switch(p->type) {
147 case PLIST_NAME:
148 PkgName = p->name;
149 if (Verbose)
150 printf("extract: Package name is %s\n", p->name);
151 break;
153 case PLIST_FILE:
154 last_file = p->name;
155 if (Verbose)
156 printf("extract: %s/%s\n", Directory, p->name);
157 if (!Fake) {
158 char try[FILENAME_MAX];
160 /* first try to rename it into place */
161 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
162 if (fexists(try)) {
163 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */
164 if (preserve && PkgName) {
165 char pf[FILENAME_MAX];
167 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
168 if (rename(try, pf)) {
169 warnx(
170 "unable to back up %s to %s, aborting pkg_add",
171 try, pf);
172 rollback(PkgName, home, pkg->head, p);
173 return;
178 if (rename(p->name, try) == 0) {
179 /* try to add to list of perms to be changed and run in bulk. */
180 if (p->name[0] == '/' || TOOBIG(p->name)) {
181 PUSHOUT(Directory);
183 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
184 if (add_count < 0 || add_count >= maxargs - perm_count) {
185 cleanup(0);
186 errx(2, "%s: oops, miscounted strings!", __func__);
188 perm_count += add_count;
190 else {
191 /* rename failed, try copying with a big tar command */
192 if (last_chdir != Directory) {
193 if (last_chdir == NULL) {
194 PUSHOUT(Directory);
195 } else {
196 PUSHOUT(last_chdir);
198 last_chdir = Directory;
200 else if (p->name[0] == '/' || TOOBIG(p->name)) {
201 PUSHOUT(Directory);
203 add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name);
204 if (add_count < 0 || add_count >= maxargs - where_count) {
205 cleanup(0);
206 errx(2, "%s: oops, miscounted strings!", __func__);
208 where_count += add_count;
209 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
210 if (add_count < 0 || add_count >= maxargs - perm_count) {
211 cleanup(0);
212 errx(2, "%s: oops, miscounted strings!", __func__);
214 perm_count += add_count;
217 break;
219 case PLIST_CWD:
220 if (!prefix)
221 prefix = p->name;
222 if (p->name == NULL)
223 p->name = strdup(prefix);
224 if (Verbose)
225 printf("extract: CWD to %s\n", p->name);
226 PUSHOUT(Directory);
227 if (strcmp(p->name, ".")) {
228 if (!Fake && make_hierarchy(p->name) == FAIL) {
229 cleanup(0);
230 errx(2, "%s: unable to cwd to '%s'", __func__, p->name);
232 Directory = p->name;
234 else
235 Directory = (char *)home;
236 break;
238 case PLIST_CMD:
239 if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
240 strstr(p->name, "%f")) && last_file == NULL) {
241 cleanup(0);
242 errx(2, "%s: no last file specified for '%s' command",
243 __func__, p->name);
245 if (strstr(p->name, "%D") && Directory == NULL) {
246 cleanup(0);
247 errx(2, "%s: no directory specified for '%s' command",
248 __func__, p->name);
250 format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file);
251 PUSHOUT(Directory);
252 if (Verbose)
253 printf("extract: execute '%s'\n", cmd);
254 if (!Fake && system(cmd))
255 warnx("command '%s' failed", cmd);
256 break;
258 case PLIST_CHMOD:
259 PUSHOUT(Directory);
260 Mode = p->name;
261 break;
263 case PLIST_CHOWN:
264 PUSHOUT(Directory);
265 Owner = p->name;
266 break;
268 case PLIST_CHGRP:
269 PUSHOUT(Directory);
270 Group = p->name;
271 break;
273 case PLIST_COMMENT: /* FALLTHROUGH */
274 case PLIST_NOINST:
275 break;
277 case PLIST_IGNORE:
278 p = p->next;
279 break;
281 default:
282 break;
284 p = p->next;
286 PUSHOUT(Directory);