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$"); 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 long maxargs; 114 int where_count = 0, perm_count = 0, add_count; 115 Boolean preserve; 116 117 maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */ 118 where_args = alloca(maxargs); 119 if (!where_args) { 120 cleanup(0); 121 errx(2, "%s: can't get argument list space", __func__); 122 } 123 perm_args = alloca(maxargs); 124 if (!perm_args) { 125 cleanup(0); 126 errx(2, "%s: can't get argument list space", __func__); 127 } 128 129 strcpy(where_args, STARTSTRING); 130 where_count = sizeof(STARTSTRING)-1; 131 perm_args[0] = 0; 132 133 last_chdir = 0; 134 preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE; 135 136 /* Reset the world */ 137 Owner = NULL; 138 Group = NULL; 139 Mode = NULL; 140 last_file = NULL; 141 Directory = (char *)home; 142 143 /* Do it */ 144 while (p) { 145 char cmd[FILENAME_MAX]; 146 147 switch(p->type) { 148 case PLIST_NAME: 149 PkgName = p->name; 150 if (Verbose) 151 printf("extract: Package name is %s\n", p->name); 152 break; 153 154 case PLIST_FILE: 155 last_file = p->name; 156 if (Verbose) 157 printf("extract: %s/%s\n", Directory, p->name); 158 if (!Fake) { 159 char try[FILENAME_MAX]; 160 161 /* first try to rename it into place */ 162 snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name); 163 if (fexists(try)) { 164 (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */ 165 if (preserve && PkgName) { 166 char pf[FILENAME_MAX]; 167 168 if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) { 169 if (rename(try, pf)) { 170 warnx( 171 "unable to back up %s to %s, aborting pkg_add", 172 try, pf); 173 rollback(PkgName, home, pkg->head, p); 174 return; 175 } 176 } 177 } 178 } 179 if (rename(p->name, try) == 0) { 180 /* try to add to list of perms to be changed and run in bulk. */ 181 if (p->name[0] == '/' || TOOBIG(p->name)) { 182 PUSHOUT(Directory); 183 } 184 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name); 185 if (add_count < 0 || add_count >= maxargs - perm_count) { 186 cleanup(0); 187 errx(2, "%s: oops, miscounted strings!", __func__); 188 } 189 perm_count += add_count; 190 } 191 else { 192 /* rename failed, try copying with a big tar command */ 193 if (last_chdir != Directory) { 194 if (last_chdir == NULL) { 195 PUSHOUT(Directory); 196 } else { 197 PUSHOUT(last_chdir); 198 } 199 last_chdir = Directory; 200 } 201 else if (p->name[0] == '/' || TOOBIG(p->name)) { 202 PUSHOUT(Directory); 203 } 204 add_count = add_arg(&where_args[where_count], maxargs - where_count, p->name); 205 if (add_count < 0 || add_count >= maxargs - where_count) { 206 cleanup(0); 207 errx(2, "%s: oops, miscounted strings!", __func__); 208 } 209 where_count += add_count; 210 add_count = add_arg(&perm_args[perm_count], maxargs - perm_count, p->name); 211 if (add_count < 0 || add_count >= maxargs - perm_count) { 212 cleanup(0); 213 errx(2, "%s: oops, miscounted strings!", __func__); 214 } 215 perm_count += add_count; 216 } 217 } 218 break; 219 220 case PLIST_CWD: 221 if (!prefix) 222 prefix = p->name; 223 if (p->name == NULL) 224 p->name = strdup(prefix); 225 if (Verbose) 226 printf("extract: CWD to %s\n", p->name); 227 PUSHOUT(Directory); 228 if (strcmp(p->name, ".")) { 229 if (!Fake && make_hierarchy(p->name, TRUE) == FAIL) { 230 cleanup(0); 231 errx(2, "%s: unable to cwd to '%s'", __func__, p->name); 232 } 233 Directory = p->name; 234 } 235 else 236 Directory = (char *)home; 237 break; 238 239 case PLIST_CMD: 240 if ((strstr(p->name, "%B") || strstr(p->name, "%F") || 241 strstr(p->name, "%f")) && last_file == NULL) { 242 cleanup(0); 243 errx(2, "%s: no last file specified for '%s' command", 244 __func__, p->name); 245 } 246 if (strstr(p->name, "%D") && Directory == NULL) { 247 cleanup(0); 248 errx(2, "%s: no directory specified for '%s' command", 249 __func__, p->name); 250 } 251 format_cmd(cmd, FILENAME_MAX, p->name, Directory, last_file); 252 PUSHOUT(Directory); 253 if (Verbose) 254 printf("extract: execute '%s'\n", cmd); 255 if (!Fake && system(cmd)) 256 warnx("command '%s' failed", cmd); 257 break; 258 259 case PLIST_CHMOD: 260 PUSHOUT(Directory); 261 Mode = p->name; 262 break; 263 264 case PLIST_CHOWN: 265 PUSHOUT(Directory); 266 Owner = p->name; 267 break; 268 269 case PLIST_CHGRP: 270 PUSHOUT(Directory); 271 Group = p->name; 272 break; 273 274 case PLIST_COMMENT: /* FALLTHROUGH */ 275 case PLIST_NOINST: 276 break; 277 278 case PLIST_IGNORE: 279 p = p->next; 280 break; 281 282 default: 283 break; 284 } 285 p = p->next; 286 } 287 PUSHOUT(Directory); 288} 289