Imported Upstream version 0.7.27
[platform/upstream/libsolv.git] / examples / solv / repoinfo_type_plaindir.c
1 #if defined(ENABLE_RPMDB) || defined(ENABLE_RPMPKG)
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <dirent.h>
7 #include <sys/stat.h>
8
9 #include "pool.h"
10 #include "repo.h"
11
12 #ifdef SUSE
13 #include "repo_autopattern.h"
14 #endif
15 #include "repoinfo.h"
16 #include "repoinfo_cache.h"
17 #include "repoinfo_download.h"
18 #include "repoinfo_type_rpmmd.h"
19 #include "ext/repo_rpmdb.h"
20
21 static inline int endswith(const char* str, const char* suf)
22 {
23   if (strlen(str) < strlen(suf))
24     return 0;
25   return strcmp(str + strlen(str) - strlen(suf), suf) == 0;
26 }
27
28 int
29 plaindir_load(struct repoinfo *cinfo, Pool **sigpoolp)
30 {
31   Repo *repo = cinfo->repo;
32   Repodata *data;
33   DIR *dp;
34   struct dirent *de;
35   struct stat stb;
36
37   printf("plaindir repo '%s':", cinfo->alias);
38   fflush(stdout);
39   if (stat(cinfo->path, &stb))
40     {
41       perror(cinfo->path);
42       return -1;
43     }
44   calc_cookie_stat(&stb, REPOKEY_TYPE_SHA256, NULL, cinfo->cookie);
45   cinfo->cookieset = 1;
46   if (usecachedrepo(cinfo, 0, 1))
47     {
48       printf(" cached\n");
49       return 1;
50     }
51   printf(" reading\n");
52   if ((dp = opendir(cinfo->path)) == 0)
53     {
54       perror(cinfo->path);
55       return -1;
56     }
57   while ((de = readdir(dp)) != 0)
58     {
59       if (de->d_name[0] == 0 || de->d_name[0] == '.')
60         continue;
61       if (!endswith(de->d_name, ".rpm") || endswith(de->d_name, ".delta.rpm") || endswith(de->d_name, ".patch.rpm"))
62         continue;
63       char* fn = solv_dupjoin(cinfo->path, "/", de->d_name);
64       repo_add_rpm(repo, fn, 0);
65       solv_free(fn);
66     }
67   closedir(dp);
68
69 #ifdef SUSE
70   repo_add_autopattern(repo, 0);
71 #endif
72   data = repo_add_repodata(repo, 0);
73   repodata_internalize(data);
74   writecachedrepo(cinfo, 0, 0);
75   repodata_create_stubs(repo_last_repodata(repo));
76   return 1;
77 }
78
79 #endif