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