introduce the active_list for searching.
[oweals/opkg-lede.git] / libopkg / pkg.h
1 /* pkg.h - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
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.
11
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.
16 */
17
18 #ifndef PKG_H
19 #define PKG_H
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "pkg_vec.h"
26 #include "str_list.h"
27 #include "active_list.h"
28 #include "pkg_src.h"
29 #include "pkg_dest.h"
30 #include "opkg_conf.h"
31 #include "conffile_list.h"
32
33 struct opkg_conf;
34
35
36 #define ARRAY_SIZE(array) sizeof(array) / sizeof((array)[0])
37
38 /* I think "Size" is currently the shortest field name */
39 #define PKG_MINIMUM_FIELD_NAME_LEN 4
40
41 enum pkg_state_want
42 {
43     SW_UNKNOWN = 1,
44     SW_INSTALL,
45     SW_DEINSTALL,
46     SW_PURGE,
47     SW_LAST_STATE_WANT
48 };
49 typedef enum pkg_state_want pkg_state_want_t;
50
51 enum pkg_state_flag
52 {
53     SF_OK        = 0,
54     SF_REINSTREQ = 1,
55     SF_HOLD      = 2,  /* do not upgrade version */
56     SF_REPLACE   = 4,  /* replace this package */
57     SF_NOPRUNE   = 8,  /* do not remove obsolete files */
58     SF_PREFER    = 16, /* prefer this version */
59     SF_OBSOLETE  = 32, /* old package in upgrade pair */
60     SF_MARKED    = 64, /* temporary mark */
61     SF_FILELIST_CHANGED = 128, /* needs filelist written */
62     SF_USER      = 256,
63     SF_LAST_STATE_FLAG
64 };
65 typedef enum pkg_state_flag pkg_state_flag_t;
66 #define SF_NONVOLATILE_FLAGS (SF_HOLD|SF_NOPRUNE|SF_PREFER|SF_OBSOLETE|SF_USER)
67
68 enum pkg_state_status
69 {
70     SS_NOT_INSTALLED = 1,
71     SS_UNPACKED,
72     SS_HALF_CONFIGURED,
73     SS_INSTALLED,
74     SS_HALF_INSTALLED,
75     SS_CONFIG_FILES,
76     SS_POST_INST_FAILED,
77     SS_REMOVAL_FAILED,
78     SS_LAST_STATE_STATUS
79 };
80 typedef enum pkg_state_status pkg_state_status_t;
81
82 struct abstract_pkg{
83     char * name;
84     int dependencies_checked;
85     pkg_vec_t * pkgs;
86     pkg_state_status_t state_status;
87     pkg_state_flag_t state_flag;
88     struct abstract_pkg ** depended_upon_by; /* @@@@ this should be abstract_pkg_vec_t -Jamey */
89     abstract_pkg_vec_t * provided_by;
90     abstract_pkg_vec_t * replaced_by;
91     struct active_list   searched_node;   /* Used for hash search */
92 };
93
94 #include "pkg_depends.h"
95
96 /* XXX: CLEANUP: I'd like to clean up pkg_t in several ways:
97
98    The 3 version fields should go into a single version struct. (This
99    is especially important since, currently, pkg->version can easily
100    be mistaken for pkg_verson_str_alloc(pkg) although they are very
101    distinct. This has been the source of multiple bugs.
102
103    The 3 state fields could possibly also go into their own struct.
104
105    All fields which deal with lists of packages, (Depends,
106    Pre-Depends, Provides, Suggests, Recommends, Enhances), should each
107    be handled by a single struct in pkg_t
108
109    All string fields for which there is a small set of possible
110    values, (section, maintainer, architecture, maybe version?), that
111    are reused among different packages -- for all such packages we
112    should move from "char *"s to some atom datatype to share data
113    storage and use less memory. We might even do reference counting,
114    but probably not since most often we only create new pkg_t structs,
115    we don't often free them.  */
116 struct pkg
117 {
118      char *name;
119      unsigned long epoch;
120      char *version;
121      char *revision;
122      pkg_src_t *src;
123      pkg_dest_t *dest;
124      char *architecture;
125      char *section;
126      char *maintainer;
127      char *description;
128      char *tags;
129      pkg_state_want_t state_want;
130      pkg_state_flag_t state_flag;
131      pkg_state_status_t state_status;
132      char **depends_str;
133      int depends_count;
134      char **pre_depends_str;
135      int pre_depends_count;
136      char **recommends_str;
137      int recommends_count;
138      char **suggests_str;
139      int suggests_count;
140      struct active_list list; /* Used for installing|upgrading */
141      struct active_list searched_node;  /* Used for searching */
142      compound_depend_t * depends;
143
144      /* Abhaya: new conflicts */
145      char **conflicts_str;
146      compound_depend_t * conflicts;
147      int conflicts_count;
148         
149      char **replaces_str;
150      int replaces_count;
151      abstract_pkg_t ** replaces;
152
153      char **provides_str;
154      int provides_count;
155      abstract_pkg_t ** provides;
156
157      abstract_pkg_t *parent;
158
159      pkg_t *old_pkg; /* during upgrade, points from installee to previously installed */
160
161      char *filename;
162      char *local_filename;
163      char *url;
164      char *tmp_unpack_dir;
165      char *md5sum;
166      char *size;
167      char *installed_size;
168      char *priority;
169      char *source;
170      conffile_list_t conffiles;
171      time_t installed_time;
172      /* As pointer for lazy evaluation */
173      str_list_t *installed_files;
174      /* XXX: CLEANUP: I'd like to perhaps come up with a better
175         mechanism to avoid the problem here, (which is that the
176         installed_files list was being freed from an inner loop while
177         still being used within an outer loop. */
178      int installed_files_ref_cnt;
179      int essential;
180      int arch_priority;
181 /* Adding this flag, to "force" opkg to choose a "provided_by_hand" package, if there are multiple choice */
182      int provided_by_hand;
183
184      /* this flag specifies whether the package was installed to satisfy another
185       * package's dependancies */
186      int auto_installed;
187 };
188
189 pkg_t *pkg_new(void);
190 int pkg_init(pkg_t *pkg);
191 void pkg_deinit(pkg_t *pkg);
192 int pkg_init_from_file(pkg_t *pkg, const char *filename);
193 abstract_pkg_t *abstract_pkg_new(void);
194 int abstract_pkg_init(abstract_pkg_t *ab_pkg);
195
196 /* 
197  * merges fields from newpkg into oldpkg.
198  * Forcibly sets oldpkg state_status, state_want and state_flags if set_status is nonzero 
199  */
200 int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status);
201
202 char *pkg_version_str_alloc(pkg_t *pkg);
203
204 int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg);
205 int pkg_name_version_and_architecture_compare(const void *a, const void *b);
206 int abstract_pkg_name_compare(const void *a, const void *b);
207
208 char * pkg_formatted_info(pkg_t *pkg );
209 char * pkg_formatted_field(pkg_t *pkg, const char *field );
210
211 void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg);
212
213 void pkg_print_info(pkg_t *pkg, FILE *file);
214 void pkg_print_status(pkg_t * pkg, FILE * file);
215 void pkg_print_field(pkg_t *pkg, FILE *file, const char *field);
216 str_list_t *pkg_get_installed_files(pkg_t *pkg);
217 int pkg_free_installed_files(pkg_t *pkg);
218 int pkg_remove_installed_files_list(opkg_conf_t *conf, pkg_t *pkg);
219 conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name);
220 int pkg_run_script(struct opkg_conf *conf, pkg_t *pkg,
221                    const char *script, const char *args);
222
223 /* enum mappings */
224 char *pkg_state_want_to_str(pkg_state_want_t sw);
225 pkg_state_want_t pkg_state_want_from_str(char *str);
226 char *pkg_state_flag_to_str(pkg_state_flag_t sf);
227 pkg_state_flag_t pkg_state_flag_from_str(const char *str);
228 char *pkg_state_status_to_str(pkg_state_status_t ss);
229 pkg_state_status_t pkg_state_status_from_str(const char *str);
230
231 int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op);
232
233 int pkg_arch_supported(opkg_conf_t *conf, pkg_t *pkg);
234 int pkg_info_preinstall_check(opkg_conf_t *conf);
235 int pkg_free_installed_files(pkg_t *pkg);
236
237 int pkg_write_filelist(opkg_conf_t *conf, pkg_t *pkg);
238 int pkg_write_changed_filelists(opkg_conf_t *conf);
239
240 #endif