- patch memory leaks
[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   "filelist",
28   0
29 };
30
31 static unsigned char *filter;
32 static int nfilter;
33
34 static void
35 create_filter(Pool *pool)
36 {
37   char **s;
38   Id id;
39   for (s = verticals; *s; s++)
40     {
41       id = str2id(pool, *s, 1);
42       if (id >= nfilter)
43         {
44           filter = sat_realloc(filter, id + 16);
45           memset(filter + nfilter, 0, id + 16 - nfilter);
46           nfilter = id + 16;
47         }
48       filter[id] = 1;
49     }
50 }
51
52 static int test_separate = 0;
53
54 static int
55 keyfilter_solv(Repo *data, Repokey *key, void *kfdata)
56 {
57   if (test_separate && key->storage != KEY_STORAGE_SOLVABLE)
58     return KEY_STORAGE_DROPPED;
59   if (key->name < nfilter && filter[key->name])
60     return KEY_STORAGE_VERTICAL_OFFSET;
61   return KEY_STORAGE_INCORE;
62 }
63
64 static int
65 keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
66 {
67   if (key->storage == KEY_STORAGE_SOLVABLE)
68     return KEY_STORAGE_DROPPED;
69   if (key->name < nfilter && filter[key->name])
70     return KEY_STORAGE_VERTICAL_OFFSET;
71   return KEY_STORAGE_INCORE;
72 }
73
74 /*
75  * Write <repo> to stdout
76  * If <attrname> is given, write attributes to <attrname>
77  */
78
79 int
80 tool_write(Repo *repo, const char *basename, const char *attrname)
81 {
82   Pool *pool = repo->pool;
83   Repodatafile fileinfoa[1];
84   Repodatafile *fileinfo = 0;
85   int nsubfiles = 0;
86
87   create_filter(pool);
88   memset (fileinfoa, 0, sizeof fileinfoa);
89   if (attrname)
90     {
91       test_separate = 1;
92       fileinfo = fileinfoa;
93       FILE *fp = fopen (attrname, "w");
94       repo_write(repo, fp, keyfilter_attr, 0, fileinfo, 0);
95       fclose (fp);
96       fileinfo->location = strdup (attrname);
97       fileinfo++;
98
99       nsubfiles = fileinfo - fileinfoa;
100       fileinfo = fileinfoa;
101     }
102   repo_write(repo, stdout, keyfilter_solv, 0, fileinfo, nsubfiles);
103   sat_free(filter);
104   return 0;
105 }