extract.c revision 147381
1174199Srwatson/*
2224859Srwatson * FreeBSD install - a package for the installation and maintainance
3174199Srwatson * of non-core utilities.
4174199Srwatson *
5174199Srwatson * Redistribution and use in source and binary forms, with or without
6174199Srwatson * modification, are permitted provided that the following conditions
7174199Srwatson * are met:
8174199Srwatson * 1. Redistributions of source code must retain the above copyright
9174199Srwatson *    notice, this list of conditions and the following disclaimer.
10174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11174199Srwatson *    notice, this list of conditions and the following disclaimer in the
12174199Srwatson *    documentation and/or other materials provided with the distribution.
13174199Srwatson *
14174199Srwatson * Jordan K. Hubbard
15174199Srwatson * 18 July 1993
16174199Srwatson *
17174199Srwatson * This is the package extraction code for the add module.
18174199Srwatson *
19174199Srwatson */
20174199Srwatson
21174199Srwatson#include <sys/cdefs.h>
22174199Srwatson__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/extract.c 147381 2005-06-14 15:05:43Z krion $");
23174199Srwatson
24174199Srwatson#include <ctype.h>
25174199Srwatson#include <err.h>
26174199Srwatson#include "lib.h"
27174199Srwatson#include "add.h"
28174199Srwatson
29186567Srwatson
30174199Srwatson#define STARTSTRING "/usr/bin/tar cf -"
31174199Srwatson#define TOOBIG(str) \
32174199Srwatson    (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) ||\
33174199Srwatson	((int)strlen(str) + FILENAME_MAX + perm_count > maxargs))
34221807Sstas
35195853Sbrooks#define PUSHOUT(todir) /* push out string */ \
36174199Srwatson    if (where_count > (int)sizeof(STARTSTRING)-1) { \
37195853Sbrooks	strcat(where_args, "|/usr/bin/tar --unlink -xpf - -C "); \
38174199Srwatson	strcat(where_args, todir); \
39174199Srwatson	if (system(where_args)) { \
40174199Srwatson	    cleanup(0); \
41249673Strociny	    errx(2, "%s: can not invoke %ld byte tar pipeline: %s", \
42249673Strociny		 __func__, (long)strlen(where_args), where_args); \
43232182Strociny	} \
44174199Srwatson	strcpy(where_args, STARTSTRING); \
45249671Strociny	where_count = sizeof(STARTSTRING)-1; \
46174199Srwatson    } \
47249671Strociny    if (perm_count) { \
48249671Strociny	apply_perms(todir, perm_args); \
49174199Srwatson	perm_args[0] = 0;\
50174199Srwatson	perm_count = 0; \
51232182Strociny    }
52232182Strociny
53232182Strocinystatic void
54174199Srwatsonrollback(const char *name, const char *home, PackingList start, PackingList stop)
55221807Sstas{
56174530Srwatson    PackingList q;
57174199Srwatson    char try[FILENAME_MAX], bup[FILENAME_MAX];
58174199Srwatson    const char *dir;
59174199Srwatson
60174199Srwatson    dir = home;
61174199Srwatson    for (q = start; q != stop; q = q->next) {
62174199Srwatson	if (q->type == PLIST_FILE) {
63249673Strociny	    snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
64224859Srwatson	    if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
65224859Srwatson		(void)chflags(try, 0);
66195853Sbrooks		(void)unlink(try);
67249671Strociny		if (rename(bup, try))
68195853Sbrooks		    warnx("rollback: unable to rename %s back to %s", bup, try);
69195853Sbrooks	    }
70249671Strociny	}
71249671Strociny	else if (q->type == PLIST_CWD) {
72195853Sbrooks	    if (strcmp(q->name, "."))
73249671Strociny		dir = q->name;
74249671Strociny	    else
75195853Sbrooks		dir = home;
76195853Sbrooks	}
77195853Sbrooks    }
78195853Sbrooks}
79195853Sbrooks
80195853Sbrooks#define add_char(buf, len, pos, ch) do {\
81195853Sbrooks    if ((pos) < (len)) { \
82249671Strociny        buf[(pos)] = (ch); \
83195853Sbrooks        buf[(pos) + 1] = '\0'; \
84174199Srwatson    } \
85174199Srwatson    ++(pos); \
86232182Strociny} while (0)
87232182Strociny
88249673Strocinystatic int
89232182Strocinyadd_arg(char *buf, int len, const char *str)
90232182Strociny{
91232182Strociny    int i = 0;
92232182Strociny
93249673Strociny    add_char(buf, len, i, ' ');
94232182Strociny    for (; *str != '\0'; ++str) {
95232182Strociny	if (!isalnum(*str) && *str != '/' && *str != '.' && *str != '-')
96232182Strociny	    add_char(buf, len, i, '\\');
97232182Strociny	add_char(buf, len, i, *str);
98232182Strociny    }
99232182Strociny    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: /* FALLTHROUGH */
265	case PLIST_NOINST:
266	    break;
267
268	case PLIST_IGNORE:
269	    p = p->next;
270	    break;
271
272	default:
273	    break;
274	}
275	p = p->next;
276    }
277    PUSHOUT(Directory);
278}
279