Factor out the filter code in preparation for nicely configuring where
[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 int
74 tool_write(Repo *repo, const char *basename, int separate)
75 {
76   Pool *pool = repo->pool;
77   Repodatafile fileinfoa[1];
78   Repodatafile *fileinfo = 0;
79   int nsubfiles = 0;
80
81   create_filter(pool);
82   memset (fileinfoa, 0, sizeof fileinfoa);
83   if (separate)
84     {
85       test_separate = 1;
86       fileinfo = fileinfoa;
87       FILE *fp = fopen ("test.attr", "w");
88       repo_write(repo, fp, keyfilter_attr, 0, fileinfo, 0);
89       fclose (fp);
90       fileinfo->location = strdup ("test.attr");
91       fileinfo++;
92
93       nsubfiles = fileinfo - fileinfoa;
94       fileinfo = fileinfoa;
95     }
96   repo_write(repo, stdout, keyfilter_solv, 0, fileinfo, nsubfiles);
97   return 0;
98 }