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