4 * Copyright (c) 2006 by Christian Hamar <krics@linuxforum.hu>
5 * Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
38 #include <limits.h> /* PATH_MAX */
43 #include "alpm_list.h"
52 /* This function is used to convert the downloaded db file to the proper backend
55 int _alpm_db_install(pmdb_t
*db
, const char *dbfile
)
59 /* TODO we should not simply unpack the archive, but better parse it and
60 * db_write each entry (see sync_load_dbarchive to get archive content) */
61 _alpm_log(PM_LOG_DEBUG
, _("unpacking database '%s'"), dbfile
);
63 if(_alpm_unpack(dbfile
, db
->path
, NULL
)) {
64 RET_ERR(PM_ERR_SYSTEM
, -1);
67 return unlink(dbfile
);
70 int _alpm_db_open(pmdb_t
*db
)
75 RET_ERR(PM_ERR_DB_NULL
, -1);
78 _alpm_log(PM_LOG_DEBUG
, _("opening database from path '%s'"), db
->path
);
79 db
->handle
= opendir(db
->path
);
80 if(db
->handle
== NULL
) {
81 RET_ERR(PM_ERR_DB_OPEN
, -1);
87 void _alpm_db_close(pmdb_t
*db
)
101 void _alpm_db_rewind(pmdb_t
*db
)
105 if(db
== NULL
|| db
->handle
== NULL
) {
109 rewinddir(db
->handle
);
112 pmpkg_t
*_alpm_db_scan(pmdb_t
*db
, const char *target
)
114 struct dirent
*ent
= NULL
;
117 char name
[PKG_FULLNAME_LEN
];
125 RET_ERR(PM_ERR_DB_NULL
, NULL
);
128 /* We loop here until we read a valid package. When an iteration of this loop
129 * fails, it means alpm_db_read failed to read a valid package, so we'll read
130 * the next so as not to abort whole-db operations early
134 /* search for a specific package (by name only) */
135 rewinddir(db
->handle
);
136 while(!found
&& (ent
= readdir(db
->handle
)) != NULL
) {
137 if(!strcmp(ent
->d_name
, ".") || !strcmp(ent
->d_name
, "..")) {
140 /* stat the entry, make sure it's a directory */
141 snprintf(path
, PATH_MAX
, "%s/%s", db
->path
, ent
->d_name
);
142 if(stat(path
, &sbuf
) || !S_ISDIR(sbuf
.st_mode
)) {
145 STRNCPY(name
, ent
->d_name
, PKG_FULLNAME_LEN
);
146 /* truncate the string at the second-to-last hyphen, */
147 /* which will give us the package name */
148 if((ptr
= rindex(name
, '-'))) {
151 if((ptr
= rindex(name
, '-'))) {
154 if(!strcmp(name
, target
)) {
161 } else { /* target == NULL, full scan */
164 ent
= readdir(db
->handle
);
168 if(!strcmp(ent
->d_name
, ".") || !strcmp(ent
->d_name
, "..")) {
172 /* stat the entry, make sure it's a directory */
173 snprintf(path
, PATH_MAX
, "%s/%s", db
->path
, ent
->d_name
);
174 if(!stat(path
, &sbuf
) && S_ISDIR(sbuf
.st_mode
)) {
180 pkg
= _alpm_pkg_new(NULL
, NULL
);
182 _alpm_log(PM_LOG_DEBUG
, _("db scan could not find package: %s"), target
);
185 if(_alpm_pkg_splitname(ent
->d_name
, pkg
->name
, pkg
->version
, 0) == -1) {
186 _alpm_log(PM_LOG_ERROR
, _("invalid name for database entry '%s'"), ent
->d_name
);
190 /* explicitly read with only 'BASE' data, accessors will handle the rest */
191 if(_alpm_db_read(db
, pkg
, INFRQ_BASE
) == -1) {
192 /* TODO removed corrupt entry from the FS here */
196 pkg
->origin
= PKG_FROM_CACHE
;
203 int _alpm_db_read(pmdb_t
*db
, pmpkg_t
*info
, pmdbinfrq_t inforeq
)
207 char path
[PATH_MAX
+1];
210 alpm_list_t *tmplist;
217 RET_ERR(PM_ERR_DB_NULL
, -1);
220 if(info
== NULL
|| info
->name
[0] == 0 || info
->version
[0] == 0) {
221 _alpm_log(PM_LOG_DEBUG
, _("invalid package entry provided to _alpm_db_read, skipping"));
225 if(info
->origin
== PKG_FROM_FILE
) {
226 _alpm_log(PM_LOG_DEBUG
, _("request to read database info for a file-based package '%s', skipping..."), info
->name
);
230 /* bitmask logic here:
231 * infolevel: 00001111
234 * == to inforeq? nope, we need to load more info. */
235 if((info
->infolevel
& inforeq
) == inforeq
) {
236 /* already loaded this info, do nothing */
239 _alpm_log(PM_LOG_FUNCTION
, _("loading package data for %s : level=%d"), info
->name
, inforeq
);
241 /* clear out 'line', to be certain - and to make valgrind happy */
242 memset(line
, 0, 513);
244 snprintf(path
, PATH_MAX
, "%s/%s-%s", db
->path
, info
->name
, info
->version
);
245 if(stat(path
, &buf
)) {
246 /* directory doesn't exist or can't be opened */
247 _alpm_log(PM_LOG_DEBUG
, _("cannot find '%s-%s' in db '%s'"), info
->name
, info
->version
, db
->treename
);
252 if(inforeq
& INFRQ_DESC
) {
253 snprintf(path
, PATH_MAX
, "%s/%s-%s/desc", db
->path
, info
->name
, info
->version
);
254 if((fp
= fopen(path
, "r")) == NULL
) {
255 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s"), path
, strerror(errno
));
259 if(fgets(line
, 256, fp
) == NULL
) {
263 if(!strcmp(line
, "%FILENAME%")) {
264 /* filename is _new_ - it provides the real name of the package, on the
265 * server, to allow for us to not tie the name of the actual file to the
266 * data of the package
268 if(fgets(info
->filename
, sizeof(info
->filename
), fp
) == NULL
) {
271 _alpm_strtrim(info
->filename
);
272 } else if(!strcmp(line
, "%DESC%")) {
273 if(fgets(info
->desc
, sizeof(info
->desc
), fp
) == NULL
) {
277 while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
278 info->desc_localized = alpm_list_add(info->desc_localized, strdup(line));
282 if((locale = setlocale(LC_ALL, "")) == NULL) { //To fix segfault when locale invalid
283 setenv("LC_ALL", "C", 1);
284 locale = setlocale(LC_ALL, "");
287 if(info->desc_localized && !info->desc_localized->next) {
288 snprintf(info->desc, 512, "%s", (char*)info->desc_localized->data);
290 for (tmplist = info->desc_localized; tmplist; tmplist = tmplist->next) {
291 if (tmplist->data && strncmp(tmplist->data, locale, strlen(locale))) {
292 strncpy(info->desc, (char *)info->desc_localized->data, sizeof(info->desc));
294 char *p = (char *)tmplist->data;
295 p += strlen(locale) + 1;
296 strncpy(info->desc, p, sizeof(info->desc));
302 _alpm_strtrim(info
->desc
);
303 } else if(!strcmp(line
, "%GROUPS%")) {
304 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
305 info
->groups
= alpm_list_add(info
->groups
, strdup(line
));
307 } else if(!strcmp(line
, "%URL%")) {
308 if(fgets(info
->url
, sizeof(info
->url
), fp
) == NULL
) {
311 _alpm_strtrim(info
->url
);
312 } else if(!strcmp(line
, "%LICENSE%")) {
313 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
314 info
->licenses
= alpm_list_add(info
->licenses
, strdup(line
));
316 } else if(!strcmp(line
, "%ARCH%")) {
317 if(fgets(info
->arch
, sizeof(info
->arch
), fp
) == NULL
) {
320 _alpm_strtrim(info
->arch
);
321 } else if(!strcmp(line
, "%BUILDDATE%")) {
322 if(fgets(info
->builddate
, sizeof(info
->builddate
), fp
) == NULL
) {
325 _alpm_strtrim(info
->builddate
);
326 } else if(!strcmp(line
, "%BUILDTYPE%")) {
327 if(fgets(info
->buildtype
, sizeof(info
->buildtype
), fp
) == NULL
) {
330 _alpm_strtrim(info
->buildtype
);
331 } else if(!strcmp(line
, "%INSTALLDATE%")) {
332 if(fgets(info
->installdate
, sizeof(info
->installdate
), fp
) == NULL
) {
335 _alpm_strtrim(info
->installdate
);
336 } else if(!strcmp(line
, "%PACKAGER%")) {
337 if(fgets(info
->packager
, sizeof(info
->packager
), fp
) == NULL
) {
340 _alpm_strtrim(info
->packager
);
341 } else if(!strcmp(line
, "%REASON%")) {
343 if(fgets(tmp
, sizeof(tmp
), fp
) == NULL
) {
347 info
->reason
= atol(tmp
);
348 } else if(!strcmp(line
, "%SIZE%") || !strcmp(line
, "%CSIZE%")) {
349 /* NOTE: the CSIZE and SIZE fields both share the "size" field
350 * in the pkginfo_t struct. This can be done b/c CSIZE
351 * is currently only used in sync databases, and SIZE is
352 * only used in local databases.
355 if(fgets(tmp
, sizeof(tmp
), fp
) == NULL
) {
359 info
->size
= atol(tmp
);
360 } else if(!strcmp(line
, "%ISIZE%")) {
361 /* ISIZE (installed size) tag only appears in sync repositories,
362 * not the local one. */
364 if(fgets(tmp
, sizeof(tmp
), fp
) == NULL
) {
368 info
->isize
= atol(tmp
);
369 } else if(!strcmp(line
, "%SHA1SUM%")) {
370 /* SHA1SUM tag only appears in sync repositories,
371 * not the local one. */
372 if(fgets(info
->sha1sum
, sizeof(info
->sha1sum
), fp
) == NULL
) {
375 } else if(!strcmp(line
, "%MD5SUM%")) {
376 /* MD5SUM tag only appears in sync repositories,
377 * not the local one. */
378 if(fgets(info
->md5sum
, sizeof(info
->md5sum
), fp
) == NULL
) {
381 } else if(!strcmp(line
, "%REPLACES%")) {
382 /* the REPLACES tag is special -- it only appears in sync repositories,
383 * not the local one. */
384 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
385 info
->replaces
= alpm_list_add(info
->replaces
, strdup(line
));
387 } else if(!strcmp(line
, "%FORCE%")) {
388 /* FORCE tag only appears in sync repositories,
389 * not the local one. */
398 if(inforeq
& INFRQ_FILES
) {
399 snprintf(path
, PATH_MAX
, "%s/%s-%s/files", db
->path
, info
->name
, info
->version
);
400 if((fp
= fopen(path
, "r")) == NULL
) {
401 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s"), path
, strerror(errno
));
404 while(fgets(line
, 256, fp
)) {
406 if(!strcmp(line
, "%FILES%")) {
407 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
408 info
->files
= alpm_list_add(info
->files
, strdup(line
));
410 } else if(!strcmp(line
, "%BACKUP%")) {
411 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
412 info
->backup
= alpm_list_add(info
->backup
, strdup(line
));
421 if(inforeq
& INFRQ_DEPENDS
) {
422 snprintf(path
, PATH_MAX
, "%s/%s-%s/depends", db
->path
, info
->name
, info
->version
);
423 if((fp
= fopen(path
, "r")) == NULL
) {
424 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s"), path
, strerror(errno
));
428 fgets(line
, 255, fp
);
430 if(!strcmp(line
, "%DEPENDS%")) {
431 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
432 info
->depends
= alpm_list_add(info
->depends
, strdup(line
));
434 } else if(!strcmp(line
, "%REQUIREDBY%")) {
435 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
436 info
->requiredby
= alpm_list_add(info
->requiredby
, strdup(line
));
438 } else if(!strcmp(line
, "%CONFLICTS%")) {
439 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
440 info
->conflicts
= alpm_list_add(info
->conflicts
, strdup(line
));
442 } else if(!strcmp(line
, "%PROVIDES%")) {
443 while(fgets(line
, 512, fp
) && strlen(_alpm_strtrim(line
))) {
444 info
->provides
= alpm_list_add(info
->provides
, strdup(line
));
447 /* TODO: we were going to move these things here, but it should wait.
448 * A better change would be to figure out how to restructure the DB. */
449 /* else if(!strcmp(line, "%REPLACES%")) {
450 * the REPLACES tag is special -- it only appears in sync repositories,
451 * not the local one. *
452 while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
453 info->replaces = alpm_list_add(info->replaces, strdup(line));
455 } else if(!strcmp(line, "%FORCE%")) {
456 * FORCE tag only appears in sync repositories,
457 * not the local one. *
466 if(inforeq
& INFRQ_SCRIPTLET
) {
467 snprintf(path
, PATH_MAX
, "%s/%s-%s/install", db
->path
, info
->name
, info
->version
);
468 if(!stat(path
, &buf
)) {
474 info
->infolevel
|= inforeq
;
485 int _alpm_db_write(pmdb_t
*db
, pmpkg_t
*info
, pmdbinfrq_t inforeq
)
490 alpm_list_t
*lp
= NULL
;
496 if(db
== NULL
|| info
== NULL
) {
500 snprintf(path
, PATH_MAX
, "%s/%s-%s", db
->path
, info
->name
, info
->version
);
501 oldmask
= umask(0000);
503 /* make sure we have a sane umask */
506 if(strcmp(db
->treename
, "local") == 0) {
511 if(inforeq
& INFRQ_DESC
) {
512 _alpm_log(PM_LOG_DEBUG
, _("writing %s-%s DESC information back to db"), info
->name
, info
->version
);
513 snprintf(path
, PATH_MAX
, "%s/%s-%s/desc", db
->path
, info
->name
, info
->version
);
514 if((fp
= fopen(path
, "w")) == NULL
) {
515 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s"), path
, strerror(errno
));
519 fprintf(fp
, "%%NAME%%\n%s\n\n"
520 "%%VERSION%%\n%s\n\n", info
->name
, info
->version
);
522 /*fputs("%DESC%\n", fp);
523 for(lp = info->desc_localized; lp; lp = lp->next) {
524 fprintf(fp, "%s\n", (char *)lp->data);
528 fprintf(fp
, "%%DESC%%\n"
529 "%s\n\n", info
->desc
);
532 fputs("%GROUPS%\n", fp
);
533 for(lp
= info
->groups
; lp
; lp
= lp
->next
) {
534 fprintf(fp
, "%s\n", (char *)lp
->data
);
540 fprintf(fp
, "%%URL%%\n"
541 "%s\n\n", info
->url
);
544 fputs("%LICENSE%\n", fp
);
545 for(lp
= info
->licenses
; lp
; lp
= lp
->next
) {
546 fprintf(fp
, "%s\n", (char *)lp
->data
);
551 fprintf(fp
, "%%ARCH%%\n"
552 "%s\n\n", info
->arch
);
554 if(info
->builddate
[0]) {
555 fprintf(fp
, "%%BUILDDATE%%\n"
556 "%s\n\n", info
->builddate
);
558 if(info
->buildtype
[0]) {
559 fprintf(fp
, "%%BUILDTYPE%%\n"
560 "%s\n\n", info
->buildtype
);
562 if(info
->installdate
[0]) {
563 fprintf(fp
, "%%INSTALLDATE%%\n"
564 "%s\n\n", info
->installdate
);
566 if(info
->packager
[0]) {
567 fprintf(fp
, "%%PACKAGER%%\n"
568 "%s\n\n", info
->packager
);
571 /* only write installed size, csize is irrelevant once installed */
572 fprintf(fp
, "%%SIZE%%\n"
573 "%lu\n\n", info
->isize
);
576 fprintf(fp
, "%%REASON%%\n"
577 "%u\n\n", info
->reason
);
581 fprintf(fp
, "%%CSIZE%%\n"
582 "%lu\n\n", info
->size
);
585 fprintf(fp
, "%%ISIZE%%\n"
586 "%lu\n\n", info
->isize
);
589 fprintf(fp
, "%%SHA1SUM%%\n"
590 "%s\n\n", info
->sha1sum
);
591 } else if(info
->md5sum
) {
592 fprintf(fp
, "%%MD5SUM%%\n"
593 "%s\n\n", info
->md5sum
);
601 if(local
&& (inforeq
& INFRQ_FILES
)) {
602 _alpm_log(PM_LOG_DEBUG
, _("writing %s-%s FILES information back to db"), info
->name
, info
->version
);
603 snprintf(path
, PATH_MAX
, "%s/%s-%s/files", db
->path
, info
->name
, info
->version
);
604 if((fp
= fopen(path
, "w")) == NULL
) {
605 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s"), path
, strerror(errno
));
610 fprintf(fp
, "%%FILES%%\n");
611 for(lp
= info
->files
; lp
; lp
= lp
->next
) {
612 fprintf(fp
, "%s\n", (char *)lp
->data
);
617 fprintf(fp
, "%%BACKUP%%\n");
618 for(lp
= info
->backup
; lp
; lp
= lp
->next
) {
619 fprintf(fp
, "%s\n", (char *)lp
->data
);
628 if(inforeq
& INFRQ_DEPENDS
) {
629 _alpm_log(PM_LOG_DEBUG
, _("writing %s-%s DEPENDS information back to db"), info
->name
, info
->version
);
630 snprintf(path
, PATH_MAX
, "%s/%s-%s/depends", db
->path
, info
->name
, info
->version
);
631 if((fp
= fopen(path
, "w")) == NULL
) {
632 _alpm_log(PM_LOG_ERROR
, _("could not open file %s: %s"), path
, strerror(errno
));
637 fputs("%DEPENDS%\n", fp
);
638 for(lp
= info
->depends
; lp
; lp
= lp
->next
) {
639 fprintf(fp
, "%s\n", (char *)lp
->data
);
643 if(local
&& info
->requiredby
) {
644 fputs("%REQUIREDBY%\n", fp
);
645 for(lp
= info
->requiredby
; lp
; lp
= lp
->next
) {
646 fprintf(fp
, "%s\n", (char *)lp
->data
);
650 if(info
->conflicts
) {
651 fputs("%CONFLICTS%\n", fp
);
652 for(lp
= info
->conflicts
; lp
; lp
= lp
->next
) {
653 fprintf(fp
, "%s\n", (char *)lp
->data
);
658 fputs("%PROVIDES%\n", fp
);
659 for(lp
= info
->provides
; lp
; lp
= lp
->next
) {
660 fprintf(fp
, "%s\n", (char *)lp
->data
);
666 fputs("%REPLACES%\n", fp
);
667 for(lp
= info
->replaces
; lp
; lp
= lp
->next
) {
668 fprintf(fp
, "%s\n", (char *)lp
->data
);
673 fprintf(fp
, "%%FORCE%%\n"
682 /* nothing needed here (script is automatically extracted) */
694 int _alpm_db_remove(pmdb_t
*db
, pmpkg_t
*info
)
700 if(db
== NULL
|| info
== NULL
) {
701 RET_ERR(PM_ERR_DB_NULL
, -1);
704 snprintf(path
, PATH_MAX
, "%s%s-%s", db
->path
, info
->name
, info
->version
);
705 if(_alpm_rmrf(path
) == -1) {
712 /* reads dbpath/.lastupdate and populates *ts with the contents.
713 * *ts should be malloc'ed and should be at least 15 bytes.
715 * Returns 0 on success, 1 on error
718 int _alpm_db_getlastupdate(pmdb_t
*db
, char *ts
)
725 if(db
== NULL
|| ts
== NULL
) {
729 snprintf(file
, PATH_MAX
, "%s%s.lastupdate", handle
->root
, db
->path
);
731 /* get the last update time, if it's there */
732 if((fp
= fopen(file
, "r")) == NULL
) {
736 if(fgets(line
, sizeof(line
), fp
)) {
737 STRNCPY(ts
, line
, 15); /* YYYYMMDDHHMMSS */
748 /* writes the dbpath/.lastupdate with the contents of *ts
750 int _alpm_db_setlastupdate(pmdb_t
*db
, char *ts
)
757 if(db
== NULL
|| ts
== NULL
|| strlen(ts
) == 0) {
761 snprintf(file
, PATH_MAX
, "%s%s.lastupdate", handle
->root
, db
->path
);
763 if((fp
= fopen(file
, "w")) == NULL
) {
766 if(fputs(ts
, fp
) <= 0) {
775 /* vim: set ts=2 sw=2 noet: */