]>
Commit | Line | Data |
---|---|---|
e2faeb46 | 1 | /* |
5b2de3d8 | 2 | * be_sync.c |
23229097 | 3 | * |
d2882404 | 4 | * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org> |
6cebd4e6 | 5 | * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> |
23229097 | 6 | * |
e2faeb46 AF |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
9781d0d6 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
e2faeb46 AF |
19 | */ |
20 | ||
21 | #include "config.h" | |
869e81e1 | 22 | |
e2faeb46 | 23 | #include <errno.h> |
81dd9d3e | 24 | #include <limits.h> |
869e81e1 | 25 | |
2f4ee434 XC |
26 | /* libarchive */ |
27 | #include <archive.h> | |
28 | #include <archive_entry.h> | |
29 | ||
869e81e1 | 30 | /* libalpm */ |
e2faeb46 | 31 | #include "util.h" |
5b2de3d8 | 32 | #include "log.h" |
e2faeb46 | 33 | #include "alpm.h" |
5b2de3d8 | 34 | #include "alpm_list.h" |
aa1c0ba9 | 35 | #include "package.h" |
5b2de3d8 | 36 | #include "handle.h" |
0c2cc108 | 37 | #include "delta.h" |
c244cfec | 38 | #include "deps.h" |
04600384 | 39 | #include "dload.h" |
d37ad048 | 40 | |
04600384 | 41 | /** Update a package database |
63fc9360 SN |
42 | * |
43 | * An update of the package database \a db will be attempted. Unless | |
44 | * \a force is true, the update will only be performed if the remote | |
45 | * database was modified since the last update. | |
46 | * | |
47 | * A transaction is necessary for this operation, in order to obtain a | |
48 | * database lock. During this transaction the front-end will be informed | |
49 | * of the download progress of the database via the download callback. | |
50 | * | |
51 | * Example: | |
52 | * @code | |
ed1aef7b | 53 | * alpm_list_t *syncs = alpm_option_get_syncdbs(); |
8ff3b870 | 54 | * if(alpm_trans_init(0, NULL, NULL, NULL) == 0) { |
ed1aef7b XC |
55 | * for(i = syncs; i; i = alpm_list_next(i)) { |
56 | * pmdb_t *db = alpm_list_getdata(i); | |
57 | * result = alpm_db_update(0, db); | |
58 | * alpm_trans_release(); | |
63fc9360 | 59 | * |
ed1aef7b XC |
60 | * if(result < 0) { |
61 | * printf("Unable to update database: %s\n", alpm_strerrorlast()); | |
62 | * } else if(result == 1) { | |
63 | * printf("Database already up to date\n"); | |
64 | * } else { | |
65 | * printf("Database updated\n"); | |
66 | * } | |
63fc9360 SN |
67 | * } |
68 | * } | |
69 | * @endcode | |
70 | * | |
71 | * @ingroup alpm_databases | |
72 | * @note After a successful update, the \link alpm_db_get_pkgcache() | |
73 | * package cache \endlink will be invalidated | |
04600384 DM |
74 | * @param force if true, then forces the update, otherwise update only in case |
75 | * the database isn't up to date | |
76 | * @param db pointer to the package database to update | |
ed1aef7b | 77 | * @return 0 on success, -1 on error (pm_errno is set accordingly), 1 if up to |
04600384 | 78 | * to date |
e8275fa9 | 79 | */ |
04600384 | 80 | int SYMEXPORT alpm_db_update(int force, pmdb_t *db) |
e8275fa9 | 81 | { |
149ab6b2 AM |
82 | char *dbfile, *syncpath; |
83 | const char *dbpath; | |
1ff81182 | 84 | struct stat buf; |
0966c33a | 85 | size_t len; |
04600384 DM |
86 | int ret; |
87 | ||
da648bc2 AG |
88 | ALPM_LOG_FUNC; |
89 | ||
04600384 DM |
90 | /* Sanity checks */ |
91 | ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1)); | |
92 | ASSERT(db != NULL && db != handle->db_local, RET_ERR(PM_ERR_WRONG_ARGS, -1)); | |
04600384 DM |
93 | |
94 | if(!alpm_list_find_ptr(handle->dbs_sync, db)) { | |
95 | RET_ERR(PM_ERR_DB_NOT_FOUND, -1); | |
96 | } | |
97 | ||
68dcabdf | 98 | len = strlen(db->treename) + 4; |
0966c33a | 99 | MALLOC(dbfile, len, RET_ERR(PM_ERR_MEMORY, -1)); |
68dcabdf | 100 | sprintf(dbfile, "%s.db", db->treename); |
0966c33a | 101 | |
04600384 | 102 | dbpath = alpm_option_get_dbpath(); |
a8355994 AM |
103 | len = strlen(dbpath) + 6; |
104 | MALLOC(syncpath, len, RET_ERR(PM_ERR_MEMORY, -1)); | |
105 | sprintf(syncpath, "%s%s", dbpath, "sync/"); | |
04600384 | 106 | |
1ff81182 DM |
107 | if(stat(syncpath, &buf) != 0) { |
108 | _alpm_log(PM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", | |
109 | syncpath); | |
110 | if(_alpm_makepath(syncpath) != 0) { | |
111 | free(dbfile); | |
112 | free(syncpath); | |
113 | RET_ERR(PM_ERR_SYSTEM, -1); | |
114 | } | |
115 | } else if(!S_ISDIR(buf.st_mode)) { | |
116 | _alpm_log(PM_LOG_WARNING, _("removing invalid file: %s\n"), syncpath); | |
117 | if(unlink(syncpath) != 0 || _alpm_makepath(syncpath) != 0) { | |
118 | free(dbfile); | |
119 | free(syncpath); | |
120 | RET_ERR(PM_ERR_SYSTEM, -1); | |
121 | } | |
122 | } | |
123 | ||
a8355994 | 124 | ret = _alpm_download_single_file(dbfile, db->servers, syncpath, force); |
0966c33a | 125 | free(dbfile); |
a8355994 | 126 | free(syncpath); |
04600384 DM |
127 | |
128 | if(ret == 1) { | |
d2dbb04a | 129 | /* files match, do nothing */ |
04600384 DM |
130 | pm_errno = 0; |
131 | return(1); | |
132 | } else if(ret == -1) { | |
133 | /* pm_errno was set by the download code */ | |
134 | _alpm_log(PM_LOG_DEBUG, "failed to sync db: %s\n", alpm_strerrorlast()); | |
135 | return(-1); | |
2f4ee434 XC |
136 | } |
137 | ||
2f4ee434 XC |
138 | /* Cache needs to be rebuilt */ |
139 | _alpm_db_free_pkgcache(db); | |
140 | ||
04600384 | 141 | return(0); |
e8275fa9 AG |
142 | } |
143 | ||
94d3d665 | 144 | /* Forward decl so I don't reorganize the whole file right now */ |
3863e487 DM |
145 | static int sync_db_read(pmdb_t *db, struct archive *archive, |
146 | struct archive_entry *entry, pmpkg_t *likely_pkg); | |
94d3d665 | 147 | |
c00e0599 | 148 | static int sync_db_populate(pmdb_t *db) |
e2faeb46 | 149 | { |
f6711472 | 150 | int count = 0; |
5e61f077 AM |
151 | struct archive *archive; |
152 | struct archive_entry *entry; | |
3863e487 | 153 | pmpkg_t *pkg = NULL; |
e2faeb46 | 154 | |
da648bc2 AG |
155 | ALPM_LOG_FUNC; |
156 | ||
f6711472 | 157 | ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1)); |
e2faeb46 | 158 | |
5e61f077 AM |
159 | if((archive = archive_read_new()) == NULL) |
160 | RET_ERR(PM_ERR_LIBARCHIVE, 1); | |
161 | ||
162 | archive_read_support_compression_all(archive); | |
163 | archive_read_support_format_all(archive); | |
164 | ||
165 | if(archive_read_open_filename(archive, _alpm_db_path(db), | |
166 | ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) { | |
167 | _alpm_log(PM_LOG_ERROR, _("could not open %s: %s\n"), _alpm_db_path(db), | |
168 | archive_error_string(archive)); | |
fe76c353 DM |
169 | archive_read_finish(archive); |
170 | RET_ERR(PM_ERR_DB_OPEN, 1); | |
14230869 | 171 | } |
5e61f077 AM |
172 | |
173 | while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) { | |
174 | const struct stat *st; | |
ccc1c731 | 175 | |
5e61f077 AM |
176 | st = archive_entry_stat(entry); |
177 | ||
4a8e396a | 178 | if(S_ISDIR(st->st_mode)) { |
3863e487 DM |
179 | const char *name; |
180 | ||
4a8e396a AM |
181 | pkg = _alpm_pkg_new(); |
182 | if(pkg == NULL) { | |
183 | archive_read_finish(archive); | |
fe76c353 | 184 | RET_ERR(PM_ERR_MEMORY, -1); |
4a8e396a | 185 | } |
5e61f077 | 186 | |
4a8e396a | 187 | name = archive_entry_pathname(entry); |
5e61f077 | 188 | |
e464339e | 189 | if(_alpm_splitname(name, pkg) != 0) { |
4a8e396a AM |
190 | _alpm_log(PM_LOG_ERROR, _("invalid name for database entry '%s'\n"), |
191 | name); | |
192 | _alpm_pkg_free(pkg); | |
193 | continue; | |
194 | } | |
7f5dada8 | 195 | |
4a8e396a AM |
196 | /* duplicated database entries are not allowed */ |
197 | if(_alpm_pkg_find(db->pkgcache, pkg->name)) { | |
198 | _alpm_log(PM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name); | |
199 | _alpm_pkg_free(pkg); | |
200 | continue; | |
201 | } | |
444ff956 | 202 | |
4a8e396a AM |
203 | pkg->origin = PKG_FROM_SYNCDB; |
204 | pkg->ops = &default_pkg_ops; | |
205 | pkg->origin_data.db = db; | |
5e61f077 | 206 | |
4a8e396a AM |
207 | /* add to the collection */ |
208 | _alpm_log(PM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n", | |
209 | pkg->name, db->treename); | |
210 | db->pkgcache = alpm_list_add(db->pkgcache, pkg); | |
211 | count++; | |
212 | } else { | |
213 | /* we have desc, depends or deltas - parse it */ | |
3863e487 | 214 | sync_db_read(db, archive, entry, pkg); |
4a8e396a | 215 | } |
e2faeb46 AF |
216 | } |
217 | ||
f966f3a8 | 218 | db->pkgcache = alpm_list_msort(db->pkgcache, (size_t)count, _alpm_pkg_cmp); |
5e61f077 AM |
219 | archive_read_finish(archive); |
220 | ||
f6711472 | 221 | return(count); |
e2faeb46 AF |
222 | } |
223 | ||
8ac7f7e6 | 224 | #define READ_NEXT(s) do { \ |
f2dff086 DM |
225 | if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \ |
226 | s = _alpm_strtrim(buf.line); \ | |
8ac7f7e6 DM |
227 | } while(0) |
228 | ||
229 | #define READ_AND_STORE(f) do { \ | |
230 | READ_NEXT(line); \ | |
231 | STRDUP(f, line, goto error); \ | |
232 | } while(0) | |
233 | ||
234 | #define READ_AND_STORE_ALL(f) do { \ | |
235 | char *linedup; \ | |
236 | READ_NEXT(line); \ | |
237 | if(strlen(line) == 0) break; \ | |
238 | STRDUP(linedup, line, goto error); \ | |
239 | f = alpm_list_add(f, linedup); \ | |
240 | } while(1) /* note the while(1) and not (0) */ | |
241 | ||
3863e487 DM |
242 | static int sync_db_read(pmdb_t *db, struct archive *archive, |
243 | struct archive_entry *entry, pmpkg_t *likely_pkg) | |
e2faeb46 | 244 | { |
3863e487 DM |
245 | const char *entryname = NULL, *filename; |
246 | char *pkgname, *p, *q; | |
4a8e396a | 247 | pmpkg_t *pkg; |
f2dff086 | 248 | struct archive_read_buffer buf; |
e2faeb46 | 249 | |
da648bc2 AG |
250 | ALPM_LOG_FUNC; |
251 | ||
e8275fa9 AG |
252 | if(db == NULL) { |
253 | RET_ERR(PM_ERR_DB_NULL, -1); | |
254 | } | |
255 | ||
c2cce1f4 XC |
256 | if(entry != NULL) { |
257 | entryname = archive_entry_pathname(entry); | |
258 | } | |
259 | if(entryname == NULL) { | |
4a8e396a | 260 | _alpm_log(PM_LOG_DEBUG, "invalid archive entry provided to _alpm_sync_db_read, skipping\n"); |
e2faeb46 AF |
261 | return(-1); |
262 | } | |
263 | ||
4a8e396a AM |
264 | _alpm_log(PM_LOG_FUNCTION, "loading package data from archive entry %s\n", |
265 | entryname); | |
266 | ||
f2dff086 DM |
267 | memset(&buf, 0, sizeof(buf)); |
268 | /* 512K for a line length seems reasonable */ | |
269 | buf.max_line_size = 512 * 1024; | |
270 | ||
4a8e396a AM |
271 | /* get package and db file names */ |
272 | STRDUP(pkgname, entryname, RET_ERR(PM_ERR_MEMORY, -1)); | |
273 | p = pkgname + strlen(pkgname); | |
274 | for(q = --p; *q && *q != '/'; q--); | |
3863e487 | 275 | filename = q + 1; |
4a8e396a AM |
276 | for(p = --q; *p && *p != '-'; p--); |
277 | for(q = --p; *q && *q != '-'; q--); | |
278 | *q = '\0'; | |
279 | ||
280 | /* package is already in db due to parsing of directory name */ | |
3863e487 DM |
281 | if(likely_pkg && strcmp(likely_pkg->name, pkgname) == 0) { |
282 | pkg = likely_pkg; | |
283 | } else { | |
284 | pkg = _alpm_pkg_find(db->pkgcache, pkgname); | |
285 | } | |
4a8e396a AM |
286 | if(pkg == NULL) { |
287 | _alpm_log(PM_LOG_DEBUG, "package %s not found in %s sync database", | |
288 | pkgname, db->treename); | |
3bf918dc AG |
289 | return(-1); |
290 | } | |
291 | ||
3030542d DM |
292 | if(strcmp(filename, "desc") == 0 || strcmp(filename, "depends") == 0 |
293 | || strcmp(filename, "deltas") == 0) { | |
f2dff086 DM |
294 | while(_alpm_archive_fgets(archive, &buf) == ARCHIVE_OK) { |
295 | char *line = _alpm_strtrim(buf.line); | |
296 | ||
da1c11cc | 297 | if(strcmp(line, "%NAME%") == 0) { |
8ac7f7e6 DM |
298 | READ_NEXT(line); |
299 | if(strcmp(line, pkg->name) != 0) { | |
da1c11cc | 300 | _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name " |
4a8e396a | 301 | "mismatch on package %s\n"), db->treename, pkg->name); |
da1c11cc NG |
302 | } |
303 | } else if(strcmp(line, "%VERSION%") == 0) { | |
8ac7f7e6 DM |
304 | READ_NEXT(line); |
305 | if(strcmp(line, pkg->version) != 0) { | |
da1c11cc | 306 | _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version " |
4a8e396a | 307 | "mismatch on package %s\n"), db->treename, pkg->name); |
da1c11cc NG |
308 | } |
309 | } else if(strcmp(line, "%FILENAME%") == 0) { | |
8ac7f7e6 | 310 | READ_AND_STORE(pkg->filename); |
da1c11cc | 311 | } else if(strcmp(line, "%DESC%") == 0) { |
8ac7f7e6 | 312 | READ_AND_STORE(pkg->desc); |
ccc1c731 | 313 | } else if(strcmp(line, "%GROUPS%") == 0) { |
8ac7f7e6 | 314 | READ_AND_STORE_ALL(pkg->groups); |
ccc1c731 | 315 | } else if(strcmp(line, "%URL%") == 0) { |
8ac7f7e6 | 316 | READ_AND_STORE(pkg->url); |
ccc1c731 | 317 | } else if(strcmp(line, "%LICENSE%") == 0) { |
8ac7f7e6 | 318 | READ_AND_STORE_ALL(pkg->licenses); |
ccc1c731 | 319 | } else if(strcmp(line, "%ARCH%") == 0) { |
8ac7f7e6 | 320 | READ_AND_STORE(pkg->arch); |
ccc1c731 | 321 | } else if(strcmp(line, "%BUILDDATE%") == 0) { |
8ac7f7e6 | 322 | READ_NEXT(line); |
4bc6ed56 | 323 | pkg->builddate = _alpm_parsedate(line); |
ccc1c731 | 324 | } else if(strcmp(line, "%PACKAGER%") == 0) { |
8ac7f7e6 | 325 | READ_AND_STORE(pkg->packager); |
5b17d8f2 | 326 | } else if(strcmp(line, "%CSIZE%") == 0) { |
8ac7f7e6 DM |
327 | /* Note: the CSIZE and SIZE fields both share the "size" field in the |
328 | * pkginfo_t struct. This can be done b/c CSIZE is currently only used | |
329 | * in sync databases, and SIZE is only used in local databases. | |
e2faeb46 | 330 | */ |
8ac7f7e6 DM |
331 | READ_NEXT(line); |
332 | pkg->size = atol(line); | |
5e12d3de | 333 | /* also store this value to isize if isize is unset */ |
4a8e396a AM |
334 | if(pkg->isize == 0) { |
335 | pkg->isize = pkg->size; | |
5e12d3de | 336 | } |
ccc1c731 | 337 | } else if(strcmp(line, "%ISIZE%") == 0) { |
8ac7f7e6 DM |
338 | READ_NEXT(line); |
339 | pkg->isize = atol(line); | |
ccc1c731 | 340 | } else if(strcmp(line, "%MD5SUM%") == 0) { |
8ac7f7e6 | 341 | READ_AND_STORE(pkg->md5sum); |
ccc1c731 | 342 | } else if(strcmp(line, "%REPLACES%") == 0) { |
8ac7f7e6 | 343 | READ_AND_STORE_ALL(pkg->replaces); |
a44c7b89 | 344 | } else if(strcmp(line, "%DEPENDS%") == 0) { |
8ac7f7e6 DM |
345 | /* Different than the rest because of the _alpm_splitdep call. */ |
346 | while(1) { | |
347 | READ_NEXT(line); | |
348 | if(strlen(line) == 0) break; | |
349 | pkg->depends = alpm_list_add(pkg->depends, _alpm_splitdep(line)); | |
e2faeb46 | 350 | } |
ccc1c731 | 351 | } else if(strcmp(line, "%OPTDEPENDS%") == 0) { |
8ac7f7e6 | 352 | READ_AND_STORE_ALL(pkg->optdepends); |
ccc1c731 | 353 | } else if(strcmp(line, "%CONFLICTS%") == 0) { |
8ac7f7e6 | 354 | READ_AND_STORE_ALL(pkg->conflicts); |
ccc1c731 | 355 | } else if(strcmp(line, "%PROVIDES%") == 0) { |
8ac7f7e6 | 356 | READ_AND_STORE_ALL(pkg->provides); |
3030542d | 357 | } else if(strcmp(line, "%DELTAS%") == 0) { |
8ac7f7e6 | 358 | READ_AND_STORE_ALL(pkg->deltas); |
3030542d | 359 | } |
e2faeb46 | 360 | } |
4a8e396a AM |
361 | } else { |
362 | /* unknown database file */ | |
363 | _alpm_log(PM_LOG_DEBUG, "unknown database file: %s", filename); | |
e2faeb46 AF |
364 | } |
365 | ||
e2faeb46 | 366 | error: |
4a8e396a | 367 | FREE(pkgname); |
94d3d665 | 368 | /* TODO: return 0 always? */ |
4a8e396a | 369 | return(0); |
e2faeb46 AF |
370 | } |
371 | ||
fc32faaa | 372 | struct db_operations sync_db_ops = { |
c00e0599 | 373 | .populate = sync_db_populate, |
fc32faaa AM |
374 | .unregister = _alpm_db_unregister, |
375 | }; | |
376 | ||
377 | pmdb_t *_alpm_db_register_sync(const char *treename) | |
378 | { | |
379 | pmdb_t *db; | |
380 | alpm_list_t *i; | |
381 | ||
382 | ALPM_LOG_FUNC; | |
383 | ||
384 | for(i = handle->dbs_sync; i; i = i->next) { | |
385 | pmdb_t *sdb = i->data; | |
386 | if(strcmp(treename, sdb->treename) == 0) { | |
387 | _alpm_log(PM_LOG_DEBUG, "attempt to re-register the '%s' database, using existing\n", sdb->treename); | |
388 | return sdb; | |
389 | } | |
390 | } | |
391 | ||
392 | _alpm_log(PM_LOG_DEBUG, "registering sync database '%s'\n", treename); | |
393 | ||
394 | db = _alpm_db_new(treename, 0); | |
395 | db->ops = &sync_db_ops; | |
396 | if(db == NULL) { | |
397 | RET_ERR(PM_ERR_DB_CREATE, NULL); | |
398 | } | |
399 | ||
400 | handle->dbs_sync = alpm_list_add(handle->dbs_sync, db); | |
401 | return(db); | |
402 | } | |
403 | ||
404 | ||
e2faeb46 | 405 | /* vim: set ts=2 sw=2 noet: */ |