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 (pkg_parse_from_stream(pkg
, control_file
, 0))
316 fclose(control_file
);
318 unlink(control_path
);
325 /* Merge any new information in newpkg into oldpkg */
327 pkg_merge(pkg_t
*oldpkg
, pkg_t
*newpkg
)
329 if (oldpkg
== newpkg
) {
333 if (!oldpkg
->auto_installed
)
334 oldpkg
->auto_installed
= newpkg
->auto_installed
;
337 oldpkg
->src
= newpkg
->src
;
339 oldpkg
->dest
= newpkg
->dest
;
340 if (!oldpkg
->architecture
)
341 oldpkg
->architecture
= xstrdup(newpkg
->architecture
);
342 if (!oldpkg
->arch_priority
)
343 oldpkg
->arch_priority
= newpkg
->arch_priority
;
344 if (!oldpkg
->section
)
345 oldpkg
->section
= xstrdup(newpkg
->section
);
346 if(!oldpkg
->maintainer
)
347 oldpkg
->maintainer
= xstrdup(newpkg
->maintainer
);
348 if(!oldpkg
->description
)
349 oldpkg
->description
= xstrdup(newpkg
->description
);
351 if (!oldpkg
->depends_count
&& !oldpkg
->pre_depends_count
&& !oldpkg
->recommends_count
&& !oldpkg
->suggests_count
) {
352 oldpkg
->depends_count
= newpkg
->depends_count
;
353 newpkg
->depends_count
= 0;
355 oldpkg
->depends
= newpkg
->depends
;
356 newpkg
->depends
= NULL
;
358 oldpkg
->pre_depends_count
= newpkg
->pre_depends_count
;
359 newpkg
->pre_depends_count
= 0;
361 oldpkg
->recommends_count
= newpkg
->recommends_count
;
362 newpkg
->recommends_count
= 0;
364 oldpkg
->suggests_count
= newpkg
->suggests_count
;
365 newpkg
->suggests_count
= 0;
368 if (oldpkg
->provides_count
<= 1) {
369 oldpkg
->provides_count
= newpkg
->provides_count
;
370 newpkg
->provides_count
= 0;
372 if (!oldpkg
->provides
) {
373 oldpkg
->provides
= newpkg
->provides
;
374 newpkg
->provides
= NULL
;
378 if (!oldpkg
->conflicts_count
) {
379 oldpkg
->conflicts_count
= newpkg
->conflicts_count
;
380 newpkg
->conflicts_count
= 0;
382 oldpkg
->conflicts
= newpkg
->conflicts
;
383 newpkg
->conflicts
= NULL
;
386 if (!oldpkg
->replaces_count
) {
387 oldpkg
->replaces_count
= newpkg
->replaces_count
;
388 newpkg
->replaces_count
= 0;
390 oldpkg
->replaces
= newpkg
->replaces
;
391 newpkg
->replaces
= NULL
;
394 if (!oldpkg
->filename
)
395 oldpkg
->filename
= xstrdup(newpkg
->filename
);
396 if (!oldpkg
->local_filename
)
397 oldpkg
->local_filename
= xstrdup(newpkg
->local_filename
);
398 if (!oldpkg
->tmp_unpack_dir
)
399 oldpkg
->tmp_unpack_dir
= xstrdup(newpkg
->tmp_unpack_dir
);
401 oldpkg
->md5sum
= xstrdup(newpkg
->md5sum
);
402 #if defined HAVE_SHA256
403 if (!oldpkg
->sha256sum
)
404 oldpkg
->sha256sum
= xstrdup(newpkg
->sha256sum
);
407 oldpkg
->size
= newpkg
->size
;
408 if (!oldpkg
->installed_size
)
409 oldpkg
->installed_size
= newpkg
->installed_size
;
410 if (!oldpkg
->priority
)
411 oldpkg
->priority
= xstrdup(newpkg
->priority
);
413 oldpkg
->source
= xstrdup(newpkg
->source
);
415 if (nv_pair_list_empty(&oldpkg
->conffiles
)){
416 list_splice_init(&newpkg
->conffiles
.head
, &oldpkg
->conffiles
.head
);
419 if (!oldpkg
->installed_files
){
420 oldpkg
->installed_files
= newpkg
->installed_files
;
421 oldpkg
->installed_files_ref_cnt
= newpkg
->installed_files_ref_cnt
;
422 newpkg
->installed_files
= NULL
;
425 if (!oldpkg
->essential
)
426 oldpkg
->essential
= newpkg
->essential
;
432 abstract_pkg_init(abstract_pkg_t
*ab_pkg
)
434 ab_pkg
->provided_by
= abstract_pkg_vec_alloc();
435 ab_pkg
->dependencies_checked
= 0;
436 ab_pkg
->state_status
= SS_NOT_INSTALLED
;
440 abstract_pkg_new(void)
442 abstract_pkg_t
* ab_pkg
;
444 ab_pkg
= xcalloc(1, sizeof(abstract_pkg_t
));
445 abstract_pkg_init(ab_pkg
);
451 set_flags_from_control(pkg_t
*pkg
){
455 sprintf_alloc(&file_name
,"%s/%s.control", pkg
->dest
->info_dir
, pkg
->name
);
457 fp
= fopen(file_name
, "r");
459 opkg_perror(ERROR
, "Failed to open %s");
466 if (pkg_parse_from_stream(pkg
, fp
, PFM_ALL
^ PFM_ESSENTIAL
)) {
467 opkg_msg(DEBUG
, "Unable to read control file for %s. May be empty.\n",
477 pkg_state_want_to_str(pkg_state_want_t sw
)
481 for (i
=0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
482 if (pkg_state_want_map
[i
].value
== sw
) {
483 return pkg_state_want_map
[i
].str
;
487 opkg_msg(ERROR
, "Internal error: state_want=%d\n", sw
);
488 return "<STATE_WANT_UNKNOWN>";
492 pkg_state_want_from_str(char *str
)
496 for (i
=0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
497 if (strcmp(str
, pkg_state_want_map
[i
].str
) == 0) {
498 return pkg_state_want_map
[i
].value
;
502 opkg_msg(ERROR
, "Internal error: state_want=%s\n", str
);
507 pkg_state_flag_to_str(pkg_state_flag_t sf
)
513 /* clear the temporary flags before converting to string */
514 sf
&= SF_NONVOLATILE_FLAGS
;
517 return xstrdup("ok");
520 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
521 if (sf
& pkg_state_flag_map
[i
].value
)
522 len
+= strlen(pkg_state_flag_map
[i
].str
) + 1;
525 str
= xmalloc(len
+1);
528 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
529 if (sf
& pkg_state_flag_map
[i
].value
) {
530 strncat(str
, pkg_state_flag_map
[i
].str
, len
);
531 strncat(str
, ",", len
);
536 str
[len
-1] = '\0'; /* squash last comma */
542 pkg_state_flag_from_str(const char *str
)
547 unsigned int sfname_len
;
549 if (strcmp(str
, "ok") == 0) {
552 for (i
=0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
553 sfname
= pkg_state_flag_map
[i
].str
;
554 sfname_len
= strlen(sfname
);
555 if (strncmp(str
, sfname
, sfname_len
) == 0) {
556 sf
|= pkg_state_flag_map
[i
].value
;
570 pkg_state_status_to_str(pkg_state_status_t ss
)
574 for (i
=0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
575 if (pkg_state_status_map
[i
].value
== ss
) {
576 return pkg_state_status_map
[i
].str
;
580 opkg_msg(ERROR
, "Internal error: state_status=%d\n", ss
);
581 return "<STATE_STATUS_UNKNOWN>";
585 pkg_state_status_from_str(const char *str
)
589 for (i
=0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
590 if (strcmp(str
, pkg_state_status_map
[i
].str
) == 0) {
591 return pkg_state_status_map
[i
].value
;
595 opkg_msg(ERROR
, "Internal error: state_status=%s\n", str
);
596 return SS_NOT_INSTALLED
;
600 pkg_formatted_field(FILE *fp
, pkg_t
*pkg
, const char *field
)
604 int depends_count
= pkg
->pre_depends_count
+
606 pkg
->recommends_count
+
609 if (strlen(field
) < PKG_MINIMUM_FIELD_NAME_LEN
) {
610 goto UNKNOWN_FMT_FIELD
;
617 if (strcasecmp(field
, "Architecture") == 0) {
618 if (pkg
->architecture
) {
619 fprintf(fp
, "Architecture: %s\n", pkg
->architecture
);
621 } else if (strcasecmp(field
, "Auto-Installed") == 0) {
622 if (pkg
->auto_installed
)
623 fprintf(fp
, "Auto-Installed: yes\n");
625 goto UNKNOWN_FMT_FIELD
;
630 if (strcasecmp(field
, "Conffiles") == 0) {
631 conffile_list_elt_t
*iter
;
633 if (nv_pair_list_empty(&pkg
->conffiles
))
636 fprintf(fp
, "Conffiles:\n");
637 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
638 if (((conffile_t
*)iter
->data
)->name
&& ((conffile_t
*)iter
->data
)->value
) {
639 fprintf(fp
, " %s %s\n",
640 ((conffile_t
*)iter
->data
)->name
,
641 ((conffile_t
*)iter
->data
)->value
);
644 } else if (strcasecmp(field
, "Conflicts") == 0) {
646 if (pkg
->conflicts_count
) {
647 fprintf(fp
, "Conflicts:");
648 for(i
= 0; i
< pkg
->conflicts_count
; i
++) {
649 cdep
= pkg
->conflicts
[i
].possibilities
[0];
650 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
653 fprintf(fp
, " (%s%s)",
654 constraint_to_str(cdep
->constraint
),
661 goto UNKNOWN_FMT_FIELD
;
666 if (strcasecmp(field
, "Depends") == 0) {
667 if (pkg
->depends_count
) {
668 fprintf(fp
, "Depends:");
669 for (j
=0, i
=0; i
<depends_count
; i
++) {
670 if (pkg
->depends
[i
].type
!= DEPEND
)
672 str
= pkg_depend_str(pkg
, i
);
673 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
679 } else if (strcasecmp(field
, "Description") == 0) {
680 if (pkg
->description
) {
681 fprintf(fp
, "Description: %s\n", pkg
->description
);
684 goto UNKNOWN_FMT_FIELD
;
689 if (pkg
->essential
) {
690 fprintf(fp
, "Essential: yes\n");
696 fprintf(fp
, "Filename: %s\n", pkg
->filename
);
701 if (strcasecmp(field
, "Installed-Size") == 0) {
702 fprintf(fp
, "Installed-Size: %ld\n", pkg
->installed_size
);
703 } else if (strcasecmp(field
, "Installed-Time") == 0 && pkg
->installed_time
) {
704 fprintf(fp
, "Installed-Time: %lu\n", pkg
->installed_time
);
709 if (strcasecmp(field
, "Maintainer") == 0) {
710 if (pkg
->maintainer
) {
711 fprintf(fp
, "maintainer: %s\n", pkg
->maintainer
);
713 } else if (strcasecmp(field
, "MD5sum") == 0) {
715 fprintf(fp
, "MD5Sum: %s\n", pkg
->md5sum
);
718 goto UNKNOWN_FMT_FIELD
;
723 if (strcasecmp(field
, "Package") == 0) {
724 fprintf(fp
, "Package: %s\n", pkg
->name
);
725 } else if (strcasecmp(field
, "Priority") == 0) {
726 fprintf(fp
, "Priority: %s\n", pkg
->priority
);
727 } else if (strcasecmp(field
, "Provides") == 0) {
728 if (pkg
->provides_count
) {
729 fprintf(fp
, "Provides:");
730 for(i
= 1; i
< pkg
->provides_count
; i
++) {
731 fprintf(fp
, "%s %s", i
== 1 ? "" : ",",
732 pkg
->provides
[i
]->name
);
737 goto UNKNOWN_FMT_FIELD
;
742 if (strcasecmp (field
, "Replaces") == 0) {
743 if (pkg
->replaces_count
) {
744 fprintf(fp
, "Replaces:");
745 for (i
= 0; i
< pkg
->replaces_count
; i
++) {
746 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
747 pkg
->replaces
[i
]->name
);
751 } else if (strcasecmp (field
, "Recommends") == 0) {
752 if (pkg
->recommends_count
) {
753 fprintf(fp
, "Recommends:");
754 for (j
=0, i
=0; i
<depends_count
; i
++) {
755 if (pkg
->depends
[i
].type
!= RECOMMEND
)
757 str
= pkg_depend_str(pkg
, i
);
758 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
765 goto UNKNOWN_FMT_FIELD
;
770 if (strcasecmp(field
, "Section") == 0) {
772 fprintf(fp
, "Section: %s\n", pkg
->section
);
774 #if defined HAVE_SHA256
775 } else if (strcasecmp(field
, "SHA256sum") == 0) {
776 if (pkg
->sha256sum
) {
777 fprintf(fp
, "SHA256sum: %s\n", pkg
->sha256sum
);
780 } else if (strcasecmp(field
, "Size") == 0) {
782 fprintf(fp
, "Size: %ld\n", pkg
->size
);
784 } else if (strcasecmp(field
, "Source") == 0) {
786 fprintf(fp
, "Source: %s\n", pkg
->source
);
788 } else if (strcasecmp(field
, "Status") == 0) {
789 char *pflag
= pkg_state_flag_to_str(pkg
->state_flag
);
790 fprintf(fp
, "Status: %s %s %s\n",
791 pkg_state_want_to_str(pkg
->state_want
),
793 pkg_state_status_to_str(pkg
->state_status
));
795 } else if (strcasecmp(field
, "Suggests") == 0) {
796 if (pkg
->suggests_count
) {
797 fprintf(fp
, "Suggests:");
798 for (j
=0, i
=0; i
<depends_count
; i
++) {
799 if (pkg
->depends
[i
].type
!= SUGGEST
)
801 str
= pkg_depend_str(pkg
, i
);
802 fprintf(fp
, "%s %s", j
== 0 ? "" : ",", str
);
809 goto UNKNOWN_FMT_FIELD
;
814 if (strcasecmp(field
, "Tags") == 0) {
816 fprintf(fp
, "Tags: %s\n", pkg
->tags
);
823 char *version
= pkg_version_str_alloc(pkg
);
826 fprintf(fp
, "Version: %s\n", version
);
831 goto UNKNOWN_FMT_FIELD
;
837 opkg_msg(ERROR
, "Internal error: field=%s\n", field
);
841 pkg_formatted_info(FILE *fp
, pkg_t
*pkg
)
843 pkg_formatted_field(fp
, pkg
, "Package");
844 pkg_formatted_field(fp
, pkg
, "Version");
845 pkg_formatted_field(fp
, pkg
, "Depends");
846 pkg_formatted_field(fp
, pkg
, "Recommends");
847 pkg_formatted_field(fp
, pkg
, "Suggests");
848 pkg_formatted_field(fp
, pkg
, "Provides");
849 pkg_formatted_field(fp
, pkg
, "Replaces");
850 pkg_formatted_field(fp
, pkg
, "Conflicts");
851 pkg_formatted_field(fp
, pkg
, "Status");
852 pkg_formatted_field(fp
, pkg
, "Section");
853 pkg_formatted_field(fp
, pkg
, "Essential");
854 pkg_formatted_field(fp
, pkg
, "Architecture");
855 pkg_formatted_field(fp
, pkg
, "Maintainer");
856 pkg_formatted_field(fp
, pkg
, "MD5sum");
857 pkg_formatted_field(fp
, pkg
, "Size");
858 pkg_formatted_field(fp
, pkg
, "Filename");
859 pkg_formatted_field(fp
, pkg
, "Conffiles");
860 pkg_formatted_field(fp
, pkg
, "Source");
861 pkg_formatted_field(fp
, pkg
, "Description");
862 pkg_formatted_field(fp
, pkg
, "Installed-Time");
863 pkg_formatted_field(fp
, pkg
, "Tags");
868 pkg_print_status(pkg_t
* pkg
, FILE * file
)
874 pkg_formatted_field(file
, pkg
, "Package");
875 pkg_formatted_field(file
, pkg
, "Version");
876 pkg_formatted_field(file
, pkg
, "Depends");
877 pkg_formatted_field(file
, pkg
, "Recommends");
878 pkg_formatted_field(file
, pkg
, "Suggests");
879 pkg_formatted_field(file
, pkg
, "Provides");
880 pkg_formatted_field(file
, pkg
, "Replaces");
881 pkg_formatted_field(file
, pkg
, "Conflicts");
882 pkg_formatted_field(file
, pkg
, "Status");
883 pkg_formatted_field(file
, pkg
, "Essential");
884 pkg_formatted_field(file
, pkg
, "Architecture");
885 pkg_formatted_field(file
, pkg
, "Conffiles");
886 pkg_formatted_field(file
, pkg
, "Installed-Time");
887 pkg_formatted_field(file
, pkg
, "Auto-Installed");
892 * libdpkg - Debian packaging suite library routines
893 * vercmp.c - comparison of version numbers
895 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
898 /* assume ascii; warning: evaluates x multiple times! */
899 #define order(x) ((x) == '~' ? -1 \
902 : isalpha((x)) ? (x) \
906 verrevcmp(const char *val
, const char *ref
) {
910 while (*val
|| *ref
) {
913 while ( (*val
&& !isdigit(*val
)) || (*ref
&& !isdigit(*ref
)) ) {
914 int vc
= order(*val
), rc
= order(*ref
);
915 if (vc
!= rc
) return vc
- rc
;
919 while ( *val
== '0' ) val
++;
920 while ( *ref
== '0' ) ref
++;
921 while (isdigit(*val
) && isdigit(*ref
)) {
922 if (!first_diff
) first_diff
= *val
- *ref
;
925 if (isdigit(*val
)) return 1;
926 if (isdigit(*ref
)) return -1;
927 if (first_diff
) return first_diff
;
933 pkg_compare_versions(const pkg_t
*pkg
, const pkg_t
*ref_pkg
)
937 if (pkg
->epoch
> ref_pkg
->epoch
) {
941 if (pkg
->epoch
< ref_pkg
->epoch
) {
945 r
= verrevcmp(pkg
->version
, ref_pkg
->version
);
950 r
= verrevcmp(pkg
->revision
, ref_pkg
->revision
);
960 pkg_version_satisfied(pkg_t
*it
, pkg_t
*ref
, const char *op
)
964 r
= pkg_compare_versions(it
, ref
);
966 if (strcmp(op
, "<=") == 0 || strcmp(op
, "<") == 0) {
970 if (strcmp(op
, ">=") == 0 || strcmp(op
, ">") == 0) {
974 if (strcmp(op
, "<<") == 0) {
978 if (strcmp(op
, ">>") == 0) {
982 if (strcmp(op
, "=") == 0) {
986 opkg_msg(ERROR
, "Unknown operator: %s.\n", op
);
991 pkg_name_version_and_architecture_compare(const void *p1
, const void *p2
)
993 const pkg_t
*a
= *(const pkg_t
**) p1
;
994 const pkg_t
*b
= *(const pkg_t
**) p2
;
997 if (!a
->name
|| !b
->name
) {
998 opkg_msg(ERROR
, "Internal error: a->name=%p, b->name=%p.\n",
1003 namecmp
= strcmp(a
->name
, b
->name
);
1006 vercmp
= pkg_compare_versions(a
, b
);
1009 if (!a
->arch_priority
|| !b
->arch_priority
) {
1010 opkg_msg(ERROR
, "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1011 a
->arch_priority
, b
->arch_priority
);
1014 if (a
->arch_priority
> b
->arch_priority
)
1016 if (a
->arch_priority
< b
->arch_priority
)
1022 abstract_pkg_name_compare(const void *p1
, const void *p2
)
1024 const abstract_pkg_t
*a
= *(const abstract_pkg_t
**)p1
;
1025 const abstract_pkg_t
*b
= *(const abstract_pkg_t
**)p2
;
1026 if (!a
->name
|| !b
->name
) {
1027 opkg_msg(ERROR
, "Internal error: a->name=%p b->name=%p.\n",
1031 return strcmp(a
->name
, b
->name
);
1036 pkg_version_str_alloc(pkg_t
*pkg
)
1042 sprintf_alloc(&version
, "%d:%s-%s",
1043 pkg
->epoch
, pkg
->version
, pkg
->revision
);
1045 sprintf_alloc(&version
, "%d:%s",
1046 pkg
->epoch
, pkg
->version
);
1049 sprintf_alloc(&version
, "%s-%s",
1050 pkg
->version
, pkg
->revision
);
1052 version
= xstrdup(pkg
->version
);
1059 * XXX: this should be broken into two functions
1062 pkg_get_installed_files(pkg_t
*pkg
)
1065 char *list_file_name
= NULL
;
1066 FILE *list_file
= NULL
;
1068 char *installed_file_name
;
1069 unsigned int rootdirlen
= 0;
1071 pkg
->installed_files_ref_cnt
++;
1073 if (pkg
->installed_files
) {
1074 return pkg
->installed_files
;
1077 pkg
->installed_files
= str_list_alloc();
1079 /* For uninstalled packages, get the file list directly from the package.
1080 For installed packages, look at the package.list file in the database.
1082 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
) {
1083 if (pkg
->local_filename
== NULL
) {
1084 return pkg
->installed_files
;
1086 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1087 file. In other words, change deb_extract so that it can
1088 simply return the file list as a char *[] rather than
1089 insisting on writing in to a FILE * as it does now. */
1090 sprintf_alloc(&list_file_name
, "%s/%s.list.XXXXXX",
1091 conf
->tmp_dir
, pkg
->name
);
1092 fd
= mkstemp(list_file_name
);
1094 opkg_perror(ERROR
, "Failed to make temp file %s.",
1096 free(list_file_name
);
1097 return pkg
->installed_files
;
1099 list_file
= fdopen(fd
, "r+");
1100 if (list_file
== NULL
) {
1101 opkg_perror(ERROR
, "Failed to fdopen temp file %s.",
1104 unlink(list_file_name
);
1105 free(list_file_name
);
1106 return pkg
->installed_files
;
1108 err
= pkg_extract_data_file_names_to_stream(pkg
, list_file
);
1110 opkg_msg(ERROR
, "Error extracting file list from %s.\n",
1111 pkg
->local_filename
);
1113 unlink(list_file_name
);
1114 free(list_file_name
);
1115 str_list_deinit(pkg
->installed_files
);
1116 pkg
->installed_files
= NULL
;
1121 sprintf_alloc(&list_file_name
, "%s/%s.list",
1122 pkg
->dest
->info_dir
, pkg
->name
);
1123 list_file
= fopen(list_file_name
, "r");
1124 if (list_file
== NULL
) {
1125 opkg_perror(ERROR
, "Failed to open %s",
1127 free(list_file_name
);
1128 return pkg
->installed_files
;
1130 free(list_file_name
);
1133 if (conf
->offline_root
)
1134 rootdirlen
= strlen(conf
->offline_root
);
1139 line
= file_read_line_alloc(list_file
);
1145 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
) {
1146 if (*file_name
== '.') {
1149 if (*file_name
== '/') {
1152 sprintf_alloc(&installed_file_name
, "%s%s",
1153 pkg
->dest
->root_dir
, file_name
);
1155 if (conf
->offline_root
&&
1156 strncmp(conf
->offline_root
, file_name
, rootdirlen
)) {
1157 sprintf_alloc(&installed_file_name
, "%s%s",
1158 conf
->offline_root
, file_name
);
1160 // already contains root_dir as header -> ABSOLUTE
1161 sprintf_alloc(&installed_file_name
, "%s", file_name
);
1164 str_list_append(pkg
->installed_files
, installed_file_name
);
1165 free(installed_file_name
);
1171 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
) {
1172 unlink(list_file_name
);
1173 free(list_file_name
);
1176 return pkg
->installed_files
;
1179 /* XXX: CLEANUP: This function and it's counterpart,
1180 (pkg_get_installed_files), do not match our init/deinit naming
1181 convention. Nor the alloc/free convention. But, then again, neither
1182 of these conventions currrently fit the way these two functions
1185 pkg_free_installed_files(pkg_t
*pkg
)
1187 pkg
->installed_files_ref_cnt
--;
1189 if (pkg
->installed_files_ref_cnt
> 0)
1192 if (pkg
->installed_files
) {
1193 str_list_purge(pkg
->installed_files
);
1196 pkg
->installed_files
= NULL
;
1200 pkg_remove_installed_files_list(pkg_t
*pkg
)
1202 char *list_file_name
;
1204 sprintf_alloc(&list_file_name
, "%s/%s.list",
1205 pkg
->dest
->info_dir
, pkg
->name
);
1207 if (!conf
->noaction
)
1208 (void)unlink(list_file_name
);
1210 free(list_file_name
);
1214 pkg_get_conffile(pkg_t
*pkg
, const char *file_name
)
1216 conffile_list_elt_t
*iter
;
1217 conffile_t
*conffile
;
1223 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1224 conffile
= (conffile_t
*)iter
->data
;
1226 if (strcmp(conffile
->name
, file_name
) == 0) {
1235 pkg_run_script(pkg_t
*pkg
, const char *script
, const char *args
)
1244 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1245 maintainer script within a chroot environment. */
1246 if (conf
->offline_root
) {
1247 opkg_msg(INFO
, "Offline root mode: not running %s.%s.\n",
1252 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1253 have scripts in pkg->tmp_unpack_dir. */
1254 if (pkg
->state_status
== SS_INSTALLED
|| pkg
->state_status
== SS_UNPACKED
) {
1255 if (pkg
->dest
== NULL
) {
1256 opkg_msg(ERROR
, "Internal error: %s has a NULL dest.\n",
1260 sprintf_alloc(&path
, "%s/%s.%s", pkg
->dest
->info_dir
, pkg
->name
, script
);
1262 if (pkg
->tmp_unpack_dir
== NULL
) {
1263 opkg_msg(ERROR
, "Internal error: %s has a NULL tmp_unpack_dir.\n",
1267 sprintf_alloc(&path
, "%s/%s", pkg
->tmp_unpack_dir
, script
);
1270 opkg_msg(INFO
, "Running script %s.\n", path
);
1273 pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
, 1);
1275 if (! file_exists(path
)) {
1280 sprintf_alloc(&cmd
, "%s %s", path
, args
);
1283 const char *argv
[] = {"sh", "-c", cmd
, NULL
};
1284 err
= xsystem(argv
);
1289 opkg_msg(ERROR
, "%s script returned status %d.\n", script
, err
);
1297 pkg_arch_supported(pkg_t
*pkg
)
1299 nv_pair_list_elt_t
*l
;
1301 if (!pkg
->architecture
)
1304 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
1305 nv_pair_t
*nv
= (nv_pair_t
*)l
->data
;
1306 if (strcmp(nv
->name
, pkg
->architecture
) == 0) {
1307 opkg_msg(DEBUG
, "Arch %s (priority %s) supported for pkg %s.\n",
1308 nv
->name
, nv
->value
, pkg
->name
);
1313 opkg_msg(DEBUG
, "Arch %s unsupported for pkg %s.\n",
1314 pkg
->architecture
, pkg
->name
);
1319 pkg_info_preinstall_check(void)
1322 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1324 /* update the file owner data structure */
1325 opkg_msg(INFO
, "Updating file owner list.\n");
1326 pkg_hash_fetch_all_installed(installed_pkgs
);
1327 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1328 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1329 str_list_t
*installed_files
= pkg_get_installed_files(pkg
); /* this causes installed_files to be cached */
1330 str_list_elt_t
*iter
, *niter
;
1331 if (installed_files
== NULL
) {
1332 opkg_msg(ERROR
, "Failed to determine installed "
1333 "files for pkg %s.\n", pkg
->name
);
1336 for (iter
= str_list_first(installed_files
), niter
= str_list_next(installed_files
, iter
);
1338 iter
= niter
, niter
= str_list_next(installed_files
, iter
)) {
1339 char *installed_file
= (char *) iter
->data
;
1340 file_hash_set_file_owner(installed_file
, pkg
);
1342 pkg_free_installed_files(pkg
);
1344 pkg_vec_free(installed_pkgs
);
1347 struct pkg_write_filelist_data
{
1353 pkg_write_filelist_helper(const char *key
, void *entry_
, void *data_
)
1355 struct pkg_write_filelist_data
*data
= data_
;
1356 pkg_t
*entry
= entry_
;
1357 if (entry
== data
->pkg
) {
1358 fprintf(data
->stream
, "%s\n", key
);
1363 pkg_write_filelist(pkg_t
*pkg
)
1365 struct pkg_write_filelist_data data
;
1366 char *list_file_name
;
1368 sprintf_alloc(&list_file_name
, "%s/%s.list",
1369 pkg
->dest
->info_dir
, pkg
->name
);
1371 opkg_msg(INFO
, "Creating %s file for pkg %s.\n",
1372 list_file_name
, pkg
->name
);
1374 data
.stream
= fopen(list_file_name
, "w");
1376 opkg_perror(ERROR
, "Failed to open %s",
1378 free(list_file_name
);
1383 hash_table_foreach(&conf
->file_hash
, pkg_write_filelist_helper
, &data
);
1384 fclose(data
.stream
);
1385 free(list_file_name
);
1387 pkg
->state_flag
&= ~SF_FILELIST_CHANGED
;
1393 pkg_write_changed_filelists(void)
1395 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1396 int i
, err
, ret
= 0;
1401 opkg_msg(INFO
, "Saving changed filelists.\n");
1403 pkg_hash_fetch_all_installed(installed_pkgs
);
1404 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1405 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1406 if (pkg
->state_flag
& SF_FILELIST_CHANGED
) {
1407 err
= pkg_write_filelist(pkg
);
1413 pkg_vec_free (installed_pkgs
);