2 * FreeBSD install - a package for the installation and maintenance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
17 * This is the package extraction code for the add module.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
30 #define STARTSTRING "/usr/bin/tar cf -"
32 (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) ||\
33 ((int)strlen(str) + FILENAME_MAX + perm_count > maxargs))
35 #define PUSHOUT(todir) /* push out string */ \
36 if (where_count > (int)sizeof(STARTSTRING)-1) { \
37 strcat(where_args, "|/usr/bin/tar --unlink -xpPf - -C "); \
38 strcat(where_args, todir); \
39 if (system(where_args)) { \
41 errx(2, "%s: can not invoke %ld byte tar pipeline: %s", \
42 __func__, (long)strlen(where_args), where_args); \
44 strcpy(where_args, STARTSTRING); \
45 where_count = sizeof(STARTSTRING)-1; \
48 apply_perms(todir, perm_args); \
54 rollback(const char *name, const char *home, PackingList start, PackingList stop)
57 char try[FILENAME_MAX], bup[FILENAME_MAX];
62 for (q = start; q != stop; q = q->next) {
63 if (q->type == PLIST_FILE) {
64 snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name);
65 if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) {
66 (void)chflags(try, 0);
69 warnx("rollback: unable to rename %s back to %s", bup, try);
72 else if (q->type == PLIST_CWD) {
77 else if (strcmp(q->name, "."))
85 #define add_char(buf, len, pos, ch) do {\
86 if ((pos) < (len)) { \
88 buf[(pos) + 1] = '\0'; \
94 add_arg(char *buf, int len, const char *str)
98 add_char(buf, len, i, ' ');
99 for (; *str != '\0'; ++str) {
100 if (!isalnum(*str) && *str != '/' && *str != '.' && *str != '-')
101 add_char(buf, len, i, '\\');
102 add_char(buf, len, i, *str);
108 extract_plist(const char *home, Package *pkg)
110 PackingList p = pkg->head;
111 char *last_file, *prefix = NULL;
112 char *where_args, *perm_args, *last_chdir;
114 int where_count = 0, perm_count = 0, add_count;
117 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */
118 where_args = alloca(maxargs);
121 errx(2, "%s: can't get argument list space", __func__);
123 perm_args = alloca(maxargs);
126 errx(2, "%s: can't get argument list space", __func__);
129 strcpy(where_args, STARTSTRING);
130 where_count = sizeof(STARTSTRING)-1;
134 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
136 /* Reset the world */
141 Directory = (char *)home;
145 char cmd[FILENAME_MAX];
151 printf("extract: Package name is %s\n", p->name);
157 printf("extract: %s/%s\n", Directory, p->name);
159 char try[FILENAME_MAX];
161 /* first try to rename it into place */
162 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
164 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */
165 if (preserve && PkgName) {
166 char pf[FILENAME_MAX];
168 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
169 if (rename(try, pf)) {
171 "unable to back up %s to %s, aborting pkg_add",
173 rollback(PkgName, home, pkg->head, p);
179 if (rename(p->name, try) == 0) {
180 /* try to add to list of perms to be changed and run in bulk. */
181 if (p->name[0] == '/' || TOOBIG(p->name)) {
184 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
185 if (add_count < 0 || add_count >= maxargs - perm_count) {
187 errx(2, "%s: oops, miscounted strings!", __func__);
189 perm_count += add_count;
192 /* rename failed, try copying with a big tar command */
193 if (last_chdir != Directory) {
194 if (last_chdir == NULL) {
199 last_chdir = Directory;
201 else if (p->name[0] == '/' || TOOBIG(p->name)) {
204 add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name);
205 if (add_count < 0 || add_count >= maxargs - where_count) {
207 errx(2, "%s: oops, miscounted strings!", __func__);
209 where_count += add_count;
210 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
211 if (add_count < 0 || add_count >= maxargs - perm_count) {
213 errx(2, "%s: oops, miscounted strings!", __func__);
215 perm_count += add_count;
224 p->name = strdup(prefix);
226 printf("extract: CWD to %s\n", p->name);
228 if (strcmp(p->name, ".")) {
229 if (!Fake && make_hierarchy(p->name, TRUE) == FAIL) {
231 errx(2, "%s: unable to cwd to '%s'", __func__, p->name);
236 Directory = (char *)home;
240 if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
241 strstr(p->name, "%f")) && last_file == NULL) {
243 errx(2, "%s: no last file specified for '%s' command",
246 if (strstr(p->name, "%D") && Directory == NULL) {
248 errx(2, "%s: no directory specified for '%s' command",
251 format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file);
254 printf("extract: execute '%s'\n", cmd);
255 if (!Fake && system(cmd))
256 warnx("command '%s' failed", cmd);
274 case PLIST_COMMENT: /* FALLTHROUGH */