Imported Upstream version 0.6.21
[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, "0b:m:r")) >= 0)
64     {
65       switch(c)
66         {
67         case 'b':
68           basefile = optarg;
69           break;
70         case 'm':
71           manifest = optarg;
72           break;
73         case 'r':
74           is_repo = 1;
75           break;
76         case '0':
77           manifest0 = 1;
78           break;
79         default:
80           exit(1);
81         }
82     }
83   if (manifest)
84     {
85       if (!strcmp(manifest, "-"))
86         fp = stdin;
87       else if ((fp = fopen(manifest, "r")) == 0)
88         {
89           perror(manifest);
90           exit(1);
91         }
92       for (;;)
93         {
94           if (manifest0)
95             {
96               if (!fgets0(buf, sizeof(buf), fp))
97                 break;
98             }
99           else
100             {
101               if (!fgets(buf, sizeof(buf), fp))
102                 break;
103               if ((p = strchr(buf, '\n')) != 0)
104                 *p = 0;
105             }
106           debs = solv_extend(debs, ndebs, 1, sizeof(char *), 15);
107           debs[ndebs++] = strdup(buf);
108         }
109       if (fp != stdin)
110         fclose(fp);
111     }
112   while (optind < argc)
113     {
114       debs = solv_extend(debs, ndebs, 1, sizeof(char *), 15);
115       debs[ndebs++] = strdup(argv[optind++]);
116     }
117   repo = repo_create(pool, "deb2solv");
118   repo_add_repodata(repo, 0);
119   res = 0;
120   if (!ndebs && !manifest && is_repo)
121     {
122       if (repo_add_debpackages(repo, stdin, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE))
123         {
124           fprintf(stderr, "deb2solv: %s\n", pool_errstr(pool));
125           res = 1;
126         }
127     }
128   for (i = 0; i < ndebs; i++)
129     {
130       if (is_repo)
131         {
132           if ((fp = fopen(debs[i], "r")) == 0)
133             {
134               perror(debs[i]);
135               res = 1;
136               continue;
137             }
138           if (repo_add_debpackages(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE))
139             {
140               fprintf(stderr, "deb2solv: %s\n", pool_errstr(pool));
141               res = 1;
142             }
143           fclose(fp);
144           continue;
145         }
146       if (repo_add_deb(repo, debs[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE) == 0)
147         {
148           fprintf(stderr, "deb2solv: %s\n", pool_errstr(pool));
149           res = 1;
150         }
151     }
152   repo_internalize(repo);
153   tool_write(repo, basefile, 0);
154   pool_free(pool);
155   for (c = 0; c < ndebs; c++)
156     free((char *)debs[c]);
157   solv_free(debs);
158   exit(res);
159 }
160