From a9a359d54239c117ae46d89aa59ad6f64be986a2 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 6 Mar 2018 07:33:22 +0100 Subject: [PATCH] Add pre-exists state to __file (#641) This allows checking whether a file exists, but erroring out instead of creating the file if it does not (or is not a regular file). --- cdist/conf/type/__file/gencode-local | 14 ++++++++++++++ cdist/conf/type/__file/gencode-remote | 2 +- cdist/conf/type/__file/man.rst | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cdist/conf/type/__file/gencode-local b/cdist/conf/type/__file/gencode-local index aec4e43e..15a9ee0e 100755 --- a/cdist/conf/type/__file/gencode-local +++ b/cdist/conf/type/__file/gencode-local @@ -25,6 +25,20 @@ type="$(cat "$__object/explorer/type")" [ "$state_should" = "exists" -a "$type" = "file" ] && exit 0 # nothing to do +if [ "$state_should" = "pre-exists" ]; then + if [ -f "$__object/parameter/source" ]; then + echo "--source cannot be used with --state pre-exists" + exit 1 + fi + + if [ "$type" = "file" ]; then + exit 0 # nothing to do + else + echo "File \"$destination\" does not exist" + exit 1 + fi +fi + upload_file= create_file= if [ "$state_should" = "present" -o "$state_should" = "exists" ]; then diff --git a/cdist/conf/type/__file/gencode-remote b/cdist/conf/type/__file/gencode-remote index fe1e2212..c90be0be 100755 --- a/cdist/conf/type/__file/gencode-remote +++ b/cdist/conf/type/__file/gencode-remote @@ -59,7 +59,7 @@ set_mode() { set_attributes= case "$state_should" in - present|exists) + present|exists|pre-exists) # Note: Mode - needs to happen last as a chown/chgrp can alter mode by # clearing S_ISUID and S_ISGID bits (see chown(2)) for attribute in group owner mode; do diff --git a/cdist/conf/type/__file/man.rst b/cdist/conf/type/__file/man.rst index 7d9b413b..d06fc275 100644 --- a/cdist/conf/type/__file/man.rst +++ b/cdist/conf/type/__file/man.rst @@ -23,6 +23,10 @@ symlink directory replace it with the source file +One exception is that when state is pre-exists, an error is raised if +the file would have been created otherwise (e.g. it is not present or +not a regular file). + In any case, make sure that the file attributes are as specified. @@ -41,6 +45,9 @@ state the file does not exist exists the file from source but only if it doesn't already exist + pre-exists + check that the file exists and is a regular file, but do not + create or modify it group Group to chgrp to. @@ -93,6 +100,8 @@ EXAMPLES __file /home/frodo/.bashrc --source "/etc/skel/.bashrc" \ --state exists \ --owner frodo --mode 0600 + # Check that the file is present, show an error when it is not + __file /etc/somefile --state pre-exists # Take file content from stdin __file /tmp/whatever --owner root --group root --mode 644 --source - << DONE Here goes the content for /tmp/whatever