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 path -- Extract only parts of the tree starting with the given
52 string. (extract command only)
54 fetch -- Fetch a compressed snapshot of the ports tree,
55 or update an existing snapshot.
56 cron -- Sleep rand(3600) seconds, and then fetch updates.
57 extract -- Extract snapshot of ports tree, replacing existing
58 files and directories.
59 update -- Update ports tree to match current snapshot, replacing
60 files and directories which have changed.
65 #### Parameter handling functions.
67 # Initialize parameters to null, just in case they're
68 # set in the environment.
89 # Parse the command line
91 while [ $# -gt 0 ]; do
94 if [ $# -eq 1 ]; then usage; fi
95 if [ ! -z "${WORKDIR}" ]; then usage; fi
99 QUIETREDIR="/dev/stderr"
100 STATSREDIR="/dev/stderr"
107 if [ $# -eq 1 ]; then usage; fi
108 if [ ! -z "${CONFFILE}" ]; then usage; fi
118 if [ $# -eq 1 ]; then usage; fi
119 if [ ! -z "${KEYPRINT}" ]; then usage; fi
123 if [ $# -eq 1 ]; then usage; fi
124 if [ ! -z "${LOCALDESC}" ]; then usage; fi
125 shift; LOCALDESC="$1"
128 if [ -z "${STATSREDIR}" ]; then
129 STATSREDIR="/dev/null"
134 if [ $# -eq 1 ]; then usage; fi
135 if [ ! -z "${PORTSDIR}" ]; then usage; fi
139 if [ $# -eq 1 ]; then usage; fi
140 if [ ! -z "${SERVERNAME}" ]; then usage; fi
141 shift; SERVERNAME="$1"
143 cron | extract | fetch | update)
144 COMMANDS="${COMMANDS} $1"
147 if [ $# -gt 1 ]; then usage; fi
148 if echo ${COMMANDS} | grep -vq extract; then
157 if [ -z "${COMMANDS}" ]; then
162 # If CONFFILE was specified at the command-line, make
163 # sure that it exists and is readable.
165 if [ ! -z "${CONFFILE}" ] && [ ! -r "${CONFFILE}" ]; then
166 echo -n "File does not exist "
167 echo -n "or is not readable: "
173 # If a configuration file hasn't been specified, use
174 # the default value (/etc/portsnap.conf)
176 if [ -z "${CONFFILE}" ]; then
177 CONFFILE="/etc/portsnap.conf"
181 # Read {KEYPRINT, SERVERNAME, WORKDIR, PORTSDIR} from the configuration
182 # file if they haven't already been set. If the configuration
183 # file doesn't exist, do nothing.
184 # Also read REFUSE (which cannot be set via the command line) if it is
185 # present in the configuration file.
187 if [ -r "${CONFFILE}" ]; then
188 for X in KEYPRINT WORKDIR PORTSDIR SERVERNAME; do
190 if [ -z "${_}" ]; then
191 eval ${X}=`grep "^${X}=" "${CONFFILE}" |
192 cut -f 2- -d '=' | tail -1`
196 if grep -qE "^REFUSE[[:space:]]" ${CONFFILE}; then
198 grep -E "^REFUSE[[:space:]]" "${CONFFILE}" |
199 cut -c 7- | xargs echo | tr ' ' '|'
203 if grep -qE "^INDEX[[:space:]]" ${CONFFILE}; then
205 grep -E "^INDEX[[:space:]]" "${CONFFILE}" |
206 cut -c 7- | tr ' ' '|' | xargs echo`"
211 # If parameters have not been set, use default values
213 _QUIETREDIR="/dev/null"
215 _STATSREDIR="/dev/stdout"
216 _WORKDIR="/var/db/portsnap"
217 _PORTSDIR="/usr/ports"
219 _LOCALDESC="/dev/null"
220 for X in QUIETREDIR QUIETFLAG STATSREDIR WORKDIR PORTSDIR \
224 if [ -z "${_}" ]; then
230 # Perform sanity checks and set some final parameters
231 # in preparation for fetching files. Also chdir into
232 # the working directory.
233 fetch_check_params() {
234 export HTTP_USER_AGENT="portsnap (${COMMAND}, `uname -r`)"
237 "SERVERNAME must be given via command line or configuration file."
238 _KEYPRINT_z="Key must be given via -k option or configuration file."
239 _KEYPRINT_bad="Invalid key fingerprint: "
240 _WORKDIR_bad="Directory does not exist or is not writable: "
242 if [ -z "${SERVERNAME}" ]; then
243 echo -n "`basename $0`: "
244 echo "${_SERVERNAME_z}"
247 if [ -z "${KEYPRINT}" ]; then
248 echo -n "`basename $0`: "
249 echo "${_KEYPRINT_z}"
252 if ! echo "${KEYPRINT}" | grep -qE "^[0-9a-f]{64}$"; then
253 echo -n "`basename $0`: "
254 echo -n "${_KEYPRINT_bad}"
258 if ! [ -d "${WORKDIR}" -a -w "${WORKDIR}" ]; then
259 echo -n "`basename $0`: "
260 echo -n "${_WORKDIR_bad}"
264 cd ${WORKDIR} || exit 1
266 BSPATCH=/usr/bin/bspatch
268 PHTTPGET=/usr/libexec/phttpget
271 # Perform sanity checks and set some final parameters
272 # in preparation for extracting or updating ${PORTSDIR}
273 # Complain if ${PORTSDIR} exists but is not writable,
274 # but don't complain if ${PORTSDIR} doesn't exist.
275 extract_check_params() {
276 _WORKDIR_bad="Directory does not exist: "
277 _PORTSDIR_bad="Directory is not writable: "
279 if ! [ -d "${WORKDIR}" ]; then
280 echo -n "`basename $0`: "
281 echo -n "${_WORKDIR_bad}"
285 if [ -d "${PORTSDIR}" ] && ! [ -w "${PORTSDIR}" ]; then
286 echo -n "`basename $0`: "
287 echo -n "${_PORTSDIR_bad}"
292 if ! [ -d "${WORKDIR}/files" -a -r "${WORKDIR}/tag" \
293 -a -r "${WORKDIR}/INDEX" -a -r "${WORKDIR}/tINDEX" ]; then
294 echo "No snapshot available. Try running"
295 echo "# `basename $0` fetch"
299 MKINDEX=/usr/libexec/make_index
302 # Perform sanity checks and set some final parameters
303 # in preparation for updating ${PORTSDIR}
304 update_check_params() {
307 if ! [ -r ${PORTSDIR}/.portsnap.INDEX ]; then
308 echo "${PORTSDIR} was not created by portsnap."
309 echo -n "You must run '`basename $0` extract' before "
310 echo "running '`basename $0` update'."
316 #### Core functionality -- the actual work gets done here
318 # Use an SRV query to pick a server. If the SRV query doesn't provide
319 # a useful answer, use the server name specified by the user.
320 # Put another way... look up _http._tcp.${SERVERNAME} and pick a server
321 # from that; or if no servers are returned, use ${SERVERNAME}.
322 # This allows a user to specify "portsnap.freebsd.org" (in which case
323 # portsnap will select one of the mirrors) or "portsnap5.tld.freebsd.org"
324 # (in which case portsnap will use that particular server, since there
325 # won't be an SRV entry for that name).
327 # We ignore the Port field, since we are always going to use port 80.
329 # Fetch the mirror list, but do not pick a mirror yet. Returns 1 if
330 # no mirrors are available for any reason.
331 fetch_pick_server_init() {
334 # Check that host(1) exists (i.e., that the system wasn't built with the
335 # WITHOUT_BIND set) and don't try to find a mirror if it doesn't exist.
336 if ! which -s host; then
341 echo -n "Looking up ${SERVERNAME} mirrors... "
343 # Issue the SRV query and pull out the Priority, Weight, and Target fields.
344 # BIND 9 prints "$name has SRV record ..." while BIND 8 prints
345 # "$name server selection ..."; we allow either format.
346 MLIST="_http._tcp.${SERVERNAME}"
347 host -t srv "${MLIST}" |
348 sed -nE "s/${MLIST} (has SRV record|server selection) //p" |
349 cut -f 1,2,4 -d ' ' |
351 sort > serverlist_full
353 # If no records, give up -- we'll just use the server name we were given.
354 if [ `wc -l < serverlist_full` -eq 0 ]; then
359 # Report how many mirrors we found.
360 echo `wc -l < serverlist_full` "mirrors found."
362 # Generate a random seed for use in picking mirrors. If HTTP_PROXY
363 # is set, this will be used to generate the seed; otherwise, the seed
365 if [ -n "${HTTP_PROXY}${http_proxy}" ]; then
366 RANDVALUE=`sha256 -qs "${HTTP_PROXY}${http_proxy}" |
370 RANDVALUE=`jot -r 1 0 999999999`
374 # Pick a mirror. Returns 1 if we have run out of mirrors to try.
375 fetch_pick_server() {
376 # Generate a list of not-yet-tried mirrors
377 sort serverlist_tried |
378 comm -23 serverlist_full - > serverlist
380 # Have we run out of mirrors?
381 if [ `wc -l < serverlist` -eq 0 ]; then
382 echo "No mirrors remaining, giving up."
386 # Find the highest priority level (lowest numeric value).
387 SRV_PRIORITY=`cut -f 1 -d ' ' serverlist | sort -n | head -1`
389 # Add up the weights of the response lines at that priority level.
394 SRV_W=`echo $X | cut -f 2 -d ' '`
395 SRV_WSUM=$(($SRV_WSUM + $SRV_W))
400 # If all the weights are 0, pretend that they are all 1 instead.
401 if [ ${SRV_WSUM} -eq 0 ]; then
402 SRV_WSUM=`grep -E "^${SRV_PRIORITY} " serverlist | wc -l`
408 # Pick a value between 0 and the sum of the weights - 1
409 SRV_RND=`expr ${RANDVALUE} % ${SRV_WSUM}`
411 # Read through the list of mirrors and set SERVERNAME. Write the line
412 # corresponding to the mirror we selected into serverlist_tried so that
413 # we won't try it again.
417 SRV_W=`echo $X | cut -f 2 -d ' '`
418 SRV_W=$(($SRV_W + $SRV_W_ADD))
419 if [ $SRV_RND -lt $SRV_W ]; then
420 SERVERNAME=`echo $X | cut -f 3 -d ' '`
421 echo "$X" >> serverlist_tried
424 SRV_RND=$(($SRV_RND - $SRV_W))
431 # Check that we have a public key with an appropriate hash, or
432 # fetch the key if it doesn't exist. Returns 1 if the key has
433 # not yet been fetched.
435 if [ -r pub.ssl ] && [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
439 echo -n "Fetching public key from ${SERVERNAME}... "
441 fetch ${QUIETFLAG} http://${SERVERNAME}/pub.ssl \
442 2>${QUIETREDIR} || true
443 if ! [ -r pub.ssl ]; then
447 if ! [ `${SHA256} -q pub.ssl` = ${KEYPRINT} ]; then
448 echo "key has incorrect hash."
455 # Fetch a snapshot tag
457 rm -f snapshot.ssl tag.new
459 echo ${NDEBUG} "Fetching snapshot tag from ${SERVERNAME}... "
460 fetch ${QUIETFLAG} http://${SERVERNAME}/$1.ssl \
461 2>${QUIETREDIR} || true
462 if ! [ -r $1.ssl ]; then
467 openssl rsautl -pubin -inkey pub.ssl -verify \
468 < $1.ssl > tag.new 2>${QUIETREDIR} || true
471 if ! [ `wc -l < tag.new` = 1 ] ||
472 ! grep -qE "^portsnap\|[0-9]{10}\|[0-9a-f]{64}" tag.new; then
473 echo "invalid snapshot tag."
479 SNAPSHOTDATE=`cut -f 2 -d '|' < tag.new`
480 SNAPSHOTHASH=`cut -f 3 -d '|' < tag.new`
483 # Sanity-check the date on a snapshot tag
484 fetch_snapshot_tagsanity() {
485 if [ `date "+%s"` -gt `expr ${SNAPSHOTDATE} + 31536000` ]; then
486 echo "Snapshot appears to be more than a year old!"
487 echo "(Is the system clock correct?)"
488 echo "Cowardly refusing to proceed any further."
491 if [ `date "+%s"` -lt `expr ${SNAPSHOTDATE} - 86400` ]; then
492 echo -n "Snapshot appears to have been created more than "
493 echo "one day into the future!"
494 echo "(Is the system clock correct?)"
495 echo "Cowardly refusing to proceed any further."
500 # Sanity-check the date on a snapshot update tag
501 fetch_update_tagsanity() {
502 fetch_snapshot_tagsanity || return 1
504 if [ ${OLDSNAPSHOTDATE} -gt ${SNAPSHOTDATE} ]; then
505 echo -n "Latest snapshot on server is "
506 echo "older than what we already have!"
507 echo -n "Cowardly refusing to downgrade from "
508 date -r ${OLDSNAPSHOTDATE}
509 echo "to `date -r ${SNAPSHOTDATE}`."
514 # Compare old and new tags; return 1 if update is unnecessary
515 fetch_update_neededp() {
516 if [ ${OLDSNAPSHOTDATE} -eq ${SNAPSHOTDATE} ]; then
517 echo -n "Latest snapshot on server matches "
518 echo "what we already have."
519 echo "No updates needed."
523 if [ ${OLDSNAPSHOTHASH} = ${SNAPSHOTHASH} ]; then
524 echo -n "Ports tree hasn't changed since "
525 echo "last snapshot."
526 echo "No updates needed."
534 # Fetch snapshot metadata file
536 rm -f ${SNAPSHOTHASH} tINDEX.new
538 echo ${NDEBUG} "Fetching snapshot metadata... "
539 fetch ${QUIETFLAG} http://${SERVERNAME}/t/${SNAPSHOTHASH}
540 2>${QUIETREDIR} || return
541 if [ `${SHA256} -q ${SNAPSHOTHASH}` != ${SNAPSHOTHASH} ]; then
542 echo "snapshot metadata corrupt."
545 mv ${SNAPSHOTHASH} tINDEX.new
549 # Warn user about bogus metadata
550 fetch_metadata_freakout() {
552 echo "Portsnap metadata is correctly signed, but contains"
553 echo "at least one line which appears bogus."
554 echo "Cowardly refusing to proceed any further."
557 # Sanity-check a snapshot metadata file
558 fetch_metadata_sanity() {
559 if grep -qvE "^[0-9A-Z.]+\|[0-9a-f]{64}$" tINDEX.new; then
560 fetch_metadata_freakout
563 if [ `look INDEX tINDEX.new | wc -l` != 1 ]; then
565 echo "Portsnap metadata appears bogus."
566 echo "Cowardly refusing to proceed any further."
571 # Take a list of ${oldhash}|${newhash} and output a list of needed patches
572 fetch_make_patchlist() {
573 grep -vE "^([0-9a-f]{64})\|\1$" |
575 X=`echo ${LINE} | cut -f 1 -d '|'`
576 Y=`echo ${LINE} | cut -f 2 -d '|'`
577 if [ -f "files/${Y}.gz" ]; then continue; fi
578 if [ ! -f "files/${X}.gz" ]; then continue; fi
583 # Print user-friendly progress statistics
588 if [ $(($LNC % 10)) = 0 ]; then
590 elif [ $(($LNC % 2)) = 0 ]; then
597 # Sanity-check an index file
598 fetch_index_sanity() {
599 if grep -qvE "^[-_+./@0-9A-Za-z]+\|[0-9a-f]{64}$" INDEX.new ||
600 fgrep -q "./" INDEX.new; then
601 fetch_metadata_freakout
606 # Verify a list of files
607 fetch_snapshot_verify() {
609 if [ `gunzip -c snap/${F} | ${SHA256} -q` != ${F} ]; then
610 echo "snapshot corrupt."
617 # Fetch a snapshot tarball, extract, and verify.
619 while ! fetch_tag snapshot; do
620 fetch_pick_server || return 1
622 fetch_snapshot_tagsanity || return 1
623 fetch_metadata || return 1
624 fetch_metadata_sanity || return 1
628 # Don't ask fetch(1) to be quiet -- downloading a snapshot of ~ 35MB will
629 # probably take a while, so the progrees reports that fetch(1) generates
630 # will be useful for keeping the users' attention from drifting.
631 echo "Fetching snapshot generated at `date -r ${SNAPSHOTDATE}`:"
632 fetch -r http://${SERVERNAME}/s/${SNAPSHOTHASH}.tgz || return 1
634 echo -n "Extracting snapshot... "
635 tar -xzf ${SNAPSHOTHASH}.tgz snap/ || return 1
636 rm ${SNAPSHOTHASH}.tgz
639 echo -n "Verifying snapshot integrity... "
640 # Verify the metadata files
641 cut -f 2 -d '|' tINDEX.new | fetch_snapshot_verify || return 1
644 gunzip -c snap/`look INDEX tINDEX.new |
645 cut -f 2 -d '|'`.gz > INDEX.new
646 fetch_index_sanity || return 1
647 # Verify the snapshot contents
648 cut -f 2 -d '|' INDEX.new | fetch_snapshot_verify || return 1
651 # Move files into their proper locations
652 rm -f tag INDEX tINDEX
662 # Update a compressed snapshot
664 rm -f patchlist diff OLD NEW filelist INDEX.new
666 OLDSNAPSHOTDATE=`cut -f 2 -d '|' < tag`
667 OLDSNAPSHOTHASH=`cut -f 3 -d '|' < tag`
669 while ! fetch_tag latest; do
670 fetch_pick_server || return 1
672 fetch_update_tagsanity || return 1
673 fetch_update_neededp || return 0
674 fetch_metadata || return 1
675 fetch_metadata_sanity || return 1
677 echo -n "Updating from `date -r ${OLDSNAPSHOTDATE}` "
678 echo "to `date -r ${SNAPSHOTDATE}`."
680 # Generate a list of wanted metadata patches
681 join -t '|' -o 1.2,2.2 tINDEX tINDEX.new |
682 fetch_make_patchlist > patchlist
684 # Attempt to fetch metadata patches
685 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
686 echo ${NDEBUG} "metadata patches.${DDSTATS}"
687 tr '|' '-' < patchlist |
688 lam -s "tp/" - -s ".gz" |
689 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
690 2>${STATSREDIR} | fetch_progress
693 # Attempt to apply metadata patches
694 echo -n "Applying metadata patches... "
696 X=`echo ${LINE} | cut -f 1 -d '|'`
697 Y=`echo ${LINE} | cut -f 2 -d '|'`
698 if [ ! -f "${X}-${Y}.gz" ]; then continue; fi
699 gunzip -c < ${X}-${Y}.gz > diff
700 gunzip -c < files/${X}.gz > OLD
701 cut -c 2- diff | join -t '|' -v 2 - OLD > ptmp
702 grep '^\+' diff | cut -c 2- |
703 sort -k 1,1 -t '|' -m - ptmp > NEW
704 if [ `${SHA256} -q NEW` = ${Y} ]; then
708 rm -f diff OLD NEW ${X}-${Y}.gz ptmp
709 done < patchlist 2>${QUIETREDIR}
712 # Update metadata without patches
713 join -t '|' -v 2 tINDEX tINDEX.new |
714 cut -f 2 -d '|' /dev/stdin patchlist |
716 if [ ! -f "files/${Y}.gz" ]; then
720 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
721 echo ${NDEBUG} "metadata files... "
722 lam -s "f/" - -s ".gz" < filelist |
723 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
727 if [ `gunzip -c < ${Y}.gz | ${SHA256} -q` = ${Y} ]; then
728 mv ${Y}.gz files/${Y}.gz
730 echo "metadata is corrupt."
737 gunzip -c files/`look INDEX tINDEX.new |
738 cut -f 2 -d '|'`.gz > INDEX.new
739 fetch_index_sanity || return 1
741 # If we have decided to refuse certain updates, construct a hybrid index which
742 # is equal to the old index for parts of the tree which we don't want to
743 # update, and equal to the new index for parts of the tree which gets updates.
744 # This means that we should always have a "complete snapshot" of the ports
745 # tree -- with the caveat that it isn't actually a snapshot.
746 if [ ! -z "${REFUSE}" ]; then
747 echo "Refusing to download updates for ${REFUSE}" \
750 grep -Ev "${REFUSE}" INDEX.new > INDEX.tmp
751 grep -E "${REFUSE}" INDEX |
752 sort -m -k 1,1 -t '|' - INDEX.tmp > INDEX.new
756 # Generate a list of wanted ports patches
757 join -t '|' -o 1.2,2.2 INDEX INDEX.new |
758 fetch_make_patchlist > patchlist
760 # Attempt to fetch ports patches
761 echo -n "Fetching `wc -l < patchlist | tr -d ' '` "
762 echo ${NDEBUG} "patches.${DDSTATS}"
763 tr '|' '-' < patchlist | lam -s "bp/" - |
764 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
765 2>${STATSREDIR} | fetch_progress
768 # Attempt to apply ports patches
769 echo -n "Applying patches... "
771 X=`echo ${LINE} | cut -f 1 -d '|'`
772 Y=`echo ${LINE} | cut -f 2 -d '|'`
773 if [ ! -f "${X}-${Y}" ]; then continue; fi
774 gunzip -c < files/${X}.gz > OLD
775 ${BSPATCH} OLD NEW ${X}-${Y}
776 if [ `${SHA256} -q NEW` = ${Y} ]; then
780 rm -f diff OLD NEW ${X}-${Y}
781 done < patchlist 2>${QUIETREDIR}
784 # Update ports without patches
785 join -t '|' -v 2 INDEX INDEX.new |
786 cut -f 2 -d '|' /dev/stdin patchlist |
788 if [ ! -f "files/${Y}.gz" ]; then
792 echo -n "Fetching `wc -l < filelist | tr -d ' '` "
793 echo ${NDEBUG} "new ports or files... "
794 lam -s "f/" - -s ".gz" < filelist |
795 xargs ${XARGST} ${PHTTPGET} ${SERVERNAME} \
799 if [ `gunzip -c < ${Y}.gz | ${SHA256} -q` = ${Y} ]; then
800 mv ${Y}.gz files/${Y}.gz
802 echo "snapshot is corrupt."
808 # Remove files which are no longer needed
809 cut -f 2 -d '|' tINDEX INDEX | sort > oldfiles
810 cut -f 2 -d '|' tINDEX.new INDEX.new | sort | comm -13 - oldfiles |
811 lam -s "files/" - -s ".gz" | xargs rm -f
812 rm patchlist filelist oldfiles
822 # Do the actual work involved in "fetch" / "cron".
824 fetch_pick_server_init && fetch_pick_server
826 while ! fetch_key; do
827 fetch_pick_server || return 1
830 if ! [ -d files -a -r tag -a -r INDEX -a -r tINDEX ]; then
831 fetch_snapshot || return 1
833 fetch_update || return 1
836 # Build a ports INDEX file
837 extract_make_index() {
838 if ! look $1 ${WORKDIR}/tINDEX > /dev/null; then
839 echo -n "$1 not provided by portsnap server; "
840 echo "$2 not being generated."
842 gunzip -c "${WORKDIR}/files/`look $1 ${WORKDIR}/tINDEX |
843 cut -f 2 -d '|'`.gz" |
845 ${MKINDEX} /dev/stdin > ${PORTSDIR}/$2
849 # Create INDEX, INDEX-5, INDEX-6
851 echo -n "Building new INDEX files... "
852 for PAIR in ${INDEXPAIRS}; do
853 INDEXFILE=`echo ${PAIR} | cut -f 1 -d '|'`
854 DESCRIBEFILE=`echo ${PAIR} | cut -f 2 -d '|'`
855 extract_make_index ${DESCRIBEFILE} ${INDEXFILE} || return 1
860 # Create .portsnap.INDEX; if we are REFUSEing to touch certain directories,
861 # merge the values from any exiting .portsnap.INDEX file.
863 if [ -z "${REFUSE}" ]; then
864 sort ${WORKDIR}/INDEX > ${PORTSDIR}/.portsnap.INDEX
865 elif [ -f ${PORTSDIR}/.portsnap.INDEX ]; then
866 grep -E "${REFUSE}" ${PORTSDIR}/.portsnap.INDEX \
867 > ${PORTSDIR}/.portsnap.INDEX.tmp
868 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort |
869 sort -m - ${PORTSDIR}/.portsnap.INDEX.tmp \
870 > ${PORTSDIR}/.portsnap.INDEX
871 rm -f ${PORTSDIR}/.portsnap.INDEX.tmp
873 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort \
874 > ${PORTSDIR}/.portsnap.INDEX
878 # Do the actual work involved in "extract"
880 mkdir -p ${PORTSDIR} || return 1
883 if ! [ -z "${EXTRACTPATH}" ]; then
884 grep "^${EXTRACTPATH}" ${WORKDIR}/INDEX
885 elif ! [ -z "${REFUSE}" ]; then
886 grep -vE "${REFUSE}" ${WORKDIR}/INDEX
889 fi | tr '|' ' ' | while read FILE HASH; do
890 echo ${PORTSDIR}/${FILE}
891 if ! [ -r "${WORKDIR}/files/${HASH}.gz" ]; then
892 echo "files/${HASH}.gz not found -- snapshot corrupt."
897 rm -rf ${PORTSDIR}/${FILE%/}
898 mkdir -p ${PORTSDIR}/${FILE}
899 tar -xzf ${WORKDIR}/files/${HASH}.gz \
900 -C ${PORTSDIR}/${FILE}
903 rm -f ${PORTSDIR}/${FILE}
904 tar -xzf ${WORKDIR}/files/${HASH}.gz \
905 -C ${PORTSDIR} ${FILE}
911 if [ ! -z "${EXTRACTPATH}" ]; then
919 # Do the actual work involved in "update"
921 if ! [ -z "${INDEXONLY}" ]; then
922 extract_indices >/dev/null || return 1
926 if sort ${WORKDIR}/INDEX |
927 cmp -s ${PORTSDIR}/.portsnap.INDEX -; then
928 echo "Ports tree is already up to date."
932 # If we are REFUSEing to touch certain directories, don't remove files
933 # from those directories (even if they are out of date)
934 echo -n "Removing old files and directories... "
935 if ! [ -z "${REFUSE}" ]; then
936 sort ${WORKDIR}/INDEX |
937 comm -23 ${PORTSDIR}/.portsnap.INDEX - | cut -f 1 -d '|' |
938 grep -vE "${REFUSE}" |
939 lam -s "${PORTSDIR}/" - |
940 sed -e 's|/$||' | xargs rm -rf
942 sort ${WORKDIR}/INDEX |
943 comm -23 ${PORTSDIR}/.portsnap.INDEX - | cut -f 1 -d '|' |
944 lam -s "${PORTSDIR}/" - |
945 sed -e 's|/$||' | xargs rm -rf
950 echo "Extracting new files:"
952 if ! [ -z "${REFUSE}" ]; then
953 grep -vE "${REFUSE}" ${WORKDIR}/INDEX | sort
955 sort ${WORKDIR}/INDEX
957 comm -13 ${PORTSDIR}/.portsnap.INDEX - |
959 FILE=`echo ${LINE} | cut -f 1 -d '|'`
960 HASH=`echo ${LINE} | cut -f 2 -d '|'`
961 echo ${PORTSDIR}/${FILE}
962 if ! [ -r "${WORKDIR}/files/${HASH}.gz" ]; then
963 echo "files/${HASH}.gz not found -- snapshot corrupt."
968 mkdir -p ${PORTSDIR}/${FILE}
969 tar -xzf ${WORKDIR}/files/${HASH}.gz \
970 -C ${PORTSDIR}/${FILE}
973 tar -xzf ${WORKDIR}/files/${HASH}.gz \
974 -C ${PORTSDIR} ${FILE}
985 #### Main functions -- call parameter-handling and core functions
987 # Using the command line, configuration file, and defaults,
988 # set all the parameters which are needed later.
998 # Fetch command. Make sure that we're being called
999 # interactively, then run fetch_check_params and fetch_run
1002 echo -n "`basename $0` fetch should not "
1003 echo "be run non-interactively."
1004 echo "Run `basename $0` cron instead."
1011 # Cron command. Make sure the parameters are sensible; wait
1012 # rand(3600) seconds; then fetch updates. While fetching updates,
1013 # send output to a temporary file; only print that file if the
1017 sleep `jot -r 1 0 3600`
1019 TMPFILE=`mktemp /tmp/portsnap.XXXXXX` || exit 1
1020 if ! fetch_run >> ${TMPFILE}; then
1029 # Extract command. Make sure the parameters are sensible,
1030 # then extract the ports tree (or part thereof).
1032 extract_check_params
1033 extract_run || exit 1
1036 # Update command. Make sure the parameters are sensible,
1037 # then update the ports tree.
1040 update_run || exit 1
1045 # Make sure we find utilities from the base system
1046 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:${PATH}
1048 # Set LC_ALL in order to avoid problems with character ranges like [A-Z].
1052 for COMMAND in ${COMMANDS}; do