ad275415c96118d05f5f72bf240be7390a281bed
[platform/upstream/libsolv.git] / tools / deb2solv.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 /*
9  * deb2solv - create a solv file from one or multiple debs
10  * 
11  */
12
13 #include <sys/types.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #include "util.h"
20 #include "pool.h"
21 #include "repo.h"
22 #include "repo_deb.h"
23 #include "repo_solv.h"
24 #include "common_write.h"
25
26 static char *
27 fgets0(char *s, int size, FILE *stream)
28 {
29   char *p = s;
30   int c;
31
32   while (--size > 0)
33     {
34       c = getc(stream);
35       if (c == EOF)
36         {
37           if (p == s)
38             return 0;
39           c = 0;
40         }
41       *p++ = c;
42       if (!c)
43         return s;
44     }
45   *p = 0;
46   return s;
47 }
48
49 int
50 main(int argc, char **argv)
51 {
52   const char **debs = 0;
53   char *manifest = 0;
54   int manifest0 = 0;
55   int c, i, res, ndebs = 0;
56   Pool *pool = pool_create();
57   Repo *repo;
58   FILE *fp;
59   char buf[4096], *p;
60   const char *basefile = 0;
61   int is_repo = 0;
62
63   while ((c = getopt(argc, argv, "0:m:r")) >= 0)
64     {
65       switch(c)
66         {
67         case 'm':
68           manifest = optarg;
69           break;
70         case 'r':
71           is_repo = 1;
72           break;
73         case '0':
74           manifest0 = 1;
75           break;
76         default:
77           exit(1);
78         }
79     }
80   if (manifest)
81     {
82       if (!strcmp(manifest, "-"))
83         fp = stdin;
84       else if ((fp = fopen(manifest, "r")) == 0)
85         {
86           perror(manifest);
87           exit(1);
88         }
89       for (;;)
90         {
91           if (manifest0)
92             {
93               if (!fgets0(buf, sizeof(buf), fp))
94                 break;
95             }
96           else
97             {
98               if (!fgets(buf, sizeof(buf), fp))
99                 break;
100               if ((p = strchr(buf, '\n')) != 0)
101                 *p = 0;
102             }
103           debs = solv_extend(debs, ndebs, 1, sizeof(char *), 15);
104           debs[ndebs++] = strdup(buf);
105         }
106       if (fp != stdin)
107         fclose(fp);
108     }
109   while (optind < argc)
110     {
111       debs = solv_extend(debs, ndebs, 1, sizeof(char *), 15);
112       debs[ndebs++] = strdup(argv[optind++]);
113     }
114   repo = repo_create(pool, "deb2solv");
115   repo_add_repodata(repo, 0);
116   res = 0;
117   if (!ndebs && !manifest && is_repo)
118     {
119       if (repo_add_debpackages(repo, stdin, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE))
120         {
121           fprintf(stderr, "deb2solv: %s\n", pool_errstr(pool));
122           res = 1;
123         }
124     }
125   for (i = 0; i < ndebs; i++)
126     {
127       if (is_repo)
128         {
129           if ((fp = fopen(debs[i], "r")) == 0)
130             {
131               perror(debs[i]);
132               res = 1;
133               continue;
134             }
135           if (repo_add_debpackages(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE))
136             {
137               fprintf(stderr, "deb2solv: %s\n", pool_errstr(pool));
138               res = 1;
139             }
140           fclose(fp);
141           continue;
142         }
143       if (repo_add_deb(repo, debs[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE) == 0)
144         {
145           fprintf(stderr, "deb2solv: %s\n", pool_errstr(pool));
146           res = 1;
147         }
148     }
149   repo_internalize(repo);
150   tool_write(repo, stdout);
151   pool_free(pool);
152   for (c = 0; c < ndebs; c++)
153     free((char *)debs[c]);
154   solv_free(debs);
155   exit(res);
156 }
157