Imported Upstream version 0.6.33
[platform/upstream/libsolv.git] / tools / archpkgs2solv.c
1 /*
2  * Copyright (c) 2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * archpkgs2solv - create a solv file from multiple arch packages
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_arch.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 **pkgs = 0;
53   char *manifest = 0;
54   int manifest0 = 0;
55   int i, c, res, npkgs = 0;
56   Pool *pool = pool_create();
57   Repo *repo;
58   FILE *fp;
59   char buf[4096], *p;
60   const char *basefile = 0;
61   int flags = 0;
62
63   while ((c = getopt(argc, argv, "0b:m:i")) >= 0)
64     {
65       switch(c)
66         {
67         case 'b':
68           basefile = optarg;
69           break;
70         case 'm':
71           manifest = optarg;
72           break;
73         case '0':
74           manifest0 = 1;
75           break;
76         case 'i':
77           flags |= ARCH_ADD_WITH_PKGID;
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           pkgs = solv_extend(pkgs, npkgs, 1, sizeof(char *), 15);
107           pkgs[npkgs++] = strdup(buf);
108         }
109       if (fp != stdin)
110         fclose(fp);
111     }
112   while (optind < argc)
113     {
114       pkgs = solv_extend(pkgs, npkgs, 1, sizeof(char *), 15);
115       pkgs[npkgs++] = solv_strdup(argv[optind++]);
116     }
117   repo = repo_create(pool, "archpkgs2solv");
118   repo_add_repodata(repo, 0);
119   res = 0;
120   for (i = 0; i < npkgs; i++)
121     if (repo_add_arch_pkg(repo, pkgs[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|flags) == 0)
122       {
123         fprintf(stderr, "archpkgs2solv: %s\n", pool_errstr(pool));
124         res = 1;
125       }
126   repo_internalize(repo);
127   tool_write(repo, basefile, 0);
128   pool_free(pool);
129   for (c = 0; c < npkgs; c++)
130     solv_free((char *)pkgs[c]);
131   solv_free(pkgs);
132   exit(res);
133 }
134