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