1 /* pkg.c - the opkg package management system
5 Copyright (C) 2001 University of Southern California
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.
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.
28 #include "pkg_parse.h"
29 #include "pkg_extract.h"
30 #include "opkg_message.h"
31 #include "opkg_utils.h"
33 #include "libbb/libbb.h"
34 #include "sprintf_alloc.h"
35 #include "file_util.h"
37 #include "opkg_conf.h"
39 typedef struct enum_map enum_map_t
;
45 static const enum_map_t pkg_state_want_map
[] = {
46 {SW_UNKNOWN
, "unknown"},
47 {SW_INSTALL
, "install"},
48 {SW_DEINSTALL
, "deinstall"},
52 static const enum_map_t pkg_state_flag_map
[] = {
54 {SF_REINSTREQ
, "reinstreq"},
56 {SF_REPLACE
, "replace"},
57 {SF_NOPRUNE
, "noprune"},
58 {SF_PREFER
, "prefer"},
59 {SF_OBSOLETE
, "obsolete"},
63 static const enum_map_t pkg_state_status_map
[] = {
64 {SS_NOT_INSTALLED
, "not-installed"},
65 {SS_UNPACKED
, "unpacked"},
66 {SS_HALF_CONFIGURED
, "half-configured"},
67 {SS_INSTALLED
, "installed"},
68 {SS_HALF_INSTALLED
, "half-installed"},
69 {SS_CONFIG_FILES
, "config-files"},
70 {SS_POST_INST_FAILED
, "post-inst-failed"},
71 {SS_REMOVAL_FAILED
, "removal-failed"}
74 static void pkg_init(pkg_t
* pkg
)
82 pkg
->architecture
= NULL
;
83 pkg
->maintainer
= NULL
;
85 pkg
->description
= NULL
;
86 pkg
->state_want
= SW_UNKNOWN
;
87 pkg
->state_flag
= SF_OK
;
88 pkg
->state_status
= SS_NOT_INSTALLED
;
89 pkg
->depends_str
= NULL
;
90 pkg
->provides_str
= NULL
;
91 pkg
->depends_count
= 0;
93 pkg
->suggests_str
= NULL
;
94 pkg
->recommends_str
= NULL
;
95 pkg
->suggests_count
= 0;
96 pkg
->recommends_count
= 0;
98 active_list_init(&pkg
->list
);
100 pkg
->conflicts
= NULL
;
101 pkg
->conflicts_count
= 0;
103 pkg
->replaces
= NULL
;
104 pkg
->replaces_count
= 0;
106 pkg
->pre_depends_count
= 0;
107 pkg
->pre_depends_str
= NULL
;
108 pkg
->provides_count
= 0;
109 pkg
->provides
= NULL
;
110 pkg
->filename
= NULL
;
111 pkg
->local_filename
= NULL
;
112 pkg
->tmp_unpack_dir
= NULL
;
114 #if defined HAVE_SHA256
115 pkg
->sha256sum
= NULL
;
118 pkg
->installed_size
= 0;
119 pkg
->priority
= NULL
;
121 conffile_list_init(&pkg
->conffiles
);
122 pkg
->installed_files
= NULL
;
123 pkg
->installed_files_ref_cnt
= 0;
125 pkg
->provided_by_hand
= 0;
132 pkg
= xcalloc(1, sizeof(pkg_t
));
138 static void compound_depend_deinit(compound_depend_t
* depends
)
141 for (i
= 0; i
< depends
->possibility_count
; i
++) {
143 d
= depends
->possibilities
[i
];
147 free(depends
->possibilities
);
150 void pkg_deinit(pkg_t
* pkg
)
163 /* revision shares storage with version, so don't free */
164 pkg
->revision
= NULL
;
166 /* owned by opkg_conf_t */
168 /* owned by opkg_conf_t */
171 if (pkg
->architecture
)
172 free(pkg
->architecture
);
173 pkg
->architecture
= NULL
;
176 free(pkg
->maintainer
);
177 pkg
->maintainer
= NULL
;
183 if (pkg
->description
)
184 free(pkg
->description
);
185 pkg
->description
= NULL
;
187 pkg
->state_want
= SW_UNKNOWN
;
188 pkg
->state_flag
= SF_OK
;
189 pkg
->state_status
= SS_NOT_INSTALLED
;
191 active_list_clear(&pkg
->list
);
195 pkg
->replaces
= NULL
;
198 int count
= pkg
->pre_depends_count
200 + pkg
->recommends_count
+ pkg
->suggests_count
;
202 for (i
= 0; i
< count
; i
++)
203 compound_depend_deinit(&pkg
->depends
[i
]);
207 if (pkg
->conflicts
) {
208 for (i
= 0; i
< pkg
->conflicts_count
; i
++)
209 compound_depend_deinit(&pkg
->conflicts
[i
]);
210 free(pkg
->conflicts
);
216 pkg
->pre_depends_count
= 0;
217 pkg
->provides_count
= 0;
221 pkg
->filename
= NULL
;
223 if (pkg
->local_filename
)
224 free(pkg
->local_filename
);
225 pkg
->local_filename
= NULL
;
227 /* CLEANUP: It'd be nice to pullin the cleanup function from
228 opkg_install.c here. See comment in
229 opkg_install.c:cleanup_temporary_files */
230 if (pkg
->tmp_unpack_dir
)
231 free(pkg
->tmp_unpack_dir
);
232 pkg
->tmp_unpack_dir
= NULL
;
238 #if defined HAVE_SHA256
240 free(pkg
->sha256sum
);
241 pkg
->sha256sum
= NULL
;
246 pkg
->priority
= NULL
;
252 conffile_list_deinit(&pkg
->conffiles
);
254 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
255 since if they are calling deinit, they should know. Maybe do an
256 assertion here instead? */
257 pkg
->installed_files_ref_cnt
= 1;
258 pkg_free_installed_files(pkg
);
266 int pkg_init_from_file(pkg_t
* pkg
, const char *filename
)
270 char *control_path
, *tmp
;
274 pkg
->local_filename
= xstrdup(filename
);
276 tmp
= xstrdup(filename
);
277 sprintf_alloc(&control_path
, "%s/%s.control.XXXXXX",
278 conf
->tmp_dir
, basename(tmp
));
280 fd
= mkstemp(control_path
);
282 opkg_perror(ERROR
, "Failed to make temp file %s", control_path
);
287 control_file
= fdopen(fd
, "r+");
288 if (control_file
== NULL
) {
289 opkg_perror(ERROR
, "Failed to fdopen %s", control_path
);
295 err
= pkg_extract_control_file_to_stream(pkg
, control_file
);
297 opkg_msg(ERROR
, "Failed to extract control file from %s.\n",
302 rewind(control_file
);
304 if ((err
= pkg_parse_from_stream(pkg
, control_file
, 0))) {
306 opkg_msg(ERROR
, "Malformed package file %s.\n",
313 fclose(control_file
);
315 unlink(control_path
);
322 /* Merge any new information in newpkg into oldpkg */
323 int pkg_merge(pkg_t
* oldpkg
, pkg_t
* newpkg
)
325 if (oldpkg
== newpkg
) {
329 if (!oldpkg
->auto_installed
)
330 oldpkg
->auto_installed
= newpkg
->auto_installed
;
333 oldpkg
->src
= newpkg
->src
;
335 oldpkg
->dest
= newpkg
->dest
;
336 if (!oldpkg
->architecture
)
337 oldpkg
->architecture
= xstrdup(newpkg
->architecture
);
338 if (!oldpkg
->arch_priority
)
339 oldpkg
->arch_priority
= newpkg
->arch_priority
;
340 if (!oldpkg
->section
)
341 oldpkg
->section
= xstrdup(newpkg
->section
);
342 if (!oldpkg
->maintainer
)
343 oldpkg
->maintainer
= xstrdup(newpkg
->maintainer
);
344 if (!oldpkg
->description
)
345 oldpkg
->description
= xstrdup(newpkg
->description
);
347 if (!oldpkg
->depends_count
&& !oldpkg
->pre_depends_count
348 && !oldpkg
->recommends_count
&& !oldpkg
->suggests_count
) {
349 oldpkg
->depends_count
= newpkg
->depends_count
;
350 newpkg
->depends_count
= 0;
352 oldpkg
->depends
= newpkg
->depends
;
353 newpkg
->depends
= NULL
;
355 oldpkg
->pre_depends_count
= newpkg
->pre_depends_count
;
356 newpkg
->pre_depends_count
= 0;
358 oldpkg
->recommends_count
= newpkg
->recommends_count
;
359 newpkg
->recommends_count
= 0;
361 oldpkg
->suggests_count
= newpkg
->suggests_count
;
362 newpkg
->suggests_count
= 0;
365 if (oldpkg
->provides_count
<= 1) {
366 oldpkg
->provides_count
= newpkg
->provides_count
;
367 newpkg
->provides_count
= 0;
369 if (!oldpkg
->provides
) {
370 oldpkg
->provides
= newpkg
->provides
;
371 newpkg
->provides
= NULL
;
375 if (!oldpkg
->conflicts_count
) {
376 oldpkg
->conflicts_count
= newpkg
->conflicts_count
;
377 newpkg
->conflicts_count
= 0;
379 oldpkg
->conflicts
= newpkg
->conflicts
;
380 newpkg
->conflicts
= NULL
;
383 if (!oldpkg
->replaces_count
) {
384 oldpkg
->replaces_count
= newpkg
->replaces_count
;
385 newpkg
->replaces_count
= 0;
387 oldpkg
->replaces
= newpkg
->replaces
;
388 newpkg
->replaces
= NULL
;
391 if (!oldpkg
->filename
)
392 oldpkg
->filename
= xstrdup(newpkg
->filename
);
393 if (!oldpkg
->local_filename
)
394 oldpkg
->local_filename
= xstrdup(newpkg
->local_filename
);
395 if (!oldpkg
->tmp_unpack_dir
)
396 oldpkg
->tmp_unpack_dir
= xstrdup(newpkg
->tmp_unpack_dir
);
398 oldpkg
->md5sum
= xstrdup(newpkg
->md5sum
);
399 #if defined HAVE_SHA256
400 if (!oldpkg
->sha256sum
)
401 oldpkg
->sha256sum
= xstrdup(newpkg
->sha256sum
);
404 oldpkg
->size
= newpkg
->size
;
405 if (!oldpkg
->installed_size
)
406 oldpkg
->installed_size
= newpkg
->installed_size
;
407 if (!oldpkg
->priority
)
408 oldpkg
->priority
= xstrdup(newpkg
->priority
);
410 oldpkg
->source
= xstrdup(newpkg
->source
);
412 if (nv_pair_list_empty(&oldpkg
->conffiles
)) {
413 list_splice_init(&newpkg
->conffiles
.head
,
414 &oldpkg
->conffiles
.head
);
417 if (!oldpkg
->installed_files
) {
418 oldpkg
->installed_files
= newpkg
->installed_files
;
419 oldpkg
->installed_files_ref_cnt
=
420 newpkg
->installed_files_ref_cnt
;
421 newpkg
->installed_files
= NULL
;
424 if (!oldpkg
->essential
)
425 oldpkg
->essential
= newpkg
->essential
;
430 static void abstract_pkg_init(abstract_pkg_t
* ab_pkg
)
432 ab_pkg
->provided_by
= abstract_pkg_vec_alloc();
433 ab_pkg
->dependencies_checked
= 0;
434 ab_pkg
->state_status
= SS_NOT_INSTALLED
;
437 abstract_pkg_t
*abstract_pkg_new(void)
439 abstract_pkg_t
*ab_pkg
;
441 ab_pkg
= xcalloc(1, sizeof(abstract_pkg_t
));
442 abstract_pkg_init(ab_pkg
);
447 void set_flags_from_control(pkg_t
* pkg
)
452 sprintf_alloc(&file_name
, "%s/%s.control", pkg
->dest
->info_dir
,
455 fp
= fopen(file_name
, "r");
457 opkg_perror(ERROR
, "Failed to open %s", file_name
);
464 if (pkg_parse_from_stream(pkg
, fp
, PFM_ALL
^ PFM_ESSENTIAL
)) {
466 "Unable to read control file for %s. May be empty.\n",
475 static const char *pkg_state_want_to_str(pkg_state_want_t sw
)
479 for (i
= 0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
480 if (pkg_state_want_map
[i
].value
== sw
) {
481 return pkg_state_want_map
[i
].str
;
485 opkg_msg(ERROR
, "Internal error: state_want=%d\n", sw
);
486 return "<STATE_WANT_UNKNOWN>";
489 pkg_state_want_t
pkg_state_want_from_str(char *str
)
493 for (i
= 0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
494 if (strcmp(str
, pkg_state_want_map
[i
].str
) == 0) {
495 return pkg_state_want_map
[i
].value
;
499 opkg_msg(ERROR
, "Internal error: state_want=%s\n", str
);
503 static char *pkg_state_flag_to_str(pkg_state_flag_t sf
)
509 /* clear the temporary flags before converting to string */
510 sf
&= SF_NONVOLATILE_FLAGS
;
513 return xstrdup("ok");
516 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
517 if (sf
& pkg_state_flag_map
[i
].value
)
518 len
+= strlen(pkg_state_flag_map
[i
].str
) + 1;
521 str
= xmalloc(len
+ 1);
524 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
525 if (sf
& pkg_state_flag_map
[i
].value
) {
526 strncat(str
, pkg_state_flag_map
[i
].str
, len
);
527 strncat(str
, ",", len
);
532 str
[len
- 1] = '\0'; /* squash last comma */
537 pkg_state_flag_t
pkg_state_flag_from_str(const char *str
)
542 unsigned int sfname_len
;
544 if (strcmp(str
, "ok") == 0) {
547 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
548 sfname
= pkg_state_flag_map
[i
].str
;
549 sfname_len
= strlen(sfname
);
550 if (strncmp(str
, sfname
, sfname_len
) == 0) {
551 sf
|= pkg_state_flag_map
[i
].value
;
564 static const char *pkg_state_status_to_str(pkg_state_status_t ss
)
568 for (i
= 0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
569 if (pkg_state_status_map
[i
].value
== ss
) {
570 return pkg_state_status_map
[i
].str
;
574 opkg_msg(ERROR
, "Internal error: state_status=%d\n", ss
);
575 return "<STATE_STATUS_UNKNOWN>";
578 pkg_state_status_t
pkg_state_status_from_str(const char *str
)
582 for (i
= 0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
583 if (strcmp(str
, pkg_state_status_map
[i
].str
) == 0) {
584 return pkg_state_status_map
[i
].value
;
588 opkg_msg(ERROR
, "Internal error: state_status=%s\n", str
);
589 return SS_NOT_INSTALLED
;
592 void pkg_formatted_field(FILE * fp
, pkg_t
* pkg
, const char *field
)
596 int depends_count
= pkg
->pre_depends_count
+
597 pkg
->depends_count
+ pkg
->recommends_count
+ pkg
->suggests_count
;
599 if (strlen(field
) < PKG_MINIMUM_FIELD_NAME_LEN
) {
600 goto UNKNOWN_FMT_FIELD
;
606 if (strcasecmp(field
, "Architecture") == 0) {
607 if (pkg
->architecture
) {
608 fprintf(fp
, "Architecture: %s\n",
611 } else if (strcasecmp(field
, "Auto-Installed") == 0) {
612 if (pkg
->auto_installed
)
613 fprintf(fp
, "Auto-Installed: yes\n");
615 goto UNKNOWN_FMT_FIELD
;
620 if (strcasecmp(field
, "Conffiles") == 0) {
621 conffile_list_elt_t
*iter
;
623 if (nv_pair_list_empty(&pkg
->conffiles
))
626 fprintf(fp
, "Conffiles:\n");
627 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
;
628 iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
629 if (((conffile_t
*) iter
->data
)->name
630 && ((conffile_t
*) iter
->data
)->value
) {
631 fprintf(fp
, " %s %s\n",
632 ((conffile_t
*) iter
->data
)->
634 ((conffile_t
*) iter
->data
)->
638 } else if (strcasecmp(field
, "Conflicts") == 0) {
640 if (pkg
->conflicts_count
) {
641 fprintf(fp
, "Conflicts:");
642 for (i
= 0; i
< pkg
->conflicts_count
; i
++) {
644 pkg
->conflicts
[i
].possibilities
[0];
645 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
648 fprintf(fp
, " (%s%s)",
649 constraint_to_str(cdep
->
657 goto UNKNOWN_FMT_FIELD
;
662 if (strcasecmp(field
, "Depends") == 0) {
663 if (pkg
->depends_count
) {
664 fprintf(fp
, "Depends:");
665 for (j
= 0, i
= 0; i
< depends_count
; i
++) {
666 if (pkg
->depends
[i
].type
!= DEPEND
)
668 str
= pkg_depend_str(pkg
, i
);
669 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
676 } else if (strcasecmp(field
, "Description") == 0) {
677 if (pkg
->description
) {
678 fprintf(fp
, "Description: %s\n",
682 goto UNKNOWN_FMT_FIELD
;
687 if (pkg
->essential
) {
688 fprintf(fp
, "Essential: yes\n");
694 fprintf(fp
, "Filename: %s\n", pkg
->filename
);
699 if (strcasecmp(field
, "Installed-Size") == 0) {
700 fprintf(fp
, "Installed-Size: %ld\n",
701 pkg
->installed_size
);
702 } else if (strcasecmp(field
, "Installed-Time") == 0
703 && pkg
->installed_time
) {
704 fprintf(fp
, "Installed-Time: %lu\n",
705 pkg
->installed_time
);
710 if (strcasecmp(field
, "Maintainer") == 0) {
711 if (pkg
->maintainer
) {
712 fprintf(fp
, "Maintainer: %s\n",
715 } else if (strcasecmp(field
, "MD5sum") == 0) {
717 fprintf(fp
, "MD5Sum: %s\n", pkg
->md5sum
);
720 goto UNKNOWN_FMT_FIELD
;
725 if (strcasecmp(field
, "Package") == 0) {
726 fprintf(fp
, "Package: %s\n", pkg
->name
);
727 } else if (strcasecmp(field
, "Priority") == 0) {
728 fprintf(fp
, "Priority: %s\n", pkg
->priority
);
729 } else if (strcasecmp(field
, "Provides") == 0) {
730 if (pkg
->provides_count
> 1) {
731 fprintf(fp
, "Provides:");
732 for (i
= 1; i
< pkg
->provides_count
; i
++) {
733 fprintf(fp
, "%s %s", i
== 1 ? "" : ",",
734 pkg
->provides
[i
]->name
);
739 goto UNKNOWN_FMT_FIELD
;
744 if (strcasecmp(field
, "Replaces") == 0) {
745 if (pkg
->replaces_count
) {
746 fprintf(fp
, "Replaces:");
747 for (i
= 0; i
< pkg
->replaces_count
; i
++) {
748 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
749 pkg
->replaces
[i
]->name
);
753 } else if (strcasecmp(field
, "Recommends") == 0) {
754 if (pkg
->recommends_count
) {
755 fprintf(fp
, "Recommends:");
756 for (j
= 0, i
= 0; i
< depends_count
; i
++) {
757 if (pkg
->depends
[i
].type
!= RECOMMEND
)
759 str
= pkg_depend_str(pkg
, i
);
760 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
768 goto UNKNOWN_FMT_FIELD
;
773 if (strcasecmp(field
, "Section") == 0) {
775 fprintf(fp
, "Section: %s\n", pkg
->section
);
777 #if defined HAVE_SHA256
778 } else if (strcasecmp(field
, "SHA256sum") == 0) {
779 if (pkg
->sha256sum
) {
780 fprintf(fp
, "SHA256sum: %s\n", pkg
->sha256sum
);
783 } else if (strcasecmp(field
, "Size") == 0) {
785 fprintf(fp
, "Size: %ld\n", pkg
->size
);
787 } else if (strcasecmp(field
, "Source") == 0) {
789 fprintf(fp
, "Source: %s\n", pkg
->source
);
791 } else if (strcasecmp(field
, "Status") == 0) {
792 char *pflag
= pkg_state_flag_to_str(pkg
->state_flag
);
793 fprintf(fp
, "Status: %s %s %s\n",
794 pkg_state_want_to_str(pkg
->state_want
),
796 pkg_state_status_to_str(pkg
->state_status
));
798 } else if (strcasecmp(field
, "Suggests") == 0) {
799 if (pkg
->suggests_count
) {
800 fprintf(fp
, "Suggests:");
801 for (j
= 0, i
= 0; i
< depends_count
; i
++) {
802 if (pkg
->depends
[i
].type
!= SUGGEST
)
804 str
= pkg_depend_str(pkg
, i
);
805 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
813 goto UNKNOWN_FMT_FIELD
;
818 if (strcasecmp(field
, "Tags") == 0) {
820 fprintf(fp
, "Tags: %s\n", pkg
->tags
);
827 char *version
= pkg_version_str_alloc(pkg
);
830 fprintf(fp
, "Version: %s\n", version
);
835 goto UNKNOWN_FMT_FIELD
;
841 opkg_msg(ERROR
, "Internal error: field=%s\n", field
);
844 void pkg_formatted_info(FILE * fp
, pkg_t
* pkg
)
846 pkg_formatted_field(fp
, pkg
, "Package");
847 pkg_formatted_field(fp
, pkg
, "Version");
848 pkg_formatted_field(fp
, pkg
, "Depends");
849 pkg_formatted_field(fp
, pkg
, "Recommends");
850 pkg_formatted_field(fp
, pkg
, "Suggests");
851 pkg_formatted_field(fp
, pkg
, "Provides");
852 pkg_formatted_field(fp
, pkg
, "Replaces");
853 pkg_formatted_field(fp
, pkg
, "Conflicts");
854 pkg_formatted_field(fp
, pkg
, "Status");
855 pkg_formatted_field(fp
, pkg
, "Section");
856 pkg_formatted_field(fp
, pkg
, "Essential");
857 pkg_formatted_field(fp
, pkg
, "Architecture");
858 pkg_formatted_field(fp
, pkg
, "Maintainer");
859 pkg_formatted_field(fp
, pkg
, "MD5sum");
860 pkg_formatted_field(fp
, pkg
, "Size");
861 pkg_formatted_field(fp
, pkg
, "Filename");
862 pkg_formatted_field(fp
, pkg
, "Conffiles");
863 pkg_formatted_field(fp
, pkg
, "Source");
864 pkg_formatted_field(fp
, pkg
, "Description");
865 pkg_formatted_field(fp
, pkg
, "Installed-Time");
866 pkg_formatted_field(fp
, pkg
, "Tags");
870 void pkg_print_status(pkg_t
* pkg
, FILE * file
)
876 pkg_formatted_field(file
, pkg
, "Package");
877 pkg_formatted_field(file
, pkg
, "Version");
878 pkg_formatted_field(file
, pkg
, "Depends");
879 pkg_formatted_field(file
, pkg
, "Recommends");
880 pkg_formatted_field(file
, pkg
, "Suggests");
881 pkg_formatted_field(file
, pkg
, "Provides");
882 pkg_formatted_field(file
, pkg
, "Replaces");
883 pkg_formatted_field(file
, pkg
, "Conflicts");
884 pkg_formatted_field(file
, pkg
, "Status");
885 pkg_formatted_field(file
, pkg
, "Essential");
886 pkg_formatted_field(file
, pkg
, "Architecture");
887 pkg_formatted_field(file
, pkg
, "Conffiles");
888 pkg_formatted_field(file
, pkg
, "Installed-Time");
889 pkg_formatted_field(file
, pkg
, "Auto-Installed");
894 * libdpkg - Debian packaging suite library routines
895 * vercmp.c - comparison of version numbers
897 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
900 /* assume ascii; warning: evaluates x multiple times! */
901 #define order(x) ((x) == '~' ? -1 \
904 : isalpha((x)) ? (x) \
907 static int verrevcmp(const char *val
, const char *ref
)
914 while (*val
|| *ref
) {
917 while ((*val
&& !isdigit(*val
)) || (*ref
&& !isdigit(*ref
))) {
918 int vc
= order(*val
), rc
= order(*ref
);
929 while (isdigit(*val
) && isdigit(*ref
)) {
931 first_diff
= *val
- *ref
;
945 int pkg_compare_versions(const pkg_t
* pkg
, const pkg_t
* ref_pkg
)
949 if (pkg
->epoch
> ref_pkg
->epoch
) {
953 if (pkg
->epoch
< ref_pkg
->epoch
) {
957 r
= verrevcmp(pkg
->version
, ref_pkg
->version
);
962 r
= verrevcmp(pkg
->revision
, ref_pkg
->revision
);
970 int pkg_version_satisfied(pkg_t
* it
, pkg_t
* ref
, const char *op
)
974 r
= pkg_compare_versions(it
, ref
);
976 if (strcmp(op
, "<=") == 0 || strcmp(op
, "<") == 0) {
980 if (strcmp(op
, ">=") == 0 || strcmp(op
, ">") == 0) {
984 if (strcmp(op
, "<<") == 0) {
988 if (strcmp(op
, ">>") == 0) {
992 if (strcmp(op
, "=") == 0) {
996 opkg_msg(ERROR
, "Unknown operator: %s.\n", op
);
1000 int pkg_name_version_and_architecture_compare(const void *p1
, const void *p2
)
1002 const pkg_t
*a
= *(const pkg_t
**)p1
;
1003 const pkg_t
*b
= *(const pkg_t
**)p2
;
1006 if (!a
->name
|| !b
->name
) {
1007 opkg_msg(ERROR
, "Internal error: a->name=%p, b->name=%p.\n",
1012 namecmp
= strcmp(a
->name
, b
->name
);
1015 vercmp
= pkg_compare_versions(a
, b
);
1018 if (!a
->arch_priority
|| !b
->arch_priority
) {
1020 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1021 a
->arch_priority
, b
->arch_priority
);
1024 if (a
->arch_priority
> b
->arch_priority
)
1026 if (a
->arch_priority
< b
->arch_priority
)
1031 int abstract_pkg_name_compare(const void *p1
, const void *p2
)
1033 const abstract_pkg_t
*a
= *(const abstract_pkg_t
**)p1
;
1034 const abstract_pkg_t
*b
= *(const abstract_pkg_t
**)p2
;
1035 if (!a
->name
|| !b
->name
) {
1036 opkg_msg(ERROR
, "Internal error: a->name=%p b->name=%p.\n",
1040 return strcmp(a
->name
, b
->name
);
1043 char *pkg_version_str_alloc(pkg_t
* pkg
)
1049 sprintf_alloc(&version
, "%d:%s-%s",
1050 pkg
->epoch
, pkg
->version
, pkg
->revision
);
1052 sprintf_alloc(&version
, "%d:%s",
1053 pkg
->epoch
, pkg
->version
);
1056 sprintf_alloc(&version
, "%s-%s",
1057 pkg
->version
, pkg
->revision
);
1059 version
= xstrdup(pkg
->version
);
1066 * XXX: this should be broken into two functions
1068 str_list_t
*pkg_get_installed_files(pkg_t
* pkg
)
1071 char *list_file_name
= NULL
;
1072 FILE *list_file
= NULL
;
1074 char *installed_file_name
;
1075 unsigned int rootdirlen
= 0;
1076 int list_from_package
;
1078 pkg
->installed_files_ref_cnt
++;
1080 if (pkg
->installed_files
) {
1081 return pkg
->installed_files
;
1084 pkg
->installed_files
= str_list_alloc();
1087 * For installed packages, look at the package.list file in the database.
1088 * For uninstalled packages, get the file list directly from the package.
1090 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
)
1091 list_from_package
= 1;
1093 list_from_package
= 0;
1095 if (list_from_package
) {
1096 if (pkg
->local_filename
== NULL
) {
1097 return pkg
->installed_files
;
1099 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1100 file. In other words, change deb_extract so that it can
1101 simply return the file list as a char *[] rather than
1102 insisting on writing it to a FILE * as it does now. */
1103 sprintf_alloc(&list_file_name
, "%s/%s.list.XXXXXX",
1104 conf
->tmp_dir
, pkg
->name
);
1105 fd
= mkstemp(list_file_name
);
1107 opkg_perror(ERROR
, "Failed to make temp file %s.",
1109 free(list_file_name
);
1110 return pkg
->installed_files
;
1112 list_file
= fdopen(fd
, "r+");
1113 if (list_file
== NULL
) {
1114 opkg_perror(ERROR
, "Failed to fdopen temp file %s.",
1117 unlink(list_file_name
);
1118 free(list_file_name
);
1119 return pkg
->installed_files
;
1121 err
= pkg_extract_data_file_names_to_stream(pkg
, list_file
);
1123 opkg_msg(ERROR
, "Error extracting file list from %s.\n",
1124 pkg
->local_filename
);
1126 unlink(list_file_name
);
1127 free(list_file_name
);
1128 str_list_deinit(pkg
->installed_files
);
1129 pkg
->installed_files
= NULL
;
1134 sprintf_alloc(&list_file_name
, "%s/%s.list",
1135 pkg
->dest
->info_dir
, pkg
->name
);
1136 list_file
= fopen(list_file_name
, "r");
1137 if (list_file
== NULL
) {
1138 opkg_perror(ERROR
, "Failed to open %s", list_file_name
);
1139 free(list_file_name
);
1140 return pkg
->installed_files
;
1142 free(list_file_name
);
1145 if (conf
->offline_root
)
1146 rootdirlen
= strlen(conf
->offline_root
);
1151 line
= file_read_line_alloc(list_file
);
1157 if (list_from_package
) {
1158 if (*file_name
== '.') {
1161 if (*file_name
== '/') {
1164 sprintf_alloc(&installed_file_name
, "%s%s",
1165 pkg
->dest
->root_dir
, file_name
);
1167 if (conf
->offline_root
&&
1168 strncmp(conf
->offline_root
, file_name
,
1170 sprintf_alloc(&installed_file_name
, "%s%s",
1171 conf
->offline_root
, file_name
);
1173 // already contains root_dir as header -> ABSOLUTE
1174 sprintf_alloc(&installed_file_name
, "%s",
1178 str_list_append(pkg
->installed_files
, installed_file_name
);
1179 free(installed_file_name
);
1185 if (list_from_package
) {
1186 unlink(list_file_name
);
1187 free(list_file_name
);
1190 return pkg
->installed_files
;
1193 /* XXX: CLEANUP: This function and it's counterpart,
1194 (pkg_get_installed_files), do not match our init/deinit naming
1195 convention. Nor the alloc/free convention. But, then again, neither
1196 of these conventions currrently fit the way these two functions
1198 void pkg_free_installed_files(pkg_t
* pkg
)
1200 pkg
->installed_files_ref_cnt
--;
1202 if (pkg
->installed_files_ref_cnt
> 0)
1205 if (pkg
->installed_files
) {
1206 str_list_purge(pkg
->installed_files
);
1209 pkg
->installed_files
= NULL
;
1212 void pkg_remove_installed_files_list(pkg_t
* pkg
)
1214 char *list_file_name
;
1216 sprintf_alloc(&list_file_name
, "%s/%s.list",
1217 pkg
->dest
->info_dir
, pkg
->name
);
1219 if (!conf
->noaction
)
1220 (void)unlink(list_file_name
);
1222 free(list_file_name
);
1225 conffile_t
*pkg_get_conffile(pkg_t
* pkg
, const char *file_name
)
1227 conffile_list_elt_t
*iter
;
1228 conffile_t
*conffile
;
1234 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
;
1235 iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1236 conffile
= (conffile_t
*) iter
->data
;
1238 if (strcmp(conffile
->name
, file_name
) == 0) {
1246 int pkg_run_script(pkg_t
* pkg
, const char *script
, const char *args
)
1255 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1256 maintainer script within a chroot environment. */
1257 if (conf
->offline_root
&& !conf
->force_postinstall
) {
1258 opkg_msg(INFO
, "Offline root mode: not running %s.%s.\n",
1263 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1264 have scripts in pkg->tmp_unpack_dir. */
1265 if (pkg
->state_status
== SS_INSTALLED
1266 || pkg
->state_status
== SS_UNPACKED
) {
1267 if (pkg
->dest
== NULL
) {
1268 opkg_msg(ERROR
, "Internal error: %s has a NULL dest.\n",
1272 sprintf_alloc(&path
, "%s/%s.%s", pkg
->dest
->info_dir
, pkg
->name
,
1275 if (pkg
->tmp_unpack_dir
== NULL
) {
1277 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1281 sprintf_alloc(&path
, "%s/%s", pkg
->tmp_unpack_dir
, script
);
1284 opkg_msg(INFO
, "Running script %s.\n", path
);
1287 pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
,
1290 if (pkg
->is_upgrade
)
1291 setenv("PKG_UPGRADE", "1", 1);
1293 setenv("PKG_UPGRADE", "0", 1);
1295 if (!file_exists(path
)) {
1300 sprintf_alloc(&cmd
, "%s %s", path
, args
);
1303 const char *argv
[] = { "sh", "-c", cmd
, NULL
};
1304 err
= xsystem(argv
);
1310 "package \"%s\" %s script returned status %d.\n",
1311 pkg
->name
, script
, err
);
1318 int pkg_arch_supported(pkg_t
* pkg
)
1320 nv_pair_list_elt_t
*l
;
1322 if (!pkg
->architecture
)
1325 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
1326 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
1327 if (strcmp(nv
->name
, pkg
->architecture
) == 0) {
1329 "Arch %s (priority %s) supported for pkg %s.\n",
1330 nv
->name
, nv
->value
, pkg
->name
);
1335 opkg_msg(DEBUG
, "Arch %s unsupported for pkg %s.\n",
1336 pkg
->architecture
, pkg
->name
);
1340 void pkg_info_preinstall_check(void)
1343 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1345 /* update the file owner data structure */
1346 opkg_msg(INFO
, "Updating file owner list.\n");
1347 pkg_hash_fetch_all_installed(installed_pkgs
);
1348 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1349 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1350 str_list_t
*installed_files
= pkg_get_installed_files(pkg
); /* this causes installed_files to be cached */
1351 str_list_elt_t
*iter
, *niter
;
1352 if (installed_files
== NULL
) {
1353 opkg_msg(ERROR
, "Failed to determine installed "
1354 "files for pkg %s.\n", pkg
->name
);
1357 for (iter
= str_list_first(installed_files
), niter
=
1358 str_list_next(installed_files
, iter
); iter
;
1359 iter
= niter
, niter
=
1360 str_list_next(installed_files
, iter
)) {
1361 char *installed_file
= (char *)iter
->data
;
1362 file_hash_set_file_owner(installed_file
, pkg
);
1364 pkg_free_installed_files(pkg
);
1366 pkg_vec_free(installed_pkgs
);
1369 struct pkg_write_filelist_data
{
1375 pkg_write_filelist_helper(const char *key
, void *entry_
, void *data_
)
1377 struct pkg_write_filelist_data
*data
= data_
;
1378 pkg_t
*entry
= entry_
;
1379 if (entry
== data
->pkg
) {
1380 fprintf(data
->stream
, "%s\n", key
);
1384 int pkg_write_filelist(pkg_t
* pkg
)
1386 struct pkg_write_filelist_data data
;
1387 char *list_file_name
;
1389 sprintf_alloc(&list_file_name
, "%s/%s.list",
1390 pkg
->dest
->info_dir
, pkg
->name
);
1392 opkg_msg(INFO
, "Creating %s file for pkg %s.\n",
1393 list_file_name
, pkg
->name
);
1395 data
.stream
= fopen(list_file_name
, "w");
1397 opkg_perror(ERROR
, "Failed to open %s", list_file_name
);
1398 free(list_file_name
);
1403 hash_table_foreach(&conf
->file_hash
, pkg_write_filelist_helper
, &data
);
1404 fclose(data
.stream
);
1405 free(list_file_name
);
1407 pkg
->state_flag
&= ~SF_FILELIST_CHANGED
;
1412 int pkg_write_changed_filelists(void)
1414 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1415 int i
, err
, ret
= 0;
1420 opkg_msg(INFO
, "Saving changed filelists.\n");
1422 pkg_hash_fetch_all_installed(installed_pkgs
);
1423 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1424 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1425 if (pkg
->state_flag
& SF_FILELIST_CHANGED
) {
1426 err
= pkg_write_filelist(pkg
);
1432 pkg_vec_free(installed_pkgs
);