poldek: poldek/Cellar/source-playing-with-negative-pri.c (NEW) - t...

mis mis at pld-linux.org
Sun Jun 17 22:29:11 CEST 2007


Author: mis                          Date: Sun Jun 17 20:29:11 2007 GMT
Module: poldek                        Tag: HEAD
---- Log message:
- tracking pri's bug

---- Files affected:
poldek/poldek/Cellar:
   source-playing-with-negative-pri.c (NONE -> 1.1)  (NEW)

---- Diffs:

================================================================
Index: poldek/poldek/Cellar/source-playing-with-negative-pri.c
diff -u /dev/null poldek/poldek/Cellar/source-playing-with-negative-pri.c:1.1
--- /dev/null	Sun Jun 17 22:29:11 2007
+++ poldek/poldek/Cellar/source-playing-with-negative-pri.c	Sun Jun 17 22:29:06 2007
@@ -0,0 +1,1140 @@
+/*
+  Copyright (C) 2000 - 2005 Pawel A. Gajda <mis at k2.net.pl>
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License, version 2 as
+  published by the Free Software Foundation (see file COPYING for details).
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+/*
+  $Id$
+*/
+
+#include <string.h>
+#include <limits.h>
+#include <sys/param.h>          /* for PATH_MAX */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <trurl/nmalloc.h>
+#include <trurl/nassert.h>
+#include <trurl/nstr.h>
+#include <trurl/n_snprintf.h>
+#include <trurl/nhash.h>
+
+#include <vfile/vfile.h>
+
+#define ENABLE_TRACE 0
+
+#include "pkgdir.h"
+#include "pkgdir_intern.h"
+#include "source.h"
+#include "misc.h"
+#include "log.h"
+#include "poldek_term.h"
+#include "i18n.h"
+#include "conf.h"
+
+#define SOURCE_DEFAULT_PRI 0
+
+
+const char source_TYPE_GROUP[] = "group";
+const char *poldek_conf_PKGDIR_DEFAULT_TYPE = "pndir";
+
+struct subopt {
+    char      *name;
+    unsigned  flag;              /* MUST BE non-zero */
+    char      *val;
+    int       isdefault;         /* is default value */
+};
+
+struct src_option {
+    char      *name;
+    int       len;
+    unsigned  flag;
+    struct subopt *subopts;
+};
+
+#define PKGSRC_OPTION_SUBOPT  (1 << 15)
+#define PKGSRC_OPTION_STRING  (1 << 16)
+#define PKGSRC_OPTION_OPTMASK (PKGSRC_OPTION_SUBOPT | PKGSRC_OPTION_STRING)
+
+static struct src_option source_options[] = {
+    { "noauto",   0, PKGSOURCE_NOAUTO,      NULL}, 
+    { "noautoup", 0, PKGSOURCE_NOAUTOUP,    NULL}, 
+    { "gpg",      0, PKGSOURCE_VRFY_GPG,    NULL},
+    { "pgp",      0, PKGSOURCE_VRFY_PGP,    NULL},
+    { "sign",     0, PKGSOURCE_VRFY_SIGN,   NULL},
+    { "type",     0, PKGSOURCE_TYPE |
+                     PKGSRC_OPTION_STRING | PKGSRC_OPTION_SUBOPT, NULL },
+    { "lang",     0, PKGSOURCE_DSCR |
+                     PKGSRC_OPTION_STRING | PKGSRC_OPTION_SUBOPT, NULL },
+    { "pri",      0, PKGSOURCE_PRI | PKGSRC_OPTION_SUBOPT, NULL},
+    { "compress", 0, PKGSOURCE_COMPRESS |
+                     PKGSRC_OPTION_STRING | PKGSRC_OPTION_SUBOPT, NULL },
+    {  NULL,      0, 0, NULL }, 
+};
+#if 0    
+static unsigned find_subopt(const char *optstr, struct subopt *subopts)
+{
+    int i;
+    unsigned flag = 0;
+    
+    i = 0;
+    while (subopts[i].name) {
+        if (strcmp(subopts[i].name, optstr) == 0) {
+            flag = subopts[i].flag;
+            break;
+        }
+        i++;
+    }
+
+    return flag;
+}
+#endif
+
+static
+unsigned get_subopt(struct source *src, struct src_option *opt,
+                    const char *str, const char *options_str)
+{
+    unsigned v = 0;
+    
+    n_assert(strncmp(str, opt->name, opt->len) == 0);
+    
+    str += opt->len;
+    if (*str != '=') {
+        logn(LOGWARN, _("%s: %s unknown option"), options_str, str);
+        return 0;
+    }
+    
+    str++;
+    
+    if (opt->flag & PKGSOURCE_TYPE) {
+        src->type = n_strdup(str);
+        v = 1;
+        
+    } else if (opt->flag & PKGSOURCE_DSCR) {
+        src->dscr = n_strdup(str);
+        v = 1;
+
+    } else if (opt->flag & PKGSOURCE_COMPRESS) {
+        src->compress = n_strdup(str);
+        v = 1;
+        
+    } else if (opt->flag & PKGSOURCE_PRI) {
+        if (sscanf(str, "%d", &v) == 1) {
+            src->pri = v;
+            v = 1;
+        }
+    }
+    
+
+    if (v == 0)
+        logn(LOGWARN, _("%s%sinvalid value ('%s') for option '%s'"),
+             src->name ? src->name : "", src->name ? ": " : "", 
+             str, opt->name);
+
+    return v;
+}
+
+
+const char *source_guess_type(const char *path) 
+{
+    path = path;
+    return NULL;
+}
+
+
+struct source *source_malloc(void)
+{
+    struct source *src;
+    
+    src = n_malloc(sizeof(*src));
+    memset(src, '\0', sizeof(*src));
+
+    src->type = src->original_type = NULL;
+    src->flags = src->subopt_flags = 0;
+    src->pri = 0;
+    src->no = 0;
+    //src->flags |= PKGSOURCE_PRI;
+    src->name = src->path = src->pkg_prefix = NULL;
+    src->dscr = src->type = NULL;
+    src->lc_lang = NULL;
+    src->_refcnt = 0;
+    src->exclude_path = n_array_new(4, free, (tn_fn_cmp)strcmp);
+    src->ign_patterns = n_array_new(4, free, (tn_fn_cmp)strcmp);
+    return src;
+}
+
+
+struct source *source_link(struct source *src) 
+{
+    src->_refcnt++;
+    return src;
+}
+
+static void cp_str_ifnotnull(char **dst, const char *src)
+{
+    if (src)
+        *dst = n_strdup(src);
+}
+
+static struct source *source_dup(const struct source *src) 
+{
+    struct source *nsrc;
+    
+    nsrc = source_malloc();
+
+    nsrc->flags = src->flags;
+    cp_str_ifnotnull(&nsrc->type, src->type);
+    cp_str_ifnotnull(&nsrc->name, src->name);
+    cp_str_ifnotnull(&nsrc->path, src->path);
+    cp_str_ifnotnull(&nsrc->pkg_prefix, src->pkg_prefix);
+    cp_str_ifnotnull(&nsrc->compress, src->compress);
+
+    cp_str_ifnotnull(&nsrc->dscr, src->dscr);
+    cp_str_ifnotnull(&nsrc->lc_lang, src->lc_lang);
+    cp_str_ifnotnull(&nsrc->original_type, src->original_type);
+
+    n_array_free(nsrc->exclude_path);
+    nsrc->exclude_path = n_ref(src->exclude_path);
+
+    n_array_free(nsrc->ign_patterns);
+    nsrc->ign_patterns = n_ref(src->ign_patterns);
+    
+    nsrc->subopt_flags = src->subopt_flags;
+    return nsrc;
+}
+
+void source_free(struct source *src)
+{
+    if (src->_refcnt > 0) {
+        src->_refcnt--;
+        return;
+    }
+
+    n_cfree(&src->type);
+    n_cfree(&src->name);
+    n_cfree(&src->path);
+    n_cfree(&src->pkg_prefix);
+
+    n_cfree(&src->compress);
+    n_cfree(&src->dscr);
+    n_cfree(&src->lc_lang);
+    n_cfree(&src->original_type);
+
+    if (src->exclude_path)
+        n_array_free(src->exclude_path);
+
+    if (src->ign_patterns)
+        n_array_free(src->ign_patterns);
+
+    memset(src, 0, sizeof(*src));
+    free(src);
+}
+
+struct source *source_set_pkg_prefix(struct source *src, const char *prefix)
+{
+    char  clprefix[PATH_MAX];
+    int   n;
+
+    n_assert(prefix);
+    n_assert(src->pkg_prefix == NULL);
+    
+    if ((n = vf_cleanpath(clprefix, sizeof(clprefix), prefix)) == 0 ||
+        n == sizeof(clprefix))
+        return NULL;
+    
+    
+    src->pkg_prefix = n_strdup(clprefix);
+    return src;
+}
+
+static
+char *source_set(char **member, const char *value)
+{
+    if (*member) {
+        free(*member);
+        *member = NULL;
+    }
+
+    if (value)
+        *member = n_strdup(value);
+
+    return *member;
+}
+
+struct source *source_set_type(struct source *src, const char *type)
+{
+    source_set(&src->type, type);
+    return src;
+}
+
+struct source *source_set_default_type(struct source *src)
+{
+    if ((src->flags & PKGSOURCE_TYPE) == 0) /* not set by config*/
+        source_set(&src->type, poldek_conf_PKGDIR_DEFAULT_TYPE);
+    return src;
+}
+
+static char *parse_cmdl_pathspec(const char *pathspec, const char **path)
+{
+    const char  *p;
+    char        *type;
+    int         len, seplen = 1;
+
+    
+    p = pathspec;
+    while (isalnum(*p))
+        p++;
+
+    if (p == pathspec)
+        return NULL;
+
+    if (*p != '#' && (seplen = strspn(p, ",")) != 2)
+        return NULL;
+    
+    len  = p - pathspec + 1;
+    type = n_malloc(len);
+    memcpy(type, pathspec, len - 1);
+    type[len - 1] = '\0';
+
+    *path = p + seplen;
+    return type;
+}
+
+static
+void setup_langs(struct source *src)
+{
+    const char **langs_tokl, **p, *lang;
+    char       lc_lang[256];
+    tn_hash   *langs = NULL;
+    int  n;
+    
+    if (src->dscr)
+        lang = src->dscr;
+    else 
+        lang = lc_messages_lang();
+    
+    
+    if (lang == NULL || *lang == '\0' || strcasecmp(lang, "none") == 0)
+        return;
+
+    
+    langs_tokl = n_str_tokl(lang, ":");
+
+    langs = n_hash_new(7, NULL);
+    n_hash_ctl(langs, TN_HASH_NOCPKEY);
+    
+    n = 0;
+    p = langs_tokl;
+    while (*p) {
+        if (n_hash_exists(langs, *p)) {
+            p++;
+            continue;
+        }
+
+        n += n_snprintf(&lc_lang[n], sizeof(lc_lang) - n, "%s:", *p);
+        n_hash_insert(langs, *p, *p);
+        p++;
+    }
+    if (!n_hash_exists(langs, "C"))
+        n += n_snprintf(&lc_lang[n], sizeof(lc_lang) - n, "C:");
+    
+    if (n)
+        lc_lang[n - 1] = '\0';  /* eat last ':' */
+
+    
+    src->lc_lang = n_strdupl(lc_lang, n - 1);
+    //printf("source_setup_lc %s: %s -> %s\n", src->path, lang, lc_lang);
+    n_hash_free(langs);
+    n_str_tokl_free(langs_tokl);
+}
+
+
+struct source *source_new(const char *name, const char *type,
+                          const char *path, const char *pkg_prefix)
+{
+    struct source   *src;
+    struct stat     st;
+    char            clpath[PATH_MAX], clprefix[PATH_MAX];
+    int             n;
+
+    n_assert(name || path);
+    
+    if (path) {
+        if ((n = vf_cleanpath(clpath, sizeof(clpath), path)) == 0 ||
+            n == sizeof(clpath))
+            return NULL;
+    
+        if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
+            if (clpath[n - 1] != '/')
+                clpath[n++] = '/';
+    
+        } else {
+            int l = strlen(path);
+            if (clpath[n - 1] != '/' && path[l - 1] == '/')
+                clpath[n++] = '/';
+        }
+        clpath[n] = '\0';
+        
+    }
+
+    if (pkg_prefix) {
+        n_assert(path);
+        if ((n = vf_cleanpath(clprefix, sizeof(clprefix), pkg_prefix)) == 0 ||
+            n == sizeof(clprefix))
+            return NULL;
+    }
+    
+    src = source_malloc();
+    if (name) {
+        src->flags |= PKGSOURCE_NAMED;
+        src->name = n_strdup(name);
+    }
+    
+    if (type) {
+        src->type = n_strdup(type);
+        src->flags |= PKGSOURCE_TYPE;
+        
+    } else {
+        src->type = n_strdup(poldek_conf_PKGDIR_DEFAULT_TYPE);
+    }
+    
+    if (path)
+        src->path = n_strdup(clpath);
+    
+    if (pkg_prefix)
+        src->pkg_prefix = n_strdup(clprefix);
+    
+    return src;
+}
+
+struct source *source_new_pathspec(const char *type, const char *pathspec,
+                                   const char *pkg_prefix)
+{
+    struct source   *src;
+    const char      *path, *p;
+    char            *name, *q, *spec_type, *opts = NULL;
+    int             len;
+    unsigned        flags = 0;
+
+    
+    if (*pathspec == '\0')
+        return NULL;
+    
+    if ((spec_type = parse_cmdl_pathspec(pathspec, &path)))
+        pathspec = path;
+    
+    p = pathspec;
+    while (*p && *p != '|' && *p != '#' && !isspace(*p))
+        p++;
+
+    if (*p == '\0') {           /* path only */
+        path = pathspec;
+        name = NULL;
+        
+    } else {
+        path = p + 1;
+        while (isspace(*path))
+            path++;
+        
+        len = p - pathspec;
+        name = alloca(len + 1);
+        memcpy(name, pathspec, len);
+        name[len] = '\0';
+        
+        if (*name == '[') 
+            name++;
+        
+        if ((q = strrchr(name, ']')))
+            *q = '\0';
+        
+        if ((q = strchr(name, ','))) {
+            *q = '\0';
+            opts = ++q;
+        }
+
+        if (*name == '\0')
+            name = NULL;
+    }
+    
+    src = source_new(name, type ? type : spec_type, path, pkg_prefix);
+    if (src == NULL)
+        return NULL;
+    
+    src->flags |= flags;
+    if (spec_type != NULL)
+        free(spec_type);
+    
+    if (opts) {
+        const char **tl, **t;
+
+        tl = t = n_str_tokl(opts, ",");
+        n_assert(tl);
+
+        while (*t) {
+            int n = 0;
+            while (source_options[n].name != NULL) {
+                struct src_option *opt = &source_options[n];
+                
+                if (opt->len == 0)
+                    opt->len = strlen(opt->name);
+                
+                if (opt->flag & PKGSRC_OPTION_SUBOPT) {
+                    if (strncmp(*t, opt->name, opt->len) == 0) {
+                        if (get_subopt(src, opt, *t, name))
+                            src->flags |= opt->flag;
+                        break;
+                    }
+                    
+                } else if (strcmp(*t, opt->name) == 0) {
+                    src->flags |= opt->flag;
+                    break;
+                }
+                
+                n++;
+            }
+            
+            if (source_options[n].name == NULL)
+                logn(LOGWARN, _("%s: %s unknown option"), name, *t);
+            t++;
+        }
+        n_str_tokl_free(tl);
+    }
+
+    setup_langs(src);
+    return src;
+}
+
+struct source *source_new_v0_18(const char *pathspec, const char *pkg_prefix)
+{
+    struct source *src = source_new_pathspec(NULL, pathspec, pkg_prefix);
+    if ((src->flags & PKGSOURCE_TYPE) == 0)
+        source_set_type(src, "pdir"); /* default for v0.18.x */
+    return src;
+}
+
+static
+int get_conf_opt_list(const tn_hash *htcnf, const char *name,
+                      tn_array *tolist)
+{
+    tn_array *list;
+    int i = 0;
+
+    if (n_array_size(tolist) > 0)
+        return 0;
+    
+    if ((list = poldek_conf_get_multi(htcnf, name))) {
+        for (i=0; i < n_array_size(list); i++)
+            n_array_push(tolist, n_strdup(n_array_nth(list, i)));
+        
+        n_array_free(list);
+    }
+    
+    n_array_sort(tolist);
+    n_array_uniq(tolist);
+    return i;
+}
+
+struct source *source_new_htcnf(const tn_hash *htcnf) 
+{
+    char spec[PATH_MAX];
+    struct source *src;
+    const char *vs, *type, *srcnam;
+    int  n = 0;
+    int  v;
+    
+    vs = poldek_conf_get(htcnf, "name", NULL);
+    if (vs == NULL)
+        vs = "anon";
+    srcnam = vs;
+    
+    n += n_snprintf(&spec[n], sizeof(spec) - n, "%s", vs);
+
+    if ((vs = poldek_conf_get(htcnf, "type", NULL)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",type=%s", vs);
+    type = vs;
+    
+    if ((v = poldek_conf_get_int(htcnf, "pri", 0)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",pri=%d", v);
+    
+    if ((v = poldek_conf_get_bool(htcnf, "noauto", 0)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",noauto");
+    
+    else if ((v = poldek_conf_get_bool(htcnf, "auto", 1)) == 0)
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",noauto");
+
+    if ((v = poldek_conf_get_bool(htcnf, "noautoup", 0)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",noautoup");
+    
+    else if ((v = poldek_conf_get_bool(htcnf, "autoup", 1)) == 0)
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",noautoup");
+
+    if ((v = poldek_conf_get_bool(htcnf, "signed", 0)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",sign");
+    
+    else if ((v = poldek_conf_get_bool(htcnf, "sign", 0)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",sign");
+
+    if ((vs = poldek_conf_get(htcnf, "lang", NULL)))
+        n += n_snprintf(&spec[n], sizeof(spec) - n, ",lang=%s", vs);
+
+    vs = poldek_conf_get(htcnf, "path", NULL);
+    if (vs == NULL)
+        vs = poldek_conf_get(htcnf, "url", NULL);
+    
<<Diff was trimmed, longer than 597 lines>>


More information about the pld-cvs-commit mailing list