A _count is an unsigned int, not an Id
[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
23 #include "pool.h"
24 #include "repo.h"
25 #include "util.h"
26 #define DISABLE_SPLIT
27 #include "tools_util.h"
28 #include "repo_releasefile_products.h"
29
30 #define BUFF_SIZE 8192
31
32 static void
33 add_releasefile_product(Repo *repo, FILE *fp)
34 {
35   Pool *pool = repo->pool;
36   char buf[BUFF_SIZE];
37   Id name = 0;
38   Id arch = 0;
39   Id version = 0;
40   int lnum = 0; /* line number */
41   char *ptr, *ptr1;
42
43   /* parse /etc/<xyz>-release file */
44   while (fgets(buf, sizeof(buf), fp))
45     {
46       /* remove trailing \n */
47       int l = strlen(buf);
48       if (l && buf[l - 1] == '\n')
49         buf[--l] = 0;
50       ++lnum;
51
52       if (lnum == 1)
53         {
54           /* 1st line, <name> [(<arch>)] */
55           ptr = strchr(buf, '(');
56           if (ptr)
57             {
58               ptr1 = ptr - 1;
59               *ptr++ = 0;
60             }
61           else
62             ptr1 = buf + l - 1;
63
64           /* track back until non-blank, non-digit */
65           while (ptr1 > buf
66                  && (*ptr1 == ' ' || isdigit(*ptr1) || *ptr1 == '.'))
67             --ptr1;
68           *(++ptr1) = 0;
69           name = str2id(pool, join2("product", ":", buf), 1);
70
71           if (ptr)
72             {
73               /* have arch */
74               char *ptr1 = strchr(ptr, ')');
75               if (ptr1)
76                 {
77                   *ptr1 = 0;
78                   /* downcase arch */
79                   ptr1 = ptr;
80                   while (*ptr1)
81                     {
82                       if (isupper(*ptr1))
83                          *ptr1 = tolower(*ptr1);
84                       ++ptr1;
85                     }
86                   arch = str2id(pool, ptr, 1);
87                 }
88             }
89         }
90       else if (strncmp(buf, "VERSION", 7) == 0)
91         {
92           ptr = strchr(buf + 7, '=');
93           if (ptr)
94             {
95               while (*++ptr == ' ')
96                 ;
97               version = makeevr(pool, ptr);
98             }
99         }
100     }
101   if (name)
102     {
103       Solvable *s = pool_id2solvable(pool, repo_add_solvable(repo));
104       s->name = name;
105       if (version)
106         s->evr = version;
107       if (arch)
108         s->arch = arch;
109       if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
110         s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
111     }
112 }
113
114
115 void
116 repo_add_releasefile_products(Repo *repo, const char *dirpath, int flags)
117 {
118   DIR *dir;
119   struct dirent *entry;
120   FILE *fp;
121   char *fullpath;
122
123   dir = opendir(dirpath);
124   if (!dir)
125     return;
126
127   while ((entry = readdir(dir)))
128     {
129       int len = strlen(entry->d_name);
130       if (len > 8 && !strcmp(entry->d_name + len - 8, "-release"))
131         {
132           /* skip /etc/lsb-release, thats not a product per-se */
133           if (strcmp(entry->d_name, "lsb-release") == 0)
134             continue;
135           fullpath = join2(dirpath, "/", entry->d_name);
136           if ((fp = fopen(fullpath, "r")) == 0)
137             {
138               perror(fullpath);
139               continue;
140             }
141           add_releasefile_product(repo, fp);
142         }
143     }
144   closedir(dir);
145   join_freemem();
146
147   if (!(flags & REPO_NO_INTERNALIZE))
148     {
149       if (!(flags & REPO_REUSE_REPODATA))
150         {
151           Repodata *data = repo_add_repodata(repo, 0);
152           repodata_internalize(data);
153         }
154     }
155 }
156