[pacman-dev] [PATCH 01/14] Split optdep into alpm_depend_t and description

Dan McGee dpmcgee at gmail.com
Wed Nov 30 21:52:09 EST 2011


On Wed, Nov 23, 2011 at 9:51 AM, Benedikt Morbach
<benedikt.morbach at googlemail.com> wrote:
> This is done as a preparation to better handle optdepends.
> This commit should not change pacman's behaviour, as it simply adds new
> functions and data structures and doesn't yet hook them up anywhere.
>
> Signed-off-by: Benedikt Morbach <benedikt.morbach at googlemail.com>
> ---
>  lib/libalpm/alpm.h |   12 +++++++++
>  lib/libalpm/deps.c |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  lib/libalpm/deps.h |    3 ++
>  3 files changed, 83 insertions(+), 0 deletions(-)
>
> diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
> index 1751c81..943ceec 100644
> --- a/lib/libalpm/alpm.h
> +++ b/lib/libalpm/alpm.h
> @@ -150,6 +150,12 @@ typedef struct _alpm_depend_t {
>        alpm_depmod_t mod;
>  } alpm_depend_t;
>
> +/** Optional dependency */
> +typedef struct _alpm_optdepend_t {
> +       alpm_depend_t *depend;
> +       char *description;
> +} alpm_optdepend_t;
> +

Rather than add a whole new type and all of the boilerplate, why don't
we just do this for now?

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 1751c81..df49d8e 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -146,6 +146,7 @@ typedef struct __alpm_trans_t alpm_trans_t;
 typedef struct _alpm_depend_t {
        char *name;
        char *version;
+       char *description;
        unsigned long name_hash;
        alpm_depmod_t mod;
 } alpm_depend_t;

