From: Klaus Kaempf Date: Thu, 14 Feb 2008 11:58:19 +0000 (+0000) Subject: more flexibility in naming attribute files, X-Git-Tag: BASE-SuSE-Code-12_1-Branch~308^2~624 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=072758cef2678baa95100fc72d868acf7d9e27df;p=thirdparty%2Flibsolv.git more flexibility in naming attribute files, one can even have multiple ones in a single directory now ;-) --- diff --git a/tools/common_write.c b/tools/common_write.c index 78623388..f72670d0 100644 --- a/tools/common_write.c +++ b/tools/common_write.c @@ -70,8 +70,13 @@ keyfilter_attr(Repo *data, Repokey *key, void *kfdata) return KEY_STORAGE_INCORE; } +/* + * Write to stdout + * If is given, write attributes to + */ + int -tool_write(Repo *repo, const char *basename, int separate) +tool_write(Repo *repo, const char *basename, const char *attrname) { Pool *pool = repo->pool; Repodatafile fileinfoa[1]; @@ -80,14 +85,14 @@ tool_write(Repo *repo, const char *basename, int separate) create_filter(pool); memset (fileinfoa, 0, sizeof fileinfoa); - if (separate) + if (attrname) { test_separate = 1; fileinfo = fileinfoa; - FILE *fp = fopen ("test.attr", "w"); + FILE *fp = fopen (attrname, "w"); repo_write(repo, fp, keyfilter_attr, 0, fileinfo, 0); fclose (fp); - fileinfo->location = strdup ("test.attr"); + fileinfo->location = strdup (attrname); fileinfo++; nsubfiles = fileinfo - fileinfoa; diff --git a/tools/common_write.h b/tools/common_write.h index d32e0f5a..22676c9f 100644 --- a/tools/common_write.h +++ b/tools/common_write.h @@ -10,6 +10,6 @@ #include "repo.h" -int tool_write(Repo *repo, const char *basename, int separate); +int tool_write(Repo *repo, const char *basename, const char *attrname); #endif diff --git a/tools/dumpsolv.c b/tools/dumpsolv.c index 631e529e..cc55250c 100644 --- a/tools/dumpsolv.c +++ b/tools/dumpsolv.c @@ -10,6 +10,8 @@ #include #include +static char *attrname = 0; + #include "pool.h" #include "repo_solv.h" #if 0 @@ -211,27 +213,111 @@ dump_some_attrs(Repo *repo, Solvable *s) static FILE * loadcallback (Pool *pool, Repodata *data, void *vdata) { - FILE *fp; + FILE *fp = 0; fprintf (stderr, "Loading SOLV file %s\n", data->location); - fp = fopen ("test.attr", "r"); + if (attrname) + { + fp = fopen (attrname, "r"); + if (!fp) + perror(attrname); + } return fp; } + +static void +usage( const char *err ) +{ + if (err) + fprintf (stderr, "\n** Error:\n %s\n", err); + fprintf( stderr, "\nUsage:\n" + "dumpsolv [-a] [-n ] []\n" + " -a read attributes.\n" + " If no attribute name (-n) is given,\n" + " it is deduced from the .solv name\n" + " by replacing '.solv' with '.attr'\n" + " If neither an attribute name nor a solvfile are given,\n" + " the attribute name defaults to 'test.attr'\n" + " -n use (evtl. suffixed by '.attr') for attributes\n" + ); + exit(0); +} + + int main(int argc, char **argv) { Repo *repo; Pool *pool; int i, n; Solvable *s; + const char *solvname = 0; + + argv++; + argc--; + while (argc--) + { + const char *s = argv[0]; + if (*s++ == '-') + while (*s) + switch (*s++) + { + case 'h': usage(NULL); break; + case 'a': + if (!attrname) + attrname = ""; + break; + case 'n': + if (argc) + { + attrname = argv[1]; + argv++; + argc--; + } + else + usage("argument required for '-n'"); + break; + default : break; + } + else + { + solvname = argv[0]; + if (freopen (solvname, "r", stdin) == 0) + { + perror(solvname); + exit(1); + } + break; + } + argv++; + } - if (argc != 1) + if (attrname) /* attributes wanted */ { - if (freopen(argv[1], "r", stdin) == 0) + if (*attrname == 0) /* no attrname given */ + { + if (solvname) /* solvname given -> deduce attrname from it */ { - perror(argv[1]); - exit(1); + attrname = strdup (solvname); + char *dot = strrchr(attrname, '.'); + if (dot && !strcmp(dot, ".solv")) /* if it ends in .solv, just keep the dot */ + dot[1] = 0; } + else + attrname = "test.attr"; /* default to "test.attr" */ + } + + /* ensure '.attr' suffix */ + const char *dot = strrchr(attrname, '.'); + if (!dot || strcmp(dot, ".attr")) + { + int len = strlen (attrname); + char *newname = (char *)malloc (len + 6); /* alloc for +'.attr'+'\0' */ + strcpy (newname, attrname); + strcpy (newname+len, ".attr"); + attrname = newname; + } } + pool = pool_create(); pool_setdebuglevel(pool, 1); pool_setloadcallback(pool, loadcallback, 0); diff --git a/tools/repo_susetags.c b/tools/repo_susetags.c index e52fa3dc..7f25973f 100644 --- a/tools/repo_susetags.c +++ b/tools/repo_susetags.c @@ -369,7 +369,7 @@ tag_from_string (char *cs) */ void -repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr) +repo_add_susetags(Repo *repo, FILE *fp, Id vendor, const char *attrname) { Pool *pool = repo->pool; char *line, *linep; @@ -383,7 +383,7 @@ repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr) struct parsedata pd; Repodata *data = 0; - if (with_attr) + if (attrname) { data = repo_add_repodata(repo); init_attr_ids(pool); @@ -616,7 +616,7 @@ repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr) indesc++; continue; } - if (!with_attr) + if (!attrname) continue; switch (tag) { diff --git a/tools/repo_susetags.h b/tools/repo_susetags.h index f764d0b0..44dba8dc 100644 --- a/tools/repo_susetags.h +++ b/tools/repo_susetags.h @@ -5,4 +5,8 @@ * for further information */ -extern void repo_add_susetags(Repo *repo, FILE *fp, Id vendor, int with_attr); +/* read susetags file into + * if given, write attributes as '.attr' + */ + +extern void repo_add_susetags(Repo *repo, FILE *fp, Id vendor, const char *attrname); diff --git a/tools/susetags2solv.c b/tools/susetags2solv.c index f8b825e2..5446a118 100644 --- a/tools/susetags2solv.c +++ b/tools/susetags2solv.c @@ -19,24 +19,26 @@ #include "common_write.h" static void -usage(void) +usage(const char *err) { - fprintf(stderr, "Usage:\n" + if (err) + fprintf(stderr, "\n** Error:\n %s\n", err); + fprintf(stderr, "\nUsage:\n" "susetags2solv [-a][-s][-c ][-h]\n" " reads a 'susetags' repository from and writes a .solv file to \n" " -a : with attributes\n" - " -c : parse given contentfile (for product information)\n" + " -c : parse given contentfile (for product information)\n" " -h : print help & exit\n" - " -s : test separate\n" + " -n : save attributes as .attr\n" ); + exit(0); } int main(int argc, char **argv) { - int with_attr = 0; - int test_separate = 0; const char *contentfile = 0; + const char *attrname = 0; Id vendor = 0; argv++; argc--; @@ -47,9 +49,21 @@ main(int argc, char **argv) while (*s) switch (*s++) { - case 'h': usage(); exit(0); - case 'a': with_attr = 1; break; - case 's': test_separate = 1; break; + case 'h': usage(NULL); break; + case 'a': + if (attrname == NULL) + attrname = "test.attr"; + break; + case 'n': + if (argc) + { + attrname = argv[1]; + argv++; + argc--; + } + else + usage("argument required for '-n'"); + break; case 'c': if (argc) { @@ -57,6 +71,8 @@ main(int argc, char **argv) argv++; argc--; } + else + usage("argument required for '-c'"); break; default : break; } @@ -77,8 +93,21 @@ main(int argc, char **argv) vendor = pool->solvables[repo->start].vendor; fclose (fp); } - repo_add_susetags(repo, stdin, vendor, with_attr); - tool_write(repo, 0, with_attr && test_separate); + if (attrname) + { + /* ensure '.attr' suffix */ + const char *dot = strrchr(attrname, '.'); + if (!dot || strcmp(dot, ".attr")) + { + int len = strlen (attrname); + char *newname = (char *)malloc (len + 6); /* alloc for +'.attr'+'\0' */ + strcpy (newname, attrname); + strcpy (newname+len, ".attr"); + attrname = newname; + } + } + repo_add_susetags(repo, stdin, vendor, attrname); + tool_write(repo, 0, attrname); pool_free(pool); exit(0); }