move dedupe to utilities and write a wrapper

This commit is contained in:
Connor Olding 2022-09-23 14:04:28 -07:00
parent d1216438ee
commit 298260f1a4
4 changed files with 34 additions and 14 deletions

View File

@ -127,18 +127,9 @@ RUN . ./env \
;
FROM builder AS distrib
COPY dedupe.awk /
COPY --chmod=0755 --from=localhost/notwa-util /usr/local/bin/dedupe /usr/bin/
RUN --mount=type=tmpfs,target=/tmp : \
&& . ./env \
&& dedupe() \
(: \
&& h=0 && { [ "$1" != --hard ] || shift && h=1 ;} \
&& cd "$1" && shift && D="$1" && shift && t="$(mktemp)" \
&& { [ $# -eq 0 ] || sha256sum >"$t" "$@" ;} \
&& find ! -type d -exec sha256sum {} + >>"$t~" \
&& sort -k2, <"$t~" >>"$t" \
&& awk -v D="$D" -v h="$h" -F' ' -f /dedupe.awk <"$t" \
) \
&& find o -name '*.sym' -delete \
&& dedupe o/third_party/gcc /distrib/gcc/ \
bin/x86_64-linux-musl-gcc \

View File

@ -1 +0,0 @@
BEGIN{if(!D){print U;exit 64}E=(Q="'")"\"'\""Q;H=(G=".")(F="/");I=" -- "Q;J="[^/]*/";sub(F"?$",F,D)gsub(Q,E,D)}function C(x){if(X=system(x))exit X}{if((p=$2)==B){print V;exit 65}for(r=z=i=d=B;d!=!i;i++){b=a=p;d=sub("/.*",B,a);sub(J,B,p);if(a==G G)e=!sub(J,B,z);else z=a==G?z:b!=a?z a F:z a;if(e||!i&&!a){print W;exit 65}}P=p=!z?H:z;if((o=O[$1])==p)next;gsub(Q,E,P);M=m=D P;gsub("/[^/]+$",B,M);if(!u[M]++)C("mkdir -p"I M Q);if(o){T=split(o,t,F);S=split(p,s,F);for(i=1;t[i]==s[i];i++);for(j=i;j<S;j++)r=r G H;for(j=i;j<T;j++)r=r t[j]F;r=h?o:r t[T];gsub(Q,E,r);C("ln"(h?I D:" -s"I)r"' '"m Q);next}O[$1]=p;C("cp -i"I P"' '"m Q)}

View File

@ -3,9 +3,8 @@
FROM alpine:3.16 as partial
# need this for acquire (.tar.xz files):
RUN apk add --no-cache xz
COPY --chmod=0755 acquire /usr/local/bin/acquire
COPY --chmod=0755 cosmocc /usr/local/bin/cosmocc
COPY --chmod=0755 quickconf /usr/local/bin/quickconf
COPY --chmod=0755 acquire cosmocc dedupe quickconf \
/usr/local/bin/
FROM partial as downloader
# this demonstrates how to use the `acquire` script.

31
notwa-util/dedupe Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env sh
: \
&& complain()
{ : \
&& printf '\033[1m%s\033[m: %s\n' >&2 dedupe "$*" \
;} \
\
&& { : \
&& [ -d /tmp ] \
|| { complain "/tmp must be mounted"; exit 72 ;} \
;} \
\
&& f="$(mktemp)" \
\
&& { : \
&& [ -s "$f" ] \
|| { : \
&& cat >"$f" <<'EOF'
BEGIN{if(!D){print U;exit 64}E=(Q="'")"\"'\""Q;H=(G=".")(F="/");I=" -- "Q;J="[^/]*/";sub(F"?$",F,D)gsub(Q,E,D)}function C(x){if(X=system(x))exit X}{if((p=$2)==B){print V;exit 65}for(r=z=i=d=B;d!=!i;i++){b=a=p;d=sub("/.*",B,a);sub(J,B,p);if(a==G G)e=!sub(J,B,z);else z=a==G?z:b!=a?z a F:z a;if(e||!i&&!a){print W;exit 65}}P=p=!z?H:z;if((o=O[$1])==p)next;gsub(Q,E,P);M=m=D P;gsub("/[^/]+$",B,M);if(!u[M]++)C("mkdir -p"I M Q);if(o){T=split(o,t,F);S=split(p,s,F);for(i=1;t[i]==s[i];i++);for(j=i;j<S;j++)r=r G H;for(j=i;j<T;j++)r=r t[j]F;r=h?o:r t[T];gsub(Q,E,r);C("ln"(h?I D:" -s"I)r"' '"m Q);next}O[$1]=p;C("cp -i"I P"' '"m Q)}
EOF
`##`;} \
;} \
\
&& h=0 && { [ "$1" != --hard ] || shift && h=1 ;} \
&& cd "$1" && shift && D="$1" && shift && t="$(mktemp)" \
&& { [ $# -eq 0 ] || sha256sum >"$t" "$@" ;} \
&& find ! -type d -exec sha256sum {} + >>"$t~" \
&& sort -k2, <"$t~" >>"$t" \
&& awk -v D="$D" -v h="$h" -F' ' -f "$f" <"$t" \
;