A few final changes for the 3.0.6 release
[pacman.git] / lib / libalpm / be_files.c
blobf36192777aec6fadca9f7e4fe117e13cae81d486
1 /*
2 * be_files.c
3 *
4 * Copyright (c) 2006 by Christian Hamar <krics@linuxforum.hu>
5 * Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.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.
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,
20 * USA.
23 #include "config.h"
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30 #ifdef __sun__
31 #include <strings.h>
32 #endif
33 #include <sys/stat.h>
34 #include <dirent.h>
35 #include <libintl.h>
36 #include <locale.h>
37 #ifdef CYGWIN
38 #include <limits.h> /* PATH_MAX */
39 #endif
41 /* libalpm */
42 #include "db.h"
43 #include "alpm_list.h"
44 #include "log.h"
45 #include "util.h"
46 #include "alpm.h"
47 #include "error.h"
48 #include "handle.h"
49 #include "package.h"
52 /* This function is used to convert the downloaded db file to the proper backend
53 * format
55 int _alpm_db_install(pmdb_t *db, const char *dbfile)
57 ALPM_LOG_FUNC;
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)
72 ALPM_LOG_FUNC;
74 if(db == NULL) {
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);
84 return(0);
87 void _alpm_db_close(pmdb_t *db)
89 ALPM_LOG_FUNC;
91 if(db == NULL) {
92 return;
95 if(db->handle) {
96 closedir(db->handle);
97 db->handle = NULL;
101 void _alpm_db_rewind(pmdb_t *db)
103 ALPM_LOG_FUNC;
105 if(db == NULL || db->handle == NULL) {
106 return;
109 rewinddir(db->handle);
112 pmpkg_t *_alpm_db_scan(pmdb_t *db, const char *target)
114 struct dirent *ent = NULL;
115 struct stat sbuf;
116 char path[PATH_MAX];
117 char name[PKG_FULLNAME_LEN];
118 char *ptr = NULL;
119 int found = 0;
120 pmpkg_t *pkg = NULL;
122 ALPM_LOG_FUNC;
124 if(db == NULL) {
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
132 while(!pkg) {
133 if(target != NULL) {
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, "..")) {
138 continue;
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)) {
143 continue;
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, '-'))) {
149 *ptr = '\0';
151 if((ptr = rindex(name, '-'))) {
152 *ptr = '\0';
154 if(!strcmp(name, target)) {
155 found = 1;
158 if(!found) {
159 return(NULL);
161 } else { /* target == NULL, full scan */
162 int isdir = 0;
163 while(!isdir) {
164 ent = readdir(db->handle);
165 if(ent == NULL) {
166 return(NULL);
168 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
169 isdir = 0;
170 continue;
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)) {
175 isdir = 1;
180 pkg = _alpm_pkg_new(NULL, NULL);
181 if(pkg == NULL) {
182 _alpm_log(PM_LOG_DEBUG, _("db scan could not find package: %s"), target);
183 return(NULL);
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);
187 return(NULL);
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 */
193 FREEPKG(pkg);
194 } else {
195 pkg->data = db;
196 pkg->origin = PKG_FROM_CACHE;
200 return(pkg);
203 int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
205 FILE *fp = NULL;
206 struct stat buf;
207 char path[PATH_MAX+1];
208 char line[513];
210 alpm_list_t *tmplist;
211 char *locale;
214 ALPM_LOG_FUNC;
216 if(db == NULL) {
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"));
222 return(-1);
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);
227 return(-1);
230 /* bitmask logic here:
231 * infolevel: 00001111
232 * inforeq: 00010100
233 * & result: 00000100
234 * == to inforeq? nope, we need to load more info. */
235 if((info->infolevel & inforeq) == inforeq) {
236 /* already loaded this info, do nothing */
237 return(0);
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);
248 return(-1);
251 /* DESC */
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));
256 goto error;
258 while(!feof(fp)) {
259 if(fgets(line, 256, fp) == NULL) {
260 break;
262 _alpm_strtrim(line);
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) {
269 goto error;
271 _alpm_strtrim(info->filename);
272 } else if(!strcmp(line, "%DESC%")) {
273 if(fgets(info->desc, sizeof(info->desc), fp) == NULL) {
274 goto error;
277 while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
278 info->desc_localized = alpm_list_add(info->desc_localized, strdup(line));
279 PKG_
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);
289 } else {
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));
293 } else {
294 char *p = (char *)tmplist->data;
295 p += strlen(locale) + 1;
296 strncpy(info->desc, p, sizeof(info->desc));
297 break;
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) {
309 goto error;
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) {
318 goto error;
320 _alpm_strtrim(info->arch);
321 } else if(!strcmp(line, "%BUILDDATE%")) {
322 if(fgets(info->builddate, sizeof(info->builddate), fp) == NULL) {
323 goto error;
325 _alpm_strtrim(info->builddate);
326 } else if(!strcmp(line, "%BUILDTYPE%")) {
327 if(fgets(info->buildtype, sizeof(info->buildtype), fp) == NULL) {
328 goto error;
330 _alpm_strtrim(info->buildtype);
331 } else if(!strcmp(line, "%INSTALLDATE%")) {
332 if(fgets(info->installdate, sizeof(info->installdate), fp) == NULL) {
333 goto error;
335 _alpm_strtrim(info->installdate);
336 } else if(!strcmp(line, "%PACKAGER%")) {
337 if(fgets(info->packager, sizeof(info->packager), fp) == NULL) {
338 goto error;
340 _alpm_strtrim(info->packager);
341 } else if(!strcmp(line, "%REASON%")) {
342 char tmp[32];
343 if(fgets(tmp, sizeof(tmp), fp) == NULL) {
344 goto error;
346 _alpm_strtrim(tmp);
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.
354 char tmp[32];
355 if(fgets(tmp, sizeof(tmp), fp) == NULL) {
356 goto error;
358 _alpm_strtrim(tmp);
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. */
363 char tmp[32];
364 if(fgets(tmp, sizeof(tmp), fp) == NULL) {
365 goto error;
367 _alpm_strtrim(tmp);
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) {
373 goto error;
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) {
379 goto error;
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. */
390 info->force = 1;
393 fclose(fp);
394 fp = NULL;
397 /* FILES */
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));
402 goto error;
404 while(fgets(line, 256, fp)) {
405 _alpm_strtrim(line);
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));
416 fclose(fp);
417 fp = NULL;
420 /* DEPENDS */
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));
425 goto error;
427 while(!feof(fp)) {
428 fgets(line, 255, fp);
429 _alpm_strtrim(line);
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. *
458 info->force = 1;
459 } */
461 fclose(fp);
462 fp = NULL;
465 /* INSTALL */
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)) {
469 info->scriptlet = 1;
473 /* internal */
474 info->infolevel |= inforeq;
476 return(0);
478 error:
479 if(fp) {
480 fclose(fp);
482 return(-1);
485 int _alpm_db_write(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
487 FILE *fp = NULL;
488 char path[PATH_MAX];
489 mode_t oldmask;
490 alpm_list_t *lp = NULL;
491 int retval = 0;
492 int local = 0;
494 ALPM_LOG_FUNC;
496 if(db == NULL || info == NULL) {
497 return(-1);
500 snprintf(path, PATH_MAX, "%s/%s-%s", db->path, info->name, info->version);
501 oldmask = umask(0000);
502 mkdir(path, 0755);
503 /* make sure we have a sane umask */
504 umask(0022);
506 if(strcmp(db->treename, "local") == 0) {
507 local = 1;
510 /* DESC */
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));
516 retval = -1;
517 goto cleanup;
519 fprintf(fp, "%%NAME%%\n%s\n\n"
520 "%%VERSION%%\n%s\n\n", info->name, info->version);
521 if(info->desc[0]) {
522 /*fputs("%DESC%\n", fp);
523 for(lp = info->desc_localized; lp; lp = lp->next) {
524 fprintf(fp, "%s\n", (char *)lp->data);
526 fprintf(fp, "\n");
528 fprintf(fp, "%%DESC%%\n"
529 "%s\n\n", info->desc);
531 if(info->groups) {
532 fputs("%GROUPS%\n", fp);
533 for(lp = info->groups; lp; lp = lp->next) {
534 fprintf(fp, "%s\n", (char *)lp->data);
536 fprintf(fp, "\n");
538 if(local) {
539 if(info->url[0]) {
540 fprintf(fp, "%%URL%%\n"
541 "%s\n\n", info->url);
543 if(info->licenses) {
544 fputs("%LICENSE%\n", fp);
545 for(lp = info->licenses; lp; lp = lp->next) {
546 fprintf(fp, "%s\n", (char *)lp->data);
548 fprintf(fp, "\n");
550 if(info->arch[0]) {
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);
570 if(info->size) {
571 /* only write installed size, csize is irrelevant once installed */
572 fprintf(fp, "%%SIZE%%\n"
573 "%lu\n\n", info->isize);
575 if(info->reason) {
576 fprintf(fp, "%%REASON%%\n"
577 "%u\n\n", info->reason);
579 } else {
580 if(info->size) {
581 fprintf(fp, "%%CSIZE%%\n"
582 "%lu\n\n", info->size);
584 if(info->isize) {
585 fprintf(fp, "%%ISIZE%%\n"
586 "%lu\n\n", info->isize);
588 if(info->sha1sum) {
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);
596 fclose(fp);
597 fp = NULL;
600 /* FILES */
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));
606 retval = -1;
607 goto cleanup;
609 if(info->files) {
610 fprintf(fp, "%%FILES%%\n");
611 for(lp = info->files; lp; lp = lp->next) {
612 fprintf(fp, "%s\n", (char *)lp->data);
614 fprintf(fp, "\n");
616 if(info->backup) {
617 fprintf(fp, "%%BACKUP%%\n");
618 for(lp = info->backup; lp; lp = lp->next) {
619 fprintf(fp, "%s\n", (char *)lp->data);
621 fprintf(fp, "\n");
623 fclose(fp);
624 fp = NULL;
627 /* DEPENDS */
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));
633 retval = -1;
634 goto cleanup;
636 if(info->depends) {
637 fputs("%DEPENDS%\n", fp);
638 for(lp = info->depends; lp; lp = lp->next) {
639 fprintf(fp, "%s\n", (char *)lp->data);
641 fprintf(fp, "\n");
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);
648 fprintf(fp, "\n");
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);
655 fprintf(fp, "\n");
657 if(info->provides) {
658 fputs("%PROVIDES%\n", fp);
659 for(lp = info->provides; lp; lp = lp->next) {
660 fprintf(fp, "%s\n", (char *)lp->data);
662 fprintf(fp, "\n");
664 if(!local) {
665 if(info->replaces) {
666 fputs("%REPLACES%\n", fp);
667 for(lp = info->replaces; lp; lp = lp->next) {
668 fprintf(fp, "%s\n", (char *)lp->data);
670 fprintf(fp, "\n");
672 if(info->force) {
673 fprintf(fp, "%%FORCE%%\n"
674 "\n");
677 fclose(fp);
678 fp = NULL;
681 /* INSTALL */
682 /* nothing needed here (script is automatically extracted) */
684 cleanup:
685 umask(oldmask);
687 if(fp) {
688 fclose(fp);
691 return(retval);
694 int _alpm_db_remove(pmdb_t *db, pmpkg_t *info)
696 char path[PATH_MAX];
698 ALPM_LOG_FUNC;
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) {
706 return(-1);
709 return(0);
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)
720 FILE *fp;
721 char file[PATH_MAX];
723 ALPM_LOG_FUNC;
725 if(db == NULL || ts == NULL) {
726 return(-1);
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) {
733 return(-1);
734 } else {
735 char line[256];
736 if(fgets(line, sizeof(line), fp)) {
737 STRNCPY(ts, line, 15); /* YYYYMMDDHHMMSS */
738 ts[14] = '\0';
739 } else {
740 fclose(fp);
741 return(-1);
744 fclose(fp);
745 return(0);
748 /* writes the dbpath/.lastupdate with the contents of *ts
750 int _alpm_db_setlastupdate(pmdb_t *db, char *ts)
752 FILE *fp;
753 char file[PATH_MAX];
755 ALPM_LOG_FUNC;
757 if(db == NULL || ts == NULL || strlen(ts) == 0) {
758 return(-1);
761 snprintf(file, PATH_MAX, "%s%s.lastupdate", handle->root, db->path);
763 if((fp = fopen(file, "w")) == NULL) {
764 return(-1);
766 if(fputs(ts, fp) <= 0) {
767 fclose(fp);
768 return(-1);
770 fclose(fp);
772 return(0);
775 /* vim: set ts=2 sw=2 noet: */