4 # Copyright 2004-2005 Colin Percival
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted providing that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
30 #### Usage function -- called from command-line handling code.
32 # Usage instructions. Options not listed:
33 # --debug -- don't filter output from utilities
34 # --no-stats -- don't show progress statistics while fetching files
37 usage: `basename $0` [options] command ... [path]
40 -d workdir -- Store working files in workdir
41 (default: /var/db/portsnap/)
42 -f conffile -- Read configuration options from conffile
43 (default: /etc/portsnap.conf)
44 -I -- Update INDEX only. (update command only)
45 -k KEY -- Trust an RSA key with SHA256 hash of KEY
46 -l descfile -- Merge the specified local describes file into the INDEX.
47 -p portsdir -- Location of uncompressed ports tree
48 (default: /usr/ports/)
49 -s server -- Server from which to fetch updates.
50 (default: portsnap.FreeBSD.org)
51 --interactive -- interactive: override auto-detection of calling process
52 (use this when calling portsnap from an interactive, non-
53 terminal application AND NEVER ELSE).
54 path -- Extract only parts of the tree starting with the given
55 string. (extract command only)
57 fetch -- Fetch a compressed snapshot of the ports tree,
58 or update an existing snapshot.
59 cron -- Sleep rand(3600) seconds, and then fetch updates.
60 extract -- Extract snapshot of ports tree, replacing existing
61 files and directories.
62 update -- Update ports tree to match current snapshot, replacing
63 files and directories which have changed.
68 #### Parameter handling functions.
70 # Initialize parameters to null, just in case they're
71 # set in the environment.
93 # Parse the command line
95 while [ $# -gt 0 ]; do
98 if [ $# -eq 1 ]; then usage; fi
99 if [ ! -z "${WORKDIR}" ]; then usage; fi
103 QUIETREDIR="/dev/stderr"
104 STATSREDIR="/dev/stderr"
114 if [ $# -eq 1 ]; then usage; fi
115 if [ ! -z "${CONFFILE}" ]; then usage; fi
125 if [ $# -eq 1 ]; then usage; fi
126 if [ ! -z "${KEYPRINT}" ]; then usage; fi
130 if [ $# -eq 1 ]; then usage; fi
131 if [ ! -z "${LOCALDESC}" ]; then usage; fi
132 shift; LOCALDESC="$1"
135 if [ -z "${STATSREDIR}" ]; then
136 STATSREDIR="/dev/null"
141 if [ $# -eq 1 ]; then usage; fi
142 if [ ! -z "${PORTSDIR}" ]; then usage; fi
146 if [ $# -eq 1 ]; then usage; fi
147 if [ ! -z "${SERVERNAME}" ]; then usage; fi
148 shift; SERVERNAME="$1"
150 cron | extract | fetch | update | alfred)
151 COMMANDS="${COMMANDS} $1"
154 COMMANDS="${COMMANDS} update"
157 if [ $# -gt 1 ]; then usage; fi
158 if echo ${COMMANDS} | grep -vq extract; then
167 if [ -z "${COMMANDS}" ]; then
172 # If CONFFILE was specified at the command-line, make
173 # sure that it exists and is readable.
175 if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
176 echo -n "File does not exist "
177 echo -n "or is not readable: "
183 # If a configuration file hasn't been specified, use
184 # the default value (/etc/portsnap.conf)
186 if [ -z "${CONFFILE}" ]; then
187 CONFFILE="/etc/portsnap.conf"
191 # Read {KEYPRINT, SERVERNAME, WORKDIR, PORTSDIR} from the configuration
192 # file if they haven't already been set. If the configuration
193 # file doesn't exist, do nothing.
194 # Also read REFUSE (which cannot be set via the command line) if it is
195 # present in the configuration file.
197 if [ -r "${CONFFILE}" ]; then
198 for X in KEYPRINT WORKDIR PORTSDIR SERVERNAME; do
200 if [ -z "${_}" ]; then
201 eval ${X}=`grep "^${X}=" "${CONFFILE}" |
202 cut -f 2- -d '=' | tail -1`
206 if grep -qE "^REFUSE[[:space:]]" ${CONFFILE}; then
208 grep -E "^REFUSE[[:space:]]" "${CONFFILE}" |
209 cut -c 7- | xargs echo | tr ' ' '|'
213 if grep -qE "^INDEX[[:space:]]" ${CONFFILE}; then
215 grep -E "^INDEX[[:space:]]" "${CONFFILE}" |
216 cut -c 7- | tr ' ' '|' | xargs echo`"
221 # If parameters have not been set, use default values
223 _QUIETREDIR="/dev/null"
225 _STATSREDIR="/dev/stdout"
226 _WORKDIR="/var/db/portsnap"
227 _PORTSDIR="/usr/ports"
229 _LOCALDESC="/dev/null"
230 for X in QUIETREDIR QUIETFLAG STATSREDIR WORKDIR PORTSDIR \
234 if [ -z "${_}" ]; then
238 if [ -z "${INTERACTIVE}" ]; then
247 # Perform sanity checks and set some final parameters
248 # in preparation for fetching files. Also chdir into
249 # the working directory.
250 fetch_check_params() {
251 export HTTP_USER_AGENT="portsnap (${COMMAND}, `uname -r`)"
254 "SERVERNAME must be given via command line or configuration file."
255 _KEYPRINT_z="Key must be given via -k option or configuration file."
256 _KEYPRINT_bad="Invalid key fingerprint: "
257 _WORKDIR_bad="Directory does not exist or is not writable: "
259 if [ -z "${SERVERNAME}" ]; then
260 echo -n "`basename $0`: "
261 echo "${_SERVERNAME_z}"
264 if [ -z "${KEYPRINT}" ]; then
265 echo -n "`basename $0`: "
266 echo "${_KEYPRINT_z}"
269 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
270 echo -n "`basename $0`: "
271 echo -n "${_KEYPRINT_bad}"
275 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
276 echo -n "`basename $0`: "
277 echo -n "${_WORKDIR_bad}"
281 cd ${WORKDIR} || exit 1
283 BSPATCH=/usr/bin/bspatch
285 PHTTPGET=/usr/libexec/phttpget
288 # Perform sanity checks and set some final parameters
289 # in preparation for extracting or updating ${PORTSDIR}
290 # Complain if ${PORTSDIR} exists but is not writable,
291 # but don't complain if ${PORTSDIR} doesn't exist.
292 extract_check_params() {
293 _WORKDIR_bad="Directory does not exist: "
294 _PORTSDIR_bad="Directory is not writable: "
296 if ! [ -d "${WORKDIR}" ]; then
297 echo -n "`basename $0`: "
298 echo -n "${_WORKDIR_bad}"
302 if [ -d "${PORTSDIR}" ] && ! [ -w "${PORTSDIR}" ]; then
303 echo -n "`basename $0`: "
304 echo -n "${_PORTSDIR_bad}"
309 if ! [ -d "${WORKDIR}/files" -a -r "${WORKDIR}/tag" \
310 -a -r "${WORKDIR}/INDEX" -a -r "${WORKDIR}/tINDEX" ]; then
311 echo "No snapshot available. Try running"
312 echo "# `basename $0` fetch"
316 MKINDEX=/usr/libexec/make_index
319 # Perform sanity checks and set some final parameters
320 # in preparation for updating ${PORTSDIR}
321 update_check_params() {
324 if ! [ -r ${PORTSDIR}/.portsnap.INDEX ]; then
325 echo "${PORTSDIR} was not created by portsnap."
326 echo -n "You must run '`basename $0` extract' before "
327 echo "running '`basename $0` update'."
333 #### Core functionality -- the actual work gets done here
335 # Use an SRV query to pick a server. If the SRV query doesn't provide
336 # a useful answer, use the server name specified by the user.
337 # Put another way... look up _http._tcp.${SERVERNAME} and pick a server
338 # from that; or if no servers are returned, use ${SERVERNAME}.
339 # This allows a user to specify "portsnap.freebsd.org" (in which case
340 # portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
341 # (in which case portsnap will use that particular server, since there
342 # won't be an SRV entry for that name).
344 # We ignore the Port field, since we are always going to use port 80.
346 # Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
347 # no mirrors are available for any reason.
348 fetch_pick_server_init() {
351 # Check that host(1) exists (i.e., that the system wasn't built with the
352 # WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
353 if ! which -s host; then
358 echo -n "Looking up ${SERVERNAME} mirrors... "
360 # Issue the SRV query and pull out the Priority, Weight, and Target fields.
361 # BIND 9 prints "$name has SRV record ..." while BIND 8 prints
362 # "$name server selection ..."; we allow either format.
363 MLIST="_http._tcp.${SERVERNAME}"
364 host -t srv "${MLIST}" |
365 sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
366 cut -f 1,2,4 -d ' ' |
368 sort > serverlist_full
370 # If no records, give up -- we'll just use the server name we were given.
371 if [ `wc -l < serverlist_full` -eq 0 ]; then
376 # Report how many mirrors we found.
377 echo `wc -l < serverlist_full` "mirrors found."
379 # Generate a random seed for use in picking mirrors. If HTTP_PROXY
380 # is set, this will be used to generate the seed; otherwise, the seed
382 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
383 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
387 RANDVALUE=`jot -r 1 0 999999999`
391 # Pick a mirror. Returns 1 if we have run out of mirrors to try.
392 fetch_pick_server() {
393 # Generate a list of not-yet-tried mirrors
394 sort serverlist_tried |
395 comm -23 serverlist_full - > serverlist
397 # Have we run out of mirrors?
398 if [ `wc -l < serverlist` -eq 0 ]; then
399 echo "No mirrors remaining, giving up."
403 # Find the highest priority level (lowest numeric value).
404 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
406 # Add up the weights of the response lines at that priority level.
411 SRV_W=`echo $X | cut -f 2 -d ' '`
412 SRV_WSUM=$(($SRV_WSUM + $SRV_W))
417 # If all the weights are 0, pretend that they are all 1 instead.
418 if [ ${SRV_WSUM} -eq 0 ]; then
419 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
425 # Pick a value between 0 and the sum of the weights - 1
426 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
428 # Read through the list of mirrors and set SERVERNAME. Write the line
429 # corresponding to the mirror we selected into serverlist_tried so that
430 # we won't try it again.
434 SRV_W=`echo $X | cut -f 2 -d ' '`
435 SRV_W=$(($SRV_W + $SRV_W_ADD))
436 if [ $SRV_RND -lt $SRV_W ]; then
437 SERVERNAME=`echo $X | cut -f 3 -d ' '`
438 echo "$X" >> serverlist_tried
441 SRV_RND=$(($SRV_RND - $SRV_W))
448 # Check that we have a public key with an appropriate hash, or
449 # fetch the key if it doesn't exist. Returns 1 if the key has
450 # not yet been fetched.
452 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
456 echo -n "Fetching public key from ${SERVERNAME}... "
458 fetch ${QUIETFLAG} http://${SERVERNAME}/pub.ssl \
459 2>${QUIETREDIR} || true
460 if ! [ -r pub.ssl ]; then
464 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
465 echo "key has incorrect hash."
472 # Fetch a snapshot tag
474 rm -f snapshot.ssl tag.new
476 echo ${NDEBUG} "Fetching snapshot tag from ${SERVERNAME}... "
477 fetch ${QUIETFLAG} http://${SERVERNAME}/$1.ssl \
478 2>${QUIETREDIR} || true
479 if ! [ -r $1.ssl ]; then
484 openssl rsautl -pubin -inkey pub.ssl -verify \
485 < $1.ssl > tag.new 2>${QUIETREDIR} || true
488 if ! [ `wc -l < tag.new` = 1 ] ||
489 ! grep -qE "^portsnap\|[0-9]{10}\|[0-9a-f]{64}" tag.new; then
490 echo "invalid snapshot tag."
496 SNAPSHOTDATE=`cut -f 2 -d '|' < tag.new`
497 SNAPSHOTHASH=`cut -f 3 -d '|' < tag.new`
500 # Sanity-check the date on a snapshot tag
501 fetch_snapshot_tagsanity() {
502 if [ `date "+%s"` -gt `expr ${SNAPSHOTDATE} + 31536000` ]; then
503 echo "Snapshot appears to be more than a year old!"
504 echo "(Is the system clock correct?)"
505 echo "Cowardly refusing to proceed any further."
508 if [ `date "+%s"` -lt `expr ${SNAPSHOTDATE} - 86400` ]; then
509 echo -n "Snapshot appears to have been created more than "
510 echo "one day into the future!"
511 echo "(Is the system clock correct?)"
512 echo "Cowardly refusing to proceed any further."
517 # Sanity-check the date on a snapshot update tag
518 fetch_update_tagsanity() {
519 fetch_snapshot_tagsanity || return 1
521 if [ ${OLDSNAPSHOTDATE} -gt ${SNAPSHOTDATE} ]; then
522 echo -n "Latest snapshot on server is "
523 echo "older than what we already have!"
524 echo -n "Cowardly refusing to downgrade from "
525 date -r ${OLDSNAPSHOTDATE}
526 echo "to `date -r ${SNAPSHOTDATE}`."
531 # Compare old and new tags; return 1 if update is unnecessary
532 fetch_update_neededp() {
533 if [ ${OLDSNAPSHOTDATE} -eq ${SNAPSHOTDATE} ]; then
534 echo -n "Latest snapshot on server matches "
535 echo "what we already have."
536 echo "No updates needed."
540 if [ ${OLDSNAPSHOTHASH} = ${SNAPSHOTHASH} ]; then
541 echo -n "Ports tree hasn't changed since "
542 echo "last snapshot."
543 echo "No updates needed."
551 # Fetch snapshot metadata file
553 rm -f ${SNAPSHOTHASH} tINDEX.new
555 echo ${NDEBUG} "Fetching snapshot metadata... "
556 fetch ${QUIETFLAG} http://${SERVERNAME}/t/${SNAPSHOTHASH} \
557 2>${QUIETREDIR} || return
558 if [ "`${SHA256} -q ${SNAPSHOTHASH}`" != ${SNAPSHOTHASH} ]; then
559 echo "snapshot metadata corrupt."
562 mv ${SNAPSHOTHASH} tINDEX.new
566 # Warn user about bogus metadata
567 fetch_metadata_freakout() {
569 echo "Portsnap metadata is correctly signed, but contains"
570 echo "at least one line which appears bogus."
571 echo "Cowardly refusing to proceed any further."
574 # Sanity-check a snapshot metadata file
575 fetch_metadata_sanity() {
576 if grep -qvE "^[0-9A-Z.]+\|[0-9a-f]{64}$" tINDEX.new; then
577 fetch_metadata_freakout
580 if [ `look INDEX tINDEX.new | wc -l` != 1 ]; then
582 echo "Portsnap metadata appears bogus."
583 echo "Cowardly refusing to proceed any further."
588 # Take a list of ${oldhash}|${newhash} and output a list of needed patches
589 fetch_make_patchlist() {
591 echo "" 1>${QUIETREDIR}
592 grep -vE "^([0-9a-f]{64})\|\1$" |
594 printf "Processing: $X $Y ...\r" 1>${QUIETREDIR}
595 if [ -f "files/${Y}.gz" -o ! -f "files/${X}.gz" ]; then continue; fi
598 echo "" 1>${QUIETREDIR}
601 # Print user-friendly progress statistics
606 if [ $(($LNC % 10)) = 0 ]; then
608 elif [ $(($LNC % 2)) = 0 ]; then
618 printf "($1/$2) %02.2f%% " `echo "scale=4;$LNC / $TOTAL * 100"|bc`
621 fetch_progress_percent() {
627 if [ $(($LNC % 100)) = 0 ]; then
629 elif [ $(($LNC % 10)) = 0 ]; then
637 # Sanity-check an index file
638 fetch_index_sanity() {
639 if grep -qvE "^[-_+./@0-9A-Za-z]+\|[0-9a-f]{64}$" INDEX.new ||
640 fgrep -q "./" INDEX.new; then
641 fetch_metadata_freakout
646 # Verify a list of files
647 fetch_snapshot_verify() {
649 if [ "`gunzip -c snap/${F} | ${SHA256} -q`" != ${F} ]; then
650 echo "snapshot corrupt."
657 # Fetch a snapshot tarball, extract, and verify.
659 while ! fetch_tag snapshot; do
660 fetch_pick_server || return 1
662 fetch_snapshot_tagsanity || return 1
663 fetch_metadata || return 1
664 fetch_metadata_sanity || return 1
668 # Don't ask fetch(1) to be quiet -- downloading a snapshot of ~ 35MB will
669 # probably take a while, so the progrees reports that fetch(1) generates
670 # will be useful for keeping the users' attention from drifting.
671 echo "Fetching snapshot generated at `date -r ${SNAPSHOTDATE}`:"
672 fetch -r http://${SERVERNAME}/s/${SNAPSHOTHASH}.tgz || return 1
674 echo -n "Extracting snapshot... "
675 tar -xz --numeric-owner -f ${SNAPSHOTHASH}.tgz snap/ || return 1
676 rm ${SNAPSHOTHASH}.tgz
679 echo -n "Verifying snapshot integrity... "
680 # Verify the metadata files
681 cut -f 2 -d '|' tINDEX.new | fetch_snapshot_verify || return 1
684 gunzip -c snap/`look INDEX tINDEX.new |
685 cut -f 2 -d '|'`.gz > INDEX.new
686 fetch_index_sanity || return 1
687 # Verify the snapshot contents
688 cut -f 2 -d '|' INDEX.new | fetch_snapshot_verify || return 1
691 # Move files into their proper locations
692 rm -f tag INDEX tINDEX
702 # Update a compressed snapshot
704 rm -f patchlist diff OLD NEW filelist INDEX.new
706 OLDSNAPSHOTDATE=`cut -f 2 -d '|' < tag`
707 OLDSNAPSHOTHASH=`cut -f 3 -d '|' < tag`
709 while ! fetch_tag latest; do
710 fetch_pick_server || return 1
712 fetch_update_tagsanity || return 1
713 fetch_update_neededp || return 0
714 fetch_metadata || return 1
715 fetch_metadata_sanity || return 1
717 echo -n "Updating from `date -r ${OLDSNAPSHOTDATE}` "
718 echo "to `date -r ${SNAPSHOTDATE}`."
720 # Generate a list of wanted metadata patches
721 join -t '|' -o 1.2,2.2 tINDEX tINDEX.new |
722 fetch_make_patchlist > patchlist
724 # Attempt to fetch metadata patches
725 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
726 echo ${NDEBUG} "metadata patches.${DDSTATS}"
727 tr '|' '-' < patchlist |
728 lam -s "tp/" - -s ".gz" |
729 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
730 2>${STATSREDIR} | fetch_progress
733 # Attempt to apply metadata patches
734 echo -n "Applying metadata patches... "
735 local oldifs="$IFS" IFS='|'
737 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
738 gunzip -c < ${X}-${Y}.gz > diff
739 gunzip -c < files/${X}.gz > OLD
740 cut -c 2- diff | join -t '|' -v 2 - OLD > ptmp
741 grep '^\+' diff | cut -c 2- |
742 sort -k 1,1 -t '|' -m - ptmp > NEW
743 if [ `${SHA256} -q NEW` = ${Y} ]; then
747 rm -f diff OLD NEW ${X}-${Y}.gz ptmp
748 done < patchlist 2>${QUIETREDIR}
752 # Update metadata without patches
753 join -t '|' -v 2 tINDEX tINDEX.new |
754 cut -f 2 -d '|' /dev/stdin patchlist |
756 if [ ! -f "files/${Y}.gz" ]; then
760 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
761 echo ${NDEBUG} "metadata files... "
762 lam -s "f/" - -s ".gz" < filelist |
763 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
767 echo -n "Verifying ${Y}... " 1>${QUIETREDIR}
768 if [ `gunzip -c < ${Y}.gz | ${SHA256} -q` = ${Y} ]; then
769 mv ${Y}.gz files/${Y}.gz
771 echo "metadata is corrupt."
774 echo "ok." 1>${QUIETREDIR}
779 echo -n "Extracting index... " 1>${QUIETREDIR}
780 gunzip -c files/`look INDEX tINDEX.new |
781 cut -f 2 -d '|'`.gz > INDEX.new
782 fetch_index_sanity || return 1
784 # If we have decided to refuse certain updates, construct a hybrid index which
785 # is equal to the old index for parts of the tree which we don't want to
786 # update, and equal to the new index for parts of the tree which gets updates.
787 # This means that we should always have a "complete snapshot" of the ports
788 # tree -- with the caveat that it isn't actually a snapshot.
789 if [ ! -z "${REFUSE}" ]; then
790 echo "Refusing to download updates for ${REFUSE}" \
793 grep -Ev "${REFUSE}" INDEX.new > INDEX.tmp
794 grep -E "${REFUSE}" INDEX |
795 sort -m -k 1,1 -t '|' - INDEX.tmp > INDEX.new
799 # Generate a list of wanted ports patches
800 echo -n "Generating list of wanted patches..." 1>${QUIETREDIR}
801 join -t '|' -o 1.2,2.2 INDEX INDEX.new |
802 fetch_make_patchlist > patchlist
803 echo " done." 1>${QUIETREDIR}
805 # Attempt to fetch ports patches
806 patchcnt=`wc -l < patchlist | tr -d ' '`
807 echo -n "Fetching $patchcnt "
808 echo ${NDEBUG} "patches.${DDSTATS}"
810 tr '|' '-' < patchlist | lam -s "bp/" - |
811 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
812 2>${STATSREDIR} | fetch_progress_percent $patchcnt
815 # Attempt to apply ports patches
816 PATCHCNT=`wc -l patchlist`
817 echo "Applying patches... "
818 local oldifs="$IFS" IFS='|'
823 if [ ! -f "${F}" ]; then
824 printf " Skipping ${F} (${I} of ${PATCHCNT}).\r"
827 echo " Processing ${F}..." 1>${QUIETREDIR}
828 gunzip -c < files/${X}.gz > OLD
829 ${BSPATCH} OLD NEW ${X}-${Y}
830 if [ `${SHA256} -q NEW` = ${Y} ]; then
834 rm -f diff OLD NEW ${X}-${Y}
835 done < patchlist 2>${QUIETREDIR}
839 # Update ports without patches
840 join -t '|' -v 2 INDEX INDEX.new |
841 cut -f 2 -d '|' /dev/stdin patchlist |
843 if [ ! -f "files/${Y}.gz" ]; then
847 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
848 echo ${NDEBUG} "new ports or files... "
849 lam -s "f/" - -s ".gz" < filelist |
850 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
856 printf " Processing ${Y} (${I} of ${PATCHCNT}).\r" 1>${QUIETREDIR}
857 if [ `gunzip -c < ${Y}.gz | ${SHA256} -q` = ${Y} ]; then
858 mv ${Y}.gz files/${Y}.gz
860 echo "snapshot is corrupt."
866 # Remove files which are no longer needed
867 cut -f 2 -d '|' tINDEX INDEX | sort -u > oldfiles
868 cut -f 2 -d '|' tINDEX.new INDEX.new | sort -u | comm -13 - oldfiles |
869 lam -s "files/" - -s ".gz" | xargs rm -f
870 rm patchlist filelist oldfiles
880 # Do the actual work involved in "fetch" / "cron".
882 fetch_pick_server_init && fetch_pick_server
884 while ! fetch_key; do
885 fetch_pick_server || return 1
888 if ! [ -d files -a -r tag -a -r INDEX -a -r tINDEX ]; then
889 fetch_snapshot || return 1
891 fetch_update || return 1
894 # Build a ports INDEX file
895 extract_make_index() {
896 if ! look $1 ${WORKDIR}/tINDEX > /dev/null; then
897 echo -n "$1 not provided by portsnap server; "
898 echo "$2 not being generated."
900 gunzip -c "${WORKDIR}/files/`look $1 ${WORKDIR}/tINDEX |
901 cut -f 2 -d '|'`.gz" |
903 ${MKINDEX} /dev/stdin > ${PORTSDIR}/$2
907 # Create INDEX, INDEX-5, INDEX-6
909 echo -n "Building new INDEX files... "
910 for PAIR in ${INDEXPAIRS}; do
911 INDEXFILE=`echo ${PAIR} | cut -f 1 -d '|'`
912 DESCRIBEFILE=`echo ${PAIR} | cut -f 2 -d '|'`
913 extract_make_index ${DESCRIBEFILE} ${INDEXFILE} || return 1
918 # Create .portsnap.INDEX; if we are REFUSEing to touch certain directories,
919 # merge the values from any exiting .portsnap.INDEX file.
921 if [ -z "${REFUSE}" ]; then
922 sort ${WORKDIR}/INDEX > ${PORTSDIR}/.portsnap.INDEX
923 elif [ -f ${PORTSDIR}/.portsnap.INDEX ]; then
924 grep -E "${REFUSE}" ${PORTSDIR}/.portsnap.INDEX \
925 > ${PORTSDIR}/.portsnap.INDEX.tmp
926 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort |
927 sort -m - ${PORTSDIR}/.portsnap.INDEX.tmp \
928 > ${PORTSDIR}/.portsnap.INDEX
929 rm -f ${PORTSDIR}/.portsnap.INDEX.tmp
931 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort \
932 > ${PORTSDIR}/.portsnap.INDEX
936 # Do the actual work involved in "extract"
938 local oldifs="$IFS" IFS='|'
939 mkdir -p ${PORTSDIR} || return 1
942 if ! [ -z "${EXTRACTPATH}" ]; then
943 grep "^${EXTRACTPATH}" ${WORKDIR}/INDEX
944 elif ! [ -z "${REFUSE}" ]; then
945 grep -vE "${REFUSE}" ${WORKDIR}/INDEX
948 fi | while read FILE HASH; do
949 echo ${PORTSDIR}/${FILE}
950 if ! [ -r "${WORKDIR}/files/${HASH}.gz" ]; then
951 echo "files/${HASH}.gz not found -- snapshot corrupt."
956 rm -rf ${PORTSDIR}/${FILE%/}
957 mkdir -p ${PORTSDIR}/${FILE}
958 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
959 -C ${PORTSDIR}/${FILE}
962 rm -f ${PORTSDIR}/${FILE}
963 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
964 -C ${PORTSDIR} ${FILE}
970 if [ ! -z "${EXTRACTPATH}" ]; then
980 update_run_extract() {
984 echo "Extracting new files:"
986 if ! [ -z "${REFUSE}" ]; then
987 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort
989 sort ${WORKDIR}/INDEX
991 comm -13 ${PORTSDIR}/.portsnap.INDEX - |
992 while read FILE HASH; do
993 echo ${PORTSDIR}/${FILE}
994 if ! [ -r "${WORKDIR}/files/${HASH}.gz" ]; then
995 echo "files/${HASH}.gz not found -- snapshot corrupt."
1000 mkdir -p ${PORTSDIR}/${FILE}
1001 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
1002 -C ${PORTSDIR}/${FILE}
1005 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
1006 -C ${PORTSDIR} ${FILE}
1014 # Do the actual work involved in "update"
1016 if ! [ -z "${INDEXONLY}" ]; then
1017 extract_indices >/dev/null || return 1
1021 if sort ${WORKDIR}/INDEX |
1022 cmp -s ${PORTSDIR}/.portsnap.INDEX -; then
1023 echo "Ports tree is already up to date."
1027 # If we are REFUSEing to touch certain directories, don't remove files
1028 # from those directories (even if they are out of date)
1029 echo -n "Removing old files and directories... "
1030 if ! [ -z "${REFUSE}" ]; then
1031 sort ${WORKDIR}/INDEX |
1032 comm -23 ${PORTSDIR}/.portsnap.INDEX - | cut -f 1 -d '|' |
1033 grep -vE "${REFUSE}" |
1034 lam -s "${PORTSDIR}/" - |
1035 sed -e 's|/$||' | xargs rm -rf
1037 sort ${WORKDIR}/INDEX |
1038 comm -23 ${PORTSDIR}/.portsnap.INDEX - | cut -f 1 -d '|' |
1039 lam -s "${PORTSDIR}/" - |
1040 sed -e 's|/$||' | xargs rm -rf
1044 update_run_extract || return 1
1049 #### Main functions -- call parameter-handling and core functions
1051 # Using the command line, configuration file, and defaults,
1052 # set all the parameters which are needed later.
1062 # Fetch command. Make sure that we're being called
1063 # interactively, then run fetch_check_params and fetch_run
1065 if [ "${INTERACTIVE}" != "YES" ]; then
1066 echo -n "`basename $0` fetch should not "
1067 echo "be run non-interactively."
1068 echo "Run `basename $0` cron instead"
1075 # Cron command. Make sure the parameters are sensible; wait
1076 # rand(3600) seconds; then fetch updates. While fetching updates,
1077 # send output to a temporary file; only print that file if the
1081 sleep `jot -r 1 0 3600`
1083 TMPFILE=`mktemp /tmp/portsnap.XXXXXX` || exit 1
1084 if ! fetch_run >> ${TMPFILE}; then
1093 # Extract command. Make sure the parameters are sensible,
1094 # then extract the ports tree (or part thereof).
1096 extract_check_params
1097 extract_run || exit 1
1100 # Update command. Make sure the parameters are sensible,
1101 # then update the ports tree.
1104 update_run || exit 1
1107 # Alfred command. Run 'fetch' or 'cron' depending on
1108 # whether stdin is a terminal; then run 'update' or
1109 # 'extract' depending on whether ${PORTSDIR} exists.
1111 if [ "${INTERACTIVE}" = "YES" ]; then
1116 if [ -r ${PORTSDIR}/.portsnap.INDEX ]; then
1125 # Make sure we find utilities from the base system
1126 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
1128 # Set LC_ALL in order to avoid problems with character ranges like [A-Z].
1132 for COMMAND in ${COMMANDS}; do