• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt