fileset

git mirror of https://ccx.te2000.cz/bzr/fileset
git clone https://ccx.te2000.cz/git/fileset
Log | Files | Refs | README

commit 511842b72a9e6ec0a7b4e095e7e3204a8936158c
parent 083bb9aa8a5acab265308993f69c841eaf8f59c8
Author: Jan Pobrislo <ccx@wpr.cz>
Date:   Wed,  9 Dec 2015 09:05:43 +0100

WIP change to check/do style of execution
Diffstat:
Mbin/fileset.awk | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mbin/fileset_inc.sh | 218++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
2 files changed, 223 insertions(+), 66 deletions(-)

diff --git a/bin/fileset.awk b/bin/fileset.awk @@ -8,6 +8,8 @@ BEGIN { # clean the filter file printf "" >ENVIRON["RSFILTER"] } + + func_num=0 } function escaped(str) { @@ -115,12 +117,12 @@ function process_statement() { } if(cchar == "o") { - print_d("chown "crest" "fname) + print_i("o "crest) continue } if(cchar == "m") { - print_d("chmod "crest" "fname) + print_i("m "crest) continue } @@ -162,20 +164,35 @@ function process_statement() { # last line, if not already present printf_fmt = crest ~ /n/ || (crest !~ /N/ && content !~ /\n$/) ? \ "%s\\n" : "%s" - printf_redir = (crest ~ /a/ ? ">>" : ">") fname - # print_i("content="quoted(content)) - # print_d("printf "printf_redir" '"printf_fmt"' \"$content\"") - print_d("printf "printf_redir" '"printf_fmt"' "quoted(content)) + + func_name = "f_" func_name++ + funcs[func_name] = "\tprintf '"printf_fmt"' "quoted(content) + + if(crest ~ /a/) { + print_b("if ! $check_only; then") + print_i(func_name " >>" fname) + print_e("fi") + } else { + print_i("c " func_name) + } continue } # binary, base64; decodes the arguments and changes file content if(cchar ~ /[bB]/) { - shellfunc("f 1", crest) + shellfunc("f", crest) content = get_argument(cchar == "B") - base64_redir = (crest ~ /a/ ? ">>" : ">") fname - print_d("base64 "base64_redir" <<<"quoted(content)) + func_name = "f_" func_name++ + funcs[func_name] = "\tbase64 <<<"quoted(content) + + if(crest ~ /a/) { + print_b("if ! $check_only; then") + print_i(func_name " >>" fname) + print_e("fi") + } else { + print_i("c " func_name) + } continue } @@ -186,25 +203,34 @@ function process_statement() { if(crest ~ /c/) { shellfunc("f", crest) } else { - shellfunc("f 1", crest) + shellfunc("req type_or_missing f", crest) } print_d("{ "statement"\n} <"fname" >"fname".tmp.$$") print_d("cat >"fname" "fname".tmp.$$") + print_i("c cat "fname".tmp.$$") print_d("rm "fname".tmp.$$") # use file as Input } else if(crest ~ /i/) { + print_b("if ! $check_only; then") print_d("{ "statement"\n} <"fname) + print_e("fi") # use file as Output } else if(crest ~ /o/) { - shellfunc("f 1", crest) + shellfunc("req type_or_missing f", crest) + print_b("if ! $check_only; then") print_d("{ "statement"\n} >"fname) + print_e("fi") # Append to file } else if(crest ~ /a/) { - shellfunc("f 1", crest) + shellfunc("req type_or_missing f", crest) + print_b("if ! $check_only; then") print_d("{ "statement"\n} >>"fname) + print_e("fi") # do nothing special with file } else { + print_b("if ! $check_only; then") print_d("{ "statement"\n}") + print_e("fi") } statement = "" continue @@ -231,6 +257,25 @@ function parse_line(line) { } } +function print_functions() { + for( func_name in funcs ) { + print func_name "(){\n" funcs[func_name] "\n}\n" + } +} + +BEGIN { + print_b("check_main() {" +} + { parse_line($0) } -END { process_statement() } +END { + process_statement() + print_e("}") + print_b("check_main() {") + print_i("true") + print_e("}") + print + print_functions() + print_i "req main" +} diff --git a/bin/fileset_inc.sh b/bin/fileset_inc.sh @@ -18,85 +18,197 @@ flags() { } # TODO: search $PATH -if test -x /bin/readlink -o -x /usr/bin/readlink; then - has_readlink=true -else - has_readlink=false +if ! test -x /bin/readlink -o -x /usr/bin/readlink; then + # Hackity hack. If you know of better way, let me know. + if test -x /bin/rsync -o -x /usr/bin/rsync; then + readlink() { + rsync -l8 --list-only "$1" | sed 's|^.* ->||' + } + else + readlink() { + ls -ld "$1" | sed 's|^.* ->||' + } + fi fi -rm_flags() { - if $f_f; then - rm -f$1 "$fname" || exit $? - elif test -n "$1"; then - rm -$1 "$fname" || exit $? - else - rm "$fname" || exit $? +: ${check_only:=false} + +req() { + if ! "check_$@"; then + if $check_only; then + die "check failed on $filename: $*" + else + "do_$1" || die "action failed on $filename: $*" + check_only=true + req "$@" + check_only=false + fi fi } -mkdir_p() { - if $f_p; then - mkdir -p "$(dirname "$fname")" || exit $? - fi + +check_p() { + test -d "$(dirname "$filename")" } -l() { - if test -L "$fname"; then - if $has_readlink && test x"$(readlink "$fname")" = x"$1"; then - return - elif $f_f; then - rm_flags +do_p() { + mkdir -p "$(dirname "$fname")" || exit $? +} + + +check_type_or_missing() { + test '!' -e "$fname" -o "-$1" "$fname" +} + +do_type_or_missing() { + if $f_r; then + if $f_f; then + rm -rf "$fname" || exit $? + else + rm -r "$fname" || exit $? fi - elif test -e "$fname"; then - if $f_bang; then - rm_flags r + elif test -d "$fname"; then + rmdir "$fname" + else + if $f_f; then + rm -f "$fname" || exit $? else - die "already present but not a symbolic link: $fname" + rm "$fname" || exit $? fi fi - mkdir_p +} + + +do_l() { + if test -L "$fname"; then + rm "$fname" + elif test -e "$fname"; then + die "already present but not a symbolic link: $fname" + fi ln -s "$1" "$fname" || exit $? } -r() { - if test -e "$fname" -o -L "$fname"; then - if $f_r; then - rm_flags r - else - rm_flags - fi +check_l() { + if $f_p; then + req p + fi + if $f_bang; then + req type_or_missing L fi + test -L "$fname" && test x"$(readlink "$fname")" = x"$1" } -f() { +l() { + req l "$@" +} + + +check_r() { + test -e "$fname" -o -L "$fname" +} + +do_r() { + do_type_or_missing +} + +r() { + req r "$@" +} + + +check_m() { + test -e "$fname" -o -L "$fname" && \ + test 0 -lt $(find "$fname" -prune -perm "$1" | wc -l) +} + +do_m() { + chmod "$1" "$fname" +} + +m() { + req m "$@" +} + + +check_o() { + test -e "$fname" -o -L "$fname" || return 1 + case "$1" in + (*:*) test 0 -lt $( \ + find "$fname" -prune -user "${1%:*}" -group "${1#*:}" | wc -l \ + ) + return $?;; + (*) test 0 -lt $(find "$fname" -prune -user "$1" | wc -l) + return $?;; + esac +} + +do_o() { + chown "$1" "$fname" +} + +o() { + req o "$@" +} + + +check_f() { if test -f "$fname"; then - return - elif test -e "$fname" -o -L "$fname"; then - if $f_bang; then - rm_flags r - else - die "already present but not a file: $fname" - fi + return 0 fi - mkdir_p - if test -z "$1"; then - touch "$fname" || exit $? + if $f_p; then + req p fi + if $f_bang; then + req type_or_missing f + elif test -e "$fname" -o -L "$fname"; then + die "already present but not a file: $fname" + fi + return 1 } -d() { +do_f() { + touch "$fname" +} + +f() { + req f "$@" +} + + +check_d() { if test -d "$fname"; then - return + return 0 + fi + if $f_bang; then + req type_or_missing f elif test -e "$fname" -o -L "$fname"; then - if $f_bang; then - rm_flags - else - die "already present but not a directory: $fname" - fi + die "already present but not a directory: $fname" fi + return 1 +} + +do_d() { if $f_p; then - mkdir -p "$fname" || exit $? + mkdir -p "$fname" else - mkdir "$fname" || exit $? + mkdir "$fname" fi } + +d() { + req d "$@" +} + + +check_c() { + req f + "$@" | cmp -s - "$fname" +} + +do_c() { + "$@" > "$fname" +} + +c() { + req c "$@" +}