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.
64 auto -- Fetch updates, and either extract a new ports tree or
65 update an existing tree.
70 #### Parameter handling functions.
72 # Initialize parameters to null, just in case they're
73 # set in the environment.
95 # Parse the command line
97 while [ $# -gt 0 ]; do
100 if [ $# -eq 1 ]; then usage; fi
101 if [ ! -z "${WORKDIR}" ]; then usage; fi
105 QUIETREDIR="/dev/stderr"
106 STATSREDIR="/dev/stderr"
116 if [ $# -eq 1 ]; then usage; fi
117 if [ ! -z "${CONFFILE}" ]; then usage; fi
127 if [ $# -eq 1 ]; then usage; fi
128 if [ ! -z "${KEYPRINT}" ]; then usage; fi
132 if [ $# -eq 1 ]; then usage; fi
133 if [ ! -z "${LOCALDESC}" ]; then usage; fi
134 shift; LOCALDESC="$1"
137 if [ -z "${STATSREDIR}" ]; then
138 STATSREDIR="/dev/null"
143 if [ $# -eq 1 ]; then usage; fi
144 if [ ! -z "${PORTSDIR}" ]; then usage; fi
148 if [ $# -eq 1 ]; then usage; fi
149 if [ ! -z "${SERVERNAME}" ]; then usage; fi
150 shift; SERVERNAME="$1"
152 cron | extract | fetch | update | auto)
153 COMMANDS="${COMMANDS} $1"
156 COMMANDS="${COMMANDS} update"
159 COMMANDS="${COMMANDS} auto"
162 if [ $# -gt 1 ]; then usage; fi
163 if echo ${COMMANDS} | grep -vq extract; then
172 if [ -z "${COMMANDS}" ]; then
177 # If CONFFILE was specified at the command-line, make
178 # sure that it exists and is readable.
180 if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
181 echo -n "File does not exist "
182 echo -n "or is not readable: "
188 # If a configuration file hasn't been specified, use
189 # the default value (/etc/portsnap.conf)
191 if [ -z "${CONFFILE}" ]; then
192 CONFFILE="/etc/portsnap.conf"
196 # Read {KEYPRINT, SERVERNAME, WORKDIR, PORTSDIR} from the configuration
197 # file if they haven't already been set. If the configuration
198 # file doesn't exist, do nothing.
199 # Also read REFUSE (which cannot be set via the command line) if it is
200 # present in the configuration file.
202 if [ -r "${CONFFILE}" ]; then
203 for X in KEYPRINT WORKDIR PORTSDIR SERVERNAME; do
205 if [ -z "${_}" ]; then
206 eval ${X}=`grep "^${X}=" "${CONFFILE}" |
207 cut -f 2- -d '=' | tail -1`
211 if grep -qE "^REFUSE[[:space:]]" ${CONFFILE}; then
213 grep -E "^REFUSE[[:space:]]" "${CONFFILE}" |
214 cut -c 7- | xargs echo | tr ' ' '|'
218 if grep -qE "^INDEX[[:space:]]" ${CONFFILE}; then
220 grep -E "^INDEX[[:space:]]" "${CONFFILE}" |
221 cut -c 7- | tr ' ' '|' | xargs echo`"
226 # If parameters have not been set, use default values
228 _QUIETREDIR="/dev/null"
230 _STATSREDIR="/dev/stdout"
231 _WORKDIR="/var/db/portsnap"
232 _PORTSDIR="/usr/ports"
234 _LOCALDESC="/dev/null"
235 for X in QUIETREDIR QUIETFLAG STATSREDIR WORKDIR PORTSDIR \
239 if [ -z "${_}" ]; then
243 if [ -z "${INTERACTIVE}" ]; then
252 # Perform sanity checks and set some final parameters
253 # in preparation for fetching files. Also chdir into
254 # the working directory.
255 fetch_check_params() {
256 export HTTP_USER_AGENT="portsnap (${COMMAND}, `uname -r`)"
259 "SERVERNAME must be given via command line or configuration file."
260 _KEYPRINT_z="Key must be given via -k option or configuration file."
261 _KEYPRINT_bad="Invalid key fingerprint: "
262 _WORKDIR_bad="Directory does not exist or is not writable: "
264 if [ -z "${SERVERNAME}" ]; then
265 echo -n "`basename $0`: "
266 echo "${_SERVERNAME_z}"
269 if [ -z "${KEYPRINT}" ]; then
270 echo -n "`basename $0`: "
271 echo "${_KEYPRINT_z}"
274 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
275 echo -n "`basename $0`: "
276 echo -n "${_KEYPRINT_bad}"
280 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
281 echo -n "`basename $0`: "
282 echo -n "${_WORKDIR_bad}"
286 cd ${WORKDIR} || exit 1
288 BSPATCH=/usr/bin/bspatch
290 PHTTPGET=/usr/libexec/phttpget
293 # Perform sanity checks and set some final parameters
294 # in preparation for extracting or updating ${PORTSDIR}
295 # Complain if ${PORTSDIR} exists but is not writable,
296 # but don't complain if ${PORTSDIR} doesn't exist.
297 extract_check_params() {
298 _WORKDIR_bad="Directory does not exist: "
299 _PORTSDIR_bad="Directory is not writable: "
301 if ! [ -d "${WORKDIR}" ]; then
302 echo -n "`basename $0`: "
303 echo -n "${_WORKDIR_bad}"
307 if [ -d "${PORTSDIR}" ] && ! [ -w "${PORTSDIR}" ]; then
308 echo -n "`basename $0`: "
309 echo -n "${_PORTSDIR_bad}"
314 if ! [ -d "${WORKDIR}/files" -a -r "${WORKDIR}/tag" \
315 -a -r "${WORKDIR}/INDEX" -a -r "${WORKDIR}/tINDEX" ]; then
316 echo "No snapshot available. Try running"
317 echo "# `basename $0` fetch"
321 MKINDEX=/usr/libexec/make_index
324 # Perform sanity checks and set some final parameters
325 # in preparation for updating ${PORTSDIR}
326 update_check_params() {
329 if ! [ -r ${PORTSDIR}/.portsnap.INDEX ]; then
330 echo "${PORTSDIR} was not created by portsnap."
331 echo -n "You must run '`basename $0` extract' before "
332 echo "running '`basename $0` update'."
338 #### Core functionality -- the actual work gets done here
340 # Use an SRV query to pick a server. If the SRV query doesn't provide
341 # a useful answer, use the server name specified by the user.
342 # Put another way... look up _http._tcp.${SERVERNAME} and pick a server
343 # from that; or if no servers are returned, use ${SERVERNAME}.
344 # This allows a user to specify "portsnap.freebsd.org" (in which case
345 # portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
346 # (in which case portsnap will use that particular server, since there
347 # won't be an SRV entry for that name).
349 # We ignore the Port field, since we are always going to use port 80.
351 # Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
352 # no mirrors are available for any reason.
353 fetch_pick_server_init() {
356 # Check that host(1) exists (i.e., that the system wasn't built with the
357 # WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
358 if ! which -s host; then
363 echo -n "Looking up ${SERVERNAME} mirrors... "
365 # Issue the SRV query and pull out the Priority, Weight, and Target fields.
366 # BIND 9 prints "$name has SRV record ..." while BIND 8 prints
367 # "$name server selection ..."; we allow either format.
368 MLIST="_http._tcp.${SERVERNAME}"
369 host -t srv "${MLIST}" |
370 sed -nE "s/${MLIST} (has SRV record|server selection) //Ip" |
371 cut -f 1,2,4 -d ' ' |
373 sort > serverlist_full
375 # If no records, give up -- we'll just use the server name we were given.
376 if [ `wc -l < serverlist_full` -eq 0 ]; then
381 # Report how many mirrors we found.
382 echo `wc -l < serverlist_full` "mirrors found."
384 # Generate a random seed for use in picking mirrors. If HTTP_PROXY
385 # is set, this will be used to generate the seed; otherwise, the seed
387 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
388 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
392 RANDVALUE=`jot -r 1 0 999999999`
396 # Pick a mirror. Returns 1 if we have run out of mirrors to try.
397 fetch_pick_server() {
398 # Generate a list of not-yet-tried mirrors
399 sort serverlist_tried |
400 comm -23 serverlist_full - > serverlist
402 # Have we run out of mirrors?
403 if [ `wc -l < serverlist` -eq 0 ]; then
404 echo "No mirrors remaining, giving up."
408 # Find the highest priority level (lowest numeric value).
409 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
411 # Add up the weights of the response lines at that priority level.
416 SRV_W=`echo $X | cut -f 2 -d ' '`
417 SRV_WSUM=$(($SRV_WSUM + $SRV_W))
422 # If all the weights are 0, pretend that they are all 1 instead.
423 if [ ${SRV_WSUM} -eq 0 ]; then
424 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
430 # Pick a value between 0 and the sum of the weights - 1
431 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
433 # Read through the list of mirrors and set SERVERNAME. Write the line
434 # corresponding to the mirror we selected into serverlist_tried so that
435 # we won't try it again.
439 SRV_W=`echo $X | cut -f 2 -d ' '`
440 SRV_W=$(($SRV_W + $SRV_W_ADD))
441 if [ $SRV_RND -lt $SRV_W ]; then
442 SERVERNAME=`echo $X | cut -f 3 -d ' '`
443 echo "$X" >> serverlist_tried
446 SRV_RND=$(($SRV_RND - $SRV_W))
453 # Check that we have a public key with an appropriate hash, or
454 # fetch the key if it doesn't exist. Returns 1 if the key has
455 # not yet been fetched.
457 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
461 echo -n "Fetching public key from ${SERVERNAME}... "
463 fetch ${QUIETFLAG} http://${SERVERNAME}/pub.ssl \
464 2>${QUIETREDIR} || true
465 if ! [ -r pub.ssl ]; then
469 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
470 echo "key has incorrect hash."
477 # Fetch a snapshot tag
479 rm -f snapshot.ssl tag.new
481 echo ${NDEBUG} "Fetching snapshot tag from ${SERVERNAME}... "
482 fetch ${QUIETFLAG} http://${SERVERNAME}/$1.ssl \
483 2>${QUIETREDIR} || true
484 if ! [ -r $1.ssl ]; then
489 openssl rsautl -pubin -inkey pub.ssl -verify \
490 < $1.ssl > tag.new 2>${QUIETREDIR} || true
493 if ! [ `wc -l < tag.new` = 1 ] ||
494 ! grep -qE "^portsnap\|[0-9]{10}\|[0-9a-f]{64}" tag.new; then
495 echo "invalid snapshot tag."
501 SNAPSHOTDATE=`cut -f 2 -d '|' < tag.new`
502 SNAPSHOTHASH=`cut -f 3 -d '|' < tag.new`
505 # Sanity-check the date on a snapshot tag
506 fetch_snapshot_tagsanity() {
507 if [ `date "+%s"` -gt `expr ${SNAPSHOTDATE} + 31536000` ]; then
508 echo "Snapshot appears to be more than a year old!"
509 echo "(Is the system clock correct?)"
510 echo "Cowardly refusing to proceed any further."
513 if [ `date "+%s"` -lt `expr ${SNAPSHOTDATE} - 86400` ]; then
514 echo -n "Snapshot appears to have been created more than "
515 echo "one day into the future!"
516 echo "(Is the system clock correct?)"
517 echo "Cowardly refusing to proceed any further."
522 # Sanity-check the date on a snapshot update tag
523 fetch_update_tagsanity() {
524 fetch_snapshot_tagsanity || return 1
526 if [ ${OLDSNAPSHOTDATE} -gt ${SNAPSHOTDATE} ]; then
527 echo -n "Latest snapshot on server is "
528 echo "older than what we already have!"
529 echo -n "Cowardly refusing to downgrade from "
530 date -r ${OLDSNAPSHOTDATE}
531 echo "to `date -r ${SNAPSHOTDATE}`."
536 # Compare old and new tags; return 1 if update is unnecessary
537 fetch_update_neededp() {
538 if [ ${OLDSNAPSHOTDATE} -eq ${SNAPSHOTDATE} ]; then
539 echo -n "Latest snapshot on server matches "
540 echo "what we already have."
541 echo "No updates needed."
545 if [ ${OLDSNAPSHOTHASH} = ${SNAPSHOTHASH} ]; then
546 echo -n "Ports tree hasn't changed since "
547 echo "last snapshot."
548 echo "No updates needed."
556 # Fetch snapshot metadata file
558 rm -f ${SNAPSHOTHASH} tINDEX.new
560 echo ${NDEBUG} "Fetching snapshot metadata... "
561 fetch ${QUIETFLAG} http://${SERVERNAME}/t/${SNAPSHOTHASH} \
562 2>${QUIETREDIR} || return
563 if [ "`${SHA256} -q ${SNAPSHOTHASH}`" != ${SNAPSHOTHASH} ]; then
564 echo "snapshot metadata corrupt."
567 mv ${SNAPSHOTHASH} tINDEX.new
571 # Warn user about bogus metadata
572 fetch_metadata_freakout() {
574 echo "Portsnap metadata is correctly signed, but contains"
575 echo "at least one line which appears bogus."
576 echo "Cowardly refusing to proceed any further."
579 # Sanity-check a snapshot metadata file
580 fetch_metadata_sanity() {
581 if grep -qvE "^[0-9A-Z.]+\|[0-9a-f]{64}$" tINDEX.new; then
582 fetch_metadata_freakout
585 if [ `look INDEX tINDEX.new | wc -l` != 1 ]; then
587 echo "Portsnap metadata appears bogus."
588 echo "Cowardly refusing to proceed any further."
593 # Take a list of ${oldhash}|${newhash} and output a list of needed patches
594 fetch_make_patchlist() {
596 echo "" 1>${QUIETREDIR}
597 grep -vE "^([0-9a-f]{64})\|\1$" |
599 printf "Processing: $X $Y ...\r" 1>${QUIETREDIR}
600 if [ -f "files/${Y}.gz" -o ! -f "files/${X}.gz" ]; then continue; fi
603 echo "" 1>${QUIETREDIR}
606 # Print user-friendly progress statistics
611 if [ $(($LNC % 10)) = 0 ]; then
613 elif [ $(($LNC % 2)) = 0 ]; then
623 printf "($1/$2) %02.2f%% " `echo "scale=4;$LNC / $TOTAL * 100"|bc`
626 fetch_progress_percent() {
632 if [ $(($LNC % 100)) = 0 ]; then
634 elif [ $(($LNC % 10)) = 0 ]; then
642 # Sanity-check an index file
643 fetch_index_sanity() {
644 if grep -qvE "^[-_+./@0-9A-Za-z]+\|[0-9a-f]{64}$" INDEX.new ||
645 fgrep -q "./" INDEX.new; then
646 fetch_metadata_freakout
651 # Verify a list of files
652 fetch_snapshot_verify() {
654 if [ "`gunzip -c < snap/${F}.gz | ${SHA256} -q`" != ${F} ]; then
655 echo "snapshot corrupt."
662 # Fetch a snapshot tarball, extract, and verify.
664 while ! fetch_tag snapshot; do
665 fetch_pick_server || return 1
667 fetch_snapshot_tagsanity || return 1
668 fetch_metadata || return 1
669 fetch_metadata_sanity || return 1
673 # Don't ask fetch(1) to be quiet -- downloading a snapshot of ~ 35MB will
674 # probably take a while, so the progrees reports that fetch(1) generates
675 # will be useful for keeping the users' attention from drifting.
676 echo "Fetching snapshot generated at `date -r ${SNAPSHOTDATE}`:"
677 fetch -r http://${SERVERNAME}/s/${SNAPSHOTHASH}.tgz || return 1
679 echo -n "Extracting snapshot... "
680 tar -xz --numeric-owner -f ${SNAPSHOTHASH}.tgz snap/ || return 1
681 rm ${SNAPSHOTHASH}.tgz
684 echo -n "Verifying snapshot integrity... "
685 # Verify the metadata files
686 cut -f 2 -d '|' tINDEX.new | fetch_snapshot_verify || return 1
689 gunzip -c < snap/`look INDEX tINDEX.new |
690 cut -f 2 -d '|'`.gz > INDEX.new
691 fetch_index_sanity || return 1
692 # Verify the snapshot contents
693 cut -f 2 -d '|' INDEX.new | fetch_snapshot_verify || return 1
694 cut -f 2 -d '|' tINDEX.new INDEX.new | sort -u |
695 lam -s 'snap/' - -s '.gz' > files.expected
696 find snap -mindepth 1 | sort > files.snap
697 if ! cmp -s files.expected files.snap; then
698 echo "unexpected files in snapshot."
701 rm files.expected files.snap
704 # Move files into their proper locations
705 rm -f tag INDEX tINDEX
715 # Update a compressed snapshot
717 rm -f patchlist diff OLD NEW filelist INDEX.new
719 OLDSNAPSHOTDATE=`cut -f 2 -d '|' < tag`
720 OLDSNAPSHOTHASH=`cut -f 3 -d '|' < tag`
722 while ! fetch_tag latest; do
723 fetch_pick_server || return 1
725 fetch_update_tagsanity || return 1
726 fetch_update_neededp || return 0
727 fetch_metadata || return 1
728 fetch_metadata_sanity || return 1
730 echo -n "Updating from `date -r ${OLDSNAPSHOTDATE}` "
731 echo "to `date -r ${SNAPSHOTDATE}`."
733 # Generate a list of wanted metadata patches
734 join -t '|' -o 1.2,2.2 tINDEX tINDEX.new |
735 fetch_make_patchlist > patchlist
737 # Attempt to fetch metadata patches
738 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
739 echo ${NDEBUG} "metadata patches.${DDSTATS}"
740 tr '|' '-' < patchlist |
741 lam -s "tp/" - -s ".gz" |
742 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
743 2>${STATSREDIR} | fetch_progress
746 # Attempt to apply metadata patches
747 echo -n "Applying metadata patches... "
748 local oldifs="$IFS" IFS='|'
750 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
751 gunzip -c < ${X}-${Y}.gz > diff
752 gunzip -c < files/${X}.gz > OLD
753 cut -c 2- diff | join -t '|' -v 2 - OLD > ptmp
754 grep '^\+' diff | cut -c 2- |
755 sort -k 1,1 -t '|' -m - ptmp > NEW
756 if [ `${SHA256} -q NEW` = ${Y} ]; then
760 rm -f diff OLD NEW ${X}-${Y}.gz ptmp
761 done < patchlist 2>${QUIETREDIR}
765 # Update metadata without patches
766 join -t '|' -v 2 tINDEX tINDEX.new |
767 cut -f 2 -d '|' /dev/stdin patchlist |
769 if [ ! -f "files/${Y}.gz" ]; then
773 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
774 echo ${NDEBUG} "metadata files... "
775 lam -s "f/" - -s ".gz" < filelist |
776 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
780 echo -n "Verifying ${Y}... " 1>${QUIETREDIR}
781 if [ `gunzip -c < ${Y}.gz | ${SHA256} -q` = ${Y} ]; then
782 mv ${Y}.gz files/${Y}.gz
784 echo "metadata is corrupt."
787 echo "ok." 1>${QUIETREDIR}
792 echo -n "Extracting index... " 1>${QUIETREDIR}
793 gunzip -c < files/`look INDEX tINDEX.new |
794 cut -f 2 -d '|'`.gz > INDEX.new
795 fetch_index_sanity || return 1
797 # If we have decided to refuse certain updates, construct a hybrid index which
798 # is equal to the old index for parts of the tree which we don't want to
799 # update, and equal to the new index for parts of the tree which gets updates.
800 # This means that we should always have a "complete snapshot" of the ports
801 # tree -- with the caveat that it isn't actually a snapshot.
802 if [ ! -z "${REFUSE}" ]; then
803 echo "Refusing to download updates for ${REFUSE}" \
806 grep -Ev "${REFUSE}" INDEX.new > INDEX.tmp
807 grep -E "${REFUSE}" INDEX |
808 sort -m -k 1,1 -t '|' - INDEX.tmp > INDEX.new
812 # Generate a list of wanted ports patches
813 echo -n "Generating list of wanted patches..." 1>${QUIETREDIR}
814 join -t '|' -o 1.2,2.2 INDEX INDEX.new |
815 fetch_make_patchlist > patchlist
816 echo " done." 1>${QUIETREDIR}
818 # Attempt to fetch ports patches
819 patchcnt=`wc -l < patchlist | tr -d ' '`
820 echo -n "Fetching $patchcnt "
821 echo ${NDEBUG} "patches.${DDSTATS}"
823 tr '|' '-' < patchlist | lam -s "bp/" - |
824 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
825 2>${STATSREDIR} | fetch_progress_percent $patchcnt
828 # Attempt to apply ports patches
829 PATCHCNT=`wc -l patchlist`
830 echo "Applying patches... "
831 local oldifs="$IFS" IFS='|'
836 if [ ! -f "${F}" ]; then
837 printf " Skipping ${F} (${I} of ${PATCHCNT}).\r"
840 echo " Processing ${F}..." 1>${QUIETREDIR}
841 gunzip -c < files/${X}.gz > OLD
842 ${BSPATCH} OLD NEW ${X}-${Y}
843 if [ `${SHA256} -q NEW` = ${Y} ]; then
847 rm -f diff OLD NEW ${X}-${Y}
848 done < patchlist 2>${QUIETREDIR}
852 # Update ports without patches
853 join -t '|' -v 2 INDEX INDEX.new |
854 cut -f 2 -d '|' /dev/stdin patchlist |
856 if [ ! -f "files/${Y}.gz" ]; then
860 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
861 echo ${NDEBUG} "new ports or files... "
862 lam -s "f/" - -s ".gz" < filelist |
863 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
869 printf " Processing ${Y} (${I} of ${PATCHCNT}).\r" 1>${QUIETREDIR}
870 if [ `gunzip -c < ${Y}.gz | ${SHA256} -q` = ${Y} ]; then
871 mv ${Y}.gz files/${Y}.gz
873 echo "snapshot is corrupt."
879 # Remove files which are no longer needed
880 cut -f 2 -d '|' tINDEX INDEX | sort -u > oldfiles
881 cut -f 2 -d '|' tINDEX.new INDEX.new | sort -u | comm -13 - oldfiles |
882 lam -s "files/" - -s ".gz" | xargs rm -f
883 rm patchlist filelist oldfiles
893 # Do the actual work involved in "fetch" / "cron".
895 fetch_pick_server_init && fetch_pick_server
897 while ! fetch_key; do
898 fetch_pick_server || return 1
901 if ! [ -d files -a -r tag -a -r INDEX -a -r tINDEX ]; then
902 fetch_snapshot || return 1
904 fetch_update || return 1
907 # Build a ports INDEX file
908 extract_make_index() {
909 if ! look $1 ${WORKDIR}/tINDEX > /dev/null; then
910 echo -n "$1 not provided by portsnap server; "
911 echo "$2 not being generated."
913 gunzip -c < "${WORKDIR}/files/`look $1 ${WORKDIR}/tINDEX |
914 cut -f 2 -d '|'`.gz" |
916 ${MKINDEX} /dev/stdin > ${PORTSDIR}/$2
920 # Create INDEX, INDEX-5, INDEX-6
922 echo -n "Building new INDEX files... "
923 for PAIR in ${INDEXPAIRS}; do
924 INDEXFILE=`echo ${PAIR} | cut -f 1 -d '|'`
925 DESCRIBEFILE=`echo ${PAIR} | cut -f 2 -d '|'`
926 extract_make_index ${DESCRIBEFILE} ${INDEXFILE} || return 1
931 # Create .portsnap.INDEX; if we are REFUSEing to touch certain directories,
932 # merge the values from any exiting .portsnap.INDEX file.
934 if [ -z "${REFUSE}" ]; then
935 sort ${WORKDIR}/INDEX > ${PORTSDIR}/.portsnap.INDEX
936 elif [ -f ${PORTSDIR}/.portsnap.INDEX ]; then
937 grep -E "${REFUSE}" ${PORTSDIR}/.portsnap.INDEX \
938 > ${PORTSDIR}/.portsnap.INDEX.tmp
939 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort |
940 sort -m - ${PORTSDIR}/.portsnap.INDEX.tmp \
941 > ${PORTSDIR}/.portsnap.INDEX
942 rm -f ${PORTSDIR}/.portsnap.INDEX.tmp
944 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort \
945 > ${PORTSDIR}/.portsnap.INDEX
949 # Do the actual work involved in "extract"
951 local oldifs="$IFS" IFS='|'
952 mkdir -p ${PORTSDIR} || return 1
955 if ! [ -z "${EXTRACTPATH}" ]; then
956 grep "^${EXTRACTPATH}" ${WORKDIR}/INDEX
957 elif ! [ -z "${REFUSE}" ]; then
958 grep -vE "${REFUSE}" ${WORKDIR}/INDEX
961 fi | while read FILE HASH; do
962 echo ${PORTSDIR}/${FILE}
963 if ! [ -s "${WORKDIR}/files/${HASH}.gz" ]; then
964 echo "files/${HASH}.gz not found -- snapshot corrupt."
969 rm -rf ${PORTSDIR}/${FILE%/}
970 mkdir -p ${PORTSDIR}/${FILE}
971 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
972 -C ${PORTSDIR}/${FILE}
975 rm -f ${PORTSDIR}/${FILE}
976 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
977 -C ${PORTSDIR} ${FILE}
983 if [ ! -z "${EXTRACTPATH}" ]; then
993 update_run_extract() {
997 echo "Extracting new files:"
999 if ! [ -z "${REFUSE}" ]; then
1000 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort
1002 sort ${WORKDIR}/INDEX
1004 comm -13 ${PORTSDIR}/.portsnap.INDEX - |
1005 while read FILE HASH; do
1006 echo ${PORTSDIR}/${FILE}
1007 if ! [ -s "${WORKDIR}/files/${HASH}.gz" ]; then
1008 echo "files/${HASH}.gz not found -- snapshot corrupt."
1013 mkdir -p ${PORTSDIR}/${FILE}
1014 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
1015 -C ${PORTSDIR}/${FILE}
1018 tar -xz --numeric-owner -f ${WORKDIR}/files/${HASH}.gz \
1019 -C ${PORTSDIR} ${FILE}
1027 # Do the actual work involved in "update"
1029 if ! [ -z "${INDEXONLY}" ]; then
1030 extract_indices >/dev/null || return 1
1034 if sort ${WORKDIR}/INDEX |
1035 cmp -s ${PORTSDIR}/.portsnap.INDEX -; then
1036 echo "Ports tree is already up to date."
1040 # If we are REFUSEing to touch certain directories, don't remove files
1041 # from those directories (even if they are out of date)
1042 echo -n "Removing old files and directories... "
1043 if ! [ -z "${REFUSE}" ]; then
1044 sort ${WORKDIR}/INDEX |
1045 comm -23 ${PORTSDIR}/.portsnap.INDEX - | cut -f 1 -d '|' |
1046 grep -vE "${REFUSE}" |
1047 lam -s "${PORTSDIR}/" - |
1048 sed -e 's|/$||' | xargs rm -rf
1050 sort ${WORKDIR}/INDEX |
1051 comm -23 ${PORTSDIR}/.portsnap.INDEX - | cut -f 1 -d '|' |
1052 lam -s "${PORTSDIR}/" - |
1053 sed -e 's|/$||' | xargs rm -rf
1057 update_run_extract || return 1
1062 #### Main functions -- call parameter-handling and core functions
1064 # Using the command line, configuration file, and defaults,
1065 # set all the parameters which are needed later.
1075 # Fetch command. Make sure that we're being called
1076 # interactively, then run fetch_check_params and fetch_run
1078 if [ "${INTERACTIVE}" != "YES" ]; then
1079 echo -n "`basename $0` fetch should not "
1080 echo "be run non-interactively."
1081 echo "Run `basename $0` cron instead"
1088 # Cron command. Make sure the parameters are sensible; wait
1089 # rand(3600) seconds; then fetch updates. While fetching updates,
1090 # send output to a temporary file; only print that file if the
1094 sleep `jot -r 1 0 3600`
1096 TMPFILE=`mktemp /tmp/portsnap.XXXXXX` || exit 1
1097 if ! fetch_run >> ${TMPFILE}; then
1106 # Extract command. Make sure the parameters are sensible,
1107 # then extract the ports tree (or part thereof).
1109 extract_check_params
1110 extract_run || exit 1
1113 # Update command. Make sure the parameters are sensible,
1114 # then update the ports tree.
1117 update_run || exit 1
1120 # Auto command. Run 'fetch' or 'cron' depending on
1121 # whether stdin is a terminal; then run 'update' or
1122 # 'extract' depending on whether ${PORTSDIR} exists.
1124 if [ "${INTERACTIVE}" = "YES" ]; then
1129 if [ -r ${PORTSDIR}/.portsnap.INDEX ]; then
1138 # Make sure we find utilities from the base system
1139 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
1141 # Set LC_ALL in order to avoid problems with character ranges like [A-Z].
1145 for COMMAND in ${COMMANDS}; do