extract.c revision 171934
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 */ 20 21#include <sys/cdefs.h> 22__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/add/extract.c 171934 2007-08-23 13:05:10Z krion $"); 23 24#include <ctype.h> 25#include <err.h> 26#include "lib.h" 27#include "add.h" 28 29 30#define STARTSTRING "/usr/bin/tar cf -" 31#define TOOBIG(str) \ 32 (((int)strlen(str) + FILENAME_MAX + where_count > maxargs) ||\ 33 ((int)strlen(str) + FILENAME_MAX + perm_count > maxargs)) 34 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)) { \ 40 cleanup(0); \ 41 errx(2, "%s: can not invoke %ld byte tar pipeline: %s", \ 42 __func__, (long)strlen(where_args), where_args); \ 43 } \ 44 strcpy(where_args, STARTSTRING); \ 45 where_count = sizeof(STARTSTRING)-1; \ 46 } \ 47 if (perm_count) { \ 48 apply_perms(todir, perm_args); \ 49 perm_args[0] = 0;\ 50 perm_count = 0; \ 51 } 52 53static void 54rollback(const char *name, const char *home, PackingList start, PackingList stop) 55{ 56 PackingList q; 57 char try[FILENAME_MAX], bup[FILENAME_MAX]; 58 const char *dir; 59 char *prefix = NULL; 60 61 dir = home; 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); 67 (void)unlink(try); 68 if (rename(bup, try)) 69 warnx("rollback: unable to rename %s back to %s", bup, try); 70 } 71 } 72 else if (q->type == PLIST_CWD) { 73 if (!prefix) 74 prefix = q->name; 75 if (q->name == NULL) 76 q->name = prefix; 77 else if (strcmp(q->name, ".")) 78 dir = q->name; 79 else 80 dir = home; 81 } 82 } 83} 84 85#define add_char(buf, len, pos, ch) do {\ 86 if ((pos) < (len)) { \ 87 buf[(pos)] = (ch); \ 88 buf[(pos) + 1] = '\0'; \ 89 } \ 90 ++(pos); \ 91} while (0) 92 93static int 94add_arg(char *buf, int len, const char *str) 95{ 96 int i = 0; 97 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); 103 } 104 return (i); 105} 106 107void 108extract_plist(const char *home, Package *pkg) 109{ 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; 114 Boolean preserve; 115 116 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */ 117 where_args = alloca(maxargs); 118 if (!where_args) { 119 cleanup(0); 120 errx(2, "%s: can't get argument list space", __func__); 121 } 122 perm_args = alloca(maxargs); 123 if (!perm_args) { 124 cleanup(0); 125 errx(2, "%s: can't get argument list space", __func__); 126 } 127 128 strcpy(where_args, STARTSTRING); 129 where_count = sizeof(STARTSTRING)-1; 130 perm_args[0] = 0; 131 132 last_chdir = 0; 133 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE; 134 135 /* Reset the world */ 136 Owner = NULL; 137 Group = NULL; 138 Mode = NULL; 139 last_file = NULL; 140 Directory = (char *)home; 141 142 /* Do it */ 143 while (p) { 144 char cmd[FILENAME_MAX]; 145 146 switch(p->type) { 147 case PLIST_NAME: 148 PkgName = p->name; 149 if (Verbose) 150 printf("extract: Package name is %s\n", p->name); 151 break; 152 153 case PLIST_FILE: 154 last_file = p->name; 155 if (Verbose) 156 printf("extract: %s/%s\n", Directory, p->name); 157 if (!Fake) { 158 char try[FILENAME_MAX]; 159 160 /* first try to rename it into place */ 161 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name); 162 if (fexists(try)) { 163 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */ 164 if (preserve && PkgName) { 165 char pf[FILENAME_MAX]; 166 167 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) { 168 if (rename(try, pf)) { 169 warnx( 170 "unable to back up %s to %s, aborting pkg_add", 171 try, pf); 172 rollback(PkgName, home, pkg->head, p); 173 return; 174 } 175 } 176 } 177 } 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)) { 181 PUSHOUT(Directory); 182 } 183 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name); 184 if (add_count < 0 || add_count >= maxargs - perm_count) { 185 cleanup(0); 186 errx(2, "%s: oops, miscounted strings!", __func__); 187 } 188 perm_count += add_count; 189 } 190 else { 191 /* rename failed, try copying with a big tar command */ 192 if (last_chdir != Directory) { 193 if (last_chdir == NULL) { 194 PUSHOUT(Directory); 195 } else { 196 PUSHOUT(last_chdir); 197 } 198 last_chdir = Directory; 199 } 200 else if (p->name[0] == '/' || TOOBIG(p->name)) { 201 PUSHOUT(Directory); 202 } 203 add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name); 204 if (add_count < 0 || add_count >= maxargs - where_count) { 205 cleanup(0); 206 errx(2, "%s: oops, miscounted strings!", __func__); 207 } 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) { 211 cleanup(0); 212 errx(2, "%s: oops, miscounted strings!", __func__); 213 } 214 perm_count += add_count; 215 } 216 } 217 break; 218 219 case PLIST_CWD: 220 if (!prefix) 221 prefix = p->name; 222 if (p->name == NULL) 223 p->name = strdup(prefix); 224 if (Verbose) 225 printf("extract: CWD to %s\n", p->name); 226 PUSHOUT(Directory); 227 if (strcmp(p->name, ".")) { 228 if (!Fake && make_hierarchy(p->name) == FAIL) { 229 cleanup(0); 230 errx(2, "%s: unable to cwd to '%s'", __func__, p->name); 231 } 232 Directory = p->name; 233 } 234 else 235 Directory = (char *)home; 236 break; 237 238 case PLIST_CMD: 239 if ((strstr(p->name, "%B") || strstr(p->name, "%F") || 240 strstr(p->name, "%f")) && last_file == NULL) { 241 cleanup(0); 242 errx(2, "%s: no last file specified for '%s' command", 243 __func__, p->name); 244 } 245 if (strstr(p->name, "%D") && Directory == NULL) { 246 cleanup(0); 247 errx(2, "%s: no directory specified for '%s' command", 248 __func__, p->name); 249 } 250 format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file); 251 PUSHOUT(Directory); 252 if (Verbose) 253 printf("extract: execute '%s'\n", cmd); 254 if (!Fake && system(cmd)) 255 warnx("command '%s' failed", cmd); 256 break; 257 258 case PLIST_CHMOD: 259 PUSHOUT(Directory); 260 Mode = p->name; 261 break; 262 263 case PLIST_CHOWN: 264 PUSHOUT(Directory); 265 Owner = p->name; 266 break; 267 268 case PLIST_CHGRP: 269 PUSHOUT(Directory); 270 Group = p->name; 271 break; 272 273 case PLIST_COMMENT: /* FALLTHROUGH */ 274 case PLIST_NOINST: 275 break; 276 277 case PLIST_IGNORE: 278 p = p->next; 279 break; 280 281 default: 282 break; 283 } 284 p = p->next; 285 } 286 PUSHOUT(Directory); 287} 288