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