[pacman-dev] Long functions and other minor fixes

James Rosten seinfeld90 at gmail.com
Fri Dec 29 15:59:10 EST 2006


This is my massacre of _alpm_load_pkg in package.c (just joking about
the massacre).  I moved the extremely large for loop into its own
function (and made it a while loop).  Feel free to change the name of
_alpm_pkg_checker (first thing that came to mind).

The patches for package.c and package.h are below.

~ Jamie / yankees26
-------------- next part --------------
Index: package.c
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/package.c,v
retrieving revision 1.42
diff -u -r1.42 package.c
--- package.c	22 Nov 2006 09:03:42 -0000	1.42
+++ package.c	29 Dec 2006 20:47:18 -0000
@@ -275,98 +275,72 @@
 	return(0);
 }
 
-pmpkg_t *_alpm_pkg_load(char *pkgfile)
+/* function for _alpm_pkg_load */
+int _alpm_pkg_checker(archive *archive, 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);
+			fd = mktemp(fn);
+			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,72 @@
 			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;
+}
+
+pmpkg_t *_alpm_pkg_load(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;
 
-error:
-	FREEPKG(info);
-	archive_read_finish (archive);
+	if(pkgfile == NULL || strlen(pkgfile) == 0) {
+		RET_ERR(PM_ERR_WRONG_ARGS, NULL);
+	}
 
-	return(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);
 }
 
-------------- next part --------------
Index: package.h
===================================================================
RCS file: /home/cvs-pacman/pacman-lib/lib/libalpm/package.h,v
retrieving revision 1.22
diff -u -r1.22 package.h
--- package.h	22 Nov 2006 09:03:42 -0000	1.22
+++ package.h	29 Dec 2006 20:51:03 -0000
@@ -93,6 +93,7 @@
 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);


More information about the pacman-dev mailing list