extract.c revision 39068
1174199Srwatson#ifndef lint
2224859Srwatsonstatic const char rcsid[] =
3174199Srwatson	"$Id: extract.c,v 1.20 1998/07/04 14:13:01 jkh Exp $";
4174199Srwatson#endif
5174199Srwatson
6174199Srwatson/*
7174199Srwatson * FreeBSD install - a package for the installation and maintainance
8174199Srwatson * of non-core utilities.
9174199Srwatson *
10174199Srwatson * Redistribution and use in source and binary forms, with or without
11174199Srwatson * modification, are permitted provided that the following conditions
12174199Srwatson * are met:
13174199Srwatson * 1. Redistributions of source code must retain the above copyright
14174199Srwatson *    notice, this list of conditions and the following disclaimer.
15174199Srwatson * 2. Redistributions in binary form must reproduce the above copyright
16174199Srwatson *    notice, this list of conditions and the following disclaimer in the
17174199Srwatson *    documentation and/or other materials provided with the distribution.
18174199Srwatson *
19174199Srwatson * Jordan K. Hubbard
20174199Srwatson * 18 July 1993
21174199Srwatson *
22174199Srwatson * This is the package extraction code for the add module.
23174199Srwatson *
24174199Srwatson */
25174199Srwatson
26174199Srwatson#include <err.h>
27174199Srwatson#include "lib.h"
28174199Srwatson#include "add.h"
29186567Srwatson
30174199Srwatson
31174199Srwatson#define STARTSTRING "tar cf - "
32174199Srwatson#define TOOBIG(str) ((strlen(str) + 22 + strlen(home) + where_count > maxargs) \
33174199Srwatson		|| (strlen(str) + 6 + strlen(home) + perm_count > maxargs))
34221807Sstas
35195853Sbrooks#define PUSHOUT(todir) /* push out string */ \
36174199Srwatson        if (where_count > sizeof(STARTSTRING)-1) { \
37195853Sbrooks		    strcat(where_args, "|tar xf - -C "); \
38174199Srwatson		    strcat(where_args, todir); \
39174199Srwatson		    if (system(where_args)) { \
40174199Srwatson	                cleanup(0); \
41249673Strociny		        errx(2, "can not invoke %d byte tar pipeline: %s", \
42249673Strociny			     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(char *name, char *home, PackingList start, PackingList stop)
55221807Sstas{
56174530Srwatson    PackingList q;
57174199Srwatson    char try[FILENAME_MAX], bup[FILENAME_MAX], *dir;
58174199Srwatson
59174199Srwatson    dir = home;
60174199Srwatson    for (q = start; q != stop; q = q->next) {
61174199Srwatson	if (q->type == PLIST_FILE) {
62174199Srwatson	    snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
63249673Strociny	    if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
64224859Srwatson		(void)chflags(try, 0);
65224859Srwatson		(void)unlink(try);
66195853Sbrooks		if (rename(bup, try))
67249671Strociny		    warnx("rollback: unable to rename %s back to %s", bup, try);
68195853Sbrooks	    }
69195853Sbrooks	}
70249671Strociny	else if (q->type == PLIST_CWD) {
71249671Strociny	    if (strcmp(q->name, "."))
72195853Sbrooks		dir = q->name;
73249671Strociny	    else
74249671Strociny		dir = home;
75195853Sbrooks	}
76195853Sbrooks    }
77195853Sbrooks}
78195853Sbrooks
79195853Sbrooksvoid
80195853Sbrooksextract_plist(char *home, Package *pkg)
81195853Sbrooks{
82249671Strociny    PackingList p = pkg->head;
83195853Sbrooks    char *last_file;
84174199Srwatson    char *where_args, *perm_args, *last_chdir;
85174199Srwatson    int maxargs, where_count = 0, perm_count = 0, add_count;
86232182Strociny    Boolean preserve;
87232182Strociny
88249673Strociny    maxargs = sysconf(_SC_ARG_MAX) / 2;	/* Just use half the argument space */
89232182Strociny    where_args = alloca(maxargs);
90232182Strociny    if (!where_args) {
91232182Strociny	cleanup(0);
92232182Strociny	errx(2, "can't get argument list space");
93249673Strociny    }
94232182Strociny    perm_args = alloca(maxargs);
95232182Strociny    if (!perm_args) {
96232182Strociny	cleanup(0);
97232182Strociny	errx(2, "can't get argument list space");
98232182Strociny    }
99232182Strociny
100    strcpy(where_args, STARTSTRING);
101    where_count = sizeof(STARTSTRING)-1;
102    perm_args[0] = 0;
103
104    last_chdir = 0;
105    preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
106
107    /* Reset the world */
108    Owner = NULL;
109    Group = NULL;
110    Mode = NULL;
111    last_file = NULL;
112    Directory = home;
113
114    /* Do it */
115    while (p) {
116	char cmd[FILENAME_MAX];
117
118	switch(p->type) {
119	case PLIST_NAME:
120	    PkgName = p->name;
121	    if (Verbose)
122		printf("extract: Package name is %s\n", p->name);
123	    break;
124
125	case PLIST_FILE:
126	    last_file = p->name;
127	    if (Verbose)
128		printf("extract: %s/%s\n", Directory, p->name);
129	    if (!Fake) {
130		char try[FILENAME_MAX];
131
132		if (strrchr(p->name,'\'')) {
133		  cleanup(0);
134		  errx(2, "Bogus filename \"%s\"", p->name);
135		}
136
137		/* first try to rename it into place */
138		snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
139		if (fexists(try)) {
140		    (void)chflags(try, 0);	/* XXX hack - if truly immutable, rename fails */
141		    if (preserve && PkgName) {
142			char pf[FILENAME_MAX];
143
144			if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
145			    if (rename(try, pf)) {
146				warnx(
147				"unable to back up %s to %s, aborting pkg_add",
148				try, pf);
149				rollback(PkgName, home, pkg->head, p);
150				return;
151			    }
152			}
153		    }
154		}
155		if (rename(p->name, try) == 0) {
156		    /* try to add to list of perms to be changed and run in bulk. */
157		    if (p->name[0] == '/' || TOOBIG(p->name)) {
158			PUSHOUT(Directory);
159		    }
160		    add_count = snprintf(&perm_args[perm_count], maxargs - perm_count, "'%s' ", p->name);
161		    if (add_count > maxargs - perm_count) {
162			cleanup(0);
163			errx(2, "oops, miscounted strings!");
164		    }
165		    perm_count += add_count;
166		}
167		else {
168		    /* rename failed, try copying with a big tar command */
169		    if (last_chdir != Directory) {
170			if (last_chdir == NULL) {
171			    PUSHOUT(Directory);
172			} else {
173			    PUSHOUT(last_chdir);
174			}
175			last_chdir = Directory;
176		    }
177		    else if (p->name[0] == '/' || TOOBIG(p->name)) {
178			PUSHOUT(Directory);
179		    }
180		    add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name);
181		    if (add_count > maxargs - where_count) {
182			cleanup(0);
183			errx(2, "oops, miscounted strings!");
184		    }
185		    where_count += add_count;
186		    add_count = snprintf(&perm_args[perm_count],
187					 maxargs - perm_count,
188					 "'%s' ", p->name);
189		    if (add_count > maxargs - perm_count) {
190			cleanup(0);
191			errx(2, "oops, miscounted strings!");
192		    }
193		    perm_count += add_count;
194		}
195	    }
196	    break;
197
198	case PLIST_CWD:
199	    if (Verbose)
200		printf("extract: CWD to %s\n", p->name);
201	    PUSHOUT(Directory);
202	    if (strcmp(p->name, ".")) {
203		if (!Fake && make_hierarchy(p->name) == FAIL) {
204		    cleanup(0);
205		    errx(2, "unable to make directory '%s'", p->name);
206		}
207		Directory = p->name;
208	    }
209	    else
210		Directory = home;
211	    break;
212
213	case PLIST_CMD:
214	    if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
215		 strstr(p->name, "%f")) && last_file == NULL) {
216		cleanup(0);
217		errx(2, "no last file specified for '%s' command", p->name);
218	    }
219	    if (strstr(p->name, "%D") && Directory == NULL) {
220		cleanup(0);
221		errx(2, "no directory specified for '%s' command", p->name);
222	    }
223	    format_cmd(cmd, p->name, Directory, last_file);
224	    PUSHOUT(Directory);
225	    if (Verbose)
226		printf("extract: execute '%s'\n", cmd);
227	    if (!Fake && system(cmd))
228		warnx("command '%s' failed", cmd);
229	    break;
230
231	case PLIST_CHMOD:
232	    PUSHOUT(Directory);
233	    Mode = p->name;
234	    break;
235
236	case PLIST_CHOWN:
237	    PUSHOUT(Directory);
238	    Owner = p->name;
239	    break;
240
241	case PLIST_CHGRP:
242	    PUSHOUT(Directory);
243	    Group = p->name;
244	    break;
245
246	case PLIST_COMMENT:
247	    break;
248
249	case PLIST_IGNORE:
250	    p = p->next;
251	    break;
252	}
253	p = p->next;
254    }
255    PUSHOUT(Directory);
256}
257