Initial stab at untangling the #include maze. Probably needs a second pass.
[oweals/opkg-lede.git] / libopkg / opkg_install.c
1 /* opkg_install.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include <stdio.h>
19 #include <time.h>
20 #include <signal.h>
21 #include <unistd.h>
22
23 #include "pkg.h"
24 #include "pkg_hash.h"
25 #include "pkg_extract.h"
26
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
31
32 #include "opkg_utils.h"
33 #include "opkg_message.h"
34 #include "opkg_cmd.h"
35 #include "opkg_defines.h"
36
37 #include "sprintf_alloc.h"
38 #include "file_util.h"
39 #include "xsystem.h"
40 #include "libbb/libbb.h"
41
42 static int
43 satisfy_dependencies_for(pkg_t *pkg)
44 {
45      int i, err;
46      pkg_vec_t *depends = pkg_vec_alloc();
47      pkg_t *dep;
48      char **tmp, **unresolved = NULL;
49      int ndepends;
50
51      ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends, 
52                                                         &unresolved);
53
54      if (unresolved) {
55           opkg_msg(ERROR, "Cannot satisfy the following dependencies for %s:\n",
56                        pkg->name);
57           tmp = unresolved;
58           while (*unresolved) {
59                opkg_msg(ERROR, "\t%s", *unresolved);
60                free(*unresolved);
61                unresolved++;
62           }
63           free(tmp);
64           opkg_message(ERROR, "\n");
65           if (! conf->force_depends) {
66                opkg_msg(INFO,
67                             "This could mean that your package list is out of date or that the packages\n"
68                             "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
69                             "of this problem try again with the '-force-depends' option.\n");
70                pkg_vec_free(depends);
71                return -1;
72           }
73      }
74
75      if (ndepends <= 0) {
76           pkg_vec_free(depends);
77           return 0;
78      }
79
80      /* Mark packages as to-be-installed */
81      for (i=0; i < depends->len; i++) {
82           /* Dependencies should be installed the same place as pkg */
83           if (depends->pkgs[i]->dest == NULL) {
84                depends->pkgs[i]->dest = pkg->dest;
85           }
86           depends->pkgs[i]->state_want = SW_INSTALL;
87      }
88
89      for (i = 0; i < depends->len; i++) {
90           dep = depends->pkgs[i];
91           /* The package was uninstalled when we started, but another
92              dep earlier in this loop may have depended on it and pulled
93              it in, so check first. */
94           if ((dep->state_status != SS_INSTALLED)
95               && (dep->state_status != SS_UNPACKED)) {
96                opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n");
97                err = opkg_install_pkg(dep, 0);
98                /* mark this package as having been automatically installed to
99                 * satisfy a dependancy */
100                dep->auto_installed = 1;
101                if (err) {
102                     pkg_vec_free(depends);
103                     return err;
104                }
105           }
106      }
107
108      pkg_vec_free(depends);
109
110      return 0;
111 }
112
113 static int
114 check_conflicts_for(pkg_t *pkg)
115 {
116      int i;
117      pkg_vec_t *conflicts = NULL;
118      message_level_t level;
119
120      if (conf->force_depends) {
121           level = NOTICE;
122      } else {
123           level = ERROR;
124      }
125
126      if (!conf->force_depends)
127           conflicts = pkg_hash_fetch_conflicts(pkg);
128
129      if (conflicts) {
130           opkg_msg(level, "The following packages conflict with %s:\n",
131                        pkg->name);
132           i = 0;
133           while (i < conflicts->len)
134                opkg_msg(level, "\t%s", conflicts->pkgs[i++]->name);
135           opkg_msg(level, "\n");
136           pkg_vec_free(conflicts);
137           return -1;
138      }
139      return 0;
140 }
141
142 static int
143 update_file_ownership(pkg_t *new_pkg, pkg_t *old_pkg)
144 {
145      str_list_t *new_list, *old_list;
146      str_list_elt_t *iter, *niter;
147
148      new_list = pkg_get_installed_files(new_pkg);
149      if (new_list == NULL)
150              return -1;
151
152      for (iter = str_list_first(new_list), niter = str_list_next(new_list, iter); 
153              iter; 
154              iter = niter, niter = str_list_next(new_list, niter)) {
155           char *new_file = (char *)iter->data;
156           pkg_t *owner = file_hash_get_file_owner(new_file);
157           if (!new_file)
158                opkg_msg(ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
159           if (!owner || (owner == old_pkg))
160                file_hash_set_file_owner(new_file, new_pkg);
161      }
162
163      if (old_pkg) {
164           old_list = pkg_get_installed_files(old_pkg);
165           if (old_list == NULL) {
166                   pkg_free_installed_files(new_pkg);
167                   return -1;
168           }
169
170           for (iter = str_list_first(old_list), niter = str_list_next(old_list, iter); 
171                   iter; 
172                   iter = niter, niter = str_list_next(old_list, niter)) {
173                char *old_file = (char *)iter->data;
174                pkg_t *owner = file_hash_get_file_owner(old_file);
175                if (owner == old_pkg) {
176                     /* obsolete */
177                     hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
178                }
179           }
180           pkg_free_installed_files(old_pkg);
181      }
182      pkg_free_installed_files(new_pkg);
183      return 0;
184 }
185
186 static int
187 verify_pkg_installable(pkg_t *pkg)
188 {
189         unsigned long kbs_available, pkg_size_kbs;
190         char *root_dir;
191
192         if (conf->force_space || pkg->installed_size == 0)
193                 return 0;
194
195         root_dir = pkg->dest ? pkg->dest->root_dir :
196                                                 conf->default_dest->root_dir;
197         kbs_available = get_available_kbytes(root_dir);
198
199         pkg_size_kbs = (pkg->installed_size + 1023)/1024;
200
201         if (pkg_size_kbs >= kbs_available) {
202                 opkg_msg(ERROR, "Only have %dkb available on filesystem %s, "
203                         "pkg %s needs %d\n",
204                         kbs_available, root_dir, pkg->name, pkg_size_kbs);
205                 return -1;
206         }
207
208      return 0;
209 }
210
211 static int
212 unpack_pkg_control_files(pkg_t *pkg)
213 {
214      int err;
215      char *conffiles_file_name;
216      char *root_dir;
217      FILE *conffiles_file;
218
219      sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
220
221      pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
222      if (pkg->tmp_unpack_dir == NULL) {
223           opkg_perror(ERROR, "Failed to create temporary directory '%s'",
224                        pkg->tmp_unpack_dir);
225           return -1;
226      }
227
228      err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
229      if (err) {
230           return err;
231      }
232
233      /* XXX: CLEANUP: There might be a cleaner place to read in the
234         conffiles. Seems like I should be able to get everything to go
235         through pkg_init_from_file. If so, maybe it would make sense to
236         move all of unpack_pkg_control_files to that function. */
237
238      /* Don't need to re-read conffiles if we already have it */
239      if (!nv_pair_list_empty(&pkg->conffiles)) {
240           return 0;
241      }
242
243      sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
244      if (! file_exists(conffiles_file_name)) {
245           free(conffiles_file_name);
246           return 0;
247      }
248     
249      conffiles_file = fopen(conffiles_file_name, "r");
250      if (conffiles_file == NULL) {
251           opkg_perror(ERROR, "Failed to open %s", conffiles_file_name);
252           free(conffiles_file_name);
253           return -1;
254      }
255      free(conffiles_file_name);
256
257      while (1) {
258           char *cf_name;
259           char *cf_name_in_dest;
260
261           cf_name = file_read_line_alloc(conffiles_file);
262           if (cf_name == NULL) {
263                break;
264           }
265           if (cf_name[0] == '\0') {
266                continue;
267           }
268
269           /* Prepend dest->root_dir to conffile name.
270              Take pains to avoid multiple slashes. */
271           root_dir = pkg->dest->root_dir;
272           if (conf->offline_root)
273                /* skip the offline_root prefix */
274                root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
275           sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
276                         cf_name[0] == '/' ? (cf_name + 1) : cf_name);
277
278           /* Can't get an md5sum now, (file isn't extracted yet).
279              We'll wait until resolve_conffiles */
280           conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
281
282           free(cf_name);
283           free(cf_name_in_dest);
284      }
285
286      fclose(conffiles_file);
287
288      return 0;
289 }
290
291 /*
292  * Remove packages which were auto_installed due to a dependency by old_pkg,
293  * which are no longer a dependency in the new (upgraded) pkg.
294  */
295 static int
296 pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg) 
297 {
298         int i, j, k, l, found;
299         int n_deps;
300         pkg_t *p;
301         struct compound_depend *cd0, *cd1;
302         abstract_pkg_t **dependents;
303
304         int count0 = old_pkg->pre_depends_count +
305                                 old_pkg->depends_count +
306                                 old_pkg->recommends_count +
307                                 old_pkg->suggests_count;
308         int count1 = pkg->pre_depends_count +
309                                 pkg->depends_count +
310                                 pkg->recommends_count +
311                                 pkg->suggests_count;
312
313         for (i=0; i<count0; i++) {
314                 cd0 = &old_pkg->depends[i];
315                 if (cd0->type != DEPEND)
316                         continue;
317                 for (j=0; j<cd0->possibility_count; j++) {
318
319                         found = 0;
320
321                         for (k=0; k<count1; k++) {
322                                 cd1 = &pkg->depends[i];
323                                 if (cd1->type != DEPEND)
324                                         continue;
325                                 for (l=0; l<cd1->possibility_count; l++) {
326                                         if (cd0->possibilities[j]
327                                          == cd1->possibilities[l]) {
328                                                 found = 1;
329                                                 break;
330                                         }
331                                 }
332                                 if (found)
333                                         break;
334                         }
335
336                         if (found)
337                                 continue;
338
339                         /*
340                          * old_pkg has a dependency that pkg does not.
341                          */
342                         p = pkg_hash_fetch_installed_by_name(
343                                         cd0->possibilities[j]->pkg->name);
344
345                         if (!p)
346                                 continue;
347
348                         if (!p->auto_installed)
349                                 continue;
350
351                         n_deps = pkg_has_installed_dependents(p, &dependents);
352                         n_deps--; /* don't count old_pkg */
353
354                         if (n_deps == 0) {
355                                 opkg_msg(NOTICE, "%s was autoinstalled and is "
356                                                 "now orphaned, removing.\n",
357                                                 p->name);
358
359                                 /* p has one installed dependency (old_pkg),
360                                  * which we need to ignore during removal. */
361                                 p->state_flag |= SF_REPLACE;
362
363                                 opkg_remove_pkg(p, 0);
364                         } else 
365                                 opkg_msg(INFO, "%s was autoinstalled and is "
366                                                 "still required by %d "
367                                                 "installed packages.\n",
368                                                 p->name, n_deps);
369
370                 }
371         }
372
373         return 0;
374 }
375
376 /* returns number of installed replacees */
377 static int
378 pkg_get_installed_replacees(pkg_t *pkg, pkg_vec_t *installed_replacees)
379 {
380      abstract_pkg_t **replaces = pkg->replaces;
381      int replaces_count = pkg->replaces_count;
382      int i, j;
383      for (i = 0; i < replaces_count; i++) {
384           abstract_pkg_t *ab_pkg = replaces[i];
385           pkg_vec_t *pkg_vec = ab_pkg->pkgs;
386           if (pkg_vec) {
387                for (j = 0; j < pkg_vec->len; j++) {
388                     pkg_t *replacee = pkg_vec->pkgs[j];
389                     if (!pkg_conflicts(pkg, replacee))
390                          continue;
391                     if (replacee->state_status == SS_INSTALLED) {
392                          pkg_vec_insert(installed_replacees, replacee);
393                     }
394                }
395           }
396      }
397      return installed_replacees->len;
398 }
399
400 static int
401 pkg_remove_installed_replacees(pkg_vec_t *replacees)
402 {
403      int i;
404      int replaces_count = replacees->len;
405      for (i = 0; i < replaces_count; i++) {
406           pkg_t *replacee = replacees->pkgs[i];
407           int err;
408           replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
409           err = opkg_remove_pkg(replacee, 0);
410           if (err)
411                return err;
412      }
413      return 0;
414 }
415
416 /* to unwind the removal: make sure they are installed */
417 static int
418 pkg_remove_installed_replacees_unwind(pkg_vec_t *replacees)
419 {
420      int i, err;
421      int replaces_count = replacees->len;
422      for (i = 0; i < replaces_count; i++) {
423           pkg_t *replacee = replacees->pkgs[i];
424           if (replacee->state_status != SS_INSTALLED) {
425                opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
426                err = opkg_install_pkg(replacee, 0);
427                if (err)
428                     return err;
429           }
430      }
431      return 0;
432 }
433
434 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
435 static int
436 opkg_install_check_downgrade(pkg_t *pkg, pkg_t *old_pkg, int message)
437 {         
438      if (old_pkg) {
439           char message_out[15];
440           char *old_version = pkg_version_str_alloc(old_pkg);
441           char *new_version = pkg_version_str_alloc(pkg);
442           int cmp = pkg_compare_versions(old_pkg, pkg);
443           int rc = 0;
444
445           memset(message_out,'\x0',15);
446           strncpy (message_out,"Upgrading ",strlen("Upgrading ")); 
447           if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
448              cmp = -1 ;                                       /* then we force opkg to downgrade */ 
449              strncpy (message_out,"Downgrading ",strlen("Downgrading "));         /* We need to use a value < 0 because in the 0 case we are asking to */
450                                                               /* reinstall, and some check could fail asking the "force-reinstall" option */
451           } 
452
453           if (cmp > 0) {
454                opkg_msg(NOTICE,
455                             "Not downgrading package %s on %s from %s to %s.\n",
456                             old_pkg->name, old_pkg->dest->name, old_version, new_version);
457                rc = 1;
458           } else if (cmp < 0) {
459                opkg_msg(NOTICE, "%s%s on %s from %s to %s...\n",
460                             message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
461                pkg->dest = old_pkg->dest;
462                rc = 0;
463           } else /* cmp == 0 */ {
464                if (conf->force_reinstall) {
465                     opkg_msg(NOTICE, "Reinstalling %s (%s) on %s...\n",
466                                  pkg->name, new_version, old_pkg->dest->name);
467                     pkg->dest = old_pkg->dest;
468                     rc = 0;
469                } else {
470                     opkg_msg(NOTICE, "%s (%s) already install on %s."
471                                    " Not reinstalling.\n",
472                                  pkg->name, new_version, old_pkg->dest->name);
473                     rc = 1;
474                }
475           } 
476           free(old_version);
477           free(new_version);
478           return rc;
479      } else {
480       char message_out[15] ;
481       memset(message_out,'\x0',15);
482       if ( message ) 
483           strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
484       else
485           strncpy( message_out,"Installing ",strlen("Installing ") );
486           char *version = pkg_version_str_alloc(pkg);
487       
488           opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out,
489                        pkg->name, version, pkg->dest->name);
490           free(version);
491           return 0;
492      }
493 }
494
495
496 static int
497 prerm_upgrade_old_pkg(pkg_t *pkg, pkg_t *old_pkg)
498 {
499      /* DPKG_INCOMPATIBILITY:
500         dpkg does some things here that we don't do yet. Do we care?
501         
502         1. If a version of the package is already installed, call
503            old-prerm upgrade new-version
504         2. If the script runs but exits with a non-zero exit status
505            new-prerm failed-upgrade old-version
506            Error unwind, for both the above cases:
507            old-postinst abort-upgrade new-version
508      */
509      return 0;
510 }
511
512 static int
513 prerm_upgrade_old_pkg_unwind(pkg_t *pkg, pkg_t *old_pkg)
514 {
515      /* DPKG_INCOMPATIBILITY:
516         dpkg does some things here that we don't do yet. Do we care?
517         (See prerm_upgrade_old_package for details)
518      */
519      return 0;
520 }
521
522 static int
523 prerm_deconfigure_conflictors(pkg_t *pkg, pkg_vec_t *conflictors)
524 {
525      /* DPKG_INCOMPATIBILITY:
526         dpkg does some things here that we don't do yet. Do we care?
527         2. If a 'conflicting' package is being removed at the same time:
528                 1. If any packages depended on that conflicting package and
529                    --auto-deconfigure is specified, call, for each such package:
530                    deconfigured's-prerm deconfigure \
531                    in-favour package-being-installed version \
532                    removing conflicting-package version
533                 Error unwind:
534                    deconfigured's-postinst abort-deconfigure \
535                    in-favour package-being-installed-but-failed version \
536                    removing conflicting-package version
537
538                    The deconfigured packages are marked as requiring
539                    configuration, so that if --install is used they will be
540                    configured again if possible.
541                 2. To prepare for removal of the conflicting package, call:
542                    conflictor's-prerm remove in-favour package new-version
543                 Error unwind:
544                    conflictor's-postinst abort-remove in-favour package new-version
545      */
546      return 0;
547 }
548
549 static int
550 prerm_deconfigure_conflictors_unwind(pkg_t *pkg, pkg_vec_t *conflictors)
551 {
552      /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
553         do yet. Do we care?  (See prerm_deconfigure_conflictors for
554         details) */
555      return 0;
556 }
557
558 static int
559 preinst_configure(pkg_t *pkg, pkg_t *old_pkg)
560 {
561      int err;
562      char *preinst_args;
563
564      if (old_pkg) {
565           char *old_version = pkg_version_str_alloc(old_pkg);
566           sprintf_alloc(&preinst_args, "upgrade %s", old_version);
567           free(old_version);
568      } else if (pkg->state_status == SS_CONFIG_FILES) {
569           char *pkg_version = pkg_version_str_alloc(pkg);
570           sprintf_alloc(&preinst_args, "install %s", pkg_version);
571           free(pkg_version);
572      } else {
573           preinst_args = xstrdup("install");
574      }
575
576      err = pkg_run_script(pkg, "preinst", preinst_args);
577      if (err) {
578           opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name);
579           return -1;
580      }
581
582      free(preinst_args);
583
584      return 0;
585 }
586
587 static int
588 preinst_configure_unwind(pkg_t *pkg, pkg_t *old_pkg)
589 {
590      /* DPKG_INCOMPATIBILITY:
591         dpkg does the following error unwind, should we?
592         pkg->postrm abort-upgrade old-version
593         OR pkg->postrm abort-install old-version
594         OR pkg->postrm abort-install
595      */
596      return 0;
597 }
598
599 static char *
600 backup_filename_alloc(const char *file_name)
601 {
602      char *backup;
603
604      sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
605
606      return backup;
607 }
608
609
610 static int
611 backup_make_backup(const char *file_name)
612 {
613      int err;
614      char *backup;
615     
616      backup = backup_filename_alloc(file_name);
617      err = file_copy(file_name, backup);
618      if (err) {
619           opkg_msg(ERROR, "Failed to copy %s to %s\n",
620                        file_name, backup);
621      }
622
623      free(backup);
624
625      return err;
626 }
627
628 static int
629 backup_exists_for(const char *file_name)
630 {
631      int ret;
632      char *backup;
633
634      backup = backup_filename_alloc(file_name);
635
636      ret = file_exists(backup);
637
638      free(backup);
639
640      return ret;
641 }
642
643 static int
644 backup_remove(const char *file_name)
645 {
646      char *backup;
647
648      backup = backup_filename_alloc(file_name);
649      unlink(backup);
650      free(backup);
651
652      return 0;
653 }
654
655 static int
656 backup_modified_conffiles(pkg_t *pkg, pkg_t *old_pkg)
657 {
658      int err;
659      conffile_list_elt_t *iter;
660      conffile_t *cf;
661
662      if (conf->noaction) return 0;
663
664      /* Backup all modified conffiles */
665      if (old_pkg) {
666           for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
667                char *cf_name;
668                
669                cf = iter->data;
670                cf_name = root_filename_alloc(cf->name);
671
672                /* Don't worry if the conffile is just plain gone */
673                if (file_exists(cf_name) && conffile_has_been_modified(cf)) {
674                     err = backup_make_backup(cf_name);
675                     if (err) {
676                          return err;
677                     }
678                }
679                free(cf_name);
680           }
681      }
682
683      /* Backup all conffiles that were not conffiles in old_pkg */
684      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
685           char *cf_name;
686           cf = (conffile_t *)iter->data;
687           cf_name = root_filename_alloc(cf->name);
688           /* Ignore if this was a conffile in old_pkg as well */
689           if (pkg_get_conffile(old_pkg, cf->name)) {
690                continue;
691           }
692
693           if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
694                err = backup_make_backup(cf_name);
695                if (err) {
696                     return err;
697                }
698           }
699           free(cf_name);
700      }
701
702      return 0;
703 }
704
705 static int
706 backup_modified_conffiles_unwind(pkg_t *pkg, pkg_t *old_pkg)
707 {
708      conffile_list_elt_t *iter;
709
710      if (old_pkg) {
711           for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
712                backup_remove(((nv_pair_t *)iter->data)->name);
713           }
714      }
715
716      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
717           backup_remove(((nv_pair_t *)iter->data)->name);
718      }
719
720      return 0;
721 }
722
723
724 static int
725 check_data_file_clashes(pkg_t *pkg, pkg_t *old_pkg)
726 {
727      /* DPKG_INCOMPATIBILITY:
728         opkg takes a slightly different approach than dpkg at this
729         point.  dpkg installs each file in the new package while
730         creating a backup for any file that is replaced, (so that it
731         can unwind if necessary).  To avoid complexity and redundant
732         storage, opkg doesn't do any installation until later, (at the
733         point at which dpkg removes the backups.
734         
735         But, we do have to check for data file clashes, since after
736         installing a package with a file clash, removing either of the
737         packages involved in the clash has the potential to break the
738         other package.
739      */
740      str_list_t *files_list;
741      str_list_elt_t *iter, *niter;
742
743      int clashes = 0;
744
745      files_list = pkg_get_installed_files(pkg);
746      if (files_list == NULL)
747              return -1;
748
749      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
750              iter; 
751              iter = niter, niter = str_list_next(files_list, iter)) {
752           char *root_filename;
753           char *filename = (char *) iter->data;
754           root_filename = root_filename_alloc(filename);
755           if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
756                pkg_t *owner;
757                pkg_t *obs;
758
759                if (backup_exists_for(root_filename)) {
760                     continue;
761                }
762
763                /* Pre-existing files are OK if force-overwrite was asserted. */ 
764                if (conf->force_overwrite) {
765                     /* but we need to change who owns this file */
766                     file_hash_set_file_owner(filename, pkg);
767                     continue;
768                }
769
770                owner = file_hash_get_file_owner(filename);
771
772                /* Pre-existing files are OK if owned by the pkg being upgraded. */
773                if (owner && old_pkg) {
774                     if (strcmp(owner->name, old_pkg->name) == 0) {
775                          continue;
776                     }
777                }
778
779                /* Pre-existing files are OK if owned by a package replaced by new pkg. */
780                if (owner) {
781                     opkg_msg(DEBUG2, "Checking replaces for %s in package %s\n",
782                                 filename, owner->name);
783                     if (pkg_replaces(pkg, owner)) {
784                          continue;
785                     }
786 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
787    then it's ok to overwrite. */
788                     if (strcmp(owner->name,pkg->name)==0){
789                          opkg_msg(INFO, "Replacing pre-existing file %s"
790                                         " owned by package %s\n",
791                                         filename, owner->name);
792                          continue;
793                     }
794                }
795
796                /* Pre-existing files are OK if they are obsolete */
797                obs = hash_table_get(&conf->obs_file_hash, filename);
798                if (obs) {
799                     opkg_msg(INFO, "Pre-exiting file %s is obsolete."
800                                    " obs_pkg=%s\n",
801                                     filename, obs->name);
802                     continue;
803                }
804
805                /* We have found a clash. */
806                opkg_msg(ERROR, "Package %s wants to install file %s\n"
807                             "\tBut that file is already provided by package ",
808                             pkg->name, filename);
809                if (owner) {
810                     opkg_msg(ERROR, "%s\n", owner->name);
811                } else {
812                     opkg_msg(ERROR, "<no package>\n"
813                         "Please move this file out of the way and try again.\n");
814                }
815                clashes++;
816           }
817           free(root_filename);
818      }
819      pkg_free_installed_files(pkg);
820
821      return clashes;
822 }
823
824 /*
825  * XXX: This function sucks, as does the below comment.
826  */
827 static int
828 check_data_file_clashes_change(pkg_t *pkg, pkg_t *old_pkg)
829 {
830     /* Basically that's the worst hack I could do to be able to change ownership of
831        file list, but, being that we have no way to unwind the mods, due to structure
832        of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
833        What we do here is change the ownership of file in hash if a replace ( or similar events
834        happens )
835        Only the action that are needed to change name should be considered.
836        @@@ To change after 1.0 release.
837      */
838      str_list_t *files_list;
839      str_list_elt_t *iter, *niter;
840
841      char *root_filename = NULL;
842
843      files_list = pkg_get_installed_files(pkg);
844      if (files_list == NULL)
845              return -1;
846
847      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
848              iter; 
849              iter = niter, niter = str_list_next(files_list, niter)) {
850           char *filename = (char *) iter->data;
851           if (root_filename) {
852               free(root_filename);
853               root_filename = NULL;
854           }
855           root_filename = root_filename_alloc(filename);
856           if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
857                pkg_t *owner;
858
859                owner = file_hash_get_file_owner(filename);
860
861                if (conf->force_overwrite) {
862                     /* but we need to change who owns this file */
863                     file_hash_set_file_owner(filename, pkg);
864                     continue;
865                }
866
867
868                /* Pre-existing files are OK if owned by a package replaced by new pkg. */
869                if (owner) {
870                     if (pkg_replaces(pkg, owner)) {
871 /* It's now time to change the owner of that file. 
872    It has been "replaced" from the new "Replaces", then I need to inform lists file about that.  */
873                          opkg_msg(INFO, "Replacing pre-existing file %s "
874                                          "owned by package %s\n",
875                                          filename, owner->name);
876                          file_hash_set_file_owner(filename, pkg);
877                          continue;
878                     }
879                }
880
881           }
882      }
883      if (root_filename) {
884          free(root_filename);
885          root_filename = NULL;
886      }
887      pkg_free_installed_files(pkg);
888
889      return 0;
890 }
891
892 static int
893 check_data_file_clashes_unwind(pkg_t *pkg, pkg_t *old_pkg)
894 {
895      /* Nothing to do since check_data_file_clashes doesn't change state */
896      return 0;
897 }
898
899 static int
900 postrm_upgrade_old_pkg(pkg_t *pkg, pkg_t *old_pkg)
901 {
902      /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
903         1. If the package is being upgraded, call
904            old-postrm upgrade new-version
905         2. If this fails, attempt:
906            new-postrm failed-upgrade old-version
907         Error unwind, for both cases:
908            old-preinst abort-upgrade new-version    */
909      return 0;
910 }
911
912 static int
913 postrm_upgrade_old_pkg_unwind(pkg_t *pkg, pkg_t *old_pkg)
914 {
915      /* DPKG_INCOMPATIBILITY:
916         dpkg does some things here that we don't do yet. Do we care?
917         (See postrm_upgrade_old_pkg for details)
918      */
919     return 0;
920 }
921
922 static int
923 remove_obsolesced_files(pkg_t *pkg, pkg_t *old_pkg)
924 {
925      int err;
926      str_list_t *old_files;
927      str_list_elt_t *of;
928      str_list_t *new_files;
929      str_list_elt_t *nf;
930      hash_table_t new_files_table;
931
932      old_files = pkg_get_installed_files(old_pkg);
933      if (old_files == NULL)
934           return -1;
935
936      new_files = pkg_get_installed_files(pkg);
937      if (new_files == NULL) {
938           pkg_free_installed_files(old_pkg);
939           return -1;
940      }
941
942      new_files_table.entries = NULL;
943      hash_table_init("new_files" , &new_files_table, 20);
944      for (nf = str_list_first(new_files); nf; nf = str_list_next(new_files, nf)) {
945          if (nf && nf->data)
946             hash_table_insert(&new_files_table, nf->data, nf->data);
947      }
948
949      for (of = str_list_first(old_files); of; of = str_list_next(old_files, of)) {
950           pkg_t *owner;
951           char *old, *new;
952           old = (char *)of->data;
953           new = (char *) hash_table_get (&new_files_table, old);
954           if (new)
955                continue;
956
957           if (file_is_dir(old)) {
958                continue;
959           }
960           owner = file_hash_get_file_owner(old);
961           if (owner != old_pkg) {
962                /* in case obsolete file no longer belongs to old_pkg */
963                continue;
964           }
965  
966           /* old file is obsolete */
967           opkg_msg(INFO, "Removing obsolete file %s.\n", old);
968           if (!conf->noaction) {
969                err = unlink(old);
970                if (err) {
971                     opkg_perror(ERROR, "unlinking %s failed", old);
972                }
973           }
974      }
975
976      hash_table_deinit(&new_files_table);
977      pkg_free_installed_files(old_pkg);
978      pkg_free_installed_files(pkg);
979
980      return 0;
981 }
982
983 static int
984 install_maintainer_scripts(pkg_t *pkg, pkg_t *old_pkg)
985 {
986      int ret;
987      char *prefix;
988
989      sprintf_alloc(&prefix, "%s.", pkg->name);
990      ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
991                                                         pkg->dest->info_dir,
992                                                         prefix);
993      free(prefix);
994      return ret;
995 }
996
997 static int
998 remove_disappeared(pkg_t *pkg)
999 {
1000      /* DPKG_INCOMPATIBILITY:
1001         This is a fairly sophisticated dpkg operation. Shall we
1002         skip it? */
1003      
1004      /* Any packages all of whose files have been overwritten during the
1005         installation, and which aren't required for dependencies, are
1006         considered to have been removed. For each such package
1007         1. disappearer's-postrm disappear overwriter overwriter-version
1008         2. The package's maintainer scripts are removed
1009         3. It is noted in the status database as being in a sane state,
1010            namely not installed (any conffiles it may have are ignored,
1011            rather than being removed by dpkg). Note that disappearing
1012            packages do not have their prerm called, because dpkg doesn't
1013            know in advance that the package is going to vanish.
1014      */
1015      return 0;
1016 }
1017
1018 static int
1019 install_data_files(pkg_t *pkg)
1020 {
1021      int err;
1022
1023      /* opkg takes a slightly different approach to data file backups
1024         than dpkg. Rather than removing backups at this point, we
1025         actually do the data file installation now. See comments in
1026         check_data_file_clashes() for more details. */
1027     
1028      opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir);
1029      err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1030      if (err) {
1031           return err;
1032      }
1033
1034      /* The "Essential" control field may only be present in the control
1035       * file and not in the Packages list. Ensure we capture it regardless.
1036       *
1037       * XXX: This should be fixed outside of opkg, in the Package list.
1038       */
1039      set_flags_from_control(pkg) ;
1040      
1041      opkg_msg(DEBUG, "Calling pkg_write_filelist.\n");
1042      err = pkg_write_filelist(pkg);
1043      if (err)
1044           return err;
1045
1046      /* XXX: FEATURE: opkg should identify any files which existed
1047         before installation and which were overwritten, (see
1048         check_data_file_clashes()). What it must do is remove any such
1049         files from the filelist of the old package which provided the
1050         file. Otherwise, if the old package were removed at some point
1051         it would break the new package. Removing the new package will
1052         also break the old one, but this cannot be helped since the old
1053         package's file has already been deleted. This is the importance
1054         of check_data_file_clashes(), and only allowing opkg to install
1055         a clashing package with a user force. */
1056
1057      return 0;
1058 }
1059
1060 static int
1061 resolve_conffiles(pkg_t *pkg)
1062 {
1063      conffile_list_elt_t *iter;
1064      conffile_t *cf;
1065      char *cf_backup;
1066      char *md5sum;
1067
1068      if (conf->noaction) return 0;
1069
1070      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1071           char *root_filename;
1072           cf = (conffile_t *)iter->data;
1073           root_filename = root_filename_alloc(cf->name);
1074
1075           /* Might need to initialize the md5sum for each conffile */
1076           if (cf->value == NULL) {
1077                cf->value = file_md5sum_alloc(root_filename);
1078           }
1079
1080           if (!file_exists(root_filename)) {
1081                free(root_filename);
1082                continue;
1083           }
1084
1085           cf_backup = backup_filename_alloc(root_filename);
1086
1087           if (file_exists(cf_backup)) {
1088               /* Let's compute md5 to test if files are changed */
1089               md5sum = file_md5sum_alloc(cf_backup);
1090               if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
1091                   if (conf->force_maintainer) {
1092                       opkg_msg(NOTICE, "Conffile %s using maintainer's setting.\n",
1093                                       cf_backup);
1094                   } else {
1095                       char *new_conffile;
1096                       sprintf_alloc(&new_conffile, "%s-opkg", root_filename);
1097                       opkg_msg(NOTICE, "Existing conffile %s "
1098                            "is different from the conffile in the new package."
1099                            " The new conffile will be placed at %s.\n",
1100                            root_filename, new_conffile);
1101                       rename(root_filename, new_conffile);
1102                       rename(cf_backup, root_filename);
1103                       free(new_conffile);
1104                   }
1105               }
1106               unlink(cf_backup);
1107               if (md5sum)
1108                   free(md5sum);
1109           }
1110
1111           free(cf_backup);
1112           free(root_filename);
1113      }
1114
1115      return 0;
1116 }
1117
1118
1119 int
1120 opkg_install_by_name(const char *pkg_name)
1121 {
1122      int cmp;
1123      pkg_t *old, *new;
1124      char *old_version, *new_version;
1125
1126      old = pkg_hash_fetch_installed_by_name(pkg_name);
1127      if (old)
1128         opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n",
1129                         old->version);
1130     
1131      new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name);
1132      if (new == NULL)
1133         return -1;
1134
1135      opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:");
1136      if ( old ) 
1137         opkg_msg(DEBUG2, " old %s ", old->version);
1138      opkg_msg(DEBUG2, " new %s\n", new->version);
1139
1140      new->state_flag |= SF_USER;
1141      if (old) {
1142           old_version = pkg_version_str_alloc(old);
1143           new_version = pkg_version_str_alloc(new);
1144
1145           cmp = pkg_compare_versions(old, new);
1146           if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
1147              opkg_msg(DEBUG, "Forcing downgrade\n");
1148              cmp = -1 ;                                       /* then we force opkg to downgrade */ 
1149                                                               /* We need to use a value < 0 because in the 0 case we are asking to */
1150                                                               /* reinstall, and some check could fail asking the "force-reinstall" option */
1151           } 
1152           opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
1153                        "\n\t%s is installed "
1154                        "\n\t%s is available "
1155                        "\n\t%d was comparison result\n",
1156                        pkg_name, old_version, new_version, cmp);
1157           if (cmp == 0 && !conf->force_reinstall) {
1158                opkg_msg(NOTICE,
1159                             "Package %s (%s) installed in %s is up to date.\n",
1160                             old->name, old_version, old->dest->name);
1161                free(old_version);
1162                free(new_version);
1163                return 0;
1164           } else if (cmp > 0) {
1165                opkg_msg(NOTICE,
1166                             "Not downgrading package %s on %s from %s to %s.\n",
1167                             old->name, old->dest->name, old_version, new_version);
1168                free(old_version);
1169                free(new_version);
1170                return 0;
1171           } else if (cmp < 0) {
1172                new->dest = old->dest;
1173                old->state_want = SW_DEINSTALL;    /* Here probably the problem for bug 1277 */
1174           }
1175           free(old_version);
1176           free(new_version);
1177      }
1178
1179      opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n");
1180      return opkg_install_pkg(new, 0);
1181 }
1182
1183 /**
1184  *  @brief Really install a pkg_t 
1185  */
1186 int
1187 opkg_install_pkg(pkg_t *pkg, int from_upgrade)
1188 {
1189      int err = 0;
1190      int message = 0;
1191      pkg_t *old_pkg = NULL;
1192      pkg_vec_t *replacees;
1193      abstract_pkg_t *ab_pkg = NULL;
1194      int old_state_flag;
1195      char* file_md5;
1196 #ifdef HAVE_SHA256
1197      char* file_sha256;
1198 #endif
1199      sigset_t newset, oldset;
1200
1201      if ( from_upgrade ) 
1202         message = 1;            /* Coming from an upgrade, and should change the output message */
1203
1204      opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n");
1205
1206      if (!pkg_arch_supported(pkg)) {
1207           opkg_msg(ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1208                        pkg->architecture, pkg->name);
1209           return -1;
1210      }
1211      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
1212           err = satisfy_dependencies_for(pkg);
1213           if (err)
1214                   return -1;
1215
1216           opkg_msg(NOTICE, "Package %s is already installed on %s.\n", 
1217                        pkg->name, pkg->dest->name);
1218           return 0;
1219      }
1220
1221      if (pkg->dest == NULL) {
1222           pkg->dest = conf->default_dest;
1223      }
1224
1225      old_pkg = pkg_hash_fetch_installed_by_name(pkg->name);
1226
1227      err = opkg_install_check_downgrade(pkg, old_pkg, message);
1228      if (err)
1229              return -1;
1230
1231      pkg->state_want = SW_INSTALL;
1232      if (old_pkg){                          
1233          old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
1234      }
1235
1236      err = check_conflicts_for(pkg);
1237      if (err)
1238              return -1;
1239     
1240      /* this setup is to remove the upgrade scenario in the end when
1241         installing pkg A, A deps B & B deps on A. So both B and A are
1242         installed. Then A's installation is started resulting in an
1243         uncecessary upgrade */ 
1244      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0)
1245              return 0;
1246     
1247      err = verify_pkg_installable(pkg);
1248      if (err)
1249              return -1;
1250
1251      if (pkg->local_filename == NULL) {
1252           err = opkg_download_pkg(pkg, conf->tmp_dir);
1253           if (err) {
1254                opkg_msg(ERROR, "Failed to download %s. "
1255                                "Perhaps you need to run 'opkg update'?\n",
1256                             pkg->name);
1257                return -1;
1258           }
1259      }
1260
1261      /* check that the repository is valid */
1262      #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1263      char *list_file_name, *sig_file_name, *lists_dir;
1264
1265      /* check to ensure the package has come from a repository */
1266      if (conf->check_signature && pkg->src)
1267      {
1268        sprintf_alloc (&lists_dir, "%s",
1269                      (conf->restrict_to_default_dest)
1270                       ? conf->default_dest->lists_dir
1271                       : conf->lists_dir);
1272        sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
1273        sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
1274
1275        if (file_exists (sig_file_name))
1276        {
1277          if (opkg_verify_file (list_file_name, sig_file_name)){
1278            opkg_msg(ERROR, "Failed to verify the signature of %s.\n",
1279                            list_file_name);
1280            return -1;
1281          }
1282        }else{
1283          opkg_msg(ERROR, "Signature file is missing for %s. "
1284                          "Perhaps you need to run 'opkg update'?\n",
1285                          pkg->name);
1286          return -1;
1287        }
1288
1289        free (lists_dir);
1290        free (list_file_name);
1291        free (sig_file_name);
1292      }
1293      #endif
1294
1295      /* Check for md5 values */
1296      if (pkg->md5sum)
1297      {
1298          file_md5 = file_md5sum_alloc(pkg->local_filename);
1299          if (file_md5 && strcmp(file_md5, pkg->md5sum))
1300          {
1301               opkg_msg(ERROR, "Package %s md5sum mismatch. "
1302                         "Either the opkg or the package index are corrupt. "
1303                         "Try 'opkg update'.\n",
1304                         pkg->name);
1305               free(file_md5);
1306               return -1;
1307          }
1308          if (file_md5)
1309               free(file_md5);
1310      }
1311
1312 #ifdef HAVE_SHA256
1313      /* Check for sha256 value */
1314      if(pkg->sha256sum)
1315      {
1316          file_sha256 = file_sha256sum_alloc(pkg->local_filename);
1317          if (file_sha256 && strcmp(file_sha256, pkg->sha256sum))
1318          {
1319               opkg_msg(ERROR, "Package %s sha256sum mismatch. "
1320                         "Either the opkg or the package index are corrupt. "
1321                         "Try 'opkg update'.\n",
1322                         pkg->name);
1323               free(file_sha256);
1324               return -1;
1325          }
1326          if (file_sha256)
1327               free(file_sha256);
1328      }
1329 #endif
1330
1331      if (pkg->tmp_unpack_dir == NULL) {
1332           if (unpack_pkg_control_files(pkg) == -1) {
1333                opkg_msg(ERROR, "Failed to unpack control files from %s.\n",
1334                                pkg->local_filename);
1335                return -1;
1336           }
1337      }
1338
1339      err = update_file_ownership(pkg, old_pkg);
1340      if (err)
1341              return -1;
1342
1343      if (conf->nodeps == 0) {
1344           err = satisfy_dependencies_for(pkg);
1345           if (err)
1346                 return -1;
1347           if (pkg->state_status == SS_UNPACKED)
1348                /* Circular dependency has installed it for us. */
1349                 return 0;
1350      }
1351
1352      replacees = pkg_vec_alloc();
1353      pkg_get_installed_replacees(pkg, replacees);
1354
1355      /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1356
1357           sigemptyset(&newset);
1358           sigaddset(&newset, SIGINT);
1359           sigprocmask(SIG_BLOCK, &newset, &oldset);
1360
1361           opkg_state_changed++;
1362           pkg->state_flag |= SF_FILELIST_CHANGED;
1363
1364           if (old_pkg)
1365                pkg_remove_orphan_dependent(pkg, old_pkg);
1366
1367           /* XXX: BUG: we really should treat replacement more like an upgrade
1368            *      Instead, we're going to remove the replacees 
1369            */
1370           err = pkg_remove_installed_replacees(replacees);
1371           if (err)
1372                   goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1373
1374           err = prerm_upgrade_old_pkg(pkg, old_pkg);
1375           if (err)
1376                   goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1377
1378           err = prerm_deconfigure_conflictors(pkg, replacees);
1379           if (err)
1380                   goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1381
1382           err = preinst_configure(pkg, old_pkg);
1383           if (err)
1384                   goto UNWIND_PREINST_CONFIGURE;
1385
1386           err = backup_modified_conffiles(pkg, old_pkg);
1387           if (err)
1388                   goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1389
1390           err = check_data_file_clashes(pkg, old_pkg);
1391           if (err)
1392                   goto UNWIND_CHECK_DATA_FILE_CLASHES;
1393
1394           err = postrm_upgrade_old_pkg(pkg, old_pkg);
1395           if (err)
1396                   goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1397
1398           if (conf->noaction)
1399                   return 0;
1400
1401           /* point of no return: no unwinding after this */
1402           if (old_pkg && !conf->force_reinstall) {
1403                old_pkg->state_want = SW_DEINSTALL;
1404
1405                if (old_pkg->state_flag & SF_NOPRUNE) {
1406                     opkg_msg(INFO, "Not removing obsolesced files because "
1407                                     "package %s marked noprune.\n",
1408                                     old_pkg->name);
1409                } else {
1410                     opkg_msg(INFO, "Removing obsolesced files for %s\n",
1411                                     old_pkg->name);
1412                     if (remove_obsolesced_files(pkg, old_pkg)) {
1413                         opkg_msg(ERROR, "Failed to determine "
1414                                         "obsolete files from previously "
1415                                         "installed %s\n", old_pkg->name);
1416                     }
1417                }
1418
1419                /* removing files from old package, to avoid ghost files */ 
1420                remove_data_files_and_list(old_pkg);
1421                remove_maintainer_scripts(old_pkg);
1422           }
1423
1424
1425           opkg_msg(INFO, "Installing maintainer scripts.\n");
1426           if (install_maintainer_scripts(pkg, old_pkg)) {
1427                 opkg_msg(ERROR, "Failed to extract maintainer scripts for %s."
1428                                " Package debris may remain!\n",
1429                                pkg->name);
1430                 goto pkg_is_hosed;
1431           }
1432
1433           /* the following just returns 0 */
1434           remove_disappeared(pkg);
1435
1436           opkg_msg(INFO, "Installing data files for %s.\n", pkg->name);
1437
1438           if (install_data_files(pkg)) {
1439                 opkg_msg(ERROR, "Failed to extract data files for %s. "
1440                                 "Package debris may remain!\n",
1441                                pkg->name);
1442                 goto pkg_is_hosed;
1443           }
1444
1445           err = check_data_file_clashes_change(pkg, old_pkg);
1446           if (err) {
1447                 opkg_msg(ERROR, "check_data_file_clashes_change() failed for "
1448                                "for files belonging to %s.\n",
1449                                pkg->name);
1450           }
1451
1452           opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name);
1453           resolve_conffiles(pkg);
1454
1455           pkg->state_status = SS_UNPACKED;
1456           old_state_flag = pkg->state_flag;
1457           pkg->state_flag &= ~SF_PREFER;
1458           opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n",
1459                           pkg->name, old_state_flag, pkg->state_flag);
1460
1461           if (old_pkg && !conf->force_reinstall) {
1462                old_pkg->state_status = SS_NOT_INSTALLED;
1463           }
1464
1465           time(&pkg->installed_time);
1466
1467           ab_pkg = pkg->parent;
1468           if (ab_pkg)
1469                ab_pkg->state_status = pkg->state_status;
1470
1471           sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1472           pkg_vec_free (replacees);
1473           return 0;
1474      
1475
1476      UNWIND_POSTRM_UPGRADE_OLD_PKG:
1477           postrm_upgrade_old_pkg_unwind(pkg, old_pkg);
1478      UNWIND_CHECK_DATA_FILE_CLASHES:
1479           check_data_file_clashes_unwind(pkg, old_pkg);
1480      UNWIND_BACKUP_MODIFIED_CONFFILES:
1481           backup_modified_conffiles_unwind(pkg, old_pkg);
1482      UNWIND_PREINST_CONFIGURE:
1483           preinst_configure_unwind(pkg, old_pkg);
1484      UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1485           prerm_deconfigure_conflictors_unwind(pkg, replacees);
1486      UNWIND_PRERM_UPGRADE_OLD_PKG:
1487           prerm_upgrade_old_pkg_unwind(pkg, old_pkg);
1488      UNWIND_REMOVE_INSTALLED_REPLACEES:
1489           pkg_remove_installed_replacees_unwind(replacees);
1490
1491 pkg_is_hosed:
1492           sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1493
1494           pkg_vec_free (replacees);
1495           return -1;
1496 }