1 /*
2  * libdpkg - Debian packaging suite library routines
3  * dpkg-db.h - declarations for in-core package database management
4  *
5  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2000,2001 Wichert Akkerman
7  * Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
8  *
9  * This is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef LIBDPKG_DPKG_DB_H
24 #define LIBDPKG_DPKG_DB_H
25 
26 #include <sys/types.h>
27 
28 #include <stdbool.h>
29 #include <stdio.h>
30 
31 #include <dpkg/macros.h>
32 #include <dpkg/varbuf.h>
33 #include <dpkg/version.h>
34 #include <dpkg/arch.h>
35 
36 DPKG_BEGIN_DECLS
37 
38 /**
39  * @defgroup dpkg-db In-core package database management
40  * @ingroup dpkg-public
41  * @{
42  */
43 
44 enum deptype {
45   dep_suggests,
46   dep_recommends,
47   dep_depends,
48   dep_predepends,
49   dep_breaks,
50   dep_conflicts,
51   dep_provides,
52   dep_replaces,
53   dep_enhances
54 };
55 
56 struct dependency {
57   struct pkginfo *up;
58   struct dependency *next;
59   struct deppossi *list;
60   enum deptype type;
61 };
62 
63 struct deppossi {
64   struct dependency *up;
65   struct pkgset *ed;
66   struct deppossi *next, *rev_next, *rev_prev;
67   const struct dpkg_arch *arch;
68   struct dpkg_version version;
69   enum dpkg_relation verrel;
70   bool arch_is_implicit;
71   bool cyclebreak;
72 };
73 
74 struct arbitraryfield {
75   struct arbitraryfield *next;
76   const char *name;
77   const char *value;
78 };
79 
80 struct conffile {
81   struct conffile *next;
82   const char *name;
83   const char *hash;
84   bool obsolete;
85 };
86 
87 struct archivedetails {
88   struct archivedetails *next;
89   const char *name;
90   const char *msdosname;
91   const char *size;
92   const char *md5sum;
93 };
94 
95 enum pkgmultiarch {
96 	PKG_MULTIARCH_NO,
97 	PKG_MULTIARCH_SAME,
98 	PKG_MULTIARCH_ALLOWED,
99 	PKG_MULTIARCH_FOREIGN,
100 };
101 
102 /**
103  * Node describing a binary package file.
104  *
105  * This structure holds information contained on each binary package.
106  */
107 struct pkgbin {
108   struct dependency *depends;
109   /** The ‘essential’ flag, true = yes, false = no (absent). */
110   bool essential;
111   enum pkgmultiarch multiarch;
112   const struct dpkg_arch *arch;
113   /** The following is the "pkgname:archqual" cached string, if this was a
114    * C++ class this member would be mutable. */
115   const char *pkgname_archqual;
116   const char *description;
117   const char *maintainer;
118   const char *source;
119   const char *installedsize;
120   const char *origin;
121   const char *bugs;
122   struct dpkg_version version;
123   struct conffile *conffiles;
124   struct arbitraryfield *arbs;
125 };
126 
127 /**
128  * Node indicates that parent's Triggers-Pending mentions name.
129  *
130  * Note: These nodes do double duty: after they're removed from a package's
131  * trigpend list, references may be preserved by the trigger cycle checker
132  * (see trigproc.c).
133  */
134 struct trigpend {
135   struct trigpend *next;
136   const char *name;
137 };
138 
139 /**
140  * Node indicates that aw's Triggers-Awaited mentions pend.
141  */
142 struct trigaw {
143   struct pkginfo *aw, *pend;
144   struct trigaw *samepend_next;
145   struct {
146     struct trigaw *next, *prev;
147   } sameaw;
148 };
149 
150 /* Note: dselect and dpkg have different versions of this. */
151 struct perpackagestate;
152 
153 enum pkgwant {
154 	PKG_WANT_UNKNOWN,
155 	PKG_WANT_INSTALL,
156 	PKG_WANT_HOLD,
157 	PKG_WANT_DEINSTALL,
158 	PKG_WANT_PURGE,
159 	/** Not allowed except as special sentinel value in some places. */
160 	PKG_WANT_SENTINEL,
161 };
162 
163 enum pkgeflag {
164 	PKG_EFLAG_OK		= 0,
165 	PKG_EFLAG_REINSTREQ	= 1,
166 };
167 
168 enum pkgstatus {
169 	PKG_STAT_NOTINSTALLED,
170 	PKG_STAT_CONFIGFILES,
171 	PKG_STAT_HALFINSTALLED,
172 	PKG_STAT_UNPACKED,
173 	PKG_STAT_HALFCONFIGURED,
174 	PKG_STAT_TRIGGERSAWAITED,
175 	PKG_STAT_TRIGGERSPENDING,
176 	PKG_STAT_INSTALLED,
177 };
178 
179 enum pkgpriority {
180 	PKG_PRIO_REQUIRED,
181 	PKG_PRIO_IMPORTANT,
182 	PKG_PRIO_STANDARD,
183 	PKG_PRIO_OPTIONAL,
184 	PKG_PRIO_EXTRA,
185 	PKG_PRIO_OTHER,
186 	PKG_PRIO_UNKNOWN,
187 	PKG_PRIO_UNSET = -1,
188 };
189 
190 /**
191  * Node describing an architecture package instance.
192  *
193  * This structure holds state information.
194  */
195 struct pkginfo {
196   struct pkgset *set;
197   struct pkginfo *arch_next;
198 
199   enum pkgwant want;
200   /** The error flag bitmask. */
201   enum pkgeflag eflag;
202   enum pkgstatus status;
203   enum pkgpriority priority;
204   const char *otherpriority;
205   const char *section;
206   struct dpkg_version configversion;
207   struct pkgbin installed;
208   struct pkgbin available;
209   struct perpackagestate *clientdata;
210 
211   struct archivedetails *archives;
212 
213   struct {
214     /* ->aw == this */
215     struct trigaw *head, *tail;
216   } trigaw;
217 
218   /* ->pend == this, non-NULL for us when Triggers-Pending. */
219   struct trigaw *othertrigaw_head;
220   struct trigpend *trigpend_head;
221 
222   /**
223    * files_list_valid  files  Meaning
224    * ----------------  -----  -------
225    * false             NULL   Not read yet, must do so if want them.
226    * false             !NULL  Read, but rewritten and now out of date. If want
227    *                          info must throw away old and reread file.
228    * true              !NULL  Read, all is OK.
229    * true              NULL   Read OK, but, there were no files.
230    */
231   struct fsys_namenode_list *files;
232   off_t files_list_phys_offs;
233   bool files_list_valid;
234 
235   /* The status has changed, it needs to be logged. */
236   bool status_dirty;
237 };
238 
239 /**
240  * Node describing a package set sharing the same package name.
241  */
242 struct pkgset {
243   struct pkgset *next;
244   const char *name;
245   struct pkginfo pkg;
246   struct {
247     struct deppossi *available;
248     struct deppossi *installed;
249   } depended;
250   int installed_instances;
251 };
252 
253 /*** from dbdir.c ***/
254 
255 const char *dpkg_db_set_dir(const char *dir);
256 const char *dpkg_db_get_dir(void);
257 char *dpkg_db_get_path(const char *pathpart);
258 
259 #include <dpkg/atomic-file.h>
260 
261 /*** from dbmodify.c ***/
262 
263 enum modstatdb_rw {
264   /* Those marked with \*s*\ are possible returns from modstatdb_init. */
265   msdbrw_readonly/*s*/, msdbrw_needsuperuserlockonly/*s*/,
266   msdbrw_writeifposs,
267   msdbrw_write/*s*/, msdbrw_needsuperuser,
268 
269   /* Now some optional flags (starting at bit 8): */
270   msdbrw_available_readonly	= DPKG_BIT(8),
271   msdbrw_available_write	= DPKG_BIT(9),
272   msdbrw_available_mask		= 0xff00,
273 };
274 
275 void modstatdb_init(void);
276 void modstatdb_done(void);
277 bool modstatdb_is_locked(void);
278 bool modstatdb_can_lock(void);
279 void modstatdb_lock(void);
280 void modstatdb_unlock(void);
281 enum modstatdb_rw modstatdb_open(enum modstatdb_rw reqrwflags);
282 enum modstatdb_rw modstatdb_get_status(void);
283 void modstatdb_note(struct pkginfo *pkg);
284 void modstatdb_note_ifwrite(struct pkginfo *pkg);
285 void modstatdb_checkpoint(void);
286 void modstatdb_shutdown(void);
287 
288 /*** from database.c ***/
289 
290 void pkgset_blank(struct pkgset *set);
291 int pkgset_installed_instances(struct pkgset *set);
292 
293 void pkg_blank(struct pkginfo *pp);
294 void pkgbin_blank(struct pkgbin *pkgbin);
295 bool pkg_is_informative(struct pkginfo *pkg, struct pkgbin *info);
296 
297 struct pkgset *
298 pkg_hash_find_set(const char *name);
299 struct pkginfo *
300 pkg_hash_get_singleton(struct pkgset *set);
301 struct pkginfo *
302 pkg_hash_find_singleton(const char *name);
303 struct pkginfo *
304 pkg_hash_get_pkg(struct pkgset *set, const struct dpkg_arch *arch);
305 struct pkginfo *
306 pkg_hash_find_pkg(const char *name, const struct dpkg_arch *arch);
307 int
308 pkg_hash_count_set(void);
309 int
310 pkg_hash_count_pkg(void);
311 void
312 pkg_hash_reset(void);
313 
314 struct pkg_hash_iter *
315 pkg_hash_iter_new(void);
316 struct pkgset *
317 pkg_hash_iter_next_set(struct pkg_hash_iter *iter);
318 struct pkginfo *
319 pkg_hash_iter_next_pkg(struct pkg_hash_iter *iter);
320 void
321 pkg_hash_iter_free(struct pkg_hash_iter *iter);
322 
323 void
324 pkg_hash_report(FILE *);
325 
326 /*** from parse.c ***/
327 
328 enum parsedbflags {
329   /** Parse a single control stanza. */
330   pdb_single_stanza		= DPKG_BIT(0),
331   /** Store in ‘available’ in-core structures, not ‘status’. */
332   pdb_recordavailable		= DPKG_BIT(1),
333   /** Throw up an error if ‘Status’ encountered. */
334   pdb_rejectstatus		= DPKG_BIT(2),
335   /** Ignore priority/section info if we already have any. */
336   pdb_weakclassification	= DPKG_BIT(3),
337   /** Ignore archives info if we already have them. */
338   pdb_ignore_archives		= DPKG_BIT(4),
339   /** Ignore packages with older versions already read. */
340   pdb_ignoreolder		= DPKG_BIT(5),
341   /** Perform laxer version parsing. */
342   pdb_lax_version_parser	= DPKG_BIT(6),
343   /** Perform laxer control stanza parsing. */
344   pdb_lax_stanza_parser		= DPKG_BIT(9),
345   /** Perform laxer parsing, used to transition to stricter parsing. */
346   pdb_lax_parser		= pdb_lax_stanza_parser | pdb_lax_version_parser,
347   /** Close file descriptor on context destruction. */
348   pdb_close_fd			= DPKG_BIT(7),
349   /** Interpret filename ‘-’ as stdin. */
350   pdb_dash_is_stdin		= DPKG_BIT(8),
351 
352   /* Standard operations. */
353 
354   pdb_parse_status		= pdb_lax_parser | pdb_weakclassification,
355   pdb_parse_update		= pdb_parse_status | pdb_single_stanza,
356   pdb_parse_available		= pdb_recordavailable | pdb_rejectstatus |
357 				  pdb_lax_parser,
358   pdb_parse_binary		= pdb_recordavailable | pdb_rejectstatus |
359 				  pdb_single_stanza,
360 };
361 
362 const char *pkg_name_is_illegal(const char *p);
363 
364 const struct fieldinfo *
365 find_field_info(const struct fieldinfo *fields, const char *fieldname);
366 const struct arbitraryfield *
367 find_arbfield_info(const struct arbitraryfield *arbs, const char *fieldname);
368 
369 int parsedb(const char *filename, enum parsedbflags, struct pkginfo **donep);
370 void copy_dependency_links(struct pkginfo *pkg,
371                            struct dependency **updateme,
372                            struct dependency *newdepends,
373                            bool available);
374 
375 /*** from parsehelp.c ***/
376 
377 #include <dpkg/namevalue.h>
378 
379 extern const struct namevalue booleaninfos[];
380 extern const struct namevalue multiarchinfos[];
381 extern const struct namevalue priorityinfos[];
382 extern const struct namevalue statusinfos[];
383 extern const struct namevalue eflaginfos[];
384 extern const struct namevalue wantinfos[];
385 
386 #include <dpkg/error.h>
387 
388 enum versiondisplayepochwhen { vdew_never, vdew_nonambig, vdew_always };
389 void varbufversion(struct varbuf *, const struct dpkg_version *,
390                    enum versiondisplayepochwhen);
391 int parseversion(struct dpkg_version *version, const char *,
392                  struct dpkg_error *err);
393 const char *versiondescribe(const struct dpkg_version *,
394                             enum versiondisplayepochwhen);
395 
396 enum pkg_name_arch_when {
397   /** Never display arch. */
398   pnaw_never,
399   /** Display arch only when it's non-ambiguous. */
400   pnaw_nonambig,
401   /** Display arch only when it's a foreign one. */
402   pnaw_foreign,
403   /** Always display arch. */
404   pnaw_always,
405 };
406 
407 void varbuf_add_pkgbin_name(struct varbuf *vb, const struct pkginfo *pkg,
408                             const struct pkgbin *pkgbin,
409                             enum pkg_name_arch_when pnaw);
410 
411 const char *
412 pkgbin_name_archqual(const struct pkginfo *pkg, const struct pkgbin *pkgbin);
413 
414 const char *
415 pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
416             enum pkg_name_arch_when pnaw);
417 const char *
418 pkg_name(struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
419 
420 const char *
421 pkgbin_name_const(const struct pkginfo *pkg, const struct pkgbin *pkgbin,
422                   enum pkg_name_arch_when pnaw);
423 const char *
424 pkg_name_const(const struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
425 
426 void
427 pkg_source_version(struct dpkg_version *version,
428                    const struct pkginfo *pkg, const struct pkgbin *pkgbin);
429 
430 void
431 varbuf_add_source_version(struct varbuf *vb,
432                           const struct pkginfo *pkg, const struct pkgbin *pkgbin);
433 
434 const char *pkg_want_name(const struct pkginfo *pkg);
435 const char *pkg_status_name(const struct pkginfo *pkg);
436 const char *pkg_eflag_name(const struct pkginfo *pkg);
437 
438 const char *pkg_priority_name(const struct pkginfo *pkg);
439 
440 /*** from dump.c ***/
441 
442 void writerecord(FILE*, const char*,
443                  const struct pkginfo *, const struct pkgbin *);
444 
445 enum writedb_flags {
446   /** Dump ‘available’ in-core structures, not ‘status’. */
447   wdb_dump_available		= DPKG_BIT(0),
448   /** Must sync the written file. */
449   wdb_must_sync			= DPKG_BIT(1),
450 };
451 
452 void writedb_records(FILE *fp, const char *filename, enum writedb_flags flags);
453 void writedb(const char *filename, enum writedb_flags flags);
454 
455 /* Note: The varbufs must have been initialized and will not be
456  * NUL-terminated. */
457 void varbufrecord(struct varbuf *, const struct pkginfo *,
458                   const struct pkgbin *);
459 void varbufdependency(struct varbuf *vb, struct dependency *dep);
460 
461 /*** from depcon.c ***/
462 
463 bool versionsatisfied(struct pkgbin *it, struct deppossi *against);
464 bool deparchsatisfied(struct pkgbin *it, const struct dpkg_arch *arch,
465                       struct deppossi *against);
466 bool archsatisfied(struct pkgbin *it, struct deppossi *against);
467 
468 bool
469 pkg_virtual_deppossi_satisfied(struct deppossi *dependee,
470                                struct deppossi *provider);
471 
472 /*** from nfmalloc.c ***/
473 void *nfmalloc(size_t);
474 char *nfstrsave(const char*);
475 char *nfstrnsave(const char*, size_t);
476 void nffreeall(void);
477 
478 /** @} */
479 
480 DPKG_END_DECLS
481 
482 #endif /* LIBDPKG_DPKG_DB_H */
483