Then simply modify the existing free, dup, split, and compute
functions to support description, rather than having to reimplement
them all. Even though we don't have descriptions on regular depends at
the moment, I don't see any reason why we can't support them in pacman
(even if their creation isn't supported yet).

Yes, we'll have 4 to 8 more bytes per depend struct, but this isn't a
whole lot to worry about given the simpler code in this and the
following patches.

>  /** Missing dependency */
>  typedef struct _alpm_depmissing_t {
>        char *target;
> @@ -1103,6 +1109,12 @@ alpm_list_t *alpm_checkconflicts(alpm_handle_t *handle, alpm_list_t *pkglist);
>  */
>  char *alpm_dep_compute_string(const alpm_depend_t *dep);
>
> +/** Returns a newly allocated string representing the optional dependency information.
> + * @param dep a optional dependency info structure
> + * @return a formatted string, e.g. "sqlite: for Database support"
> + */
> +char *alpm_optdep_compute_string(const alpm_optdepend_t *optdep);
> +
>  /** @} */
>
>  /** @} */
> diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c
> index 89f6d69..12d7156 100644
> --- a/lib/libalpm/deps.c
> +++ b/lib/libalpm/deps.c
> @@ -44,6 +44,13 @@ void _alpm_dep_free(alpm_depend_t *dep)
>        FREE(dep);
>  }
>
> +void _alpm_optdep_free(alpm_optdepend_t *optdep)
> +{
> +       _alpm_dep_free(optdep->depend);
> +       FREE(optdep->description);
> +       FREE(optdep);
> +}
> +
>  static alpm_depmissing_t *depmiss_new(const char *target, alpm_depend_t *dep,
>                const char *causingpkg)
>  {
> @@ -475,6 +482,44 @@ alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep)
>        return newdep;
>  }
>
> +alpm_optdepend_t *_alpm_splitoptdep(const char *optstring)
> +{
> +       alpm_optdepend_t *optdep;
> +       char *depstring;
> +       const char *ptr;
> +
> +       ASSERT(optstring != NULL, return NULL);
> +
> +       CALLOC(optdep, 1, sizeof(alpm_optdepend_t), return NULL);
> +
> +       /* Note the extra space in ": " to avoid matching the epoch */
> +       if((ptr = strstr(optstring, ": ")) == NULL) {
> +               ptr = optstring + strlen(optstring);
> +       }
> +
> +       STRNDUP(depstring, optstring, ptr - optstring, return NULL);
> +       optdep->depend = _alpm_splitdep(depstring);
> +       FREE(depstring);
> +
> +       if(*ptr != '\0') {
> +               STRDUP(optdep->description, ptr + 2, return NULL);
> +               optdep->description = _alpm_strtrim(optdep->description);
> +       }
> +
> +       return optdep;
> +}
> +
> +alpm_optdepend_t *_alpm_optdep_dup(const alpm_optdepend_t *optdep)
> +{
> +       alpm_optdepend_t *newdep;
> +       CALLOC(newdep, 1, sizeof(alpm_optdepend_t), return NULL);
> +
> +       newdep->depend = _alpm_dep_dup(optdep->depend);
> +       STRDUP(newdep->description, optdep->description, return NULL);
> +
> +       return newdep;
> +}
> +
>  /* These parameters are messy. We check if this package, given a list of
>  * targets and a db is safe to remove. We do NOT remove it if it is in the
>  * target list, or if if the package was explictly installed and
> @@ -872,4 +917,27 @@ char SYMEXPORT *alpm_dep_compute_string(const alpm_depend_t *dep)
>
>        return str;
>  }
> +
> +/** Reverse of splitoptdep; make a optdep string from a alpm_optdepend_t struct.
> + * The string must be freed!
> + * @param optdep the optdepend to turn into a string
> + * @return a string-formatted optional dependency with description
> + */
> +char SYMEXPORT *alpm_optdep_compute_string(const alpm_optdepend_t *optdep)
> +{
> +       ASSERT(optdep != NULL, return NULL);
> +
> +       char *depstring = alpm_dep_compute_string(optdep->depend);
> +
> +       if(optdep->description != NULL) {
> +               char *str;
> +               size_t len = strlen(depstring) + strlen(optdep->description) + 3;
> +               MALLOC(str, len, return NULL);
> +               snprintf(str, len, "%s: %s", depstring, optdep->description);
> +               free(depstring);
> +               return str;
> +       }
> +
> +       return depstring;
> +}
>  /* vim: set ts=2 sw=2 noet: */
> diff --git a/lib/libalpm/deps.h b/lib/libalpm/deps.h
> index ce25bda..69b65df 100644
> --- a/lib/libalpm/deps.h
> +++ b/lib/libalpm/deps.h
> @@ -28,7 +28,9 @@
>  #include "alpm.h"
>
>  void _alpm_dep_free(alpm_depend_t *dep);
> +void _alpm_optdep_free(alpm_optdepend_t *optdep);
>  alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep);
> +alpm_optdepend_t *_alpm_optdep_dup(const alpm_optdepend_t *optdep);
>  void _alpm_depmiss_free(alpm_depmissing_t *miss);
>  alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle, alpm_list_t *targets, int reverse);
>  int _alpm_recursedeps(alpm_db_t *db, alpm_list_t *targs, int include_explicit);
> @@ -36,6 +38,7 @@ int _alpm_resolvedeps(alpm_handle_t *handle, alpm_list_t *localpkgs, alpm_pkg_t
>                alpm_list_t *preferred, alpm_list_t **packages, alpm_list_t *remove,
>                alpm_list_t **data);
>  alpm_depend_t *_alpm_splitdep(const char *depstring);
> +alpm_optdepend_t *_alpm_splitoptdep(const char *optstring);
>  int _alpm_depcmp_literal(alpm_pkg_t *pkg, alpm_depend_t *dep);
>  int _alpm_depcmp(alpm_pkg_t *pkg, alpm_depend_t *dep);
>
> --
> 1.7.7.3


More information about the pacman-dev mailing list