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
;
46 static const enum_map_t pkg_state_want_map
[] = {
47 { SW_UNKNOWN
, "unknown"},
48 { SW_INSTALL
, "install"},
49 { SW_DEINSTALL
, "deinstall"},
53 static const enum_map_t pkg_state_flag_map
[] = {
55 { SF_REINSTREQ
, "reinstreq"},
57 { SF_REPLACE
, "replace"},
58 { SF_NOPRUNE
, "noprune"},
59 { SF_PREFER
, "prefer"},
60 { SF_OBSOLETE
, "obsolete"},
64 static const enum_map_t pkg_state_status_map
[] = {
65 { SS_NOT_INSTALLED
, "not-installed" },
66 { SS_UNPACKED
, "unpacked" },
67 { SS_HALF_CONFIGURED
, "half-configured" },
68 { SS_INSTALLED
, "installed" },
69 { SS_HALF_INSTALLED
, "half-installed" },
70 { SS_CONFIG_FILES
, "config-files" },
71 { SS_POST_INST_FAILED
, "post-inst-failed" },
72 { SS_REMOVAL_FAILED
, "removal-failed" }
84 pkg
->architecture
= NULL
;
85 pkg
->maintainer
= NULL
;
87 pkg
->description
= NULL
;
88 pkg
->state_want
= SW_UNKNOWN
;
89 pkg
->state_flag
= SF_OK
;
90 pkg
->state_status
= SS_NOT_INSTALLED
;
91 pkg
->depends_str
= NULL
;
92 pkg
->provides_str
= NULL
;
93 pkg
->depends_count
= 0;
95 pkg
->suggests_str
= NULL
;
96 pkg
->recommends_str
= NULL
;
97 pkg
->suggests_count
= 0;
98 pkg
->recommends_count
= 0;
100 active_list_init(&pkg
->list
);
102 pkg
->conflicts
= NULL
;
103 pkg
->conflicts_count
= 0;
105 pkg
->replaces
= NULL
;
106 pkg
->replaces_count
= 0;
108 pkg
->pre_depends_count
= 0;
109 pkg
->pre_depends_str
= NULL
;
110 pkg
->provides_count
= 0;
111 pkg
->provides
= NULL
;
112 pkg
->filename
= NULL
;
113 pkg
->local_filename
= NULL
;
114 pkg
->tmp_unpack_dir
= NULL
;
116 #if defined HAVE_SHA256
117 pkg
->sha256sum
= NULL
;
120 pkg
->installed_size
= 0;
121 pkg
->priority
= NULL
;
123 conffile_list_init(&pkg
->conffiles
);
124 pkg
->installed_files
= NULL
;
125 pkg
->installed_files_ref_cnt
= 0;
127 pkg
->provided_by_hand
= 0;
135 pkg
= xcalloc(1, sizeof(pkg_t
));
142 compound_depend_deinit(compound_depend_t
*depends
)
145 for (i
= 0; i
< depends
->possibility_count
; i
++)
148 d
= depends
->possibilities
[i
];
152 free (depends
->possibilities
);
156 pkg_deinit(pkg_t
*pkg
)
169 /* revision shares storage with version, so don't free */
170 pkg
->revision
= NULL
;
172 /* owned by opkg_conf_t */
174 /* owned by opkg_conf_t */
177 if (pkg
->architecture
)
178 free(pkg
->architecture
);
179 pkg
->architecture
= NULL
;
182 free(pkg
->maintainer
);
183 pkg
->maintainer
= NULL
;
189 if (pkg
->description
)
190 free(pkg
->description
);
191 pkg
->description
= NULL
;
193 pkg
->state_want
= SW_UNKNOWN
;
194 pkg
->state_flag
= SF_OK
;
195 pkg
->state_status
= SS_NOT_INSTALLED
;
197 active_list_clear(&pkg
->list
);
200 free (pkg
->replaces
);
201 pkg
->replaces
= NULL
;
204 int count
= pkg
->pre_depends_count
206 + pkg
->recommends_count
207 + pkg
->suggests_count
;
209 for (i
=0; i
<count
; i
++)
210 compound_depend_deinit (&pkg
->depends
[i
]);
214 if (pkg
->conflicts
) {
215 for (i
=0; i
<pkg
->conflicts_count
; i
++)
216 compound_depend_deinit (&pkg
->conflicts
[i
]);
217 free (pkg
->conflicts
);
221 free (pkg
->provides
);
223 pkg
->pre_depends_count
= 0;
224 pkg
->provides_count
= 0;
228 pkg
->filename
= NULL
;
230 if (pkg
->local_filename
)
231 free(pkg
->local_filename
);
232 pkg
->local_filename
= NULL
;
234 /* CLEANUP: It'd be nice to pullin the cleanup function from
235 opkg_install.c here. See comment in
236 opkg_install.c:cleanup_temporary_files */
237 if (pkg
->tmp_unpack_dir
)
238 free(pkg
->tmp_unpack_dir
);
239 pkg
->tmp_unpack_dir
= NULL
;
245 #if defined HAVE_SHA256
247 free(pkg
->sha256sum
);
248 pkg
->sha256sum
= NULL
;
253 pkg
->priority
= NULL
;
259 conffile_list_deinit(&pkg
->conffiles
);
261 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
262 since if they are calling deinit, they should know. Maybe do an
263 assertion here instead? */
264 pkg
->installed_files_ref_cnt
= 1;
265 pkg_free_installed_files(pkg
);
274 pkg_init_from_file(pkg_t
*pkg
, const char *filename
)
278 char *control_path
, *tmp
;
282 pkg
->local_filename
= xstrdup(filename
);
284 tmp
= xstrdup(filename
);
285 sprintf_alloc(&control_path
, "%s/%s.control.XXXXXX",
289 fd
= mkstemp(control_path
);
291 opkg_perror(ERROR
, "Failed to make temp file %s", control_path
);
296 control_file
= fdopen(fd
, "r+");
297 if (control_file
== NULL
) {
298 opkg_perror(ERROR
, "Failed to fdopen %s", control_path
);
304 err
= pkg_extract_control_file_to_stream(pkg
, control_file
);
306 opkg_msg(ERROR
, "Failed to extract control file from %s.\n",
311 rewind(control_file
);
313 if ((err
= pkg_parse_from_stream(pkg
, control_file
, 0))) {
315 opkg_msg(ERROR
, "Malformed package file %s.\n",
322 fclose(control_file
);
324 unlink(control_path
);
331 /* Merge any new information in newpkg into oldpkg */
333 pkg_merge(pkg_t
*oldpkg
, pkg_t
*newpkg
)
335 if (oldpkg
== newpkg
) {
339 if (!oldpkg
->auto_installed
)
340 oldpkg
->auto_installed
= newpkg
->auto_installed
;
343 oldpkg
->src
= newpkg
->src
;
345 oldpkg
->dest
= newpkg
->dest
;
346 if (!oldpkg
->architecture
)
347 oldpkg
->architecture
= xstrdup(newpkg
->architecture
);
348 if (!oldpkg
->arch_priority
)
349 oldpkg
->arch_priority
= newpkg
->arch_priority
;
350 if (!oldpkg
->section
)
351 oldpkg
->section
= xstrdup(newpkg
->section
);
352 if(!oldpkg
->maintainer
)
353 oldpkg
->maintainer
= xstrdup(newpkg
->maintainer
);
354 if(!oldpkg
->description
)
355 oldpkg
->description
= xstrdup(newpkg
->description
);
357 if (!oldpkg
->depends_count
&& !oldpkg
->pre_depends_count
&& !oldpkg
->recommends_count
&& !oldpkg
->suggests_count
) {
358 oldpkg
->depends_count
= newpkg
->depends_count
;
359 newpkg
->depends_count
= 0;
361 oldpkg
->depends
= newpkg
->depends
;
362 newpkg
->depends
= NULL
;
364 oldpkg
->pre_depends_count
= newpkg
->pre_depends_count
;
365 newpkg
->pre_depends_count
= 0;
367 oldpkg
->recommends_count
= newpkg
->recommends_count
;
368 newpkg
->recommends_count
= 0;
370 oldpkg
->suggests_count
= newpkg
->suggests_count
;
371 newpkg
->suggests_count
= 0;
374 if (oldpkg
->provides_count
<= 1) {
375 oldpkg
->provides_count
= newpkg
->provides_count
;
376 newpkg
->provides_count
= 0;
378 if (!oldpkg
->provides
) {
379 oldpkg
->provides
= newpkg
->provides
;
380 newpkg
->provides
= NULL
;
384 if (!oldpkg
->conflicts_count
) {
385 oldpkg
->conflicts_count
= newpkg
->conflicts_count
;
386 newpkg
->conflicts_count
= 0;
388 oldpkg
->conflicts
= newpkg
->conflicts
;
389 newpkg
->conflicts
= NULL
;
392 if (!oldpkg
->replaces_count
) {
393 oldpkg
->replaces_count
= newpkg
->replaces_count
;
394 newpkg
->replaces_count
= 0;
396 oldpkg
->replaces
= newpkg
->replaces
;
397 newpkg
->replaces
= NULL
;
400 if (!oldpkg
->filename
)
401 oldpkg
->filename
= xstrdup(newpkg
->filename
);
402 if (!oldpkg
->local_filename
)
403 oldpkg
->local_filename
= xstrdup(newpkg
->local_filename
);
404 if (!oldpkg
->tmp_unpack_dir
)
405 oldpkg
->tmp_unpack_dir
= xstrdup(newpkg
->tmp_unpack_dir
);
407 oldpkg
->md5sum
= xstrdup(newpkg
->md5sum
);
408 #if defined HAVE_SHA256
409 if (!oldpkg
->sha256sum
)
410 oldpkg
->sha256sum
= xstrdup(newpkg
->sha256sum
);
413 oldpkg
->size
= newpkg
->size
;
414 if (!oldpkg
->installed_size
)
415 oldpkg
->installed_size
= newpkg
->installed_size
;
416 if (!oldpkg
->priority
)
417 oldpkg
->priority
= xstrdup(newpkg
->priority
);
419 oldpkg
->source
= xstrdup(newpkg
->source
);
421 if (nv_pair_list_empty(&oldpkg
->conffiles
)){
422 list_splice_init(&newpkg
->conffiles
.head
, &oldpkg
->conffiles
.head
);
425 if (!oldpkg
->installed_files
){
426 oldpkg
->installed_files
= newpkg
->installed_files
;
427 oldpkg
->installed_files_ref_cnt
= newpkg
->installed_files_ref_cnt
;
428 newpkg
->installed_files
= NULL
;
431 if (!oldpkg
->essential
)
432 oldpkg
->essential
= newpkg
->essential
;
438 abstract_pkg_init(abstract_pkg_t
*ab_pkg
)
440 ab_pkg
->provided_by
= abstract_pkg_vec_alloc();
441 ab_pkg
->dependencies_checked
= 0;
442 ab_pkg
->state_status
= SS_NOT_INSTALLED
;
446 abstract_pkg_new(void)
448 abstract_pkg_t
* ab_pkg
;
450 ab_pkg
= xcalloc(1, sizeof(abstract_pkg_t
));
451 abstract_pkg_init(ab_pkg
);
457 set_flags_from_control(pkg_t
*pkg
){
461 sprintf_alloc(&file_name
,"%s/%s.control", pkg
->dest
->info_dir
, pkg
->name
);
463 fp
= fopen(file_name
, "r");
465 opkg_perror(ERROR
, "Failed to open %s", file_name
);
472 if (pkg_parse_from_stream(pkg
, fp
, PFM_ALL
^ PFM_ESSENTIAL
)) {
473 opkg_msg(DEBUG
, "Unable to read control file for %s. May be empty.\n",
483 pkg_state_want_to_str(pkg_state_want_t sw
)
487 for (i
=0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
488 if (pkg_state_want_map
[i
].value
== sw
) {
489 return pkg_state_want_map
[i
].str
;
493 opkg_msg(ERROR
, "Internal error: state_want=%d\n", sw
);
494 return "<STATE_WANT_UNKNOWN>";
498 pkg_state_want_from_str(char *str
)
502 for (i
=0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
503 if (strcmp(str
, pkg_state_want_map
[i
].str
) == 0) {
504 return pkg_state_want_map
[i
].value
;
508 opkg_msg(ERROR
, "Internal error: state_want=%s\n", str
);
513 pkg_state_flag_to_str(pkg_state_flag_t sf
)
519 /* clear the temporary flags before converting to string */
520 sf
&= SF_NONVOLATILE_FLAGS
;
523 return xstrdup("ok");
526 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
527 if (sf
& pkg_state_flag_map
[i
].value
)
528 len
+= strlen(pkg_state_flag_map
[i
].str
) + 1;
531 str
= xmalloc(len
+1);
534 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
535 if (sf
& pkg_state_flag_map
[i
].value
) {
536 strncat(str
, pkg_state_flag_map
[i
].str
, len
);
537 strncat(str
, ",", len
);
542 str
[len
-1] = '\0'; /* squash last comma */
548 pkg_state_flag_from_str(const char *str
)
553 unsigned int sfname_len
;
555 if (strcmp(str
, "ok") == 0) {
558 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
559 sfname
= pkg_state_flag_map
[i
].str
;
560 sfname_len
= strlen(sfname
);
561 if (strncmp(str
, sfname
, sfname_len
) == 0) {
562 sf
|= pkg_state_flag_map
[i
].value
;
576 pkg_state_status_to_str(pkg_state_status_t ss
)
580 for (i
=0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
581 if (pkg_state_status_map
[i
].value
== ss
) {
582 return pkg_state_status_map
[i
].str
;
586 opkg_msg(ERROR
, "Internal error: state_status=%d\n", ss
);
587 return "<STATE_STATUS_UNKNOWN>";
591 pkg_state_status_from_str(const char *str
)
595 for (i
=0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
596 if (strcmp(str
, pkg_state_status_map
[i
].str
) == 0) {
597 return pkg_state_status_map
[i
].value
;
601 opkg_msg(ERROR
, "Internal error: state_status=%s\n", str
);
602 return SS_NOT_INSTALLED
;
606 pkg_formatted_field(FILE *fp
, pkg_t
*pkg
, const char *field
)
610 int depends_count
= pkg
->pre_depends_count
+
612 pkg
->recommends_count
+
615 if (strlen(field
) < PKG_MINIMUM_FIELD_NAME_LEN
) {
616 goto UNKNOWN_FMT_FIELD
;
623 if (strcasecmp(field
, "Architecture") == 0) {
624 if (pkg
->architecture
) {
625 fprintf(fp
, "Architecture: %s\n", pkg
->architecture
);
627 } else if (strcasecmp(field
, "Auto-Installed") == 0) {
628 if (pkg
->auto_installed
)
629 fprintf(fp
, "Auto-Installed: yes\n");
631 goto UNKNOWN_FMT_FIELD
;
636 if (strcasecmp(field
, "Conffiles") == 0) {
637 conffile_list_elt_t
*iter
;
639 if (nv_pair_list_empty(&pkg
->conffiles
))
642 fprintf(fp
, "Conffiles:\n");
643 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
644 if (((conffile_t
*)iter
->data
)->name
&& ((conffile_t
*)iter
->data
)->value
) {
645 fprintf(fp
, " %s %s\n",
646 ((conffile_t
*)iter
->data
)->name
,
647 ((conffile_t
*)iter
->data
)->value
);
650 } else if (strcasecmp(field
, "Conflicts") == 0) {
652 if (pkg
->conflicts_count
) {
653 fprintf(fp
, "Conflicts:");
654 for(i
= 0; i
< pkg
->conflicts_count
; i
++) {
655 cdep
= pkg
->conflicts
[i
].possibilities
[0];
656 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
659 fprintf(fp
, " (%s%s)",
660 constraint_to_str(cdep
->constraint
),
667 goto UNKNOWN_FMT_FIELD
;
672 if (strcasecmp(field
, "Depends") == 0) {
673 if (pkg
->depends_count
) {
674 fprintf(fp
, "Depends:");
675 for (j
=0, i
=0; i
<depends_count
; i
++) {
676 if (pkg
->depends
[i
].type
!= DEPEND
)
678 str
= pkg_depend_str(pkg
, i
);
679 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
685 } else if (strcasecmp(field
, "Description") == 0) {
686 if (pkg
->description
) {
687 fprintf(fp
, "Description: %s\n", pkg
->description
);
690 goto UNKNOWN_FMT_FIELD
;
695 if (pkg
->essential
) {
696 fprintf(fp
, "Essential: yes\n");
702 fprintf(fp
, "Filename: %s\n", pkg
->filename
);
707 if (strcasecmp(field
, "Installed-Size") == 0) {
708 fprintf(fp
, "Installed-Size: %ld\n", pkg
->installed_size
);
709 } else if (strcasecmp(field
, "Installed-Time") == 0 && pkg
->installed_time
) {
710 fprintf(fp
, "Installed-Time: %lu\n", pkg
->installed_time
);
715 if (strcasecmp(field
, "Maintainer") == 0) {
716 if (pkg
->maintainer
) {
717 fprintf(fp
, "Maintainer: %s\n", pkg
->maintainer
);
719 } else if (strcasecmp(field
, "MD5sum") == 0) {
721 fprintf(fp
, "MD5Sum: %s\n", pkg
->md5sum
);
724 goto UNKNOWN_FMT_FIELD
;
729 if (strcasecmp(field
, "Package") == 0) {
730 fprintf(fp
, "Package: %s\n", pkg
->name
);
731 } else if (strcasecmp(field
, "Priority") == 0) {
732 fprintf(fp
, "Priority: %s\n", pkg
->priority
);
733 } else if (strcasecmp(field
, "Provides") == 0) {
734 if (pkg
->provides_count
> 1) {
735 fprintf(fp
, "Provides:");
736 for(i
= 1; i
< pkg
->provides_count
; i
++) {
737 fprintf(fp
, "%s %s", i
== 1 ? "" : ",",
738 pkg
->provides
[i
]->name
);
743 goto UNKNOWN_FMT_FIELD
;
748 if (strcasecmp (field
, "Replaces") == 0) {
749 if (pkg
->replaces_count
) {
750 fprintf(fp
, "Replaces:");
751 for (i
= 0; i
< pkg
->replaces_count
; i
++) {
752 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
753 pkg
->replaces
[i
]->name
);
757 } else if (strcasecmp (field
, "Recommends") == 0) {
758 if (pkg
->recommends_count
) {
759 fprintf(fp
, "Recommends:");
760 for (j
=0, i
=0; i
<depends_count
; i
++) {
761 if (pkg
->depends
[i
].type
!= RECOMMEND
)
763 str
= pkg_depend_str(pkg
, i
);
764 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
771 goto UNKNOWN_FMT_FIELD
;
776 if (strcasecmp(field
, "Section") == 0) {
778 fprintf(fp
, "Section: %s\n", pkg
->section
);
780 #if defined HAVE_SHA256
781 } else if (strcasecmp(field
, "SHA256sum") == 0) {
782 if (pkg
->sha256sum
) {
783 fprintf(fp
, "SHA256sum: %s\n", pkg
->sha256sum
);
786 } else if (strcasecmp(field
, "Size") == 0) {
788 fprintf(fp
, "Size: %ld\n", pkg
->size
);
790 } else if (strcasecmp(field
, "Source") == 0) {
792 fprintf(fp
, "Source: %s\n", pkg
->source
);
794 } else if (strcasecmp(field
, "Status") == 0) {
795 char *pflag
= pkg_state_flag_to_str(pkg
->state_flag
);
796 fprintf(fp
, "Status: %s %s %s\n",
797 pkg_state_want_to_str(pkg
->state_want
),
799 pkg_state_status_to_str(pkg
->state_status
));
801 } else if (strcasecmp(field
, "Suggests") == 0) {
802 if (pkg
->suggests_count
) {
803 fprintf(fp
, "Suggests:");
804 for (j
=0, i
=0; i
<depends_count
; i
++) {
805 if (pkg
->depends
[i
].type
!= SUGGEST
)
807 str
= pkg_depend_str(pkg
, i
);
808 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
815 goto UNKNOWN_FMT_FIELD
;
820 if (strcasecmp(field
, "Tags") == 0) {
822 fprintf(fp
, "Tags: %s\n", pkg
->tags
);
829 char *version
= pkg_version_str_alloc(pkg
);
832 fprintf(fp
, "Version: %s\n", version
);
837 goto UNKNOWN_FMT_FIELD
;
843 opkg_msg(ERROR
, "Internal error: field=%s\n", field
);
847 pkg_formatted_info(FILE *fp
, pkg_t
*pkg
)
849 pkg_formatted_field(fp
, pkg
, "Package");
850 pkg_formatted_field(fp
, pkg
, "Version");
851 pkg_formatted_field(fp
, pkg
, "Depends");
852 pkg_formatted_field(fp
, pkg
, "Recommends");
853 pkg_formatted_field(fp
, pkg
, "Suggests");
854 pkg_formatted_field(fp
, pkg
, "Provides");
855 pkg_formatted_field(fp
, pkg
, "Replaces");
856 pkg_formatted_field(fp
, pkg
, "Conflicts");
857 pkg_formatted_field(fp
, pkg
, "Status");
858 pkg_formatted_field(fp
, pkg
, "Section");
859 pkg_formatted_field(fp
, pkg
, "Essential");
860 pkg_formatted_field(fp
, pkg
, "Architecture");
861 pkg_formatted_field(fp
, pkg
, "Maintainer");
862 pkg_formatted_field(fp
, pkg
, "MD5sum");
863 pkg_formatted_field(fp
, pkg
, "Size");
864 pkg_formatted_field(fp
, pkg
, "Filename");
865 pkg_formatted_field(fp
, pkg
, "Conffiles");
866 pkg_formatted_field(fp
, pkg
, "Source");
867 pkg_formatted_field(fp
, pkg
, "Description");
868 pkg_formatted_field(fp
, pkg
, "Installed-Time");
869 pkg_formatted_field(fp
, pkg
, "Tags");
874 pkg_print_status(pkg_t
* pkg
, FILE * file
)
880 pkg_formatted_field(file
, pkg
, "Package");
881 pkg_formatted_field(file
, pkg
, "Version");
882 pkg_formatted_field(file
, pkg
, "Depends");
883 pkg_formatted_field(file
, pkg
, "Recommends");
884 pkg_formatted_field(file
, pkg
, "Suggests");
885 pkg_formatted_field(file
, pkg
, "Provides");
886 pkg_formatted_field(file
, pkg
, "Replaces");
887 pkg_formatted_field(file
, pkg
, "Conflicts");
888 pkg_formatted_field(file
, pkg
, "Status");
889 pkg_formatted_field(file
, pkg
, "Essential");
890 pkg_formatted_field(file
, pkg
, "Architecture");
891 pkg_formatted_field(file
, pkg
, "Conffiles");
892 pkg_formatted_field(file
, pkg
, "Installed-Time");
893 pkg_formatted_field(file
, pkg
, "Auto-Installed");
898 * libdpkg - Debian packaging suite library routines
899 * vercmp.c - comparison of version numbers
901 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
904 /* assume ascii; warning: evaluates x multiple times! */
905 #define order(x) ((x) == '~' ? -1 \
908 : isalpha((x)) ? (x) \
912 verrevcmp(const char *val
, const char *ref
) {
916 while (*val
|| *ref
) {
919 while ( (*val
&& !isdigit(*val
)) || (*ref
&& !isdigit(*ref
)) ) {
920 int vc
= order(*val
), rc
= order(*ref
);
921 if (vc
!= rc
) return vc
- rc
;
925 while ( *val
== '0' ) val
++;
926 while ( *ref
== '0' ) ref
++;
927 while (isdigit(*val
) && isdigit(*ref
)) {
928 if (!first_diff
) first_diff
= *val
- *ref
;
931 if (isdigit(*val
)) return 1;
932 if (isdigit(*ref
)) return -1;
933 if (first_diff
) return first_diff
;
939 pkg_compare_versions(const pkg_t
*pkg
, const pkg_t
*ref_pkg
)
943 if (pkg
->epoch
> ref_pkg
->epoch
) {
947 if (pkg
->epoch
< ref_pkg
->epoch
) {
951 r
= verrevcmp(pkg
->version
, ref_pkg
->version
);
956 r
= verrevcmp(pkg
->revision
, ref_pkg
->revision
);
966 pkg_version_satisfied(pkg_t
*it
, pkg_t
*ref
, const char *op
)
970 r
= pkg_compare_versions(it
, ref
);
972 if (strcmp(op
, "<=") == 0 || strcmp(op
, "<") == 0) {
976 if (strcmp(op
, ">=") == 0 || strcmp(op
, ">") == 0) {
980 if (strcmp(op
, "<<") == 0) {
984 if (strcmp(op
, ">>") == 0) {
988 if (strcmp(op
, "=") == 0) {
992 opkg_msg(ERROR
, "Unknown operator: %s.\n", op
);
997 pkg_name_version_and_architecture_compare(const void *p1
, const void *p2
)
999 const pkg_t
*a
= *(const pkg_t
**) p1
;
1000 const pkg_t
*b
= *(const pkg_t
**) p2
;
1003 if (!a
->name
|| !b
->name
) {
1004 opkg_msg(ERROR
, "Internal error: a->name=%p, b->name=%p.\n",
1009 namecmp
= strcmp(a
->name
, b
->name
);
1012 vercmp
= pkg_compare_versions(a
, b
);
1015 if (!a
->arch_priority
|| !b
->arch_priority
) {
1016 opkg_msg(ERROR
, "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1017 a
->arch_priority
, b
->arch_priority
);
1020 if (a
->arch_priority
> b
->arch_priority
)
1022 if (a
->arch_priority
< b
->arch_priority
)
1028 abstract_pkg_name_compare(const void *p1
, const void *p2
)
1030 const abstract_pkg_t
*a
= *(const abstract_pkg_t
**)p1
;
1031 const abstract_pkg_t
*b
= *(const abstract_pkg_t
**)p2
;
1032 if (!a
->name
|| !b
->name
) {
1033 opkg_msg(ERROR
, "Internal error: a->name=%p b->name=%p.\n",
1037 return strcmp(a
->name
, b
->name
);
1042 pkg_version_str_alloc(pkg_t
*pkg
)
1048 sprintf_alloc(&version
, "%d:%s-%s",
1049 pkg
->epoch
, pkg
->version
, pkg
->revision
);
1051 sprintf_alloc(&version
, "%d:%s",
1052 pkg
->epoch
, pkg
->version
);
1055 sprintf_alloc(&version
, "%s-%s",
1056 pkg
->version
, pkg
->revision
);
1058 version
= xstrdup(pkg
->version
);
1065 * XXX: this should be broken into two functions
1068 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",
1140 free(list_file_name
);
1141 return pkg
->installed_files
;
1143 free(list_file_name
);
1146 if (conf
->offline_root
)
1147 rootdirlen
= strlen(conf
->offline_root
);
1152 line
= file_read_line_alloc(list_file
);
1158 if (list_from_package
) {
1159 if (*file_name
== '.') {
1162 if (*file_name
== '/') {
1165 sprintf_alloc(&installed_file_name
, "%s%s",
1166 pkg
->dest
->root_dir
, file_name
);
1168 if (conf
->offline_root
&&
1169 strncmp(conf
->offline_root
, file_name
, rootdirlen
)) {
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", file_name
);
1177 str_list_append(pkg
->installed_files
, installed_file_name
);
1178 free(installed_file_name
);
1184 if (list_from_package
) {
1185 unlink(list_file_name
);
1186 free(list_file_name
);
1189 return pkg
->installed_files
;
1192 /* XXX: CLEANUP: This function and it's counterpart,
1193 (pkg_get_installed_files), do not match our init/deinit naming
1194 convention. Nor the alloc/free convention. But, then again, neither
1195 of these conventions currrently fit the way these two functions
1198 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
;
1213 pkg_remove_installed_files_list(pkg_t
*pkg
)
1215 char *list_file_name
;
1217 sprintf_alloc(&list_file_name
, "%s/%s.list",
1218 pkg
->dest
->info_dir
, pkg
->name
);
1220 if (!conf
->noaction
)
1221 (void)unlink(list_file_name
);
1223 free(list_file_name
);
1227 pkg_get_conffile(pkg_t
*pkg
, const char *file_name
)
1229 conffile_list_elt_t
*iter
;
1230 conffile_t
*conffile
;
1236 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1237 conffile
= (conffile_t
*)iter
->data
;
1239 if (strcmp(conffile
->name
, file_name
) == 0) {
1248 pkg_run_script(pkg_t
*pkg
, const char *script
, const char *args
)
1257 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1258 maintainer script within a chroot environment. */
1259 if (conf
->offline_root
&& !conf
->force_postinstall
) {
1260 opkg_msg(INFO
, "Offline root mode: not running %s.%s.\n",
1265 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1266 have scripts in pkg->tmp_unpack_dir. */
1267 if (pkg
->state_status
== SS_INSTALLED
|| pkg
->state_status
== SS_UNPACKED
) {
1268 if (pkg
->dest
== NULL
) {
1269 opkg_msg(ERROR
, "Internal error: %s has a NULL dest.\n",
1273 sprintf_alloc(&path
, "%s/%s.%s", pkg
->dest
->info_dir
, pkg
->name
, script
);
1275 if (pkg
->tmp_unpack_dir
== NULL
) {
1276 opkg_msg(ERROR
, "Internal error: %s has a NULL tmp_unpack_dir.\n",
1280 sprintf_alloc(&path
, "%s/%s", pkg
->tmp_unpack_dir
, script
);
1283 opkg_msg(INFO
, "Running script %s.\n", path
);
1286 pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
, 1);
1288 if (pkg
->is_upgrade
)
1289 setenv("PKG_UPGRADE", "1", 1);
1291 setenv("PKG_UPGRADE", "0", 1);
1294 if (! file_exists(path
)) {
1299 sprintf_alloc(&cmd
, "%s %s", path
, args
);
1302 const char *argv
[] = {"sh", "-c", cmd
, NULL
};
1303 err
= xsystem(argv
);
1308 opkg_msg(ERROR
, "package \"%s\" %s script returned status %d.\n",
1309 pkg
->name
, script
, err
);
1317 pkg_arch_supported(pkg_t
*pkg
)
1319 nv_pair_list_elt_t
*l
;
1321 if (!pkg
->architecture
)
1324 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
1325 nv_pair_t
*nv
= (nv_pair_t
*)l
->data
;
1326 if (strcmp(nv
->name
, pkg
->architecture
) == 0) {
1327 opkg_msg(DEBUG
, "Arch %s (priority %s) supported for pkg %s.\n",
1328 nv
->name
, nv
->value
, pkg
->name
);
1333 opkg_msg(DEBUG
, "Arch %s unsupported for pkg %s.\n",
1334 pkg
->architecture
, pkg
->name
);
1339 pkg_info_preinstall_check(void)
1342 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1344 /* update the file owner data structure */
1345 opkg_msg(INFO
, "Updating file owner list.\n");
1346 pkg_hash_fetch_all_installed(installed_pkgs
);
1347 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1348 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1349 str_list_t
*installed_files
= pkg_get_installed_files(pkg
); /* this causes installed_files to be cached */
1350 str_list_elt_t
*iter
, *niter
;
1351 if (installed_files
== NULL
) {
1352 opkg_msg(ERROR
, "Failed to determine installed "
1353 "files for pkg %s.\n", pkg
->name
);
1356 for (iter
= str_list_first(installed_files
), niter
= str_list_next(installed_files
, iter
);
1358 iter
= niter
, niter
= str_list_next(installed_files
, iter
)) {
1359 char *installed_file
= (char *) iter
->data
;
1360 file_hash_set_file_owner(installed_file
, pkg
);
1362 pkg_free_installed_files(pkg
);
1364 pkg_vec_free(installed_pkgs
);
1367 struct pkg_write_filelist_data
{
1373 pkg_write_filelist_helper(const char *key
, void *entry_
, void *data_
)
1375 struct pkg_write_filelist_data
*data
= data_
;
1376 pkg_t
*entry
= entry_
;
1377 if (entry
== data
->pkg
) {
1378 fprintf(data
->stream
, "%s\n", key
);
1383 pkg_write_filelist(pkg_t
*pkg
)
1385 struct pkg_write_filelist_data data
;
1386 char *list_file_name
;
1388 sprintf_alloc(&list_file_name
, "%s/%s.list",
1389 pkg
->dest
->info_dir
, pkg
->name
);
1391 opkg_msg(INFO
, "Creating %s file for pkg %s.\n",
1392 list_file_name
, pkg
->name
);
1394 data
.stream
= fopen(list_file_name
, "w");
1396 opkg_perror(ERROR
, "Failed to open %s",
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
;
1413 pkg_write_changed_filelists(void)
1415 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1416 int i
, err
, ret
= 0;
1421 opkg_msg(INFO
, "Saving changed filelists.\n");
1423 pkg_hash_fetch_all_installed(installed_pkgs
);
1424 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1425 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1426 if (pkg
->state_flag
& SF_FILELIST_CHANGED
) {
1427 err
= pkg_write_filelist(pkg
);
1433 pkg_vec_free (installed_pkgs
);