[pacman-dev] be_files.c work
K. Piche
kpiche at rogers.com
Sun Apr 13 23:01:39 EDT 2008
Added functions to read standard types from db files. This is the first
patch in a larger work to follow eventually.
>From 84a7a5db2dc58a1ae800075d3acd604d18c86747 Mon Sep 17 00:00:00 2001
From: K. Piche <kevin at archlinux.org>
Date: Sun, 13 Apr 2008 22:54:37 -0400
Subject: [PATCH] Add helper functions to read standard types from
package database files.
Signed-off-by: K. Piche <kevin at archlinux.org>
---
lib/libalpm/be_files.c | 305
+++++++++++++++++++++++++++++++-----------------
1 files changed, 197 insertions(+), 108 deletions(-)
diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index 7cc01c2..7d78296 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -244,6 +244,134 @@ pmpkg_t *_alpm_db_scan(pmdb_t *db, const char
*target)
return(pkg);
}
+
+
+/********** db file type parsing **********/
+
+
+
+/**
+ * @brief Read a string from a package's db file.
+ * @param fp File to read from.
+ * @param pstr pointer to char pointer to store the string.
+ * @return 0 for success or -1 for failure.
+ */
+int _alpm_db_read_string(FILE *fp, char **pstr)
+{
+ char line[512];
+
+ ALPM_LOG_FUNC;
+
+ if(fgets(line, 512, fp) == NULL) {
+ return(-1);
+ }
+
+ STRDUP(*pstr, _alpm_strtrim(line), return(-1));
+
+ return(0);
+}
+
+
+/**
+ * @brief Read a long from a package's db file.
+ * @param fp File to read from.
+ * @param plong pointer to long to store the input.
+ * @return 0 for success or -1 for failure.
+ */
+int _alpm_db_read_long(FILE *fp, unsigned long *plong)
+{
+ char line[512];
+
+ ALPM_LOG_FUNC;
+
+ if(fgets(line, 512, fp) == NULL) {
+ return(-1);
+ }
+
+ *plong = atol(_alpm_strtrim(line));
+
+ return(0);
+}
+
+
+/**
+ * @brief Read a list of strings from a package's db file.
+ * @param fp File to read from.
+ * @param plist pointer to alpm_list_t pointer to store the list.
+ * @return 0 for success or -1 for failure.
+ */
+int _alpm_db_read_list(FILE *fp, alpm_list_t **plist)
+{
+ char line[512];
+
+ ALPM_LOG_FUNC;
+
+ /* FIXME no error return */
+ while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ char *linedup;
+ STRDUP(linedup, _alpm_strtrim(line), return(-1));
+ *plist = alpm_list_add(*plist, linedup);
+ }
+
+ return(0);
+}
+
+
+/**
+ * @brief Read a pmdepend_t list from a package's db file.
+ * @param fp File to read from.
+ * @param pdlist pointer to alpm_list_t pointer to store the
dependencies.
+ * @return 0 for success or -1 for failure.
+ */
+int _alpm_db_read_deplist(FILE *fp, alpm_list_t **pdlist)
+{
+ char line[512];
+
+ ALPM_LOG_FUNC;
+
+ /* FIXME no error return */
+ while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
+ pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
+ *pdlist = alpm_list_add(*pdlist, dep);
+ }
+
+ return(0);
+}
+
+
+/**
+ * @brief Read a date from a package's db file.
+ * @param fp File to read from.
+ * @param pdate pointer to a long to store the date.
+ * @return 0 for success or -1 for failure.
+ */
+int _alpm_db_read_date(FILE *fp, long *pdate)
+{
+ char line[512];
+
+ ALPM_LOG_FUNC;
+
+ if(fgets(line, 512, fp) == NULL) {
+ return(-1);
+ }
+ _alpm_strtrim(line);
+
+ char first = tolower(line[0]);
+ if(first > 'a' && first < 'z') {
+ struct tm tmp_tm = {0}; /* Initialize to null incase of failure. */
+ setlocale(LC_TIME, "C");
+ strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
+ *pdate = mktime(&tmp_tm);
+ setlocale(LC_TIME, "");
+ } else {
+ *pdate = atol(line);
+ }
+
+ return(0);
+}
+
+
+
int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
{
FILE *fp = NULL;
@@ -297,123 +425,89 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
pmdbinfrq_t inforeq)
_alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path,
strerror(errno));
goto error;
}
+
while(!feof(fp)) {
if(fgets(line, 256, fp) == NULL) {
break;
}
_alpm_strtrim(line);
if(strcmp(line, "%FILENAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->filename, _alpm_strtrim(line), goto error);
+
+ if (_alpm_db_read_string(fp, &info->filename)) goto error;
+
} else if(strcmp(line, "%DESC%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->desc, _alpm_strtrim(line), goto error);
+
+ if (_alpm_db_read_string(fp, &info->desc)) goto error;
+
} else if(strcmp(line, "%GROUPS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->groups = alpm_list_add(info->groups, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->groups)) goto error;
+
} else if(strcmp(line, "%URL%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->url, _alpm_strtrim(line), goto error);
+
+ if (_alpm_db_read_string(fp, &info->url)) goto error;
+
} else if(strcmp(line, "%LICENSE%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->licenses = alpm_list_add(info->licenses, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->licenses)) goto error;
+
} else if(strcmp(line, "%ARCH%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->arch, _alpm_strtrim(line), goto error);
+
+ if (_alpm_db_read_string(fp, &info->arch)) goto error;
+
} else if(strcmp(line, "%BUILDDATE%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- _alpm_strtrim(line);
- char first = tolower(line[0]);
- if(first > 'a' && first < 'z') {
- struct tm tmp_tm = {0}; //initialize to null incase of failure
- setlocale(LC_TIME, "C");
- strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
- info->builddate = mktime(&tmp_tm);
- setlocale(LC_TIME, "");
- } else {
- info->builddate = atol(line);
- }
+ if (_alpm_db_read_date(fp, &info->builddate)) goto error;
+
} else if(strcmp(line, "%INSTALLDATE%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- _alpm_strtrim(line);
- char first = tolower(line[0]);
- if(first > 'a' && first < 'z') {
- struct tm tmp_tm = {0}; //initialize to null incase of failure
- setlocale(LC_TIME, "C");
- strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
- info->installdate = mktime(&tmp_tm);
- setlocale(LC_TIME, "");
- } else {
- info->installdate = atol(line);
- }
+ if (_alpm_db_read_date(fp, &info->installdate)) goto error;
+
} else if(strcmp(line, "%PACKAGER%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->packager, _alpm_strtrim(line), goto error);
+
+ if (_alpm_db_read_string(fp, &info->packager)) goto error;
+
} else if(strcmp(line, "%REASON%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- info->reason = atol(_alpm_strtrim(line));
+
+ unsigned long tempreason;
+ if (_alpm_db_read_long(fp, &tempreason)) goto error;
+ info->reason = tempreason;
+
} else if(strcmp(line, "%SIZE%") == 0 || strcmp(line, "%CSIZE%") ==
0) {
+
/* NOTE: the CSIZE and SIZE fields both share the "size" field
* in the pkginfo_t struct. This can be done b/c CSIZE
* is currently only used in sync databases, and SIZE is
* only used in local databases.
*/
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- info->size = atol(_alpm_strtrim(line));
+ if (_alpm_db_read_long(fp, &info->size)) goto error;
+
/* also store this value to isize if isize is unset */
if(info->isize == 0) {
info->isize = info->size;
}
+
} else if(strcmp(line, "%ISIZE%") == 0) {
+
/* ISIZE (installed size) tag only appears in sync repositories,
* not the local one. */
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- info->isize = atol(_alpm_strtrim(line));
+ if (_alpm_db_read_long(fp, &info->isize)) goto error;
+
} else if(strcmp(line, "%MD5SUM%") == 0) {
+
/* MD5SUM tag only appears in sync repositories,
* not the local one. */
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
+ if (_alpm_db_read_string(fp, &info->md5sum)) goto error;
+
} else if(strcmp(line, "%REPLACES%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->replaces = alpm_list_add(info->replaces, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->replaces)) goto error;
+
} else if(strcmp(line, "%FORCE%") == 0) {
info->force = 1;
}
}
+
fclose(fp);
fp = NULL;
}
@@ -425,22 +519,20 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
pmdbinfrq_t inforeq)
_alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path,
strerror(errno));
goto error;
}
+
while(fgets(line, 256, fp)) {
_alpm_strtrim(line);
if(strcmp(line, "%FILES%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->files = alpm_list_add(info->files, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->files)) goto error;
+
} else if(strcmp(line, "%BACKUP%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->backup = alpm_list_add(info->backup, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->backup)) goto error;
+
}
}
+
fclose(fp);
fp = NULL;
}
@@ -452,34 +544,29 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
pmdbinfrq_t inforeq)
_alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path,
strerror(errno));
goto error;
}
+
while(!feof(fp)) {
fgets(line, 255, fp);
_alpm_strtrim(line);
if(strcmp(line, "%DEPENDS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
- info->depends = alpm_list_add(info->depends, dep);
- }
+
+ if (_alpm_db_read_deplist(fp, &info->depends)) goto error;
+
} else if(strcmp(line, "%OPTDEPENDS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->optdepends = alpm_list_add(info->optdepends, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->optdepends)) goto error;
+
} else if(strcmp(line, "%CONFLICTS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->conflicts = alpm_list_add(info->conflicts, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->conflicts)) goto error;
+
} else if(strcmp(line, "%PROVIDES%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- char *linedup;
- STRDUP(linedup, _alpm_strtrim(line), goto error);
- info->provides = alpm_list_add(info->provides, linedup);
- }
+
+ if (_alpm_db_read_list(fp, &info->provides)) goto error;
+
}
}
+
fclose(fp);
fp = NULL;
}
@@ -489,6 +576,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
pmdbinfrq_t inforeq)
snprintf(path, PATH_MAX, "%s/%s-%s/deltas", db->path,
info->name, info->version);
if((fp = fopen(path, "r"))) {
+
while(!feof(fp)) {
fgets(line, 255, fp);
_alpm_strtrim(line);
@@ -498,6 +586,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info,
pmdbinfrq_t inforeq)
}
}
}
+
fclose(fp);
fp = NULL;
}
--
1.5.5
--
K. Piche <kpiche at rogers.com>
More information about the pacman-dev
mailing list