extract.c revision 40266
1#ifndef lint 2static const char rcsid[] = 3 "$Id: extract.c,v 1.21 1998/09/11 07:26:54 jkh Exp $"; 4#endif 5 6/* 7 * FreeBSD install - a package for the installation and maintainance 8 * of non-core utilities. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Jordan K. Hubbard 20 * 18 July 1993 21 * 22 * This is the package extraction code for the add module. 23 * 24 */ 25 26#include <err.h> 27#include "lib.h" 28#include "add.h" 29 30 31#define STARTSTRING "tar cf - " 32#define TOOBIG(str) ((strlen(str) + 22 + strlen(home) + where_count > maxargs) \ 33 || (strlen(str) + 6 + strlen(home) + perm_count > maxargs)) 34 35#define PUSHOUT(todir) /* push out string */ \ 36 if (where_count > sizeof(STARTSTRING)-1) { \ 37 strcat(where_args, "|tar xf - -C "); \ 38 strcat(where_args, todir); \ 39 if (system(where_args)) { \ 40 cleanup(0); \ 41 errx(2, "can not invoke %d byte tar pipeline: %s", \ 42 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(char *name, char *home, PackingList start, PackingList stop) 55{ 56 PackingList q; 57 char try[FILENAME_MAX], bup[FILENAME_MAX], *dir; 58 59 dir = home; 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); 65 (void)unlink(try); 66 if (rename(bup, try)) 67 warnx("rollback: unable to rename %s back to %s", bup, try); 68 } 69 } 70 else if (q->type == PLIST_CWD) { 71 if (strcmp(q->name, ".")) 72 dir = q->name; 73 else 74 dir = home; 75 } 76 } 77} 78 79void 80extract_plist(char *home, Package *pkg) 81{ 82 PackingList p = pkg->head; 83 char *last_file; 84 char *where_args, *perm_args, *last_chdir; 85 int maxargs, where_count = 0, perm_count = 0, add_count; 86 Boolean preserve; 87 88 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */ 89 where_args = alloca(maxargs); 90 if (!where_args) { 91 cleanup(0); 92 errx(2, "can't get argument list space"); 93 } 94 perm_args = alloca(maxargs); 95 if (!perm_args) { 96 cleanup(0); 97 errx(2, "can't get argument list space"); 98 } 99 100 strcpy(where_args, STARTSTRING); 101 where_count = sizeof(STARTSTRING)-1; 102 perm_args[0] = 0; 103 104 last_chdir = 0; 105 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE; 106 107 /* Reset the world */ 108 Owner = NULL; 109 Group = NULL; 110 Mode = NULL; 111 last_file = NULL; 112 Directory = home; 113 114 /* Do it */ 115 while (p) { 116 char cmd[FILENAME_MAX]; 117 118 switch(p->type) { 119 case PLIST_NAME: 120 PkgName = p->name; 121 if (Verbose) 122 printf("extract: Package name is %s\n", p->name); 123 break; 124 125 case PLIST_FILE: 126 last_file = p->name; 127 if (Verbose) 128 printf("extract: %s/%s\n", Directory, p->name); 129 if (!Fake) { 130 char try[FILENAME_MAX]; 131 132 if (strrchr(p->name,'\'')) { 133 cleanup(0); 134 errx(2, "Bogus filename \"%s\"", p->name); 135 } 136 137 /* first try to rename it into place */ 138 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name); 139 if (fexists(try)) { 140 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */ 141 if (preserve && PkgName) { 142 char pf[FILENAME_MAX]; 143 144 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) { 145 if (rename(try, pf)) { 146 warnx( 147 "unable to back up %s to %s, aborting pkg_add", 148 try, pf); 149 rollback(PkgName, home, pkg->head, p); 150 return; 151 } 152 } 153 } 154 } 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)) { 158 PUSHOUT(Directory); 159 } 160 add_count = snprintf(&perm_args[perm_count], maxargs - perm_count, "'%s' ", p->name); 161 if (add_count > maxargs - perm_count) { 162 cleanup(0); 163 errx(2, "oops, miscounted strings!"); 164 } 165 perm_count += add_count; 166 } 167 else { 168 /* rename failed, try copying with a big tar command */ 169 if (last_chdir != Directory) { 170 if (last_chdir == NULL) { 171 PUSHOUT(Directory); 172 } else { 173 PUSHOUT(last_chdir); 174 } 175 last_chdir = Directory; 176 } 177 else if (p->name[0] == '/' || TOOBIG(p->name)) { 178 PUSHOUT(Directory); 179 } 180 add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name); 181 if (add_count > maxargs - where_count) { 182 cleanup(0); 183 errx(2, "oops, miscounted strings!"); 184 } 185 where_count += add_count; 186 add_count = snprintf(&perm_args[perm_count], 187 maxargs - perm_count, 188 "'%s' ", p->name); 189 if (add_count > maxargs - perm_count) { 190 cleanup(0); 191 errx(2, "oops, miscounted strings!"); 192 } 193 perm_count += add_count; 194 } 195 } 196 break; 197 198 case PLIST_CWD: 199 if (Verbose) 200 printf("extract: CWD to %s\n", p->name); 201 PUSHOUT(Directory); 202 if (strcmp(p->name, ".")) { 203 if (!Fake && make_hierarchy(p->name) == FAIL) { 204 cleanup(0); 205 errx(2, "unable to cwd to '%s'", p->name); 206 } 207 Directory = p->name; 208 } 209 else 210 Directory = home; 211 break; 212 213 case PLIST_CMD: 214 if ((strstr(p->name, "%B") || strstr(p->name, "%F") || 215 strstr(p->name, "%f")) && last_file == NULL) { 216 cleanup(0); 217 errx(2, "no last file specified for '%s' command", p->name); 218 } 219 if (strstr(p->name, "%D") && Directory == NULL) { 220 cleanup(0); 221 errx(2, "no directory specified for '%s' command", p->name); 222 } 223 format_cmd(cmd, p->name, Directory, last_file); 224 PUSHOUT(Directory); 225 if (Verbose) 226 printf("extract: execute '%s'\n", cmd); 227 if (!Fake && system(cmd)) 228 warnx("command '%s' failed", cmd); 229 break; 230 231 case PLIST_CHMOD: 232 PUSHOUT(Directory); 233 Mode = p->name; 234 break; 235 236 case PLIST_CHOWN: 237 PUSHOUT(Directory); 238 Owner = p->name; 239 break; 240 241 case PLIST_CHGRP: 242 PUSHOUT(Directory); 243 Group = p->name; 244 break; 245 246 case PLIST_COMMENT: 247 break; 248 249 case PLIST_IGNORE: 250 p = p->next; 251 break; 252 } 253 p = p->next; 254 } 255 PUSHOUT(Directory); 256} 257