Imported Upstream version 0.7.12
[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 <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "pool.h"
14 #include "repo.h"
15 #include "repo_write.h"
16 #include "common_write.h"
17 #include "solvversion.h"
18
19 /* toolversion history
20  * 1.0: initial tool version
21  * 1.1: changed PRODUCT_ENDOFLIFE parsing
22 */
23
24 static int
25 keyfilter_solv(Repo *repo, Repokey *key, void *kfdata)
26 {
27   if (key->name == SUSETAGS_SHARE_NAME || key->name == SUSETAGS_SHARE_EVR || key->name == SUSETAGS_SHARE_ARCH)
28     return KEY_STORAGE_DROPPED;
29   return repo_write_stdkeyfilter(repo, key, kfdata);
30 }
31
32 /*
33  * Write <repo> to fp
34  */
35 void
36 tool_write(Repo *repo, FILE *fp)
37 {
38   Repodata *info;
39   Queue addedfileprovides;
40   Repowriter *writer;
41
42   info = repo_add_repodata(repo, 0);    /* add new repodata for our meta info */
43   repodata_set_str(info, SOLVID_META, REPOSITORY_TOOLVERSION, LIBSOLV_TOOLVERSION);
44   repodata_unset(info, SOLVID_META, REPOSITORY_EXTERNAL);       /* do not propagate this */
45
46   queue_init(&addedfileprovides);
47   pool_addfileprovides_queue(repo->pool, &addedfileprovides, 0);
48   if (addedfileprovides.count)
49     repodata_set_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &addedfileprovides);
50   else
51     repodata_unset(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES);
52   queue_free(&addedfileprovides);
53
54   pool_freeidhashes(repo->pool);        /* free some mem */
55
56   repodata_internalize(info);
57   writer = repowriter_create(repo);
58   repowriter_set_keyfilter(writer, keyfilter_solv, 0);
59   if (repowriter_write(writer, fp) != 0)
60     {
61       fprintf(stderr, "repo write failed: %s\n", pool_errstr(repo->pool));
62       exit(1);
63     }
64   if (fflush(fp))
65     {
66       perror("fflush");
67       exit(1);
68     }
69   repowriter_free(writer);
70   repodata_free(info);          /* delete meta info repodata again */
71 }