extract.c revision 132789
1/*
2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
4 *
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.
13 *
14 * Jordan K. Hubbard
15 * 18 July 1993
16 *
17 * This is the package extraction code for the add module.
18 *
19 */
20
21#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/extract.c 132789 2004-07-28 07:19:15Z kan $");
23
24#include <ctype.h>
25#include <err.h>
26#include "lib.h"
27#include "add.h"
28
29
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))
34
35#define PUSHOUT(todir) /* push out string */ \
36    if (where_count > (int)sizeof(STARTSTRING)-1) { \
37	strcat(where_args, "|/usr/bin/tar --unlink -xpf - -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; \
51    }
52
53static void
54rollback(const char *name, const char *home, PackingList start, PackingList stop)
55{
56    PackingList q;
57    char try[FILENAME_MAX], bup[FILENAME_MAX];
58    const char *dir;
59
60    dir = home;
61    for (q = start; q != stop; q = q->next) {
62	if (q->type == PLIST_FILE) {
63	    snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
64	    if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
65		(void)chflags(try, 0);
66		(void)unlink(try);
67		if (rename(bup, try))
68		    warnx("rollback: unable to rename %s back to %s", bup, try);
69	    }
70	}
71	else if (q->type == PLIST_CWD) {
72	    if (strcmp(q->name, "."))
73		dir = q->name;
74	    else
75		dir = home;
76	}
77    }
78}
79
80#define add_char(buf, len, pos, ch) do {\
81    if ((pos) < (len)) { \
82        buf[(pos)] = (ch); \
83        buf[(pos) + 1] = '\0'; \
84    } \
85    ++(pos); \
86} while (0)
87
88static int
89add_arg(char *buf, int len, const char *str)
90{
91    int i = 0;
92
93    add_char(buf, len, i, ' ');
94    for (; *str != '\0'; ++str) {
95	if (!isalnum(*str) && *str != '/' && *str != '.' && *str != '-')
96	    add_char(buf, len, i, '\\');
97	add_char(buf, len, i, *str);
98    }
99    return (i);
100}
101
102void
103extract_plist(const char *home, Package *pkg)
104{
105    PackingList p = pkg->head;
106    char *last_file;
107    char *where_args, *perm_args, *last_chdir;
108    int maxargs, where_count = 0, perm_count = 0, add_count;
109    Boolean preserve;
110
111    maxargs = sysconf(_SC_ARG_MAX) / 2;	/* Just use half the argument space */
112    where_args = alloca(maxargs);
113    if (!where_args) {
114	cleanup(0);
115	errx(2, "%s: can't get argument list space", __func__);
116    }
117    perm_args = alloca(maxargs);
118    if (!perm_args) {
119	cleanup(0);
120	errx(2, "%s: can't get argument list space", __func__);
121    }
122
123    strcpy(where_args, STARTSTRING);
124    where_count = sizeof(STARTSTRING)-1;
125    perm_args[0] = 0;
126
127    last_chdir = 0;
128    preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
129
130    /* Reset the world */
131    Owner = NULL;
132    Group = NULL;
133    Mode = NULL;
134    last_file = NULL;
135    Directory = (char *)home;
136
137    /* Do it */
138    while (p) {
139	char cmd[FILENAME_MAX];
140
141	switch(p->type) {
142	case PLIST_NAME:
143	    PkgName = p->name;
144	    if (Verbose)
145		printf("extract: Package name is %s\n", p->name);
146	    break;
147
148	case PLIST_FILE:
149	    last_file = p->name;
150	    if (Verbose)
151		printf("extract: %s/%s\n", Directory, p->name);
152	    if (!Fake) {
153		char try[FILENAME_MAX];
154
155		/* first try to rename it into place */
156		snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
157		if (fexists(try)) {
158		    (void)chflags(try, 0);	/* XXX hack - if truly immutable, rename fails */
159		    if (preserve && PkgName) {
160			char pf[FILENAME_MAX];
161
162			if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
163			    if (rename(try, pf)) {
164				warnx(
165				"unable to back up %s to %s, aborting pkg_add",
166				try, pf);
167				rollback(PkgName, home, pkg->head, p);
168				return;
169			    }
170			}
171		    }
172		}
173		if (rename(p->name, try) == 0) {
174		    /* try to add to list of perms to be changed and run in bulk. */
175		    if (p->name[0] == '/' || TOOBIG(p->name)) {
176			PUSHOUT(Directory);
177		    }
178		    add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
179		    if (add_count < 0 || add_count >= maxargs - perm_count) {
180			cleanup(0);
181			errx(2, "%s: oops, miscounted strings!", __func__);
182		    }
183		    perm_count += add_count;
184		}
185		else {
186		    /* rename failed, try copying with a big tar command */
187		    if (last_chdir != Directory) {
188			if (last_chdir == NULL) {
189			    PUSHOUT(Directory);
190			} else {
191			    PUSHOUT(last_chdir);
192			}
193			last_chdir = Directory;
194		    }
195		    else if (p->name[0] == '/' || TOOBIG(p->name)) {
196			PUSHOUT(Directory);
197		    }
198		    add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name);
199		    if (add_count < 0 || add_count >= maxargs - where_count) {
200			cleanup(0);
201			errx(2, "%s: oops, miscounted strings!", __func__);
202		    }
203		    where_count += add_count;
204		    add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
205		    if (add_count < 0 || add_count >= maxargs - perm_count) {
206			cleanup(0);
207			errx(2, "%s: oops, miscounted strings!", __func__);
208		    }
209		    perm_count += add_count;
210		}
211	    }
212	    break;
213
214	case PLIST_CWD:
215	    if (Verbose)
216		printf("extract: CWD to %s\n", p->name);
217	    PUSHOUT(Directory);
218	    if (strcmp(p->name, ".")) {
219		if (!Fake && make_hierarchy(p->name) == FAIL) {
220		    cleanup(0);
221		    errx(2, "%s: unable to cwd to '%s'", __func__, p->name);
222		}
223		Directory = p->name;
224	    }
225	    else
226		Directory = (char *)home;
227	    break;
228
229	case PLIST_CMD:
230	    if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
231		 strstr(p->name, "%f")) && last_file == NULL) {
232		cleanup(0);
233		errx(2, "%s: no last file specified for '%s' command",
234		    __func__, p->name);
235	    }
236	    if (strstr(p->name, "%D") && Directory == NULL) {
237		cleanup(0);
238		errx(2, "%s: no directory specified for '%s' command",
239		    __func__, p->name);
240	    }
241	    format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file);
242	    PUSHOUT(Directory);
243	    if (Verbose)
244		printf("extract: execute '%s'\n", cmd);
245	    if (!Fake && system(cmd))
246		warnx("command '%s' failed", cmd);
247	    break;
248
249	case PLIST_CHMOD:
250	    PUSHOUT(Directory);
251	    Mode = p->name;
252	    break;
253
254	case PLIST_CHOWN:
255	    PUSHOUT(Directory);
256	    Owner = p->name;
257	    break;
258
259	case PLIST_CHGRP:
260	    PUSHOUT(Directory);
261	    Group = p->name;
262	    break;
263
264	case PLIST_COMMENT:
265	    break;
266
267	case PLIST_IGNORE:
268	    p = p->next;
269	    break;
270
271	default:
272	    break;
273	}
274	p = p->next;
275    }
276    PUSHOUT(Directory);
277}
278