extract.c revision 32659
1#ifndef lint 2static const char rcsid[] = 3 "$Id: extract.c,v 1.18 1997/10/24 08:32:06 max 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 PUSHOUT(last_chdir); 161 last_chdir = Directory; 162 } 163 else if (p->name[0] == '/' || TOOBIG(p->name)) { 164 PUSHOUT(Directory); 165 } 166 add_count = snprintf(&where_args[where_count], maxargs - where_count, " '%s'", p->name); 167 if (add_count > maxargs - where_count) 168 cleanup(0), errx(2, "oops, miscounted strings!"); 169 where_count += add_count; 170 add_count = snprintf(&perm_args[perm_count], 171 maxargs - perm_count, 172 "'%s' ", p->name); 173 if (add_count > maxargs - perm_count) 174 cleanup(0), errx(2, "oops, miscounted strings!"); 175 perm_count += add_count; 176 } 177 } 178 break; 179 180 case PLIST_CWD: 181 if (Verbose) 182 printf("extract: CWD to %s\n", p->name); 183 PUSHOUT(Directory); 184 if (strcmp(p->name, ".")) { 185 if (!Fake && make_hierarchy(p->name) == FAIL) 186 cleanup(0), errx(2, "unable to make directory '%s'", 187 p->name); 188 Directory = p->name; 189 } 190 else 191 Directory = home; 192 break; 193 194 case PLIST_CMD: 195 if ((strstr(p->name, "%B") || strstr(p->name, "%F") || 196 strstr(p->name, "%f")) && last_file == NULL) 197 cleanup(0), errx(2, "no last file specified for '%s' command", 198 p->name); 199 if (strstr(p->name, "%D") && Directory == NULL) 200 cleanup(0), errx(2, "no directory specified for '%s' command", 201 p->name); 202 format_cmd(cmd, p->name, Directory, last_file); 203 PUSHOUT(Directory); 204 if (Verbose) 205 printf("extract: execute '%s'\n", cmd); 206 if (!Fake && system(cmd)) 207 warnx("command '%s' failed", cmd); 208 break; 209 210 case PLIST_CHMOD: 211 PUSHOUT(Directory); 212 Mode = p->name; 213 break; 214 215 case PLIST_CHOWN: 216 PUSHOUT(Directory); 217 Owner = p->name; 218 break; 219 220 case PLIST_CHGRP: 221 PUSHOUT(Directory); 222 Group = p->name; 223 break; 224 225 case PLIST_COMMENT: 226 break; 227 228 case PLIST_IGNORE: 229 p = p->next; 230 break; 231 } 232 p = p->next; 233 } 234 PUSHOUT(Directory); 235} 236