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