/* * main * DESCRIPTION: Play the adventure game. * INPUTS: none (command line arguments are ignored) * OUTPUTS: none * RETURN VALUE: 0 on success, 3 in panic situations */ int main () { game_condition_t game; /* outcome of playing */ /* Randomize for more fun (remove for deterministic layout). */ srand (time (NULL)); /* Provide some protection against fatal errors. */ clean_on_signals (); if (!build_world ()) {PANIC ("can't build world");} init_game (); /* Perform sanity checks. */ if (0 != sanity_check ()) { PANIC ("failed sanity checks"); } /* Create status message thread. */ if (0 != pthread_create (&status_thread_id, NULL, status_thread, NULL)) { PANIC ("failed to create status thread"); } push_cleanup (cancel_status_thread, NULL); { /* Start mode X. */ if (0 != set_mode_X (fill_horiz_buffer, fill_vert_buffer)) { PANIC ("cannot initialize mode X"); } push_cleanup ((cleanup_fn_t)clear_mode_X, NULL); { /* Initialize the keyboard and/or Tux controller. */ if (0 != init_input ()) { PANIC ("cannot initialize input"); } push_cleanup ((cleanup_fn_t)shutdown_input, NULL); { game = game_loop (); } pop_cleanup (1); } pop_cleanup (1); } pop_cleanup (1); /* Print a message about the outcome. */ switch (game) { case GAME_WON: printf ("You win the game! CONGRATULATIONS!\n"); break; case GAME_QUIT: printf ("Quitter!\n"); break; } /* Return success. */ return 0; }
static int do_script(struct pkginfo *pkg, struct pkgbin *pif, struct command *cmd, struct stat *stab, int warn) { pid_t pid; int r; setexecute(cmd->filename, stab); push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0); pid = subproc_fork(); if (pid == 0) { if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->set->name, 1) || setenv("DPKG_MAINTSCRIPT_ARCH", pif->arch->name, 1) || setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) || setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1)) ohshite(_("unable to setenv for maintainer script")); cmd->filename = cmd->argv[0] = preexecscript(cmd); command_exec(cmd); } subproc_signals_setup(cmd->name); /* This does a push_cleanup(). */ r = subproc_wait_check(pid, cmd->name, warn); pop_cleanup(ehflag_normaltidy); pop_cleanup(ehflag_normaltidy); return r; }
static void trig_file_interests_update(void) { struct trigfileint *tfi; FILE *nf; nf = fopen(triggersnewfilefile, "w"); if (!nf) ohshite(_("unable to create new file triggers file `%.250s'"), triggersnewfilefile); push_cleanup(cu_closestream, ~ehflag_normaltidy, NULL, 0, 1, nf); for (tfi = filetriggers.head; tfi; tfi = tfi->inoverall.next) fprintf(nf, "%s %s%s\n", trigh.namenode_name(tfi->fnn), tfi->pkg->name, (tfi->options == trig_noawait) ? "/noawait" : ""); if (ferror(nf)) ohshite(_("unable to write new file triggers file `%.250s'"), triggersnewfilefile); if (fflush(nf)) ohshite(_("unable to flush new file triggers file '%.250s'"), triggersnewfilefile); if (fsync(fileno(nf))) ohshite(_("unable to sync new file triggers file '%.250s'"), triggersnewfilefile); pop_cleanup(ehflag_normaltidy); if (fclose(nf)) ohshite(_("unable to close new file triggers file `%.250s'"), triggersnewfilefile); if (rename(triggersnewfilefile, triggersfilefile)) ohshite(_("unable to install new file triggers file as `%.250s'"), triggersfilefile); }
/** * Lock a file. * * @param lockfd The pointer to the lock file descriptor. It must be allocated * statically as its addresses is passed to a cleanup handler. * @param flags The lock flags specifying what type of locking to perform. * @param filename The name of the file to lock. * @param desc The description of the file to lock. */ void file_lock(int *lockfd, enum file_lock_flags flags, const char *filename, const char *desc) { struct flock fl; int lock_cmd; setcloexec(*lockfd, filename); file_lock_setup(&fl, F_WRLCK); if (flags == FILE_LOCK_WAIT) lock_cmd = F_SETLKW; else lock_cmd = F_SETLK; if (fcntl(*lockfd, lock_cmd, &fl) == -1) { if (errno == EACCES || errno == EAGAIN) ohshit(_("%s is locked by another process"), desc); else ohshite(_("unable to lock %s"), desc); } push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 2, lockfd, desc); }
void atomic_file_open(struct atomic_file *file) { file->fp = fopen(file->name_new, "w"); if (file->fp == NULL) ohshite(_("unable to create new file '%.250s'"), file->name_new); fchmod(fileno(file->fp), 0644); push_cleanup(cu_closestream, ~ehflag_normaltidy, NULL, 0, 1, file->fp); }
int m_fork(void) { pid_t r; r= fork(); if (r == -1) { onerr_abort++; ohshite(_("fork failed")); } if (r > 0) return r; push_cleanup(cu_m_fork,~0, NULL,0, 0); set_error_display(print_error_forked,NULL); return r; }
void trig_file_interests_ensure(void) { FILE *f; char linebuf[1024], *space; struct pkginfo *pkg; struct pkgbin *pkgbin; if (filetriggers_edited >= 0) return; f = fopen(triggersfilefile, "r"); if (!f) { if (errno == ENOENT) goto ok; ohshite(_("unable to read file triggers file '%.250s'"), triggersfilefile); } push_cleanup(cu_closestream, ~0, 1, f); while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) { struct dpkg_error err; char *slash; enum trig_options trig_opts = TRIG_AWAIT; space = strchr(linebuf, ' '); if (!space || linebuf[0] != '/') ohshit(_("syntax error in file triggers file '%.250s'"), triggersfilefile); *space++ = '\0'; slash = strchr(space, '/'); if (slash && strcmp("/noawait", slash) == 0) { trig_opts = TRIG_NOAWAIT; *slash = '\0'; } if (slash && strcmp("/await", slash) == 0) { trig_opts = TRIG_AWAIT; *slash = '\0'; } pkg = pkg_spec_parse_pkg(space, &err); if (pkg == NULL) ohshit(_("file triggers record mentions illegal " "package name '%.250s' (for interest in file " "'%.250s'): %.250s"), space, linebuf, err.str); pkgbin = &pkg->installed; trk_file_interest_change(linebuf, pkg, pkgbin, +2, trig_opts); } pop_cleanup(ehflag_normaltidy); ok: filetriggers_edited = 0; }
void trig_parse_ci(const char *file, trig_parse_cicb *interest, trig_parse_cicb *activate, struct pkginfo *pkg, struct pkgbin *pkgbin) { FILE *f; char linebuf[MAXTRIGDIRECTIVE], *cmd, *spc, *eol; int l; f = fopen(file, "r"); if (!f) { if (errno == ENOENT) return; /* No file is just like an empty one. */ ohshite(_("unable to open triggers ci file '%.250s'"), file); } push_cleanup(cu_closestream, ~0, 1, f); while ((l = fgets_checked(linebuf, sizeof(linebuf), f, file)) >= 0) { for (cmd = linebuf; c_iswhite(*cmd); cmd++) ; if (*cmd == '#') continue; for (eol = linebuf + l; eol > cmd && c_iswhite(eol[-1]); eol--) ; if (eol == cmd) continue; *eol = '\0'; for (spc = cmd; *spc && !c_iswhite(*spc); spc++) ; if (!*spc) ohshit(_("triggers ci file contains unknown directive syntax")); *spc++ = '\0'; while (c_iswhite(*spc)) spc++; if (strcmp(cmd, "interest") == 0 || strcmp(cmd, "interest-await") == 0) { parse_ci_call(file, cmd, interest, spc, pkg, pkgbin, TRIG_AWAIT); } else if (strcmp(cmd, "interest-noawait") == 0) { parse_ci_call(file, cmd, interest, spc, pkg, pkgbin, TRIG_NOAWAIT); } else if (strcmp(cmd, "activate") == 0 || strcmp(cmd, "activate-await") == 0) { parse_ci_call(file, cmd, activate, spc, pkg, pkgbin, TRIG_AWAIT); } else if (strcmp(cmd, "activate-noawait") == 0) { parse_ci_call(file, cmd, activate, spc, pkg, pkgbin, TRIG_NOAWAIT); } else { ohshit(_("triggers ci file contains unknown directive '%.250s'"), cmd); } } pop_cleanup(ehflag_normaltidy); /* fclose() */ }
/* * If mask is nonzero, will not write any file whose filenamenode * has any flag bits set in mask. */ void write_filelist_except(struct pkginfo *pkg, struct pkgbin *pkgbin, struct fileinlist *list, enum fnnflags mask) { static struct varbuf newvb; const char *listfile; FILE *file; listfile = pkgadminfile(pkg, pkgbin, LISTFILE); varbuf_reset(&newvb); varbuf_add_str(&newvb, listfile); varbuf_add_str(&newvb, NEWDBEXT); varbuf_end_str(&newvb); file= fopen(newvb.buf,"w+"); if (!file) ohshite(_("unable to create updated files list file for package %s"), pkg_describe(pkg, pdo_foreign)); push_cleanup(cu_closestream, ehflag_bombout, NULL, 0, 1, (void *)file); while (list) { if (!(mask && (list->namenode->flags & mask))) { fputs(list->namenode->name,file); putc('\n',file); } list= list->next; } if (ferror(file)) ohshite(_("failed to write to updated files list file for package %s"), pkg_describe(pkg, pdo_foreign)); if (fflush(file)) ohshite(_("failed to flush updated files list file for package %s"), pkg_describe(pkg, pdo_foreign)); if (fsync(fileno(file))) ohshite(_("failed to sync updated files list file for package %s"), pkg_describe(pkg, pdo_foreign)); pop_cleanup(ehflag_normaltidy); /* file = fopen() */ if (fclose(file)) ohshite(_("failed to close updated files list file for package %s"), pkg_describe(pkg, pdo_foreign)); if (rename(newvb.buf, listfile)) ohshite(_("failed to install updated files list file for package %s"), pkg_describe(pkg, pdo_foreign)); dir_sync_path(pkgadmindir()); note_must_reread_files_inpackage(pkg); }
static void trk_explicit_interest_change(const char *trig, struct pkginfo *pkg, int signum, enum trig_options opts) { static struct varbuf newfn; char buf[1024]; FILE *nf; bool empty = true; trk_explicit_start(trig); varbuf_reset(&newfn); varbuf_printf(&newfn, "%s/%s.new", triggersdir, trig); nf = fopen(newfn.buf, "w"); if (!nf) ohshite(_("unable to create new trigger interest file `%.250s'"), newfn.buf); push_cleanup(cu_closestream, ~ehflag_normaltidy, NULL, 0, 1, nf); while (trk_explicit_f && trk_explicit_fgets(buf, sizeof(buf)) >= 0) { int len = strlen(pkg->name); if (strncmp(buf, pkg->name, len) == 0 && (buf[len] == '\0' || buf[len] == '/')) continue; fprintf(nf, "%s\n", buf); empty = false; } if (signum > 0) { fprintf(nf, "%s%s\n", pkg->name, (opts == trig_noawait) ? "/noawait" : ""); empty = false; } if (!empty) trk_explicit_interest_flush(newfn.buf, nf); pop_cleanup(ehflag_normaltidy); if (fclose(nf)) ohshite(_("unable to close new trigger interest file `%.250s'"), newfn.buf); if (empty) trk_explicit_interest_remove(newfn.buf); else trk_explicit_interest_commit(newfn.buf); dir_sync_path(triggersdir); }
void trig_file_interests_ensure(void) { FILE *f; char linebuf[1024], *space; struct pkginfo *pkg; const char *emsg; if (filetriggers_edited >= 0) return; f = fopen(triggersfilefile, "r"); if (!f) { if (errno == ENOENT) goto ok; ohshite(_("unable to read file triggers file `%.250s'"), triggersfilefile); } push_cleanup(cu_closestream, ~0, NULL, 0, 1, f); while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) { char *slash; enum trig_options trig_opts = trig_await; space = strchr(linebuf, ' '); if (!space || linebuf[0] != '/') ohshit(_("syntax error in file triggers file `%.250s'"), triggersfilefile); *space++ = '\0'; slash = strchr(space, '/'); if (slash && strcmp("/noawait", slash) == 0) { trig_opts = trig_noawait; *slash = '\0'; } emsg = pkg_name_is_illegal(space, NULL); if (emsg) ohshit(_("file triggers record mentions illegal " "package name `%.250s' (for interest in file " "`%.250s'): %.250s"), space, linebuf, emsg); pkg = pkg_db_find(space); trk_file_interest_change(linebuf, pkg, +2, trig_opts); } pop_cleanup(ehflag_normaltidy); ok: filetriggers_edited = 0; }
static void info_prepare(const char *const **argvp, const char **debarp, const char **dirp, int admininfo) { char *dbuf; *debarp= *(*argvp)++; if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong); dbuf = mkdtemp(path_make_temp_template("dpkg-deb")); if (!dbuf) ohshite(_("unable to create temporary directory")); *dirp = dbuf; push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf); extracthalf(*debarp, dbuf, DPKG_TAR_EXTRACT | DPKG_TAR_NOMTIME, admininfo); }
/** * Open a file for RFC-822 parsing. */ void parse_open(struct parsedb_state *ps, const char *filename, enum parsedbflags flags) { static int fd; struct stat st; ps->filename = filename; ps->flags = flags; ps->lno = 0; ps->pkg = NULL; ps->pkgbin = NULL; fd = open(filename, O_RDONLY); if (fd == -1) ohshite(_("failed to open package info file `%.255s' for reading"), filename); push_cleanup(cu_closefd, ~ehflag_normaltidy, NULL, 0, 1, &fd); if (fstat(fd, &st) == -1) ohshite(_("can't stat package info file `%.255s'"), filename); if (st.st_size > 0) { #ifdef USE_MMAP ps->dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (ps->dataptr == MAP_FAILED) ohshite(_("can't mmap package info file `%.255s'"), filename); #else ps->dataptr = m_malloc(st.st_size); if (fd_read(fd, ps->dataptr, st.st_size) < 0) ohshite(_("reading package info file '%.255s'"), filename); #endif ps->data = ps->dataptr; ps->endptr = ps->dataptr + st.st_size; } else { ps->data = ps->dataptr = ps->endptr = NULL; } pop_cleanup(ehflag_normaltidy); if (close(fd)) ohshite(_("failed to close after read: `%.255s'"), filename); }
void subproc_signals_ignore(const char *name) { struct sigaction sa; size_t i; onerr_abort++; memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; for (i = 0; i < array_count(signo_ignores); i++) subproc_set_signal(signo_ignores[i], &sa, &sa_save[i], name); push_cleanup(subproc_signals_cleanup, ~0, NULL, 0, 0); onerr_abort--; }
/* lockfd must be allocated statically as its addresses is passed to * a cleanup handler. */ void file_lock(int *lockfd, const char *filename, const char *emsg, const char *emsg_eagain) { struct flock fl; setcloexec(*lockfd, filename); file_lock_setup(&fl, F_WRLCK); if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) { if (emsg_eagain && (errno == EACCES || errno == EAGAIN)) ohshit(emsg_eagain); ohshite(emsg); } push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 1, lockfd); }
void subproc_signals_setup(const char *name) { struct sigaction sa; size_t i; onerr_abort++; memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; for (i = 0; i < array_count(signo_ignores); i++) if (sigaction(signo_ignores[i], &sa, &sa_save[i])) ohshite(_("unable to ignore signal %s before running %.250s"), strsignal(signo_ignores[i]), name); push_cleanup(subproc_signals_cleanup, ~0, NULL, 0, 0); onerr_abort--; }
static bool deb_reassemble(const char **filename, const char **pfilename) { static char *reasmbuf = NULL; struct stat stab; int status; pid_t pid; if (!reasmbuf) reasmbuf = dpkg_db_get_path(REASSEMBLETMP); if (unlink(reasmbuf) && errno != ENOENT) ohshite(_("error ensuring `%.250s' doesn't exist"), reasmbuf); push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf); pid = subproc_fork(); if (!pid) { execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, *filename, NULL); ohshite(_("unable to execute %s (%s)"), _("split package reassembly"), SPLITTER); } status = subproc_wait(pid, SPLITTER); switch (WIFEXITED(status) ? WEXITSTATUS(status) : -1) { case 0: /* It was a part - is it complete? */ if (!stat(reasmbuf, &stab)) { /* Yes. */ *filename = reasmbuf; *pfilename = _("reassembled package file"); break; } else if (errno == ENOENT) { /* No. That's it, we skip it. */ return false; } case 1: /* No, it wasn't a part. */ break; default: subproc_check(status, SPLITTER, 0); } return true; }
static void file_copy(const char *src, const char *dst) { struct dpkg_error err; char *tmp; int srcfd, dstfd; srcfd = open(src, O_RDONLY); if (srcfd < 0) ohshite(_("unable to open file '%s'"), src); tmp = str_fmt("%s%s", dst, ".dpkg-divert.tmp"); dstfd = creat(tmp, 0600); if (dstfd < 0) ohshite(_("unable to create file '%s'"), tmp); push_cleanup(cu_filename, ~ehflag_normaltidy, 1, tmp); if (fd_fd_copy(srcfd, dstfd, -1, &err) < 0) ohshit(_("cannot copy '%s' to '%s': %s"), src, tmp, err.str); close(srcfd); if (fsync(dstfd)) ohshite(_("unable to sync file '%s'"), tmp); if (close(dstfd)) ohshite(_("unable to close file '%s'"), tmp); file_copy_perms(src, tmp); if (rename(tmp, dst) != 0) ohshite(_("cannot rename '%s' to '%s'"), tmp, dst); free(tmp); pop_cleanup(ehflag_normaltidy); }
/** * Load the list of files in this package into memory, or update the * list if it is there but stale. */ void ensure_packagefiles_available(struct pkginfo *pkg) { static int fd; const char *filelistfile; struct fileinlist **lendp; struct stat stat_buf; char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr; if (pkg->clientdata && pkg->clientdata->fileslistvalid) return; ensure_package_clientdata(pkg); /* Throw away any stale data, if there was any. */ pkg_files_blank(pkg); /* Packages which aren't installed don't have a files list. */ if (pkg->status == stat_notinstalled) { pkg->clientdata->fileslistvalid = true; return; } filelistfile = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE); onerr_abort++; fd= open(filelistfile,O_RDONLY); if (fd==-1) { if (errno != ENOENT) ohshite(_("unable to open files list file for package `%.250s'"), pkg_name(pkg, pnaw_nonambig)); onerr_abort--; if (pkg->status != stat_configfiles && dpkg_version_is_informative(&pkg->configversion)) { warning(_("files list file for package '%.250s' missing; assuming " "package has no files currently installed"), pkg_name(pkg, pnaw_nonambig)); } pkg->clientdata->files = NULL; pkg->clientdata->fileslistvalid = true; return; } push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd); if (fstat(fd, &stat_buf)) ohshite(_("unable to stat files list file for package '%.250s'"), pkg_name(pkg, pnaw_nonambig)); if (!S_ISREG(stat_buf.st_mode)) ohshit(_("files list for package '%.250s' is not a regular file"), pkg_name(pkg, pnaw_nonambig)); if (stat_buf.st_size) { loaded_list = nfmalloc(stat_buf.st_size); loaded_list_end = loaded_list + stat_buf.st_size; if (fd_read(fd, loaded_list, stat_buf.st_size) < 0) ohshite(_("reading files list for package '%.250s'"), pkg_name(pkg, pnaw_nonambig)); lendp= &pkg->clientdata->files; thisline = loaded_list; while (thisline < loaded_list_end) { struct filenamenode *namenode; ptr = memchr(thisline, '\n', loaded_list_end - thisline); if (ptr == NULL) ohshit(_("files list file for package '%.250s' is missing final newline"), pkg_name(pkg, pnaw_nonambig)); /* Where to start next time around. */ nextline = ptr + 1; /* Strip trailing ‘/’. */ if (ptr > thisline && ptr[-1] == '/') ptr--; /* Add the file to the list. */ if (ptr == thisline) ohshit(_("files list file for package `%.250s' contains empty filename"), pkg_name(pkg, pnaw_nonambig)); *ptr = '\0'; namenode = findnamenode(thisline, fnn_nocopy); lendp = pkg_files_add_file(pkg, namenode, lendp); thisline = nextline; } } pop_cleanup(ehflag_normaltidy); /* fd = open() */ if (close(fd)) ohshite(_("error closing files list file for package `%.250s'"), pkg_name(pkg, pnaw_nonambig)); onerr_abort--; pkg->clientdata->fileslistvalid = true; }
/** * Load the list of files in this package into memory, or update the * list if it is there but stale. */ void ensure_packagefiles_available(struct pkginfo *pkg) { static int fd; const char *filelistfile; struct fileinlist **lendp; struct stat stat_buf; char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr; if (pkg->clientdata && pkg->clientdata->fileslistvalid) return; ensure_package_clientdata(pkg); /* Throw away any stale data, if there was any. */ pkg_files_blank(pkg); /* Packages which aren't installed don't have a files list. */ if (pkg->status == stat_notinstalled) { pkg->clientdata->fileslistvalid = true; return; } /* In a package set that is not multiarch same, there's only * one .list file. Don't read it multiple times. */ if (pkg->installed.multiarch != multiarch_same) { struct pkginfo *otherpkg; for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) { /* If we find a non-multiarch-same package in the set with a more * advanced status, we assume the list file is not for this package. */ if (otherpkg->status > pkg->status && otherpkg->installed.multiarch != multiarch_same) { pkg->clientdata->fileslistvalid = true; return; } } } filelistfile = pkgadminfile(pkg, &pkg->installed, LISTFILE); onerr_abort++; fd= open(filelistfile,O_RDONLY); if (fd==-1) { if (errno != ENOENT) ohshite(_("unable to open files list file for package `%.250s'"), pkg_describe(pkg, pdo_foreign)); onerr_abort--; if (pkg->status != stat_configfiles) { if (saidread == 1) putc('\n',stderr); warning(_("files list file for package `%.250s' missing, assuming " "package has no files currently installed."), pkg_describe(pkg, pdo_foreign)); } pkg->clientdata->files = NULL; pkg->clientdata->fileslistvalid = true; return; } push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd); if(fstat(fd, &stat_buf)) ohshite(_("unable to stat files list file for package '%.250s'"), pkg_describe(pkg, pdo_foreign)); if (stat_buf.st_size) { loaded_list = nfmalloc(stat_buf.st_size); loaded_list_end = loaded_list + stat_buf.st_size; if (fd_read(fd, loaded_list, stat_buf.st_size) < 0) ohshite(_("reading files list for package '%.250s'"), pkg_describe(pkg, pdo_foreign)); lendp= &pkg->clientdata->files; thisline = loaded_list; while (thisline < loaded_list_end) { if (!(ptr = memchr(thisline, '\n', loaded_list_end - thisline))) ohshit(_("files list file for package '%.250s' is missing final newline"), pkg_describe(pkg, pdo_foreign)); /* Where to start next time around. */ nextline = ptr + 1; /* Strip trailing ‘/’. */ if (ptr > thisline && ptr[-1] == '/') ptr--; /* Add the file to the list. */ if (ptr == thisline) ohshit(_("files list file for package `%.250s' contains empty filename"), pkg_describe(pkg, pdo_foreign)); *ptr = '\0'; lendp = pkg_files_add_file(pkg, thisline, fnn_nocopy, lendp); thisline = nextline; } } pop_cleanup(ehflag_normaltidy); /* fd = open() */ if (close(fd)) ohshite(_("error closing files list file for package `%.250s'"), pkg_describe(pkg, pdo_foreign)); onerr_abort--; pkg->clientdata->fileslistvalid = true; }
int parsedb(const char *filename, enum parsedbflags flags, struct pkginfo **donep, FILE *warnto, int *warncount) { /* warnto, warncount and donep may be null. * If donep is not null only one package's information is expected. */ static int fd; struct pkginfo newpig, *pigp; struct pkginfoperfile *newpifp, *pifp; struct arbitraryfield *arp, **larpp; struct trigaw *ta; int pdone; int fieldencountered[array_count(fieldinfos)]; const struct fieldinfo *fip; const struct nickname *nick; char *data, *dataptr, *endptr; const char *fieldstart, *valuestart; char *value= NULL; int fieldlen= 0, valuelen= 0; int *ip, c; struct stat st; struct parsedb_state ps; ps.filename = filename; ps.flags = flags; ps.lno = 0; ps.warnto = warnto; ps.warncount = 0; newpifp= (flags & pdb_recordavailable) ? &newpig.available : &newpig.installed; fd= open(filename, O_RDONLY); if (fd == -1) ohshite(_("failed to open package info file `%.255s' for reading"),filename); push_cleanup(cu_closefd, ~ehflag_normaltidy, NULL, 0, 1, &fd); if (fstat(fd, &st) == -1) ohshite(_("can't stat package info file `%.255s'"),filename); if (st.st_size > 0) { #ifdef USE_MMAP dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (dataptr == MAP_FAILED) ohshite(_("can't mmap package info file `%.255s'"),filename); #else dataptr = m_malloc(st.st_size); fd_buf_copy(fd, dataptr, st.st_size, _("copy info file `%.255s'"), filename); #endif data= dataptr; endptr = dataptr + st.st_size; } else { data= dataptr= endptr= NULL; } pdone= 0; #define EOF_mmap(dataptr, endptr) (dataptr >= endptr) #define getc_mmap(dataptr) *dataptr++; #define ungetc_mmap(c, dataptr, data) dataptr--; for (;;) { /* loop per package */ memset(fieldencountered, 0, sizeof(fieldencountered)); blankpackage(&newpig); /* Skip adjacent new lines */ while(!EOF_mmap(dataptr, endptr)) { c= getc_mmap(dataptr); if (c!='\n' && c!=MSDOS_EOF_CHAR ) break; ps.lno++; } if (EOF_mmap(dataptr, endptr)) break; for (;;) { /* loop per field */ fieldstart= dataptr - 1; while (!EOF_mmap(dataptr, endptr) && !isspace(c) && c!=':' && c!=MSDOS_EOF_CHAR) c= getc_mmap(dataptr); fieldlen= dataptr - fieldstart - 1; while (!EOF_mmap(dataptr, endptr) && c != '\n' && isspace(c)) c= getc_mmap(dataptr); if (EOF_mmap(dataptr, endptr)) parse_error(&ps, &newpig, _("EOF after field name `%.*s'"), fieldlen, fieldstart); if (c == '\n') parse_error(&ps, &newpig, _("newline in field name `%.*s'"), fieldlen, fieldstart); if (c == MSDOS_EOF_CHAR) parse_error(&ps, &newpig, _("MSDOS EOF (^Z) in field name `%.*s'"), fieldlen, fieldstart); if (c != ':') parse_error(&ps, &newpig, _("field name `%.*s' must be followed by colon"), fieldlen, fieldstart); /* Skip space after ':' but before value and eol */ while(!EOF_mmap(dataptr, endptr)) { c= getc_mmap(dataptr); if (c == '\n' || !isspace(c)) break; } if (EOF_mmap(dataptr, endptr)) parse_error(&ps, &newpig, _("EOF before value of field `%.*s' (missing final newline)"), fieldlen,fieldstart); if (c == MSDOS_EOF_CHAR) parse_error(&ps, &newpig, _("MSDOS EOF char in value of field `%.*s' (missing newline?)"), fieldlen,fieldstart); valuestart= dataptr - 1; for (;;) { if (c == '\n' || c == MSDOS_EOF_CHAR) { ps.lno++; if (EOF_mmap(dataptr, endptr)) break; c= getc_mmap(dataptr); /* Found double eol, or start of new field */ if (EOF_mmap(dataptr, endptr) || c == '\n' || !isspace(c)) break; ungetc_mmap(c,dataptr, data); c= '\n'; } else if (EOF_mmap(dataptr, endptr)) { parse_error(&ps, &newpig, _("EOF during value of field `%.*s' (missing final newline)"), fieldlen,fieldstart); } c= getc_mmap(dataptr); } valuelen= dataptr - valuestart - 1; /* trim ending space on value */ while (valuelen && isspace(*(valuestart+valuelen-1))) valuelen--; for (nick = nicknames; nick->nick && (strncasecmp(nick->nick, fieldstart, fieldlen) || nick->nick[fieldlen] != '\0'); nick++) ; if (nick->nick) { fieldstart= nick->canon; fieldlen= strlen(fieldstart); } for (fip= fieldinfos, ip= fieldencountered; fip->name && strncasecmp(fieldstart,fip->name, fieldlen); fip++, ip++); if (fip->name) { value = m_realloc(value, valuelen + 1); memcpy(value,valuestart,valuelen); *(value + valuelen) = '\0'; if ((*ip)++) parse_error(&ps, &newpig, _("duplicate value for `%s' field"), fip->name); fip->rcall(&newpig, newpifp, &ps, value, fip); } else { if (fieldlen<2) parse_error(&ps, &newpig, _("user-defined field name `%.*s' too short"), fieldlen, fieldstart); larpp= &newpifp->arbs; while ((arp= *larpp) != NULL) { if (!strncasecmp(arp->name,fieldstart,fieldlen)) parse_error(&ps, &newpig, _("duplicate value for user-defined field `%.*s'"), fieldlen, fieldstart); larpp= &arp->next; } arp= nfmalloc(sizeof(struct arbitraryfield)); arp->name= nfstrnsave(fieldstart,fieldlen); arp->value= nfstrnsave(valuestart,valuelen); arp->next= NULL; *larpp= arp; } if (EOF_mmap(dataptr, endptr) || c == '\n' || c == MSDOS_EOF_CHAR) break; } /* loop per field */ if (pdone && donep) parse_error(&ps, &newpig, _("several package info entries found, only one allowed")); parse_must_have_field(&ps, &newpig, newpig.name, "package name"); if ((flags & pdb_recordavailable) || newpig.status != stat_notinstalled) { parse_ensure_have_field(&ps, &newpig, &newpifp->description, "description"); parse_ensure_have_field(&ps, &newpig, &newpifp->maintainer, "maintainer"); if (newpig.status != stat_halfinstalled) parse_must_have_field(&ps, &newpig, newpifp->version.version, "version"); } if (flags & pdb_recordavailable) parse_ensure_have_field(&ps, &newpig, &newpifp->architecture, "architecture"); /* Check the Config-Version information: * If there is a Config-Version it is definitely to be used, but * there shouldn't be one if the package is `installed' (in which case * the Version and/or Revision will be copied) or if the package is * `not-installed' (in which case there is no Config-Version). */ if (!(flags & pdb_recordavailable)) { if (newpig.configversion.version) { if (newpig.status == stat_installed || newpig.status == stat_notinstalled) parse_error(&ps, &newpig, _("Configured-Version for package with inappropriate Status")); } else { if (newpig.status == stat_installed) newpig.configversion= newpifp->version; } } if (newpig.trigaw.head && (newpig.status <= stat_configfiles || newpig.status >= stat_triggerspending)) parse_error(&ps, &newpig, _("package has status %s but triggers are awaited"), statusinfos[newpig.status].name); else if (newpig.status == stat_triggersawaited && !newpig.trigaw.head) parse_error(&ps, &newpig, _("package has status triggers-awaited but no triggers " "awaited")); if (!(newpig.status == stat_triggerspending || newpig.status == stat_triggersawaited) && newpig.trigpend_head) parse_error(&ps, &newpig, _("package has status %s but triggers are pending"), statusinfos[newpig.status].name); else if (newpig.status == stat_triggerspending && !newpig.trigpend_head) parse_error(&ps, &newpig, _("package has status triggers-pending but no triggers " "pending")); /* FIXME: There was a bug that could make a not-installed package have * conffiles, so we check for them here and remove them (rather than * calling it an error, which will do at some point). */ if (!(flags & pdb_recordavailable) && newpig.status == stat_notinstalled && newpifp->conffiles) { parse_warn(&ps, &newpig, _("Package which in state not-installed has conffiles, " "forgetting them")); newpifp->conffiles= NULL; } /* XXX: Mark not-installed leftover packages for automatic removal on * next database dump. This code can be removed after dpkg 1.16.x, when * there's guarantee that no leftover is found on the status file on * major distributions. */ if (!(flags & pdb_recordavailable) && newpig.status == stat_notinstalled && newpig.eflag == eflag_ok && (newpig.want == want_purge || newpig.want == want_deinstall || newpig.want == want_hold)) { newpig.want = want_unknown; } pigp= findpackage(newpig.name); pifp= (flags & pdb_recordavailable) ? &pigp->available : &pigp->installed; if ((flags & pdb_ignoreolder) && versioncompare(&newpifp->version, &pifp->version) < 0) continue; /* Copy the priority and section across, but don't overwrite existing * values if the pdb_weakclassification flag is set. */ if (newpig.section && *newpig.section && !((flags & pdb_weakclassification) && pigp->section && *pigp->section)) pigp->section= newpig.section; if (newpig.priority != pri_unknown && !((flags & pdb_weakclassification) && pigp->priority != pri_unknown)) { pigp->priority= newpig.priority; if (newpig.priority == pri_other) pigp->otherpriority= newpig.otherpriority; } /* Sort out the dependency mess. */ copy_dependency_links(pigp,&pifp->depends,newpifp->depends, (flags & pdb_recordavailable) ? 1 : 0); /* Leave the `depended' pointer alone, we've just gone to such * trouble to get it right :-). The `depends' pointer in * pifp was indeed also updated by copy_dependency_links, * but since the value was that from newpifp anyway there's * no need to copy it back. */ newpifp->depended= pifp->depended; /* Copy across data */ memcpy(pifp,newpifp,sizeof(struct pkginfoperfile)); if (!(flags & pdb_recordavailable)) { pigp->want= newpig.want; pigp->eflag= newpig.eflag; pigp->status= newpig.status; pigp->configversion= newpig.configversion; pigp->files= NULL; pigp->trigpend_head = newpig.trigpend_head; pigp->trigaw = newpig.trigaw; for (ta = pigp->trigaw.head; ta; ta = ta->sameaw.next) { assert(ta->aw == &newpig); ta->aw = pigp; /* ->othertrigaw_head is updated by trig_note_aw in *(findpackage()) * rather than in newpig */ } } else if (!(flags & pdb_ignorefiles)) { pigp->files= newpig.files; } if (donep) *donep= pigp; pdone++; if (EOF_mmap(dataptr, endptr)) break; if (c == '\n') ps.lno++; } if (data != NULL) { #ifdef USE_MMAP munmap(data, st.st_size); #else free(data); #endif } free(value); pop_cleanup(ehflag_normaltidy); if (close(fd)) ohshite(_("failed to close after read: `%.255s'"),filename); if (donep && !pdone) ohshit(_("no package information in `%.255s'"),filename); if (warncount) *warncount = ps.warncount; return pdone; }
void process_archive(const char *filename) { static const struct TarFunctions tf = { tarfileread, tarobject, tarobject, tarobject, tarobject, tarobject }; /* These need to be static so that we can pass their addresses to * push_cleanup as arguments to the cu_xxx routines; if an error occurs * we unwind the stack before processing the cleanup list, and these * variables had better still exist ... */ static int p1[2]; static char cidirtmpnambuf[L_tmpnam+100]; static char *cidirbuf = NULL, *reasmbuf = NULL; static struct fileinlist *newconffiles, *newfileslist; static enum pkgstatus oldversionstatus; static struct varbuf infofnvb, fnvb, depprobwhy; static struct tarcontext tc; int c1, r, admindirlen, i, infodirlen, infodirbaseused, status; struct pkgiterator *it; struct pkginfo *pkg, *otherpkg, *divpkg; char *cidir, *cidirrest, *p; char *pfilenamebuf, conffilenamebuf[MAXCONFFILENAME]; char *psize; const char *pfilename, *newinfofilename; struct fileinlist *newconff, **newconffileslastp; struct fileinlist *cfile; struct reversefilelistiter rlistit; struct conffile *searchconff, **iconffileslastp, *newiconff; struct filepackages *packageslump; struct dependency *dsearch, *newdeplist, **newdeplistlastp; struct dependency *newdep, *dep, *providecheck; struct deppossi *psearch, **newpossilastp, *possi, *newpossi, *pdep; FILE *conff; DIR *dsd; struct filenamenode *namenode; struct dirent *de; struct stat stab, oldfs; struct pkg_deconf_list *deconpil, *deconpiltemp; cleanup_pkg_failed= cleanup_conflictor_failed= 0; admindirlen= strlen(admindir); for (pfilename= filename ; pfilename && strlen(pfilename) > 30 && strchr(pfilename,'/') != NULL ; pfilename++ ) pfilename= strchr(pfilename,'/'); if (pfilename && pfilename != filename) { pfilenamebuf= (char *)nfmalloc(strlen(pfilename)+5); strcpy(pfilenamebuf,".../"); strcat(pfilenamebuf,pfilename); pfilename= pfilenamebuf; } else { pfilename= filename; } if (stat(filename,&stab)) ohshite(_("cannot access archive")); if (!f_noact) { /* We can't `tentatively-reassemble' packages. */ if (!reasmbuf) { reasmbuf= m_malloc(admindirlen+sizeof(REASSEMBLETMP)+5); strcpy(reasmbuf,admindir); strcat(reasmbuf,"/" REASSEMBLETMP); } if (unlink(reasmbuf) && errno != ENOENT) ohshite(_("error ensuring `%.250s' doesn't exist"),reasmbuf); push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf); c1= m_fork(); if (!c1) { execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, filename, NULL); ohshite(_("failed to exec dpkg-split to see if it's part of a multiparter")); } while ((r= waitpid(c1,&status,0)) == -1 && errno == EINTR); if (r != c1) { onerr_abort++; ohshite(_("wait for dpkg-split failed")); } switch (WIFEXITED(status) ? WEXITSTATUS(status) : -1) { case 0: /* It was a part - is it complete ? */ if (!stat(reasmbuf,&stab)) { /* Yes. */ filename= reasmbuf; pfilename= _("reassembled package file"); break; } else if (errno == ENOENT) { /* No. That's it, we skip it. */ return; } case 1: /* No, it wasn't a part. */ break; default: checksubprocerr(status,SPLITTER,0); } } /* Verify the package. */ if (!f_nodebsig && (stat(DEBSIGVERIFY, &stab)==0)) { printf(_("Authenticating %s ...\n"), filename); fflush(stdout); c1 = m_fork(); if (!c1) { execl(DEBSIGVERIFY, DEBSIGVERIFY, "-q", filename, NULL); ohshite(_("failed to execl debsig-verify")); } else { int status; waitpid(c1, &status, 0); if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) { if (! fc_badverify) { ohshit(_("Verification on package %s failed!"), filename); } else { fprintf(stderr, _("Verification on package %s failed,\nbut installing anyway as you requested.\n"), filename); } } else { printf(_("passed\n")); } } } if (f_noact) { cidir= cidirtmpnambuf; /* We use tmpnam here, not to get a unique filename, but to get a unique directory. */ if (!tmpnam(cidir)) ohshite(_("unable to get unique filename for control info")); strcat(cidir,"/"); } else { /* We want it to be on the same filesystem so that we can * use rename(2) to install the postinst &c. */ if (!cidirbuf) cidirbuf= m_malloc(admindirlen+sizeof(CONTROLDIRTMP)+MAXCONTROLFILENAME+10); cidir= cidirbuf; strcpy(cidir,admindir); strcat(cidir, "/" CONTROLDIRTMP); } cidirrest= cidir + strlen(cidir); assert(*cidir && cidirrest[-1] == '/'); cidirrest[-1] = '\0'; ensure_pathname_nonexisting(cidir); cidirrest[-1]= '/'; push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest); c1= m_fork(); if (!c1) { cidirrest[-1] = '\0'; execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL); ohshite(_("failed to exec dpkg-deb to extract control information")); } waitsubproc(c1,BACKEND " --control",0); strcpy(cidirrest,CONTROLFILE); parsedb(cidir, pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles, &pkg,NULL,NULL); if (!pkg->files) { pkg->files= nfmalloc(sizeof(struct filedetails)); pkg->files->next = NULL; pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL; } /* Always nfmalloc. Otherwise, we may overwrite some other field(like md5sum). */ psize = nfmalloc(30); sprintf(psize, "%lu", (unsigned long)stab.st_size); pkg->files->size = psize; if (cipaction->arg == act_avail) { printf(_("Recorded info about %s from %s.\n"),pkg->name,pfilename); pop_cleanup(ehflag_normaltidy); return; } if (pkg->available.architecture && *pkg->available.architecture && strcmp(pkg->available.architecture,"all") && strcmp(pkg->available.architecture,architecture)) forcibleerr(fc_architecture, _("package architecture (%s) does not match system (%s)"), pkg->available.architecture,architecture); if (!pkg->installed.valid) blankpackageperfile(&pkg->installed); assert(pkg->available.valid); for (deconpil= deconfigure; deconpil; deconpil= deconpiltemp) { deconpiltemp= deconpil->next; free(deconpil); } deconfigure = NULL; clear_istobes(); if (!wanttoinstall(pkg,&pkg->available.version,1)) { pop_cleanup(ehflag_normaltidy); return; } /* Check if anything is installed that we conflict with, or not installed * that we need */ pkg->clientdata->istobe= itb_installnew; for (dsearch= pkg->available.depends; dsearch; dsearch= dsearch->next) { switch (dsearch->type) { case dep_conflicts: /* Look for things we conflict with. */ check_conflict(dsearch, pkg, pfilename); break; case dep_breaks: /* Look for things we break. */ check_breaks(dsearch, pkg, pfilename); break; case dep_provides: /* Look for things that conflict with what we provide. */ if (dsearch->list->ed->installed.valid) { for (psearch= dsearch->list->ed->installed.depended; psearch; psearch= psearch->nextrev) { if (psearch->up->type != dep_conflicts) continue; check_conflict(psearch->up, pkg, pfilename); } } break; case dep_suggests: case dep_recommends: case dep_depends: case dep_replaces: case dep_enhances: /* Ignore these here. */ break; case dep_predepends: if (!depisok(dsearch, &depprobwhy, NULL, 1)) { varbufaddc(&depprobwhy,0); fprintf(stderr, _("dpkg: regarding %s containing %s, pre-dependency problem:\n%s"), pfilename, pkg->name, depprobwhy.buf); if (!force_depends(dsearch->list)) ohshit(_("pre-dependency problem - not installing %.250s"),pkg->name); warning(_("ignoring pre-dependency problem!")); } } } /* Look for things that conflict with us. */ for (psearch= pkg->installed.depended; psearch; psearch= psearch->nextrev) { if (psearch->up->type != dep_conflicts) continue; check_conflict(psearch->up, pkg, pfilename); } ensure_allinstfiles_available(); filesdbinit(); trig_file_interests_ensure(); if (pkg->status != stat_notinstalled && pkg->status != stat_configfiles) { printf(_("Preparing to replace %s %s (using %s) ...\n"), pkg->name, versiondescribe(&pkg->installed.version,vdew_nonambig), pfilename); log_action("upgrade", pkg); } else { printf(_("Unpacking %s (from %s) ...\n"),pkg->name,pfilename); log_action("install", pkg); } if (f_noact) { pop_cleanup(ehflag_normaltidy); return; } /* * OK, we're going ahead. */ trig_activate_packageprocessing(pkg); strcpy(cidirrest, TRIGGERSCIFILE); trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg); /* Read the conffiles, and copy the hashes across. */ newconffiles = NULL; newconffileslastp = &newconffiles; push_cleanup(cu_fileslist, ~0, NULL, 0, 0); strcpy(cidirrest,CONFFILESFILE); conff= fopen(cidir,"r"); if (conff) { push_cleanup(cu_closefile, ehflag_bombout, NULL, 0, 1, (void *)conff); while (fgets(conffilenamebuf,MAXCONFFILENAME-2,conff)) { p= conffilenamebuf + strlen(conffilenamebuf); assert(p != conffilenamebuf); if (p[-1] != '\n') ohshit(_("name of conffile (starting `%.250s') is too long (>%d characters)"), conffilenamebuf, MAXCONFFILENAME); while (p > conffilenamebuf && isspace(p[-1])) --p; if (p == conffilenamebuf) continue; *p = '\0'; namenode= findnamenode(conffilenamebuf, 0); namenode->oldhash= NEWCONFFILEFLAG; newconff= newconff_append(&newconffileslastp, namenode); /* Let's see if any packages have this file. If they do we * check to see if they listed it as a conffile, and if they did * we copy the hash across. Since (for plain file conffiles, * which is the only kind we are supposed to have) there will * only be one package which `has' the file, this will usually * mean we only look in the package which we're installing now. * The `conffiles' data in the status file is ignored when a * package isn't also listed in the file ownership database as * having that file. If several packages are listed as owning * the file we pick one at random. */ searchconff = NULL; for (packageslump= newconff->namenode->packages; packageslump; packageslump= packageslump->more) { for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) { otherpkg= packageslump->pkgs[i]; debug(dbg_conffdetail,"process_archive conffile `%s' in package %s - conff ?", newconff->namenode->name,otherpkg->name); for (searchconff= otherpkg->installed.conffiles; searchconff && strcmp(newconff->namenode->name,searchconff->name); searchconff= searchconff->next) debug(dbg_conffdetail, "process_archive conffile `%s' in package %s - conff ? not `%s'", newconff->namenode->name,otherpkg->name,searchconff->name); if (searchconff) { debug(dbg_conff,"process_archive conffile `%s' package=%s %s hash=%s", newconff->namenode->name,otherpkg->name, otherpkg == pkg ? "same" : "different!", searchconff->hash); if (otherpkg == pkg) goto xit_conff_hashcopy_srch; } } } xit_conff_hashcopy_srch: if (searchconff) { newconff->namenode->oldhash= searchconff->hash; /* we don't copy `obsolete'; it's not obsolete in the new package */ } else { debug(dbg_conff,"process_archive conffile `%s' no package, no hash", newconff->namenode->name); } newconff->namenode->flags |= fnnf_new_conff; } if (ferror(conff)) ohshite(_("read error in %.250s"),cidir); pop_cleanup(ehflag_normaltidy); /* conff= fopen() */ if (fclose(conff)) ohshite(_("error closing %.250s"),cidir); } else { if (errno != ENOENT) ohshite(_("error trying to open %.250s"),cidir); } /* All the old conffiles are marked with a flag, so that we don't delete * them if they seem to disappear completely. */ oldconffsetflags(pkg->installed.conffiles); for (i = 0 ; i < cflict_index ; i++) { oldconffsetflags(conflictor[i]->installed.conffiles); } oldversionstatus= pkg->status; assert(oldversionstatus <= stat_installed); debug(dbg_general,"process_archive oldversionstatus=%s", statusstrings[oldversionstatus]); if (oldversionstatus == stat_halfconfigured || oldversionstatus == stat_triggersawaited || oldversionstatus == stat_triggerspending || oldversionstatus == stat_installed) { pkg->eflag |= eflag_reinstreq; pkg->status= stat_halfconfigured; modstatdb_note(pkg); push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg); maintainer_script_alternative(pkg, PRERMFILE, "pre-removal", cidir, cidirrest, "upgrade", "failed-upgrade"); pkg->status= stat_unpacked; oldversionstatus= stat_unpacked; modstatdb_note(pkg); } for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) { struct pkginfo *removing = deconpil->pkg_removal; if (removing) printf(_("De-configuring %s, to allow removal of %s ...\n"), deconpil->pkg->name, removing->name); else printf(_("De-configuring %s ...\n"), deconpil->pkg->name); trig_activate_packageprocessing(deconpil->pkg); deconpil->pkg->status= stat_halfconfigured; modstatdb_note(deconpil->pkg); /* This means that we *either* go and run postinst abort-deconfigure, * *or* queue the package for later configure processing, depending * on which error cleanup route gets taken. */ push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy, ok_prermdeconfigure, ehflag_normaltidy, 3, (void*)deconpil->pkg, (void*)removing, (void*)pkg); if (removing) { maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal", "deconfigure", "in-favour", pkg->name, versiondescribe(&pkg->available.version, vdew_nonambig), "removing", removing->name, versiondescribe(&removing->installed.version, vdew_nonambig), NULL); } else { maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal", "deconfigure", "in-favour", pkg->name, versiondescribe(&pkg->available.version, vdew_nonambig), NULL); } } for (i = 0 ; i < cflict_index; i++) { if (!(conflictor[i]->status == stat_halfconfigured || conflictor[i]->status == stat_triggersawaited || conflictor[i]->status == stat_triggerspending || conflictor[i]->status == stat_installed)) continue; trig_activate_packageprocessing(conflictor[i]); conflictor[i]->status= stat_halfconfigured; modstatdb_note(conflictor[i]); push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0, 2,(void*)conflictor[i],(void*)pkg); maintainer_script_installed(conflictor[i], PRERMFILE, "pre-removal", "remove", "in-favour", pkg->name, versiondescribe(&pkg->available.version, vdew_nonambig), NULL); conflictor[i]->status= stat_halfinstalled; modstatdb_note(conflictor[i]); } pkg->eflag |= eflag_reinstreq; if (pkg->status == stat_notinstalled) pkg->installed.version= pkg->available.version; pkg->status= stat_halfinstalled; modstatdb_note(pkg); if (oldversionstatus == stat_notinstalled) { push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0, 3,(void*)pkg,(void*)cidir,(void*)cidirrest); maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest, "install", NULL); } else if (oldversionstatus == stat_configfiles) { push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0, 3,(void*)pkg,(void*)cidir,(void*)cidirrest); maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest, "install", versiondescribe(&pkg->installed.version, vdew_nonambig), NULL); } else { push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0, 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus); maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest, "upgrade", versiondescribe(&pkg->installed.version, vdew_nonambig), NULL); printf(_("Unpacking replacement %.250s ...\n"),pkg->name); } /* * Now we unpack the archive, backing things up as we go. * For each file, we check to see if it already exists. * There are several possibilities: * + We are trying to install a non-directory ... * - It doesn't exist. In this case we simply extract it. * - It is a plain file, device, symlink, &c. We do an `atomic * overwrite' using link() and rename(), but leave a backup copy. * Later, when we delete the backup, we remove it from any other * packages' lists. * - It is a directory. In this case it depends on whether we're * trying to install a symlink or something else. * = If we're not trying to install a symlink we move the directory * aside and extract the node. Later, when we recursively remove * the backed-up directory, we remove it from any other packages' * lists. * = If we are trying to install a symlink we do nothing - ie, * dpkg will never replace a directory tree with a symlink. This * is to avoid embarrassing effects such as replacing a directory * tree with a link to a link to the original directory tree. * + We are trying to install a directory ... * - It doesn't exist. We create it with the appropriate modes. * - It exists as a directory or a symlink to one. We do nothing. * - It is a plain file or a symlink (other than to a directory). * We move it aside and create the directory. Later, when we * delete the backup, we remove it from any other packages' lists. * * Install non-dir Install symlink Install dir * Exists not X X X * File/node/symlink LXR LXR BXR * Directory BXR - - * * X: extract file/node/link/directory * LX: atomic overwrite leaving backup * B: ordinary backup * R: later remove from other packages' lists * -: do nothing * * After we've done this we go through the remaining things in the * lists of packages we're trying to remove (including the old * version of the current package). This happens in reverse order, * so that we process files before the directories (or symlinks-to- * directories) containing them. * + If the thing is a conffile then we leave it alone for the purge * operation. * + Otherwise, there are several possibilities too: * - The listed thing does not exist. We ignore it. * - The listed thing is a directory or a symlink to a directory. * We delete it only if it isn't listed in any other package. * - The listed thing is not a directory, but was part of the package * that was upgraded, we check to make sure the files aren't the * same ones from the old package by checking dev/inode * - The listed thing is not a directory or a symlink to one (ie, * it's a plain file, device, pipe, &c, or a symlink to one, or a * dangling symlink). We delete it. * The removed packages' list becomes empty (of course, the new * version of the package we're installing will have a new list, * which replaces the old version's list). * * If at any stage we remove a file from a package's list, and the * package isn't one we're already processing, and the package's * list becomes empty as a result, we `vanish' the package. This * means that we run its postrm with the `disappear' argument, and * put the package in the `not-installed' state. If it had any * conffiles, their hashes and ownership will have been transferred * already, so we just ignore those and forget about them from the * point of view of the disappearing package. * * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the * files get replaced `as we go'. */ m_pipe(p1); push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]); c1= m_fork(); if (!c1) { m_dup2(p1[1],1); close(p1[0]); close(p1[1]); execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL); ohshite(_("unable to exec dpkg-deb to get filesystem archive")); } close(p1[1]); p1[1] = -1; newfileslist = NULL; tc.newfilesp = &newfileslist; push_cleanup(cu_fileslist, ~0, NULL, 0, 0); tc.pkg= pkg; tc.backendpipe= p1[0]; r= TarExtractor((void*)&tc, &tf); if (r) { if (errno) { ohshite(_("error reading dpkg-deb tar output")); } else { ohshit(_("corrupted filesystem tarfile - corrupted package archive")); } } fd_null_copy(p1[0], -1, _("dpkg-deb: zap possible trailing zeros")); close(p1[0]); p1[0] = -1; waitsubproc(c1,BACKEND " --fsys-tarfile",PROCPIPE); if (oldversionstatus == stat_halfinstalled || oldversionstatus == stat_unpacked) { /* Packages that were in `installed' and `postinstfailed' have been reduced * to `unpacked' by now, by the running of the prerm script. */ pkg->status= stat_halfinstalled; modstatdb_note(pkg); push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg); maintainer_script_alternative(pkg, POSTRMFILE, "post-removal", cidir, cidirrest, "upgrade", "failed-upgrade"); } /* If anything goes wrong while tidying up it's a bit late to do * anything about it. However, we don't install the new status * info yet, so that a future dpkg installation will put everything * right (we hope). * * If something does go wrong later the `conflictor' package will be * left in the `removal_failed' state. Removing or installing it * will be impossible if it was required because of the conflict with * the package we're installing now and (presumably) the dependency * by other packages. This means that the files it contains in * common with this package will hang around until we successfully * get this package installed, after which point we can trust the * conflicting package's file list, which will have been updated to * remove any files in this package. */ push_checkpoint(~ehflag_bombout, ehflag_normaltidy); /* Now we delete all the files that were in the old version of * the package only, except (old or new) conffiles, which we leave * alone. */ reversefilelist_init(&rlistit,pkg->clientdata->files); while ((namenode= reversefilelist_next(&rlistit))) { struct filenamenode *usenode; if ((namenode->flags & fnnf_new_conff) || (namenode->flags & fnnf_new_inarchive)) continue; usenode = namenodetouse(namenode, pkg); trig_file_activate(usenode, pkg); fnamevb.used= fnameidlu; varbufaddstr(&fnamevb, usenode->name); varbufaddc(&fnamevb,0); if (!stat(namenode->name,&stab) && S_ISDIR(stab.st_mode)) { debug(dbg_eachfiledetail, "process_archive: %s is a directory", namenode->name); if (isdirectoryinuse(namenode,pkg)) continue; } if (lstat(fnamevb.buf, &oldfs)) { if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR)) warning(_("could not stat old file '%.250s' so not deleting it: %s"), fnamevb.buf, strerror(errno)); continue; } if (S_ISDIR(oldfs.st_mode)) { if (rmdir(fnamevb.buf)) { warning(_("unable to delete old directory '%.250s': %s"), namenode->name, strerror(errno)); } else if ((namenode->flags & fnnf_old_conff)) { warning(_("old conffile '%.250s' was an empty directory " "(and has now been deleted)"), namenode->name); } } else { /* Ok, it's an old file, but is it really not in the new package? * It might be known by a different name because of symlinks. * * We need to check to make sure, so we stat the file, then compare * it to the new list. If we find a dev/inode match, we assume they * are the same file, and leave it alone. NOTE: we don't check in * other packages for sanity reasons (we don't want to stat _all_ * the files on the system). * * We run down the list of _new_ files in this package. This keeps * the process a little leaner. We are only worried about new ones * since ones that stayed the same don't really apply here. */ struct fileinlist *sameas = NULL; static struct stat empty_stat; struct varbuf cfilename = VARBUF_INIT; /* If we can't stat the old or new file, or it's a directory, * we leave it up to the normal code */ debug(dbg_eachfile, "process_archive: checking %s for same files on " "upgrade/downgrade", fnamevb.buf); for (cfile= newfileslist; cfile; cfile= cfile->next) { if (!cfile->namenode->filestat) { struct stat tmp_stat; varbufreset(&cfilename); varbufaddstr(&cfilename, instdir); varbufaddc(&cfilename, '/'); varbufaddstr(&cfilename, cfile->namenode->name); varbufaddc(&cfilename, '\0'); if (lstat(cfilename.buf, &tmp_stat) == 0) { cfile->namenode->filestat = nfmalloc(sizeof(struct stat)); memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat)); } else { if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR)) ohshite(_("unable to stat other new file `%.250s'"), cfile->namenode->name); cfile->namenode->filestat = &empty_stat; continue; } } if (cfile->namenode->filestat == &empty_stat) continue; if (oldfs.st_dev == cfile->namenode->filestat->st_dev && oldfs.st_ino == cfile->namenode->filestat->st_ino) { if (sameas) warning(_("old file '%.250s' is the same as several new files! " "(both '%.250s' and '%.250s')"), fnamevb.buf, sameas->namenode->name, cfile->namenode->name); sameas= cfile; debug(dbg_eachfile, "process_archive: not removing %s," " since it matches %s", fnamevb.buf, cfile->namenode->name); } } varbuffree(&cfilename); if ((namenode->flags & fnnf_old_conff)) { if (sameas) { if (sameas->namenode->flags & fnnf_new_conff) { if (!strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG)) { sameas->namenode->oldhash= namenode->oldhash; debug(dbg_eachfile, "process_archive: old conff %s" " is same as new conff %s, copying hash", namenode->name, sameas->namenode->name); } else { debug(dbg_eachfile, "process_archive: old conff %s" " is same as new conff %s but latter already has hash", namenode->name, sameas->namenode->name); } } } else { debug(dbg_eachfile, "process_archive: old conff %s" " is disappearing", namenode->name); namenode->flags |= fnnf_obs_conff; newconff_append(&newconffileslastp, namenode); addfiletolist(&tc, namenode); } continue; } if (sameas) continue; if (secure_unlink_statted(fnamevb.buf, &oldfs)) { warning(_("unable to securely remove old file '%.250s': %s"), namenode->name, strerror(errno)); } } /* !S_ISDIR */ } /* OK, now we can write the updated files-in-this package list, * since we've done away (hopefully) with all the old junk. */ write_filelist_except(pkg,newfileslist,0); /* Trigger interests may have changed. * Firstly we go through the old list of interests deleting them. * Then we go through the new list adding them. */ strcpy(cidirrest, TRIGGERSCIFILE); trig_parse_ci(pkgadminfile(pkg, TRIGGERSCIFILE), trig_cicb_interest_delete, NULL, pkg); trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg); trig_file_interests_save(); /* We also install the new maintainer scripts, and any other * cruft that may have come along with the package. First * we go through the existing scripts replacing or removing * them as appropriate; then we go through the new scripts * (any that are left) and install them. */ debug(dbg_general, "process_archive updating info directory"); varbufreset(&infofnvb); varbufaddstr(&infofnvb,admindir); varbufaddstr(&infofnvb, "/" INFODIR); infodirlen= infofnvb.used; varbufaddc(&infofnvb,0); dsd= opendir(infofnvb.buf); if (!dsd) ohshite(_("cannot read info directory")); push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd); while ((de = readdir(dsd)) != NULL) { debug(dbg_veryverbose, "process_archive info file `%s'", de->d_name); if (de->d_name[0] == '.') continue; /* ignore dotfiles, including `.' and `..' */ p= strrchr(de->d_name,'.'); if (!p) continue; /* ignore anything odd */ if (strlen(pkg->name) != (size_t)(p-de->d_name) || strncmp(de->d_name,pkg->name,p-de->d_name)) continue; debug(dbg_stupidlyverbose, "process_archive info this pkg"); /* Right do we have one ? */ p++; /* skip past the full stop */ if (!strcmp(p,LISTFILE)) continue; /* We do the list separately */ if (strlen(p) > MAXCONTROLFILENAME) ohshit(_("old version of package has overly-long info file name starting `%.250s'"), de->d_name); infofnvb.used= infodirlen; varbufaddstr(&infofnvb,de->d_name); varbufaddc(&infofnvb,0); strcpy(cidirrest,p); if (!rename(cidir,infofnvb.buf)) { debug(dbg_scripts, "process_archive info installed %s as %s", cidir, infofnvb.buf); } else if (errno == ENOENT) { /* Right, no new version. */ if (unlink(infofnvb.buf)) ohshite(_("unable to remove obsolete info file `%.250s'"),infofnvb.buf); debug(dbg_scripts, "process_archive info unlinked %s",infofnvb.buf); } else { ohshite(_("unable to install (supposed) new info file `%.250s'"),cidir); } } pop_cleanup(ehflag_normaltidy); /* closedir */ *cidirrest = '\0'; /* the directory itself */ dsd= opendir(cidir); if (!dsd) ohshite(_("unable to open temp control directory")); push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd); while ((de= readdir(dsd))) { if (strchr(de->d_name,'.')) { debug(dbg_scripts,"process_archive tmp.ci script/file `%s' contains dot", de->d_name); continue; } if (strlen(de->d_name) > MAXCONTROLFILENAME) ohshit(_("package contains overly-long control info file name (starting `%.50s')"), de->d_name); strcpy(cidirrest,de->d_name); /* First we check it's not a directory. */ if (!rmdir(cidir)) ohshit(_("package control info contained directory `%.250s'"),cidir); else if (errno != ENOTDIR) ohshite(_("package control info rmdir of `%.250s' didn't say not a dir"),de->d_name); if (!strcmp(de->d_name,CONTROLFILE)) { debug(dbg_scripts,"process_archive tmp.ci script/file `%s' is control",cidir); continue; /* ignore the control file */ } if (!strcmp(de->d_name,LISTFILE)) { warning(_("package %s contained list as info file"), pkg->name); continue; } /* Right, install it */ newinfofilename= pkgadminfile(pkg,de->d_name); if (rename(cidir,newinfofilename)) ohshite(_("unable to install new info file `%.250s' as `%.250s'"), cidir,newinfofilename); debug(dbg_scripts,"process_archive tmp.ci script/file `%s' installed as `%s'", cidir, newinfofilename); } pop_cleanup(ehflag_normaltidy); /* closedir */ /* Update the status database. * This involves copying each field across from the `available' * to the `installed' half of the pkg structure. * For some of the fields we have to do a complicated construction * operation; for others we can just copy the value. * We tackle the fields in the order they appear, so that * we don't miss any out :-). * At least we don't have to copy any strings that are referred * to, because these are never modified and never freed. */ /* The dependencies are the most difficult. We have to build * a whole new forward dependency tree. At least the reverse * links (linking our deppossi's into the reverse chains) * can be done by copy_dependency_links. */ newdeplist = NULL; newdeplistlastp = &newdeplist; for (dep= pkg->available.depends; dep; dep= dep->next) { newdep= nfmalloc(sizeof(struct dependency)); newdep->up= pkg; newdep->next = NULL; newdep->list = NULL; newpossilastp = &newdep->list; for (possi= dep->list; possi; possi= possi->next) { newpossi= nfmalloc(sizeof(struct deppossi)); newpossi->up= newdep; newpossi->ed= possi->ed; newpossi->next = NULL; newpossi->nextrev = newpossi->backrev = NULL; newpossi->verrel= possi->verrel; if (possi->verrel != dvr_none) newpossi->version= possi->version; else blankversion(&newpossi->version); newpossi->cyclebreak= 0; *newpossilastp= newpossi; newpossilastp= &newpossi->next; } newdep->type= dep->type; *newdeplistlastp= newdep; newdeplistlastp= &newdep->next; } /* Right, now we've replicated the forward tree, we * get copy_dependency_links to remove all the old dependency * structures from the reverse links and add the new dependency * structures in instead. It also copies the new dependency * structure pointer for this package into the right field. */ copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0); /* The `depended' pointer in the structure doesn't represent anything * that is actually specified by this package - it's there so we * can find out what other packages refer to this one. So, * we don't copy it. We go straight on to copy the text fields. */ pkg->installed.essential= pkg->available.essential; pkg->installed.description= pkg->available.description; pkg->installed.maintainer= pkg->available.maintainer; pkg->installed.source= pkg->available.source; pkg->installed.architecture= pkg->available.architecture; pkg->installed.installedsize= pkg->available.installedsize; pkg->installed.version= pkg->available.version; pkg->installed.origin = pkg->available.origin; pkg->installed.bugs = pkg->available.bugs; /* We have to generate our own conffiles structure. */ pkg->installed.conffiles = NULL; iconffileslastp = &pkg->installed.conffiles; for (cfile= newconffiles; cfile; cfile= cfile->next) { newiconff= nfmalloc(sizeof(struct conffile)); newiconff->next = NULL; newiconff->name= nfstrsave(cfile->namenode->name); newiconff->hash= nfstrsave(cfile->namenode->oldhash); newiconff->obsolete= !!(cfile->namenode->flags & fnnf_obs_conff); *iconffileslastp= newiconff; iconffileslastp= &newiconff->next; } /* We can just copy the arbitrary fields list, because it is * never even rearranged. Phew! */ pkg->installed.arbs= pkg->available.arbs; /* Check for disappearing packages: * We go through all the packages on the system looking for ones * whose files are entirely part of the one we've just unpacked * (and which actually *have* some files!). * * Any that we find are removed - we run the postrm with `disappear' * as an argument, and remove their info/... files and status info. * Conffiles are ignored (the new package had better do something * with them !). */ it= iterpkgstart(); while ((otherpkg = iterpkgnext(it)) != NULL) { ensure_package_clientdata(otherpkg); if (otherpkg == pkg || otherpkg->status == stat_notinstalled || otherpkg->status == stat_configfiles || otherpkg->clientdata->istobe == itb_remove || !otherpkg->clientdata->files) continue; debug(dbg_veryverbose, "process_archive checking disappearance %s",otherpkg->name); assert(otherpkg->clientdata->istobe == itb_normal || otherpkg->clientdata->istobe == itb_deconfigure); for (cfile= otherpkg->clientdata->files; cfile && !strcmp(cfile->namenode->name,"/."); cfile= cfile->next); if (!cfile) { debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear"); continue; } for (cfile= otherpkg->clientdata->files; cfile && !filesavespackage(cfile,otherpkg,pkg); cfile= cfile->next); if (cfile) continue; /* So dependency things will give right answers ... */ otherpkg->clientdata->istobe= itb_remove; debug(dbg_veryverbose, "process_archive disappear checking dependencies"); for (pdep= otherpkg->installed.depended; pdep; pdep= pdep->nextrev) { if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends && pdep->up->type != dep_recommends) continue; if (depisok(pdep->up, &depprobwhy, NULL, 0)) continue; varbufaddc(&depprobwhy,0); debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf); break; } if (!pdep) { /* If we haven't found a reason not to yet, let's look some more. */ for (providecheck= otherpkg->installed.depends; providecheck; providecheck= providecheck->next) { if (providecheck->type != dep_provides) continue; for (pdep= providecheck->list->ed->installed.depended; pdep; pdep= pdep->nextrev) { if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends && pdep->up->type != dep_recommends) continue; if (depisok(pdep->up, &depprobwhy, NULL, 0)) continue; varbufaddc(&depprobwhy,0); debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s", providecheck->list->ed->name, depprobwhy.buf); goto break_from_both_loops_at_once; } } break_from_both_loops_at_once:; } otherpkg->clientdata->istobe= itb_normal; if (pdep) continue; printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"), otherpkg->name); debug(dbg_general, "process_archive disappearing %s",otherpkg->name); /* No, we're disappearing it. This is the wrong time to go and * run maintainer scripts and things, as we can't back out. But * what can we do ? It has to be run this late. */ trig_activate_packageprocessing(otherpkg); maintainer_script_installed(otherpkg, POSTRMFILE, "post-removal script (for disappearance)", "disappear", pkg->name, versiondescribe(&pkg->available.version, vdew_nonambig), NULL); /* OK, now we delete all the stuff in the `info' directory .. */ varbufreset(&fnvb); varbufaddstr(&fnvb,admindir); varbufaddstr(&fnvb,"/" INFODIR); infodirbaseused= fnvb.used; varbufaddc(&fnvb,0); dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory")); push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd); debug(dbg_general, "process_archive disappear cleaning info directory"); while ((de = readdir(dsd)) != NULL) { debug(dbg_veryverbose, "process_archive info file `%s'", de->d_name); if (de->d_name[0] == '.') continue; p= strrchr(de->d_name,'.'); if (!p) continue; if (strlen(otherpkg->name) != (size_t)(p-de->d_name) || strncmp(de->d_name,otherpkg->name,p-de->d_name)) continue; debug(dbg_stupidlyverbose, "process_archive info this pkg"); fnvb.used= infodirbaseused; varbufaddstr(&fnvb,de->d_name); varbufaddc(&fnvb,0); if (unlink(fnvb.buf)) ohshite(_("unable to delete disappearing control info file `%.250s'"),fnvb.buf); debug(dbg_scripts, "process_archive info unlinked %s",fnvb.buf); } pop_cleanup(ehflag_normaltidy); /* closedir */ otherpkg->status= stat_notinstalled; otherpkg->want = want_unknown; otherpkg->eflag = eflag_ok; blankversion(&otherpkg->configversion); blankpackageperfile(&otherpkg->installed); otherpkg->clientdata->fileslistvalid= 0; modstatdb_note(otherpkg); } /* while (otherpkg= ... */ iterpkgend(it); /* Delete files from any other packages' lists. * We have to do this before we claim this package is in any * sane kind of state, as otherwise we might delete by mistake * a file that we overwrote, when we remove the package which * had the version we overwrote. To prevent this we make * sure that we don't claim this package is OK until we * have claimed `ownership' of all its files. */ for (cfile= newfileslist; cfile; cfile= cfile->next) { if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue; if (cfile->namenode->divert && cfile->namenode->divert->useinstead) { divpkg= cfile->namenode->divert->pkg; if (divpkg == pkg) { debug(dbg_eachfile, "process_archive not overwriting any `%s' (overriding, `%s')", cfile->namenode->name, cfile->namenode->divert->useinstead->name); continue; } else { debug(dbg_eachfile, "process_archive looking for overwriting `%s' (overridden by %s)", cfile->namenode->name, divpkg ? divpkg->name : "<local>"); } } else { divpkg = NULL; debug(dbg_eachfile, "process_archive looking for overwriting `%s'", cfile->namenode->name); } for (packageslump= cfile->namenode->packages; packageslump; packageslump= packageslump->more) { for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) { otherpkg= packageslump->pkgs[i]; debug(dbg_eachfiledetail, "process_archive ... found in %s\n",otherpkg->name); /* If !fileslistvalid then it's one of the disappeared packages above * and we don't bother with it here, clearly. */ if (otherpkg == pkg || !otherpkg->clientdata->fileslistvalid) continue; if (otherpkg == divpkg) { debug(dbg_eachfiledetail, "process_archive ... diverted, skipping\n"); continue; } /* Found one. We delete remove the list entry for this file, * (and any others in the same package) and then mark the package * as requiring a reread. */ write_filelist_except(otherpkg, otherpkg->clientdata->files, 1); ensure_package_clientdata(otherpkg); debug(dbg_veryverbose, "process_archive overwrote from %s",otherpkg->name); } } } /* Right, the package we've unpacked is now in a reasonable state. * The only thing that we have left to do with it is remove * backup files, and we can leave the user to fix that if and when * it happens (we leave the reinstall required flag, of course). */ pkg->status= stat_unpacked; modstatdb_note(pkg); /* Now we delete all the backup files that we made when * extracting the archive - except for files listed as conffiles * in the new package. * This time we count it as an error if something goes wrong. * * Note that we don't ever delete things that were in the old * package as a conffile and don't appear at all in the new. * They stay recorded as obsolete conffiles and will eventually * (if not taken over by another package) be forgotten. */ for (cfile= newfileslist; cfile; cfile= cfile->next) { if (cfile->namenode->flags & fnnf_new_conff) continue; fnametmpvb.used= fnameidlu; varbufaddstr(&fnametmpvb,namenodetouse(cfile->namenode,pkg)->name); varbufaddstr(&fnametmpvb,DPKGTEMPEXT); varbufaddc(&fnametmpvb,0); ensure_pathname_nonexisting(fnametmpvb.buf); } /* OK, we're now fully done with the main package. * This is quite a nice state, so we don't unwind past here. */ pkg->eflag = eflag_ok; modstatdb_note(pkg); push_checkpoint(~ehflag_bombout, ehflag_normaltidy); /* Only the removal of the conflictor left to do. * The files list for the conflictor is still a little inconsistent in-core, * as we have not yet updated the filename->packages mappings; however, * the package->filenames mapping is */ for (i = 0 ; i < cflict_index ; i++) { /* We need to have the most up-to-date info about which files are what ... */ ensure_allinstfiles_available(); removal_bulk(conflictor[i]); } if (cipaction->arg == act_install) add_to_queue(pkg); }
int tarobject(void *ctx, struct tar_entry *ti) { static struct varbuf conffderefn, hardlinkfn, symlinkfn; static int fd; const char *usename; struct filenamenode *usenode; struct filenamenode *linknode; struct conffile *conff; struct tarcontext *tc = ctx; bool existingdirectory, keepexisting; int statr; ssize_t r; struct stat stab, stabtmp; char databuf[TARBLKSZ]; struct file_stat *st; struct fileinlist *nifd, **oldnifd; struct pkginfo *divpkg, *otherpkg; ensureobstackinit(); /* Append to list of files. * The trailing ‘/’ put on the end of names in tarfiles has already * been stripped by tar_extractor(). */ oldnifd= tc->newfilesp; nifd= addfiletolist(tc, findnamenode(ti->name, 0)); nifd->namenode->flags |= fnnf_new_inarchive; debug(dbg_eachfile, "tarobject ti->name='%s' mode=%lo owner=%u.%u type=%d(%c)" " ti->linkname='%s' namenode='%s' flags=%o instead='%s'", ti->name, (long)ti->stat.mode, (unsigned)ti->stat.uid, (unsigned)ti->stat.gid, ti->type, ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?', ti->linkname, nifd->namenode->name, nifd->namenode->flags, nifd->namenode->divert && nifd->namenode->divert->useinstead ? nifd->namenode->divert->useinstead->name : "<none>"); if (nifd->namenode->divert && nifd->namenode->divert->camefrom) { divpkg= nifd->namenode->divert->pkg; if (divpkg) { forcibleerr(fc_overwritediverted, _("trying to overwrite `%.250s', which is the " "diverted version of `%.250s' (package: %.100s)"), nifd->namenode->name, nifd->namenode->divert->camefrom->name, divpkg->name); } else { forcibleerr(fc_overwritediverted, _("trying to overwrite `%.250s', which is the " "diverted version of `%.250s'"), nifd->namenode->name, nifd->namenode->divert->camefrom->name); } } if (nifd->namenode->statoverride) st = nifd->namenode->statoverride; else st = &ti->stat; usenode = namenodetouse(nifd->namenode, tc->pkg); usename = usenode->name + 1; /* Skip the leading '/'. */ trig_file_activate(usenode, tc->pkg); if (nifd->namenode->flags & fnnf_new_conff) { /* If it's a conffile we have to extract it next to the installed * version (i.e. we do the usual link-following). */ if (conffderef(tc->pkg, &conffderefn, usename)) usename= conffderefn.buf; debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename); } setupfnamevbs(usename); statr= lstat(fnamevb.buf,&stab); if (statr) { /* The lstat failed. */ if (errno != ENOENT && errno != ENOTDIR) ohshite(_("unable to stat `%.255s' (which I was about to install)"), ti->name); /* OK, so it doesn't exist. * However, it's possible that we were in the middle of some other * backup/restore operation and were rudely interrupted. * So, we see if we have .dpkg-tmp, and if so we restore it. */ if (rename(fnametmpvb.buf,fnamevb.buf)) { if (errno != ENOENT && errno != ENOTDIR) ohshite(_("unable to clean up mess surrounding `%.255s' before " "installing another version"), ti->name); debug(dbg_eachfiledetail,"tarobject nonexistent"); } else { debug(dbg_eachfiledetail,"tarobject restored tmp to main"); statr= lstat(fnamevb.buf,&stab); if (statr) ohshite(_("unable to stat restored `%.255s' before installing" " another version"), ti->name); } } else { debug(dbg_eachfiledetail,"tarobject already exists"); } /* Check to see if it's a directory or link to one and we don't need to * do anything. This has to be done now so that we don't die due to * a file overwriting conflict. */ existingdirectory = false; switch (ti->type) { case tar_filetype_symlink: /* If it's already an existing directory, do nothing. */ if (!statr && S_ISDIR(stab.st_mode)) { debug(dbg_eachfiledetail, "tarobject symlink exists as directory"); existingdirectory = true; } else if (!statr && S_ISLNK(stab.st_mode)) { if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn)) existingdirectory = true; } break; case tar_filetype_dir: /* If it's already an existing directory, do nothing. */ if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) { debug(dbg_eachfiledetail, "tarobject directory exists"); existingdirectory = true; } break; case tar_filetype_file: case tar_filetype_chardev: case tar_filetype_blockdev: case tar_filetype_fifo: case tar_filetype_hardlink: break; default: ohshit(_("archive contained object `%.255s' of unknown type 0x%x"), ti->name, ti->type); } keepexisting = false; if (!existingdirectory) { struct filepackages_iterator *iter; iter = filepackages_iter_new(nifd->namenode); while ((otherpkg = filepackages_iter_next(iter))) { if (otherpkg == tc->pkg) continue; debug(dbg_eachfile, "tarobject ... found in %s", otherpkg->name); if (nifd->namenode->divert && nifd->namenode->divert->useinstead) { /* Right, so we may be diverting this file. This makes the conflict * OK iff one of us is the diverting package (we don't need to * check for both being the diverting package, obviously). */ divpkg = nifd->namenode->divert->pkg; debug(dbg_eachfile, "tarobject ... diverted, divpkg=%s", divpkg ? divpkg->name : "<none>"); if (otherpkg == divpkg || tc->pkg == divpkg) continue; } /* Nope? Hmm, file conflict, perhaps. Check Replaces. */ switch (otherpkg->clientdata->replacingfilesandsaid) { case 2: keepexisting = true; case 1: continue; } /* Is the package with the conflicting file in the “config files only” * state? If so it must be a config file and we can silenty take it * over. */ if (otherpkg->status == stat_configfiles) continue; /* Perhaps we're removing a conflicting package? */ if (otherpkg->clientdata->istobe == itb_remove) continue; /* Is the file an obsolete conffile in the other package * and a conffile in the new package? */ if ((nifd->namenode->flags & fnnf_new_conff) && !statr && S_ISREG(stab.st_mode)) { for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) { if (!conff->obsolete) continue; if (stat(conff->name, &stabtmp)) if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP) continue; if (stabtmp.st_dev == stab.st_dev && stabtmp.st_ino == stab.st_ino) break; } if (conff) { debug(dbg_eachfiledetail, "tarobject other's obsolete conffile"); /* process_archive() will have copied its hash already. */ continue; } } if (does_replace(tc->pkg, &tc->pkg->available, otherpkg, &otherpkg->installed)) { printf(_("Replacing files in old package %s ...\n"),otherpkg->name); otherpkg->clientdata->replacingfilesandsaid = 1; } else if (does_replace(otherpkg, &otherpkg->installed, tc->pkg, &tc->pkg->available)) { printf(_("Replaced by files in installed package %s ...\n"), otherpkg->name); otherpkg->clientdata->replacingfilesandsaid = 2; nifd->namenode->flags &= ~fnnf_new_inarchive; keepexisting = true; } else { if (!statr && S_ISDIR(stab.st_mode)) { forcibleerr(fc_overwritedir, _("trying to overwrite directory '%.250s' " "in package %.250s %.250s with nondirectory"), nifd->namenode->name, otherpkg->name, versiondescribe(&otherpkg->installed.version, vdew_nonambig)); } else { /* At this point we are replacing something without a Replaces. * If the new object is a directory and the previous object does * not exist assume it's also a directory and don't complain. */ if (!(statr && ti->type == tar_filetype_dir)) forcibleerr(fc_overwrite, _("trying to overwrite '%.250s', " "which is also in package %.250s %.250s"), nifd->namenode->name, otherpkg->name, versiondescribe(&otherpkg->installed.version, vdew_nonambig)); } } } filepackages_iter_free(iter); } if (keepexisting) { remove_file_from_list(tc, ti, oldnifd, nifd); tarfile_skip_one_forward(tc, ti); return 0; } if (filter_should_skip(ti)) { nifd->namenode->flags &= ~fnnf_new_inarchive; nifd->namenode->flags |= fnnf_filtered; tarfile_skip_one_forward(tc, ti); return 0; } if (existingdirectory) return 0; /* Now, at this stage we want to make sure neither of .dpkg-new and * .dpkg-tmp are hanging around. */ ensure_pathname_nonexisting(fnamenewvb.buf); ensure_pathname_nonexisting(fnametmpvb.buf); /* Now we start to do things that we need to be able to undo * if something goes wrong. Watch out for the CLEANUP comments to * keep an eye on what's installed on the disk at each point. */ push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, (void *)nifd); /* * CLEANUP: Now we either have the old file on the disk, or not, in * its original filename. */ /* Extract whatever it is as .dpkg-new ... */ switch (ti->type) { case tar_filetype_file: /* We create the file with mode 0 to make sure nobody can do anything with * it until we apply the proper mode, which might be a statoverride. */ fd= open(fnamenewvb.buf, (O_CREAT|O_EXCL|O_WRONLY), 0); if (fd < 0) ohshite(_("unable to create `%.255s' (while processing `%.255s')"), fnamenewvb.buf, ti->name); push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd); debug(dbg_eachfiledetail, "tarobject file open size=%lu", (unsigned long)ti->size); { char fnamebuf[256]; fd_fd_copy(tc->backendpipe, fd, ti->size, _("backend dpkg-deb during `%.255s'"), path_quote_filename(fnamebuf, ti->name, 256)); } r = ti->size % TARBLKSZ; if (r > 0) if (safe_read(tc->backendpipe, databuf, TARBLKSZ - r) == -1) ohshite(_("error reading from dpkg-deb pipe")); fd_writeback_init(fd); if (nifd->namenode->statoverride) debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o", nifd->namenode->statoverride->uid, nifd->namenode->statoverride->gid, nifd->namenode->statoverride->mode); if (fchown(fd, st->uid, st->gid)) ohshite(_("error setting ownership of `%.255s'"), ti->name); if (fchmod(fd, st->mode & ~S_IFMT)) ohshite(_("error setting permissions of `%.255s'"), ti->name); /* Postpone the fsync, to try to avoid massive I/O degradation. */ if (!fc_unsafe_io) nifd->namenode->flags |= fnnf_deferred_fsync; pop_cleanup(ehflag_normaltidy); /* fd = open(fnamenewvb.buf) */ if (close(fd)) ohshite(_("error closing/writing `%.255s'"), ti->name); newtarobject_utime(fnamenewvb.buf, st); break; case tar_filetype_fifo: if (mkfifo(fnamenewvb.buf,0)) ohshite(_("error creating pipe `%.255s'"), ti->name); debug(dbg_eachfiledetail, "tarobject fifo"); newtarobject_allmodes(fnamenewvb.buf, st); break; case tar_filetype_chardev: if (mknod(fnamenewvb.buf, S_IFCHR, ti->dev)) ohshite(_("error creating device `%.255s'"), ti->name); debug(dbg_eachfiledetail, "tarobject chardev"); newtarobject_allmodes(fnamenewvb.buf, st); break; case tar_filetype_blockdev: if (mknod(fnamenewvb.buf, S_IFBLK, ti->dev)) ohshite(_("error creating device `%.255s'"), ti->name); debug(dbg_eachfiledetail, "tarobject blockdev"); newtarobject_allmodes(fnamenewvb.buf, st); break; case tar_filetype_hardlink: varbufreset(&hardlinkfn); varbufaddstr(&hardlinkfn,instdir); varbufaddc(&hardlinkfn,'/'); varbufaddstr(&hardlinkfn, ti->linkname); linknode = findnamenode(ti->linkname, 0); if (linknode->flags & fnnf_deferred_rename) varbufaddstr(&hardlinkfn, DPKGNEWEXT); varbufaddc(&hardlinkfn, '\0'); if (link(hardlinkfn.buf,fnamenewvb.buf)) ohshite(_("error creating hard link `%.255s'"), ti->name); debug(dbg_eachfiledetail, "tarobject hardlink"); newtarobject_allmodes(fnamenewvb.buf, st); break; case tar_filetype_symlink: /* We've already cheched for an existing directory. */ if (symlink(ti->linkname, fnamenewvb.buf)) ohshite(_("error creating symbolic link `%.255s'"), ti->name); debug(dbg_eachfiledetail, "tarobject symlink creating"); if (lchown(fnamenewvb.buf, st->uid, st->gid)) ohshite(_("error setting ownership of symlink `%.255s'"), ti->name); break; case tar_filetype_dir: /* We've already checked for an existing directory. */ if (mkdir(fnamenewvb.buf,0)) ohshite(_("error creating directory `%.255s'"), ti->name); debug(dbg_eachfiledetail, "tarobject directory creating"); newtarobject_allmodes(fnamenewvb.buf, st); break; default: internerr("unknown tar type '%d', but already checked", ti->type); } set_selinux_path_context(fnamevb.buf, fnamenewvb.buf, st->mode); /* * CLEANUP: Now we have extracted the new object in .dpkg-new (or, * if the file already exists as a directory and we were trying to * extract a directory or symlink, we returned earlier, so we don't * need to worry about that here). * * The old file is still in the original filename, */ /* First, check to see if it's a conffile. If so we don't install * it now - we leave it in .dpkg-new for --configure to take care of. */ if (nifd->namenode->flags & fnnf_new_conff) { debug(dbg_conffdetail,"tarobject conffile extracted"); nifd->namenode->flags |= fnnf_elide_other_lists; return 0; } /* Now we move the old file out of the way, the backup file will * be deleted later. */ if (statr) { /* Don't try to back it up if it didn't exist. */ debug(dbg_eachfiledetail,"tarobject new - no backup"); } else { if (ti->type == tar_filetype_dir || S_ISDIR(stab.st_mode)) { /* One of the two is a directory - can't do atomic install. */ debug(dbg_eachfiledetail,"tarobject directory, nonatomic"); nifd->namenode->flags |= fnnf_no_atomic_overwrite; if (rename(fnamevb.buf,fnametmpvb.buf)) ohshite(_("unable to move aside `%.255s' to install new version"), ti->name); } else if (S_ISLNK(stab.st_mode)) { /* We can't make a symlink with two hardlinks, so we'll have to * copy it. (Pretend that making a copy of a symlink is the same * as linking to it.) */ varbufreset(&symlinkfn); varbuf_grow(&symlinkfn, stab.st_size + 1); r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size); if (r < 0) ohshite(_("unable to read link `%.255s'"), ti->name); assert(r == stab.st_size); varbuf_trunc(&symlinkfn, r); varbufaddc(&symlinkfn, '\0'); if (symlink(symlinkfn.buf,fnametmpvb.buf)) ohshite(_("unable to make backup symlink for `%.255s'"), ti->name); if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid)) ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name); set_selinux_path_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode); } else { debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup"); if (link(fnamevb.buf,fnametmpvb.buf)) ohshite(_("unable to make backup link of `%.255s' before installing new version"), ti->name); } } /* * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still * in .dpkg-new. */ if (ti->type == tar_filetype_file || ti->type == tar_filetype_symlink) { nifd->namenode->flags |= fnnf_deferred_rename; debug(dbg_eachfiledetail, "tarobject done and installation deferred"); } else { if (rename(fnamenewvb.buf, fnamevb.buf)) ohshite(_("unable to install new version of `%.255s'"), ti->name); /* * CLEANUP: Now the new file is in the destination file, and the * old file is in .dpkg-tmp to be cleaned up later. We now need * to take a different attitude to cleanup, because we need to * remove the new file. */ nifd->namenode->flags |= fnnf_placed_on_disk; nifd->namenode->flags |= fnnf_elide_other_lists; debug(dbg_eachfiledetail, "tarobject done and installed"); } return 0; }
static void pkg_infodb_update(struct pkginfo *pkg, char *cidir, char *cidirrest) { struct match_node *match_node; DIR *dsd; struct dirent *de; /* Deallocate the match list in case we aborted previously. */ while ((match_node = match_head)) { match_head = match_node->next; match_node_free(match_node); } pkg_infodb_foreach(pkg, &pkg->available, pkg_infodb_update_file); while ((match_node = match_head)) { strcpy(cidirrest, match_node->filetype); if (!rename(cidir, match_node->filename)) { debug(dbg_scripts, "process_archive info installed %s as %s", cidir, match_node->filename); } else if (errno == ENOENT) { /* Right, no new version. */ if (unlink(match_node->filename)) ohshite(_("unable to remove obsolete info file `%.250s'"), match_node->filename); debug(dbg_scripts, "process_archive info unlinked %s", match_node->filename); } else { ohshite(_("unable to install (supposed) new info file `%.250s'"), cidir); } match_head = match_node->next; match_node_free(match_node); } /* The control directory itself. */ cidirrest[0] = '\0'; dsd = opendir(cidir); if (!dsd) ohshite(_("unable to open temp control directory")); push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd); while ((de = readdir(dsd))) { const char *newinfofilename; if (strchr(de->d_name, '.')) { debug(dbg_scripts,"process_archive tmp.ci script/file `%s' contains dot", de->d_name); continue; } if (strlen(de->d_name) > MAXCONTROLFILENAME) ohshit(_("package contains overly-long control info file name (starting `%.50s')"), de->d_name); strcpy(cidirrest, de->d_name); /* First we check it's not a directory. */ if (rmdir(cidir) == 0) ohshit(_("package control info contained directory `%.250s'"), cidir); else if (errno != ENOTDIR) ohshite(_("package control info rmdir of `%.250s' didn't say not a dir"), de->d_name); /* Ignore the control file. */ if (strcmp(de->d_name, CONTROLFILE) == 0) { debug(dbg_scripts, "process_archive tmp.ci script/file `%s' is control", cidir); continue; } if (strcmp(de->d_name, LISTFILE) == 0) { warning(_("package %s contained list as info file"), pkgbin_name(pkg, &pkg->available, pnaw_nonambig)); continue; } /* Right, install it */ newinfofilename = pkgadminfile(pkg, &pkg->available, de->d_name); if (rename(cidir, newinfofilename)) ohshite(_("unable to install new info file `%.250s' as `%.250s'"), cidir, newinfofilename); debug(dbg_scripts, "process_archive tmp.ci script/file `%s' installed as `%s'", cidir, newinfofilename); } pop_cleanup(ehflag_normaltidy); /* closedir */ dir_sync_path(pkgadmindir()); }
void process_archive(const char *filename) { static const struct tar_operations tf = { .read = tarfileread, .extract_file = tarobject, .link = tarobject, .symlink = tarobject, .mkdir = tarobject, .mknod = tarobject, }; /* These need to be static so that we can pass their addresses to * push_cleanup as arguments to the cu_xxx routines; if an error occurs * we unwind the stack before processing the cleanup list, and these * variables had better still exist ... */ static int p1[2]; static char *cidir = NULL; static struct fileinlist *newconffiles, *newfileslist; static enum pkgstatus oldversionstatus; static struct varbuf depprobwhy; static struct tarcontext tc; int r, i; pid_t pid; struct pkgiterator *it; struct pkginfo *pkg, *otherpkg; char *cidirrest, *p; char conffilenamebuf[MAXCONFFILENAME]; char *psize; const char *pfilename; struct fileinlist *newconff, **newconffileslastp; struct fileinlist *cfile; struct reversefilelistiter rlistit; struct conffile *searchconff, **iconffileslastp, *newiconff; struct dependency *dsearch, *newdeplist, **newdeplistlastp; struct dependency *newdep, *dep, *providecheck; struct deppossi *psearch, **newpossilastp, *possi, *newpossi, *pdep; FILE *conff; struct filenamenode *namenode; struct stat stab, oldfs; struct pkg_deconf_list *deconpil, *deconpiltemp; struct pkginfo *fixbytrigaw; cleanup_pkg_failed= cleanup_conflictor_failed= 0; pfilename = summarize_filename(filename); if (stat(filename,&stab)) ohshite(_("cannot access archive")); /* We can't ‘tentatively-reassemble’ packages. */ if (!f_noact) { if (!deb_reassemble(&filename, &pfilename)) return; } /* Verify the package. */ if (!f_nodebsig) deb_verify(filename); /* Get the control information directory. */ cidir = get_control_dir(cidir); cidirrest = cidir + strlen(cidir); push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest); pid = subproc_fork(); if (pid == 0) { cidirrest[-1] = '\0'; execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL); ohshite(_("unable to execute %s (%s)"), _("package control information extraction"), BACKEND); } subproc_wait_check(pid, BACKEND " --control", 0); /* We want to guarantee the extracted files are on the disk, so that the * subsequent renames to the info database do not end up with old or zero * length files in case of a system crash. As neither dpkg-deb nor tar do * explicit fsync()s, we have to do them here. * XXX: This could be avoided by switching to an internal tar extractor. */ dir_sync_contents(cidir); strcpy(cidirrest,CONTROLFILE); parsedb(cidir, pdb_parse_binary | pdb_ignorefiles | parsedb_force_flags(), &pkg); if (!pkg->files) { pkg->files= nfmalloc(sizeof(struct filedetails)); pkg->files->next = NULL; pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL; } /* Always nfmalloc. Otherwise, we may overwrite some other field (like * md5sum). */ psize = nfmalloc(30); sprintf(psize, "%jd", (intmax_t)stab.st_size); pkg->files->size = psize; if (cipaction->arg_int == act_avail) { printf(_("Recorded info about %s from %s.\n"), pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename); pop_cleanup(ehflag_normaltidy); return; } if (pkg->available.arch->type != arch_all && pkg->available.arch->type != arch_native) forcibleerr(fc_architecture, _("package architecture (%s) does not match system (%s)"), pkg->available.arch->name, dpkg_arch_get(arch_native)->name); for (deconpil= deconfigure; deconpil; deconpil= deconpiltemp) { deconpiltemp= deconpil->next; free(deconpil); } deconfigure = NULL; clear_istobes(); if (!wanttoinstall(pkg)) { pop_cleanup(ehflag_normaltidy); return; } /* Check if anything is installed that we conflict with, or not installed * that we need. */ pkg->clientdata->istobe= itb_installnew; for (dsearch= pkg->available.depends; dsearch; dsearch= dsearch->next) { switch (dsearch->type) { case dep_conflicts: /* Look for things we conflict with. */ check_conflict(dsearch, pkg, pfilename); break; case dep_breaks: /* Look for things we break. */ check_breaks(dsearch, pkg, pfilename); break; case dep_provides: /* Look for things that conflict with what we provide. */ for (psearch = dsearch->list->ed->depended.installed; psearch; psearch = psearch->rev_next) { if (psearch->up->type != dep_conflicts) continue; check_conflict(psearch->up, pkg, pfilename); } break; case dep_suggests: case dep_recommends: case dep_depends: case dep_replaces: case dep_enhances: /* Ignore these here. */ break; case dep_predepends: if (!depisok(dsearch, &depprobwhy, NULL, &fixbytrigaw, true)) { if (fixbytrigaw) { while (fixbytrigaw->trigaw.head) trigproc(fixbytrigaw->trigaw.head->pend); } else { varbuf_end_str(&depprobwhy); fprintf(stderr, _("dpkg: regarding %s containing %s, pre-dependency problem:\n%s"), pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig), depprobwhy.buf); if (!force_depends(dsearch->list)) ohshit(_("pre-dependency problem - not installing %.250s"), pkgbin_name(pkg, &pkg->available, pnaw_nonambig)); warning(_("ignoring pre-dependency problem!")); } } } } /* Look for things that conflict with us. */ for (psearch = pkg->set->depended.installed; psearch; psearch = psearch->rev_next) { if (psearch->up->type != dep_conflicts) continue; check_conflict(psearch->up, pkg, pfilename); } ensure_allinstfiles_available(); filesdbinit(); trig_file_interests_ensure(); if (pkg->status != stat_notinstalled && pkg->status != stat_configfiles) { printf(_("Preparing to replace %s %s (using %s) ...\n"), pkg_name(pkg, pnaw_nonambig), versiondescribe(&pkg->installed.version,vdew_nonambig), pfilename); log_action("upgrade", pkg, &pkg->installed); } else { printf(_("Unpacking %s (from %s) ...\n"), pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename); log_action("install", pkg, &pkg->available); } if (f_noact) { pop_cleanup(ehflag_normaltidy); return; } /* * OK, we're going ahead. */ trig_activate_packageprocessing(pkg); strcpy(cidirrest, TRIGGERSCIFILE); trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg, &pkg->available); /* Read the conffiles, and copy the hashes across. */ newconffiles = NULL; newconffileslastp = &newconffiles; push_cleanup(cu_fileslist, ~0, NULL, 0, 0); strcpy(cidirrest,CONFFILESFILE); conff= fopen(cidir,"r"); if (conff) { push_cleanup(cu_closestream, ehflag_bombout, NULL, 0, 1, (void *)conff); while (fgets(conffilenamebuf,MAXCONFFILENAME-2,conff)) { struct filepackages_iterator *iter; p= conffilenamebuf + strlen(conffilenamebuf); assert(p != conffilenamebuf); if (p[-1] != '\n') ohshit(_("name of conffile (starting `%.250s') is too long (>%d characters)"), conffilenamebuf, MAXCONFFILENAME); while (p > conffilenamebuf && isspace(p[-1])) --p; if (p == conffilenamebuf) continue; *p = '\0'; namenode= findnamenode(conffilenamebuf, 0); namenode->oldhash= NEWCONFFILEFLAG; newconff= newconff_append(&newconffileslastp, namenode); /* Let's see if any packages have this file. If they do we * check to see if they listed it as a conffile, and if they did * we copy the hash across. Since (for plain file conffiles, * which is the only kind we are supposed to have) there will * only be one package which ‘has’ the file, this will usually * mean we only look in the package which we're installing now. * The ‘conffiles’ data in the status file is ignored when a * package isn't also listed in the file ownership database as * having that file. If several packages are listed as owning * the file we pick one at random. */ searchconff = NULL; iter = filepackages_iter_new(newconff->namenode); while ((otherpkg = filepackages_iter_next(iter))) { debug(dbg_conffdetail, "process_archive conffile `%s' in package %s - conff ?", newconff->namenode->name, pkg_name(otherpkg, pnaw_nonambig)); for (searchconff = otherpkg->installed.conffiles; searchconff && strcmp(newconff->namenode->name, searchconff->name); searchconff = searchconff->next) debug(dbg_conffdetail, "process_archive conffile `%s' in package %s - conff ? not `%s'", newconff->namenode->name, pkg_name(otherpkg, pnaw_nonambig), searchconff->name); if (searchconff) { debug(dbg_conff, "process_archive conffile `%s' package=%s %s hash=%s", newconff->namenode->name, pkg_name(otherpkg, pnaw_nonambig), otherpkg == pkg ? "same" : "different!", searchconff->hash); if (otherpkg == pkg) break; } } filepackages_iter_free(iter); if (searchconff) { newconff->namenode->oldhash= searchconff->hash; /* We don't copy ‘obsolete’; it's not obsolete in the new package. */ } else { debug(dbg_conff,"process_archive conffile `%s' no package, no hash", newconff->namenode->name); } newconff->namenode->flags |= fnnf_new_conff; } if (ferror(conff)) ohshite(_("read error in %.250s"),cidir); pop_cleanup(ehflag_normaltidy); /* conff = fopen() */ if (fclose(conff)) ohshite(_("error closing %.250s"),cidir); } else { if (errno != ENOENT) ohshite(_("error trying to open %.250s"),cidir); } /* All the old conffiles are marked with a flag, so that we don't delete * them if they seem to disappear completely. */ oldconffsetflags(pkg->installed.conffiles); for (i = 0 ; i < cflict_index ; i++) { oldconffsetflags(conflictor[i]->installed.conffiles); } oldversionstatus= pkg->status; assert(oldversionstatus <= stat_installed); debug(dbg_general,"process_archive oldversionstatus=%s", statusstrings[oldversionstatus]); if (oldversionstatus == stat_halfconfigured || oldversionstatus == stat_triggersawaited || oldversionstatus == stat_triggerspending || oldversionstatus == stat_installed) { pkg_set_eflags(pkg, eflag_reinstreq); pkg_set_status(pkg, stat_halfconfigured); modstatdb_note(pkg); push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg); if (versioncompare(&pkg->available.version, &pkg->installed.version) >= 0) /* Upgrade or reinstall */ maintainer_script_alternative(pkg, PRERMFILE, "pre-removal", cidir, cidirrest, "upgrade", "failed-upgrade"); else /* Downgrade => no fallback */ maintainer_script_installed(pkg, PRERMFILE, "pre-removal", "upgrade", versiondescribe(&pkg->available.version, vdew_nonambig), NULL); pkg_set_status(pkg, stat_unpacked); oldversionstatus= stat_unpacked; modstatdb_note(pkg); } for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) { struct pkginfo *removing = deconpil->pkg_removal; if (removing) printf(_("De-configuring %s, to allow removal of %s ...\n"), pkg_name(deconpil->pkg, pnaw_nonambig), pkg_name(removing, pnaw_nonambig)); else printf(_("De-configuring %s ...\n"), pkg_name(deconpil->pkg, pnaw_nonambig)); trig_activate_packageprocessing(deconpil->pkg); pkg_set_status(deconpil->pkg, stat_halfconfigured); modstatdb_note(deconpil->pkg); /* This means that we *either* go and run postinst abort-deconfigure, * *or* queue the package for later configure processing, depending * on which error cleanup route gets taken. */ push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy, ok_prermdeconfigure, ehflag_normaltidy, 3, (void*)deconpil->pkg, (void*)removing, (void*)pkg); if (removing) { maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal", "deconfigure", "in-favour", pkg->set->name, versiondescribe(&pkg->available.version, vdew_nonambig), "removing", removing->set->name, versiondescribe(&removing->installed.version, vdew_nonambig), NULL); } else { maintainer_script_installed(deconpil->pkg, PRERMFILE, "pre-removal", "deconfigure", "in-favour", pkg->set->name, versiondescribe(&pkg->available.version, vdew_nonambig), NULL); } } for (i = 0 ; i < cflict_index; i++) { if (!(conflictor[i]->status == stat_halfconfigured || conflictor[i]->status == stat_triggersawaited || conflictor[i]->status == stat_triggerspending || conflictor[i]->status == stat_installed)) continue; trig_activate_packageprocessing(conflictor[i]); pkg_set_status(conflictor[i], stat_halfconfigured); modstatdb_note(conflictor[i]); push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0, 2,(void*)conflictor[i],(void*)pkg); maintainer_script_installed(conflictor[i], PRERMFILE, "pre-removal", "remove", "in-favour", pkg->set->name, versiondescribe(&pkg->available.version, vdew_nonambig), NULL); pkg_set_status(conflictor[i], stat_halfinstalled); modstatdb_note(conflictor[i]); } pkg_set_eflags(pkg, eflag_reinstreq); if (pkg->status == stat_notinstalled) pkg->installed.version= pkg->available.version; pkg_set_status(pkg, stat_halfinstalled); modstatdb_note(pkg); if (oldversionstatus == stat_notinstalled) { push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0, 3,(void*)pkg,(void*)cidir,(void*)cidirrest); maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest, "install", NULL); } else if (oldversionstatus == stat_configfiles) { push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0, 3,(void*)pkg,(void*)cidir,(void*)cidirrest); maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest, "install", versiondescribe(&pkg->installed.version, vdew_nonambig), NULL); } else { push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0, 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus); maintainer_script_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest, "upgrade", versiondescribe(&pkg->installed.version, vdew_nonambig), NULL); printf(_("Unpacking replacement %.250s ...\n"), pkgbin_name(pkg, &pkg->available, pnaw_nonambig)); } /* * Now we unpack the archive, backing things up as we go. * For each file, we check to see if it already exists. * There are several possibilities: * * + We are trying to install a non-directory ... * - It doesn't exist. In this case we simply extract it. * - It is a plain file, device, symlink, &c. We do an ‘atomic * overwrite’ using link() and rename(), but leave a backup copy. * Later, when we delete the backup, we remove it from any other * packages' lists. * - It is a directory. In this case it depends on whether we're * trying to install a symlink or something else. * = If we're not trying to install a symlink we move the directory * aside and extract the node. Later, when we recursively remove * the backed-up directory, we remove it from any other packages' * lists. * = If we are trying to install a symlink we do nothing - ie, * dpkg will never replace a directory tree with a symlink. This * is to avoid embarrassing effects such as replacing a directory * tree with a link to a link to the original directory tree. * + We are trying to install a directory ... * - It doesn't exist. We create it with the appropriate modes. * - It exists as a directory or a symlink to one. We do nothing. * - It is a plain file or a symlink (other than to a directory). * We move it aside and create the directory. Later, when we * delete the backup, we remove it from any other packages' lists. * * Install non-dir Install symlink Install dir * Exists not X X X * File/node/symlink LXR LXR BXR * Directory BXR - - * * X: extract file/node/link/directory * LX: atomic overwrite leaving backup * B: ordinary backup * R: later remove from other packages' lists * -: do nothing * * After we've done this we go through the remaining things in the * lists of packages we're trying to remove (including the old * version of the current package). This happens in reverse order, * so that we process files before the directories (or symlinks-to- * directories) containing them. * * + If the thing is a conffile then we leave it alone for the purge * operation. * + Otherwise, there are several possibilities too: * - The listed thing does not exist. We ignore it. * - The listed thing is a directory or a symlink to a directory. * We delete it only if it isn't listed in any other package. * - The listed thing is not a directory, but was part of the package * that was upgraded, we check to make sure the files aren't the * same ones from the old package by checking dev/inode * - The listed thing is not a directory or a symlink to one (ie, * it's a plain file, device, pipe, &c, or a symlink to one, or a * dangling symlink). We delete it. * * The removed packages' list becomes empty (of course, the new * version of the package we're installing will have a new list, * which replaces the old version's list). * * If at any stage we remove a file from a package's list, and the * package isn't one we're already processing, and the package's * list becomes empty as a result, we ‘vanish’ the package. This * means that we run its postrm with the ‘disappear’ argument, and * put the package in the ‘not-installed’ state. If it had any * conffiles, their hashes and ownership will have been transferred * already, so we just ignore those and forget about them from the * point of view of the disappearing package. * * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the * files get replaced ‘as we go’. */ m_pipe(p1); push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]); pid = subproc_fork(); if (pid == 0) { m_dup2(p1[1],1); close(p1[0]); close(p1[1]); execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL); ohshite(_("unable to execute %s (%s)"), _("package filesystem archive extraction"), BACKEND); } close(p1[1]); p1[1] = -1; newfileslist = NULL; tc.newfilesp = &newfileslist; push_cleanup(cu_fileslist, ~0, NULL, 0, 0); tc.pkg= pkg; tc.backendpipe= p1[0]; r = tar_extractor(&tc, &tf); if (r) { if (errno) { ohshite(_("error reading dpkg-deb tar output")); } else { ohshit(_("corrupted filesystem tarfile - corrupted package archive")); } } fd_skip(p1[0], -1, _("dpkg-deb: zap possible trailing zeros")); close(p1[0]); p1[0] = -1; subproc_wait_check(pid, BACKEND " --fsys-tarfile", PROCPIPE); tar_deferred_extract(newfileslist, pkg); if (oldversionstatus == stat_halfinstalled || oldversionstatus == stat_unpacked) { /* Packages that were in ‘installed’ and ‘postinstfailed’ have been * reduced to ‘unpacked’ by now, by the running of the prerm script. */ pkg_set_status(pkg, stat_halfinstalled); modstatdb_note(pkg); push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg); maintainer_script_alternative(pkg, POSTRMFILE, "post-removal", cidir, cidirrest, "upgrade", "failed-upgrade"); } /* If anything goes wrong while tidying up it's a bit late to do * anything about it. However, we don't install the new status * info yet, so that a future dpkg installation will put everything * right (we hope). * * If something does go wrong later the ‘conflictor’ package will be * left in the ‘removal_failed’ state. Removing or installing it * will be impossible if it was required because of the conflict with * the package we're installing now and (presumably) the dependency * by other packages. This means that the files it contains in * common with this package will hang around until we successfully * get this package installed, after which point we can trust the * conflicting package's file list, which will have been updated to * remove any files in this package. */ push_checkpoint(~ehflag_bombout, ehflag_normaltidy); /* Now we delete all the files that were in the old version of * the package only, except (old or new) conffiles, which we leave * alone. */ reversefilelist_init(&rlistit,pkg->clientdata->files); while ((namenode= reversefilelist_next(&rlistit))) { struct filenamenode *usenode; if ((namenode->flags & fnnf_new_conff) || (namenode->flags & fnnf_new_inarchive)) continue; usenode = namenodetouse(namenode, pkg, &pkg->installed); trig_file_activate(usenode, pkg); varbuf_trunc(&fnamevb, fnameidlu); varbuf_add_str(&fnamevb, usenode->name); varbuf_end_str(&fnamevb); if (!stat(namenode->name,&stab) && S_ISDIR(stab.st_mode)) { debug(dbg_eachfiledetail, "process_archive: %s is a directory", namenode->name); if (dir_is_used_by_others(namenode, pkg)) continue; } if (lstat(fnamevb.buf, &oldfs)) { if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR)) warning(_("could not stat old file '%.250s' so not deleting it: %s"), fnamevb.buf, strerror(errno)); continue; } if (S_ISDIR(oldfs.st_mode)) { if (rmdir(fnamevb.buf)) { warning(_("unable to delete old directory '%.250s': %s"), namenode->name, strerror(errno)); } else if ((namenode->flags & fnnf_old_conff)) { warning(_("old conffile '%.250s' was an empty directory " "(and has now been deleted)"), namenode->name); } } else { struct fileinlist *sameas = NULL; static struct stat empty_stat; struct varbuf cfilename = VARBUF_INIT; /* * Ok, it's an old file, but is it really not in the new package? * It might be known by a different name because of symlinks. * * We need to check to make sure, so we stat the file, then compare * it to the new list. If we find a dev/inode match, we assume they * are the same file, and leave it alone. NOTE: we don't check in * other packages for sanity reasons (we don't want to stat _all_ * the files on the system). * * We run down the list of _new_ files in this package. This keeps * the process a little leaner. We are only worried about new ones * since ones that stayed the same don't really apply here. */ /* If we can't stat the old or new file, or it's a directory, * we leave it up to the normal code. */ debug(dbg_eachfile, "process_archive: checking %s for same files on " "upgrade/downgrade", fnamevb.buf); for (cfile= newfileslist; cfile; cfile= cfile->next) { /* If the file has been filtered then treat it as if it didn't exist * on the file system. */ if (cfile->namenode->flags & fnnf_filtered) continue; if (!cfile->namenode->filestat) { struct stat tmp_stat; varbuf_reset(&cfilename); varbuf_add_str(&cfilename, instdir); varbuf_add_char(&cfilename, '/'); varbuf_add_str(&cfilename, cfile->namenode->name); varbuf_end_str(&cfilename); if (lstat(cfilename.buf, &tmp_stat) == 0) { cfile->namenode->filestat = nfmalloc(sizeof(struct stat)); memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat)); } else { if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR)) ohshite(_("unable to stat other new file `%.250s'"), cfile->namenode->name); cfile->namenode->filestat = &empty_stat; continue; } } if (cfile->namenode->filestat == &empty_stat) continue; if (oldfs.st_dev == cfile->namenode->filestat->st_dev && oldfs.st_ino == cfile->namenode->filestat->st_ino) { if (sameas) warning(_("old file '%.250s' is the same as several new files! " "(both '%.250s' and '%.250s')"), fnamevb.buf, sameas->namenode->name, cfile->namenode->name); sameas= cfile; debug(dbg_eachfile, "process_archive: not removing %s," " since it matches %s", fnamevb.buf, cfile->namenode->name); } } varbuf_destroy(&cfilename); if ((namenode->flags & fnnf_old_conff)) { if (sameas) { if (sameas->namenode->flags & fnnf_new_conff) { if (!strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG)) { sameas->namenode->oldhash= namenode->oldhash; debug(dbg_eachfile, "process_archive: old conff %s" " is same as new conff %s, copying hash", namenode->name, sameas->namenode->name); } else { debug(dbg_eachfile, "process_archive: old conff %s" " is same as new conff %s but latter already has hash", namenode->name, sameas->namenode->name); } } } else { debug(dbg_eachfile, "process_archive: old conff %s" " is disappearing", namenode->name); namenode->flags |= fnnf_obs_conff; newconff_append(&newconffileslastp, namenode); addfiletolist(&tc, namenode); } continue; } if (sameas) continue; if (secure_unlink_statted(fnamevb.buf, &oldfs)) { warning(_("unable to securely remove old file '%.250s': %s"), namenode->name, strerror(errno)); } } /* !S_ISDIR */ } /* OK, now we can write the updated files-in-this package list, * since we've done away (hopefully) with all the old junk. */ write_filelist_except(pkg, &pkg->available, newfileslist, 0); /* Trigger interests may have changed. * Firstly we go through the old list of interests deleting them. * Then we go through the new list adding them. */ strcpy(cidirrest, TRIGGERSCIFILE); trig_parse_ci(pkgadminfile(pkg, &pkg->installed, TRIGGERSCIFILE), trig_cicb_interest_delete, NULL, pkg, &pkg->installed); trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg, &pkg->available); trig_file_interests_save(); /* We also install the new maintainer scripts, and any other * cruft that may have come along with the package. First * we go through the existing scripts replacing or removing * them as appropriate; then we go through the new scripts * (any that are left) and install them. */ debug(dbg_general, "process_archive updating info directory"); pkg_infodb_update(pkg, cidir, cidirrest); /* * Update the status database. * * This involves copying each field across from the ‘available’ * to the ‘installed’ half of the pkg structure. * For some of the fields we have to do a complicated construction * operation; for others we can just copy the value. * We tackle the fields in the order they appear, so that * we don't miss any out :-). * At least we don't have to copy any strings that are referred * to, because these are never modified and never freed. */ /* The dependencies are the most difficult. We have to build * a whole new forward dependency tree. At least the reverse * links (linking our deppossi's into the reverse chains) * can be done by copy_dependency_links. */ newdeplist = NULL; newdeplistlastp = &newdeplist; for (dep= pkg->available.depends; dep; dep= dep->next) { newdep= nfmalloc(sizeof(struct dependency)); newdep->up= pkg; newdep->next = NULL; newdep->list = NULL; newpossilastp = &newdep->list; for (possi= dep->list; possi; possi= possi->next) { newpossi= nfmalloc(sizeof(struct deppossi)); newpossi->up= newdep; newpossi->ed= possi->ed; newpossi->next = NULL; newpossi->rev_next = newpossi->rev_prev = NULL; newpossi->arch_is_implicit = possi->arch_is_implicit; newpossi->arch = possi->arch; newpossi->verrel= possi->verrel; if (possi->verrel != dvr_none) newpossi->version= possi->version; else blankversion(&newpossi->version); newpossi->cyclebreak = false; *newpossilastp= newpossi; newpossilastp= &newpossi->next; } newdep->type= dep->type; *newdeplistlastp= newdep; newdeplistlastp= &newdep->next; } /* Right, now we've replicated the forward tree, we * get copy_dependency_links to remove all the old dependency * structures from the reverse links and add the new dependency * structures in instead. It also copies the new dependency * structure pointer for this package into the right field. */ copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0); /* We copy the text fields. */ pkg->installed.essential= pkg->available.essential; pkg->installed.multiarch = pkg->available.multiarch; pkg->installed.description= pkg->available.description; pkg->installed.maintainer= pkg->available.maintainer; pkg->installed.source= pkg->available.source; pkg->installed.arch = pkg->available.arch; pkg->installed.installedsize= pkg->available.installedsize; pkg->installed.version= pkg->available.version; pkg->installed.origin = pkg->available.origin; pkg->installed.bugs = pkg->available.bugs; /* We have to generate our own conffiles structure. */ pkg->installed.conffiles = NULL; iconffileslastp = &pkg->installed.conffiles; for (cfile= newconffiles; cfile; cfile= cfile->next) { newiconff= nfmalloc(sizeof(struct conffile)); newiconff->next = NULL; newiconff->name= nfstrsave(cfile->namenode->name); newiconff->hash= nfstrsave(cfile->namenode->oldhash); newiconff->obsolete= !!(cfile->namenode->flags & fnnf_obs_conff); *iconffileslastp= newiconff; iconffileslastp= &newiconff->next; } /* We can just copy the arbitrary fields list, because it is * never even rearranged. Phew! */ pkg->installed.arbs= pkg->available.arbs; /* Check for disappearing packages: * We go through all the packages on the system looking for ones * whose files are entirely part of the one we've just unpacked * (and which actually *have* some files!). * * Any that we find are removed - we run the postrm with ‘disappear’ * as an argument, and remove their info/... files and status info. * Conffiles are ignored (the new package had better do something * with them!). */ it = pkg_db_iter_new(); while ((otherpkg = pkg_db_iter_next_pkg(it)) != NULL) { ensure_package_clientdata(otherpkg); if (otherpkg == pkg || otherpkg->status == stat_notinstalled || otherpkg->status == stat_configfiles || otherpkg->clientdata->istobe == itb_remove || !otherpkg->clientdata->files) continue; debug(dbg_veryverbose, "process_archive checking disappearance %s", pkg_name(otherpkg, pnaw_nonambig)); assert(otherpkg->clientdata->istobe == itb_normal || otherpkg->clientdata->istobe == itb_deconfigure); for (cfile= otherpkg->clientdata->files; cfile && !strcmp(cfile->namenode->name,"/."); cfile= cfile->next); if (!cfile) { debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear"); continue; } for (cfile= otherpkg->clientdata->files; cfile && !filesavespackage(cfile,otherpkg,pkg); cfile= cfile->next); if (cfile) continue; /* So dependency things will give right answers ... */ otherpkg->clientdata->istobe= itb_remove; debug(dbg_veryverbose, "process_archive disappear checking dependencies"); for (pdep = otherpkg->set->depended.installed; pdep; pdep = pdep->rev_next) { if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends && pdep->up->type != dep_recommends) continue; if (depisok(pdep->up, &depprobwhy, NULL, NULL, false)) continue; varbuf_end_str(&depprobwhy); debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf); break; } if (!pdep) { /* If we haven't found a reason not to yet, let's look some more. */ for (providecheck= otherpkg->installed.depends; providecheck; providecheck= providecheck->next) { if (providecheck->type != dep_provides) continue; for (pdep = providecheck->list->ed->depended.installed; pdep; pdep = pdep->rev_next) { if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends && pdep->up->type != dep_recommends) continue; if (depisok(pdep->up, &depprobwhy, NULL, NULL, false)) continue; varbuf_end_str(&depprobwhy); debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s", providecheck->list->ed->name, depprobwhy.buf); goto break_from_both_loops_at_once; } } break_from_both_loops_at_once:; } otherpkg->clientdata->istobe= itb_normal; if (pdep) continue; /* No, we're disappearing it. This is the wrong time to go and * run maintainer scripts and things, as we can't back out. But * what can we do ? It has to be run this late. */ pkg_disappear(otherpkg, pkg); } /* while (otherpkg= ... */ pkg_db_iter_free(it); /* Delete files from any other packages' lists. * We have to do this before we claim this package is in any * sane kind of state, as otherwise we might delete by mistake * a file that we overwrote, when we remove the package which * had the version we overwrote. To prevent this we make * sure that we don't claim this package is OK until we * have claimed ‘ownership’ of all its files. */ for (cfile= newfileslist; cfile; cfile= cfile->next) { struct filepackages_iterator *iter; struct pkgset *divpkgset; if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue; if (cfile->namenode->divert && cfile->namenode->divert->useinstead) { divpkgset = cfile->namenode->divert->pkgset; if (divpkgset == pkg->set) { debug(dbg_eachfile, "process_archive not overwriting any `%s' (overriding, `%s')", cfile->namenode->name, cfile->namenode->divert->useinstead->name); continue; } else { debug(dbg_eachfile, "process_archive looking for overwriting `%s' (overridden by %s)", cfile->namenode->name, divpkgset ? divpkgset->name : "<local>"); } } else { divpkgset = NULL; debug(dbg_eachfile, "process_archive looking for overwriting `%s'", cfile->namenode->name); } iter = filepackages_iter_new(cfile->namenode); while ((otherpkg = filepackages_iter_next(iter))) { debug(dbg_eachfiledetail, "process_archive ... found in %s", pkg_name(otherpkg, pnaw_nonambig)); /* If !fileslistvalid then it's one of the disappeared packages above * and we don't bother with it here, clearly. */ if (otherpkg == pkg || !otherpkg->clientdata->fileslistvalid) continue; if (otherpkg->set == divpkgset) { debug(dbg_eachfiledetail, "process_archive ... diverted, skipping"); continue; } /* Found one. We delete remove the list entry for this file, * (and any others in the same package) and then mark the package * as requiring a reread. */ write_filelist_except(otherpkg, &otherpkg->installed, otherpkg->clientdata->files, fnnf_elide_other_lists); ensure_package_clientdata(otherpkg); debug(dbg_veryverbose, "process_archive overwrote from %s", pkg_name(otherpkg, pnaw_nonambig)); } filepackages_iter_free(iter); } /* Right, the package we've unpacked is now in a reasonable state. * The only thing that we have left to do with it is remove * backup files, and we can leave the user to fix that if and when * it happens (we leave the reinstall required flag, of course). */ pkg_set_status(pkg, stat_unpacked); modstatdb_note(pkg); /* Now we delete all the backup files that we made when * extracting the archive - except for files listed as conffiles * in the new package. * This time we count it as an error if something goes wrong. * * Note that we don't ever delete things that were in the old * package as a conffile and don't appear at all in the new. * They stay recorded as obsolete conffiles and will eventually * (if not taken over by another package) be forgotten. */ for (cfile= newfileslist; cfile; cfile= cfile->next) { if (cfile->namenode->flags & fnnf_new_conff) continue; varbuf_trunc(&fnametmpvb, fnameidlu); varbuf_add_str(&fnametmpvb, namenodetouse(cfile->namenode, pkg, &pkg->installed)->name); varbuf_add_str(&fnametmpvb, DPKGTEMPEXT); varbuf_end_str(&fnametmpvb); ensure_pathname_nonexisting(fnametmpvb.buf); } /* OK, we're now fully done with the main package. * This is quite a nice state, so we don't unwind past here. */ pkg_reset_eflags(pkg); modstatdb_note(pkg); push_checkpoint(~ehflag_bombout, ehflag_normaltidy); /* Only the removal of the conflictor left to do. * The files list for the conflictor is still a little inconsistent in-core, * as we have not yet updated the filename->packages mappings; however, * the package->filenames mapping is. */ for (i = 0 ; i < cflict_index ; i++) { /* We need to have the most up-to-date info about which files are * what ... */ ensure_allinstfiles_available(); removal_bulk(conflictor[i]); } if (cipaction->arg_int == act_install) add_to_queue(pkg); }