Added author notices.
[rpush-cat.git] / rpush-continue
blob5c19b1ae4db37b619ebc341a81334a659ffb71c1
1 #!/bin/bash
3 # Usage:
4 # rpush-continue SRC [--android|--unix] DEST ssh SSH_args..
5 # or
6 # rpush-continue SRC [--android|--unix] DEST eval
8 # Warning:
9 # DEST on the remote side will be appended to without questions!
11 # If DEST on the remote side doesn't exist, we'll probably exit immediately.
13 # "ssh" and "eval" are just examples; any valid bash command
14 # which interpretes arguments in the same way as "eval" will do.
16 # --android or --unix switches the pattern for reading the size from
17 # the output of "ls -l" (either on Android 2.2 or usual Unix, GNU/Linux).
18 # By default, we assume you are connecting to an Android (with DroidSSHd).
20 # Licensed to all under AGPL as published by FSF.
21 # Copyright (C) 2012 by Ivan Zakharyaschev imz at altlinux.org
23 readonly SRC="$1"
24 shift
26 case "$1" in
27 --unix)
28 shift
29 readonly readSizeRemote=readSizeUnixls
31 --android)
32 shift
33 readonly readSizeRemote=readSizeAndroidls
36 readonly readSizeRemote=readSizeAndroidls
38 esac
40 readonly DEST="$1"
41 shift
43 readonly RSH="$1"
44 shift
45 case "$RSH" in
46 ssh)
47 RSH() {
48 ssh -e none "$@"
52 RSH() {
53 "$RSH" "$@"
56 esac
58 readSizeUnixls() {
59 # The columns of "ls -l" output on GNU/Linux:
60 local perms links user group size rest
61 read perms links user group size rest \
62 && echo "$size"
65 readSizeAndroidls() {
66 # "ls -l" output on Android 2.2 (Galaxy S) misses one column:
67 local perms user group size rest
68 read perms user group size rest \
69 && echo "$size"
72 # Other (non)possibilities to get the size of a file:
73 # * "ls -s" is in blocks, but I want bytes;
74 # * "stat" is missing on Android (2.2).
76 # If SRC doesn't exist, we'll exit before doing anything else.
77 SRCsize="$(ls -l "$SRC" | readSizeUnixls)" || exit $?
79 datapipe="$(mktemp --dry-run)"
80 rm_datapipe() {
81 rm -f "$datapipe"
83 trap rm_datapipe EXIT
84 mkfifo "$datapipe"
86 # TODO: the problem here: the value $DEST should be protected from an expansion by the remote shell.
87 RSH "$@" "ls -l $DEST && cat >> $DEST" < "$datapipe" | \
89 DESTsize="$($readSizeRemote)" || exit $?
90 tail -c +$((DESTsize+1)) "$SRC" | pv --wait --size $((SRCsize-DESTsize))
91 } > "$datapipe"