[go: up one dir, main page]

Menu

[r4]: / trunk / http.repmod  Maximize  Restore  History

Download this file

101 lines (86 with data), 2.1 kB

#!/bin/sh

APK_CACHE=${APK_CACHE:-"$ROOT/var/cache/apk"}
PKGS_URL="$APK_PATH/Packages.sha1.gz"
PKGS_FILE=`beautify_path $APK_CACHE/Packages.sha1.gz`

# create the cache dir if it does not exist
mkdir -p $APK_CACHE

# update the repository
# find and install the Packages.sha1.gz
rep_update() {
	local pkg qflag
	if [ "$vflag" ] ; then 
		eecho "Downloading $PKGS_URL"
	else
		qflag="-q"
	fi
	
	rm -f $PKGS_FILE
	if ! wget $qflag -P $APK_CACHE $PKGS_URL ; then
		eecho "Failed downloading $PKGS_URLS" >&2
		rm -f $PKGS_FILE
		exit 1
	fi
}

# init repository. Check if the Packages file exist and download if not.
rep_init() {
	local size
	if [ -f "$PKGS_FILE" ] ; then
		# download if file size is zero
		if [ `ls -s $PKGS_FILE | awk '{print $1}'` -lt 1 ] ; then
			rep_update
		fi
	else
		rep_update
	fi
}

# Download the package list and return the complete package name available
rep_find_pkg() {
	local pkg
	pkg="$1"
	if ! zcat "$PKGS_FILE" | awk "/ $pkg-[0-9].*/ {print \$2}" | tail -n 1
	then
		eecho "$pkg not found in $PKGS_FILE"
		exit 1
	fi
}

# Return the path to the downloaded file
# Parameters: 
# 	$1 = package 
rep_fetch_pkg() {
	local rfile qflag oldpwd redir
	rfile=`rep_find_pkg $1`
	if [ -z "$rfile" ] ; then
		echo "$pkg not found in $APK_PATH" >&2
		exit 1
	fi

	# remove file if it exists but have wrong checksum
	if [ -f "$rfile" ] ; then 
		zcat $PKGS_FILE | grep $rfile | sha1sum -c >/dev/null 1>&2 || \
			rm -f "$rfile"
	fi

	# if -v is not set, add -q to wget to be quiet.
	if [ "$vflag" ] ; then
		eecho "Downloading $APK_PATH/$rfile"
	else
		[ -z "$nflag" ] && qflag="-q"
	fi

	# download file if it does not exist
	if ! [ -f "$rfile" ] ; then
		if ! wget $qflag -P $APK_CACHE $APK_PATH/$rfile >&2 ; then
			eecho "Failed fetching $APK_PATH/$rfile"
			exit 1
		fi
	fi
	
	# check agains checksum unless $APK_UNSAFE is set
	if [ -z "$APK_UNSAFE" ] ; then
		cd $APK_CACHE
		if ! zcat $PKGS_FILE | grep $rfile | sha1sum -c >/dev/null 1>&2; then
			eecho "Checksum failed for $1!"
			exit 1
		fi
		cd $oldpwd
	fi

	echo "$APK_CACHE/$rfile"
}

rep_cleanup() {
	[ -z "$KEEP_CACHE" ] ||	rm -f $APK_CACHE/*
}