make repodata_stringify return the result string
[platform/upstream/libsolv.git] / ext / repo_releasefile_products.c
1 /*
2  * repo_products.c
3  *
4  * Parses all files below 'proddir'
5  * See http://en.opensuse.org/Product_Management/Code11
6  *
7  *
8  * Copyright (c) 2008, Novell Inc.
9  *
10  * This program is licensed under the BSD license, read LICENSE.BSD
11  * for further information
12  */
13
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <dirent.h>
21 #include <ctype.h>
22 #include <errno.h>
23
24 #include "pool.h"
25 #include "repo.h"
26 #include "util.h"
27 #define DISABLE_SPLIT
28 #include "tools_util.h"
29 #include "repo_releasefile_products.h"
30
31 #define BUFF_SIZE 8192
32
33 struct parsedata {
34   Repo *repo;
35   struct joindata jd;
36 };
37
38 static void
39 add_releasefile_product(struct parsedata *pd, FILE *fp)
40 {
41   Repo *repo = pd->repo;
42   Pool *pool = repo->pool;
43   char buf[BUFF_SIZE];
44   Id name = 0;
45   Id arch = 0;
46   Id version = 0;
47   int lnum = 0; /* line number */
48   char *ptr, *ptr1;
49
50   /* parse /etc/<xyz>-release file */
51   while (fgets(buf, sizeof(buf), fp))
52     {
53       /* remove trailing \n */
54       int l = strlen(buf);
55       if (l && buf[l - 1] == '\n')
56         buf[--l] = 0;
57       ++lnum;
58
59       if (lnum == 1)
60         {
61           /* 1st line, <name> [(<arch>)] */
62           ptr = strchr(buf, '(');
63           if (ptr)
64             {
65               ptr1 = ptr - 1;
66               *ptr++ = 0;
67             }
68           else
69             ptr1 = buf + l - 1;
70
71           /* track back until non-blank, non-digit */
72           while (ptr1 > buf
73                  && (*ptr1 == ' ' || isdigit(*ptr1) || *ptr1 == '.'))
74             --ptr1;
75           *(++ptr1) = 0;
76           name = pool_str2id(pool, join2(&pd->jd, "product", ":", buf), 1);
77
78           if (ptr)
79             {
80               /* have arch */
81               char *ptr1 = strchr(ptr, ')');
82               if (ptr1)
83                 {
84                   *ptr1 = 0;
85                   /* downcase arch */
86                   ptr1 = ptr;
87                   while (*ptr1)
88                     {
89                       if (isupper(*ptr1))
90                          *ptr1 = tolower(*ptr1);
91                       ++ptr1;
92                     }
93                   arch = pool_str2id(pool, ptr, 1);
94                 }
95             }
96         }
97       else if (strncmp(buf, "VERSION", 7) == 0)
98         {
99           ptr = strchr(buf + 7, '=');
100           if (ptr)
101             {
102               while (*++ptr == ' ')
103                 ;
104               version = makeevr(pool, ptr);
105             }
106         }
107     }
108   if (name)
109     {
110       Solvable *s = pool_id2solvable(pool, repo_add_solvable(repo));
111       s->name = name;
112       s->evr = version ? version : ID_EMPTY;
113       s->arch = arch ? arch : ARCH_NOARCH;
114       if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
115         s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
116     }
117 }
118
119
120 int
121 repo_add_releasefile_products(Repo *repo, const char *dirpath, int flags)
122 {
123   DIR *dir;
124   struct dirent *entry;
125   FILE *fp;
126   char *fullpath;
127   struct parsedata pd;
128
129   if (!dirpath)
130     dirpath = "/etc";
131   if (flags & REPO_USE_ROOTDIR)
132     dirpath = pool_prepend_rootdir(repo->pool, dirpath);
133   dir = opendir(dirpath);
134   if (!dir)
135     {
136       if (flags & REPO_USE_ROOTDIR)
137         solv_free((char *)dirpath);
138       return 0;
139     }
140
141   memset(&pd, 0, sizeof(pd));
142   pd.repo = repo;
143   while ((entry = readdir(dir)))
144     {
145       int len = strlen(entry->d_name);
146       if (len > 8 && !strcmp(entry->d_name + len - 8, "-release"))
147         {
148           /* skip /etc/lsb-release, thats not a product per-se */
149           if (strcmp(entry->d_name, "lsb-release") == 0)
150             continue;
151           fullpath = join2(&pd.jd, dirpath, "/", entry->d_name);
152           if ((fp = fopen(fullpath, "r")) == 0)
153             {
154               pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
155               continue;
156             }
157           add_releasefile_product(&pd, fp);
158           fclose(fp);
159         }
160     }
161   closedir(dir);
162   join_freemem(&pd.jd);
163   if (flags & REPO_USE_ROOTDIR)
164     solv_free((char *)dirpath);
165
166   if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
167     repodata_internalize(repo_last_repodata(repo));
168   return 0;
169 }
170