2 * FreeBSD install - a package for the installation and maintainance
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;
113 int maxargs, where_count = 0, perm_count = 0, add_count;
116 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */
117 where_args = alloca(maxargs);
120 errx(2, "%s: can't get argument list space", __func__);
122 perm_args = alloca(maxargs);
125 errx(2, "%s: can't get argument list space", __func__);
128 strcpy(where_args, STARTSTRING);
129 where_count = sizeof(STARTSTRING)-1;
133 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
135 /* Reset the world */
140 Directory = (char *)home;
144 char cmd[FILENAME_MAX];
150 printf("extract: Package name is %s\n", p->name);
156 printf("extract: %s/%s\n", Directory, p->name);
158 char try[FILENAME_MAX];
160 /* first try to rename it into place */
161 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
163 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */
164 if (preserve && PkgName) {
165 char pf[FILENAME_MAX];
167 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
168 if (rename(try, pf)) {
170 "unable to back up %s to %s, aborting pkg_add",
172 rollback(PkgName, home, pkg->head, p);
178 if (rename(p->name, try) == 0) {
179 /* try to add to list of perms to be changed and run in bulk. */
180 if (p->name[0] == '/' || TOOBIG(p->name)) {
183 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
184 if (add_count < 0 || add_count >= maxargs - perm_count) {
186 errx(2, "%s: oops, miscounted strings!", __func__);
188 perm_count += add_count;
191 /* rename failed, try copying with a big tar command */
192 if (last_chdir != Directory) {
193 if (last_chdir == NULL) {
198 last_chdir = Directory;
200 else if (p->name[0] == '/' || TOOBIG(p->name)) {
203 add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name);
204 if (add_count < 0 || add_count >= maxargs - where_count) {
206 errx(2, "%s: oops, miscounted strings!", __func__);
208 where_count += add_count;
209 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
210 if (add_count < 0 || add_count >= maxargs - perm_count) {
212 errx(2, "%s: oops, miscounted strings!", __func__);
214 perm_count += add_count;
223 p->name = strdup(prefix);
225 printf("extract: CWD to %s\n", p->name);
227 if (strcmp(p->name, ".")) {
228 if (!Fake && make_hierarchy(p->name) == FAIL) {
230 errx(2, "%s: unable to cwd to '%s'", __func__, p->name);
235 Directory = (char *)home;
239 if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
240 strstr(p->name, "%f")) && last_file == NULL) {
242 errx(2, "%s: no last file specified for '%s' command",
245 if (strstr(p->name, "%D") && Directory == NULL) {
247 errx(2, "%s: no directory specified for '%s' command",
250 format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file);
253 printf("extract: execute '%s'\n", cmd);
254 if (!Fake && system(cmd))
255 warnx("command '%s' failed", cmd);
273 case PLIST_COMMENT: /* FALLTHROUGH */