more cp-like behaviour

This commit is contained in:
q
2017-05-14 21:24:04 +03:00
parent 09f343c52f
commit 57c9e817ba

View File

@@ -31,24 +31,48 @@ getsize() {
if [[ "$NOCOUNT" -eq 1 ]]; then if [[ "$NOCOUNT" -eq 1 ]]; then
echo 0 echo 0
else else
fastdu -s "${SRC[@]}" fastdu -s "$@"
fi fi
} }
# if 1 input, and is file, copy as file copy_file() {
pv "$1" > "$2"
chmod --reference="$1" "$2"
chown --reference="$1" "$2"
}
copy_dir() {
mkdir -p "$2"
chmod --reference="$1" "$2"
chown --reference="$1" "$2"
mysize=$( getsize "$1" )
TGT_ABS=$( readlink -f "$2" )
pushd "$1" &> /dev/null
tar -c "." | pv -s $mysize | tar -x --strip-components=1 -C "$TGT_ABS"
popd &> /dev/null
}
copy_to() {
if [[ -f "$1" ]]; then # file to folder
copy_file "$1" "$2"
else # folder to folder
copy_dir "$1" "$2"
fi
}
if [[ ${#SRC[@]} -eq 1 ]]; then # only one input if [[ ${#SRC[@]} -eq 1 ]]; then # only one input
if [[ -f ${SRC[0]} ]]; then # its a file if [[ ! -d "$TGT" ]]; then # target is not a (existing) dir
if [[ ! -d "$TGT" ]]; then # target is not a dir copy_to "${SRC[0]}" "$TGT"
pv "${SRC[0]}" > "$TGT"
chmod --reference="${SRC[0]}" "$TGT"
chown --reference="${SRC[0]}" "$TGT"
exit $? exit $?
fi fi
fi
fi fi
# Otherwise, create folder, and copy in it # Otherwise, create folder, and copy in it
[[ -f "$TGT" ]] && { echo Target can only be a folder; exit 1; } [[ -f "$TGT" ]] && { echo Copying multiple sources: Target can only be a folder; exit 1; }
mkdir -p "$TGT" mkdir -p "$TGT"
tar -c "${SRC[@]}" | pv -s $( getsize ) | tar -x -C "$TGT" for SRC_THIS in "${SRC[@]}"; do
SRC_BASE=$( basename "$SRC_THIS")
echo "$SRC_THIS"
copy_to "$SRC_THIS" "$TGT"/"$SRC_BASE"
done