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