Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.25.2.6 2002/08/31 18:45:32 obrien Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/add/Attic/extract.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include <err.h>
24 #include "lib.h"
25 #include "add.h"
26
27
28 #define STARTSTRING "tar cf - "
29 #define TOOBIG(str) (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) \
30                 || ((int)strlen(str) + FILENAME_MAX + perm_count > maxargs))
31
32 #define PUSHOUT(todir) /* push out string */ \
33         if (where_count > (int)sizeof(STARTSTRING)-1) { \
34                     strcat(where_args, "|tar --unlink -xpf - -C "); \
35                     strcat(where_args, todir); \
36                     if (system(where_args)) { \
37                         cleanup(0); \
38                         errx(2, \
39                             "%s: can not invoke %ld byte tar pipeline: %s", \
40                              __func__, \
41                              (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
59     dir = home;
60     for (q = start; q != stop; q = q->next) {
61         if (q->type == PLIST_FILE) {
62             snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
63             if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
64                 (void)chflags(try, 0);
65                 (void)unlink(try);
66                 if (rename(bup, try))
67                     warnx("rollback: unable to rename %s back to %s", bup, try);
68             }
69         }
70         else if (q->type == PLIST_CWD) {
71             if (strcmp(q->name, "."))
72                 dir = q->name;
73             else
74                 dir = home;
75         }
76     }
77 }
78
79 void
80 extract_plist(const char *home, Package *pkg)
81 {
82     PackingList p = pkg->head;
83     char *last_file;
84     char *where_args, *perm_args, *last_chdir;
85     int maxargs, where_count = 0, perm_count = 0, add_count;
86     Boolean preserve;
87
88     maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */
89     where_args = alloca(maxargs);
90     if (!where_args) {
91         cleanup(0);
92         errx(2, "%s: can't get argument list space", __func__);
93     }
94     perm_args = alloca(maxargs);
95     if (!perm_args) {
96         cleanup(0);
97         errx(2, "%s: can't get argument list space", __func__);
98     }
99
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     (const char *)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, "%s: Bogus filename \"%s\"", __func__, 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 < 0 || add_count > maxargs - perm_count) {
162                         cleanup(0);
163                         errx(2, "%s: oops, miscounted strings!", __func__);
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 < 0 || add_count > maxargs - where_count) {
182                         cleanup(0);
183                         errx(2, "%s: oops, miscounted strings!", __func__);
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 < 0 || add_count > maxargs - perm_count) {
190                         cleanup(0);
191                         errx(2, "%s: oops, miscounted strings!", __func__);
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, "%s: unable to cwd to '%s'", __func__, p->name);
206                 }
207                 Directory = p->name;
208             }
209             else
210                 (const char *)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, "%s: no last file specified for '%s' command",
218                     __func__, p->name);
219             }
220             if (strstr(p->name, "%D") && Directory == NULL) {
221                 cleanup(0);
222                 errx(2, "%s: no directory specified for '%s' command",
223                     __func__, p->name);
224             }
225             format_cmd(cmd, p->name, Directory, last_file);
226             PUSHOUT(Directory);
227             if (Verbose)
228                 printf("extract: execute '%s'\n", cmd);
229             if (!Fake && system(cmd))
230                 warnx("command '%s' failed", cmd);
231             break;
232
233         case PLIST_CHMOD:
234             PUSHOUT(Directory);
235             Mode = p->name;
236             break;
237
238         case PLIST_CHOWN:
239             PUSHOUT(Directory);
240             Owner = p->name;
241             break;
242
243         case PLIST_CHGRP:
244             PUSHOUT(Directory);
245             Group = p->name;
246             break;
247
248         case PLIST_COMMENT:
249             break;
250
251         case PLIST_IGNORE:
252             p = p->next;
253             break;
254
255         default:
256             break;
257         }
258         p = p->next;
259     }
260     PUSHOUT(Directory);
261 }