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 #include <getopt.h>
19
20 #include "util.h"
21 #include "pool.h"
22 #include "repo.h"
23 #include "repo_deb.h"
24 #include "repo_solv.h"
25 #include "common_write.h"
26
27 static char *
fgets0(char * s,int size,FILE * stream)28 fgets0(char *s, int size, FILE *stream)
29 {
30 char *p = s;
31 int c;
32
33 while (--size > 0)
34 {
35 c = getc(stream);
36 if (c == EOF)
37 {
38 if (p == s)
39 return 0;
40 c = 0;
41 }
42 *p++ = c;
43 if (!c)
44 return s;
45 }
46 *p = 0;
47 return s;
48 }
49
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53 const char **debs = 0;
54 char *manifest = 0;
55 int manifest0 = 0;
56 int c, i, res, ndebs = 0;
57 Pool *pool = pool_create();
58 Repo *repo;
59 FILE *fp;
60 char buf[4096], *p;
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
158