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.
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 $
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))
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)) { \
39 "%s: can not invoke %ld byte tar pipeline: %s", \
41 (long)strlen(where_args), where_args); \
43 strcpy(where_args, STARTSTRING); \
44 where_count = sizeof(STARTSTRING)-1; \
47 apply_perms(todir, perm_args); \
53 rollback(const char *name, const char *home, PackingList start, PackingList stop)
56 char try[FILENAME_MAX], bup[FILENAME_MAX];
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);
67 warnx("rollback: unable to rename %s back to %s", bup, try);
70 else if (q->type == PLIST_CWD) {
71 if (strcmp(q->name, "."))
80 extract_plist(const char *home, Package *pkg)
82 PackingList p = pkg->head;
84 char *where_args, *perm_args, *last_chdir;
85 int maxargs, where_count = 0, perm_count = 0, add_count;
88 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */
89 where_args = alloca(maxargs);
92 errx(2, "%s: can't get argument list space", __func__);
94 perm_args = alloca(maxargs);
97 errx(2, "%s: can't get argument list space", __func__);
100 strcpy(where_args, STARTSTRING);
101 where_count = sizeof(STARTSTRING)-1;
105 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
107 /* Reset the world */
112 (const char *)Directory = home;
116 char cmd[FILENAME_MAX];
122 printf("extract: Package name is %s\n", p->name);
128 printf("extract: %s/%s\n", Directory, p->name);
130 char try[FILENAME_MAX];
132 if (strrchr(p->name,'\'')) {
134 errx(2, "%s: Bogus filename \"%s\"", __func__, p->name);
137 /* first try to rename it into place */
138 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name);
140 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */
141 if (preserve && PkgName) {
142 char pf[FILENAME_MAX];
144 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) {
145 if (rename(try, pf)) {
147 "unable to back up %s to %s, aborting pkg_add",
149 rollback(PkgName, home, pkg->head, p);
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)) {
160 add_count = snprintf(&perm_args[perm_count], maxargs - perm_count, "'%s' ", p->name);
161 if (add_count < 0 || add_count > maxargs - perm_count) {
163 errx(2, "%s: oops, miscounted strings!", __func__);
165 perm_count += add_count;
168 /* rename failed, try copying with a big tar command */
169 if (last_chdir != Directory) {
170 if (last_chdir == NULL) {
175 last_chdir = Directory;
177 else if (p->name[0] == '/' || TOOBIG(p->name)) {
180 add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name);
181 if (add_count < 0 || add_count > maxargs - where_count) {
183 errx(2, "%s: oops, miscounted strings!", __func__);
185 where_count += add_count;
186 add_count = snprintf(&perm_args[perm_count],
187 maxargs - perm_count,
189 if (add_count < 0 || add_count > maxargs - perm_count) {
191 errx(2, "%s: oops, miscounted strings!", __func__);
193 perm_count += add_count;
200 printf("extract: CWD to %s\n", p->name);
202 if (strcmp(p->name, ".")) {
203 if (!Fake && make_hierarchy(p->name) == FAIL) {
205 errx(2, "%s: unable to cwd to '%s'", __func__, p->name);
210 (const char *)Directory = home;
214 if ((strstr(p->name, "%B") || strstr(p->name, "%F") ||
215 strstr(p->name, "%f")) && last_file == NULL) {
217 errx(2, "%s: no last file specified for '%s' command",
220 if (strstr(p->name, "%D") && Directory == NULL) {
222 errx(2, "%s: no directory specified for '%s' command",
225 format_cmd(cmd, p->name, Directory, last_file);
228 printf("extract: execute '%s'\n", cmd);
229 if (!Fake && system(cmd))
230 warnx("command '%s' failed", cmd);