Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=b4e33c8f107e5ce102fd288ee3c570008a828500

commit b4e33c8f107e5ce102fd288ee3c570008a828500
Author: Michel Hermier <herm...@frugalware.org>
Date:   Sat Nov 9 16:34:39 2013 +0100

libpacman: Split read functions of _pacman_db_read.

diff --git a/lib/libpacman/be_files.c b/lib/libpacman/be_files.c
index 7722e03..4452a56 100644
--- a/lib/libpacman/be_files.c
+++ b/lib/libpacman/be_files.c
@@ -185,110 +185,20 @@ pmpkg_t *_pacman_db_scan(pmdb_t *db, const char *target, 
unsigned int inforeq)
return(pkg);
}

-static int _pacman_localdb_file_reader(pmdb_t *db, pmpkg_t *info, unsigned int 
inforeq, unsigned int inforeq_masq, const char *file, int (*reader)(pmpkg_t *, 
unsigned int, FILE *))
-{
-       int ret = 0;
-
-       if(inforeq & inforeq_masq) {
-               FILE *fp = NULL;
-               char path[PATH_MAX];
-
-               snprintf(path, PATH_MAX, "%s/%s-%s/%s", db->path, info->name, 
info->version, file);
-               fp = fopen(path, "r");
-               if(fp == NULL) {
-                       _pacman_log(PM_LOG_WARNING, "%s (%s)", path, 
strerror(errno));
-                       return -1;
-               }
-               if(reader(info, inforeq, fp) == -1)
-                       return -1;
-               fclose(fp);
-       }
-       return ret;
-}
-
-static int _pacman_syncdb_file_reader(pmdb_t *db, pmpkg_t *info, unsigned int 
inforeq, unsigned int inforeq_masq, int (*reader)(pmpkg_t *, unsigned int, FILE 
*))
-{
-       int ret = 0;
-
-       if(inforeq & inforeq_masq) {
-               FILE *fp = _pacman_archive_read_fropen(db->handle);
-
-               ASSERT(fp != NULL, RET_ERR(PM_ERR_MEMORY, -1));
-               ret = reader(info, inforeq, fp);
-               fclose(fp);
-       }
-       return ret;
-}
-
-static int suffixcmp(const char *str, const char *suffix)
-{
-       int len = strlen(str), suflen = strlen(suffix);
-       if (len < suflen)
-               return -1;
-       else
-               return strcmp(str + len - suflen, suffix);
-}
-
int _pacman_db_read(pmdb_t *db, unsigned int inforeq, pmpkg_t *info)
{
-       ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+       int ret;

+       ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
if(info == NULL || info->name[0] == 0 || info->version[0] == 0) {
_pacman_log(PM_LOG_ERROR, _("invalid package entry provided to 
_pacman_db_read"));
return(-1);
}

-       if (islocal(db)) {
-               struct stat buf;
-               char path[PATH_MAX];
-
-               snprintf(path, PATH_MAX, "%s/%s-%s", db->path, info->name, 
info->version);
-               if(stat(path, &buf)) {
-                       /* directory doesn't exist or can't be opened */
-                       return(-1);
-               }
-
-               if (_pacman_localdb_file_reader(db, info, inforeq, INFRQ_DESC, 
"desc", _pacman_localdb_desc_fread) == -1)
-                       return -1;
-
-               if (_pacman_localdb_file_reader(db, info, inforeq, 
INFRQ_DEPENDS, "depends", _pacman_localdb_depends_fread) == -1)
-                       return -1;
-
-               /* FILES */
-               if (_pacman_localdb_file_reader(db, info, inforeq, INFRQ_FILES, 
"files", _pacman_localdb_files_fread) == -1)
-                       return -1;
-
-               /* INSTALL */
-               if(inforeq & INFRQ_SCRIPLET) {
-                       snprintf(path, PATH_MAX, "%s/%s-%s/install", db->path, 
info->name, info->version);
-                       if(!stat(path, &buf)) {
-                               info->scriptlet = 1;
-                       }
-               }
-       } else {
-               int descdone = 0, depsdone = 0;
-               while (!descdone || !depsdone) {
-                       struct archive_entry *entry = NULL;
-                       if (archive_read_next_header(db->handle, &entry) != 
ARCHIVE_OK)
-                               return -1;
-                       const char *pathname = archive_entry_pathname(entry);
-                       if (!suffixcmp(pathname, "/desc")) {
-                               if(_pacman_syncdb_file_reader(db, info, 
inforeq, INFRQ_DESC, _pacman_localdb_desc_fread) == -1)
-                                       return -1;
-                               descdone = 1;
-                       }
-                       if (!suffixcmp(pathname, "/depends")) {
-                               if(_pacman_syncdb_file_reader(db, info, 
inforeq, INFRQ_DEPENDS, _pacman_localdb_depends_fread) == -1)
-                                       return -1;
-                               depsdone = 1;
-                       }
-               }
+       if((ret = db->ops->read(db, info, inforeq)) == 0) {
+               info->infolevel |= inforeq;
}
-
-       /* internal */
-       info->infolevel |= inforeq;
-
-       return(0);
+       return ret;
}

/* reads dbpath/.lastupdate and populates *ts with the contents.
diff --git a/lib/libpacman/db.h b/lib/libpacman/db.h
index 91ac568..ba78e8b 100644
--- a/lib/libpacman/db.h
+++ b/lib/libpacman/db.h
@@ -47,6 +47,7 @@ struct __pmdb_ops_t {
/* Package iterator */
int (*rewind)(pmdb_t *db);

+       int (*read)(pmdb_t *db, pmpkg_t *info, unsigned int inforeq);
int (*write)(pmdb_t *db, pmpkg_t *info, unsigned int inforeq); /* Optional */
int (*remove)(pmdb_t *db, pmpkg_t *info); /* Optional */
};
diff --git a/lib/libpacman/db/localdb.c b/lib/libpacman/db/localdb.c
index 70a36c5..ac0078b 100644
--- a/lib/libpacman/db/localdb.c
+++ b/lib/libpacman/db/localdb.c
@@ -39,7 +39,9 @@
#endif

/* pacman-g2 */
+#include "db/localdb.h"

+#include "db/localdb_files.h"
#include "util/log.h"
#include "util/stringlist.h"
#include "util.h"
@@ -117,6 +119,63 @@ int _pacman_localdb_rewind(pmdb_t *db)
}

static
+int _pacman_localdb_file_reader(pmdb_t *db, pmpkg_t *info, unsigned int 
inforeq, unsigned int inforeq_masq, const char *file, int (*reader)(pmpkg_t *, 
unsigned int, FILE *))
+{
+       int ret = 0;
+
+       if(inforeq & inforeq_masq) {
+               FILE *fp = NULL;
+               char path[PATH_MAX];
+
+               snprintf(path, PATH_MAX, "%s/%s-%s/%s", db->path, info->name, 
info->version, file);
+               fp = fopen(path, "r");
+               if(fp == NULL) {
+                       _pacman_log(PM_LOG_WARNING, "%s (%s)", path, 
strerror(errno));
+                       return -1;
+               }
+               if(reader(info, inforeq, fp) == -1)
+                       return -1;
+               fclose(fp);
+       }
+       return ret;
+}
+
+static
+int _pacman_localdb_read(pmdb_t *db, pmpkg_t *info, unsigned int inforeq)
+{
+       struct stat buf;
+       char path[PATH_MAX];
+
+       ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+       if(info == NULL || info->name[0] == 0 || info->version[0] == 0) {
+               _pacman_log(PM_LOG_ERROR, _("invalid package entry provided to 
_pacman_localdb_read"));
+               return(-1);
+       }
+
+       snprintf(path, PATH_MAX, "%s/%s-%s", db->path, info->name, 
info->version);
+       if(stat(path, &buf)) {
+               /* directory doesn't exist or can't be opened */
+               return(-1);
+       }
+
+       if (_pacman_localdb_file_reader(db, info, inforeq, INFRQ_DESC, "desc", 
_pacman_localdb_desc_fread) == -1)
+               return -1;
+       if (_pacman_localdb_file_reader(db, info, inforeq, INFRQ_DEPENDS, 
"depends", _pacman_localdb_depends_fread) == -1)
+               return -1;
+       if (_pacman_localdb_file_reader(db, info, inforeq, INFRQ_FILES, 
"files", _pacman_localdb_files_fread) == -1)
+               return -1;
+
+       /* INSTALL */
+       if(inforeq & INFRQ_SCRIPLET) {
+               snprintf(path, PATH_MAX, "%s/%s-%s/install", db->path, 
info->name, info->version);
+               if(!stat(path, &buf)) {
+                       info->scriptlet = 1;
+               }
+       }
+       return 0;
+}
+
+static
void _pacman_localdb_write_bool(const char *entry, int value, FILE *stream)
{
if(value != 0) {
@@ -259,6 +318,7 @@ const pmdb_ops_t _pacman_localdb_ops = {
.open = _pacman_localdb_open,
.close = _pacman_localdb_close,
.rewind = _pacman_localdb_rewind,
+       .read = _pacman_localdb_read,
.write = _pacman_localdb_write,
.remove = _pacman_localdb_remove,
};
diff --git a/lib/libpacman/db/syncdb.c b/lib/libpacman/db/syncdb.c
index bdeff6d..34f4cdb 100644
--- a/lib/libpacman/db/syncdb.c
+++ b/lib/libpacman/db/syncdb.c
@@ -21,6 +21,7 @@
*/

#include "config.h"
+
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
@@ -38,8 +39,10 @@
#endif

/* pacman-g2 */
+#include "db/syncdb.h"

-#include "db/localdb.h"
+#include "db/localdb_files.h"
+#include "io/archive.h"
#include "util/log.h"
#include "util/stringlist.h"
#include "util.h"
@@ -50,6 +53,16 @@
#include "handle.h"

static
+int suffixcmp(const char *str, const char *suffix)
+{
+       int len = strlen(str), suflen = strlen(suffix);
+       if (len < suflen)
+               return -1;
+       else
+               return strcmp(str + len - suflen, suffix);
+}
+
+static
pmlist_t *_pacman_syncdb_test(pmdb_t *db)
{
/* testing sync dbs is not supported */
@@ -107,12 +120,59 @@ int _pacman_syncdb_rewind(pmdb_t *db)
return 0;
}

+static
+int _pacman_syncdb_file_reader(pmdb_t *db, pmpkg_t *info, unsigned int 
inforeq, unsigned int inforeq_masq, int (*reader)(pmpkg_t *, unsigned int, FILE 
*))
+{
+       int ret = 0;
+
+       if(inforeq & inforeq_masq) {
+               FILE *fp = _pacman_archive_read_fropen(db->handle);
+
+               ASSERT(fp != NULL, RET_ERR(PM_ERR_MEMORY, -1));
+               ret = reader(info, inforeq, fp);
+               fclose(fp);
+       }
+       return ret;
+}
+
+static
+int _pacman_syncdb_read(pmdb_t *db, pmpkg_t *info, unsigned int inforeq)
+{
+       int descdone = 0, depsdone = 0;
+
+       ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
+       if(info == NULL || info->name[0] == 0 || info->version[0] == 0) {
+               _pacman_log(PM_LOG_ERROR, _("invalid package entry provided to 
_pacman_syncdb_read"));
+               return(-1);
+       }
+
+       while (!descdone || !depsdone) {
+               struct archive_entry *entry = NULL;
+               if (archive_read_next_header(db->handle, &entry) != ARCHIVE_OK)
+                       return -1;
+               const char *pathname = archive_entry_pathname(entry);
+               if (!suffixcmp(pathname, "/desc")) {
+                       if(_pacman_syncdb_file_reader(db, info, inforeq, 
INFRQ_DESC, _pacman_localdb_desc_fread) == -1)
+                               return -1;
+                       descdone = 1;
+               }
+               if (!suffixcmp(pathname, "/depends")) {
+                       if(_pacman_syncdb_file_reader(db, info, inforeq, 
INFRQ_DEPENDS, _pacman_localdb_depends_fread) == -1)
+                               return -1;
+                       depsdone = 1;
+               }
+       }
+       return 0;
+}
+
const pmdb_ops_t _pacman_syncdb_ops = {
.test = _pacman_syncdb_test,
.open = _pacman_syncdb_open,
.close = _pacman_syncdb_close,
.rewind = _pacman_syncdb_rewind,
+       .read = _pacman_syncdb_read,
.write = NULL,
+       .remove = NULL,
};

/* vim: set ts=2 sw=2 noet: */
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to