blob: f8a472a58a5c7a6c21e775428dacb35fc56a7629 [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 [--to-target|--to-host] SRC... DST
##
## Copies a file from the host to the target device, or vice versa.
##
## --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
to_target=true
if [[ $# -gt 2 ]]; then
case "$1" in
--to-target)
to_target=true
shift
;;
--to-host)
to_target=false
shift
;;
--help)
fx-command-help
exit 1
esac
fi
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=()
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 \"${src}\" \"${dst}\""
done
) | fx-command-run sftp "${sftp_args[@]}" > /dev/null
else
(
for src in "${srcs[@]}"; do
echo "get \"${src}\" \"${dst}\""
done
) | fx-command-run sftp "${sftp_args[@]}" > /dev/null
fi