[pacman-dev] Long functions and other minor fixes

James Rosten seinfeld90 at gmail.com
Fri Dec 29 19:32:21 EST 2006


After trying to build pacman3 so I could test something for stonecrest,
I found some errors in my patch and Dan's (very small, you forgot to put
the label error before return(-1);) while compiling.

So below is the better patch than the other patch (which should be
ignored).  It patches package.c, package.h, and sync.c of libalpm.

~ Jamie / yankees26
-------------- next part --------------
Index: lib/libalpm/package.c
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/package.c,v
retrieving revision 1.42
diff -u -r1.42 package.c
--- lib/libalpm/package.c	22 Nov 2006 09:03:42 -0000	1.42
+++ lib/libalpm/package.c	30 Dec 2006 00:28:48 -0000
@@ -275,98 +275,72 @@
 	return(0);
 }
 
-pmpkg_t *_alpm_pkg_load(char *pkgfile)
+/* function for _alpm_pkg_load */
+int _alpm_pkg_checker(struct archive *archive, struct archive_entry *entry, pmpkg_t *info, char *pkgfile)
 {
 	char *expath;
-	int i;
 	int config = 0;
 	int filelist = 0;
 	int scriptcheck = 0;
-	register struct archive *archive;
-	struct archive_entry *entry;
-	pmpkg_t *info = NULL;
-
-	if(pkgfile == NULL || strlen(pkgfile) == 0) {
-		RET_ERR(PM_ERR_WRONG_ARGS, NULL);
-	}
-
-	if ((archive = archive_read_new ()) == NULL)
-		RET_ERR(PM_ERR_LIBARCHIVE_ERROR, NULL);
-
-	archive_read_support_compression_all (archive);
-	archive_read_support_format_all (archive);
-
-	if (archive_read_open_file (archive, pkgfile, ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK)
-		RET_ERR(PM_ERR_PKG_OPEN, NULL);
 
-	info = _alpm_pkg_new(NULL, NULL);
-	if(info == NULL) {
-		archive_read_finish (archive);
-		RET_ERR(PM_ERR_MEMORY, NULL);
-	}
-
-	for(i = 0; archive_read_next_header (archive, &entry) == ARCHIVE_OK; i++) {
-		if(config && filelist && scriptcheck) {
+	while ( archive_read_next_header(archive, &entry) == ARCHIVE_OK ) {
+		if ( config && filelist && scriptcheck ) {
 			/* we have everything we need */
 			break;
-		}
-		if(!strcmp(archive_entry_pathname (entry), ".PKGINFO")) {
+		} if ( !strcmp(archive_entry_pathname(entry), ".PKGINFO") ) {
 			char *descfile;
 			int fd;
 
 			/* extract this file into /tmp. it has info for us */
 			descfile = strdup("/tmp/alpm_XXXXXX");
 			fd = mkstemp(descfile);
-			archive_read_data_into_fd (archive, fd);
+			archive_read_data_into_fd(archive, fd);
 			/* parse the info file */
-			if(parse_descfile(descfile, info, 0) == -1) {
+			if ( parse_descfile(descfile, info, 0) == -1 ) {
 				_alpm_log(PM_LOG_ERROR, _("could not parse the package description file"));
 				pm_errno = PM_ERR_PKG_INVALID;
 				unlink(descfile);
 				FREE(descfile);
 				close(fd);
-				goto error;
-			}
-			if(!strlen(info->name)) {
+				return -1;
+			} if ( !strlen(info->name) ) {
 				_alpm_log(PM_LOG_ERROR, _("missing package name in %s"), pkgfile);
 				pm_errno = PM_ERR_PKG_INVALID;
 				unlink(descfile);
 				FREE(descfile);
 				close(fd);
-				goto error;
-			}
-			if(!strlen(info->version)) {
+				return -1;
+			} if ( !strlen(info->version) ) {
 				_alpm_log(PM_LOG_ERROR, _("missing package version in %s"), pkgfile);
 				pm_errno = PM_ERR_PKG_INVALID;
 				unlink(descfile);
 				FREE(descfile);
 				close(fd);
-				goto error;
+				return -1;
 			}
 			config = 1;
 			unlink(descfile);
 			FREE(descfile);
 			close(fd);
 			continue;
-		} else if(!strcmp(archive_entry_pathname (entry), "._install") || !strcmp(archive_entry_pathname (entry),  ".INSTALL")) {
+		} else if ( !strcmp(archive_entry_pathname(entry), "._install") || !strcmp(archive_entry_pathname(entry), ".INSTALL") ) {
 			info->scriptlet = 1;
 			scriptcheck = 1;
-		} else if(!strcmp(archive_entry_pathname (entry), ".FILELIST")) {
-			/* Build info->files from the filelist */
+		} else if ( !strcmp(archive_entry_pathname(entry), ".FILELIST") ) {
 			FILE *fp;
 			char *fn;
 			char *str;
 			int fd;
-			
-			if((str = (char *)malloc(PATH_MAX)) == NULL) {
-				RET_ERR(PM_ERR_MEMORY, (pmpkg_t *)-1);
+
+			if ( (str = (char *)malloc(PATH_MAX)) == NULL ) {
+				return -2;
 			}
 			fn = strdup("/tmp/alpm_XXXXXX");
 			fd = mkstemp(fn);
-			archive_read_data_into_fd (archive,fd);
+			archive_read_data_into_fd(archive, fd);
 			fp = fopen(fn, "r");
-			while(!feof(fp)) {
-				if(fgets(str, PATH_MAX, fp) == NULL) {
+			while ( !feof(fp) ) {
+				if ( fgets(str, PATH_MAX, fp) == NULL ) {
 					continue;
 				}
 				_alpm_strtrim(str);
@@ -374,7 +348,7 @@
 			}
 			FREE(str);
 			fclose(fp);
-			if(unlink(fn)) {
+			if ( unlink(fn) ) {
 				_alpm_log(PM_LOG_WARNING, _("could not remove tempfile %s"), fn);
 			}
 			FREE(fn);
@@ -383,39 +357,68 @@
 			continue;
 		} else {
 			scriptcheck = 1;
-			if(!filelist) {
-				/* no .FILELIST present in this package..  build the filelist the */
-				/* old-fashioned way, one at a time */
-				expath = strdup(archive_entry_pathname (entry));
+			if ( !filelist ) {
+				/* no .FILELIST present in this package.. build the filelist the
+				 * old-fashioned way, one at a time  */
+				expath = strdup(archive_entry_pathname(entry));
 				info->files = _alpm_list_add(info->files, expath);
 			}
 		}
 
-		if(archive_read_data_skip (archive)) {
+		if ( archive_read_data_skip(archive) ) {
 			_alpm_log(PM_LOG_ERROR, _("bad package file in %s"), pkgfile);
-			goto error;
+			return -1;
 		}
 		expath = NULL;
 	}
-	archive_read_finish (archive);
-
-	if(!config) {
+	if ( !config ) {
 		_alpm_log(PM_LOG_ERROR, _("missing package info file in %s"), pkgfile);
-		goto error;
+		return -1;
 	}
-
-	/* internal */
+	archive_read_finish(archive);
 	info->origin = PKG_FROM_FILE;
 	info->data = strdup(pkgfile);
 	info->infolevel = 0xFF;
 
-	return(info);
+	return 0;
+}
 
-error:
-	FREEPKG(info);
-	archive_read_finish (archive);
+pmpkg_t *_alpm_pkg_load(char *pkgfile)
+{
+	int i = 0;
+	register struct archive *archive;
+	struct archive_entry *entry = NULL;
+	pmpkg_t *info = NULL;
 
-	return(NULL);
+	if(pkgfile == NULL || strlen(pkgfile) == 0) {
+		RET_ERR(PM_ERR_WRONG_ARGS, NULL);
+	}
+
+	if ((archive = archive_read_new ()) == NULL)
+		RET_ERR(PM_ERR_LIBARCHIVE_ERROR, NULL);
+
+	archive_read_support_compression_gzip (archive);
+	archive_read_support_format_tar (archive);
+
+	if (archive_read_open_file (archive, pkgfile, ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK)
+		RET_ERR(PM_ERR_PKG_OPEN, NULL);
+
+	info = _alpm_pkg_new(NULL, NULL);
+	if(info == NULL) {
+		archive_read_finish (archive);
+		RET_ERR(PM_ERR_MEMORY, NULL);
+	}
+
+	i = _alpm_pkg_checker(archive, entry, info, pkgfile);
+	if ( i == -1 ) {
+		FREEPKG(info);
+		archive_read_finish(archive);
+		return(NULL);
+	} if ( i == -2 ) {
+		RET_ERR(PM_ERR_MEMORY, (pmpkg_t *)-1);
+	}
+
+	return(info);
 }
 
 /* Test for existence of a package in a pmlist_t*
Index: lib/libalpm/package.h
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/package.h,v
retrieving revision 1.22
diff -u -r1.22 package.h
--- lib/libalpm/package.h	22 Nov 2006 09:03:42 -0000	1.22
+++ lib/libalpm/package.h	30 Dec 2006 00:28:48 -0000
@@ -89,10 +89,14 @@
 #define FREEPKG(p) do { if(p){_alpm_pkg_free(p); p = NULL;}} while(0)
 #define FREELISTPKGS(p) _FREELIST(p, _alpm_pkg_free)
 
+typedef struct archive_entry archive_entry;
+typedef struct archive archive;
+
 pmpkg_t* _alpm_pkg_new(const char *name, const char *version);
 pmpkg_t *_alpm_pkg_dup(pmpkg_t *pkg);
 void _alpm_pkg_free(void *data);
 int _alpm_pkg_cmp(const void *p1, const void *p2);
+int _alpm_pkg_checker(archive *archive, archive_entry *entry, pmpkg_t *info, char *pkgfile);
 pmpkg_t *_alpm_pkg_load(char *pkgfile);
 pmpkg_t *_alpm_pkg_isin(char *needle, pmlist_t *haystack);
 int _alpm_pkg_splitname(char *target, char *name, char *version, int witharch);
Index: lib/libalpm/sync.c
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/sync.c,v
retrieving revision 1.87
diff -u -r1.87 sync.c
--- lib/libalpm/sync.c	29 Dec 2006 17:05:00 -0000	1.87
+++ lib/libalpm/sync.c	30 Dec 2006 00:28:49 -0000
@@ -266,6 +266,7 @@
 		return(0);
 	}
 	/* if we're here, it's an error */
+error:
 	return(-1);
 }
 


More information about the pacman-dev mailing list