extract.c revision 93520
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 93520 2002-04-01 09:39:07Z obrien $");
23
24#include <err.h>
25#include "lib.h"
26#include "add.h"
27
28
29#define STARTSTRING "tar cf - "
30#define TOOBIG(str) (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) \
31		|| ((int)strlen(str) + FILENAME_MAX + perm_count > maxargs))
32
33#define PUSHOUT(todir) /* push out string */ \
34        if (where_count > (int)sizeof(STARTSTRING)-1) { \
35		    strcat(where_args, "|tar --unlink -xf - -C "); \
36		    strcat(where_args, todir); \
37		    if (system(where_args)) { \
38	                cleanup(0); \
39		        errx(2, __FUNCTION__ ": can not invoke %ld byte tar pipeline: %s", \
40			     (long)strlen(where_args), where_args); \
41		    } \
42		    strcpy(where_args, STARTSTRING); \
43		    where_count = sizeof(STARTSTRING)-1; \
44	} \
45	if (perm_count) { \
46		    apply_perms(todir, perm_args); \
47		    perm_args[0] = 0;\
48		    perm_count = 0; \
49	}
50
51static void
52rollback(const char *name, const char *home, PackingList start, PackingList stop)
53{
54    PackingList q;
55    char try[FILENAME_MAX], bup[FILENAME_MAX];
56    const char *dir;
57
58    dir = home;
59    for (q = start; q != stop; q = q->next) {
60	if (q->type == PLIST_FILE) {
61	    snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
62	    if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
63		(void)chflags(try, 0);
64		(void)unlink(try);
65		if (rename(bup, try))
66		    warnx("rollback: unable to rename %s back to %s", bup, try);
67	    }
68	}
69	else if (q->type == PLIST_CWD) {
70	    if (strcmp(q->name, "."))
71		dir = q->name;
72	    else
73		dir = home;
74	}
75    }
76}
77
78void
79extract_plist(const char *home, Package *pkg)
80{
81    PackingList p = pkg->head;
82    char *last_file;
83    char *where_args, *perm_args, *last_chdir;
84    int maxargs, where_count = 0, perm_count = 0, add_count;
85    Boolean preserve;
86
87    maxargs = sysconf(_SC_ARG_MAX) / 2;	/* Just use half the argument space */
88    where_args = alloca(maxargs);
89    if (!where_args) {
90	cleanup(0);
91	errx(2, __FUNCTION__ ": can't get argument list space");
92    }
93    perm_args = alloca(maxargs);
94    if (!perm_args) {
95	cleanup(0);
96	errx(2, __FUNCTION__ ": can't get argument list space");
97    }
98
99    strcpy(where_args, STARTSTRING);
100    where_count = sizeof(STARTSTRING)-1;
101    perm_args[0] = 0;
102
103    last_chdir = 0;
104    preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
105
106    /* Reset the world */
107    Owner = NULL;
108    Group = NULL;
109    Mode = NULL;
110    last_file = NULL;
111    (const char *)Directory = home;
112
113    /* Do it */
114    while (p) {
115	char cmd[FILENAME_MAX];
116
117	switch(p->type) {
118	case PLIST_NAME:
119	    PkgName = p->name;
120	    if (Verbose)
121		printf("extract: Package name is %s\n", p->name);
122	    break;
123
124	case PLIST_FILE:
125	    last_file = p->name;
126	    if (Verbose)
127		printf("extract: %s/%s\n", Directory, p->name);
128	    if (!Fake) {
129		char try[FILENAME_MAX];
130
131		if (strrchr(p->name,'\'')) {
132		  cleanup(0);
133		  errx(2, __FUNCTION__ ": Bogus filename \"%s\"", p->name);
134		}
135
136		/* first try to rename it into place */
137		snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
138		if (fexists(try)) {
139		    (void)chflags(try, 0);	/* XXX hack - if truly immutable, rename fails */
140		    if (preserve && PkgName) {
141			char pf[FILENAME_MAX];
142
143			if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
144			    if (rename(try, pf)) {
145				warnx(
146				"unable to back up %s to %s, aborting pkg_add",
147				try, pf);
148				rollback(PkgName, home, pkg->head, p);
149				return;
150			    }
151			}
152		    }
153		}
154		if (rename(p->name, try) == 0) {
155		    /* try to add to list of perms to be changed and run in bulk. */
156		    if (p->name[0] == '/' || TOOBIG(p->name)) {
157			PUSHOUT(Directory);
158		    }
159		    add_count = snprintf(&perm_args[perm_count], maxargs - perm_count, "'%s' ", p->name);
160		    if (add_count < 0 || add_count > maxargs - perm_count) {
161			cleanup(0);
162			errx(2, __FUNCTION__ ": oops, miscounted strings!");
163		    }
164		    perm_count += add_count;
165		}
166		else {
167		    /* rename failed, try copying with a big tar command */
168		    if (last_chdir != Directory) {
169			if (last_chdir == NULL) {
170			    PUSHOUT(Directory);
171			} else {
172			    PUSHOUT(last_chdir);
173			}
174			last_chdir = Directory;
175		    }
176		    else if (p->name[0] == '/' || TOOBIG(p->name)) {
177			PUSHOUT(Directory);
178		    }
179		    add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name);
180		    if (add_count < 0 || add_count > maxargs - where_count) {
181			cleanup(0);
182			errx(2, __FUNCTION__ ": oops, miscounted strings!");
183		    }
184		    where_count += add_count;
185		    add_count = snprintf(&perm_args[perm_count],
186					 maxargs - perm_count,
187					 "'%s' ", p->name);
188		    if (add_count < 0 || add_count > maxargs - perm_count) {
189			cleanup(0);
190			errx(2, __FUNCTION__ ": oops, miscounted strings!");
191		    }
192		    perm_count += add_count;
193		}
194	    }
195	    break;
196
197	case PLIST_CWD:
198	    if (Verbose)
199		printf("extract: CWD to %s\n", p->name);
200	    PUSHOUT(Directory);
201	    if (strcmp(p->name, ".")) {
202		if (!Fake && make_hierarchy(p->name) == FAIL) {
203		    cleanup(0);
204		    errx(2, __FUNCTION__ ": unable to cwd to '%s'", p->name);
205		}
206		Directory = p->name;
207	    }
208	    else
209		(const char *)Directory = home;
210	    break;
211
212	case PLIST_CMD:
213	    if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
214		 strstr(p->name, "%f")) && last_file == NULL) {
215		cleanup(0);
216		errx(2, __FUNCTION__ ": no last file specified for '%s' command", p->name);
217	    }
218	    if (strstr(p->name, "%D") && Directory == NULL) {
219		cleanup(0);
220		errx(2, __FUNCTION__ ": no directory specified for '%s' command", p->name);
221	    }
222	    format_cmd(cmd, p->name, Directory, last_file);
223	    PUSHOUT(Directory);
224	    if (Verbose)
225		printf("extract: execute '%s'\n", cmd);
226	    if (!Fake && system(cmd))
227		warnx("command '%s' failed", cmd);
228	    break;
229
230	case PLIST_CHMOD:
231	    PUSHOUT(Directory);
232	    Mode = p->name;
233	    break;
234
235	case PLIST_CHOWN:
236	    PUSHOUT(Directory);
237	    Owner = p->name;
238	    break;
239
240	case PLIST_CHGRP:
241	    PUSHOUT(Directory);
242	    Group = p->name;
243	    break;
244
245	case PLIST_COMMENT:
246	    break;
247
248	case PLIST_IGNORE:
249	    p = p->next;
250	    break;
251
252	default:
253	    break;
254	}
255	p = p->next;
256    }
257    PUSHOUT(Directory);
258}
259