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