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