xbps-repo: improve genindex target, reorganize main.c file.
- genindex: use new recently added function, remove some objects from generated pkg dictionaries, they will be fetched remotely. - moved some code from main.c into repository.c. --HG-- extra : convert_revision : xtraeme%40gmail.com-20091125043204-nbtdtaxkga3dad2h
This commit is contained in:
		| @@ -34,8 +34,6 @@ | ||||
| #include <xbps_api.h> | ||||
| #include "defs.h" | ||||
|  | ||||
| static bool sanitize_localpath(char *, const char *); | ||||
| static int  pkgindex_verify(const char *, const char *, bool); | ||||
| static void usage(void); | ||||
|  | ||||
| static void | ||||
| @@ -68,153 +66,12 @@ usage(void) | ||||
| 	exit(EXIT_FAILURE); | ||||
| } | ||||
|  | ||||
| static int | ||||
| pkgindex_verify(const char *plist, const char *uri, bool only_sync) | ||||
| { | ||||
| 	prop_dictionary_t d; | ||||
| 	const char *pkgidx_version; | ||||
| 	uint64_t total_pkgs; | ||||
| 	int rv = 0; | ||||
|  | ||||
| 	assert(plist != NULL); | ||||
|  | ||||
| 	d = prop_dictionary_internalize_from_file(plist); | ||||
| 	if (d == NULL) { | ||||
| 		printf("E: repository %s does not contain any " | ||||
| 		    "xbps pkgindex file.\n", uri); | ||||
| 		return errno; | ||||
| 	} | ||||
|  | ||||
| 	if (!prop_dictionary_get_cstring_nocopy(d, | ||||
| 	    "pkgindex-version", &pkgidx_version)) { | ||||
| 		printf("E: missing 'pkgindex-version' object!\n"); | ||||
| 		rv = errno; | ||||
| 		goto out; | ||||
| 	} | ||||
|  | ||||
| 	if (!prop_dictionary_get_uint64(d, "total-pkgs", &total_pkgs)) { | ||||
| 		printf("E: missing 'total-pkgs' object!\n"); | ||||
| 		rv = errno; | ||||
| 		goto out; | ||||
| 	} | ||||
|  | ||||
| 	/* Reject empty repositories, how could this happen? :-) */ | ||||
| 	if (total_pkgs == 0) { | ||||
| 		printf("E: empty package list!\n"); | ||||
| 		rv = EINVAL; | ||||
| 		goto out; | ||||
| 	} | ||||
|  | ||||
| 	printf("%s package index at %s (v%s) with %ju packages.\n", | ||||
| 	    only_sync ? "Updated" : "Added", uri, pkgidx_version, total_pkgs); | ||||
|  | ||||
| out: | ||||
| 	prop_object_release(d); | ||||
| 	if (rv != 0) { | ||||
| 		printf("W: removing incorrect pkg index file: '%s' ...\n", | ||||
| 		    plist); | ||||
| 		rv = remove(plist); | ||||
| 	} | ||||
| 	return rv; | ||||
| } | ||||
|  | ||||
| static bool | ||||
| sanitize_localpath(char *buf, const char *path) | ||||
| { | ||||
| 	char *dirnp, *basenp, *dir, *base, *tmp; | ||||
| 	bool rv = false; | ||||
|  | ||||
| 	dir = strdup(path); | ||||
| 	if (dir == NULL) | ||||
| 		return false; | ||||
|  | ||||
| 	base = strdup(path); | ||||
| 	if (base == NULL) { | ||||
| 		free(dir); | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	dirnp = dirname(dir); | ||||
| 	if (strcmp(dirnp, ".") == 0) | ||||
| 		goto out; | ||||
|  | ||||
| 	basenp = basename(base); | ||||
| 	if (strcmp(basenp, base) == 0) | ||||
| 		goto out; | ||||
|  | ||||
| 	tmp = strncpy(buf, dirnp, PATH_MAX - 1); | ||||
| 	if (sizeof(*tmp) >= PATH_MAX) | ||||
| 		goto out; | ||||
|  | ||||
| 	buf[strlen(buf) + 1] = '\0'; | ||||
| 	if (strcmp(dirnp, "/")) | ||||
| 		strncat(buf, "/", 1); | ||||
| 	strncat(buf, basenp, PATH_MAX - strlen(buf) - 1); | ||||
| 	rv = true; | ||||
|  | ||||
| out: | ||||
| 	free(dir); | ||||
| 	free(base); | ||||
|  | ||||
| 	return rv; | ||||
| } | ||||
|  | ||||
| static int | ||||
| add_repository(const char *uri) | ||||
| { | ||||
| 	char *plist, idxstr[PATH_MAX]; | ||||
| 	int rv = 0; | ||||
|  | ||||
| 	if (xbps_check_is_repo_string_remote(uri)) { | ||||
| 		if (!sanitize_localpath(idxstr, uri)) | ||||
| 			return errno; | ||||
|  | ||||
| 		printf("Fetching remote package index at %s...\n", uri); | ||||
| 		rv = xbps_sync_repository_pkg_index(idxstr); | ||||
| 		if (rv == -1) { | ||||
| 			printf("Error: could not fetch pkg index file: %s.\n", | ||||
| 			    xbps_fetch_error_string()); | ||||
| 			return rv; | ||||
| 		} else if (rv == 0) { | ||||
| 			printf("Package index file is already " | ||||
| 			    "up to date.\n"); | ||||
| 			return 0; | ||||
| 		} | ||||
|  | ||||
| 		plist = xbps_get_pkg_index_plist(idxstr); | ||||
| 	} else { | ||||
| 		if (!sanitize_localpath(idxstr, uri)) | ||||
| 			return errno; | ||||
|  | ||||
| 		plist = xbps_get_pkg_index_plist(idxstr); | ||||
| 	} | ||||
|  | ||||
| 	if (plist == NULL) | ||||
| 		return errno; | ||||
|  | ||||
| 	if ((rv = pkgindex_verify(plist, idxstr, false)) != 0) | ||||
| 		goto out; | ||||
|  | ||||
| 	if ((rv = xbps_register_repository(idxstr)) != 0) { | ||||
| 		printf("ERROR: couldn't register repository (%s)\n", | ||||
| 		    strerror(rv)); | ||||
| 		goto out; | ||||
| 	} | ||||
| 	 | ||||
| out: | ||||
| 	if (plist != NULL) | ||||
| 		free(plist); | ||||
|  | ||||
| 	return rv; | ||||
| } | ||||
|  | ||||
| int | ||||
| main(int argc, char **argv) | ||||
| { | ||||
| 	prop_dictionary_t pkgd; | ||||
| 	struct repository_data *rdata; | ||||
| 	const char *pkgver; | ||||
| 	char dpkgidx[PATH_MAX], *plist, *root; | ||||
| 	char *root; | ||||
| 	int c, rv = 0; | ||||
|  | ||||
| 	while ((c = getopt(argc, argv, "Vr:")) != -1) { | ||||
| @@ -252,7 +109,7 @@ main(int argc, char **argv) | ||||
| 		if (argc != 2) | ||||
| 			usage(); | ||||
|  | ||||
| 		rv = add_repository(argv[1]); | ||||
| 		rv = register_repository(argv[1]); | ||||
|  | ||||
| 	} else if (strcasecmp(argv[0], "list") == 0) { | ||||
| 		/* Lists all repositories registered in pool. */ | ||||
| @@ -268,19 +125,7 @@ main(int argc, char **argv) | ||||
| 		if (argc != 2) | ||||
| 			usage(); | ||||
|  | ||||
| 		if (!sanitize_localpath(dpkgidx, argv[1])) { | ||||
| 			rv = EINVAL; | ||||
| 			goto out; | ||||
| 		} | ||||
|  | ||||
| 		if ((rv = xbps_unregister_repository(dpkgidx)) != 0) { | ||||
| 			if (rv == ENOENT) | ||||
| 				printf("Repository '%s' not actually " | ||||
| 				    "registered.\n", dpkgidx); | ||||
| 			else | ||||
| 				printf("ERROR: couldn't unregister " | ||||
| 				    "repository (%s)\n", strerror(rv)); | ||||
| 		} | ||||
| 		rv = unregister_repository(argv[1]); | ||||
|  | ||||
| 	} else if (strcasecmp(argv[0], "search") == 0) { | ||||
| 		/* | ||||
| @@ -301,17 +146,7 @@ main(int argc, char **argv) | ||||
| 		if (argc != 2) | ||||
| 			usage(); | ||||
|  | ||||
| 		SIMPLEQ_FOREACH(rdata, &repodata_queue, chain) { | ||||
| 			pkgd = xbps_find_pkg_in_dict(rdata->rd_repod, | ||||
| 			    "packages", argv[1]); | ||||
| 			if (pkgd == NULL) { | ||||
| 				errno = ENOENT; | ||||
| 				continue; | ||||
| 			} | ||||
| 			printf("Repository: %s\n", rdata->rd_uri); | ||||
| 			show_pkg_info(pkgd); | ||||
| 			break; | ||||
| 		} | ||||
| 		rv = show_pkg_info_from_repolist(argv[1]); | ||||
| 		if (rv == 0 && errno == ENOENT) { | ||||
| 			printf("Unable to locate package '%s' from " | ||||
| 			    "repository pool.\n", argv[1]); | ||||
| @@ -324,23 +159,7 @@ main(int argc, char **argv) | ||||
| 		if (argc != 2) | ||||
| 			usage(); | ||||
|  | ||||
| 		SIMPLEQ_FOREACH(rdata, &repodata_queue, chain) { | ||||
| 			pkgd = xbps_find_pkg_in_dict(rdata->rd_repod, | ||||
| 			    "packages", argv[1]); | ||||
| 			if (pkgd == NULL) { | ||||
| 				errno = ENOENT; | ||||
| 				continue; | ||||
| 			} | ||||
| 			if (!prop_dictionary_get_cstring_nocopy(pkgd, | ||||
| 			    "version", &pkgver)) { | ||||
| 				rv = errno; | ||||
| 				goto out; | ||||
| 			} | ||||
| 			printf("Repository %s [pkgver: %s]\n", | ||||
| 			    rdata->rd_uri, pkgver); | ||||
| 			(void)xbps_callback_array_iter_in_dict(pkgd, | ||||
| 			    "run_depends", list_strings_sep_in_array, NULL); | ||||
| 		} | ||||
| 		rv = show_pkg_deps_from_repolist(argv[1]); | ||||
| 		if (rv == 0 && errno == ENOENT) { | ||||
| 			printf("Unable to locate package '%s' from " | ||||
| 			    "repository pool.\n", argv[1]); | ||||
| @@ -354,9 +173,9 @@ main(int argc, char **argv) | ||||
| 			usage(); | ||||
|  | ||||
| 		pkgd = xbps_get_pkg_plist_dict_from_repo(argv[1], | ||||
| 		    "./files.plist"); | ||||
| 		    XBPS_PKGFILES); | ||||
| 		if (pkgd == NULL) { | ||||
| 			printf("E: couldn't read files.plist: %s.\n", | ||||
| 			printf("E: couldn't read %s: %s.\n", XBPS_PKGFILES, | ||||
| 			    strerror(errno)); | ||||
| 			rv = errno; | ||||
| 			goto out; | ||||
| @@ -376,32 +195,7 @@ main(int argc, char **argv) | ||||
| 		if (argc != 1) | ||||
| 			usage(); | ||||
|  | ||||
| 		/* | ||||
| 		 * Iterate over repository pool. | ||||
| 		 */ | ||||
| 		SIMPLEQ_FOREACH(rdata, &repodata_queue, chain) { | ||||
| 			const char *uri = rdata->rd_uri; | ||||
| 			if (xbps_check_is_repo_string_remote(uri)) { | ||||
| 				printf("Syncing package index from: %s\n", uri); | ||||
| 				rv = xbps_sync_repository_pkg_index(uri); | ||||
| 				if (rv == -1) { | ||||
| 					printf("Failed! returned: %s\n", | ||||
| 					    xbps_fetch_error_string()); | ||||
| 					goto out; | ||||
| 				} else if (rv == 0) { | ||||
| 					printf("Package index file is already " | ||||
| 					    "up to date.\n"); | ||||
| 					continue; | ||||
| 				} | ||||
| 				plist = xbps_get_pkg_index_plist(uri); | ||||
| 				if (plist == NULL) { | ||||
| 					rv = EINVAL; | ||||
| 					goto out; | ||||
| 				} | ||||
| 				(void)pkgindex_verify(plist, uri, true); | ||||
| 				free(plist); | ||||
| 			} | ||||
| 		} | ||||
| 		rv = repository_sync(); | ||||
|  | ||||
| 	} else { | ||||
| 		usage(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user