1/*-
2 * Copyright (c) 2011-2016 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
4 * Copyright (c) 2011 Will Andrews <will@FreeBSD.org>
5 * Copyright (c) 2011 Philippe Pepiot <phil@philpep.org>
6 * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
7 * Copyright (c) 2013-2014 Matthew Seaman <matthew@FreeBSD.org>
8 * Copyright (c) 2014-2016 Vsevolod Stakhov <vsevolod@FreeBSD.org>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer
16 *    in this position and unchanged.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef _PKG_H
34#define _PKG_H
35
36#ifdef __cplusplus
37extern "C" {
38#define restrict
39#endif
40
41#include <sys/types.h>
42#include <sys/param.h>
43#include <sys/cdefs.h>
44#include <stdarg.h>
45#include <stdbool.h>
46#include <stdio.h>
47
48/* The expected name of the pkg(8) binary executable. */
49#ifndef PKG_EXEC_NAME
50#define PKG_EXEC_NAME	"pkg"
51#endif
52
53/* The expected name of the pkg-static(8) binary */
54#ifndef PKG_STATIC_NAME
55#define PKG_STATIC_NAME	"pkg-static"
56#endif
57
58#define PKGVERSION "@VERSION@"
59
60/* PORTVERSION equivalent for proper pkg-static->ports-mgmt/pkg
61 * version comparison in pkgdb_query_newpkgversion() */
62
63#define PKG_PORTVERSION "@VERSION@"
64
65/* The OS major version at the time of compilation */
66#ifdef __FreeBSD__
67#define OSMAJOR	__FreeBSD__
68#endif
69
70/* Not supported under DragonFly */
71#ifdef __DragonFly__
72#undef OSMAJOR
73#endif
74
75#ifdef __NetBSD_Version__
76#define OSMAJOR ((__NetBSD_Version__ + 1000000) / 100000000)
77#endif
78
79#ifndef __DECONST
80#define __DECONST(type, var)    ((type)(uintptr_t)(const void *)(var))
81#endif
82
83#ifndef NELEM
84#define NELEM(array)    (sizeof(array) / sizeof((array)[0]))
85#endif
86
87/* Special exit status for worker processes indicating that a restart
88 * is desired -- eg. after a child has updated pkg(8) itself.
89 */
90
91#define EX_NEEDRESTART	4
92
93struct pkg;
94struct pkg_dep;
95struct pkg_conflict;
96struct pkg_file;
97struct pkg_dir;
98struct pkg_option;
99struct pkg_license;
100struct pkg_config_file;
101struct pkg_create;
102
103struct pkgdb;
104struct pkgdb_it;
105
106struct pkg_jobs;
107struct pkg_solve_problem;
108
109struct pkg_repo;
110
111struct pkg_plugin;
112
113struct pkg_manifest_key;
114struct pkg_manifest_parser;
115
116typedef struct ucl_object_s pkg_object;
117typedef void * pkg_iter;
118
119struct pkg_kv {
120	char *key;
121	char *value;
122	struct pkg_kv *next, *prev;
123};
124
125/**
126 * The system-wide pkg(8) status: ie. is it a) installed or otherwise
127 * available on the sysem, b) database (local.sqlite) initialised and
128 * c) has at least one package installed (which should be pkg
129 * itself). PKG_STATUS_UNINSTALLED logically cannot be returned by
130 * pkg(8) itself, but it can be useful for the pkg bootstrapper
131 * /usr/bin/pkg or for applications that link against libpkg.so
132 */
133
134typedef enum {
135	PKG_STATUS_ACTIVE = 0,	/* pkg in use */
136	PKG_STATUS_NOPACKAGES,	/* local.sqlite empty */
137	PKG_STATUS_NODB,	/* local.sqlite not found, unreadable or not initialised */
138	PKG_STATUS_UNINSTALLED,	/* pkg not argv[0] or not on $PATH */
139} pkg_status_t;
140
141typedef enum {
142	/**
143	 * The license logic is OR (dual in the ports)
144	 */
145	LICENSE_OR = '|',
146	/**
147	 * The license logic is AND (multi in the ports)
148	 */
149	LICENSE_AND = '&',
150	/**
151	 * The license logic un single (default in the ports)
152	 */
153	LICENSE_SINGLE = 1U
154} lic_t;
155
156typedef enum {
157	PKGDB_DEFAULT = 0,
158	PKGDB_REMOTE,
159	PKGDB_MAYBE_REMOTE
160} pkgdb_t;
161
162typedef enum {
163	PKG_INIT_FLAG_USE_IPV4 = (1U << 0),
164	PKG_INIT_FLAG_USE_IPV6 = (1U << 1)
165} pkg_init_flags;
166
167/**
168 * Specify how an argument should be used by query functions.
169 */
170typedef enum {
171	/**
172	 * The argument does not matter, all items will be matched.
173	 */
174	MATCH_ALL,
175	/**
176	 * The argument is the exact pattern.  Match will be case
177	 * sensitive or case insensitive according to
178	 * pkgdb_case_sensitive()
179	 */
180	MATCH_EXACT,
181	/**
182	 * The argument is an exact pattern except that matches will
183	 * be made case insensitively.  Match is always case sensitive
184	 */
185	MATCH_GLOB,
186	/**
187	 * The argument is a regular expression ('modern' style
188	 * according to re_format(7).  Match will be case sensitive or
189	 * case insensitive according to pkgdb_case_sensitive()
190	 */
191	MATCH_REGEX,
192} match_t;
193
194/**
195 * Specify on which field the pattern will be matched uppon.
196 */
197
198typedef enum {
199	FIELD_NONE,
200	FIELD_ORIGIN,
201	FIELD_NAME,
202	FIELD_NAMEVER,
203	FIELD_COMMENT,
204	FIELD_DESC
205} pkgdb_field;
206
207/**
208 * The type of package.
209 */
210typedef enum {
211	/**
212	 * The pkg type can not be determined.
213	 */
214	PKG_NONE = 0,
215
216	/**
217	 * The pkg refers to a local file archive.
218	 */
219	PKG_FILE = (1U << 0),
220	/**
221	 * The pkg refers to data read from a non-regular file
222	 * (device, pipeline, unix dmain socket etc.)
223	 */
224	PKG_STREAM = (1U << 1),
225	/**
226	 * The pkg refers to a package available on the remote repository.
227	 * @todo Document which attributes are available.
228	 */
229	PKG_REMOTE = (1U << 2),
230	/**
231	 * The pkg refers to a localy installed package.
232	 */
233	PKG_INSTALLED = (1U << 3),
234	/**
235	 * The pkg refers to a local file old archive.
236	 */
237	PKG_OLD_FILE = (1U << 4),
238} pkg_t;
239
240/**
241 * Contains keys to refer to a string attribute.
242 * Used by pkg_get() and pkg_set()
243 */
244typedef enum {
245	PKG_ORIGIN = 1U,
246	PKG_NAME,
247	PKG_VERSION,
248	PKG_COMMENT,
249	PKG_DESC,
250	PKG_MTREE,
251	PKG_MESSAGE,
252	PKG_ARCH,
253	PKG_ABI,
254	PKG_MAINTAINER,
255	PKG_WWW,
256	PKG_PREFIX,
257	PKG_REPOPATH,
258	PKG_CKSUM,
259	PKG_OLD_VERSION,
260	PKG_REPONAME,
261	PKG_REPOURL,
262	PKG_DIGEST,
263	PKG_REASON,
264	PKG_FLATSIZE,
265	PKG_OLD_FLATSIZE,
266	PKG_PKGSIZE,
267	PKG_LICENSE_LOGIC,
268	PKG_AUTOMATIC,
269	PKG_LOCKED,
270	PKG_ROWID,
271	PKG_TIME,
272	PKG_ANNOTATIONS,
273	PKG_UNIQUEID,
274	PKG_OLD_DIGEST,
275	PKG_DEP_FORMULA,
276	PKG_VITAL,
277	PKG_NUM_FIELDS,		/* end of fields */
278} pkg_attr;
279
280typedef enum {
281	PKG_SET_FLATSIZE = 1U,
282	PKG_SET_AUTOMATIC,
283	PKG_SET_LOCKED,
284	PKG_SET_DEPORIGIN,
285	PKG_SET_ORIGIN,
286	PKG_SET_DEPNAME,
287	PKG_SET_NAME,
288	PKG_SET_VITAL,
289	PKG_SET_MAX
290} pkg_set_attr;
291
292/**
293 * contains keys to refer to a string attribute
294 * Used by pkg_dep_get()
295 */
296typedef enum {
297	PKG_DEP_NAME = 0,
298	PKG_DEP_ORIGIN,
299	PKG_DEP_VERSION
300} pkg_dep_attr;
301
302typedef enum {
303	PKG_DEPS = 0,
304	PKG_RDEPS,
305	PKG_OPTIONS,
306	PKG_FILES,
307	PKG_DIRS,
308	PKG_USERS,
309	PKG_GROUPS,
310	PKG_SHLIBS_REQUIRED,
311	PKG_SHLIBS_PROVIDED,
312	PKG_CONFLICTS,
313	PKG_PROVIDES,
314	PKG_CONFIG_FILES,
315	PKG_REQUIRES,
316	PKG_CATEGORIES,
317	PKG_LICENSES
318} pkg_list;
319
320typedef enum {
321	SRV,
322	HTTP,
323	NOMIRROR,
324} mirror_t;
325
326typedef enum {
327	SIG_NONE = 0,
328	SIG_PUBKEY,
329	SIG_FINGERPRINT
330} signature_t;
331
332/**
333 * Determine the type of a pkg_script.
334 */
335typedef enum {
336	PKG_SCRIPT_PRE_INSTALL = 0,
337	PKG_SCRIPT_POST_INSTALL,
338	PKG_SCRIPT_PRE_DEINSTALL,
339	PKG_SCRIPT_POST_DEINSTALL,
340	__DO_NOT_USE_ME1,
341	__DO_NOT_USE_ME2,
342	PKG_SCRIPT_INSTALL,
343	PKG_SCRIPT_DEINSTALL,
344	__DO_NOT_USE_ME3,
345	PKG_SCRIPT_UNKNOWN
346} pkg_script;
347
348/**
349 * Determine the type of a pkg_lua_script.
350 */
351typedef enum {
352	PKG_LUA_PRE_INSTALL = 0,
353	PKG_LUA_POST_INSTALL,
354	PKG_LUA_PRE_DEINSTALL,
355	PKG_LUA_POST_DEINSTALL,
356	PKG_LUA_UNKNOWN
357} pkg_lua_script;
358
359typedef enum _pkg_jobs_t {
360	PKG_JOBS_INSTALL,
361	PKG_JOBS_DEINSTALL,
362	PKG_JOBS_FETCH,
363	PKG_JOBS_AUTOREMOVE,
364	PKG_JOBS_UPGRADE,
365} pkg_jobs_t;
366
367typedef enum _pkg_flags {
368	PKG_FLAG_NONE = 0,
369	PKG_FLAG_DRY_RUN = (1U << 0),
370	PKG_FLAG_FORCE = (1U << 1),
371	PKG_FLAG_RECURSIVE = (1U << 2),
372	PKG_FLAG_AUTOMATIC = (1U << 3),
373	PKG_FLAG_WITH_DEPS = (1U << 4),
374	PKG_FLAG_NOSCRIPT = (1U << 5),
375	PKG_FLAG_PKG_VERSION_TEST = (1U << 6),
376	PKG_FLAG_UPGRADES_FOR_INSTALLED = (1U << 7),
377	PKG_FLAG_SKIP_INSTALL = (1U << 8),
378	PKG_FLAG_FORCE_MISSING = (1U << 9),
379	PKG_FLAG_FETCH_MIRROR = (1U << 10),
380	PKG_FLAG_USE_IPV4 = (1U << 11),
381	PKG_FLAG_USE_IPV6 = (1U << 12),
382	PKG_FLAG_UPGRADE_VULNERABLE = (1U << 13)
383} pkg_flags;
384
385typedef enum _pkg_stats_t {
386	PKG_STATS_LOCAL_COUNT = 0,
387	PKG_STATS_LOCAL_SIZE,
388	PKG_STATS_REMOTE_COUNT,
389	PKG_STATS_REMOTE_UNIQUE,
390	PKG_STATS_REMOTE_SIZE,
391	PKG_STATS_REMOTE_REPOS,
392} pkg_stats_t;
393
394typedef enum {
395	PKG_STRING = 0,
396	PKG_BOOL,
397	PKG_INT,
398	PKG_ARRAY,
399	PKG_OBJECT,
400	PKG_NULL
401} pkg_object_t;
402
403/**
404 * Keys for accessing pkg plugin data
405 */
406typedef enum _pkg_plugin_key {
407	PKG_PLUGIN_NAME = 0,
408	PKG_PLUGIN_DESC,
409	PKG_PLUGIN_VERSION,
410	PKG_PLUGIN_PLUGINFILE
411} pkg_plugin_key;
412
413/**
414 * Keys for hooking into the library
415 */
416typedef enum _pkg_plugin_hook_t {
417	PKG_PLUGIN_HOOK_PRE_INSTALL = 1,
418	PKG_PLUGIN_HOOK_POST_INSTALL,
419	PKG_PLUGIN_HOOK_PRE_DEINSTALL,
420	PKG_PLUGIN_HOOK_POST_DEINSTALL,
421	PKG_PLUGIN_HOOK_PRE_FETCH,
422	PKG_PLUGIN_HOOK_POST_FETCH,
423	PKG_PLUGIN_HOOK_EVENT,
424	PKG_PLUGIN_HOOK_PRE_UPGRADE,
425	PKG_PLUGIN_HOOK_POST_UPGRADE,
426	PKG_PLUGIN_HOOK_PRE_AUTOREMOVE,
427	PKG_PLUGIN_HOOK_POST_AUTOREMOVE,
428	PKG_PLUGIN_HOOK_PKGDB_CLOSE_RW,
429} pkg_plugin_hook_t;
430
431/**
432 * Error type used everywhere by libpkg.
433 */
434typedef enum {
435	EPKG_OK = 0,
436	/**
437	 * No more items available (end of the loop).
438	 */
439	EPKG_END,
440	EPKG_WARN,
441	/**
442	 * The function encountered a fatal error.
443	 */
444	EPKG_FATAL,
445	/**
446	 * Can not delete the package because it is required by
447	 * another package.
448	 */
449	EPKG_REQUIRED,
450	/**
451	 * Can not install the package because it is already installed.
452	 */
453	EPKG_INSTALLED,
454	/**
455	 * Can not install the package because some dependencies are
456	 * unresolved.
457	 */
458	EPKG_DEPENDENCY,
459	/**
460	 * Can not operate on package because it is locked
461	 */
462	EPKG_LOCKED,
463	/**
464	 * Can not create local database or database non-existent
465	 */
466	EPKG_ENODB,
467	/**
468	 * local file newer than remote
469	 */
470	EPKG_UPTODATE,
471	/**
472	 * unkown keyword
473	 */
474	EPKG_UNKNOWN,
475	/**
476	 * repo DB schema incompatible version
477	 */
478	EPKG_REPOSCHEMA,
479	/**
480	 * Insufficient privilege for action
481	 */
482	EPKG_ENOACCESS,
483	/**
484	 * Insecure permissions on any component of
485	 * $PKGDB_DIR/local.sqlite or any of the repo database bits
486	 */
487	EPKG_INSECURE,
488	/**
489	 * A conflict between packages found
490	 */
491	EPKG_CONFLICT,
492	/**
493	 * Need to repeat operation
494	 */
495	EPKG_AGAIN,
496	/**
497	 * Not installed
498	 */
499	EPKG_NOTINSTALLED,
500	/**
501	 * Can not delete the package because it is vital, i.e. a kernel
502	 */
503	EPKG_VITAL,
504	/**
505	 * the package already exist
506	 */
507	EPKG_EXIST
508} pkg_error_t;
509
510/**
511 * Upgrade, downgrade or reinstall?
512 */
513
514typedef enum {
515	PKG_DOWNGRADE = 0,
516	PKG_REINSTALL,
517	PKG_UPGRADE,
518} pkg_change_t;
519
520/**
521 * Locking types for database:
522 * `PKGDB_LOCK_READONLY`: lock for read only queries (can be nested)
523 * `PKGDB_LOCK_ADVISORY`: write to DB inside a transaction (allows `PKGDB_LOCK_READONLY`)
524 * `PKGDB_LOCK_EXCLUSIVE`: possibly destructive operations (does not allow other locks)
525 */
526typedef enum {
527	PKGDB_LOCK_READONLY,
528	PKGDB_LOCK_ADVISORY,
529	PKGDB_LOCK_EXCLUSIVE
530} pkgdb_lock_t;
531
532typedef enum {
533	PKG_SOLVED_INSTALL,
534	PKG_SOLVED_DELETE,
535	PKG_SOLVED_UPGRADE,
536	PKG_SOLVED_UPGRADE_REMOVE,
537	PKG_SOLVED_FETCH,
538	PKG_SOLVED_UPGRADE_INSTALL
539} pkg_solved_t;
540
541#define PKG_OPEN_MANIFEST_ONLY 0x1
542#define PKG_OPEN_MANIFEST_COMPACT (0x1 << 1)
543#define PKG_OPEN_TRY (0x1 << 2)
544
545/**
546 * test if pkg is installed and activated.
547 * @param count	If all the tests pass, and count is non-NULL,
548 * write the number of installed packages into *count
549 */
550pkg_status_t pkg_status(int *count);
551
552/**
553 * Allocate a new pkg.
554 * Allocated pkg must be deallocated by pkg_free().
555 */
556int pkg_new(struct pkg **, pkg_t type);
557
558/**
559 * Deallocate a pkg
560 */
561void pkg_free(struct pkg *);
562
563/**
564 * Check if a package is valid according to its type.
565 */
566int pkg_is_valid(const struct pkg * restrict);
567
568/**
569 * Open a package file archive and retrive informations.
570 * @param p A pointer to pkg allocated by pkg_new(), or if it points to a
571 * NULL pointer, the function allocate a new pkg using pkg_new().
572 * @param path The path to the local package archive.
573 * @param keys manifest keys that should be initialised
574 * @param flags open flags
575 */
576int pkg_open(struct pkg **p, const char *path, struct pkg_manifest_key *keys, int flags);
577int pkg_open_fd(struct pkg **p, int fd, struct pkg_manifest_key *keys, int flags);
578
579/**
580 * @return the type of the package.
581 * @warning returns PKG_NONE on error.
582 */
583pkg_t pkg_type(const struct pkg * restrict);
584
585/**
586 * Generic getter for simple attributes.
587 * @return NULL-terminated string.
588 * @warning May return a NULL pointer.
589 */
590int pkg_get2(struct pkg const *const, ...);
591#define pkg_get(pkg, ...) pkg_get2(pkg, __VA_ARGS__, -1)
592
593int pkg_list_count(const struct pkg *, pkg_list);
594
595/**
596 * Iterates over the dependencies of the package.
597 * @param dep Must be set to NULL for the first call.
598 * @return An error code.
599 */
600int pkg_deps(const struct pkg *, struct pkg_dep **dep);
601
602/**
603 * Iterates over the reverse dependencies of the package.
604 * That is, the packages which require this package.
605 * @param dep Must be set to NULL for the first call.
606 * @return An error code.
607 */
608int pkg_rdeps(const struct pkg *, struct pkg_dep **dep);
609
610/**
611 * Iterates over the files of the package.
612 * @param file Must be set to NULL for the first call.
613 * @return An error code.
614 */
615int pkg_files(const struct pkg *, struct pkg_file **file);
616
617/**
618 * Iterates over the directories of the package.
619 * @param Must be set to NULL for the first call.
620 * @return An error code.
621 */
622int pkg_dirs(const struct pkg *pkg, struct pkg_dir **dir);
623
624/**
625 * Iterates over the users of the package.
626 * @param Must be set to NULL for the first call.
627 * @return An error code.
628 */
629int pkg_users(const struct pkg *pkg, char **user);
630
631/**
632 * Iterates over the groups of the package.
633 * @param Must be set to NULL for the first call.
634 * @return An error code.
635 */
636int pkg_groups(const struct pkg *pkg, char **group);
637
638/**
639 * Iterates over the options of the package.
640 * @param  option Must be set to NULL for the first call.
641 * @return An error code.
642 */
643int pkg_options(const struct pkg *, struct pkg_option **option);
644
645/**
646 * Iterates over the shared libraries used by the package.
647 * @param shlib must be set to NULL for the first call.
648 * @return An error code
649 */
650int pkg_shlibs_required(const struct pkg *pkg, char **shlib);
651
652/**
653 * Iterates over the shared libraries provided by the package.
654 * @param shlib must be set to NULL for the first call.
655 * @return An error code
656 */
657int pkg_shlibs_provided(const struct pkg *pkg, char **shlib);
658
659/**
660 * Iterates over the conflicts registered in the package.
661 * @param conflict must be set to NULL for the first call.
662 * @return An error code
663 */
664int pkg_conflicts(const struct pkg *pkg, struct pkg_conflict **conflict);
665
666/**
667 * Iterates over the provides registered in the package.
668 * @param provide must be set to NULL for the first call.
669 * @return An error code
670 */
671int pkg_provides(const struct pkg *pkg, char **provide);
672int pkg_requires(const struct pkg *pkg, char **require);
673
674int pkg_categories(const struct pkg *pkg, char **category);
675int pkg_licenses(const struct pkg *pkg, char **licenses);
676
677/**
678 * Iterates over the config files registered in the package.
679 * @param provide must be set to NULL for the first call.
680 * @return An error code
681 */
682int pkg_config_files(const struct pkg *pkg, struct pkg_config_file **cf);
683
684/**
685 * Iterate over all of the files within the package pkg, ensuring the
686 * dependency list contains all applicable packages providing the
687 * shared objects used by pkg.
688 * Also add all the shared object into the shlibs.
689 * It respects the SHLIBS options from configuration
690 * @return An error code
691 */
692
693 /* Don't conflict with PKG_LOAD_* q.v. */
694#define PKG_CONTAINS_ELF_OBJECTS	(1U << 24)
695#define PKG_CONTAINS_STATIC_LIBS	(1U << 25)
696#define PKG_CONTAINS_LA			(1U << 26)
697
698int pkg_analyse_files(struct pkgdb *, struct pkg *, const char *);
699
700/**
701 * Generic setter for simple attributes.
702 */
703int pkg_set2(struct pkg *pkg, ...);
704#define pkg_set(pkg, ...) pkg_set2(pkg, __VA_ARGS__, -1)
705
706int pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...);
707#define pkgdb_set(db, pkg, ...) pkgdb_set2(db, pkg, __VA_ARGS__, -1)
708
709/**
710 * Set a new debug level used inside of pkg.
711 * @param debug_level Debug level between 0 (no debugging) and 4 (max debugging).
712 * @return Previous debug level.
713 */
714int64_t pkg_set_debug_level(int64_t debug_level);
715int pkg_set_rootdir(const char *rootdir);
716
717/**
718 * Allocate a new struct pkg and add it to the deps of pkg.
719 * @return An error code.
720 */
721int pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const
722			   char *version, bool locked);
723int pkg_addrdep(struct pkg *pkg, const char *name, const char *origin, const
724			   char *version, bool locked);
725
726
727/**
728 * Helper which call pkg_addscript() with the content of the file and
729 * with the correct type.
730 */
731int pkg_addscript_fileat(int fd, struct pkg *pkg, const char *path);
732int pkg_addluascript_fileat(int fd, struct pkg *pkg, const char *path);
733
734/**
735 * Parse a manifest and set the attributes of pkg accordingly.
736 * @param buf An NULL-terminated buffer containing the manifest data.
737 * @return An error code.
738 */
739int pkg_parse_manifest(struct pkg *pkg, const char *buf, size_t len, struct pkg_manifest_key *key);
740int pkg_parse_manifest_file(struct pkg *pkg, const char *, struct pkg_manifest_key *key);
741int pkg_parse_manifest_fileat(int fd, struct pkg *pkg, const char *, struct pkg_manifest_key *key);
742int pkg_manifest_keys_new(struct pkg_manifest_key **k);
743void pkg_manifest_keys_free(struct pkg_manifest_key *k);
744int pkg_manifest_parser_new(struct pkg_manifest_parser **p);
745void pkg_manifest_parser_free(struct pkg_manifest_parser *p);
746
747#define PKG_MANIFEST_EMIT_COMPACT 0x1
748#define PKG_MANIFEST_EMIT_NOFILES (0x1 << 1)
749#define PKG_MANIFEST_EMIT_PRETTY (0x1 << 2)
750#define PKG_MANIFEST_EMIT_JSON (0x1 << 3)
751#define PKG_MANIFEST_EMIT_UCL (0x1 << 4)
752#define PKG_MANIFEST_EMIT_LOCAL_METADATA (0x1 << 5)
753
754/**
755 * Emit a manifest according to the attributes of pkg.
756 * @param buf A pointer which will hold the allocated buffer containing the
757 * manifest. To be free'ed.
758 * @param flags Flags for manifest emitting.
759 * @param pdigest A pointer that will hold digest of manifest produced, ignored
760 * if NULL. To be free'ed if not NULL.
761 * @return An error code.
762 */
763int pkg_emit_manifest(struct pkg *pkg, char **buf, short flags, char **pdigest);
764int pkg_emit_manifest_file(struct pkg*, FILE *, short, char **pdigest);
765
766/* pkg_dep */
767const char *pkg_dep_get(struct pkg_dep const * const , const pkg_dep_attr);
768#define pkg_dep_name(d) pkg_dep_get(d, PKG_DEP_NAME)
769#define pkg_dep_origin(d) pkg_dep_get(d, PKG_DEP_ORIGIN)
770#define pkg_dep_version(d) pkg_dep_get(d, PKG_DEP_VERSION)
771bool pkg_dep_is_locked(struct pkg_dep const * const);
772
773bool pkg_has_dir(struct pkg *, const char *);
774bool pkg_has_file(struct pkg *, const char *);
775
776struct pkg_file *pkg_get_file(struct pkg *p, const char *path);
777struct pkg_dir *pkg_get_dir(struct pkg *p, const char *path);
778
779/* pkg_license */
780const char *pkg_license_name(struct pkg_license const * const);
781
782/* pkg_script */
783const char *pkg_script_get(struct pkg const * const, pkg_script);
784
785/**
786 * @param db A pointer to a struct pkgdb object
787 * @param origin Package origin
788 * @param pkg An allocated struct pkg or a pointer to a NULL pointer. In the
789 * last case, the function take care of the allocation.
790 * @param flags OR'ed PKG_LOAD_*
791 * @return EPKG_OK if the package is installed,
792 * and != EPKG_OK if the package is not installed or an error occurred
793 * Match will be case sensitive or insensitive depending on
794 * pkgdb_case_sensitive()
795 */
796int pkg_try_installed(struct pkgdb *db, const char *origin,
797		struct pkg **pkg, unsigned flags);
798
799/**
800 * @param db A pointer to a struct pkgdb object
801 * @param origin Package origin
802 * @return EPKG_OK if the package is installed,
803 * and != EPKG_OK if the package is not installed or an error occurred
804 * Match will be case sensitive or insensitive depending on
805 * pkgdb_case_sensitive()
806 */
807int pkg_is_installed(struct pkgdb *db, const char *name);
808
809/**
810 * Create a repository database.
811 * @param path The path where the repository live.
812 * @param output_dir The path where the package repository should be created.
813 * @param force If true, rebuild the repository catalogue from scratch
814 * @param filesite If true, create a list of all files in repo
815 * @param metafile Open meta from the specified file
816 */
817typedef int(pkg_password_cb)(char *, int, int, void*);
818int pkg_create_repo(char *path, const char *output_dir, bool filelist,
819	const char *metafile, bool hash, bool hash_symlink);
820int pkg_finish_repo(const char *output_dir, pkg_password_cb *cb, char **argv,
821    int argc, bool filelist);
822
823
824/**
825 * Test if the EUID has sufficient privilege to carry out some
826 * operation (mode is a bitmap indicating READ, WRITE, CREATE) on the
827 * databases indicated in the database bitmap.
828 */
829#define PKGDB_MODE_READ		(0x1<<0)
830#define PKGDB_MODE_WRITE	(0x1<<1)
831#define PKGDB_MODE_CREATE	(0x1<<2)
832
833#define PKGDB_DB_LOCAL		(0x1<<0)
834#define PKGDB_DB_REPO		(0x1<<1)
835int pkgdb_access(unsigned mode, unsigned database);
836
837/**
838 * Open the local package database.
839 * The db must be free'ed with pkgdb_close().
840 * @return An error code.
841 */
842int pkgdb_open(struct pkgdb **db, pkgdb_t type);
843
844/**
845 * Open the local package database and repositories, possibly
846 * overriding configured repositories and replacing with the given
847 * reponame if not NULL
848 * @return An error code
849 */
850int pkgdb_open_all(struct pkgdb **db, pkgdb_t type, const char *reponame);
851
852/**
853 * Locking functions
854 */
855int pkgdb_obtain_lock(struct pkgdb *db, pkgdb_lock_t type);
856int pkgdb_upgrade_lock(struct pkgdb *db, pkgdb_lock_t old_type, pkgdb_lock_t new_type);
857int pkgdb_downgrade_lock(struct pkgdb *db, pkgdb_lock_t old_type,
858    pkgdb_lock_t new_type);
859int pkgdb_release_lock(struct pkgdb *db, pkgdb_lock_t type);
860
861/**
862 * Transaction/savepoint handling.
863 * @param savepoint -- if NULL or an empty string, use BEGIN, ROLLBACK, COMMIT
864 * otherwise use SAVEPOINT, ROLLBACK TO, RELEASE.
865 * @return an error code.
866 */
867int pkgdb_transaction_begin(struct pkgdb *db, const char *savepoint);
868int pkgdb_transaction_commit(struct pkgdb *db, const char *savepoint);
869int pkgdb_transaction_rollback(struct pkgdb *db, const char *savepoint);
870
871/**
872 * Close and free the struct pkgdb.
873 */
874void pkgdb_close(struct pkgdb *db);
875
876/**
877 * Initialize the local cache of the remote database with indicies
878 */
879int pkgdb_remote_init(struct pkgdb *db, const char *reponame);
880
881/**
882 * Dump to or load from a backup copy of the main database file
883 * (local.sqlite)
884 */
885
886int pkgdb_dump(struct pkgdb *db, const char *dest);
887int pkgdb_load(struct pkgdb *db, const char *src);
888
889
890/**
891 * Set the case sensitivity flag on or off.  Defaults to
892 * true (case_sensitive)
893 */
894void pkgdb_set_case_sensitivity(bool);
895
896/**
897 * Query the state of the case sensitity setting.
898 */
899bool pkgdb_case_sensitive(void);
900
901/**
902 * Query the local package database.
903 * @param type Describe how pattern should be used.
904 * @warning Returns NULL on failure.
905 */
906struct pkgdb_it * pkgdb_query(struct pkgdb *db, const char *pattern,
907    match_t type);
908struct pkgdb_it * pkgdb_query_cond(struct pkgdb *db, const char *cond,
909    const char *pattern, match_t type);
910struct pkgdb_it * pkgdb_repo_query(struct pkgdb *db, const char *pattern,
911    match_t type, const char *reponame);
912struct pkgdb_it *pkgdb_repo_query_cond(struct pkgdb *db, const char *cond,
913	const char *pattern, match_t type, const char *reponame);
914struct pkgdb_it * pkgdb_repo_search(struct pkgdb *db, const char *pattern,
915    match_t type, pkgdb_field field, pkgdb_field sort, const char *reponame);
916
917/**
918 * @todo Return directly the struct pkg?
919 */
920struct pkgdb_it * pkgdb_query_which(struct pkgdb *db, const char *path, bool glob);
921
922struct pkgdb_it * pkgdb_query_shlib_require(struct pkgdb *db, const char *shlib);
923struct pkgdb_it * pkgdb_query_shlib_provide(struct pkgdb *db, const char *shlib);
924struct pkgdb_it * pkgdb_query_require(struct pkgdb *db, const char *req);
925struct pkgdb_it * pkgdb_query_provide(struct pkgdb *db, const char *req);
926
927struct pkgdb_it * pkgdb_rquery_provide(struct pkgdb *db,
928    const char *provide, const char *repo);
929
930/**
931 * Add/Modify/Delete an annotation for a package
932 * @param tag -- tag for the annotation
933 * @param value -- text of the annotation
934 * @return An error code
935 */
936int pkgdb_add_annotation(struct pkgdb *db, struct pkg *pkg,
937	const char *tag, const char *value);
938int pkgdb_modify_annotation(struct pkgdb *db, struct pkg *pkg,
939	const char *tag, const char *value);
940int pkgdb_delete_annotation(struct pkgdb *db, struct pkg *pkg,
941	const char *tag);
942
943#define PKG_LOAD_BASIC			0
944#define PKG_LOAD_DEPS			(1U << 0)
945#define PKG_LOAD_RDEPS			(1U << 1)
946#define PKG_LOAD_FILES			(1U << 2)
947#define PKG_LOAD_SCRIPTS		(1U << 3)
948#define PKG_LOAD_OPTIONS		(1U << 4)
949#define PKG_LOAD_DIRS			(1U << 5)
950#define PKG_LOAD_CATEGORIES		(1U << 6)
951#define PKG_LOAD_LICENSES		(1U << 7)
952#define PKG_LOAD_USERS			(1U << 8)
953#define PKG_LOAD_GROUPS			(1U << 9)
954#define PKG_LOAD_SHLIBS_REQUIRED	(1U << 10)
955#define PKG_LOAD_SHLIBS_PROVIDED	(1U << 11)
956#define PKG_LOAD_ANNOTATIONS		(1U << 12)
957#define PKG_LOAD_CONFLICTS		(1U << 13)
958#define PKG_LOAD_PROVIDES		(1U << 14)
959#define PKG_LOAD_REQUIRES		(1U << 15)
960#define PKG_LOAD_LUA_SCRIPTS		(1u << 16)
961/* Make sure new PKG_LOAD don't conflict with PKG_CONTAINS_* */
962
963/**
964 * Get the next pkg.
965 * @param pkg An allocated struct pkg or a pointer to a NULL pointer. In the
966 * last case, the function take care of the allocation.
967 * @param flags OR'ed PKG_LOAD_*
968 * @return An error code.
969 */
970int pkgdb_it_next(struct pkgdb_it *, struct pkg **pkg, unsigned flags);
971
972/**
973 * Reset the pkgdb_it iterator, allowing for re-iterating over it
974 */
975void pkgdb_it_reset(struct pkgdb_it *);
976
977/**
978 * Return the number of rows found.
979 * @return -1 on error
980 */
981int pkgdb_it_count(struct pkgdb_it *);
982
983/**
984 * Free a struct pkgdb_it.
985 */
986void pkgdb_it_free(struct pkgdb_it *);
987
988/**
989 * Compact the database to save space.
990 * Note that the function will really compact the database only if some
991 * internal criterias are met.
992 * @return An error code.
993 */
994int pkgdb_compact(struct pkgdb *db);
995
996/**
997 * Install and register a new package.
998 * @param db An opened pkgdb
999 * @param path The path to the package archive file on the local disk
1000 * @return An error code.
1001 */
1002int pkg_add(struct pkgdb *db, const char *path, unsigned flags,
1003    struct pkg_manifest_key *keys, const char *location);
1004
1005int pkg_add_from_remote(struct pkgdb *db, const char *path, unsigned flags,
1006    struct pkg_manifest_key *keys, const char *location, struct pkg *rp);
1007
1008#define PKG_ADD_UPGRADE			(1U << 0)
1009/* (1U << 1) removed intentionally */
1010#define PKG_ADD_AUTOMATIC		(1U << 2)
1011#define PKG_ADD_FORCE			(1U << 3)
1012#define PKG_ADD_NOSCRIPT		(1U << 4)
1013#define PKG_ADD_FORCE_MISSING		(1U << 5)
1014#define PKG_ADD_SPLITTED_UPGRADE	(1U << 6)
1015
1016/**
1017 * Allocate a new pkg_jobs.
1018 * @param db A pkgdb open with PKGDB_REMOTE.
1019 * @return An error code.
1020 */
1021int pkg_jobs_new(struct pkg_jobs **jobs, pkg_jobs_t type, struct pkgdb *db);
1022
1023/**
1024 * Free a pkg_jobs
1025 */
1026void pkg_jobs_free(struct pkg_jobs *jobs);
1027
1028/**
1029 * Add a pkg to the jobs queue.
1030 * @return An error code.
1031 */
1032int pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc);
1033int pkg_jobs_solve(struct pkg_jobs *j);
1034int pkg_jobs_set_repository(struct pkg_jobs *j, const char *name);
1035const char* pkg_jobs_destdir(struct pkg_jobs *j);
1036int pkg_jobs_set_destdir(struct pkg_jobs *j, const char *name);
1037void pkg_jobs_set_flags(struct pkg_jobs *j, pkg_flags f);
1038pkg_jobs_t pkg_jobs_type(struct pkg_jobs *j);
1039
1040/**
1041 * Returns the number of elements in the job queue
1042 */
1043int pkg_jobs_count(struct pkg_jobs *jobs);
1044
1045/**
1046 * Returns the number of total elements in pkg universe
1047 */
1048int pkg_jobs_total(struct pkg_jobs *jobs);
1049
1050/**
1051 * Iterates over the packages in the jobs queue.
1052 * @param iter Must be set to NULL for the first call.
1053 * @return A next pkg or NULL.
1054 */
1055bool pkg_jobs_iter(struct pkg_jobs *jobs, void **iter, struct pkg **n,
1056	struct pkg **o, int *type);
1057
1058/**
1059 * Apply the jobs in the queue (fetch and install).
1060 * @return An error code.
1061 */
1062int pkg_jobs_apply(struct pkg_jobs *jobs);
1063
1064/**
1065 * Emit CUDF spec to a file for a specified jobs request
1066 * @return error code
1067 */
1068int pkg_jobs_cudf_emit_file(struct pkg_jobs *, pkg_jobs_t , FILE *);
1069
1070/**
1071 * Parse the output of an external CUDF solver
1072 * @return error code
1073 */
1074int pkg_jobs_cudf_parse_output(struct pkg_jobs *j, FILE *f);
1075
1076/**
1077 * Check if there are locked packages
1078 * @return true if locked packages exist, false if not
1079 */
1080bool pkg_jobs_has_lockedpkgs(struct pkg_jobs *j);
1081
1082/**
1083 * Iterate through the locked packages, calling the passed in function pointer
1084 * on each.
1085 */
1086typedef int(*locked_pkgs_cb)(struct pkg *, void *);
1087void pkg_jobs_iter_lockedpkgs(struct pkg_jobs *j, locked_pkgs_cb, void *);
1088
1089/**
1090 * Solve a SAT problem
1091 * @return true if a problem is solvable
1092 */
1093int pkg_solve_sat_problem(struct pkg_solve_problem *problem);
1094
1095/**
1096 * Export SAT problem to a dot graph description
1097 */
1098void pkg_solve_dot_export(struct pkg_solve_problem *problem, FILE *file);
1099
1100/**
1101 * Convert package jobs to a SAT problem
1102 * @return SAT problem or NULL if failed
1103 */
1104struct pkg_solve_problem * pkg_solve_jobs_to_sat(struct pkg_jobs *j);
1105
1106/**
1107 * Export sat problem to the DIMACS format
1108 * @return error code
1109 */
1110int pkg_solve_dimacs_export(struct pkg_solve_problem *problem, FILE *f);
1111
1112/**
1113 * Move solved problem to the jobs structure
1114 * @return error code
1115 */
1116int pkg_solve_sat_to_jobs(struct pkg_solve_problem *problem);
1117
1118/**
1119 * Parse SAT solver output and convert it to jobs
1120 * @return error code
1121 */
1122int pkg_solve_parse_sat_output(FILE *f, struct pkg_solve_problem *problem);
1123
1124/**
1125 * Free a SAT problem structure
1126 */
1127void pkg_solve_problem_free(struct pkg_solve_problem *problem);
1128
1129/**
1130 * Archive formats options.
1131 */
1132typedef enum pkg_formats { TAR, TGZ, TBZ, TXZ, TZS } pkg_formats;
1133
1134
1135int pkg_load_metadata(struct pkg *, const char *, const char *, const char *, const char *, bool);
1136
1137/**
1138 * Download the latest repo db file and checks its signature if any
1139 * @param force Always download the repo catalogue
1140 */
1141int pkg_update(struct pkg_repo *repo, bool force);
1142
1143/**
1144 * Get statistics information from the package database(s)
1145 * @param db A valid database object as returned by pkgdb_open()
1146 * @param type Type of statistics to be returned
1147 * @return The statistic information requested
1148 */
1149int64_t pkgdb_stats(struct pkgdb *db, pkg_stats_t type);
1150
1151/**
1152 * pkg plugin functions
1153 * @todo Document
1154 */
1155int pkg_plugins_init(void);
1156void pkg_plugins_shutdown(void);
1157int pkg_plugins(struct pkg_plugin **plugin);
1158int pkg_plugin_set(struct pkg_plugin *p, pkg_plugin_key key, const char *str);
1159const char *pkg_plugin_get(struct pkg_plugin *p, pkg_plugin_key key);
1160void *pkg_plugin_func(struct pkg_plugin *p, const char *func);
1161
1162int pkg_plugin_conf_add(struct pkg_plugin *p, pkg_object_t type, const char *key, const char *def);
1163const pkg_object *pkg_plugin_conf(struct pkg_plugin *p);
1164
1165int pkg_plugin_parse(struct pkg_plugin *p);
1166void pkg_plugin_errno(struct pkg_plugin *p, const char *func, const char *arg);
1167void pkg_plugin_error(struct pkg_plugin *p, const char *fmt, ...);
1168void pkg_plugin_info(struct pkg_plugin *p, const char *fmt, ...);
1169/**
1170 * This is where plugin hook into the library using pkg_plugin_hook()
1171 * @todo: Document
1172 */
1173typedef int(*pkg_plugin_callback)(void *data, struct pkgdb *db);
1174int pkg_plugins_hook_run(pkg_plugin_hook_t hook, void *data, struct pkgdb *db);
1175int pkg_plugin_hook_register(struct pkg_plugin *p, pkg_plugin_hook_t hook, pkg_plugin_callback callback);
1176
1177/**
1178 * Get the value of a configuration key
1179 */
1180
1181const pkg_object *pkg_config_get(const char *);
1182pkg_object_t pkg_object_type(const pkg_object *);
1183const pkg_object *pkg_object_find(const pkg_object *o, const char *key);
1184int64_t pkg_object_int(const pkg_object *o);
1185bool pkg_object_bool(const pkg_object *o);
1186const char *pkg_object_string(const pkg_object *o);
1187void pkg_object_free(pkg_object *o);
1188const char *pkg_object_key(const pkg_object *);
1189const pkg_object *pkg_object_iterate(const pkg_object *, pkg_iter *);
1190unsigned pkg_object_count(const pkg_object *);
1191char *pkg_object_dump(const pkg_object *o);
1192char *pkg_config_dump(void);
1193
1194/**
1195 * @todo Document
1196 */
1197int pkg_version_cmp(const char * const , const char * const);
1198pkg_change_t pkg_version_change(const struct pkg * restrict);
1199pkg_change_t pkg_version_change_between(const struct pkg * pkg1, const struct pkg *pkg2);
1200
1201/**
1202 * Fetch a file.
1203 * @return An error code.
1204 */
1205int pkg_fetch_file(struct pkg_repo *repo, const char *url, char *dest, time_t t,
1206    ssize_t offset, int64_t size);
1207/**
1208 * Fetch a file to temporary destination
1209 */
1210int pkg_fetch_file_tmp(struct pkg_repo *repo, const char *url, char *dest,
1211	time_t t);
1212
1213/**
1214 * Get cached name of a package
1215 */
1216int pkg_repo_cached_name(struct pkg *pkg, char *dest, size_t destlen);
1217
1218/* glue to deal with ports */
1219int ports_parse_plist(struct pkg *, const char *, const char *);
1220
1221/**
1222 * Special structure to report about conflicts
1223 */
1224struct pkg_event_conflict {
1225	char *uid;
1226	struct pkg_event_conflict *next;
1227};
1228
1229/*
1230 * Capsicum sandbox callbacks
1231 */
1232typedef int (*pkg_sandbox_cb)(int fd, void *user_data);
1233
1234/**
1235 * Event type used to report progress or problems.
1236 */
1237typedef enum {
1238	/* informational */
1239	PKG_EVENT_INSTALL_BEGIN = 0,
1240	PKG_EVENT_INSTALL_FINISHED,
1241	PKG_EVENT_DEINSTALL_BEGIN,
1242	PKG_EVENT_DEINSTALL_FINISHED,
1243	PKG_EVENT_UPGRADE_BEGIN,
1244	PKG_EVENT_UPGRADE_FINISHED,
1245	PKG_EVENT_EXTRACT_BEGIN,
1246	PKG_EVENT_EXTRACT_FINISHED,
1247	PKG_EVENT_DELETE_FILES_BEGIN,
1248	PKG_EVENT_DELETE_FILES_FINISHED,
1249	PKG_EVENT_ADD_DEPS_BEGIN,
1250	PKG_EVENT_ADD_DEPS_FINISHED,
1251	PKG_EVENT_FETCHING,
1252	PKG_EVENT_FETCH_BEGIN,
1253	PKG_EVENT_FETCH_FINISHED,
1254	PKG_EVENT_UPDATE_ADD,
1255	PKG_EVENT_UPDATE_REMOVE,
1256	PKG_EVENT_INTEGRITYCHECK_BEGIN,
1257	PKG_EVENT_INTEGRITYCHECK_FINISHED,
1258	PKG_EVENT_INTEGRITYCHECK_CONFLICT,
1259	PKG_EVENT_NEWPKGVERSION,
1260	PKG_EVENT_NOTICE,
1261	PKG_EVENT_DEBUG,
1262	PKG_EVENT_INCREMENTAL_UPDATE,
1263	PKG_EVENT_QUERY_YESNO,
1264	PKG_EVENT_QUERY_SELECT,
1265	PKG_EVENT_SANDBOX_CALL,
1266	PKG_EVENT_SANDBOX_GET_STRING,
1267	PKG_EVENT_PROGRESS_START,
1268	PKG_EVENT_PROGRESS_TICK,
1269	PKG_EVENT_BACKUP,
1270	PKG_EVENT_RESTORE,
1271	/* errors */
1272	PKG_EVENT_ERROR,
1273	PKG_EVENT_ERRNO,
1274	PKG_EVENT_ARCHIVE_COMP_UNSUP = 65536,
1275	PKG_EVENT_ALREADY_INSTALLED,
1276	PKG_EVENT_FAILED_CKSUM,
1277	PKG_EVENT_CREATE_DB_ERROR,
1278	PKG_EVENT_LOCKED,
1279	PKG_EVENT_REQUIRED,
1280	PKG_EVENT_MISSING_DEP,
1281	PKG_EVENT_NOREMOTEDB,
1282	PKG_EVENT_NOLOCALDB,
1283	PKG_EVENT_FILE_MISMATCH,
1284	PKG_EVENT_DEVELOPER_MODE,
1285	PKG_EVENT_PLUGIN_ERRNO,
1286	PKG_EVENT_PLUGIN_ERROR,
1287	PKG_EVENT_PLUGIN_INFO,
1288	PKG_EVENT_NOT_FOUND,
1289	PKG_EVENT_NEW_ACTION,
1290	PKG_EVENT_MESSAGE,
1291	PKG_EVENT_FILE_MISSING,
1292	PKG_EVENT_CLEANUP_CALLBACK_REGISTER,
1293	PKG_EVENT_CLEANUP_CALLBACK_UNREGISTER,
1294	PKG_EVENT_CONFLICTS,
1295	PKG_EVENT_TRIGGERS_BEGIN,
1296	PKG_EVENT_TRIGGER,
1297	PKG_EVENT_TRIGGERS_FINISHED
1298} pkg_event_t;
1299
1300struct pkg_event {
1301	pkg_event_t type;
1302	union {
1303		struct {
1304			const char *func;
1305			const char *arg;
1306			int no;
1307		} e_errno;
1308		struct {
1309			char *msg;
1310		} e_pkg_error;
1311		struct {
1312			char *msg;
1313		} e_pkg_notice;
1314		struct {
1315			int total;
1316			int done;
1317		} e_upd_add;
1318		struct {
1319			int total;
1320			int done;
1321		} e_upd_remove;
1322		struct {
1323			const char *url;
1324		} e_fetching;
1325		struct {
1326			struct pkg *pkg;
1327		} e_already_installed;
1328		struct {
1329			struct pkg *pkg;
1330		} e_install_begin;
1331		struct {
1332			struct pkg *pkg;
1333			struct pkg *old;
1334		} e_install_finished;
1335		struct {
1336			struct pkg *pkg;
1337		} e_deinstall_begin;
1338		struct {
1339			struct pkg *pkg;
1340		} e_deinstall_finished;
1341		struct {
1342			struct pkg *n;
1343			struct pkg *o;
1344		} e_upgrade_begin;
1345		struct {
1346			struct pkg *n;
1347			struct pkg *o;
1348		} e_upgrade_finished;
1349		struct {
1350			struct pkg *pkg;
1351		} e_extract_begin;
1352		struct {
1353			struct pkg *pkg;
1354		} e_extract_finished;
1355		struct {
1356			struct pkg *pkg;
1357		} e_delete_files_begin;
1358		struct {
1359			struct pkg *pkg;
1360		} e_delete_files_finished;
1361		struct {
1362			struct pkg *pkg;
1363		} e_add_deps_begin;
1364		struct {
1365			struct pkg *pkg;
1366		} e_add_deps_finished;
1367		struct {
1368			struct pkg *pkg;
1369			struct pkg_dep *dep;
1370		} e_missing_dep;
1371		struct {
1372			struct pkg *pkg;
1373		} e_locked;
1374		struct {
1375			struct pkg *pkg;
1376			int force;
1377		} e_required;
1378		struct {
1379			const char *repo;
1380		} e_remotedb;
1381		struct {
1382			struct pkg *pkg;
1383			struct pkg_file *file;
1384			const char *newsum;
1385		} e_file_mismatch;
1386		struct {
1387			struct pkg_plugin *plugin;
1388			char *msg;
1389		} e_plugin_info;
1390		struct {
1391			struct pkg_plugin *plugin;
1392			const char *func;
1393			const char *arg;
1394			int no;
1395		} e_plugin_errno;
1396		struct {
1397			struct pkg_plugin *plugin;
1398			char *msg;
1399		} e_plugin_error;
1400		struct {
1401			const char *pkg_name;
1402		} e_not_found;
1403		struct {
1404			const char *pkg_uid;
1405			const char *pkg_path;
1406			struct pkg_event_conflict *conflicts;
1407		} e_integrity_conflict;
1408		struct {
1409			int conflicting;
1410		} e_integrity_finished;
1411		struct {
1412			const char *reponame;
1413			int processed;
1414		} e_incremental_update;
1415		struct {
1416			int level;
1417			char *msg;
1418		} e_debug;
1419		struct {
1420			const char *msg;
1421			int deft;
1422		} e_query_yesno;
1423		struct {
1424			const char *msg;
1425			const char **items;
1426			int ncnt;
1427			int deft;
1428		} e_query_select;
1429		struct {
1430			pkg_sandbox_cb call;
1431			int fd;
1432			void *userdata;
1433		} e_sandbox_call;
1434		struct {
1435			pkg_sandbox_cb call;
1436			void *userdata;
1437			char **result;
1438			int64_t *len;
1439		} e_sandbox_call_str;
1440		struct {
1441			char *msg;
1442		} e_progress_start;
1443		struct {
1444			int64_t current;
1445			int64_t total;
1446		} e_progress_tick;
1447		struct {
1448			const char *msg;
1449		} e_pkg_message;
1450		struct {
1451			struct pkg *pkg;
1452			struct pkg_file *file;
1453		} e_file_missing;
1454		struct {
1455			void *data;
1456			void (*cleanup_cb)(void *data);
1457		} e_cleanup_callback;
1458		struct {
1459			struct pkg *p1;
1460			struct pkg *p2;
1461			const char *path;
1462		} e_conflicts;
1463		struct {
1464			char *name;
1465			bool cleanup;
1466		} e_trigger;
1467	};
1468};
1469
1470/**
1471 * Event callback mechanism.  Events will be reported using this callback,
1472 * providing an event identifier and up to two event-specific pointers.
1473 */
1474typedef int(*pkg_event_cb)(void *, struct pkg_event *);
1475
1476void pkg_event_register(pkg_event_cb cb, void *data);
1477
1478bool pkg_compiled_for_same_os_major(void);
1479int pkg_ini(const char *, const char *, pkg_init_flags);
1480int pkg_init(const char *, const char *);
1481int pkg_initialized(void);
1482void pkg_shutdown(void);
1483
1484int pkg_test_filesum(struct pkg *);
1485int pkg_recompute(struct pkgdb *, struct pkg *);
1486int pkgdb_reanalyse_shlibs(struct pkgdb *, struct pkg *);
1487
1488void pkgdb_cmd(int argc, char **argv);
1489int pkg_sshserve(int fd);
1490
1491int pkg_repos_total_count(void);
1492int pkg_repos_activated_count(void);
1493int pkg_repos(struct pkg_repo **);
1494const char *pkg_repo_url(struct pkg_repo *r);
1495const char *pkg_repo_name(struct pkg_repo *r);
1496const char *pkg_repo_key(struct pkg_repo *r);
1497const char *pkg_repo_fingerprints(struct pkg_repo *r);
1498signature_t pkg_repo_signature_type(struct pkg_repo *r);
1499bool pkg_repo_enabled(struct pkg_repo *r);
1500mirror_t pkg_repo_mirror_type(struct pkg_repo *r);
1501unsigned pkg_repo_priority(struct pkg_repo *r);
1502unsigned pkg_repo_ip_version(struct pkg_repo *r);
1503struct pkg_repo *pkg_repo_find(const char *name);
1504
1505/**
1506 * pkg_printf() and friends.  These parallel the similarly named libc
1507 * functions printf(), fprintf() etc.
1508 */
1509
1510/**
1511 * print to stdout data from pkg as indicated by the format code format
1512 * @param ... Varargs list of struct pkg etc. supplying the data
1513 * @param format String with embedded %-escapes indicating what to print
1514 * @return count of the number of characters printed
1515 */
1516int pkg_printf(const char * restrict format, ...);
1517
1518/**
1519 * print to stdout data from pkg as indicated by the format code format
1520 * @param ap Varargs list of struct pkg etc. supplying the data
1521 * @param format String with embedded %-escapes indicating what to print
1522 * @return count of the number of characters printed
1523 */
1524int pkg_vprintf(const char * restrict format, va_list ap);
1525
1526/**
1527 * print to named stream from pkg as indicated by the format code format
1528 * @param ... Varargs list of struct pkg etc. supplying the data
1529 * @param format String with embedded %-escapes indicating what to output
1530 * @return count of the number of characters printed
1531 */
1532int pkg_fprintf(FILE * restrict stream, const char * restrict format, ...);
1533
1534/**
1535 * print to named stream from pkg as indicated by the format code format
1536 * @param ap varargs arglist
1537 * @param format String with embedded %-escapes indicating what to output
1538 * @return count of the number of characters printed
1539 */
1540int pkg_vfprintf(FILE * restrict stream, const char * restrict format,
1541	va_list ap);
1542
1543/**
1544 * print to file descriptor fd data from pkg as indicated by the format
1545 * code format
1546 * @param fd Previously opened file descriptor to print to
1547 * @param ... Varargs list of struct pkg etc. supplying the data
1548 * @param format String with embedded %-escapes indicating what to print
1549 * @return count of the number of characters printed
1550 */
1551int pkg_dprintf(int fd, const char * restrict format, ...);
1552
1553/**
1554 * print to file descriptor fd data from pkg as indicated by the format
1555 * code format
1556 * @param fd Previously opened file descriptor to print to
1557 * @param ap Varargs list of struct pkg etc. supplying the data
1558 * @param format String with embedded %-escapes indicating what to print
1559 * @return count of the number of characters printed
1560 */
1561int pkg_vdprintf(int fd, const char * restrict format, va_list ap);
1562
1563/**
1564 * print to buffer str of given size data from pkg as indicated by the
1565 * format code format as a NULL-terminated string
1566 * @param str Character array buffer to receive output
1567 * @param size Length of the buffer str
1568 * @param ... Varargs list of struct pkg etc. supplying the data
1569 * @param format String with embedded %-escapes indicating what to output
1570 * @return count of the number of characters that would have been output
1571 * disregarding truncation to fit size
1572 */
1573int pkg_snprintf(char * restrict str, size_t size,
1574	const char * restrict format, ...);
1575
1576/**
1577 * print to buffer str of given size data from pkg as indicated by the
1578 * format code format as a NULL-terminated string
1579 * @param str Character array buffer to receive output
1580 * @param size Length of the buffer str
1581 * @param ap Varargs list of struct pkg etc. supplying the data
1582 * @param format String with embedded %-escapes indicating what to output
1583 * @return count of the number of characters that would have been output
1584 * disregarding truncation to fit size
1585 */
1586int pkg_vsnprintf(char * restrict str, size_t size,
1587	const char * restrict format,va_list ap);
1588
1589/**
1590 * Allocate a string buffer ret sufficiently big to contain formatted
1591 * data data from pkg as indicated by the format code format
1592 * @param ret location of pointer to be set to point to buffer containing
1593 * result
1594 * @param ... Varargs list of struct pkg etc. supplying the data
1595 * @param format String with embedded %-escapes indicating what to output
1596 * @return count of the number of characters printed
1597 */
1598int pkg_asprintf(char **ret, const char * restrict format, ...);
1599
1600/**
1601 * Allocate a string buffer ret sufficiently big to contain formatted
1602 * data data from pkg as indicated by the format code format
1603 * @param ret location of pointer to be set to point to buffer containing
1604 * result
1605 * @param ap Varargs list of struct pkg etc. supplying the data
1606 * @param format String with embedded %-escapes indicating what to output
1607 * @return count of the number of characters printed
1608 */
1609int pkg_vasprintf(char **ret, const char * restrict format, va_list ap);
1610
1611bool pkg_has_message(struct pkg *p);
1612bool pkg_is_locked(const struct pkg * restrict p);
1613
1614
1615/**
1616 * Defines how many chars of checksum are there in a package's name
1617 * We define this number as sufficient for 24k packages.
1618 * To find out probability of collision it is possible to use the following
1619 * python function to calculate 'birthday paradox' probability:
1620 *  def bp(m, n):
1621 *      power = -(n * n) / (2. * m)
1622 *      return 1. - exp(power)
1623 *
1624 * For our choice of 2^40 (or 10 hex characters) it is:
1625 *  >>> bp(float(2 ** 40), 24500.)
1626 *  0.00027292484660568217
1627 *
1628 * And it is negligible probability
1629 */
1630#define PKG_FILE_CKSUM_CHARS 10
1631
1632char *pkg_utils_tokenize(char **);
1633int pkg_utils_count_spaces(const char *);
1634int pkg_add_port(struct pkgdb *db, struct pkg *pkg, const char *root, \
1635    const char *locationn, bool testing);
1636char *pkg_absolutepath(const char *src, char *dest, size_t dest_len, bool fromroot);
1637
1638void pkg_cache_full_clean(void);
1639
1640const char *pkg_get_cachedir(void);
1641int pkg_get_cachedirfd(void);
1642int pkg_get_dbdirfd(void);
1643
1644int pkg_namecmp(struct pkg *, struct pkg *);
1645
1646struct pkg_create *pkg_create_new(void);
1647void pkg_create_free(struct pkg_create *);
1648bool pkg_create_set_format(struct pkg_create *, const char *);
1649void pkg_create_set_compression_level(struct pkg_create *, int);
1650void pkg_create_set_overwrite(struct pkg_create *, bool);
1651void pkg_create_set_rootdir(struct pkg_create *, const char *);
1652void pkg_create_set_output_dir(struct pkg_create *, const char *);
1653void pkg_create_set_timestamp(struct pkg_create *, time_t);
1654void pkg_create_set_expand_manifest(struct pkg_create *, bool);
1655int pkg_create(struct pkg_create *, const char *, const char *, bool);
1656int pkg_create_i(struct pkg_create *, struct pkg *, bool);
1657/* deprecated */
1658int pkg_create_from_manifest(const char *, pkg_formats, const char *,
1659    const char *, const char *) __attribute__((deprecated));
1660int pkg_create_staged(const char *, pkg_formats, const char *, const char *, char *, bool) __attribute__((deprecated));
1661int pkg_create_installed(const char *, pkg_formats, struct pkg *) __attribute__((deprecated));
1662int pkgdb_register_ports(struct pkgdb *db, struct pkg *pkg) __attribute__((deprecated));;
1663int pkg_execute_deferred_triggers(void);
1664int pkg_add_triggers(void);
1665
1666#ifdef __cplusplus
1667}
1668#endif
1669#endif
1670