about summary refs log tree commit diff
path: root/Functions/Zftp/zfcget
blob: fd6accfed0607acc1aee042a839ba79ebad4f933 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# function zfcget {
# Continuation get of files from remote server.
# For each file, if it's shorter here, try to get the remainder from
# over there.  This requires the server to support the REST command
# in the way many do but RFC959 doesn't specify.
# Options:
#   -G   don't to remote globbing, else do
#   -t   update the local file times to the same time as the remote.
#        Currently this only works if you have the `perl' command,
#        and that perl is version 5 with the standard library.
#        See the function zfrtime for more gory details.

emulate -L zsh

local loc rem stat=0 optlist opt nglob remlist locst remst
local tmpfile=${TMPPREFIX}zfcget$$ rstat tsize time

while [[ $1 = -* ]]; do
  if [[ $1 = - || $1 = -- ]]; then
    shift;
    break;
  fi
  optlist=${1#-}
  for (( i = 1; i <= $#optlist; i++)); do
    opt=$optlist[$i]
    case $optlist[$i] in
      G) nglob=1
	 ;;
      t) time=1
	 ;;
      *) print option $opt not recognised >&2
	 ;;
    esac
  done
  shift
done

for remlist in $*; do
  # zfcd directory hack to put the front back to ~
  if [[ $remlist = $HOME || $remlist = $HOME/* ]]; then
    remlist="~${remlist#$HOME}"
  fi
  if [[ $nglob != 1 ]]; then
    zfrglob remlist
  fi
  if (( $#remlist )); then
    for rem in $remlist; do
      loc=${rem:t}
      if [[ ! -f $loc ]]; then
	# File does not yet exist
	zftp get $rem >$loc || stat=$?
      else
	# Compare the sizes.
	locst=($(zftp local $loc))
	zftp remote $rem >$tmpfile
	rstat=$?
	remst=($(<$tmpfile))
	rm -f $tmpfile
	if [[ $rstat = 2 ]]; then
	  print "Server does not support SIZE command.\n" \
	  "Assuming you know what you're doing..." 2>&1
	  zftp getat $rem $locst[1] >>$loc || stat=$?
	  continue
	elif [[ $rstat = 1 ]]; then
	  print "Remote file not found: $rem" 2>&1
	  continue
	fi
	if [[ $locst[1] -gt $remst[1] ]]; then
	  print "Local file is larger!" 2>&1
	  continue;
	elif [[ $locst[1] == $remst[1] ]]; then
	  print "Files are already the same size." 2>&1
	  continue
	else
	  if zftp getat $rem $locst[1] >>$loc; then
	    [[ $time = 1 ]] && zfrtime $loc $rem $remst[2]
	  else
	    stat=1
	  fi
	fi
      fi
    done
  fi
done

return $stat
# }