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
)
79 pkg
->state_want
= SW_UNKNOWN
;
80 pkg
->state_flag
= SF_OK
;
81 pkg
->state_status
= SS_NOT_INSTALLED
;
82 pkg
->depends_str
= NULL
;
83 pkg
->provides_str
= NULL
;
84 pkg
->depends_count
= 0;
86 pkg
->suggests_str
= NULL
;
87 pkg
->recommends_str
= NULL
;
88 pkg
->suggests_count
= 0;
89 pkg
->recommends_count
= 0;
91 active_list_init(&pkg
->list
);
93 pkg
->conflicts
= NULL
;
94 pkg
->conflicts_count
= 0;
97 pkg
->replaces_count
= 0;
99 pkg
->pre_depends_count
= 0;
100 pkg
->pre_depends_str
= NULL
;
101 pkg
->provides_count
= 0;
102 pkg
->provides
= NULL
;
103 pkg
->tmp_unpack_dir
= NULL
;
104 conffile_list_init(&pkg
->conffiles
);
105 pkg
->installed_files
= NULL
;
106 pkg
->installed_files_ref_cnt
= 0;
108 pkg
->provided_by_hand
= 0;
110 blob_buf_init(&pkg
->blob
, 0);
117 pkg
= xcalloc(1, sizeof(pkg_t
));
123 void *pkg_set_raw(pkg_t
*pkg
, int id
, const void *val
, size_t len
)
126 struct blob_attr
*cur
;
128 blob_for_each_attr(cur
, pkg
->blob
.head
, rem
) {
129 if (blob_id(cur
) == id
) {
130 if (blob_len(cur
) < len
) {
131 fprintf(stderr
, "ERROR: truncating field %d <%p> to %d byte",
132 id
, val
, blob_len(cur
));
134 memcpy(blob_data(cur
), val
, blob_len(cur
));
135 return blob_data(cur
);
139 cur
= blob_put(&pkg
->blob
, id
, val
, len
);
140 return cur
? blob_data(cur
) : NULL
;
143 void *pkg_get_raw(const pkg_t
* pkg
, int id
)
146 struct blob_attr
*cur
;
148 blob_for_each_attr(cur
, pkg
->blob
.head
, rem
)
149 if (blob_id(cur
) == id
)
150 return blob_data(cur
);
155 char *pkg_set_string(pkg_t
*pkg
, int id
, const char *s
)
165 while (isspace(*s
)) {
170 while (len
> 0 && isspace(s
[len
- 1]))
176 p
= pkg_set_raw(pkg
, id
, s
, len
+ 1);
183 static void compound_depend_deinit(compound_depend_t
* depends
)
186 for (i
= 0; i
< depends
->possibility_count
; i
++) {
188 d
= depends
->possibilities
[i
];
192 free(depends
->possibilities
);
195 void pkg_deinit(pkg_t
* pkg
)
203 /* owned by opkg_conf_t */
205 /* owned by opkg_conf_t */
208 pkg
->state_want
= SW_UNKNOWN
;
209 pkg
->state_flag
= SF_OK
;
210 pkg
->state_status
= SS_NOT_INSTALLED
;
212 active_list_clear(&pkg
->list
);
216 pkg
->replaces
= NULL
;
219 int count
= pkg
->pre_depends_count
221 + pkg
->recommends_count
+ pkg
->suggests_count
;
223 for (i
= 0; i
< count
; i
++)
224 compound_depend_deinit(&pkg
->depends
[i
]);
228 if (pkg
->conflicts
) {
229 for (i
= 0; i
< pkg
->conflicts_count
; i
++)
230 compound_depend_deinit(&pkg
->conflicts
[i
]);
231 free(pkg
->conflicts
);
237 pkg
->pre_depends_count
= 0;
238 pkg
->provides_count
= 0;
240 /* CLEANUP: It'd be nice to pullin the cleanup function from
241 opkg_install.c here. See comment in
242 opkg_install.c:cleanup_temporary_files */
243 if (pkg
->tmp_unpack_dir
)
244 free(pkg
->tmp_unpack_dir
);
245 pkg
->tmp_unpack_dir
= NULL
;
247 conffile_list_deinit(&pkg
->conffiles
);
249 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
250 since if they are calling deinit, they should know. Maybe do an
251 assertion here instead? */
252 pkg
->installed_files_ref_cnt
= 1;
253 pkg_free_installed_files(pkg
);
256 blob_buf_free(&pkg
->blob
);
259 int pkg_init_from_file(pkg_t
* pkg
, const char *filename
)
263 char *control_path
, *tmp
;
267 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, filename
);
269 tmp
= xstrdup(filename
);
270 sprintf_alloc(&control_path
, "%s/%s.control.XXXXXX",
271 conf
->tmp_dir
, basename(tmp
));
273 fd
= mkstemp(control_path
);
275 opkg_perror(ERROR
, "Failed to make temp file %s", control_path
);
280 control_file
= fdopen(fd
, "r+");
281 if (control_file
== NULL
) {
282 opkg_perror(ERROR
, "Failed to fdopen %s", control_path
);
288 err
= pkg_extract_control_file_to_stream(pkg
, control_file
);
290 opkg_msg(ERROR
, "Failed to extract control file from %s.\n",
295 rewind(control_file
);
297 if ((err
= pkg_parse_from_stream(pkg
, control_file
, 0))) {
299 opkg_msg(ERROR
, "Malformed package file %s.\n",
306 fclose(control_file
);
308 unlink(control_path
);
315 /* Merge any new information in newpkg into oldpkg */
316 int pkg_merge(pkg_t
* oldpkg
, pkg_t
* newpkg
)
318 if (oldpkg
== newpkg
) {
322 if (!oldpkg
->auto_installed
)
323 oldpkg
->auto_installed
= newpkg
->auto_installed
;
326 oldpkg
->src
= newpkg
->src
;
328 oldpkg
->dest
= newpkg
->dest
;
329 if (!pkg_get_string(oldpkg
, PKG_ARCHITECTURE
))
330 pkg_set_string(oldpkg
, PKG_ARCHITECTURE
, pkg_get_string(newpkg
, PKG_ARCHITECTURE
));
331 if (!oldpkg
->arch_priority
)
332 oldpkg
->arch_priority
= newpkg
->arch_priority
;
333 if (!pkg_get_string(oldpkg
, PKG_SECTION
))
334 pkg_set_string(oldpkg
, PKG_SECTION
, pkg_get_string(newpkg
, PKG_SECTION
));
335 if (!pkg_get_string(oldpkg
, PKG_MAINTAINER
))
336 pkg_set_string(oldpkg
, PKG_MAINTAINER
, pkg_get_string(newpkg
, PKG_MAINTAINER
));
337 if (!pkg_get_string(oldpkg
, PKG_DESCRIPTION
))
338 pkg_set_string(oldpkg
, PKG_DESCRIPTION
, pkg_get_string(newpkg
, PKG_DESCRIPTION
));
340 if (!oldpkg
->depends_count
&& !oldpkg
->pre_depends_count
341 && !oldpkg
->recommends_count
&& !oldpkg
->suggests_count
) {
342 oldpkg
->depends_count
= newpkg
->depends_count
;
343 newpkg
->depends_count
= 0;
345 oldpkg
->depends
= newpkg
->depends
;
346 newpkg
->depends
= NULL
;
348 oldpkg
->pre_depends_count
= newpkg
->pre_depends_count
;
349 newpkg
->pre_depends_count
= 0;
351 oldpkg
->recommends_count
= newpkg
->recommends_count
;
352 newpkg
->recommends_count
= 0;
354 oldpkg
->suggests_count
= newpkg
->suggests_count
;
355 newpkg
->suggests_count
= 0;
358 if (oldpkg
->provides_count
<= 1) {
359 oldpkg
->provides_count
= newpkg
->provides_count
;
360 newpkg
->provides_count
= 0;
362 if (!oldpkg
->provides
) {
363 oldpkg
->provides
= newpkg
->provides
;
364 newpkg
->provides
= NULL
;
368 if (!oldpkg
->conflicts_count
) {
369 oldpkg
->conflicts_count
= newpkg
->conflicts_count
;
370 newpkg
->conflicts_count
= 0;
372 oldpkg
->conflicts
= newpkg
->conflicts
;
373 newpkg
->conflicts
= NULL
;
376 if (!oldpkg
->replaces_count
) {
377 oldpkg
->replaces_count
= newpkg
->replaces_count
;
378 newpkg
->replaces_count
= 0;
380 oldpkg
->replaces
= newpkg
->replaces
;
381 newpkg
->replaces
= NULL
;
384 if (!pkg_get_string(oldpkg
, PKG_FILENAME
))
385 pkg_set_string(oldpkg
, PKG_FILENAME
, pkg_get_string(newpkg
, PKG_FILENAME
));
386 if (!pkg_get_string(oldpkg
, PKG_LOCAL_FILENAME
))
387 pkg_set_string(oldpkg
, PKG_LOCAL_FILENAME
, pkg_get_string(newpkg
, PKG_LOCAL_FILENAME
));
388 if (!oldpkg
->tmp_unpack_dir
)
389 oldpkg
->tmp_unpack_dir
= xstrdup(newpkg
->tmp_unpack_dir
);
390 if (!pkg_get_string(oldpkg
, PKG_MD5SUM
))
391 pkg_set_string(oldpkg
, PKG_MD5SUM
, pkg_get_string(newpkg
, PKG_MD5SUM
));
392 if (!pkg_get_string(oldpkg
, PKG_SHA256SUM
))
393 pkg_set_string(oldpkg
, PKG_SHA256SUM
, pkg_get_string(newpkg
, PKG_SHA256SUM
));
394 if (!pkg_get_int(oldpkg
, PKG_SIZE
))
395 pkg_set_int(oldpkg
, PKG_SIZE
, pkg_get_int(newpkg
, PKG_SIZE
));
396 if (!pkg_get_int(oldpkg
, PKG_INSTALLED_SIZE
))
397 pkg_set_int(oldpkg
, PKG_INSTALLED_SIZE
, pkg_get_int(newpkg
, PKG_INSTALLED_SIZE
));
398 if (!pkg_get_string(oldpkg
, PKG_PRIORITY
))
399 pkg_set_string(oldpkg
, PKG_PRIORITY
, pkg_get_string(newpkg
, PKG_PRIORITY
));
400 if (!pkg_get_string(oldpkg
, PKG_SOURCE
))
401 pkg_set_string(oldpkg
, PKG_SOURCE
, pkg_get_string(newpkg
, PKG_SOURCE
));
403 if (nv_pair_list_empty(&oldpkg
->conffiles
)) {
404 list_splice_init(&newpkg
->conffiles
.head
,
405 &oldpkg
->conffiles
.head
);
408 if (!oldpkg
->installed_files
) {
409 oldpkg
->installed_files
= newpkg
->installed_files
;
410 oldpkg
->installed_files_ref_cnt
=
411 newpkg
->installed_files_ref_cnt
;
412 newpkg
->installed_files
= NULL
;
415 if (!oldpkg
->essential
)
416 oldpkg
->essential
= newpkg
->essential
;
421 static void abstract_pkg_init(abstract_pkg_t
* ab_pkg
)
423 ab_pkg
->provided_by
= abstract_pkg_vec_alloc();
424 ab_pkg
->dependencies_checked
= 0;
425 ab_pkg
->state_status
= SS_NOT_INSTALLED
;
428 abstract_pkg_t
*abstract_pkg_new(void)
430 abstract_pkg_t
*ab_pkg
;
432 ab_pkg
= xcalloc(1, sizeof(abstract_pkg_t
));
433 abstract_pkg_init(ab_pkg
);
438 void set_flags_from_control(pkg_t
* pkg
)
443 sprintf_alloc(&file_name
, "%s/%s.control", pkg
->dest
->info_dir
,
446 fp
= fopen(file_name
, "r");
448 opkg_perror(ERROR
, "Failed to open %s", file_name
);
455 if (pkg_parse_from_stream(pkg
, fp
, PFM_ALL
^ PFM_ESSENTIAL
)) {
457 "Unable to read control file for %s. May be empty.\n",
466 static const char *pkg_state_want_to_str(pkg_state_want_t sw
)
470 for (i
= 0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
471 if (pkg_state_want_map
[i
].value
== sw
) {
472 return pkg_state_want_map
[i
].str
;
476 opkg_msg(ERROR
, "Internal error: state_want=%d\n", sw
);
477 return "<STATE_WANT_UNKNOWN>";
480 pkg_state_want_t
pkg_state_want_from_str(char *str
)
484 for (i
= 0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
485 if (strcmp(str
, pkg_state_want_map
[i
].str
) == 0) {
486 return pkg_state_want_map
[i
].value
;
490 opkg_msg(ERROR
, "Internal error: state_want=%s\n", str
);
494 static char *pkg_state_flag_to_str(pkg_state_flag_t sf
)
500 /* clear the temporary flags before converting to string */
501 sf
&= SF_NONVOLATILE_FLAGS
;
504 return xstrdup("ok");
507 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
508 if (sf
& pkg_state_flag_map
[i
].value
)
509 len
+= strlen(pkg_state_flag_map
[i
].str
) + 1;
512 str
= xmalloc(len
+ 1);
515 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
516 if (sf
& pkg_state_flag_map
[i
].value
) {
517 strncat(str
, pkg_state_flag_map
[i
].str
, len
);
518 strncat(str
, ",", len
);
523 str
[len
- 1] = '\0'; /* squash last comma */
528 pkg_state_flag_t
pkg_state_flag_from_str(const char *str
)
533 unsigned int sfname_len
;
535 if (strcmp(str
, "ok") == 0) {
538 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
539 sfname
= pkg_state_flag_map
[i
].str
;
540 sfname_len
= strlen(sfname
);
541 if (strncmp(str
, sfname
, sfname_len
) == 0) {
542 sf
|= pkg_state_flag_map
[i
].value
;
555 static const char *pkg_state_status_to_str(pkg_state_status_t ss
)
559 for (i
= 0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
560 if (pkg_state_status_map
[i
].value
== ss
) {
561 return pkg_state_status_map
[i
].str
;
565 opkg_msg(ERROR
, "Internal error: state_status=%d\n", ss
);
566 return "<STATE_STATUS_UNKNOWN>";
569 pkg_state_status_t
pkg_state_status_from_str(const char *str
)
573 for (i
= 0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
574 if (strcmp(str
, pkg_state_status_map
[i
].str
) == 0) {
575 return pkg_state_status_map
[i
].value
;
579 opkg_msg(ERROR
, "Internal error: state_status=%s\n", str
);
580 return SS_NOT_INSTALLED
;
583 void pkg_formatted_field(FILE * fp
, pkg_t
* pkg
, const char *field
)
588 int depends_count
= pkg
->pre_depends_count
+
589 pkg
->depends_count
+ pkg
->recommends_count
+ pkg
->suggests_count
;
591 if (strlen(field
) < PKG_MINIMUM_FIELD_NAME_LEN
) {
592 goto UNKNOWN_FMT_FIELD
;
598 if (strcasecmp(field
, "Architecture") == 0) {
599 p
= pkg_get_string(pkg
, PKG_ARCHITECTURE
);
601 fprintf(fp
, "Architecture: %s\n",
604 } else if (strcasecmp(field
, "Auto-Installed") == 0) {
605 if (pkg
->auto_installed
)
606 fprintf(fp
, "Auto-Installed: yes\n");
608 goto UNKNOWN_FMT_FIELD
;
613 if (strcasecmp(field
, "Conffiles") == 0) {
614 conffile_list_elt_t
*iter
;
616 if (nv_pair_list_empty(&pkg
->conffiles
))
619 fprintf(fp
, "Conffiles:\n");
620 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
;
621 iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
622 if (((conffile_t
*) iter
->data
)->name
623 && ((conffile_t
*) iter
->data
)->value
) {
624 fprintf(fp
, " %s %s\n",
625 ((conffile_t
*) iter
->data
)->
627 ((conffile_t
*) iter
->data
)->
631 } else if (strcasecmp(field
, "Conflicts") == 0) {
633 if (pkg
->conflicts_count
) {
634 fprintf(fp
, "Conflicts:");
635 for (i
= 0; i
< pkg
->conflicts_count
; i
++) {
637 pkg
->conflicts
[i
].possibilities
[0];
638 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
641 fprintf(fp
, " (%s%s)",
642 constraint_to_str(cdep
->
650 goto UNKNOWN_FMT_FIELD
;
655 if (strcasecmp(field
, "Depends") == 0) {
656 if (pkg
->depends_count
) {
657 fprintf(fp
, "Depends:");
658 for (j
= 0, i
= 0; i
< depends_count
; i
++) {
659 if (pkg
->depends
[i
].type
!= DEPEND
)
661 str
= pkg_depend_str(pkg
, i
);
662 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
669 } else if (strcasecmp(field
, "Description") == 0) {
670 p
= pkg_get_string(pkg
, PKG_DESCRIPTION
);
672 fprintf(fp
, "Description: %s\n",
676 goto UNKNOWN_FMT_FIELD
;
681 if (pkg
->essential
) {
682 fprintf(fp
, "Essential: yes\n");
687 p
= pkg_get_string(pkg
, PKG_FILENAME
);
689 fprintf(fp
, "Filename: %s\n", p
);
694 if (strcasecmp(field
, "Installed-Size") == 0) {
695 fprintf(fp
, "Installed-Size: %lu\n",
696 (unsigned long) pkg_get_int(pkg
, PKG_INSTALLED_SIZE
));
697 } else if (strcasecmp(field
, "Installed-Time") == 0) {
698 i
= pkg_get_int(pkg
, PKG_INSTALLED_TIME
);
700 fprintf(fp
, "Installed-Time: %lu\n",
707 if (strcasecmp(field
, "Maintainer") == 0) {
708 p
= pkg_get_string(pkg
, PKG_MAINTAINER
);
710 fprintf(fp
, "Maintainer: %s\n", p
);
712 } else if (strcasecmp(field
, "MD5sum") == 0) {
713 p
= pkg_get_string(pkg
, PKG_MD5SUM
);
715 fprintf(fp
, "MD5Sum: %s\n", p
);
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_get_string(pkg
, PKG_PRIORITY
));
727 } else if (strcasecmp(field
, "Provides") == 0) {
728 if (pkg
->provides_count
> 1) {
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 ? "" : ",",
766 goto UNKNOWN_FMT_FIELD
;
771 if (strcasecmp(field
, "Section") == 0) {
772 p
= pkg_get_string(pkg
, PKG_SECTION
);
774 fprintf(fp
, "Section: %s\n", p
);
776 #if defined HAVE_SHA256
777 } else if (strcasecmp(field
, "SHA256sum") == 0) {
778 p
= pkg_get_string(pkg
, PKG_SHA256SUM
);
780 fprintf(fp
, "SHA256sum: %s\n", p
);
783 } else if (strcasecmp(field
, "Size") == 0) {
784 i
= pkg_get_int(pkg
, PKG_SIZE
);
786 fprintf(fp
, "Size: %lu\n", (unsigned long) i
);
788 } else if (strcasecmp(field
, "Source") == 0) {
789 p
= pkg_get_string(pkg
, PKG_SOURCE
);
791 fprintf(fp
, "Source: %s\n", p
);
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 ? "" : ",",
815 goto UNKNOWN_FMT_FIELD
;
820 if (strcasecmp(field
, "Tags") == 0) {
821 p
= pkg_get_string(pkg
, PKG_TAGS
);
823 fprintf(fp
, "Tags: %s\n", p
);
830 char *version
= pkg_version_str_alloc(pkg
);
833 fprintf(fp
, "Version: %s\n", version
);
838 goto UNKNOWN_FMT_FIELD
;
844 opkg_msg(ERROR
, "Internal error: field=%s\n", field
);
847 void 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");
873 void 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) \
910 static int verrevcmp(const char *val
, const char *ref
)
917 while (*val
|| *ref
) {
920 while ((*val
&& !isdigit(*val
)) || (*ref
&& !isdigit(*ref
))) {
921 int vc
= order(*val
), rc
= order(*ref
);
932 while (isdigit(*val
) && isdigit(*ref
)) {
934 first_diff
= *val
- *ref
;
948 int pkg_compare_versions(const pkg_t
* pkg
, const pkg_t
* ref_pkg
)
950 unsigned int epoch1
= (unsigned int) pkg_get_int(pkg
, PKG_EPOCH
);
951 unsigned int epoch2
= (unsigned int) pkg_get_int(ref_pkg
, PKG_EPOCH
);
952 char *revision1
= pkg_get_ptr(pkg
, PKG_REVISION
);
953 char *revision2
= pkg_get_ptr(ref_pkg
, PKG_REVISION
);
954 const char *version1
= pkg_get_string(pkg
, PKG_VERSION
);
955 const char *version2
= pkg_get_string(ref_pkg
, PKG_VERSION
);
958 if (epoch1
> epoch2
) {
962 if (epoch1
< epoch2
) {
966 r
= verrevcmp(version1
, version2
);
971 r
= verrevcmp(revision1
, revision2
);
979 int pkg_version_satisfied(pkg_t
* it
, pkg_t
* ref
, const char *op
)
983 r
= pkg_compare_versions(it
, ref
);
985 if (strcmp(op
, "<=") == 0 || strcmp(op
, "<") == 0) {
989 if (strcmp(op
, ">=") == 0 || strcmp(op
, ">") == 0) {
993 if (strcmp(op
, "<<") == 0) {
997 if (strcmp(op
, ">>") == 0) {
1001 if (strcmp(op
, "=") == 0) {
1005 opkg_msg(ERROR
, "Unknown operator: %s.\n", op
);
1009 int pkg_name_version_and_architecture_compare(const void *p1
, const void *p2
)
1011 const pkg_t
* a
= *(const pkg_t
**)p1
;
1012 const pkg_t
* b
= *(const pkg_t
**)p2
;
1015 if (!a
->name
|| !b
->name
) {
1016 opkg_msg(ERROR
, "Internal error: a->name=%p, b->name=%p.\n",
1021 namecmp
= strcmp(a
->name
, b
->name
);
1024 vercmp
= pkg_compare_versions(a
, b
);
1027 if (!a
->arch_priority
|| !b
->arch_priority
) {
1029 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1030 a
->arch_priority
, b
->arch_priority
);
1033 if (a
->arch_priority
> b
->arch_priority
)
1035 if (a
->arch_priority
< b
->arch_priority
)
1040 int abstract_pkg_name_compare(const void *p1
, const void *p2
)
1042 const abstract_pkg_t
*a
= *(const abstract_pkg_t
**)p1
;
1043 const abstract_pkg_t
*b
= *(const abstract_pkg_t
**)p2
;
1044 if (!a
->name
|| !b
->name
) {
1045 opkg_msg(ERROR
, "Internal error: a->name=%p b->name=%p.\n",
1049 return strcmp(a
->name
, b
->name
);
1052 char *pkg_version_str_alloc(pkg_t
* pkg
)
1055 char *version
, *revptr
;
1056 unsigned int epoch
= (unsigned int) pkg_get_int(pkg
, PKG_EPOCH
);
1058 revptr
= pkg_get_ptr(pkg
, PKG_REVISION
);
1059 verstr
= pkg_get_string(pkg
, PKG_VERSION
);
1063 sprintf_alloc(&version
, "%d:%s-%s",
1064 epoch
, verstr
, revptr
);
1066 sprintf_alloc(&version
, "%d:%s",
1070 sprintf_alloc(&version
, "%s-%s",
1073 version
= xstrdup(verstr
);
1080 * XXX: this should be broken into two functions
1082 str_list_t
*pkg_get_installed_files(pkg_t
* pkg
)
1085 char *list_file_name
= NULL
;
1086 FILE *list_file
= NULL
;
1088 char *installed_file_name
;
1089 unsigned int rootdirlen
= 0;
1090 int list_from_package
;
1091 const char *local_filename
;
1093 pkg
->installed_files_ref_cnt
++;
1095 if (pkg
->installed_files
) {
1096 return pkg
->installed_files
;
1099 pkg
->installed_files
= str_list_alloc();
1102 * For installed packages, look at the package.list file in the database.
1103 * For uninstalled packages, get the file list directly from the package.
1105 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
)
1106 list_from_package
= 1;
1108 list_from_package
= 0;
1110 if (list_from_package
) {
1111 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1113 if (!local_filename
) {
1114 return pkg
->installed_files
;
1116 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1117 file. In other words, change deb_extract so that it can
1118 simply return the file list as a char *[] rather than
1119 insisting on writing it to a FILE * as it does now. */
1120 sprintf_alloc(&list_file_name
, "%s/%s.list.XXXXXX",
1121 conf
->tmp_dir
, pkg
->name
);
1122 fd
= mkstemp(list_file_name
);
1124 opkg_perror(ERROR
, "Failed to make temp file %s.",
1126 free(list_file_name
);
1127 return pkg
->installed_files
;
1129 list_file
= fdopen(fd
, "r+");
1130 if (list_file
== NULL
) {
1131 opkg_perror(ERROR
, "Failed to fdopen temp file %s.",
1134 unlink(list_file_name
);
1135 free(list_file_name
);
1136 return pkg
->installed_files
;
1138 err
= pkg_extract_data_file_names_to_stream(pkg
, list_file
);
1140 opkg_msg(ERROR
, "Error extracting file list from %s.\n",
1143 unlink(list_file_name
);
1144 free(list_file_name
);
1145 str_list_deinit(pkg
->installed_files
);
1146 pkg
->installed_files
= NULL
;
1151 sprintf_alloc(&list_file_name
, "%s/%s.list",
1152 pkg
->dest
->info_dir
, pkg
->name
);
1153 list_file
= fopen(list_file_name
, "r");
1154 if (list_file
== NULL
) {
1155 opkg_perror(ERROR
, "Failed to open %s", list_file_name
);
1156 free(list_file_name
);
1157 return pkg
->installed_files
;
1159 free(list_file_name
);
1162 if (conf
->offline_root
)
1163 rootdirlen
= strlen(conf
->offline_root
);
1168 line
= file_read_line_alloc(list_file
);
1174 if (list_from_package
) {
1175 if (*file_name
== '.') {
1178 if (*file_name
== '/') {
1181 sprintf_alloc(&installed_file_name
, "%s%s",
1182 pkg
->dest
->root_dir
, file_name
);
1184 if (conf
->offline_root
&&
1185 strncmp(conf
->offline_root
, file_name
,
1187 sprintf_alloc(&installed_file_name
, "%s%s",
1188 conf
->offline_root
, file_name
);
1190 // already contains root_dir as header -> ABSOLUTE
1191 sprintf_alloc(&installed_file_name
, "%s",
1195 str_list_append(pkg
->installed_files
, installed_file_name
);
1196 free(installed_file_name
);
1202 if (list_from_package
) {
1203 unlink(list_file_name
);
1204 free(list_file_name
);
1207 return pkg
->installed_files
;
1210 /* XXX: CLEANUP: This function and it's counterpart,
1211 (pkg_get_installed_files), do not match our init/deinit naming
1212 convention. Nor the alloc/free convention. But, then again, neither
1213 of these conventions currrently fit the way these two functions
1215 void pkg_free_installed_files(pkg_t
* pkg
)
1217 pkg
->installed_files_ref_cnt
--;
1219 if (pkg
->installed_files_ref_cnt
> 0)
1222 if (pkg
->installed_files
) {
1223 str_list_purge(pkg
->installed_files
);
1226 pkg
->installed_files
= NULL
;
1229 void pkg_remove_installed_files_list(pkg_t
* pkg
)
1231 char *list_file_name
;
1233 sprintf_alloc(&list_file_name
, "%s/%s.list",
1234 pkg
->dest
->info_dir
, pkg
->name
);
1236 if (!conf
->noaction
)
1237 (void)unlink(list_file_name
);
1239 free(list_file_name
);
1242 conffile_t
*pkg_get_conffile(pkg_t
* pkg
, const char *file_name
)
1244 conffile_list_elt_t
*iter
;
1245 conffile_t
*conffile
;
1251 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
;
1252 iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1253 conffile
= (conffile_t
*) iter
->data
;
1255 if (strcmp(conffile
->name
, file_name
) == 0) {
1263 int pkg_run_script(pkg_t
* pkg
, const char *script
, const char *args
)
1272 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1273 maintainer script within a chroot environment. */
1274 if (conf
->offline_root
&& !conf
->force_postinstall
) {
1275 opkg_msg(INFO
, "Offline root mode: not running %s.%s.\n",
1280 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1281 have scripts in pkg->tmp_unpack_dir. */
1282 if (pkg
->state_status
== SS_INSTALLED
1283 || pkg
->state_status
== SS_UNPACKED
) {
1284 if (pkg
->dest
== NULL
) {
1285 opkg_msg(ERROR
, "Internal error: %s has a NULL dest.\n",
1289 sprintf_alloc(&path
, "%s/%s.%s", pkg
->dest
->info_dir
, pkg
->name
,
1292 if (pkg
->tmp_unpack_dir
== NULL
) {
1294 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1298 sprintf_alloc(&path
, "%s/%s", pkg
->tmp_unpack_dir
, script
);
1301 opkg_msg(INFO
, "Running script %s.\n", path
);
1304 pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
,
1307 if (pkg
->is_upgrade
)
1308 setenv("PKG_UPGRADE", "1", 1);
1310 setenv("PKG_UPGRADE", "0", 1);
1312 if (!file_exists(path
)) {
1317 sprintf_alloc(&cmd
, "%s %s", path
, args
);
1320 const char *argv
[] = { "sh", "-c", cmd
, NULL
};
1321 err
= xsystem(argv
);
1327 "package \"%s\" %s script returned status %d.\n",
1328 pkg
->name
, script
, err
);
1335 int pkg_arch_supported(pkg_t
* pkg
)
1337 nv_pair_list_elt_t
*l
;
1338 char *architecture
= pkg_get_string(pkg
, PKG_ARCHITECTURE
);
1343 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
1344 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
1345 if (strcmp(nv
->name
, architecture
) == 0) {
1347 "Arch %s (priority %s) supported for pkg %s.\n",
1348 nv
->name
, nv
->value
, pkg
->name
);
1353 opkg_msg(DEBUG
, "Arch %s unsupported for pkg %s.\n",
1354 architecture
, pkg
->name
);
1358 void pkg_info_preinstall_check(void)
1361 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1363 /* update the file owner data structure */
1364 opkg_msg(INFO
, "Updating file owner list.\n");
1365 pkg_hash_fetch_all_installed(installed_pkgs
);
1366 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1367 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1368 str_list_t
*installed_files
= pkg_get_installed_files(pkg
); /* this causes installed_files to be cached */
1369 str_list_elt_t
*iter
, *niter
;
1370 if (installed_files
== NULL
) {
1371 opkg_msg(ERROR
, "Failed to determine installed "
1372 "files for pkg %s.\n", pkg
->name
);
1375 for (iter
= str_list_first(installed_files
), niter
=
1376 str_list_next(installed_files
, iter
); iter
;
1377 iter
= niter
, niter
=
1378 str_list_next(installed_files
, iter
)) {
1379 char *installed_file
= (char *)iter
->data
;
1380 file_hash_set_file_owner(installed_file
, pkg
);
1382 pkg_free_installed_files(pkg
);
1384 pkg_vec_free(installed_pkgs
);
1387 struct pkg_write_filelist_data
{
1393 pkg_write_filelist_helper(const char *key
, void *entry_
, void *data_
)
1395 struct pkg_write_filelist_data
*data
= data_
;
1396 pkg_t
*entry
= entry_
;
1397 if (entry
== data
->pkg
) {
1398 fprintf(data
->stream
, "%s\n", key
);
1402 int pkg_write_filelist(pkg_t
* pkg
)
1404 struct pkg_write_filelist_data data
;
1405 char *list_file_name
;
1407 sprintf_alloc(&list_file_name
, "%s/%s.list",
1408 pkg
->dest
->info_dir
, pkg
->name
);
1410 opkg_msg(INFO
, "Creating %s file for pkg %s.\n",
1411 list_file_name
, pkg
->name
);
1413 data
.stream
= fopen(list_file_name
, "w");
1415 opkg_perror(ERROR
, "Failed to open %s", list_file_name
);
1416 free(list_file_name
);
1421 hash_table_foreach(&conf
->file_hash
, pkg_write_filelist_helper
, &data
);
1422 fclose(data
.stream
);
1423 free(list_file_name
);
1425 pkg
->state_flag
&= ~SF_FILELIST_CHANGED
;
1430 int pkg_write_changed_filelists(void)
1432 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1433 int i
, err
, ret
= 0;
1438 opkg_msg(INFO
, "Saving changed filelists.\n");
1440 pkg_hash_fetch_all_installed(installed_pkgs
);
1441 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1442 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1443 if (pkg
->state_flag
& SF_FILELIST_CHANGED
) {
1444 err
= pkg_write_filelist(pkg
);
1450 pkg_vec_free(installed_pkgs
);