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.
27 #include "pkg_parse.h"
28 #include "pkg_extract.h"
29 #include "opkg_message.h"
30 #include "opkg_utils.h"
32 #include "libbb/libbb.h"
33 #include "sprintf_alloc.h"
34 #include "file_util.h"
36 #include "opkg_conf.h"
38 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" }
83 pkg
->architecture
= NULL
;
84 pkg
->maintainer
= NULL
;
86 pkg
->description
= NULL
;
87 pkg
->state_want
= SW_UNKNOWN
;
88 pkg
->state_flag
= SF_OK
;
89 pkg
->state_status
= SS_NOT_INSTALLED
;
90 pkg
->depends_str
= NULL
;
91 pkg
->provides_str
= NULL
;
92 pkg
->depends_count
= 0;
94 pkg
->suggests_str
= NULL
;
95 pkg
->recommends_str
= NULL
;
96 pkg
->suggests_count
= 0;
97 pkg
->recommends_count
= 0;
99 active_list_init(&pkg
->list
);
101 pkg
->conflicts
= NULL
;
102 pkg
->conflicts_count
= 0;
104 pkg
->replaces
= NULL
;
105 pkg
->replaces_count
= 0;
107 pkg
->pre_depends_count
= 0;
108 pkg
->pre_depends_str
= NULL
;
109 pkg
->provides_count
= 0;
110 pkg
->provides
= NULL
;
111 pkg
->filename
= NULL
;
112 pkg
->local_filename
= NULL
;
113 pkg
->tmp_unpack_dir
= NULL
;
115 #if defined HAVE_SHA256
116 pkg
->sha256sum
= NULL
;
119 pkg
->installed_size
= 0;
120 pkg
->priority
= NULL
;
122 conffile_list_init(&pkg
->conffiles
);
123 pkg
->installed_files
= NULL
;
124 pkg
->installed_files_ref_cnt
= 0;
126 pkg
->provided_by_hand
= 0;
134 pkg
= xcalloc(1, sizeof(pkg_t
));
141 compound_depend_deinit(compound_depend_t
*depends
)
144 for (i
= 0; i
< depends
->possibility_count
; i
++)
147 d
= depends
->possibilities
[i
];
151 free (depends
->possibilities
);
155 pkg_deinit(pkg_t
*pkg
)
168 /* revision shares storage with version, so don't free */
169 pkg
->revision
= NULL
;
171 /* owned by opkg_conf_t */
173 /* owned by opkg_conf_t */
176 if (pkg
->architecture
)
177 free(pkg
->architecture
);
178 pkg
->architecture
= NULL
;
181 free(pkg
->maintainer
);
182 pkg
->maintainer
= NULL
;
188 if (pkg
->description
)
189 free(pkg
->description
);
190 pkg
->description
= NULL
;
192 pkg
->state_want
= SW_UNKNOWN
;
193 pkg
->state_flag
= SF_OK
;
194 pkg
->state_status
= SS_NOT_INSTALLED
;
196 active_list_clear(&pkg
->list
);
199 free (pkg
->replaces
);
200 pkg
->replaces
= NULL
;
203 int count
= pkg
->pre_depends_count
205 + pkg
->recommends_count
206 + pkg
->suggests_count
;
208 for (i
=0; i
<count
; i
++)
209 compound_depend_deinit (&pkg
->depends
[i
]);
213 if (pkg
->conflicts
) {
214 for (i
=0; i
<pkg
->conflicts_count
; i
++)
215 compound_depend_deinit (&pkg
->conflicts
[i
]);
216 free (pkg
->conflicts
);
220 free (pkg
->provides
);
222 pkg
->pre_depends_count
= 0;
223 pkg
->provides_count
= 0;
227 pkg
->filename
= NULL
;
229 if (pkg
->local_filename
)
230 free(pkg
->local_filename
);
231 pkg
->local_filename
= NULL
;
233 /* CLEANUP: It'd be nice to pullin the cleanup function from
234 opkg_install.c here. See comment in
235 opkg_install.c:cleanup_temporary_files */
236 if (pkg
->tmp_unpack_dir
)
237 free(pkg
->tmp_unpack_dir
);
238 pkg
->tmp_unpack_dir
= NULL
;
244 #if defined HAVE_SHA256
246 free(pkg
->sha256sum
);
247 pkg
->sha256sum
= NULL
;
252 pkg
->priority
= NULL
;
258 conffile_list_deinit(&pkg
->conffiles
);
260 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
261 since if they are calling deinit, they should know. Maybe do an
262 assertion here instead? */
263 pkg
->installed_files_ref_cnt
= 1;
264 pkg_free_installed_files(pkg
);
273 pkg_init_from_file(pkg_t
*pkg
, const char *filename
)
277 char *control_path
, *tmp
;
281 pkg
->local_filename
= xstrdup(filename
);
283 tmp
= xstrdup(filename
);
284 sprintf_alloc(&control_path
, "%s/%s.control.XXXXXX",
288 fd
= mkstemp(control_path
);
290 opkg_perror(ERROR
, "Failed to make temp file %s", control_path
);
295 control_file
= fdopen(fd
, "r+");
296 if (control_file
== NULL
) {
297 opkg_perror(ERROR
, "Failed to fdopen %s", control_path
);
303 err
= pkg_extract_control_file_to_stream(pkg
, control_file
);
305 opkg_msg(ERROR
, "Failed to extract control file from %s.\n",
310 rewind(control_file
);
312 if ((err
= pkg_parse_from_stream(pkg
, control_file
, 0))) {
314 opkg_msg(ERROR
, "Malformed package file %s.\n",
321 fclose(control_file
);
323 unlink(control_path
);
330 /* Merge any new information in newpkg into oldpkg */
332 pkg_merge(pkg_t
*oldpkg
, pkg_t
*newpkg
)
334 if (oldpkg
== newpkg
) {
338 if (!oldpkg
->auto_installed
)
339 oldpkg
->auto_installed
= newpkg
->auto_installed
;
342 oldpkg
->src
= newpkg
->src
;
344 oldpkg
->dest
= newpkg
->dest
;
345 if (!oldpkg
->architecture
)
346 oldpkg
->architecture
= xstrdup(newpkg
->architecture
);
347 if (!oldpkg
->arch_priority
)
348 oldpkg
->arch_priority
= newpkg
->arch_priority
;
349 if (!oldpkg
->section
)
350 oldpkg
->section
= xstrdup(newpkg
->section
);
351 if(!oldpkg
->maintainer
)
352 oldpkg
->maintainer
= xstrdup(newpkg
->maintainer
);
353 if(!oldpkg
->description
)
354 oldpkg
->description
= xstrdup(newpkg
->description
);
356 if (!oldpkg
->depends_count
&& !oldpkg
->pre_depends_count
&& !oldpkg
->recommends_count
&& !oldpkg
->suggests_count
) {
357 oldpkg
->depends_count
= newpkg
->depends_count
;
358 newpkg
->depends_count
= 0;
360 oldpkg
->depends
= newpkg
->depends
;
361 newpkg
->depends
= NULL
;
363 oldpkg
->pre_depends_count
= newpkg
->pre_depends_count
;
364 newpkg
->pre_depends_count
= 0;
366 oldpkg
->recommends_count
= newpkg
->recommends_count
;
367 newpkg
->recommends_count
= 0;
369 oldpkg
->suggests_count
= newpkg
->suggests_count
;
370 newpkg
->suggests_count
= 0;
373 if (oldpkg
->provides_count
<= 1) {
374 oldpkg
->provides_count
= newpkg
->provides_count
;
375 newpkg
->provides_count
= 0;
377 if (!oldpkg
->provides
) {
378 oldpkg
->provides
= newpkg
->provides
;
379 newpkg
->provides
= NULL
;
383 if (!oldpkg
->conflicts_count
) {
384 oldpkg
->conflicts_count
= newpkg
->conflicts_count
;
385 newpkg
->conflicts_count
= 0;
387 oldpkg
->conflicts
= newpkg
->conflicts
;
388 newpkg
->conflicts
= NULL
;
391 if (!oldpkg
->replaces_count
) {
392 oldpkg
->replaces_count
= newpkg
->replaces_count
;
393 newpkg
->replaces_count
= 0;
395 oldpkg
->replaces
= newpkg
->replaces
;
396 newpkg
->replaces
= NULL
;
399 if (!oldpkg
->filename
)
400 oldpkg
->filename
= xstrdup(newpkg
->filename
);
401 if (!oldpkg
->local_filename
)
402 oldpkg
->local_filename
= xstrdup(newpkg
->local_filename
);
403 if (!oldpkg
->tmp_unpack_dir
)
404 oldpkg
->tmp_unpack_dir
= xstrdup(newpkg
->tmp_unpack_dir
);
406 oldpkg
->md5sum
= xstrdup(newpkg
->md5sum
);
407 #if defined HAVE_SHA256
408 if (!oldpkg
->sha256sum
)
409 oldpkg
->sha256sum
= xstrdup(newpkg
->sha256sum
);
412 oldpkg
->size
= newpkg
->size
;
413 if (!oldpkg
->installed_size
)
414 oldpkg
->installed_size
= newpkg
->installed_size
;
415 if (!oldpkg
->priority
)
416 oldpkg
->priority
= xstrdup(newpkg
->priority
);
418 oldpkg
->source
= xstrdup(newpkg
->source
);
420 if (nv_pair_list_empty(&oldpkg
->conffiles
)){
421 list_splice_init(&newpkg
->conffiles
.head
, &oldpkg
->conffiles
.head
);
424 if (!oldpkg
->installed_files
){
425 oldpkg
->installed_files
= newpkg
->installed_files
;
426 oldpkg
->installed_files_ref_cnt
= newpkg
->installed_files_ref_cnt
;
427 newpkg
->installed_files
= NULL
;
430 if (!oldpkg
->essential
)
431 oldpkg
->essential
= newpkg
->essential
;
437 abstract_pkg_init(abstract_pkg_t
*ab_pkg
)
439 ab_pkg
->provided_by
= abstract_pkg_vec_alloc();
440 ab_pkg
->dependencies_checked
= 0;
441 ab_pkg
->state_status
= SS_NOT_INSTALLED
;
445 abstract_pkg_new(void)
447 abstract_pkg_t
* ab_pkg
;
449 ab_pkg
= xcalloc(1, sizeof(abstract_pkg_t
));
450 abstract_pkg_init(ab_pkg
);
456 set_flags_from_control(pkg_t
*pkg
){
460 sprintf_alloc(&file_name
,"%s/%s.control", pkg
->dest
->info_dir
, pkg
->name
);
462 fp
= fopen(file_name
, "r");
464 opkg_perror(ERROR
, "Failed to open %s");
471 if (pkg_parse_from_stream(pkg
, fp
, PFM_ALL
^ PFM_ESSENTIAL
)) {
472 opkg_msg(DEBUG
, "Unable to read control file for %s. May be empty.\n",
482 pkg_state_want_to_str(pkg_state_want_t sw
)
486 for (i
=0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
487 if (pkg_state_want_map
[i
].value
== sw
) {
488 return pkg_state_want_map
[i
].str
;
492 opkg_msg(ERROR
, "Internal error: state_want=%d\n", sw
);
493 return "<STATE_WANT_UNKNOWN>";
497 pkg_state_want_from_str(char *str
)
501 for (i
=0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
502 if (strcmp(str
, pkg_state_want_map
[i
].str
) == 0) {
503 return pkg_state_want_map
[i
].value
;
507 opkg_msg(ERROR
, "Internal error: state_want=%s\n", str
);
512 pkg_state_flag_to_str(pkg_state_flag_t sf
)
518 /* clear the temporary flags before converting to string */
519 sf
&= SF_NONVOLATILE_FLAGS
;
522 return xstrdup("ok");
525 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
526 if (sf
& pkg_state_flag_map
[i
].value
)
527 len
+= strlen(pkg_state_flag_map
[i
].str
) + 1;
530 str
= xmalloc(len
+1);
533 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
534 if (sf
& pkg_state_flag_map
[i
].value
) {
535 strncat(str
, pkg_state_flag_map
[i
].str
, len
);
536 strncat(str
, ",", len
);
541 str
[len
-1] = '\0'; /* squash last comma */
547 pkg_state_flag_from_str(const char *str
)
552 unsigned int sfname_len
;
554 if (strcmp(str
, "ok") == 0) {
557 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
558 sfname
= pkg_state_flag_map
[i
].str
;
559 sfname_len
= strlen(sfname
);
560 if (strncmp(str
, sfname
, sfname_len
) == 0) {
561 sf
|= pkg_state_flag_map
[i
].value
;
575 pkg_state_status_to_str(pkg_state_status_t ss
)
579 for (i
=0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
580 if (pkg_state_status_map
[i
].value
== ss
) {
581 return pkg_state_status_map
[i
].str
;
585 opkg_msg(ERROR
, "Internal error: state_status=%d\n", ss
);
586 return "<STATE_STATUS_UNKNOWN>";
590 pkg_state_status_from_str(const char *str
)
594 for (i
=0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
595 if (strcmp(str
, pkg_state_status_map
[i
].str
) == 0) {
596 return pkg_state_status_map
[i
].value
;
600 opkg_msg(ERROR
, "Internal error: state_status=%s\n", str
);
601 return SS_NOT_INSTALLED
;
605 pkg_formatted_field(FILE *fp
, pkg_t
*pkg
, const char *field
)
609 int depends_count
= pkg
->pre_depends_count
+
611 pkg
->recommends_count
+
614 if (strlen(field
) < PKG_MINIMUM_FIELD_NAME_LEN
) {
615 goto UNKNOWN_FMT_FIELD
;
622 if (strcasecmp(field
, "Architecture") == 0) {
623 if (pkg
->architecture
) {
624 fprintf(fp
, "Architecture: %s\n", pkg
->architecture
);
626 } else if (strcasecmp(field
, "Auto-Installed") == 0) {
627 if (pkg
->auto_installed
)
628 fprintf(fp
, "Auto-Installed: yes\n");
630 goto UNKNOWN_FMT_FIELD
;
635 if (strcasecmp(field
, "Conffiles") == 0) {
636 conffile_list_elt_t
*iter
;
638 if (nv_pair_list_empty(&pkg
->conffiles
))
641 fprintf(fp
, "Conffiles:\n");
642 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
643 if (((conffile_t
*)iter
->data
)->name
&& ((conffile_t
*)iter
->data
)->value
) {
644 fprintf(fp
, " %s %s\n",
645 ((conffile_t
*)iter
->data
)->name
,
646 ((conffile_t
*)iter
->data
)->value
);
649 } else if (strcasecmp(field
, "Conflicts") == 0) {
651 if (pkg
->conflicts_count
) {
652 fprintf(fp
, "Conflicts:");
653 for(i
= 0; i
< pkg
->conflicts_count
; i
++) {
654 cdep
= pkg
->conflicts
[i
].possibilities
[0];
655 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
658 fprintf(fp
, " (%s%s)",
659 constraint_to_str(cdep
->constraint
),
666 goto UNKNOWN_FMT_FIELD
;
671 if (strcasecmp(field
, "Depends") == 0) {
672 if (pkg
->depends_count
) {
673 fprintf(fp
, "Depends:");
674 for (j
=0, i
=0; i
<depends_count
; i
++) {
675 if (pkg
->depends
[i
].type
!= DEPEND
)
677 str
= pkg_depend_str(pkg
, i
);
678 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
684 } else if (strcasecmp(field
, "Description") == 0) {
685 if (pkg
->description
) {
686 fprintf(fp
, "Description: %s\n", pkg
->description
);
689 goto UNKNOWN_FMT_FIELD
;
694 if (pkg
->essential
) {
695 fprintf(fp
, "Essential: yes\n");
701 fprintf(fp
, "Filename: %s\n", pkg
->filename
);
706 if (strcasecmp(field
, "Installed-Size") == 0) {
707 fprintf(fp
, "Installed-Size: %ld\n", pkg
->installed_size
);
708 } else if (strcasecmp(field
, "Installed-Time") == 0 && pkg
->installed_time
) {
709 fprintf(fp
, "Installed-Time: %lu\n", pkg
->installed_time
);
714 if (strcasecmp(field
, "Maintainer") == 0) {
715 if (pkg
->maintainer
) {
716 fprintf(fp
, "maintainer: %s\n", pkg
->maintainer
);
718 } else if (strcasecmp(field
, "MD5sum") == 0) {
720 fprintf(fp
, "MD5Sum: %s\n", pkg
->md5sum
);
723 goto UNKNOWN_FMT_FIELD
;
728 if (strcasecmp(field
, "Package") == 0) {
729 fprintf(fp
, "Package: %s\n", pkg
->name
);
730 } else if (strcasecmp(field
, "Priority") == 0) {
731 fprintf(fp
, "Priority: %s\n", pkg
->priority
);
732 } else if (strcasecmp(field
, "Provides") == 0) {
733 if (pkg
->provides_count
) {
734 fprintf(fp
, "Provides:");
735 for(i
= 1; i
< pkg
->provides_count
; i
++) {
736 fprintf(fp
, "%s %s", i
== 1 ? "" : ",",
737 pkg
->provides
[i
]->name
);
742 goto UNKNOWN_FMT_FIELD
;
747 if (strcasecmp (field
, "Replaces") == 0) {
748 if (pkg
->replaces_count
) {
749 fprintf(fp
, "Replaces:");
750 for (i
= 0; i
< pkg
->replaces_count
; i
++) {
751 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
752 pkg
->replaces
[i
]->name
);
756 } else if (strcasecmp (field
, "Recommends") == 0) {
757 if (pkg
->recommends_count
) {
758 fprintf(fp
, "Recommends:");
759 for (j
=0, i
=0; i
<depends_count
; i
++) {
760 if (pkg
->depends
[i
].type
!= RECOMMEND
)
762 str
= pkg_depend_str(pkg
, i
);
763 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
770 goto UNKNOWN_FMT_FIELD
;
775 if (strcasecmp(field
, "Section") == 0) {
777 fprintf(fp
, "Section: %s\n", pkg
->section
);
779 #if defined HAVE_SHA256
780 } else if (strcasecmp(field
, "SHA256sum") == 0) {
781 if (pkg
->sha256sum
) {
782 fprintf(fp
, "SHA256sum: %s\n", pkg
->sha256sum
);
785 } else if (strcasecmp(field
, "Size") == 0) {
787 fprintf(fp
, "Size: %ld\n", pkg
->size
);
789 } else if (strcasecmp(field
, "Source") == 0) {
791 fprintf(fp
, "Source: %s\n", pkg
->source
);
793 } else if (strcasecmp(field
, "Status") == 0) {
794 char *pflag
= pkg_state_flag_to_str(pkg
->state_flag
);
795 fprintf(fp
, "Status: %s %s %s\n",
796 pkg_state_want_to_str(pkg
->state_want
),
798 pkg_state_status_to_str(pkg
->state_status
));
800 } else if (strcasecmp(field
, "Suggests") == 0) {
801 if (pkg
->suggests_count
) {
802 fprintf(fp
, "Suggests:");
803 for (j
=0, i
=0; i
<depends_count
; i
++) {
804 if (pkg
->depends
[i
].type
!= SUGGEST
)
806 str
= pkg_depend_str(pkg
, i
);
807 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
814 goto UNKNOWN_FMT_FIELD
;
819 if (strcasecmp(field
, "Tags") == 0) {
821 fprintf(fp
, "Tags: %s\n", pkg
->tags
);
828 char *version
= pkg_version_str_alloc(pkg
);
831 fprintf(fp
, "Version: %s\n", version
);
836 goto UNKNOWN_FMT_FIELD
;
842 opkg_msg(ERROR
, "Internal error: field=%s\n", field
);
846 pkg_formatted_info(FILE *fp
, pkg_t
*pkg
)
848 pkg_formatted_field(fp
, pkg
, "Package");
849 pkg_formatted_field(fp
, pkg
, "Version");
850 pkg_formatted_field(fp
, pkg
, "Depends");
851 pkg_formatted_field(fp
, pkg
, "Recommends");
852 pkg_formatted_field(fp
, pkg
, "Suggests");
853 pkg_formatted_field(fp
, pkg
, "Provides");
854 pkg_formatted_field(fp
, pkg
, "Replaces");
855 pkg_formatted_field(fp
, pkg
, "Conflicts");
856 pkg_formatted_field(fp
, pkg
, "Status");
857 pkg_formatted_field(fp
, pkg
, "Section");
858 pkg_formatted_field(fp
, pkg
, "Essential");
859 pkg_formatted_field(fp
, pkg
, "Architecture");
860 pkg_formatted_field(fp
, pkg
, "Maintainer");
861 pkg_formatted_field(fp
, pkg
, "MD5sum");
862 pkg_formatted_field(fp
, pkg
, "Size");
863 pkg_formatted_field(fp
, pkg
, "Filename");
864 pkg_formatted_field(fp
, pkg
, "Conffiles");
865 pkg_formatted_field(fp
, pkg
, "Source");
866 pkg_formatted_field(fp
, pkg
, "Description");
867 pkg_formatted_field(fp
, pkg
, "Installed-Time");
868 pkg_formatted_field(fp
, pkg
, "Tags");
873 pkg_print_status(pkg_t
* pkg
, FILE * file
)
879 pkg_formatted_field(file
, pkg
, "Package");
880 pkg_formatted_field(file
, pkg
, "Version");
881 pkg_formatted_field(file
, pkg
, "Depends");
882 pkg_formatted_field(file
, pkg
, "Recommends");
883 pkg_formatted_field(file
, pkg
, "Suggests");
884 pkg_formatted_field(file
, pkg
, "Provides");
885 pkg_formatted_field(file
, pkg
, "Replaces");
886 pkg_formatted_field(file
, pkg
, "Conflicts");
887 pkg_formatted_field(file
, pkg
, "Status");
888 pkg_formatted_field(file
, pkg
, "Essential");
889 pkg_formatted_field(file
, pkg
, "Architecture");
890 pkg_formatted_field(file
, pkg
, "Conffiles");
891 pkg_formatted_field(file
, pkg
, "Installed-Time");
892 pkg_formatted_field(file
, pkg
, "Auto-Installed");
897 * libdpkg - Debian packaging suite library routines
898 * vercmp.c - comparison of version numbers
900 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
903 /* assume ascii; warning: evaluates x multiple times! */
904 #define order(x) ((x) == '~' ? -1 \
907 : isalpha((x)) ? (x) \
911 verrevcmp(const char *val
, const char *ref
) {
915 while (*val
|| *ref
) {
918 while ( (*val
&& !isdigit(*val
)) || (*ref
&& !isdigit(*ref
)) ) {
919 int vc
= order(*val
), rc
= order(*ref
);
920 if (vc
!= rc
) return vc
- rc
;
924 while ( *val
== '0' ) val
++;
925 while ( *ref
== '0' ) ref
++;
926 while (isdigit(*val
) && isdigit(*ref
)) {
927 if (!first_diff
) first_diff
= *val
- *ref
;
930 if (isdigit(*val
)) return 1;
931 if (isdigit(*ref
)) return -1;
932 if (first_diff
) return first_diff
;
938 pkg_compare_versions(const pkg_t
*pkg
, const pkg_t
*ref_pkg
)
942 if (pkg
->epoch
> ref_pkg
->epoch
) {
946 if (pkg
->epoch
< ref_pkg
->epoch
) {
950 r
= verrevcmp(pkg
->version
, ref_pkg
->version
);
955 r
= verrevcmp(pkg
->revision
, ref_pkg
->revision
);
965 pkg_version_satisfied(pkg_t
*it
, pkg_t
*ref
, const char *op
)
969 r
= pkg_compare_versions(it
, ref
);
971 if (strcmp(op
, "<=") == 0 || strcmp(op
, "<") == 0) {
975 if (strcmp(op
, ">=") == 0 || strcmp(op
, ">") == 0) {
979 if (strcmp(op
, "<<") == 0) {
983 if (strcmp(op
, ">>") == 0) {
987 if (strcmp(op
, "=") == 0) {
991 opkg_msg(ERROR
, "Unknown operator: %s.\n", op
);
996 pkg_name_version_and_architecture_compare(const void *p1
, const void *p2
)
998 const pkg_t
*a
= *(const pkg_t
**) p1
;
999 const pkg_t
*b
= *(const pkg_t
**) p2
;
1002 if (!a
->name
|| !b
->name
) {
1003 opkg_msg(ERROR
, "Internal error: a->name=%p, b->name=%p.\n",
1008 namecmp
= strcmp(a
->name
, b
->name
);
1011 vercmp
= pkg_compare_versions(a
, b
);
1014 if (!a
->arch_priority
|| !b
->arch_priority
) {
1015 opkg_msg(ERROR
, "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1016 a
->arch_priority
, b
->arch_priority
);
1019 if (a
->arch_priority
> b
->arch_priority
)
1021 if (a
->arch_priority
< b
->arch_priority
)
1027 abstract_pkg_name_compare(const void *p1
, const void *p2
)
1029 const abstract_pkg_t
*a
= *(const abstract_pkg_t
**)p1
;
1030 const abstract_pkg_t
*b
= *(const abstract_pkg_t
**)p2
;
1031 if (!a
->name
|| !b
->name
) {
1032 opkg_msg(ERROR
, "Internal error: a->name=%p b->name=%p.\n",
1036 return strcmp(a
->name
, b
->name
);
1041 pkg_version_str_alloc(pkg_t
*pkg
)
1047 sprintf_alloc(&version
, "%d:%s-%s",
1048 pkg
->epoch
, pkg
->version
, pkg
->revision
);
1050 sprintf_alloc(&version
, "%d:%s",
1051 pkg
->epoch
, pkg
->version
);
1054 sprintf_alloc(&version
, "%s-%s",
1055 pkg
->version
, pkg
->revision
);
1057 version
= xstrdup(pkg
->version
);
1064 * XXX: this should be broken into two functions
1067 pkg_get_installed_files(pkg_t
*pkg
)
1070 char *list_file_name
= NULL
;
1071 FILE *list_file
= NULL
;
1073 char *installed_file_name
;
1074 unsigned int rootdirlen
= 0;
1075 int list_from_package
;
1077 pkg
->installed_files_ref_cnt
++;
1079 if (pkg
->installed_files
) {
1080 return pkg
->installed_files
;
1083 pkg
->installed_files
= str_list_alloc();
1086 * For installed packages, look at the package.list file in the database.
1087 * For uninstalled packages, get the file list directly from the package.
1089 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
)
1090 list_from_package
= 1;
1092 list_from_package
= 0;
1094 if (list_from_package
) {
1095 if (pkg
->local_filename
== NULL
) {
1096 return pkg
->installed_files
;
1098 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1099 file. In other words, change deb_extract so that it can
1100 simply return the file list as a char *[] rather than
1101 insisting on writing it to a FILE * as it does now. */
1102 sprintf_alloc(&list_file_name
, "%s/%s.list.XXXXXX",
1103 conf
->tmp_dir
, pkg
->name
);
1104 fd
= mkstemp(list_file_name
);
1106 opkg_perror(ERROR
, "Failed to make temp file %s.",
1108 free(list_file_name
);
1109 return pkg
->installed_files
;
1111 list_file
= fdopen(fd
, "r+");
1112 if (list_file
== NULL
) {
1113 opkg_perror(ERROR
, "Failed to fdopen temp file %s.",
1116 unlink(list_file_name
);
1117 free(list_file_name
);
1118 return pkg
->installed_files
;
1120 err
= pkg_extract_data_file_names_to_stream(pkg
, list_file
);
1122 opkg_msg(ERROR
, "Error extracting file list from %s.\n",
1123 pkg
->local_filename
);
1125 unlink(list_file_name
);
1126 free(list_file_name
);
1127 str_list_deinit(pkg
->installed_files
);
1128 pkg
->installed_files
= NULL
;
1133 sprintf_alloc(&list_file_name
, "%s/%s.list",
1134 pkg
->dest
->info_dir
, pkg
->name
);
1135 list_file
= fopen(list_file_name
, "r");
1136 if (list_file
== NULL
) {
1137 opkg_perror(ERROR
, "Failed to open %s",
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
, rootdirlen
)) {
1169 sprintf_alloc(&installed_file_name
, "%s%s",
1170 conf
->offline_root
, file_name
);
1172 // already contains root_dir as header -> ABSOLUTE
1173 sprintf_alloc(&installed_file_name
, "%s", file_name
);
1176 str_list_append(pkg
->installed_files
, installed_file_name
);
1177 free(installed_file_name
);
1183 if (list_from_package
) {
1184 unlink(list_file_name
);
1185 free(list_file_name
);
1188 return pkg
->installed_files
;
1191 /* XXX: CLEANUP: This function and it's counterpart,
1192 (pkg_get_installed_files), do not match our init/deinit naming
1193 convention. Nor the alloc/free convention. But, then again, neither
1194 of these conventions currrently fit the way these two functions
1197 pkg_free_installed_files(pkg_t
*pkg
)
1199 pkg
->installed_files_ref_cnt
--;
1201 if (pkg
->installed_files_ref_cnt
> 0)
1204 if (pkg
->installed_files
) {
1205 str_list_purge(pkg
->installed_files
);
1208 pkg
->installed_files
= NULL
;
1212 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
);
1226 pkg_get_conffile(pkg_t
*pkg
, const char *file_name
)
1228 conffile_list_elt_t
*iter
;
1229 conffile_t
*conffile
;
1235 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1236 conffile
= (conffile_t
*)iter
->data
;
1238 if (strcmp(conffile
->name
, file_name
) == 0) {
1247 pkg_run_script(pkg_t
*pkg
, const char *script
, const char *args
)
1256 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1257 maintainer script within a chroot environment. */
1258 if (conf
->offline_root
&& !conf
->force_postinstall
) {
1259 opkg_msg(INFO
, "Offline root mode: not running %s.%s.\n",
1264 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1265 have scripts in pkg->tmp_unpack_dir. */
1266 if (pkg
->state_status
== SS_INSTALLED
|| 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
, script
);
1274 if (pkg
->tmp_unpack_dir
== NULL
) {
1275 opkg_msg(ERROR
, "Internal error: %s has a NULL tmp_unpack_dir.\n",
1279 sprintf_alloc(&path
, "%s/%s", pkg
->tmp_unpack_dir
, script
);
1282 opkg_msg(INFO
, "Running script %s.\n", path
);
1285 pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
, 1);
1287 if (! file_exists(path
)) {
1292 sprintf_alloc(&cmd
, "%s %s", path
, args
);
1295 const char *argv
[] = {"sh", "-c", cmd
, NULL
};
1296 err
= xsystem(argv
);
1301 opkg_msg(ERROR
, "%s script returned status %d.\n", script
, err
);
1309 pkg_arch_supported(pkg_t
*pkg
)
1311 nv_pair_list_elt_t
*l
;
1313 if (!pkg
->architecture
)
1316 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
1317 nv_pair_t
*nv
= (nv_pair_t
*)l
->data
;
1318 if (strcmp(nv
->name
, pkg
->architecture
) == 0) {
1319 opkg_msg(DEBUG
, "Arch %s (priority %s) supported for pkg %s.\n",
1320 nv
->name
, nv
->value
, pkg
->name
);
1325 opkg_msg(DEBUG
, "Arch %s unsupported for pkg %s.\n",
1326 pkg
->architecture
, pkg
->name
);
1331 pkg_info_preinstall_check(void)
1334 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1336 /* update the file owner data structure */
1337 opkg_msg(INFO
, "Updating file owner list.\n");
1338 pkg_hash_fetch_all_installed(installed_pkgs
);
1339 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1340 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1341 str_list_t
*installed_files
= pkg_get_installed_files(pkg
); /* this causes installed_files to be cached */
1342 str_list_elt_t
*iter
, *niter
;
1343 if (installed_files
== NULL
) {
1344 opkg_msg(ERROR
, "Failed to determine installed "
1345 "files for pkg %s.\n", pkg
->name
);
1348 for (iter
= str_list_first(installed_files
), niter
= str_list_next(installed_files
, iter
);
1350 iter
= niter
, niter
= str_list_next(installed_files
, iter
)) {
1351 char *installed_file
= (char *) iter
->data
;
1352 file_hash_set_file_owner(installed_file
, pkg
);
1354 pkg_free_installed_files(pkg
);
1356 pkg_vec_free(installed_pkgs
);
1359 struct pkg_write_filelist_data
{
1365 pkg_write_filelist_helper(const char *key
, void *entry_
, void *data_
)
1367 struct pkg_write_filelist_data
*data
= data_
;
1368 pkg_t
*entry
= entry_
;
1369 if (entry
== data
->pkg
) {
1370 fprintf(data
->stream
, "%s\n", key
);
1375 pkg_write_filelist(pkg_t
*pkg
)
1377 struct pkg_write_filelist_data data
;
1378 char *list_file_name
;
1380 sprintf_alloc(&list_file_name
, "%s/%s.list",
1381 pkg
->dest
->info_dir
, pkg
->name
);
1383 opkg_msg(INFO
, "Creating %s file for pkg %s.\n",
1384 list_file_name
, pkg
->name
);
1386 data
.stream
= fopen(list_file_name
, "w");
1388 opkg_perror(ERROR
, "Failed to open %s",
1390 free(list_file_name
);
1395 hash_table_foreach(&conf
->file_hash
, pkg_write_filelist_helper
, &data
);
1396 fclose(data
.stream
);
1397 free(list_file_name
);
1399 pkg
->state_flag
&= ~SF_FILELIST_CHANGED
;
1405 pkg_write_changed_filelists(void)
1407 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1408 int i
, err
, ret
= 0;
1413 opkg_msg(INFO
, "Saving changed filelists.\n");
1415 pkg_hash_fetch_all_installed(installed_pkgs
);
1416 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1417 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1418 if (pkg
->state_flag
& SF_FILELIST_CHANGED
) {
1419 err
= pkg_write_filelist(pkg
);
1425 pkg_vec_free (installed_pkgs
);