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 -xpf - -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];
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 (void)chflags(try, 0);
68 warnx("rollback: unable to rename %s back to %s", bup, try);
71 else if (q->type == PLIST_CWD) {
72 if (strcmp(q->name, "."))
80 #define add_char(buf, len, pos, ch) do {\
81 if ((pos) < (len)) { \
83 buf[(pos) + 1] = '\0'; \
89 add_arg(char *buf, int len, const char *str)
93 add_char(buf, len, i, ' ');
94 for (; *str != '\0'; ++str) {
95 if (!isalnum(*str) && *str != '/' && *str != '.' && *str != '-')
96 add_char(buf, len, i, '\\');
97 add_char(buf, len, i, *str);
103 extract_plist(const char *home, Package *pkg)
105 PackingList p = pkg->head;
107 char *where_args, *perm_args, *last_chdir;
108 int maxargs, where_count = 0, perm_count = 0, add_count;
111 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */
112 where_args = alloca(maxargs);
115 errx(2, "%s: can't get argument list space", __func__);
117 perm_args = alloca(maxargs);
120 errx(2, "%s: can't get argument list space", __func__);
123 strcpy(where_args, STARTSTRING);
124 where_count = sizeof(STARTSTRING)-1;
128 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
130 /* Reset the world */
135 Directory = (char *)home;
139 char cmd[FILENAME_MAX];
145 printf("extract: Package name is %s\n", p->name);
151 printf("extract: %s/%s\n", Directory, p->name);
153 char try[FILENAME_MAX];
155 /* first try to rename it into place */
156 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
158 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */
159 if (preserve && PkgName) {
160 char pf[FILENAME_MAX];
162 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
163 if (rename(try, pf)) {
165 "unable to back up %s to %s, aborting pkg_add",
167 rollback(PkgName, home, pkg->head, p);
173 if (rename(p->name, try) == 0) {
174 /* try to add to list of perms to be changed and run in bulk. */
175 if (p->name[0] == '/' || TOOBIG(p->name)) {
178 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
179 if (add_count < 0 || add_count >= maxargs - perm_count) {
181 errx(2, "%s: oops, miscounted strings!", __func__);
183 perm_count += add_count;
186 /* rename failed, try copying with a big tar command */
187 if (last_chdir != Directory) {
188 if (last_chdir == NULL) {
193 last_chdir = Directory;
195 else if (p->name[0] == '/' || TOOBIG(p->name)) {
198 add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name);
199 if (add_count < 0 || add_count >= maxargs - where_count) {
201 errx(2, "%s: oops, miscounted strings!", __func__);
203 where_count += add_count;
204 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name);
205 if (add_count < 0 || add_count >= maxargs - perm_count) {
207 errx(2, "%s: oops, miscounted strings!", __func__);
209 perm_count += add_count;
216 printf("extract: CWD to %s\n", p->name);
218 if (strcmp(p->name, ".")) {
219 if (!Fake && make_hierarchy(p->name) == FAIL) {
221 errx(2, "%s: unable to cwd to '%s'", __func__, p->name);
226 Directory = (char *)home;
230 if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
231 strstr(p->name, "%f")) && last_file == NULL) {
233 errx(2, "%s: no last file specified for '%s' command",
236 if (strstr(p->name, "%D") && Directory == NULL) {
238 errx(2, "%s: no directory specified for '%s' command",
241 format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file);
244 printf("extract: execute '%s'\n", cmd);
245 if (!Fake && system(cmd))
246 warnx("command '%s' failed", cmd);