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