Pacman-dev
Threads by month
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
September 2009
- 37 participants
- 93 discussions
After our recent screwup with size_t and ssize_t in the download code, I
found the `-Wsign-conversion` flag to GCC to see if we were doing anything
else boneheaded. I didn't find anything quite as bad, but we did have some
goofups- most of our public unsigned methods would return -1 on error, which
is a bit odd in an unsigned context.
Signed-off-by: Dan McGee <dan(a)archlinux.org>
---
lib/libalpm/add.c | 4 ++--
lib/libalpm/alpm.h | 15 ++++++++-------
lib/libalpm/alpm_list.c | 2 +-
lib/libalpm/alpm_list.h | 2 +-
lib/libalpm/backup.c | 2 +-
lib/libalpm/be_package.c | 5 ++---
lib/libalpm/conflict.c | 2 +-
lib/libalpm/dload.c | 10 +++++-----
lib/libalpm/handle.c | 15 ++++++++++++---
lib/libalpm/handle.h | 4 ++--
lib/libalpm/package.c | 8 +++++---
lib/libalpm/remove.c | 11 ++++++-----
lib/libalpm/sync.c | 4 ++--
lib/libalpm/trans.c | 2 +-
lib/libalpm/util.c | 13 +++++--------
src/pacman/callback.c | 31 ++++++++++++++++++++++---------
src/pacman/conf.h | 2 +-
src/pacman/pacman.c | 4 ++--
src/pacman/util.c | 34 ++++++++++++++++++++--------------
19 files changed, 99 insertions(+), 71 deletions(-)
diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index c6751a4..4120b14 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -199,7 +199,7 @@ static int extract_single_file(struct archive *archive,
/* case 12: existing dir, ignore it */
if(lsbuf.st_mode != entrymode) {
/* if filesystem perms are different than pkg perms, warn user */
- int mask = 07777;
+ mode_t mask = 07777;
_alpm_log(PM_LOG_WARNING, _("directory permissions differ on %s\n"
"filesystem: %o package: %o\n"), entryname, lsbuf.st_mode & mask,
entrymode & mask);
@@ -715,7 +715,7 @@ int _alpm_upgrade_packages(pmtrans_t *trans, pmdb_t *db)
return(0);
}
- pkg_count = alpm_list_count(trans->add);
+ pkg_count = (int)alpm_list_count(trans->add);
pkg_current = 1;
/* loop through our package list adding/upgrading one at a time */
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index e9fadca..ae2dc1f 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -128,8 +128,8 @@ int alpm_option_set_logfile(const char *logfile);
const char *alpm_option_get_lockfile();
/* no set_lockfile, path is determined from dbpath */
-unsigned short alpm_option_get_usesyslog();
-void alpm_option_set_usesyslog(unsigned short usesyslog);
+int alpm_option_get_usesyslog();
+void alpm_option_set_usesyslog(int usesyslog);
alpm_list_t *alpm_option_get_noupgrades();
void alpm_option_add_noupgrade(const char *pkg);
@@ -154,7 +154,8 @@ int alpm_option_remove_ignoregrp(const char *grp);
const char *alpm_option_get_arch();
void alpm_option_set_arch(const char *arch);
-void alpm_option_set_usedelta(unsigned short usedelta);
+int alpm_option_get_usedelta();
+void alpm_option_set_usedelta(int usedelta);
pmdb_t *alpm_option_get_localdb();
alpm_list_t *alpm_option_get_syncdbs();
@@ -195,7 +196,7 @@ typedef enum _pmpkgreason_t {
PM_PKG_REASON_DEPEND = 1 /* installed as a dependency for another package */
} pmpkgreason_t;
-int alpm_pkg_load(const char *filename, unsigned short full, pmpkg_t **pkg);
+int alpm_pkg_load(const char *filename, int full, pmpkg_t **pkg);
int alpm_pkg_free(pmpkg_t *pkg);
int alpm_pkg_checkmd5sum(pmpkg_t *pkg);
char *alpm_fetch_pkgurl(const char *url);
@@ -231,8 +232,8 @@ size_t alpm_pkg_changelog_read(void *ptr, size_t size,
const pmpkg_t *pkg, const void *fp);
/*int alpm_pkg_changelog_feof(const pmpkg_t *pkg, void *fp);*/
int alpm_pkg_changelog_close(const pmpkg_t *pkg, void *fp);
-unsigned short alpm_pkg_has_scriptlet(pmpkg_t *pkg);
-unsigned short alpm_pkg_has_force(pmpkg_t *pkg);
+int alpm_pkg_has_scriptlet(pmpkg_t *pkg);
+int alpm_pkg_has_force(pmpkg_t *pkg);
off_t alpm_pkg_download_size(pmpkg_t *newpkg);
@@ -395,7 +396,7 @@ typedef void (*alpm_trans_cb_conv)(pmtransconv_t, void *, void *,
/* Transaction Progress callback */
typedef void (*alpm_trans_cb_progress)(pmtransprog_t, const char *, int, int, int);
-unsigned int alpm_trans_get_flags();
+int alpm_trans_get_flags();
alpm_list_t * alpm_trans_get_add();
alpm_list_t * alpm_trans_get_remove();
int alpm_trans_init(pmtransflag_t flags,
diff --git a/lib/libalpm/alpm_list.c b/lib/libalpm/alpm_list.c
index 8b2c7b3..392faa7 100644
--- a/lib/libalpm/alpm_list.c
+++ b/lib/libalpm/alpm_list.c
@@ -573,7 +573,7 @@ void SYMEXPORT *alpm_list_getdata(const alpm_list_t *node)
*
* @return the number of list items
*/
-int SYMEXPORT alpm_list_count(const alpm_list_t *list)
+unsigned int SYMEXPORT alpm_list_count(const alpm_list_t *list)
{
unsigned int i = 0;
const alpm_list_t *lp = list;
diff --git a/lib/libalpm/alpm_list.h b/lib/libalpm/alpm_list.h
index f079ecf..66cc7d9 100644
--- a/lib/libalpm/alpm_list.h
+++ b/lib/libalpm/alpm_list.h
@@ -73,7 +73,7 @@ alpm_list_t *alpm_list_last(const alpm_list_t *list);
void *alpm_list_getdata(const alpm_list_t *entry);
/* misc */
-int alpm_list_count(const alpm_list_t *list);
+unsigned int alpm_list_count(const alpm_list_t *list);
void *alpm_list_find(const alpm_list_t *haystack, const void *needle, alpm_list_fn_cmp fn);
void *alpm_list_find_ptr(const alpm_list_t *haystack, const void *needle);
char *alpm_list_find_str(const alpm_list_t *haystack, const char *needle);
diff --git a/lib/libalpm/backup.c b/lib/libalpm/backup.c
index e628131..ab02d90 100644
--- a/lib/libalpm/backup.c
+++ b/lib/libalpm/backup.c
@@ -33,7 +33,7 @@
#include "util.h"
/* split a backup string "file\thash" into two strings : file and hash */
-int _alpm_backup_split(const char *string, char **file, char **hash)
+static int _alpm_backup_split(const char *string, char **file, char **hash)
{
char *str = strdup(string);
char *ptr;
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 9c7c161..745e17f 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -239,7 +239,7 @@ static pmpkg_t *pkg_load(const char *pkgfile, unsigned short full)
if(full) {
/* "checking for conflicts" requires a sorted list, ensure that here */
_alpm_log(PM_LOG_DEBUG, "sorting package filelist for %s\n", pkgfile);
- newpkg->files = alpm_list_msort(newpkg->files, alpm_list_count(newpkg->files),
+ newpkg->files = alpm_list_msort(newpkg->files, (int)alpm_list_count(newpkg->files),
_alpm_str_cmp);
newpkg->infolevel = INFRQ_ALL;
} else {
@@ -269,8 +269,7 @@ error:
* @param pkg address of the package pointer
* @return 0 on success, -1 on error (pm_errno is set accordingly)
*/
-int SYMEXPORT alpm_pkg_load(const char *filename, unsigned short full,
- pmpkg_t **pkg)
+int SYMEXPORT alpm_pkg_load(const char *filename, int full, pmpkg_t **pkg)
{
ALPM_LOG_FUNC;
diff --git a/lib/libalpm/conflict.c b/lib/libalpm/conflict.c
index e934c01..418409e 100644
--- a/lib/libalpm/conflict.c
+++ b/lib/libalpm/conflict.c
@@ -403,7 +403,7 @@ alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans,
alpm_list_t *upgrade, alpm_list_t *remove)
{
alpm_list_t *i, *j, *conflicts = NULL;
- int numtargs = alpm_list_count(upgrade);
+ int numtargs = (int)alpm_list_count(upgrade);
int current;
ALPM_LOG_FUNC;
diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index 8b3226b..7538e59 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -59,7 +59,7 @@ static char *get_filename(const char *url) {
static char *get_destfile(const char *path, const char *filename) {
char *destfile;
/* len = localpath len + filename len + null */
- int len = strlen(path) + strlen(filename) + 1;
+ size_t len = strlen(path) + strlen(filename) + 1;
CALLOC(destfile, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL));
snprintf(destfile, len, "%s%s", path, filename);
@@ -69,7 +69,7 @@ static char *get_destfile(const char *path, const char *filename) {
static char *get_tempfile(const char *path, const char *filename) {
char *tempfile;
/* len = localpath len + filename len + '.part' len + null */
- int len = strlen(path) + strlen(filename) + 6;
+ size_t len = strlen(path) + strlen(filename) + 6;
CALLOC(tempfile, len, sizeof(char), RET_ERR(PM_ERR_MEMORY, NULL));
snprintf(tempfile, len, "%s%s.part", path, filename);
@@ -92,7 +92,7 @@ static int download_internal(const char *url, const char *localpath,
struct url_stat ust;
struct stat st;
int chk_resume = 0, ret = 0;
- size_t dl_thisfile = 0;
+ off_t dl_thisfile = 0;
ssize_t nread = 0;
char *tempfile, *destfile, *filename;
struct sigaction new_action, old_action;
@@ -199,7 +199,7 @@ static int download_internal(const char *url, const char *localpath,
while((nread = fetchIO_read(dlf, buffer, PM_DLBUF_LEN)) > 0) {
size_t nwritten = 0;
- nwritten = fwrite(buffer, 1, nread, localf);
+ nwritten = fwrite(buffer, 1, (size_t)nread, localf);
if((nwritten != nread) || ferror(localf)) {
_alpm_log(PM_LOG_ERROR, _("error writing to file '%s': %s\n"),
destfile, strerror(errno));
@@ -300,7 +300,7 @@ int _alpm_download_single_file(const char *filename,
for(i = servers; i; i = i->next) {
const char *server = i->data;
char *fileurl = NULL;
- int len;
+ size_t len;
/* print server + filename into a buffer */
len = strlen(server) + strlen(filename) + 2;
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 012d412..5cbf363 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -169,7 +169,7 @@ const char SYMEXPORT *alpm_option_get_lockfile()
return handle->lockfile;
}
-unsigned short SYMEXPORT alpm_option_get_usesyslog()
+int SYMEXPORT alpm_option_get_usesyslog()
{
if (handle == NULL) {
pm_errno = PM_ERR_HANDLE_NULL;
@@ -223,6 +223,15 @@ const char SYMEXPORT *alpm_option_get_arch()
return handle->arch;
}
+int SYMEXPORT alpm_option_get_usedelta()
+{
+ if (handle == NULL) {
+ pm_errno = PM_ERR_HANDLE_NULL;
+ return -1;
+ }
+ return handle->usedelta;
+}
+
pmdb_t SYMEXPORT *alpm_option_get_localdb()
{
if (handle == NULL) {
@@ -437,7 +446,7 @@ int SYMEXPORT alpm_option_set_logfile(const char *logfile)
return(0);
}
-void SYMEXPORT alpm_option_set_usesyslog(unsigned short usesyslog)
+void SYMEXPORT alpm_option_set_usesyslog(int usesyslog)
{
handle->usesyslog = usesyslog;
}
@@ -536,7 +545,7 @@ void SYMEXPORT alpm_option_set_arch(const char *arch)
if(arch) handle->arch = strdup(arch);
}
-void SYMEXPORT alpm_option_set_usedelta(unsigned short usedelta)
+void SYMEXPORT alpm_option_set_usedelta(int usedelta)
{
handle->usedelta = usedelta;
}
diff --git a/lib/libalpm/handle.h b/lib/libalpm/handle.h
index a1eb1cd..afb2a1c 100644
--- a/lib/libalpm/handle.h
+++ b/lib/libalpm/handle.h
@@ -57,9 +57,9 @@ typedef struct _pmhandle_t {
alpm_list_t *ignoregrp; /* List of groups to ignore */
/* options */
- unsigned short usesyslog; /* Use syslog instead of logfile? */ /* TODO move to frontend */
+ int usesyslog; /* Use syslog instead of logfile? */ /* TODO move to frontend */
+ int usedelta; /* Download deltas if possible */
char *arch; /* Architecture of packages we should allow */
- unsigned short usedelta; /* Download deltas if possible */
} pmhandle_t;
/* global handle variable */
diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c
index de17166..a10c829 100644
--- a/lib/libalpm/package.c
+++ b/lib/libalpm/package.c
@@ -295,7 +295,7 @@ alpm_list_t SYMEXPORT *alpm_pkg_get_groups(pmpkg_t *pkg)
return pkg->groups;
}
-unsigned short SYMEXPORT alpm_pkg_has_force(pmpkg_t *pkg)
+int SYMEXPORT alpm_pkg_has_force(pmpkg_t *pkg)
{
ALPM_LOG_FUNC;
@@ -503,7 +503,9 @@ size_t SYMEXPORT alpm_pkg_changelog_read(void *ptr, size_t size,
if(pkg->origin == PKG_FROM_CACHE) {
ret = fread(ptr, 1, size, (FILE*)fp);
} else if(pkg->origin == PKG_FROM_FILE) {
- ret = archive_read_data((struct archive*)fp, ptr, size);
+ /* TODO: This is not valid; we can lose error codes here.
+ * For now the cast allows compiling with -Wsign-conversion */
+ ret = (size_t)archive_read_data((struct archive*)fp, ptr, size);
}
return(ret);
}
@@ -541,7 +543,7 @@ int SYMEXPORT alpm_pkg_changelog_close(const pmpkg_t *pkg, void *fp)
return(ret);
}
-unsigned short SYMEXPORT alpm_pkg_has_scriptlet(pmpkg_t *pkg)
+int SYMEXPORT alpm_pkg_has_scriptlet(pmpkg_t *pkg)
{
ALPM_LOG_FUNC;
diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c
index f31fbfb..ddc0987 100644
--- a/lib/libalpm/remove.c
+++ b/lib/libalpm/remove.c
@@ -372,13 +372,14 @@ int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
ASSERT(db != NULL, RET_ERR(PM_ERR_DB_NULL, -1));
ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
- pkg_count = alpm_list_count(trans->remove);
+ pkg_count = (int)alpm_list_count(trans->remove);
for(targ = trans->remove; targ; targ = targ->next) {
int position = 0;
char scriptlet[PATH_MAX];
info = (pmpkg_t*)targ->data;
const char *pkgname = NULL;
+ int targcount = (int)alpm_list_count(targ);
if(handle->trans->state == STATE_INTERRUPTED) {
return(0);
@@ -410,10 +411,10 @@ int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
}
}
- int filenum = alpm_list_count(files);
+ unsigned int filenum = alpm_list_count(files);
double percent = 0.0;
alpm_list_t *newfiles;
- _alpm_log(PM_LOG_DEBUG, "removing %d files\n", filenum);
+ _alpm_log(PM_LOG_DEBUG, "removing %ud files\n", filenum);
/* iterate through the list backwards, unlinking files */
newfiles = alpm_list_reverse(files);
@@ -424,7 +425,7 @@ int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
percent = (double)position / (double)filenum;
PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name,
(double)(percent * 100), pkg_count,
- (pkg_count - alpm_list_count(targ) + 1));
+ (pkg_count - targcount + 1));
position++;
}
alpm_list_free(newfiles);
@@ -432,7 +433,7 @@ int _alpm_remove_packages(pmtrans_t *trans, pmdb_t *db)
/* set progress to 100% after we finish unlinking files */
PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, pkgname, 100,
- pkg_count, (pkg_count - alpm_list_count(targ) + 1));
+ pkg_count, (pkg_count - targcount + 1));
/* run the post-remove script if it exists */
if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index a1a6ea7..392d66a 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -691,7 +691,7 @@ static int apply_deltas(pmtrans_t *trans)
pmdelta_t *d = dlts->data;
char *delta, *from, *to;
char command[PATH_MAX];
- int len = 0;
+ size_t len = 0;
delta = _alpm_filecache_find(d->delta);
/* the initial package might be in a different cachedir */
@@ -788,7 +788,7 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data)
{
alpm_list_t *i, *j, *files = NULL;
alpm_list_t *deltas = NULL;
- int replaces = 0;
+ unsigned int replaces = 0;
int errors = 0;
const char *cachedir = NULL;
int ret = -1;
diff --git a/lib/libalpm/trans.c b/lib/libalpm/trans.c
index c99f596..3ac6af3 100644
--- a/lib/libalpm/trans.c
+++ b/lib/libalpm/trans.c
@@ -407,7 +407,7 @@ cleanup:
return(retval);
}
-unsigned int SYMEXPORT alpm_trans_get_flags()
+int SYMEXPORT alpm_trans_get_flags()
{
/* Sanity checks */
ASSERT(handle != NULL, return(-1));
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index f78d193..9e91658 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -200,8 +200,7 @@ char *_alpm_strtrim(char *str)
int _alpm_lckmk()
{
int fd;
- pid_t pid;
- char *dir, *ptr, *spid = NULL;
+ char *dir, *ptr;
const char *file = alpm_option_get_lockfile();
/* create the dir of the lockfile first */
@@ -216,13 +215,11 @@ int _alpm_lckmk()
while((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000)) == -1
&& errno == EINTR);
if(fd > 0) {
- pid = getpid();
- size_t len = snprintf(spid, 0, "%ld\n", (long)pid) + 1;
- spid = malloc(len);
- snprintf(spid, len, "%ld\n", (long)pid);
- while(write(fd, (void *)spid, len) == -1 && errno == EINTR);
+ FILE *f = fdopen(fd, "w");
+ fprintf(f, "%ld\n", (long)getpid());
+ fflush(f);
fsync(fd);
- free(spid);
+ fclose(f);
return(fd);
}
return(-1);
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index 1dd3ffb..551e769 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -90,10 +90,10 @@ static float get_update_timediff(int first_call)
static void fill_progress(const int bar_percent, const int disp_percent,
const int proglen)
{
- const unsigned int hashlen = proglen - 8;
- const unsigned int hash = bar_percent * hashlen / 100;
- static unsigned int lasthash = 0, mouth = 0;
- unsigned int i;
+ const int hashlen = proglen - 8;
+ const int hash = bar_percent * hashlen / 100;
+ static int lasthash = 0, mouth = 0;
+ int i;
if(bar_percent == 0) {
lasthash = 0;
@@ -323,7 +323,8 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
int tmp, digits, textlen;
char *opr = NULL;
/* used for wide character width determination and printing */
- int len, wclen, wcwid, padwid;
+ size_t len;
+ int wclen, wcwid, padwid;
wchar_t *wcstr;
if(config->noprogressbar) {
@@ -396,7 +397,12 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
wclen += mbstowcs(wcstr + wclen, " ", len - wclen);
wclen += mbstowcs(wcstr + wclen, pkgname, len - wclen);
#endif
- wcwid = wcswidth(wcstr, wclen);
+ if(wclen < 0) {
+ /* we're probably screwed, so... */
+ free(wcstr);
+ return;
+ }
+ wcwid = wcswidth(wcstr, (size_t)wclen);
padwid = textlen - wcwid;
/* if padwid is < 0, we need to trim the string so padwid = 0 */
if(padwid < 0) {
@@ -452,7 +458,8 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
const int filenamelen = infolen - 27;
char *fname, *p;
/* used for wide character width determination and printing */
- int len, wclen, wcwid, padwid;
+ size_t len;
+ int wclen, wcwid, padwid;
wchar_t *wcfname;
int totaldownload;
@@ -508,7 +515,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
rate = xfered / (timediff * 1024.0);
/* round elapsed time to the nearest second */
- eta_s = (int)(timediff + 0.5);
+ eta_s = (unsigned int)(timediff + 0.5);
} else {
/* compute current average values */
timediff = get_update_timediff(0);
@@ -557,7 +564,13 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
len = strlen(filename) + 1;
wcfname = calloc(len, sizeof(wchar_t));
wclen = mbstowcs(wcfname, fname, len);
- wcwid = wcswidth(wcfname, wclen);
+ if(wclen < 0) {
+ /* we're probably screwed, so... */
+ free(fname);
+ free(wcfname);
+ return;
+ }
+ wcwid = wcswidth(wcfname, (size_t)wclen);
padwid = filenamelen - wcwid;
/* if padwid is < 0, we need to trim the string so padwid = 0 */
if(padwid < 0) {
diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index 3c588a7..598657c 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -64,7 +64,7 @@ typedef struct __config_t {
unsigned short group;
pmtransflag_t flags;
unsigned short noask;
- unsigned int ask;
+ pmtransprog_t ask;
/* conf file options */
unsigned short chomp; /* I Love Candy! */
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index ac51502..5e824f4 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -612,7 +612,7 @@ static char *get_filename(const char *url) {
static char *get_destfile(const char *path, const char *filename) {
char *destfile;
/* len = localpath len + filename len + null */
- int len = strlen(path) + strlen(filename) + 1;
+ size_t len = strlen(path) + strlen(filename) + 1;
destfile = calloc(len, sizeof(char));
snprintf(destfile, len, "%s%s", path, filename);
@@ -622,7 +622,7 @@ static char *get_destfile(const char *path, const char *filename) {
static char *get_tempfile(const char *path, const char *filename) {
char *tempfile;
/* len = localpath len + filename len + '.part' len + null */
- int len = strlen(path) + strlen(filename) + 6;
+ size_t len = strlen(path) + strlen(filename) + 6;
tempfile = calloc(len, sizeof(char));
snprintf(tempfile, len, "%s%s.part", path, filename);
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 353aae3..94e8f4a 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -232,9 +232,9 @@ char *mdirname(const char *path)
*/
void indentprint(const char *str, int indent)
{
- wchar_t *wcstr;
- const wchar_t *p;
- int len, cidx, cols;
+ wchar_t *wcstr, *p;
+ size_t len;
+ int cidx, cols;
if(!str) {
return;
@@ -260,7 +260,8 @@ void indentprint(const char *str, int indent)
while(*p) {
if(*p == L' ') {
- const wchar_t *q, *next;
+ const wchar_t *next;
+ wchar_t *q;
p++;
if(p == NULL || *p == L' ') continue;
next = wcschr(p, L' ');
@@ -271,7 +272,14 @@ void indentprint(const char *str, int indent)
len = 0;
q = p;
while(q < next) {
- len += wcwidth(*q++);
+ int w = wcwidth(*q);
+ if(w < 0) {
+ /* hmm. this is no good, kill this char */
+ *q = L'?';
+ w = 1;
+ }
+ q++;
+ len += (unsigned)w;
}
if(len > (cols - cidx - 1)) {
/* wrap to a newline and reindent */
@@ -377,7 +385,7 @@ char *strreplace(const char *str, const char *needle, const char *replace)
q = alpm_list_getdata(i);
if(q > p){
/* add chars between this occurence and last occurence, if any */
- strncpy(newp, p, q - p);
+ strncpy(newp, p, (unsigned)(q - p));
newp += q - p;
}
strncpy(newp, replace, replacesz);
@@ -411,7 +419,7 @@ alpm_list_t *strsplit(const char *str, const char splitchar)
char *dup = NULL;
while((str = strchr(str, splitchar))) {
- dup = strndup(prev, str - prev);
+ dup = strndup(prev, (size_t)(str - prev));
if(dup == NULL) {
return(NULL);
}
@@ -430,9 +438,9 @@ alpm_list_t *strsplit(const char *str, const char splitchar)
return(list);
}
-static int string_length(const char *s)
+static size_t string_length(const char *s)
{
- int len;
+ size_t len;
wchar_t *wcstr;
if(!s) {
@@ -442,7 +450,7 @@ static int string_length(const char *s)
len = strlen(s) + 1;
wcstr = calloc(len, sizeof(wchar_t));
len = mbstowcs(wcstr, s, len);
- len = wcswidth(wcstr, len);
+ len = (size_t)wcswidth(wcstr, len);
free(wcstr);
return(len);
@@ -450,16 +458,14 @@ static int string_length(const char *s)
void string_display(const char *title, const char *string)
{
- int len = 0;
-
if(title) {
- /* compute the length of title + a space */
- len = string_length(title) + 1;
printf("%s ", title);
}
if(string == NULL || string[0] == '\0') {
printf(_("None"));
} else {
+ /* compute the length of title + a space */
+ int len = string_length(title) + 1;
indentprint(string, len);
}
printf("\n");
--
1.6.4.4
2
1
[pacman-dev] [GIT] The official pacman repository branch, master, updated. v3.3.1-50-g8e7652f
by danï¼ archlinux.org 30 Sep '09
by danï¼ archlinux.org 30 Sep '09
30 Sep '09
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The official pacman repository".
The branch, master has been updated
via 8e7652f1afd7ba5198f3aa10a94c50bb67f7a407 (commit)
via 20392c0a025d01128eb78c6656db7c15b3fbcd89 (commit)
via 2071286770b3dbe531423aa3e8517dac68f04154 (commit)
via 7f14f185a2d05650e1f99f4ec40d60b597e8b0c7 (commit)
via 20aa17c276c35e08c7a8e2aa20410b99a20f8129 (commit)
from 86d4b8a3aad8317ee4f7db9dc0d897a0809c0c04 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 8e7652f1afd7ba5198f3aa10a94c50bb67f7a407
Merge: 2071286770b3dbe531423aa3e8517dac68f04154 20392c0a025d01128eb78c6656db7c15b3fbcd89
Author: Dan McGee <dan(a)archlinux.org>
Date: Tue Sep 29 21:08:06 2009 -0500
Merge branch 'maint'
Conflicts:
lib/libalpm/dload.c
commit 2071286770b3dbe531423aa3e8517dac68f04154
Author: Dan McGee <dan(a)archlinux.org>
Date: Mon Aug 24 11:19:26 2009 -0500
repo-add: clean up options parsing
-f/--force has been dead for a while, so kill it off. In addition, the
check for > 2 args is pretty useless when you do something like:
repo-add -q -q
or a more legit:
repo-add -q /path/to/mine.db.tar.gz
So instead make repo-add just return 1 when it doesn't do anything with
the database which seems to make more sense.
Signed-off-by: Dan McGee <dan(a)archlinux.org>
-----------------------------------------------------------------------
Summary of changes:
configure.ac | 6 +++---
lib/libalpm/dload.c | 29 ++++++++++++++++++++++-------
scripts/repo-add.sh.in | 29 ++++++++++++-----------------
3 files changed, 37 insertions(+), 27 deletions(-)
hooks/post-receive
--
The official pacman repository
1
0
[pacman-dev] [GIT] The official pacman repository branch, maint, updated. v3.3.1-3-g20392c0
by danï¼ archlinux.org 30 Sep '09
by danï¼ archlinux.org 30 Sep '09
30 Sep '09
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The official pacman repository".
The branch, maint has been updated
via 20392c0a025d01128eb78c6656db7c15b3fbcd89 (commit)
via 7f14f185a2d05650e1f99f4ec40d60b597e8b0c7 (commit)
via 20aa17c276c35e08c7a8e2aa20410b99a20f8129 (commit)
from 7cead800c52e635d7f5010875cd8fcf99b93f4b2 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
-----------------------------------------------------------------------
Summary of changes:
configure.ac | 6 +++---
lib/libalpm/dload.c | 29 ++++++++++++++++++++++-------
scripts/repo-add.sh.in | 16 +++++++++++-----
3 files changed, 36 insertions(+), 15 deletions(-)
hooks/post-receive
--
The official pacman repository
1
0
26 Sep '09
This offers a cleaner way to deal with constant in enum and allow easy
maintainance
Signed-off-by: solsTiCe d'Hiver <solstice.dhiver(a)gmail.com>
---
lib/libalpm/alpm.h | 56 ++++++++++++++++++++++++++--------------------------
lib/libalpm/db.h | 12 +++++-----
2 files changed, 34 insertions(+), 34 deletions(-)
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index e9fadca..e12e009 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -67,10 +67,10 @@ const char *alpm_version(void);
/* Levels */
typedef enum _pmloglevel_t {
- PM_LOG_ERROR = 0x01,
- PM_LOG_WARNING = 0x02,
- PM_LOG_DEBUG = 0x04,
- PM_LOG_FUNCTION = 0x08
+ PM_LOG_ERROR = 1,
+ PM_LOG_WARNING = (1 << 1),
+ PM_LOG_DEBUG = (1 << 2),
+ PM_LOG_FUNCTION = (1 << 3)
} pmloglevel_t;
typedef void (*alpm_cb_log)(pmloglevel_t, char *, va_list);
@@ -265,24 +265,24 @@ pmpkg_t *alpm_sync_newversion(pmpkg_t *pkg, alpm_list_t *dbs_sync);
/* Flags */
typedef enum _pmtransflag_t {
- PM_TRANS_FLAG_NODEPS = 0x01,
- PM_TRANS_FLAG_FORCE = 0x02,
- PM_TRANS_FLAG_NOSAVE = 0x04,
- /* 0x08 flag can go here */
- PM_TRANS_FLAG_CASCADE = 0x10,
- PM_TRANS_FLAG_RECURSE = 0x20,
- PM_TRANS_FLAG_DBONLY = 0x40,
- /* 0x80 flag can go here */
- PM_TRANS_FLAG_ALLDEPS = 0x100,
- PM_TRANS_FLAG_DOWNLOADONLY = 0x200,
- PM_TRANS_FLAG_NOSCRIPTLET = 0x400,
- PM_TRANS_FLAG_NOCONFLICTS = 0x800,
- /* 0x1000 flag can go here */
- PM_TRANS_FLAG_NEEDED = 0x2000,
- PM_TRANS_FLAG_ALLEXPLICIT = 0x4000,
- PM_TRANS_FLAG_UNNEEDED = 0x8000,
- PM_TRANS_FLAG_RECURSEALL = 0x10000,
- PM_TRANS_FLAG_NOLOCK = 0x20000
+ PM_TRANS_FLAG_NODEPS = 1,
+ PM_TRANS_FLAG_FORCE = (1 << 1),
+ PM_TRANS_FLAG_NOSAVE = (1 << 2),
+ /* (1 << 3) flag can go here */
+ PM_TRANS_FLAG_CASCADE = (1 << 4),
+ PM_TRANS_FLAG_RECURSE = (1 << 5),
+ PM_TRANS_FLAG_DBONLY = (1 << 6),
+ /* (1 << 7) flag can go here */
+ PM_TRANS_FLAG_ALLDEPS = (1 << 8),
+ PM_TRANS_FLAG_DOWNLOADONLY = (1 << 9),
+ PM_TRANS_FLAG_NOSCRIPTLET = (1 << 10),
+ PM_TRANS_FLAG_NOCONFLICTS = (1 << 11),
+ /* (1 << 12) flag can go here */
+ PM_TRANS_FLAG_NEEDED = (1 << 13),
+ PM_TRANS_FLAG_ALLEXPLICIT = (1 << 14),
+ PM_TRANS_FLAG_UNNEEDED = (1 << 15),
+ PM_TRANS_FLAG_RECURSEALL = (1 << 16),
+ PM_TRANS_FLAG_NOLOCK = (1 << 17)
} pmtransflag_t;
/**
@@ -369,12 +369,12 @@ typedef enum _pmtransevt_t {
/* Transaction Conversations (ie, questions) */
typedef enum _pmtransconv_t {
- PM_TRANS_CONV_INSTALL_IGNOREPKG = 0x01,
- PM_TRANS_CONV_REPLACE_PKG = 0x02,
- PM_TRANS_CONV_CONFLICT_PKG = 0x04,
- PM_TRANS_CONV_CORRUPTED_PKG = 0x08,
- PM_TRANS_CONV_LOCAL_NEWER = 0x10,
- PM_TRANS_CONV_REMOVE_PKGS = 0x20,
+ PM_TRANS_CONV_INSTALL_IGNOREPKG = 1,
+ PM_TRANS_CONV_REPLACE_PKG = (1 << 1),
+ PM_TRANS_CONV_CONFLICT_PKG = (1 << 2),
+ PM_TRANS_CONV_CORRUPTED_PKG = (1 << 3),
+ PM_TRANS_CONV_LOCAL_NEWER = (1 << 4),
+ PM_TRANS_CONV_REMOVE_PKGS = (1 << 5),
} pmtransconv_t;
/* Transaction Progress */
diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h
index 2981603..8a838ce 100644
--- a/lib/libalpm/db.h
+++ b/lib/libalpm/db.h
@@ -28,12 +28,12 @@
/* Database entries */
typedef enum _pmdbinfrq_t {
- INFRQ_BASE = 0x01,
- INFRQ_DESC = 0x02,
- INFRQ_DEPENDS = 0x04,
- INFRQ_FILES = 0x08,
- INFRQ_SCRIPTLET = 0x10,
- INFRQ_DELTAS = 0x20,
+ INFRQ_BASE = 1,
+ INFRQ_DESC = (1 << 1),
+ INFRQ_DEPENDS = (1 << 2),
+ INFRQ_FILES = (1 << 3),
+ INFRQ_SCRIPTLET = (1 << 4),
+ INFRQ_DELTAS = (1 << 5),
/* ALL should be sum of all above */
INFRQ_ALL = 0x3F
} pmdbinfrq_t;
--
1.6.4.4
1
0
[pacman-dev] [PATCH 8/8] Replace hardcoded option numbers with enumeration
by Laszlo Papp 25 Sep '09
by Laszlo Papp 25 Sep '09
25 Sep '09
Pacman's long option parsing used hardcoded numbers to identify them.
This is not good practice, so replace them with enumeration constants.
./src/pacman/conf.h:
- This contains the enumeration of long options that are used
in pacman.c source file.
./src/pacman/pacman.c
- This source contains long options where the changing was
happened. It's safer and more comfortable now, instead of
hard coding 10-15 or more integer value into the code.
Signed-off-by: Laszlo Papp <djszapi(a)archlinux.us>
---
src/pacman/conf.h | 18 ++++++++++++++++
src/pacman/pacman.c | 56 +++++++++++++++++++++++++-------------------------
2 files changed, 46 insertions(+), 28 deletions(-)
diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index 3c588a7..c854df4 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -88,6 +88,24 @@ enum {
PM_OP_DEPTEST
};
+/* Long Operations */
+enum {
+ PM_LONG_OP_NOCONFIRM = 1000,
+ PM_LONG_OP_CONFIG,
+ PM_LONG_OP_IGNORE,
+ PM_LONG_OP_DEBUG,
+ PM_LONG_OP_NOPROGRESSBAR,
+ PM_LONG_OP_NOSCRIPTLET,
+ PM_LONG_OP_ASK,
+ PM_LONG_OP_CACHEDIR,
+ PM_LONG_OP_ASDEPS,
+ PM_LONG_OP_LOGFILE,
+ PM_LONG_OP_IGNOREGROUP,
+ PM_LONG_OP_NEEDED,
+ PM_LONG_OP_ASEXPLICIT,
+ PM_LONG_OP_ARCH
+};
+
/* clean method */
enum {
PM_CLEAN_KEEPINST = 0, /* default */
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index ac51502..30b39b5 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -378,20 +378,20 @@ static int parseargs(int argc, char *argv[])
{"verbose", no_argument, 0, 'v'},
{"downloadonly", no_argument, 0, 'w'},
{"refresh", no_argument, 0, 'y'},
- {"noconfirm", no_argument, 0, 1000},
- {"config", required_argument, 0, 1001},
- {"ignore", required_argument, 0, 1002},
- {"debug", optional_argument, 0, 1003},
- {"noprogressbar", no_argument, 0, 1004},
- {"noscriptlet", no_argument, 0, 1005},
- {"ask", required_argument, 0, 1006},
- {"cachedir", required_argument, 0, 1007},
- {"asdeps", no_argument, 0, 1008},
- {"logfile", required_argument, 0, 1009},
- {"ignoregroup", required_argument, 0, 1010},
- {"needed", no_argument, 0, 1011},
- {"asexplicit", no_argument, 0, 1012},
- {"arch", required_argument, 0, 1013},
+ {"noconfirm", no_argument, 0, PM_LONG_OP_NOCONFIRM},
+ {"config", required_argument, 0, PM_LONG_OP_CONFIG},
+ {"ignore", required_argument, 0, PM_LONG_OP_IGNORE},
+ {"debug", optional_argument, 0, PM_LONG_OP_DEBUG},
+ {"noprogressbar", no_argument, 0, PM_LONG_OP_NOPROGRESSBAR},
+ {"noscriptlet", no_argument, 0, PM_LONG_OP_NOSCRIPTLET},
+ {"ask", required_argument, 0, PM_LONG_OP_ASK},
+ {"cachedir", required_argument, 0, PM_LONG_OP_CACHEDIR},
+ {"asdeps", no_argument, 0, PM_LONG_OP_ASDEPS},
+ {"logfile", required_argument, 0, PM_LONG_OP_LOGFILE},
+ {"ignoregroup", required_argument, 0, PM_LONG_OP_IGNOREGROUP},
+ {"needed", no_argument, 0, PM_LONG_OP_NEEDED},
+ {"asexplicit", no_argument, 0, PM_LONG_OP_ASEXPLICIT},
+ {"arch", required_argument, 0, PM_LONG_OP_ARCH},
{0, 0, 0, 0}
};
@@ -403,21 +403,21 @@ static int parseargs(int argc, char *argv[])
}
switch(opt) {
case 0: break;
- case 1000: config->noconfirm = 1; break;
- case 1001:
+ case PM_LONG_OP_NOCONFIRM: config->noconfirm = 1; break;
+ case PM_LONG_OP_CONFIG:
if(config->configfile) {
free(config->configfile);
}
config->configfile = strndup(optarg, PATH_MAX);
break;
- case 1002:
+ case PM_LONG_OP_IGNORE:
list = strsplit(optarg, ',');
for(item = list; item; item = alpm_list_next(item)) {
alpm_option_add_ignorepkg((char *)alpm_list_getdata(item));
}
FREELIST(list);
break;
- case 1003:
+ case PM_LONG_OP_DEBUG:
/* debug levels are made more 'human readable' than using a raw logmask
* here, error and warning are set in config_new, though perhaps a
* --quiet option will remove these later */
@@ -440,34 +440,34 @@ static int parseargs(int argc, char *argv[])
/* progress bars get wonky with debug on, shut them off */
config->noprogressbar = 1;
break;
- case 1004: config->noprogressbar = 1; break;
- case 1005: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
- case 1006: config->noask = 1; config->ask = atoi(optarg); break;
- case 1007:
+ case PM_LONG_OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
+ case PM_LONG_OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
+ case PM_LONG_OP_ASK: config->noask = 1; config->ask = atoi(optarg); break;
+ case PM_LONG_OP_CACHEDIR:
if(alpm_option_add_cachedir(optarg) != 0) {
pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
optarg, alpm_strerrorlast());
return(1);
}
break;
- case 1008:
+ case PM_LONG_OP_ASDEPS:
config->flags |= PM_TRANS_FLAG_ALLDEPS;
break;
- case 1009:
+ case PM_LONG_OP_LOGFILE:
config->logfile = strndup(optarg, PATH_MAX);
break;
- case 1010:
+ case PM_LONG_OP_IGNOREGROUP:
list = strsplit(optarg, ',');
for(item = list; item; item = alpm_list_next(item)) {
alpm_option_add_ignoregrp((char *)alpm_list_getdata(item));
}
FREELIST(list);
break;
- case 1011: config->flags |= PM_TRANS_FLAG_NEEDED; break;
- case 1012:
+ case PM_LONG_OP_NEEDED: config->flags |= PM_TRANS_FLAG_NEEDED; break;
+ case PM_LONG_OP_ASEXPLICIT:
config->flags |= PM_TRANS_FLAG_ALLEXPLICIT;
break;
- case 1013:
+ case PM_LONG_OP_ARCH:
setarch(optarg);
break;
case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
--
1.6.4.4
1
0
Today I got very strange results downloading packages with pacman.
For example :
gnome-common-2.28.0... 8,9K 112,2K/s 00:00:00
[#####################] 21167%
> ls -lh /var/cache/pacman/pkg/gnome-common-2.28.0-1-any.pkg.tar.gz
-rw-r--r-- 1 root root 8,9K sept. 24 00:17
/var/cache/pacman/pkg/gnome-common-2.28.0-1-any.pkg.tar.gz
/var/lib/pacman/sync/gnome-unstable/gnome-common-2.28.0-1/desc:%CSIZE%
/var/lib/pacman/sync/gnome-unstable/gnome-common-2.28.0-1/desc-43
43 bytes instead of 8900 ? wtf ?
3
7
./src/pacman/pacman.h:
- The defines were placed here that are used in the
pacman.c source file
./src/pacman/pacman.c
- Long options were refactored because of a safer and more
comfortable programming style, instead of hard coding
10-15 or more integer value into the code
Signed-off-by: Laszlo Papp <djszapi(a)archlinux.us>
---
src/pacman/pacman.c | 56 +++++++++++++++++++++++++-------------------------
src/pacman/pacman.h | 16 ++++++++++++++
2 files changed, 44 insertions(+), 28 deletions(-)
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index ac51502..2d806d4 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -378,20 +378,20 @@ static int parseargs(int argc, char *argv[])
{"verbose", no_argument, 0, 'v'},
{"downloadonly", no_argument, 0, 'w'},
{"refresh", no_argument, 0, 'y'},
- {"noconfirm", no_argument, 0, 1000},
- {"config", required_argument, 0, 1001},
- {"ignore", required_argument, 0, 1002},
- {"debug", optional_argument, 0, 1003},
- {"noprogressbar", no_argument, 0, 1004},
- {"noscriptlet", no_argument, 0, 1005},
- {"ask", required_argument, 0, 1006},
- {"cachedir", required_argument, 0, 1007},
- {"asdeps", no_argument, 0, 1008},
- {"logfile", required_argument, 0, 1009},
- {"ignoregroup", required_argument, 0, 1010},
- {"needed", no_argument, 0, 1011},
- {"asexplicit", no_argument, 0, 1012},
- {"arch", required_argument, 0, 1013},
+ {"noconfirm", no_argument, 0, NOCONFIRM_OPTID},
+ {"config", required_argument, 0, CONFIG_OPTID},
+ {"ignore", required_argument, 0, IGNORE_OPTID},
+ {"debug", optional_argument, 0, DEBUG_OPTID},
+ {"noprogressbar", no_argument, 0, NOPROGRESSBAR_OPTID},
+ {"noscriptlet", no_argument, 0, NOSCRIPTLET_OPTID},
+ {"ask", required_argument, 0, ASK_OPTID},
+ {"cachedir", required_argument, 0, CACHEDIR_OPTID},
+ {"asdeps", no_argument, 0, ASDEPS_OPTID},
+ {"logfile", required_argument, 0, LOGFILE_OPTID},
+ {"ignoregroup", required_argument, 0, IGNOREGROUP_OPTID},
+ {"needed", no_argument, 0, NEEDED_OPTID},
+ {"asexplicit", no_argument, 0, ASEXPLICIT_OPTID},
+ {"arch", required_argument, 0, ARCH_OPTID},
{0, 0, 0, 0}
};
@@ -403,21 +403,21 @@ static int parseargs(int argc, char *argv[])
}
switch(opt) {
case 0: break;
- case 1000: config->noconfirm = 1; break;
- case 1001:
+ case NOCONFIRM_OPTID: config->noconfirm = 1; break;
+ case CONFIG_OPTID:
if(config->configfile) {
free(config->configfile);
}
config->configfile = strndup(optarg, PATH_MAX);
break;
- case 1002:
+ case IGNORE_OPTID:
list = strsplit(optarg, ',');
for(item = list; item; item = alpm_list_next(item)) {
alpm_option_add_ignorepkg((char *)alpm_list_getdata(item));
}
FREELIST(list);
break;
- case 1003:
+ case DEBUG_OPTID:
/* debug levels are made more 'human readable' than using a raw logmask
* here, error and warning are set in config_new, though perhaps a
* --quiet option will remove these later */
@@ -440,34 +440,34 @@ static int parseargs(int argc, char *argv[])
/* progress bars get wonky with debug on, shut them off */
config->noprogressbar = 1;
break;
- case 1004: config->noprogressbar = 1; break;
- case 1005: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
- case 1006: config->noask = 1; config->ask = atoi(optarg); break;
- case 1007:
+ case NOPROGRESSBAR_OPTID: config->noprogressbar = 1; break;
+ case NOSCRIPTLET_OPTID: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
+ case ASK_OPTID: config->noask = 1; config->ask = atoi(optarg); break;
+ case CACHEDIR_OPTID:
if(alpm_option_add_cachedir(optarg) != 0) {
pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
optarg, alpm_strerrorlast());
return(1);
}
break;
- case 1008:
+ case ASDEPS_OPTID:
config->flags |= PM_TRANS_FLAG_ALLDEPS;
break;
- case 1009:
+ case LOGFILE_OPTID:
config->logfile = strndup(optarg, PATH_MAX);
break;
- case 1010:
+ case IGNOREGROUP_OPTID:
list = strsplit(optarg, ',');
for(item = list; item; item = alpm_list_next(item)) {
alpm_option_add_ignoregrp((char *)alpm_list_getdata(item));
}
FREELIST(list);
break;
- case 1011: config->flags |= PM_TRANS_FLAG_NEEDED; break;
- case 1012:
+ case NEEDED_OPTID: config->flags |= PM_TRANS_FLAG_NEEDED; break;
+ case ASEXPLICIT_OPTID:
config->flags |= PM_TRANS_FLAG_ALLEXPLICIT;
break;
- case 1013:
+ case ARCH_OPTID:
setarch(optarg);
break;
case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
diff --git a/src/pacman/pacman.h b/src/pacman/pacman.h
index d7cb50f..f278e49 100644
--- a/src/pacman/pacman.h
+++ b/src/pacman/pacman.h
@@ -22,6 +22,22 @@
#include <alpm_list.h>
+#define NOCONFIRM_OPTID 1000
+#define CONFIG_OPTID 1001
+#define IGNORE_OPTID 1002
+#define DEBUG_OPTID 1003
+#define NOPROGRESSBAR_OPTID 1004
+#define NOSCRIPTLET_OPTID 1005
+#define ASK_OPTID 1006
+#define CACHEDIR_OPTID 1007
+#define ASDEPS_OPTID 1008
+#define LOGFILE_OPTID 1009
+#define IGNOREGROUP_OPTID 1010
+#define NEEDED_OPTID 1011
+#define ASEXPLICIT_OPTID 1012
+#define ARCH_OPTID 1013
+
+
/* query.c */
int pacman_query(alpm_list_t *targets);
/* remove.c */
--
1.6.4.4
4
4
Hi,
With the community-testing repo now being empty, I get the following:
> pacman -Syu
:: Synchronizing package databases...
kernel64 is up to date
testing 10.3K 21.2K/s 00:00:00
[#####################] 100%
core 33.8K 34.8K/s 00:00:01
[#####################] 100%
extra 429.2K 13.0K/s 00:00:33
[#####################] 100%
error: failed retrieving file 'community-testing.db.tar.gz' from
<"mirror"> : Not Found
error: failed to update community-testing (Not Found)
community 365.3K 10.2K/s 00:00:36
[#####################] 100%
:: Starting full system upgrade...
local database is up to date
Can we leave a shell database somehow when using repo-rm on the last
package? It seems [testing] has never been empty!
Allan
5
8
Split the huge list of else ifs which was used for parsing 'desc',
'depends', 'files' and 'deltas' files, into separate functions and move
them to a new file parse.c . This makes it possible for other backends
to share the same parse code. Also change the parsing from the giant
else if list to a single loop calling different functions as specified
in an array of parser structs (see parse_lines() for details).
Move _alpm_delta_parse to parse.c and make it static.
Signed-off-by: Henning Garus <henning.garus(a)gmail.com>
---
I actually managed to make it longer, instead of shorter (well if you
take out comments and license headers this version is roughly as long
as the old one), however I think it is more readable. Feel free to call
it overkill though.
I don't really like the ugets stuff I used to get the next line, but I
wanted this to work on something other than FILE*, even though this is
not used (yet), and this was the best thing I could come up with. I
toyed around with using fopencookie, but that sucks when it comes to
portability (there is funopen on bsd, beyond that you are simply
screwed).
I removed the memset for the line array, it should be fairly safe and I
ran valgrind with several pactests and it did not shout at me (at least
after I supressed the numerous leaks reported for bash).
Btw, is there a reason why pacman uses two different file formats (line
based in db files vs. keyword = value in .PKGINFO ?
lib/libalpm/Makefile.am | 1 +
lib/libalpm/be_files.c | 222 +++++-----------------------------
lib/libalpm/delta.c | 56 ---------
lib/libalpm/delta.h | 1 -
lib/libalpm/parse.c | 305 +++++++++++++++++++++++++++++++++++++++++++++++
lib/libalpm/parse.h | 40 ++++++
6 files changed, 377 insertions(+), 248 deletions(-)
create mode 100644 lib/libalpm/parse.c
create mode 100644 lib/libalpm/parse.h
diff --git a/lib/libalpm/Makefile.am b/lib/libalpm/Makefile.am
index 871855e..5ed8866 100644
--- a/lib/libalpm/Makefile.am
+++ b/lib/libalpm/Makefile.am
@@ -27,6 +27,7 @@ libalpm_la_SOURCES = \
backup.h backup.c \
be_files.c \
be_package.c \
+ parse.h parse.c \
cache.h cache.c \
conflict.h conflict.c \
db.h db.c \
diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index 21533ef..1066b60 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -31,7 +31,6 @@
#include <ctype.h>
#include <time.h>
#include <limits.h> /* PATH_MAX */
-#include <locale.h> /* setlocale */
/* libalpm */
#include "db.h"
@@ -42,9 +41,9 @@
#include "alpm.h"
#include "handle.h"
#include "package.h"
-#include "delta.h"
#include "deps.h"
#include "dload.h"
+#include "parse.h"
/*
@@ -386,7 +385,8 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
{
FILE *fp = NULL;
char path[PATH_MAX];
- char line[513];
+ char *pkgname = NULL;
+ char *pkgver = NULL;
char *pkgpath = NULL;
ALPM_LOG_FUNC;
@@ -417,9 +417,6 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
_alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
info->name, inforeq);
- /* clear out 'line', to be certain - and to make valgrind happy */
- memset(line, 0, 513);
-
pkgpath = get_pkgpath(db, info);
if(access(pkgpath, F_OK)) {
@@ -429,6 +426,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
goto error;
}
+
/* DESC */
if(inforeq & INFRQ_DESC) {
snprintf(path, PATH_MAX, "%sdesc", pkgpath);
@@ -436,139 +434,26 @@ 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, "%NAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- if(strcmp(_alpm_strtrim(line), info->name) != 0) {
- _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
- "mismatch on package %s\n"), db->treename, info->name);
- }
- } else if(strcmp(line, "%VERSION%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- if(strcmp(_alpm_strtrim(line), info->version) != 0) {
- _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
- "mismatch on package %s\n"), db->treename, info->name);
- }
- } else if(strcmp(line, "%FILENAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->filename, _alpm_strtrim(line), goto error);
- } else if(strcmp(line, "%DESC%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->desc, _alpm_strtrim(line), 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);
- }
- } else if(strcmp(line, "%URL%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->url, _alpm_strtrim(line), 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);
- }
- } else if(strcmp(line, "%ARCH%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->arch, _alpm_strtrim(line), 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 in case 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);
- }
- } 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 in case 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);
- }
- } else if(strcmp(line, "%PACKAGER%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->packager, _alpm_strtrim(line), goto error);
- } else if(strcmp(line, "%REASON%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
- } 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));
- /* 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));
- } 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);
- } 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);
- }
- } else if(strcmp(line, "%FORCE%") == 0) {
- info->force = 1;
- }
+ STRDUP(pkgname, info->name, goto error);
+ STRDUP(pkgver, info->version, goto error);
+ if(_alpm_parse_desc(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
+ if(strcmp(pkgname,info->name) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name mismatch "
+ "on package %s\n"), db->treename, pkgname);
+ }
+ if(strcmp(pkgver,info->version) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version mismatch "
+ "on package %s\n"), db->treename, pkgver);
+ }
+ FREE(pkgname);
+ FREE(pkgver);
+ /* store size in isize if isize is unset */
+ if(info->isize == 0) {
+ info->isize = info->size;
+ }
+
fclose(fp);
fp = NULL;
}
@@ -580,21 +465,8 @@ 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);
- }
- } 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_parse_files(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
fclose(fp);
fp = NULL;
@@ -607,33 +479,8 @@ 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);
- }
- } 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);
- }
- } 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);
- }
- } 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_parse_depends(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
fclose(fp);
fp = NULL;
@@ -643,17 +490,8 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
if(inforeq & INFRQ_DELTAS) {
snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
if((fp = fopen(path, "r"))) {
- while(!feof(fp)) {
- fgets(line, 255, fp);
- _alpm_strtrim(line);
- if(strcmp(line, "%DELTAS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- pmdelta_t *delta = _alpm_delta_parse(line);
- if(delta) {
- info->deltas = alpm_list_add(info->deltas, delta);
- }
- }
- }
+ if(_alpm_parse_deltas(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
fclose(fp);
fp = NULL;
@@ -675,6 +513,8 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
return(0);
error:
+ free(pkgname);
+ free(pkgver);
free(pkgpath);
if(fp) {
fclose(fp);
diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c
index 523968e..5202e4b 100644
--- a/lib/libalpm/delta.c
+++ b/lib/libalpm/delta.c
@@ -25,7 +25,6 @@
#include <stdint.h> /* intmax_t */
#include <limits.h>
#include <sys/types.h>
-#include <regex.h>
/* libalpm */
#include "delta.h"
@@ -247,61 +246,6 @@ off_t _alpm_shortest_delta_path(alpm_list_t *deltas,
return(bestsize);
}
-/** Parses the string representation of a pmdelta_t object.
- * This function assumes that the string is in the correct format.
- * This format is as follows:
- * $deltafile $deltamd5 $deltasize $oldfile $newfile
- * @param line the string to parse
- * @return A pointer to the new pmdelta_t object
- */
-/* TODO this does not really belong here, but in a parsing lib */
-pmdelta_t *_alpm_delta_parse(char *line)
-{
- pmdelta_t *delta;
- char *tmp = line, *tmp2;
- regex_t reg;
-
- regcomp(®,
- "^[^[:space:]]* [[:xdigit:]]{32} [[:digit:]]*"
- " [^[:space:]]* [^[:space:]]*$",
- REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
- if(regexec(®, line, 0, 0, 0) != 0) {
- /* delta line is invalid, return NULL */
- regfree(®);
- return(NULL);
- }
- regfree(®);
-
- CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- delta->delta_size = atol(tmp2);
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- STRDUP(delta->from, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- _alpm_log(PM_LOG_DEBUG, "delta : %s %s '%lld'\n", delta->from, delta->to, (long long)delta->delta_size);
-
- return(delta);
-}
-
void _alpm_delta_free(pmdelta_t *delta)
{
FREE(delta->from);
diff --git a/lib/libalpm/delta.h b/lib/libalpm/delta.h
index 4f426cb..3c4e4cf 100644
--- a/lib/libalpm/delta.h
+++ b/lib/libalpm/delta.h
@@ -39,7 +39,6 @@ struct __pmdelta_t {
off_t download_size;
};
-pmdelta_t *_alpm_delta_parse(char *line);
void _alpm_delta_free(pmdelta_t *delta);
off_t _alpm_shortest_delta_path(alpm_list_t *deltas,
const char *to, alpm_list_t **path);
diff --git a/lib/libalpm/parse.c b/lib/libalpm/parse.c
new file mode 100644
index 0000000..31038fc
--- /dev/null
+++ b/lib/libalpm/parse.c
@@ -0,0 +1,305 @@
+/*
+ * parse.c
+ *
+ * Copyright (c) 2006-2009 Pacman Development Team <pacman-dev(a)archlinux.org>
+ * Copyright (c) 2002-2006 by Judd Vinet <jvinet(a)zeroflux.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <locale.h> /* setlocale */
+#include <regex.h>
+#include <time.h>
+
+/* libalpm */
+#include "parse.h"
+#include "util.h"
+#include "alpm_list.h"
+#include "log.h"
+#include "package.h"
+#include "delta.h"
+#include "deps.h"
+
+enum {
+ PARSE_FINISHED = 0,
+ PARSE_UNFINISHED = 1,
+ PARSE_ERROR = 2
+};
+
+typedef struct parser {
+ char *keyword;
+ int (*func)(char*, void*);
+ void *saveto;
+} parser_t;
+
+/** Parses the string representation of a pmdelta_t object.
+ * This function assumes that the string is in the correct format.
+ * This format is as follows:
+ * $deltafile $deltamd5 $deltasize $oldfile $newfile
+ * @param line the string to parse
+ * @return A pointer to the new pmdelta_t object
+ */
+static pmdelta_t *delta_parse(char *line)
+{
+ pmdelta_t *delta;
+ char *tmp = line, *tmp2;
+ regex_t reg;
+
+ regcomp(®,
+ "^[^[:space:]]* [[:xdigit:]]{32} [[:digit:]]*"
+ " [^[:space:]]* [^[:space:]]*$",
+ REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
+ if(regexec(®, line, 0, 0, 0) != 0) {
+ /* delta line is invalid, return NULL */
+ regfree(®);
+ return(NULL);
+ }
+ regfree(®);
+
+ CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ delta->delta_size = atol(tmp2);
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ STRDUP(delta->from, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ _alpm_log(PM_LOG_DEBUG, "delta : %s %s '%lld'\n", delta->from, delta->to, (long long)delta->delta_size);
+
+ return(delta);
+}
+
+/** Parse line based data, as found in desc, depends, files and deltas files
+ * The data is expected to be in the following format:
+ * keyword
+ * [entry]
+ * [entry]
+ * ...
+ * <newline>
+ * [keyword]
+ * ...
+ *
+ * The handling of the data for each keyword is described by the matching
+ * struct of type parser_t. This struct holds a function pointer and a void
+ * pointer for each keyword. The function is called for the lines following a
+ * keyword until it returns PARSE_FINISHED. It is expected to handle the actual
+ * parsing and store the data at the passed void pointer.
+ * @param parsers describes how to parse data for each keyword
+ * @param nparsers size of parsers
+ * @param ugets pointer to an fgets like function, which gets called to get
+ * the next line
+ * @param from describes from where to read the next line, this is passed to
+ * ugets
+ */
+static int parse_lines(parser_t parsers[], int nparsers, ugets_t ugets,
+ void *from)
+{
+ int i, ret;
+ char line[513];
+ parser_t *p = NULL;
+
+ while((*ugets)(line, sizeof(line) - 1, from) != NULL) {
+ _alpm_strtrim(line);
+ if(p == NULL && strlen(line) == 0) {
+ continue;
+ }
+ if(p == NULL) {
+ for(i = 0; i < nparsers; i++) {
+ if(strcmp(line,parsers[i].keyword) == 0) {
+ p = &parsers[i];
+ }
+ }
+ }
+ else {
+ ret = (*(p->func))(line, p->saveto);
+ if(ret == PARSE_FINISHED) {
+ p = NULL;
+ }
+ else if(ret == PARSE_ERROR) {
+ return(ret);
+ }
+ }
+ }
+ return(0);
+}
+
+static int copy_line(char *line, void *target) {
+ STRDUP(*(char**)target, line, return(PARSE_ERROR));
+ return(PARSE_FINISHED);
+}
+
+static int parse_off_t(char *line, void *target) {
+ *(off_t*)target = (off_t)atol(line);
+ return(PARSE_FINISHED);
+}
+
+static int parse_reason(char *line, void *target) {
+ *(pmpkgreason_t*)target = (pmpkgreason_t)atol(line);
+ return(PARSE_FINISHED);
+}
+
+static int parse_date(char *line, void *target) {
+ char first = tolower(line[0]);
+ if(first > 'a' && first < 'z') {
+ struct tm tmp_tm = {0}; /* initialize to null in case of failure */
+ setlocale(LC_TIME, "C");
+ strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
+ *(time_t*)target = mktime(&tmp_tm);
+ setlocale(LC_TIME, "");
+ } else {
+ *(time_t*)target = atol(line);
+ }
+ return(PARSE_FINISHED);
+}
+
+static int set_flag(char *line, void *target) {
+ *(unsigned short*)target = 1;
+ return(PARSE_FINISHED);
+}
+
+static int copy_to_list(char *line, void *list) {
+ if(strlen(line) == 0) {
+ return(PARSE_FINISHED);
+ }
+ char *linedup;
+ STRDUP(linedup, line, RET_ERR(PM_ERR_MEMORY, PARSE_ERROR));
+ *(alpm_list_t**)list = alpm_list_add(*(alpm_list_t**)list, linedup);
+ return(PARSE_UNFINISHED);
+}
+
+static int deltas_to_list(char *line, void *list) {
+ if(strlen(line) == 0) {
+ return(PARSE_FINISHED);
+ }
+ pmdelta_t *delta = delta_parse(line);
+ if(delta != NULL) {
+ *(alpm_list_t**)list = alpm_list_add(*(alpm_list_t**)list, delta);
+ }
+ return(PARSE_UNFINISHED);
+}
+
+static int deps_to_list(char *line, void *list) {
+ if(strlen(line) == 0) {
+ return(PARSE_FINISHED);
+ }
+ pmdepend_t *dep = _alpm_splitdep(line);
+ *(alpm_list_t**)list = alpm_list_add(*(alpm_list_t**)list, dep);
+ return(PARSE_UNFINISHED);
+}
+
+char *_alpm_parse_from_file(char *buf,int size, void* fp)
+{
+ return(fgets(buf, size, (FILE*)fp));
+}
+
+char *_alpm_parse_from_archive(char *buf,int size, void* a)
+{
+ return(_alpm_archive_fgets(buf, size, (struct archive*)a));
+}
+
+
+int _alpm_parse_desc(ugets_t ugets, void *from, pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%NAME%", ©_line, &pkg->name},
+ {"%VERSION%", ©_line, &pkg->version},
+ {"%FILENAME%", ©_line, &pkg->filename},
+ {"%DESC%", ©_line, &pkg->desc},
+ {"%GROUPS%", ©_to_list, &pkg->groups},
+ {"%URL%", ©_line, &pkg->url},
+ {"%LICENSE%", ©_to_list, &pkg->licenses},
+ {"%ARCH%", ©_line, &pkg->arch},
+ {"%BUILDDATE%", &parse_date, &pkg->builddate},
+ {"%INSTALLDATE%", &parse_date, &pkg->installdate},
+ {"%PACKAGER%", ©_line, &pkg->packager},
+ {"%REASON%", &parse_reason, &pkg->reason},
+ {"%SIZE%", &parse_off_t, &pkg->size},
+ {"%CSIZE%", &parse_off_t, &pkg->size},
+ {"%ISIZE%", &parse_off_t, &pkg->isize},
+ {"%MD5SUM%", ©_line, &pkg->md5sum},
+ {"%REPLACES%", ©_to_list, &pkg->replaces},
+ {"%FORCE%", &set_flag, &pkg->force}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+int _alpm_parse_files(ugets_t ugets, void* from,
+ pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%FILES%", ©_to_list, &pkg->files},
+ {"%BACKUP%", ©_to_list, &pkg->backup}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+int _alpm_parse_depends(char* (*ugets)(char*, int, void*), void *from,
+ pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%DEPENDS%", &deps_to_list, &pkg->depends},
+ {"%OPTDEPENDS%", ©_to_list, &pkg->optdepends},
+ {"%CONFLICTS%", ©_to_list, &pkg->conflicts},
+ {"%PROVIDES%", ©_to_list, &pkg->provides}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+int _alpm_parse_deltas(ugets_t ugets, void *from, pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%DELTAS%", &deltas_to_list, &pkg->deltas}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libalpm/parse.h b/lib/libalpm/parse.h
new file mode 100644
index 0000000..d3ef77e
--- /dev/null
+++ b/lib/libalpm/parse.h
@@ -0,0 +1,40 @@
+/*
+ * parse.h
+ *
+ * Copyright (c) 2006-2009 Pacman Development Team <pacman-dev(a)archlinux.org>
+ * Copyright (c) 2002-2006 by Judd Vinet <jvinet(a)zeroflux.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef _ALPM_PARSE_H
+#define _ALPM_PARSE_H
+
+#include "package.h"
+
+/* An fgets like function, argument for the parse functions */
+typedef char *(*ugets_t)(char*, int, void*);
+
+/* arguments for the parse functions (ugets_t) */
+char *_alpm_parse_from_file(char*, int, void*);
+char *_alpm_parse_from_archive(char*, int, void*);
+
+/* parse functions */
+int _alpm_parse_desc(ugets_t, void*, pmpkg_t*);
+int _alpm_parse_files(ugets_t, void*, pmpkg_t*);
+int _alpm_parse_depends(ugets_t, void*, pmpkg_t*);
+int _alpm_parse_deltas(ugets_t, void*, pmpkg_t*);
+
+#endif /* _ALPM_PARSE_H */
+
+/* vim: set ts=2 sw=2 noet: */
--
1.6.4.4
2
4
Split the huge list of else ifs which was used for parsing 'desc',
'depends', 'files' and 'deltas' files, into separate functions and move
them to a new file parse.c . This makes it possible for other backends
to share the same parse code. Also change the parsing from the giant
else if list to a single loop calling different functions as specified
in an array of parser structs (see parse_lines() for details).
Move _alpm_delta_parse to parse.c and make it static.
Signed-off-by: Henning Garus <henning.garus(a)gmail.com>
---
Fix a stupid memleak I introduced in be_files.c by copying pkgname and
version.
Change all functions in parse to follow code conventions.
lib/libalpm/Makefile.am | 1 +
lib/libalpm/be_files.c | 223 +++++-----------------------------
lib/libalpm/delta.c | 56 ---------
lib/libalpm/delta.h | 1 -
lib/libalpm/parse.c | 311 +++++++++++++++++++++++++++++++++++++++++++++++
lib/libalpm/parse.h | 40 ++++++
6 files changed, 384 insertions(+), 248 deletions(-)
create mode 100644 lib/libalpm/parse.c
create mode 100644 lib/libalpm/parse.h
diff --git a/lib/libalpm/Makefile.am b/lib/libalpm/Makefile.am
index 871855e..5ed8866 100644
--- a/lib/libalpm/Makefile.am
+++ b/lib/libalpm/Makefile.am
@@ -27,6 +27,7 @@ libalpm_la_SOURCES = \
backup.h backup.c \
be_files.c \
be_package.c \
+ parse.h parse.c \
cache.h cache.c \
conflict.h conflict.c \
db.h db.c \
diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index 21533ef..d969609 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -31,7 +31,6 @@
#include <ctype.h>
#include <time.h>
#include <limits.h> /* PATH_MAX */
-#include <locale.h> /* setlocale */
/* libalpm */
#include "db.h"
@@ -42,9 +41,9 @@
#include "alpm.h"
#include "handle.h"
#include "package.h"
-#include "delta.h"
#include "deps.h"
#include "dload.h"
+#include "parse.h"
/*
@@ -386,7 +385,8 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
{
FILE *fp = NULL;
char path[PATH_MAX];
- char line[513];
+ char *pkgname = NULL;
+ char *pkgver = NULL;
char *pkgpath = NULL;
ALPM_LOG_FUNC;
@@ -417,9 +417,6 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
_alpm_log(PM_LOG_FUNCTION, "loading package data for %s : level=0x%x\n",
info->name, inforeq);
- /* clear out 'line', to be certain - and to make valgrind happy */
- memset(line, 0, 513);
-
pkgpath = get_pkgpath(db, info);
if(access(pkgpath, F_OK)) {
@@ -429,6 +426,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
goto error;
}
+
/* DESC */
if(inforeq & INFRQ_DESC) {
snprintf(path, PATH_MAX, "%sdesc", pkgpath);
@@ -436,139 +434,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)) {
- if(fgets(line, 256, fp) == NULL) {
- break;
- }
- _alpm_strtrim(line);
- if(strcmp(line, "%NAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- if(strcmp(_alpm_strtrim(line), info->name) != 0) {
- _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name "
- "mismatch on package %s\n"), db->treename, info->name);
- }
- } else if(strcmp(line, "%VERSION%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- if(strcmp(_alpm_strtrim(line), info->version) != 0) {
- _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version "
- "mismatch on package %s\n"), db->treename, info->name);
- }
- } else if(strcmp(line, "%FILENAME%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->filename, _alpm_strtrim(line), goto error);
- } else if(strcmp(line, "%DESC%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->desc, _alpm_strtrim(line), 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);
- }
- } else if(strcmp(line, "%URL%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->url, _alpm_strtrim(line), 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);
- }
- } else if(strcmp(line, "%ARCH%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->arch, _alpm_strtrim(line), 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 in case 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);
- }
- } 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 in case 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);
- }
- } else if(strcmp(line, "%PACKAGER%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- STRDUP(info->packager, _alpm_strtrim(line), goto error);
- } else if(strcmp(line, "%REASON%") == 0) {
- if(fgets(line, 512, fp) == NULL) {
- goto error;
- }
- info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
- } 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));
- /* 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));
- } 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);
- } 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);
- }
- } else if(strcmp(line, "%FORCE%") == 0) {
- info->force = 1;
- }
+ /* name and version are already set and could be in use */
+ pkgname = info->name;
+ pkgver = info->version;
+ if(_alpm_parse_desc(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
+ if(strcmp(pkgname,info->name) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: name mismatch "
+ "on package %s\n"), db->treename, pkgname);
+ }
+ if(strcmp(pkgver,info->version) != 0) {
+ _alpm_log(PM_LOG_ERROR, _("%s database is inconsistent: version mismatch "
+ "on package %s\n"), db->treename, pkgver);
+ }
+ free(info->name);
+ info->name = pkgname;
+ free(info->version);
+ info->version = pkgver;
+ /* store size in isize if isize is unset */
+ if(info->isize == 0) {
+ info->isize = info->size;
+ }
+
fclose(fp);
fp = NULL;
}
@@ -580,21 +468,8 @@ 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);
- }
- } 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_parse_files(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
fclose(fp);
fp = NULL;
@@ -607,33 +482,8 @@ 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);
- }
- } 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);
- }
- } 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);
- }
- } 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_parse_depends(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
fclose(fp);
fp = NULL;
@@ -643,17 +493,8 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
if(inforeq & INFRQ_DELTAS) {
snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
if((fp = fopen(path, "r"))) {
- while(!feof(fp)) {
- fgets(line, 255, fp);
- _alpm_strtrim(line);
- if(strcmp(line, "%DELTAS%") == 0) {
- while(fgets(line, 512, fp) && strlen(_alpm_strtrim(line))) {
- pmdelta_t *delta = _alpm_delta_parse(line);
- if(delta) {
- info->deltas = alpm_list_add(info->deltas, delta);
- }
- }
- }
+ if(_alpm_parse_deltas(&_alpm_parse_from_file, fp, info) != 0) {
+ goto error;
}
fclose(fp);
fp = NULL;
diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c
index 523968e..5202e4b 100644
--- a/lib/libalpm/delta.c
+++ b/lib/libalpm/delta.c
@@ -25,7 +25,6 @@
#include <stdint.h> /* intmax_t */
#include <limits.h>
#include <sys/types.h>
-#include <regex.h>
/* libalpm */
#include "delta.h"
@@ -247,61 +246,6 @@ off_t _alpm_shortest_delta_path(alpm_list_t *deltas,
return(bestsize);
}
-/** Parses the string representation of a pmdelta_t object.
- * This function assumes that the string is in the correct format.
- * This format is as follows:
- * $deltafile $deltamd5 $deltasize $oldfile $newfile
- * @param line the string to parse
- * @return A pointer to the new pmdelta_t object
- */
-/* TODO this does not really belong here, but in a parsing lib */
-pmdelta_t *_alpm_delta_parse(char *line)
-{
- pmdelta_t *delta;
- char *tmp = line, *tmp2;
- regex_t reg;
-
- regcomp(®,
- "^[^[:space:]]* [[:xdigit:]]{32} [[:digit:]]*"
- " [^[:space:]]* [^[:space:]]*$",
- REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
- if(regexec(®, line, 0, 0, 0) != 0) {
- /* delta line is invalid, return NULL */
- regfree(®);
- return(NULL);
- }
- regfree(®);
-
- CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- delta->delta_size = atol(tmp2);
-
- tmp2 = tmp;
- tmp = strchr(tmp, ' ');
- *(tmp++) = '\0';
- STRDUP(delta->from, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- tmp2 = tmp;
- STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- _alpm_log(PM_LOG_DEBUG, "delta : %s %s '%lld'\n", delta->from, delta->to, (long long)delta->delta_size);
-
- return(delta);
-}
-
void _alpm_delta_free(pmdelta_t *delta)
{
FREE(delta->from);
diff --git a/lib/libalpm/delta.h b/lib/libalpm/delta.h
index 4f426cb..3c4e4cf 100644
--- a/lib/libalpm/delta.h
+++ b/lib/libalpm/delta.h
@@ -39,7 +39,6 @@ struct __pmdelta_t {
off_t download_size;
};
-pmdelta_t *_alpm_delta_parse(char *line);
void _alpm_delta_free(pmdelta_t *delta);
off_t _alpm_shortest_delta_path(alpm_list_t *deltas,
const char *to, alpm_list_t **path);
diff --git a/lib/libalpm/parse.c b/lib/libalpm/parse.c
new file mode 100644
index 0000000..de699bd
--- /dev/null
+++ b/lib/libalpm/parse.c
@@ -0,0 +1,311 @@
+/*
+ * parse.c
+ *
+ * Copyright (c) 2006-2009 Pacman Development Team <pacman-dev(a)archlinux.org>
+ * Copyright (c) 2002-2006 by Judd Vinet <jvinet(a)zeroflux.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <locale.h> /* setlocale */
+#include <regex.h>
+#include <time.h>
+
+/* libalpm */
+#include "parse.h"
+#include "util.h"
+#include "alpm_list.h"
+#include "log.h"
+#include "package.h"
+#include "delta.h"
+#include "deps.h"
+
+enum {
+ PARSE_FINISHED = 0,
+ PARSE_UNFINISHED = 1,
+ PARSE_ERROR = 2
+};
+
+typedef struct parser {
+ char *keyword;
+ int (*func)(char*, void*);
+ void *saveto;
+} parser_t;
+
+/** Parses the string representation of a pmdelta_t object.
+ * This function assumes that the string is in the correct format.
+ * This format is as follows:
+ * $deltafile $deltamd5 $deltasize $oldfile $newfile
+ * @param line the string to parse
+ * @return A pointer to the new pmdelta_t object
+ */
+static pmdelta_t *delta_parse(char *line)
+{
+ pmdelta_t *delta;
+ char *tmp = line, *tmp2;
+ regex_t reg;
+
+ regcomp(®,
+ "^[^[:space:]]* [[:xdigit:]]{32} [[:digit:]]*"
+ " [^[:space:]]* [^[:space:]]*$",
+ REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
+ if(regexec(®, line, 0, 0, 0) != 0) {
+ /* delta line is invalid, return NULL */
+ regfree(®);
+ return(NULL);
+ }
+ regfree(®);
+
+ CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ delta->delta_size = atol(tmp2);
+
+ tmp2 = tmp;
+ tmp = strchr(tmp, ' ');
+ *(tmp++) = '\0';
+ STRDUP(delta->from, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ tmp2 = tmp;
+ STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+
+ _alpm_log(PM_LOG_DEBUG, "delta : %s %s '%lld'\n", delta->from, delta->to, (long long)delta->delta_size);
+
+ return(delta);
+}
+
+/** Parse line based data, as found in desc, depends, files and deltas files
+ * The data is expected to be in the following format:
+ * keyword
+ * [entry]
+ * [entry]
+ * ...
+ * <newline>
+ * [keyword]
+ * ...
+ *
+ * The handling of the data for each keyword is described by the matching
+ * struct of type parser_t. This struct holds a function pointer and a void
+ * pointer for each keyword. The function is called for the lines following a
+ * keyword until it returns PARSE_FINISHED. It is expected to handle the actual
+ * parsing and store the data at the passed void pointer.
+ * @param parsers describes how to parse data for each keyword
+ * @param nparsers size of parsers
+ * @param ugets pointer to an fgets like function, which gets called to get
+ * the next line
+ * @param from describes from where to read the next line, this is passed to
+ * ugets
+ */
+static int parse_lines(parser_t parsers[], int nparsers, ugets_t ugets,
+ void *from)
+{
+ int i, ret;
+ char line[513];
+ parser_t *p = NULL;
+
+ while((*ugets)(line, sizeof(line) - 1, from) != NULL) {
+ _alpm_strtrim(line);
+ if(p == NULL && strlen(line) == 0) {
+ continue;
+ }
+ if(p == NULL) {
+ for(i = 0; i < nparsers; i++) {
+ if(strcmp(line,parsers[i].keyword) == 0) {
+ p = &parsers[i];
+ }
+ }
+ }
+ else {
+ ret = (*(p->func))(line, p->saveto);
+ if(ret == PARSE_FINISHED) {
+ p = NULL;
+ }
+ else if(ret == PARSE_ERROR) {
+ return(ret);
+ }
+ }
+ }
+ return(0);
+}
+
+static int copy_line(char *line, void *target)
+{
+ STRDUP(*(char**)target, line, return(PARSE_ERROR));
+ return(PARSE_FINISHED);
+}
+
+static int parse_off_t(char *line, void *target)
+{
+ *(off_t*)target = (off_t)atol(line);
+ return(PARSE_FINISHED);
+}
+
+static int parse_reason(char *line, void *target)
+{
+ *(pmpkgreason_t*)target = (pmpkgreason_t)atol(line);
+ return(PARSE_FINISHED);
+}
+
+static int parse_date(char *line, void *target)
+{
+ char first = tolower(line[0]);
+ if(first > 'a' && first < 'z') {
+ struct tm tmp_tm = {0}; /* initialize to null in case of failure */
+ setlocale(LC_TIME, "C");
+ strptime(line, "%a %b %e %H:%M:%S %Y", &tmp_tm);
+ *(time_t*)target = mktime(&tmp_tm);
+ setlocale(LC_TIME, "");
+ } else {
+ *(time_t*)target = atol(line);
+ }
+ return(PARSE_FINISHED);
+}
+
+static int set_flag(char *line, void *target)
+{
+ *(unsigned short*)target = 1;
+ return(PARSE_FINISHED);
+}
+
+static int copy_to_list(char *line, void *list)
+{
+ if(strlen(line) == 0) {
+ return(PARSE_FINISHED);
+ }
+ char *linedup;
+ STRDUP(linedup, line, RET_ERR(PM_ERR_MEMORY, PARSE_ERROR));
+ *(alpm_list_t**)list = alpm_list_add(*(alpm_list_t**)list, linedup);
+ return(PARSE_UNFINISHED);
+}
+
+static int deltas_to_list(char *line, void *list)
+{
+ if(strlen(line) == 0) {
+ return(PARSE_FINISHED);
+ }
+ pmdelta_t *delta = delta_parse(line);
+ if(delta != NULL) {
+ *(alpm_list_t**)list = alpm_list_add(*(alpm_list_t**)list, delta);
+ }
+ return(PARSE_UNFINISHED);
+}
+
+static int deps_to_list(char *line, void *list)
+{
+ if(strlen(line) == 0) {
+ return(PARSE_FINISHED);
+ }
+ pmdepend_t *dep = _alpm_splitdep(line);
+ *(alpm_list_t**)list = alpm_list_add(*(alpm_list_t**)list, dep);
+ return(PARSE_UNFINISHED);
+}
+
+char *_alpm_parse_from_file(char *buf,int size, void* fp)
+{
+ return(fgets(buf, size, (FILE*)fp));
+}
+
+char *_alpm_parse_from_archive(char *buf,int size, void* a)
+{
+ return(_alpm_archive_fgets(buf, size, (struct archive*)a));
+}
+
+
+int _alpm_parse_desc(ugets_t ugets, void *from, pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%NAME%", ©_line, &pkg->name},
+ {"%VERSION%", ©_line, &pkg->version},
+ {"%FILENAME%", ©_line, &pkg->filename},
+ {"%DESC%", ©_line, &pkg->desc},
+ {"%GROUPS%", ©_to_list, &pkg->groups},
+ {"%URL%", ©_line, &pkg->url},
+ {"%LICENSE%", ©_to_list, &pkg->licenses},
+ {"%ARCH%", ©_line, &pkg->arch},
+ {"%BUILDDATE%", &parse_date, &pkg->builddate},
+ {"%INSTALLDATE%", &parse_date, &pkg->installdate},
+ {"%PACKAGER%", ©_line, &pkg->packager},
+ {"%REASON%", &parse_reason, &pkg->reason},
+ {"%SIZE%", &parse_off_t, &pkg->size},
+ {"%CSIZE%", &parse_off_t, &pkg->size},
+ {"%ISIZE%", &parse_off_t, &pkg->isize},
+ {"%MD5SUM%", ©_line, &pkg->md5sum},
+ {"%REPLACES%", ©_to_list, &pkg->replaces},
+ {"%FORCE%", &set_flag, &pkg->force}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+int _alpm_parse_files(ugets_t ugets, void* from, pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%FILES%", ©_to_list, &pkg->files},
+ {"%BACKUP%", ©_to_list, &pkg->backup}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+int _alpm_parse_depends(ugets_t ugets, void *from, pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%DEPENDS%", &deps_to_list, &pkg->depends},
+ {"%OPTDEPENDS%", ©_to_list, &pkg->optdepends},
+ {"%CONFLICTS%", ©_to_list, &pkg->conflicts},
+ {"%PROVIDES%", ©_to_list, &pkg->provides}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+int _alpm_parse_deltas(ugets_t ugets, void *from, pmpkg_t *pkg)
+{
+ ALPM_LOG_FUNC;
+
+ parser_t parsers[] = {
+ {"%DELTAS%", &deltas_to_list, &pkg->deltas}
+ };
+ int nparsers = sizeof(parsers) / sizeof(parser_t);
+
+ return(parse_lines(parsers, nparsers, ugets, from));
+}
+
+/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libalpm/parse.h b/lib/libalpm/parse.h
new file mode 100644
index 0000000..d3ef77e
--- /dev/null
+++ b/lib/libalpm/parse.h
@@ -0,0 +1,40 @@
+/*
+ * parse.h
+ *
+ * Copyright (c) 2006-2009 Pacman Development Team <pacman-dev(a)archlinux.org>
+ * Copyright (c) 2002-2006 by Judd Vinet <jvinet(a)zeroflux.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef _ALPM_PARSE_H
+#define _ALPM_PARSE_H
+
+#include "package.h"
+
+/* An fgets like function, argument for the parse functions */
+typedef char *(*ugets_t)(char*, int, void*);
+
+/* arguments for the parse functions (ugets_t) */
+char *_alpm_parse_from_file(char*, int, void*);
+char *_alpm_parse_from_archive(char*, int, void*);
+
+/* parse functions */
+int _alpm_parse_desc(ugets_t, void*, pmpkg_t*);
+int _alpm_parse_files(ugets_t, void*, pmpkg_t*);
+int _alpm_parse_depends(ugets_t, void*, pmpkg_t*);
+int _alpm_parse_deltas(ugets_t, void*, pmpkg_t*);
+
+#endif /* _ALPM_PARSE_H */
+
+/* vim: set ts=2 sw=2 noet: */
--
1.6.4.4
1
0