blob: adb02e78e623b64633418e8004cccee44bb3c9c4 [file] [log] [blame]
#!/bin/bash
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Device management
### copy a file to/from a target device
## usage: fx cp [-r] [--to-target|--to-host] SRC... DST
##
## Copies a file from the host to the target device, or vice versa.
##
## -r: recursively copy directories
## --to-target: copy file SRC from host to DST on the target
## --to-host: copy file SRC from target to DST on the host
##
## The default is to copy files to the target.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
set -e
recurse=false
to_target=true
# Process all the args except the last two.
while (( "$#" > 2 )); do
case "$1" in
-r)
recurse=true
shift
;;
--to-target)
to_target=true
shift
;;
--to-host)
to_target=false
shift
;;
--help)
fx-command-help
exit 1
esac
done
if [[ $# -lt 2 ]]; then
fx-command-help
exit 1
fi
args=( "$@" )
nargs=${#args[@]}
dst=${args[$nargs-1]}
srcs=( "${args[@]:0:$nargs-1}" )
host="$(get-fuchsia-device-addr)"
sftp_args=()
sftp_command_args=()
if [[ "${recurse}" = "true" ]]; then
sftp_command_args+=( "-r" )
fi
port="$(get-device-ssh-port)"
if [[ -n "${port}" ]]; then
sftp_args+=( "-P" "${port}" )
fi
sftp_args+=(
"-q"
"-b" "-"
"[${host}]"
)
if [[ "${to_target}" = "true" ]]; then
(
for src in "${srcs[@]}"; do
echo "put ${sftp_command_args} \"${src}\" \"${dst}\""
done
) | fx-command-run sftp "${sftp_args[@]}" > /dev/null
else
(
for src in "${srcs[@]}"; do
echo "get ${sftp_command_args} \"${src}\" \"${dst}\""
done
) | fx-command-run sftp "${sftp_args[@]}" > /dev/null
fi