[pacman-dev] [PATCH] Report local file URL for -Sp operations if package is in cache

Andrew Gregory andrew.gregory.8 at gmail.com
Fri Apr 1 13:33:02 UTC 2016


On 03/29/16 at 10:03pm, Allan McRae wrote:
> When using "pacman -Sp" operation to get URLs of packages to download, it is
> useful to know which packages are already in the file cache and do not need
> downloaded.  Print packages in the cache with a file:// prefix.
> 
> e.g
> $ pacman -Sp glibc
> file:///var/cache/pacman/glibc-2.23-1-x86_64.pkg.tar.xz
> 
> Fixes FS#15868
> 
> Signed-off-by: Allan McRae <allan at archlinux.org>
> ---
>  src/pacman/util.c | 26 +++++++++++++++++++++-----
>  1 file changed, 21 insertions(+), 5 deletions(-)
> 
> diff --git a/src/pacman/util.c b/src/pacman/util.c
> index d924bf3..5a5c0da 100644
> --- a/src/pacman/util.c
> +++ b/src/pacman/util.c
> @@ -1027,11 +1027,27 @@ static char *pkg_get_location(alpm_pkg_t *pkg)
>  	char *string = NULL;
>  	switch(config->op) {
>  		case PM_OP_SYNC:
> -			servers = alpm_db_get_servers(alpm_pkg_get_db(pkg));
> -			if(servers) {
> -				pm_asprintf(&string, "%s/%s", (char *)(servers->data),
> -						alpm_pkg_get_filename(pkg));
> -				return string;
> +			if(alpm_pkg_download_size(pkg) == 0) {
> +				/* file is already in the package cache */
> +				alpm_list_t *i;
> +				const char *pkgfile = alpm_pkg_get_filename(pkg);
> +				char path[PATH_MAX];
> +				struct stat buf;
> +
> +				for(i = alpm_option_get_cachedirs(config->handle); i; i = i->next) {
> +					snprintf(path, PATH_MAX, "%s%s", (char *)i->data, pkgfile);
> +					if(stat(path, &buf) == 0 && S_ISREG(buf.st_mode)) {
> +						pm_asprintf(&string, "file://%s", path);
> +						return string;
> +					}

We occasionally run into a problem with the fully downloaded package
still having the .part extension in the cache (FS#35789), in which
case this won't actually find the file and just the package filename
will be printed.  If the servers check is moved up out of the else
block, we can fall back to printing the url, which I think is the
correct action in that case.

> +				}
> +			} else {
> +				servers = alpm_db_get_servers(alpm_pkg_get_db(pkg));
> +				if(servers) {
> +					pm_asprintf(&string, "%s/%s", (char *)(servers->data),
> +							alpm_pkg_get_filename(pkg));
> +					return string;
> +				}
>  			}
>  		case PM_OP_UPGRADE:
>  			return strdup(alpm_pkg_get_filename(pkg));
> -- 
> 2.7.4

-- 
apg


More information about the pacman-dev mailing list