]> wolfpit.net Git - tool/Arch-pacman/.git/blob - lib/libalpm/be_local.c
libalpm: introduce a usage level for repos
[tool/Arch-pacman/.git] / lib / libalpm / be_local.c
1 /*
2 * be_local.c : backend for the local database
3 *
4 * Copyright (c) 2006-2013 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 *
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
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdint.h> /* intmax_t */
27 #include <sys/stat.h>
28 #include <dirent.h>
29 #include <limits.h> /* PATH_MAX */
30
31 /* libarchive */
32 #include <archive.h>
33 #include <archive_entry.h>
34
35 /* libalpm */
36 #include "db.h"
37 #include "alpm_list.h"
38 #include "libarchive-compat.h"
39 #include "log.h"
40 #include "util.h"
41 #include "alpm.h"
42 #include "handle.h"
43 #include "package.h"
44 #include "deps.h"
45 #include "filelist.h"
46
47 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq);
48
49 #define LAZY_LOAD(info, errret) \
50 do { \
51 if(!(pkg->infolevel & info)) { \
52 local_db_read(pkg, info); \
53 } \
54 } while(0)
55
56
57 /* Cache-specific accessor functions. These implementations allow for lazy
58 * loading by the files backend when a data member is actually needed
59 * rather than loading all pieces of information when the package is first
60 * initialized.
61 */
62
63 static const char *_cache_get_desc(alpm_pkg_t *pkg)
64 {
65 LAZY_LOAD(INFRQ_DESC, NULL);
66 return pkg->desc;
67 }
68
69 static const char *_cache_get_url(alpm_pkg_t *pkg)
70 {
71 LAZY_LOAD(INFRQ_DESC, NULL);
72 return pkg->url;
73 }
74
75 static alpm_time_t _cache_get_builddate(alpm_pkg_t *pkg)
76 {
77 LAZY_LOAD(INFRQ_DESC, 0);
78 return pkg->builddate;
79 }
80
81 static alpm_time_t _cache_get_installdate(alpm_pkg_t *pkg)
82 {
83 LAZY_LOAD(INFRQ_DESC, 0);
84 return pkg->installdate;
85 }
86
87 static const char *_cache_get_packager(alpm_pkg_t *pkg)
88 {
89 LAZY_LOAD(INFRQ_DESC, NULL);
90 return pkg->packager;
91 }
92
93 static const char *_cache_get_arch(alpm_pkg_t *pkg)
94 {
95 LAZY_LOAD(INFRQ_DESC, NULL);
96 return pkg->arch;
97 }
98
99 static off_t _cache_get_isize(alpm_pkg_t *pkg)
100 {
101 LAZY_LOAD(INFRQ_DESC, -1);
102 return pkg->isize;
103 }
104
105 static alpm_pkgreason_t _cache_get_reason(alpm_pkg_t *pkg)
106 {
107 LAZY_LOAD(INFRQ_DESC, -1);
108 return pkg->reason;
109 }
110
111 static alpm_pkgvalidation_t _cache_get_validation(alpm_pkg_t *pkg)
112 {
113 LAZY_LOAD(INFRQ_DESC, -1);
114 return pkg->validation;
115 }
116
117 static alpm_list_t *_cache_get_licenses(alpm_pkg_t *pkg)
118 {
119 LAZY_LOAD(INFRQ_DESC, NULL);
120 return pkg->licenses;
121 }
122
123 static alpm_list_t *_cache_get_groups(alpm_pkg_t *pkg)
124 {
125 LAZY_LOAD(INFRQ_DESC, NULL);
126 return pkg->groups;
127 }
128
129 static int _cache_has_scriptlet(alpm_pkg_t *pkg)
130 {
131 LAZY_LOAD(INFRQ_SCRIPTLET, NULL);
132 return pkg->scriptlet;
133 }
134
135 static alpm_list_t *_cache_get_depends(alpm_pkg_t *pkg)
136 {
137 LAZY_LOAD(INFRQ_DESC, NULL);
138 return pkg->depends;
139 }
140
141 static alpm_list_t *_cache_get_optdepends(alpm_pkg_t *pkg)
142 {
143 LAZY_LOAD(INFRQ_DESC, NULL);
144 return pkg->optdepends;
145 }
146
147 static alpm_list_t *_cache_get_conflicts(alpm_pkg_t *pkg)
148 {
149 LAZY_LOAD(INFRQ_DESC, NULL);
150 return pkg->conflicts;
151 }
152
153 static alpm_list_t *_cache_get_provides(alpm_pkg_t *pkg)
154 {
155 LAZY_LOAD(INFRQ_DESC, NULL);
156 return pkg->provides;
157 }
158
159 static alpm_list_t *_cache_get_replaces(alpm_pkg_t *pkg)
160 {
161 LAZY_LOAD(INFRQ_DESC, NULL);
162 return pkg->replaces;
163 }
164
165 static alpm_filelist_t *_cache_get_files(alpm_pkg_t *pkg)
166 {
167 LAZY_LOAD(INFRQ_FILES, NULL);
168 return &(pkg->files);
169 }
170
171 static alpm_list_t *_cache_get_backup(alpm_pkg_t *pkg)
172 {
173 LAZY_LOAD(INFRQ_FILES, NULL);
174 return pkg->backup;
175 }
176
177 /**
178 * Open a package changelog for reading. Similar to fopen in functionality,
179 * except that the returned 'file stream' is from the database.
180 * @param pkg the package (from db) to read the changelog
181 * @return a 'file stream' to the package changelog
182 */
183 static void *_cache_changelog_open(alpm_pkg_t *pkg)
184 {
185 alpm_db_t *db = alpm_pkg_get_db(pkg);
186 char *clfile = _alpm_local_db_pkgpath(db, pkg, "changelog");
187 FILE *f = fopen(clfile, "r");
188 free(clfile);
189 return f;
190 }
191
192 /**
193 * Read data from an open changelog 'file stream'. Similar to fread in
194 * functionality, this function takes a buffer and amount of data to read.
195 * @param ptr a buffer to fill with raw changelog data
196 * @param size the size of the buffer
197 * @param pkg the package that the changelog is being read from
198 * @param fp a 'file stream' to the package changelog
199 * @return the number of characters read, or 0 if there is no more data
200 */
201 static size_t _cache_changelog_read(void *ptr, size_t size,
202 const alpm_pkg_t UNUSED *pkg, void *fp)
203 {
204 return fread(ptr, 1, size, (FILE *)fp);
205 }
206
207 /**
208 * Close a package changelog for reading. Similar to fclose in functionality,
209 * except that the 'file stream' is from the database.
210 * @param pkg the package that the changelog was read from
211 * @param fp a 'file stream' to the package changelog
212 * @return whether closing the package changelog stream was successful
213 */
214 static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
215 {
216 return fclose((FILE *)fp);
217 }
218
219 /**
220 * Open a package mtree file for reading.
221 * @param pkg the local package to read the changelog of
222 * @return a archive structure for the package mtree file
223 */
224 static struct archive *_cache_mtree_open(alpm_pkg_t *pkg)
225 {
226 int r;
227 struct archive *mtree;
228
229 alpm_db_t *db = alpm_pkg_get_db(pkg);
230 char *mtfile = _alpm_local_db_pkgpath(db, pkg, "mtree");
231
232 if(access(mtfile, F_OK) != 0) {
233 /* there is no mtree file for this package */
234 goto error;
235 }
236
237 if((mtree = archive_read_new()) == NULL) {
238 pkg->handle->pm_errno = ALPM_ERR_LIBARCHIVE;
239 goto error;
240 }
241
242 _alpm_archive_read_support_filter_all(mtree);
243 archive_read_support_format_mtree(mtree);
244
245 if((r = _alpm_archive_read_open_file(mtree, mtfile, ALPM_BUFFER_SIZE))) {
246 _alpm_log(pkg->handle, ALPM_LOG_ERROR, _("error while reading file %s: %s\n"),
247 mtfile, archive_error_string(mtree));
248 pkg->handle->pm_errno = ALPM_ERR_LIBARCHIVE;
249 _alpm_archive_read_free(mtree);
250 goto error;
251 }
252
253 free(mtfile);
254 return mtree;
255
256 error:
257 free(mtfile);
258 return NULL;
259 }
260
261 /**
262 * Read next entry from a package mtree file.
263 * @param pkg the package that the mtree file is being read from
264 * @param archive the archive structure reading from the mtree file
265 * @param entry an archive_entry to store the entry header information
266 * @return 0 if end of archive is reached, non-zero otherwise.
267 */
268 static int _cache_mtree_next(const alpm_pkg_t UNUSED *pkg,
269 struct archive *mtree, struct archive_entry **entry)
270 {
271 return archive_read_next_header(mtree, entry);
272 }
273
274 /**
275 * Close a package mtree file for reading.
276 * @param pkg the package that the mtree file was read from
277 * @param mtree the archive structure use for reading from the mtree file
278 * @return whether closing the package changelog stream was successful
279 */
280 static int _cache_mtree_close(const alpm_pkg_t UNUSED *pkg,
281 struct archive *mtree)
282 {
283 return _alpm_archive_read_free(mtree);
284 }
285
286 static int _cache_force_load(alpm_pkg_t *pkg)
287 {
288 return local_db_read(pkg, INFRQ_ALL);
289 }
290
291
292 /** The local database operations struct. Get package fields through
293 * lazy accessor methods that handle any backend loading and caching
294 * logic.
295 */
296 static struct pkg_operations local_pkg_ops = {
297 .get_desc = _cache_get_desc,
298 .get_url = _cache_get_url,
299 .get_builddate = _cache_get_builddate,
300 .get_installdate = _cache_get_installdate,
301 .get_packager = _cache_get_packager,
302 .get_arch = _cache_get_arch,
303 .get_isize = _cache_get_isize,
304 .get_reason = _cache_get_reason,
305 .get_validation = _cache_get_validation,
306 .has_scriptlet = _cache_has_scriptlet,
307 .get_licenses = _cache_get_licenses,
308 .get_groups = _cache_get_groups,
309 .get_depends = _cache_get_depends,
310 .get_optdepends = _cache_get_optdepends,
311 .get_conflicts = _cache_get_conflicts,
312 .get_provides = _cache_get_provides,
313 .get_replaces = _cache_get_replaces,
314 .get_files = _cache_get_files,
315 .get_backup = _cache_get_backup,
316
317 .changelog_open = _cache_changelog_open,
318 .changelog_read = _cache_changelog_read,
319 .changelog_close = _cache_changelog_close,
320
321 .mtree_open = _cache_mtree_open,
322 .mtree_next = _cache_mtree_next,
323 .mtree_close = _cache_mtree_close,
324
325 .force_load = _cache_force_load,
326 };
327
328 static int checkdbdir(alpm_db_t *db)
329 {
330 struct stat buf;
331 const char *path = _alpm_db_path(db);
332
333 if(stat(path, &buf) != 0) {
334 _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n",
335 path);
336 if(_alpm_makepath(path) != 0) {
337 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
338 }
339 } else if(!S_ISDIR(buf.st_mode)) {
340 _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path);
341 if(unlink(path) != 0 || _alpm_makepath(path) != 0) {
342 RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1);
343 }
344 }
345 return 0;
346 }
347
348 static int is_dir(const char *path, struct dirent *entry)
349 {
350 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
351 if(entry->d_type != DT_UNKNOWN) {
352 return (entry->d_type == DT_DIR);
353 }
354 #endif
355 {
356 char buffer[PATH_MAX];
357 struct stat sbuf;
358
359 snprintf(buffer, PATH_MAX, "%s/%s", path, entry->d_name);
360
361 if(!stat(buffer, &sbuf)) {
362 return S_ISDIR(sbuf.st_mode);
363 }
364 }
365
366 return 0;
367 }
368
369 static int local_db_validate(alpm_db_t *db)
370 {
371 struct dirent *ent = NULL;
372 const char *dbpath;
373 DIR *dbdir;
374 int ret = -1;
375
376 if(db->status & DB_STATUS_VALID) {
377 return 0;
378 }
379 if(db->status & DB_STATUS_INVALID) {
380 return -1;
381 }
382
383 dbpath = _alpm_db_path(db);
384 if(dbpath == NULL) {
385 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
386 }
387 dbdir = opendir(dbpath);
388 if(dbdir == NULL) {
389 if(errno == ENOENT) {
390 /* database dir doesn't exist yet */
391 db->status |= DB_STATUS_VALID;
392 db->status &= ~DB_STATUS_INVALID;
393 db->status &= ~DB_STATUS_EXISTS;
394 db->status |= DB_STATUS_MISSING;
395 return 0;
396 } else {
397 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
398 }
399 }
400 db->status |= DB_STATUS_EXISTS;
401 db->status &= ~DB_STATUS_MISSING;
402
403 while((ent = readdir(dbdir)) != NULL) {
404 const char *name = ent->d_name;
405 char path[PATH_MAX];
406
407 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
408 continue;
409 }
410 if(!is_dir(dbpath, ent)) {
411 continue;
412 }
413
414 snprintf(path, PATH_MAX, "%s%s/depends", dbpath, name);
415 if(access(path, F_OK) == 0) {
416 /* we found a depends file- bail */
417 db->status &= ~DB_STATUS_VALID;
418 db->status |= DB_STATUS_INVALID;
419 db->handle->pm_errno = ALPM_ERR_DB_VERSION;
420 goto done;
421 }
422 }
423 /* we found no depends file after full scan */
424 db->status |= DB_STATUS_VALID;
425 db->status &= ~DB_STATUS_INVALID;
426 ret = 0;
427
428 done:
429 if(dbdir) {
430 closedir(dbdir);
431 }
432
433 return ret;
434 }
435
436 static int local_db_populate(alpm_db_t *db)
437 {
438 size_t est_count;
439 int count = 0;
440 struct stat buf;
441 struct dirent *ent = NULL;
442 const char *dbpath;
443 DIR *dbdir;
444
445 if(db->status & DB_STATUS_INVALID) {
446 RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1);
447 }
448 /* note: DB_STATUS_MISSING is not fatal for local database */
449
450 dbpath = _alpm_db_path(db);
451 if(dbpath == NULL) {
452 /* pm_errno set in _alpm_db_path() */
453 return -1;
454 }
455
456 dbdir = opendir(dbpath);
457 if(dbdir == NULL) {
458 if(errno == ENOENT) {
459 /* no database existing yet is not an error */
460 db->status &= ~DB_STATUS_EXISTS;
461 db->status |= DB_STATUS_MISSING;
462 return 0;
463 }
464 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
465 }
466 if(fstat(dirfd(dbdir), &buf) != 0) {
467 RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
468 }
469 db->status |= DB_STATUS_EXISTS;
470 db->status &= ~DB_STATUS_MISSING;
471 if(buf.st_nlink >= 2) {
472 est_count = buf.st_nlink;
473 } else {
474 /* Some filesystems don't subscribe to the two-implicit links school of
475 * thought, e.g. BTRFS, HFS+. See
476 * http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
477 */
478 est_count = 0;
479 while(readdir(dbdir) != NULL) {
480 est_count++;
481 }
482 rewinddir(dbdir);
483 }
484 if(est_count >= 2) {
485 /* subtract the '.' and '..' pointers to get # of children */
486 est_count -= 2;
487 }
488
489 db->pkgcache = _alpm_pkghash_create(est_count);
490 if(db->pkgcache == NULL){
491 closedir(dbdir);
492 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
493 }
494
495 while((ent = readdir(dbdir)) != NULL) {
496 const char *name = ent->d_name;
497
498 alpm_pkg_t *pkg;
499
500 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
501 continue;
502 }
503 if(!is_dir(dbpath, ent)) {
504 continue;
505 }
506
507 pkg = _alpm_pkg_new();
508 if(pkg == NULL) {
509 closedir(dbdir);
510 RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
511 }
512 /* split the db entry name */
513 if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
514 &(pkg->name_hash)) != 0) {
515 _alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
516 name);
517 _alpm_pkg_free(pkg);
518 continue;
519 }
520
521 /* duplicated database entries are not allowed */
522 if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
523 _alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
524 _alpm_pkg_free(pkg);
525 continue;
526 }
527
528 pkg->origin = ALPM_PKG_FROM_LOCALDB;
529 pkg->origin_data.db = db;
530 pkg->ops = &local_pkg_ops;
531 pkg->handle = db->handle;
532
533 /* explicitly read with only 'BASE' data, accessors will handle the rest */
534 if(local_db_read(pkg, INFRQ_BASE) == -1) {
535 _alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
536 _alpm_pkg_free(pkg);
537 continue;
538 }
539
540 /* add to the collection */
541 _alpm_log(db->handle, ALPM_LOG_FUNCTION, "adding '%s' to package cache for db '%s'\n",
542 pkg->name, db->treename);
543 db->pkgcache = _alpm_pkghash_add(db->pkgcache, pkg);
544 count++;
545 }
546
547 closedir(dbdir);
548 if(count > 0) {
549 db->pkgcache->list = alpm_list_msort(db->pkgcache->list, (size_t)count, _alpm_pkg_cmp);
550 }
551 _alpm_log(db->handle, ALPM_LOG_DEBUG, "added %d packages to package cache for db '%s'\n",
552 count, db->treename);
553
554 return count;
555 }
556
557 /* Note: the return value must be freed by the caller */
558 char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
559 const char *filename)
560 {
561 size_t len;
562 char *pkgpath;
563 const char *dbpath;
564
565 dbpath = _alpm_db_path(db);
566 len = strlen(dbpath) + strlen(info->name) + strlen(info->version) + 3;
567 len += filename ? strlen(filename) : 0;
568 MALLOC(pkgpath, len, RET_ERR(db->handle, ALPM_ERR_MEMORY, NULL));
569 sprintf(pkgpath, "%s%s-%s/%s", dbpath, info->name, info->version,
570 filename ? filename : "");
571 return pkgpath;
572 }
573
574 #define READ_NEXT() do { \
575 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
576 _alpm_strip_newline(line, 0); \
577 } while(0)
578
579 #define READ_AND_STORE(f) do { \
580 READ_NEXT(); \
581 STRDUP(f, line, goto error); \
582 } while(0)
583
584 #define READ_AND_STORE_ALL(f) do { \
585 char *linedup; \
586 if(fgets(line, sizeof(line), fp) == NULL) {\
587 if(!feof(fp)) goto error; else break; \
588 } \
589 if(_alpm_strip_newline(line, 0) == 0) break; \
590 STRDUP(linedup, line, goto error); \
591 f = alpm_list_add(f, linedup); \
592 } while(1) /* note the while(1) and not (0) */
593
594 #define READ_AND_SPLITDEP(f) do { \
595 if(fgets(line, sizeof(line), fp) == NULL) {\
596 if(!feof(fp)) goto error; else break; \
597 } \
598 if(_alpm_strip_newline(line, 0) == 0) break; \
599 f = alpm_list_add(f, _alpm_splitdep(line)); \
600 } while(1) /* note the while(1) and not (0) */
601
602 static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
603 {
604 FILE *fp = NULL;
605 char line[1024];
606 alpm_db_t *db = info->origin_data.db;
607
608 /* bitmask logic here:
609 * infolevel: 00001111
610 * inforeq: 00010100
611 * & result: 00000100
612 * == to inforeq? nope, we need to load more info. */
613 if((info->infolevel & inforeq) == inforeq) {
614 /* already loaded all of this info, do nothing */
615 return 0;
616 }
617
618 if(info->infolevel & INFRQ_ERROR) {
619 /* We've encountered an error loading this package before. Don't attempt
620 * repeated reloads, just give up. */
621 return -1;
622 }
623
624 _alpm_log(db->handle, ALPM_LOG_FUNCTION,
625 "loading package data for %s : level=0x%x\n",
626 info->name, inforeq);
627
628 /* clear out 'line', to be certain - and to make valgrind happy */
629 memset(line, 0, sizeof(line));
630
631 /* DESC */
632 if(inforeq & INFRQ_DESC && !(info->infolevel & INFRQ_DESC)) {
633 char *path = _alpm_local_db_pkgpath(db, info, "desc");
634 if(!path || (fp = fopen(path, "r")) == NULL) {
635 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
636 free(path);
637 goto error;
638 }
639 free(path);
640 while(!feof(fp)) {
641 if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
642 goto error;
643 }
644 if(_alpm_strip_newline(line, 0) == 0) {
645 /* length of stripped line was zero */
646 continue;
647 }
648 if(strcmp(line, "%NAME%") == 0) {
649 READ_NEXT();
650 if(strcmp(line, info->name) != 0) {
651 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: name "
652 "mismatch on package %s\n"), db->treename, info->name);
653 }
654 } else if(strcmp(line, "%VERSION%") == 0) {
655 READ_NEXT();
656 if(strcmp(line, info->version) != 0) {
657 _alpm_log(db->handle, ALPM_LOG_ERROR, _("%s database is inconsistent: version "
658 "mismatch on package %s\n"), db->treename, info->name);
659 }
660 } else if(strcmp(line, "%DESC%") == 0) {
661 READ_AND_STORE(info->desc);
662 } else if(strcmp(line, "%GROUPS%") == 0) {
663 READ_AND_STORE_ALL(info->groups);
664 } else if(strcmp(line, "%URL%") == 0) {
665 READ_AND_STORE(info->url);
666 } else if(strcmp(line, "%LICENSE%") == 0) {
667 READ_AND_STORE_ALL(info->licenses);
668 } else if(strcmp(line, "%ARCH%") == 0) {
669 READ_AND_STORE(info->arch);
670 } else if(strcmp(line, "%BUILDDATE%") == 0) {
671 READ_NEXT();
672 info->builddate = _alpm_parsedate(line);
673 } else if(strcmp(line, "%INSTALLDATE%") == 0) {
674 READ_NEXT();
675 info->installdate = _alpm_parsedate(line);
676 } else if(strcmp(line, "%PACKAGER%") == 0) {
677 READ_AND_STORE(info->packager);
678 } else if(strcmp(line, "%REASON%") == 0) {
679 READ_NEXT();
680 info->reason = (alpm_pkgreason_t)atoi(line);
681 } else if(strcmp(line, "%VALIDATION%") == 0) {
682 alpm_list_t *i, *v = NULL;
683 READ_AND_STORE_ALL(v);
684 for(i = v; i; i = alpm_list_next(i))
685 {
686 if(strcmp(i->data, "none") == 0) {
687 info->validation |= ALPM_PKG_VALIDATION_NONE;
688 } else if(strcmp(i->data, "md5") == 0) {
689 info->validation |= ALPM_PKG_VALIDATION_MD5SUM;
690 } else if(strcmp(i->data, "sha256") == 0) {
691 info->validation |= ALPM_PKG_VALIDATION_SHA256SUM;
692 } else if(strcmp(i->data, "pgp") == 0) {
693 info->validation |= ALPM_PKG_VALIDATION_SIGNATURE;
694 } else {
695 _alpm_log(db->handle, ALPM_LOG_WARNING,
696 _("unknown validation type for package %s: %s\n"),
697 info->name, (const char *)i->data);
698 }
699 }
700 FREELIST(v);
701 } else if(strcmp(line, "%SIZE%") == 0) {
702 READ_NEXT();
703 info->isize = _alpm_strtoofft(line);
704 } else if(strcmp(line, "%REPLACES%") == 0) {
705 READ_AND_SPLITDEP(info->replaces);
706 } else if(strcmp(line, "%DEPENDS%") == 0) {
707 READ_AND_SPLITDEP(info->depends);
708 } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
709 READ_AND_SPLITDEP(info->optdepends);
710 } else if(strcmp(line, "%CONFLICTS%") == 0) {
711 READ_AND_SPLITDEP(info->conflicts);
712 } else if(strcmp(line, "%PROVIDES%") == 0) {
713 READ_AND_SPLITDEP(info->provides);
714 }
715 }
716 fclose(fp);
717 fp = NULL;
718 info->infolevel |= INFRQ_DESC;
719 }
720
721 /* FILES */
722 if(inforeq & INFRQ_FILES && !(info->infolevel & INFRQ_FILES)) {
723 char *path = _alpm_local_db_pkgpath(db, info, "files");
724 if(!path || (fp = fopen(path, "r")) == NULL) {
725 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
726 free(path);
727 goto error;
728 }
729 free(path);
730 while(fgets(line, sizeof(line), fp)) {
731 _alpm_strip_newline(line, 0);
732 if(strcmp(line, "%FILES%") == 0) {
733 size_t files_count = 0, files_size = 0, len;
734 alpm_file_t *files = NULL;
735
736 while(fgets(line, sizeof(line), fp) &&
737 (len = _alpm_strip_newline(line, 0))) {
738 if(files_count >= files_size) {
739 size_t old_size = files_size;
740 if(files_size == 0) {
741 files_size = 8;
742 } else {
743 files_size *= 2;
744 }
745 files = realloc(files, sizeof(alpm_file_t) * files_size);
746 if(!files) {
747 _alpm_alloc_fail(sizeof(alpm_file_t) * files_size);
748 goto error;
749 }
750 /* ensure all new memory is zeroed out, in both the initial
751 * allocation and later reallocs */
752 memset(files + old_size, 0,
753 sizeof(alpm_file_t) * (files_size - old_size));
754 }
755 /* since we know the length of the file string already,
756 * we can do malloc + memcpy rather than strdup */
757 len += 1;
758 files[files_count].name = malloc(len);
759 if(files[files_count].name == NULL) {
760 _alpm_alloc_fail(len);
761 goto error;
762 }
763 memcpy(files[files_count].name, line, len);
764 files_count++;
765 }
766 /* attempt to hand back any memory we don't need */
767 files = realloc(files, sizeof(alpm_file_t) * files_count);
768 /* make sure the list is sorted */
769 qsort(files, files_count, sizeof(alpm_file_t), _alpm_files_cmp);
770 info->files.count = files_count;
771 info->files.files = files;
772 } else if(strcmp(line, "%BACKUP%") == 0) {
773 while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line, 0)) {
774 alpm_backup_t *backup;
775 CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
776 if(_alpm_split_backup(line, &backup)) {
777 goto error;
778 }
779 info->backup = alpm_list_add(info->backup, backup);
780 }
781 }
782 }
783 fclose(fp);
784 fp = NULL;
785 info->infolevel |= INFRQ_FILES;
786 }
787
788 /* INSTALL */
789 if(inforeq & INFRQ_SCRIPTLET && !(info->infolevel & INFRQ_SCRIPTLET)) {
790 char *path = _alpm_local_db_pkgpath(db, info, "install");
791 if(access(path, F_OK) == 0) {
792 info->scriptlet = 1;
793 }
794 free(path);
795 info->infolevel |= INFRQ_SCRIPTLET;
796 }
797
798 return 0;
799
800 error:
801 info->infolevel |= INFRQ_ERROR;
802 if(fp) {
803 fclose(fp);
804 }
805 return -1;
806 }
807
808 int _alpm_local_db_prepare(alpm_db_t *db, alpm_pkg_t *info)
809 {
810 mode_t oldmask;
811 int retval = 0;
812 char *pkgpath;
813
814 if(checkdbdir(db) != 0) {
815 return -1;
816 }
817
818 oldmask = umask(0000);
819 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
820
821 if((retval = mkdir(pkgpath, 0755)) != 0) {
822 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not create directory %s: %s\n"),
823 pkgpath, strerror(errno));
824 }
825
826 free(pkgpath);
827 umask(oldmask);
828
829 return retval;
830 }
831
832 static void write_deps(FILE *fp, const char *header, alpm_list_t *deplist)
833 {
834 alpm_list_t *lp;
835 if(!deplist) {
836 return;
837 }
838 fputs(header, fp);
839 fputc('\n', fp);
840 for(lp = deplist; lp; lp = lp->next) {
841 char *depstring = alpm_dep_compute_string(lp->data);
842 fputs(depstring, fp);
843 fputc('\n', fp);
844 free(depstring);
845 }
846 fputc('\n', fp);
847 }
848
849 int _alpm_local_db_write(alpm_db_t *db, alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
850 {
851 FILE *fp = NULL;
852 mode_t oldmask;
853 alpm_list_t *lp;
854 int retval = 0;
855
856 if(db == NULL || info == NULL || !(db->status & DB_STATUS_LOCAL)) {
857 return -1;
858 }
859
860 /* make sure we have a sane umask */
861 oldmask = umask(0022);
862
863 /* DESC */
864 if(inforeq & INFRQ_DESC) {
865 char *path;
866 _alpm_log(db->handle, ALPM_LOG_DEBUG,
867 "writing %s-%s DESC information back to db\n",
868 info->name, info->version);
869 path = _alpm_local_db_pkgpath(db, info, "desc");
870 if(!path || (fp = fopen(path, "w")) == NULL) {
871 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
872 path, strerror(errno));
873 retval = -1;
874 free(path);
875 goto cleanup;
876 }
877 free(path);
878 fprintf(fp, "%%NAME%%\n%s\n\n"
879 "%%VERSION%%\n%s\n\n", info->name, info->version);
880 if(info->desc) {
881 fprintf(fp, "%%DESC%%\n"
882 "%s\n\n", info->desc);
883 }
884 if(info->url) {
885 fprintf(fp, "%%URL%%\n"
886 "%s\n\n", info->url);
887 }
888 if(info->arch) {
889 fprintf(fp, "%%ARCH%%\n"
890 "%s\n\n", info->arch);
891 }
892 if(info->builddate) {
893 fprintf(fp, "%%BUILDDATE%%\n"
894 "%jd\n\n", (intmax_t)info->builddate);
895 }
896 if(info->installdate) {
897 fprintf(fp, "%%INSTALLDATE%%\n"
898 "%jd\n\n", (intmax_t)info->installdate);
899 }
900 if(info->packager) {
901 fprintf(fp, "%%PACKAGER%%\n"
902 "%s\n\n", info->packager);
903 }
904 if(info->isize) {
905 /* only write installed size, csize is irrelevant once installed */
906 fprintf(fp, "%%SIZE%%\n"
907 "%jd\n\n", (intmax_t)info->isize);
908 }
909 if(info->reason) {
910 fprintf(fp, "%%REASON%%\n"
911 "%u\n\n", info->reason);
912 }
913 if(info->groups) {
914 fputs("%GROUPS%\n", fp);
915 for(lp = info->groups; lp; lp = lp->next) {
916 fputs(lp->data, fp);
917 fputc('\n', fp);
918 }
919 fputc('\n', fp);
920 }
921 if(info->licenses) {
922 fputs("%LICENSE%\n", fp);
923 for(lp = info->licenses; lp; lp = lp->next) {
924 fputs(lp->data, fp);
925 fputc('\n', fp);
926 }
927 fputc('\n', fp);
928 }
929 if(info->validation) {
930 fputs("%VALIDATION%\n", fp);
931 if(info->validation & ALPM_PKG_VALIDATION_NONE) {
932 fputs("none\n", fp);
933 }
934 if(info->validation & ALPM_PKG_VALIDATION_MD5SUM) {
935 fputs("md5\n", fp);
936 }
937 if(info->validation & ALPM_PKG_VALIDATION_SHA256SUM) {
938 fputs("sha256\n", fp);
939 }
940 if(info->validation & ALPM_PKG_VALIDATION_SIGNATURE) {
941 fputs("pgp\n", fp);
942 }
943 fputc('\n', fp);
944 }
945
946 write_deps(fp, "%REPLACES%", info->replaces);
947 write_deps(fp, "%DEPENDS%", info->depends);
948 write_deps(fp, "%OPTDEPENDS%", info->optdepends);
949 write_deps(fp, "%CONFLICTS%", info->conflicts);
950 write_deps(fp, "%PROVIDES%", info->provides);
951
952 fclose(fp);
953 fp = NULL;
954 }
955
956 /* FILES */
957 if(inforeq & INFRQ_FILES) {
958 char *path;
959 _alpm_log(db->handle, ALPM_LOG_DEBUG,
960 "writing %s-%s FILES information back to db\n",
961 info->name, info->version);
962 path = _alpm_local_db_pkgpath(db, info, "files");
963 if(!path || (fp = fopen(path, "w")) == NULL) {
964 _alpm_log(db->handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
965 path, strerror(errno));
966 retval = -1;
967 free(path);
968 goto cleanup;
969 }
970 free(path);
971 if(info->files.count) {
972 size_t i;
973 fputs("%FILES%\n", fp);
974 for(i = 0; i < info->files.count; i++) {
975 const alpm_file_t *file = info->files.files + i;
976 fputs(file->name, fp);
977 fputc('\n', fp);
978 }
979 fputc('\n', fp);
980 }
981 if(info->backup) {
982 fputs("%BACKUP%\n", fp);
983 for(lp = info->backup; lp; lp = lp->next) {
984 const alpm_backup_t *backup = lp->data;
985 fprintf(fp, "%s\t%s\n", backup->name, backup->hash);
986 }
987 fputc('\n', fp);
988 }
989 fclose(fp);
990 fp = NULL;
991 }
992
993 /* INSTALL */
994 /* nothing needed here (script is automatically extracted) */
995
996 cleanup:
997 umask(oldmask);
998
999 if(fp) {
1000 fclose(fp);
1001 }
1002
1003 return retval;
1004 }
1005
1006 int _alpm_local_db_remove(alpm_db_t *db, alpm_pkg_t *info)
1007 {
1008 int ret = 0;
1009 DIR *dirp;
1010 struct dirent *dp;
1011 char *pkgpath;
1012 size_t pkgpath_len;
1013
1014 pkgpath = _alpm_local_db_pkgpath(db, info, NULL);
1015 if(!pkgpath) {
1016 return -1;
1017 }
1018 pkgpath_len = strlen(pkgpath);
1019
1020 dirp = opendir(pkgpath);
1021 if(!dirp) {
1022 free(pkgpath);
1023 return -1;
1024 }
1025 /* go through the local DB entry, removing the files within, which we know
1026 * are not nested directories of any kind. */
1027 for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1028 if(strcmp(dp->d_name, "..") != 0 && strcmp(dp->d_name, ".") != 0) {
1029 char name[PATH_MAX];
1030 if(pkgpath_len + strlen(dp->d_name) + 2 > PATH_MAX) {
1031 /* file path is too long to remove, hmm. */
1032 ret = -1;
1033 } else {
1034 sprintf(name, "%s/%s", pkgpath, dp->d_name);
1035 if(unlink(name)) {
1036 ret = -1;
1037 }
1038 }
1039 }
1040 }
1041 closedir(dirp);
1042
1043 /* after removing all enclosed files, we can remove the directory itself. */
1044 if(rmdir(pkgpath)) {
1045 ret = -1;
1046 }
1047 free(pkgpath);
1048 return ret;
1049 }
1050
1051 int SYMEXPORT alpm_pkg_set_reason(alpm_pkg_t *pkg, alpm_pkgreason_t reason)
1052 {
1053 ASSERT(pkg != NULL, return -1);
1054 ASSERT(pkg->origin == ALPM_PKG_FROM_LOCALDB,
1055 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
1056 ASSERT(pkg->origin_data.db == pkg->handle->db_local,
1057 RET_ERR(pkg->handle, ALPM_ERR_WRONG_ARGS, -1));
1058
1059 _alpm_log(pkg->handle, ALPM_LOG_DEBUG,
1060 "setting install reason %u for %s\n", reason, pkg->name);
1061 if(alpm_pkg_get_reason(pkg) == reason) {
1062 /* we are done */
1063 return 0;
1064 }
1065 /* set reason (in pkgcache) */
1066 pkg->reason = reason;
1067 /* write DESC */
1068 if(_alpm_local_db_write(pkg->handle->db_local, pkg, INFRQ_DESC)) {
1069 RET_ERR(pkg->handle, ALPM_ERR_DB_WRITE, -1);
1070 }
1071
1072 return 0;
1073 }
1074
1075 struct db_operations local_db_ops = {
1076 .validate = local_db_validate,
1077 .populate = local_db_populate,
1078 .unregister = _alpm_db_unregister,
1079 };
1080
1081 alpm_db_t *_alpm_db_register_local(alpm_handle_t *handle)
1082 {
1083 alpm_db_t *db;
1084
1085 _alpm_log(handle, ALPM_LOG_DEBUG, "registering local database\n");
1086
1087 db = _alpm_db_new("local", 1);
1088 if(db == NULL) {
1089 handle->pm_errno = ALPM_ERR_DB_CREATE;
1090 return NULL;
1091 }
1092 db->ops = &local_db_ops;
1093 db->handle = handle;
1094 db->usage = ALPM_DB_USAGE_ALL;
1095
1096 if(local_db_validate(db)) {
1097 /* pm_errno set in local_db_validate() */
1098 _alpm_db_free(db);
1099 return NULL;
1100 }
1101
1102 handle->db_local = db;
1103 return db;
1104 }
1105
1106 /* vim: set ts=2 sw=2 noet: */