diff options
author | Dominique Martinet <asmadeus@codewreck.org> | 2017-12-02 10:45:04 +0100 |
---|---|---|
committer | Dominique Martinet <asmadeus@codewreck.org> | 2017-12-02 10:45:04 +0100 |
commit | fa3dfcbb53438e3a106821f37c8f884f92676537 (patch) | |
tree | 01a7a1701c608ea6fac0c5540f470da597eaf384 | |
parent | 84e1d4bdacffd429eb29a8186fccb1996a580364 (diff) |
use dir fd, three steps
-rwxr-xr-x | mcp.sh | 48 |
1 files changed, 41 insertions, 7 deletions
@@ -1,16 +1,50 @@ #!/bin/bash +VERBOSE= + cp-dir() { - local SRC="$1" - local DST="$2" - shift 2 + local DST="$1" + shift 1 for DIR in "$@"; do - DSTDIR="${DST}${DIR#$SRC}" - echo mkdir "$DSTDIR" - find "$DIR" -maxdepth 1 -not -type d -execdir sh -c 'echo cp "$@" "$DSTDIR"' -- {} + + DSTDIR="$DST/$DIR" + find "$DIR" -maxdepth 1 -not -type d -execdir sh -c 'DSTDIR="$1"; shift; cp -a${VERBOSE+v} "$@" "$DSTDIR"' -- "$DSTDIR" {} + done } export -f cp-dir -find tree -type d -print0 | xargs -0 -P 8 bash -c 'cp-dir "$@"' -- tree /tmp/dest +mk-dirs() { + local DST="$1" + shift 1 + + for DIR in "$@"; do + DSTDIR="$DST/$DIR" + [[ -d "$DSTDIR" ]] || mkdir ${VERBOSE+-v} "$DSTDIR" + chown --reference "$DIR" "$DSTDIR" + chmod --reference "$DIR" "$DSTDIR" + done +} +export -f mk-dirs + +fixstamp-dirs() { + local DST="$1" + shift 1 + + for DIR in "$@"; do + DSTDIR="$DST/$DIR" + touch --reference "$DIR" "$DSTDIR" + done +} +export -f fixstamp-dirs + + +set -euo pipefail + +umask 0077 +mkdir -p /tmp/dest +exec {DESTFD}</tmp/dest + +cd /tmp/tree +find . -type d -print0 | xargs -0 bash -c 'mk-dirs "$@"' -- /proc/$$/fd/$DESTFD +find . -type d -print0 | xargs -0 -P 8 -n 10 bash -c 'cp-dir "$@"' -- /proc/$$/fd/$DESTFD +find . -type d -print0 | xargs -0 -P 8 -n 10 bash -c 'fixstamp-dirs "$@"' -- /proc/$$/fd/$DESTFD |