BARKS IN THE WIND: vdiff fixes

Posted on 2018-01-22 11:23 technical, vtools

original vdiff posted by asciilifeform suffers from a bug warned against in the man page,

# NOTE: If using a pipe, co-process, or socket to getline, or from print
# or printf within a loop, you must use close() to create new instances
# of the command or socket. AWK does not automatically close pipes,
# sockets, or co-processes when they return EOF.

sha512sum pipe is opened as many times as there are files in the diffed folders, so at some point, on a large folder, it's going to hit a "too many files open" system exception. the solution is of course to ensure that sha512sum pipe is closed after each execution:

#!/bin/sh
diff -uNr $1 $2 | awk 'm = /^(---|\+\+\+)/{cmd="sha512sum \"" $2 "\" 2>/dev/null ";s=cmd| getline x; if (s) { split(x, a, " "); o = a[1]; } else {o = "false";} close(cmd); print $1 " " $2 " " o} !m { print $0 }'

we can verify that the fix results in correct behavior by running the following,

mkdir -p a b; echo foo|tee b/{0..100}
ulimit -n 15
vdiff a b

(removing close(cmd) from above will simulate original behavior, and on my systems results in gawk: cmd. line:1: (FILENAME=- FNR=28) fatal: cannot open pipe `sha512sum "b/12" 2>/dev/null ' (Too many open files))

Post a comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>