more flexibility in naming attribute files,
[platform/upstream/libsolv.git] / tools / common_write.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "pool.h"
16 #include "repo.h"
17 #include "repo_write.h"
18 #include "common_write.h"
19
20 static char *verticals[] = {
21   "authors",
22   "description",
23   "messagedel",
24   "messageins",
25   "eula",
26   "diskusage",
27   0
28 };
29
30 static unsigned char *filter;
31 static int nfilter;
32
33 static void
34 create_filter(Pool *pool)
35 {
36   char **s;
37   Id id;
38   for (s = verticals; *s; s++)
39     {
40       id = str2id(pool, *s, 1);
41       if (id >= nfilter)
42         {
43           filter = sat_realloc(filter, id + 16);
44           memset(filter + nfilter, 0, id + 16 - nfilter);
45           nfilter = id + 16;
46         }
47       filter[id] = 1;
48     }
49 }
50
51 static int test_separate = 0;
52
53 static int
54 keyfilter_solv(Repo *data, Repokey *key, void *kfdata)
55 {
56   if (test_separate && key->storage != KEY_STORAGE_SOLVABLE)
57     return KEY_STORAGE_DROPPED;
58   if (key->name < nfilter && filter[key->name])
59     return KEY_STORAGE_VERTICAL_OFFSET;
60   return KEY_STORAGE_INCORE;
61 }
62
63 static int
64 keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
65 {
66   if (key->storage == KEY_STORAGE_SOLVABLE)
67     return KEY_STORAGE_DROPPED;
68   if (key->name < nfilter && filter[key->name])
69     return KEY_STORAGE_VERTICAL_OFFSET;
70   return KEY_STORAGE_INCORE;
71 }
72
73 /*
74  * Write <repo> to stdout
75  * If <attrname> is given, write attributes to <attrname>
76  */
77
78 int
79 tool_write(Repo *repo, const char *basename, const char *attrname)
80 {
81   Pool *pool = repo->pool;
82   Repodatafile fileinfoa[1];
83   Repodatafile *fileinfo = 0;
84   int nsubfiles = 0;
85
86   create_filter(pool);
87   memset (fileinfoa, 0, sizeof fileinfoa);
88   if (attrname)
89     {
90       test_separate = 1;
91       fileinfo = fileinfoa;
92       FILE *fp = fopen (attrname, "w");
93       repo_write(repo, fp, keyfilter_attr, 0, fileinfo, 0);
94       fclose (fp);
95       fileinfo->location = strdup (attrname);
96       fileinfo++;
97
98       nsubfiles = fileinfo - fileinfoa;
99       fileinfo = fileinfoa;
100     }
101   repo_write(repo, stdout, keyfilter_solv, 0, fileinfo, nsubfiles);
102   return 0;
103 }