pkg: convert most other struct members into dynamic blob buffer fields
[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 #include <libubox/blob.h>
25
26 #include "pkg_vec.h"
27 #include "str_list.h"
28 #include "active_list.h"
29 #include "pkg_src.h"
30 #include "pkg_dest.h"
31 #include "opkg_conf.h"
32 #include "conffile_list.h"
33
34 struct opkg_conf;
35
36 #ifndef ARRAY_SIZE
37 #define ARRAY_SIZE(array) sizeof(array) / sizeof((array)[0])
38 #endif
39
40 /* I think "Size" is currently the shortest field name */
41 #define PKG_MINIMUM_FIELD_NAME_LEN 4
42
43 enum pkg_state_want {
44         SW_UNKNOWN = 1,
45         SW_INSTALL,
46         SW_DEINSTALL,
47         SW_PURGE,
48         SW_LAST_STATE_WANT
49 };
50 typedef enum pkg_state_want pkg_state_want_t;
51
52 enum pkg_state_flag {
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         SS_NOT_INSTALLED = 1,
70         SS_UNPACKED,
71         SS_HALF_CONFIGURED,
72         SS_INSTALLED,
73         SS_HALF_INSTALLED,
74         SS_CONFIG_FILES,
75         SS_POST_INST_FAILED,
76         SS_REMOVAL_FAILED,
77         SS_LAST_STATE_STATUS
78 };
79 typedef enum pkg_state_status pkg_state_status_t;
80
81 enum pkg_fields {
82         PKG_MAINTAINER,
83         PKG_PRIORITY,
84         PKG_SOURCE,
85         PKG_TAGS,
86         PKG_SECTION,
87         PKG_EPOCH,
88         PKG_FILENAME,
89         PKG_LOCAL_FILENAME,
90         PKG_VERSION,
91         PKG_REVISION,
92         PKG_ARCHITECTURE,
93         PKG_ARCH_PRIORITY,
94         PKG_DESCRIPTION,
95         PKG_MD5SUM,
96         PKG_SHA256SUM,
97         PKG_SIZE,
98         PKG_INSTALLED_SIZE,
99         PKG_INSTALLED_TIME,
100         PKG_TMP_UNPACK_DIR,
101         PKG_REPLACES,
102         PKG_PROVIDES,
103         PKG_DEPENDS,
104         PKG_CONFLICTS,
105         PKG_CONFFILES,
106 };
107
108 struct abstract_pkg {
109         char *name;
110         int dependencies_checked;
111         pkg_vec_t *pkgs;
112         pkg_state_status_t state_status;
113         pkg_state_flag_t state_flag;
114
115         /* XXX: This should be abstract_pkg_vec_t for consistency. */
116         struct abstract_pkg **depended_upon_by;
117
118         abstract_pkg_vec_t *provided_by;
119         abstract_pkg_vec_t *replaced_by;
120 };
121
122 #include "pkg_depends.h"
123
124 /* XXX: CLEANUP: I'd like to clean up pkg_t in several ways:
125
126    The 3 version fields should go into a single version struct. (This
127    is especially important since, currently, pkg->version can easily
128    be mistaken for pkg_verson_str_alloc(pkg) although they are very
129    distinct. This has been the source of multiple bugs.
130
131    The 3 state fields could possibly also go into their own struct.
132
133    All fields which deal with lists of packages, (Depends,
134    Pre-Depends, Provides, Suggests, Recommends, Enhances), should each
135    be handled by a single struct in pkg_t
136
137    All string fields for which there is a small set of possible
138    values, (section, maintainer, architecture, maybe version?), that
139    are reused among different packages -- for all such packages we
140    should move from "char *"s to some atom datatype to share data
141    storage and use less memory. We might even do reference counting,
142    but probably not since most often we only create new pkg_t structs,
143    we don't often free them.  */
144 struct pkg {
145         char *name;
146         pkg_src_t *src;
147         pkg_dest_t *dest;
148         pkg_state_want_t state_want:3;
149         pkg_state_flag_t state_flag:10;
150         pkg_state_status_t state_status:4;
151         struct active_list list;        /* Used for installing|upgrading */
152
153         abstract_pkg_t *parent;
154
155         /* As pointer for lazy evaluation */
156         str_list_t *installed_files;
157         /* XXX: CLEANUP: I'd like to perhaps come up with a better
158            mechanism to avoid the problem here, (which is that the
159            installed_files list was being freed from an inner loop while
160            still being used within an outer loop. */
161         int installed_files_ref_cnt;
162
163         int essential:1;
164 /* Adding this flag, to "force" opkg to choose a "provided_by_hand" package, if there are multiple choice */
165         int provided_by_hand:1;
166
167         /* this flag specifies whether the package was installed to satisfy another
168          * package's dependancies */
169         int auto_installed:1;
170         int is_upgrade:1;
171
172         struct blob_buf blob;
173 };
174
175 pkg_t *pkg_new(void);
176 void pkg_deinit(pkg_t * pkg);
177 int pkg_init_from_file(pkg_t * pkg, const char *filename);
178
179 void *pkg_set_raw(pkg_t *pkg, int id, const void *val, size_t len);
180 void *pkg_get_raw(const pkg_t *pkg, int id);
181
182 static inline int pkg_set_int(pkg_t *pkg, int id, int val)
183 {
184         return (intptr_t) pkg_set_raw(pkg, id, &val, sizeof(val));
185 }
186
187 static inline int pkg_get_int(const pkg_t *pkg, int id)
188 {
189         return (intptr_t) pkg_get_raw(pkg, id);
190 }
191
192 char *pkg_set_string(pkg_t *pkg, int id, const char *s);
193
194 static inline char *pkg_get_string(const pkg_t *pkg, int id)
195 {
196         return (char *) pkg_get_raw(pkg, id);
197 }
198
199 static inline void * pkg_set_ptr(pkg_t *pkg, int id, void *ptr)
200 {
201         return ptr ? *(void **) pkg_set_raw(pkg, id, &ptr, sizeof(ptr)) : NULL;
202 }
203
204 static inline void * pkg_get_ptr(const pkg_t *pkg, int id)
205 {
206         void **ptr = pkg_get_raw(pkg, id);
207         return ptr ? *ptr : NULL;
208 }
209
210 abstract_pkg_t *abstract_pkg_new(void);
211
212 /*
213  * merges fields from newpkg into oldpkg.
214  * Forcibly sets oldpkg state_status, state_want and state_flags
215  */
216 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg);
217
218 char *pkg_version_str_alloc(pkg_t * pkg);
219
220 int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg);
221 int pkg_name_version_and_architecture_compare(const void *a, const void *b);
222 int abstract_pkg_name_compare(const void *a, const void *b);
223
224 void pkg_formatted_info(FILE * fp, pkg_t * pkg);
225 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field);
226
227 void set_flags_from_control(pkg_t * pkg);
228
229 void pkg_print_status(pkg_t * pkg, FILE * file);
230 str_list_t *pkg_get_installed_files(pkg_t * pkg);
231 void pkg_free_installed_files(pkg_t * pkg);
232 void pkg_remove_installed_files_list(pkg_t * pkg);
233 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name);
234 int pkg_run_script(pkg_t * pkg, const char *script, const char *args);
235
236 /* enum mappings */
237 pkg_state_want_t pkg_state_want_from_str(char *str);
238 pkg_state_flag_t pkg_state_flag_from_str(const char *str);
239 pkg_state_status_t pkg_state_status_from_str(const char *str);
240
241 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op);
242
243 int pkg_arch_supported(pkg_t * pkg);
244 void pkg_info_preinstall_check(void);
245
246 int pkg_write_filelist(pkg_t * pkg);
247 int pkg_write_changed_filelists(void);
248
249 #endif