extract.c revision 37378
1#ifndef lint 2static const char rcsid[] = 3 "$Id: extract.c,v 1.19 1998/01/21 00:12:56 imp 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), errx(2, "can not invoke %d byte tar pipeline: %s", \ 41 strlen(where_args), where_args); \ 42 strcpy(where_args, STARTSTRING); \ 43 where_count = sizeof(STARTSTRING)-1; \ 44 } \ 45 if (perm_count) { \ 46 apply_perms(todir, perm_args); \ 47 perm_args[0] = 0;\ 48 perm_count = 0; \ 49 } 50 51static void 52rollback(char *name, char *home, PackingList start, PackingList stop) 53{ 54 PackingList q; 55 char try[FILENAME_MAX], bup[FILENAME_MAX], *dir; 56 57 dir = home; 58 for (q = start; q != stop; q = q->next) { 59 if (q->type == PLIST_FILE) { 60 snprintf(try, FILENAME_MAX, "%s/%s", dir, q->name); 61 if (make_preserve_name(bup, FILENAME_MAX, name, try) && fexists(bup)) { 62 (void)chflags(try, 0); 63 (void)unlink(try); 64 if (rename(bup, try)) 65 warnx("rollback: unable to rename %s back to %s", bup, try); 66 } 67 } 68 else if (q->type == PLIST_CWD) { 69 if (strcmp(q->name, ".")) 70 dir = q->name; 71 else 72 dir = home; 73 } 74 } 75} 76 77void 78extract_plist(char *home, Package *pkg) 79{ 80 PackingList p = pkg->head; 81 char *last_file; 82 char *where_args, *perm_args, *last_chdir; 83 int maxargs, where_count = 0, perm_count = 0, add_count; 84 Boolean preserve; 85 86 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */ 87 where_args = alloca(maxargs); 88 if (!where_args) 89 cleanup(0), errx(2, "can't get argument list space"); 90 perm_args = alloca(maxargs); 91 if (!perm_args) 92 cleanup(0), errx(2, "can't get argument list space"); 93 94 strcpy(where_args, STARTSTRING); 95 where_count = sizeof(STARTSTRING)-1; 96 perm_args[0] = 0; 97 98 last_chdir = 0; 99 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE; 100 101 /* Reset the world */ 102 Owner = NULL; 103 Group = NULL; 104 Mode = NULL; 105 last_file = NULL; 106 Directory = home; 107 108 /* Do it */ 109 while (p) { 110 char cmd[FILENAME_MAX]; 111 112 switch(p->type) { 113 case PLIST_NAME: 114 PkgName = p->name; 115 if (Verbose) 116 printf("extract: Package name is %s\n", p->name); 117 break; 118 119 case PLIST_FILE: 120 last_file = p->name; 121 if (Verbose) 122 printf("extract: %s/%s\n", Directory, p->name); 123 if (!Fake) { 124 char try[FILENAME_MAX]; 125 126 if (strrchr(p->name,'\'')) 127 cleanup(0), errx(2, "Bogus filename \"%s\"", p->name); 128 129 /* first try to rename it into place */ 130 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name); 131 if (fexists(try)) { 132 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */ 133 if (preserve && PkgName) { 134 char pf[FILENAME_MAX]; 135 136 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) { 137 if (rename(try, pf)) { 138 warnx( 139 "unable to back up %s to %s, aborting pkg_add", 140 try, pf); 141 rollback(PkgName, home, pkg->head, p); 142 return; 143 } 144 } 145 } 146 } 147 if (rename(p->name, try) == 0) { 148 /* try to add to list of perms to be changed and run in bulk. */ 149 if (p->name[0] == '/' || TOOBIG(p->name)) { 150 PUSHOUT(Directory); 151 } 152 add_count = snprintf(&perm_args[perm_count], maxargs - perm_count, "'%s' ", p->name); 153 if (add_count > maxargs - perm_count) 154 cleanup(0), errx(2, "oops, miscounted strings!"); 155 perm_count += add_count; 156 } 157 else { 158 /* rename failed, try copying with a big tar command */ 159 if (last_chdir != Directory) { 160 if (last_chdir == NULL) { 161 PUSHOUT(Directory); 162 } else { 163 PUSHOUT(last_chdir); 164 } 165 last_chdir = Directory; 166 } 167 else if (p->name[0] == '/' || TOOBIG(p->name)) { 168 PUSHOUT(Directory); 169 } 170 add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name); 171 if (add_count > maxargs - where_count) 172 cleanup(0), errx(2, "oops, miscounted strings!"); 173 where_count += add_count; 174 add_count = snprintf(&perm_args[perm_count], 175 maxargs - perm_count, 176 "'%s' ", p->name); 177 if (add_count > maxargs - perm_count) 178 cleanup(0), errx(2, "oops, miscounted strings!"); 179 perm_count += add_count; 180 } 181 } 182 break; 183 184 case PLIST_CWD: 185 if (Verbose) 186 printf("extract: CWD to %s\n", p->name); 187 PUSHOUT(Directory); 188 if (strcmp(p->name, ".")) { 189 if (!Fake && make_hierarchy(p->name) == FAIL) 190 cleanup(0), errx(2, "unable to make directory '%s'", 191 p->name); 192 Directory = p->name; 193 } 194 else 195 Directory = home; 196 break; 197 198 case PLIST_CMD: 199 if ((strstr(p->name, "%B") || strstr(p->name, "%F") || 200 strstr(p->name, "%f")) && last_file == NULL) 201 cleanup(0), errx(2, "no last file specified for '%s' command", 202 p->name); 203 if (strstr(p->name, "%D") && Directory == NULL) 204 cleanup(0), errx(2, "no directory specified for '%s' command", 205 p->name); 206 format_cmd(cmd, p->name, Directory, last_file); 207 PUSHOUT(Directory); 208 if (Verbose) 209 printf("extract: execute '%s'\n", cmd); 210 if (!Fake && system(cmd)) 211 warnx("command '%s' failed", cmd); 212 break; 213 214 case PLIST_CHMOD: 215 PUSHOUT(Directory); 216 Mode = p->name; 217 break; 218 219 case PLIST_CHOWN: 220 PUSHOUT(Directory); 221 Owner = p->name; 222 break; 223 224 case PLIST_CHGRP: 225 PUSHOUT(Directory); 226 Group = p->name; 227 break; 228 229 case PLIST_COMMENT: 230 break; 231 232 case PLIST_IGNORE: 233 p = p->next; 234 break; 235 } 236 p = p->next; 237 } 238 PUSHOUT(Directory); 239} 240