]> git.ipfire.org Git - pakfire.git/blob - src/libpakfire/repocache.c
libpakfire: Move more cache logic into Pakfire
[pakfire.git] / src / libpakfire / repocache.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2013 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #define _XOPEN_SOURCE 500
22 #include <ftw.h>
23 #include <stdio.h>
24 #include <sys/stat.h>
25
26 #include <pakfire/cache.h>
27 #include <pakfire/constants.h>
28 #include <pakfire/package.h>
29 #include <pakfire/private.h>
30 #include <pakfire/repo.h>
31 #include <pakfire/repocache.h>
32 #include <pakfire/types.h>
33 #include <pakfire/util.h>
34
35 static char* pakfire_repocache_prefix(PakfireRepoCache repo_cache) {
36 const char* repo_name = pakfire_repo_get_name(repo_cache->repo);
37
38 char buffer[STRING_SIZE] = "";
39 snprintf(buffer, sizeof(buffer), "repodata/%s", repo_name);
40
41 return pakfire_strdup(buffer);
42 }
43
44 PAKFIRE_EXPORT PakfireRepoCache pakfire_repocache_create(PakfireRepo repo) {
45 PakfireRepoCache repo_cache = pakfire_calloc(1, sizeof(*repo_cache));
46
47 repo_cache->repo = repo;
48 repo_cache->prefix = pakfire_repocache_prefix(repo_cache);
49
50 return repo_cache;
51 }
52
53 PAKFIRE_EXPORT void pakfire_repocache_free(PakfireRepoCache repo_cache) {
54 pakfire_free(repo_cache->prefix);
55 pakfire_free(repo_cache);
56 }
57
58 PAKFIRE_EXPORT char* pakfire_repocache_get_cache_path(PakfireRepoCache repo_cache, const char* path) {
59 return pakfire_path_join(repo_cache->prefix, path);
60 }
61
62 PAKFIRE_EXPORT char* pakfire_repocache_get_full_path(PakfireRepoCache repo_cache, const char* path) {
63 char* cache_path = pakfire_repocache_get_cache_path(repo_cache, path);
64
65 PakfireCache cache = pakfire_repocache_cache(repo_cache);
66 char* full_path = pakfire_cache_get_full_path(cache, cache_path);
67
68 pakfire_free(cache_path);
69
70 return full_path;
71 }
72
73 PAKFIRE_EXPORT int pakfire_repocache_has_file(PakfireRepoCache repo_cache, const char* filename) {
74 char* cache_filename = pakfire_repocache_get_cache_path(repo_cache, filename);
75
76 PakfireCache cache = pakfire_repocache_cache(repo_cache);
77 int r = pakfire_cache_has_file(cache, cache_filename);
78
79 pakfire_free(cache_filename);
80 return r;
81 }
82
83 PAKFIRE_EXPORT int pakfire_repocache_age(PakfireRepoCache repo_cache, const char* filename) {
84 char* cache_filename = pakfire_repocache_get_cache_path(repo_cache, filename);
85
86 PakfireCache cache = pakfire_repocache_cache(repo_cache);
87 int age = pakfire_cache_age(cache, cache_filename);
88 pakfire_free(cache_filename);
89
90 return age;
91 }
92
93 PAKFIRE_EXPORT FILE* pakfire_repocache_open(PakfireRepoCache repo_cache, const char* filename, const char* flags) {
94 char* cache_filename = pakfire_repocache_get_cache_path(repo_cache, filename);
95
96 PakfireCache cache = pakfire_repocache_cache(repo_cache);
97 FILE* fp = pakfire_cache_open(cache, cache_filename, flags);
98 pakfire_free(cache_filename);
99
100 return fp;
101 }