diff --git a/cf-agent/files_edit.c b/cf-agent/files_edit.c index 412e63b0ac..6833efc2d3 100644 --- a/cf-agent/files_edit.c +++ b/cf-agent/files_edit.c @@ -33,6 +33,7 @@ #include #include #include +#include /*****************************************************************************/ @@ -102,9 +103,9 @@ EditContext *NewEditContext(char *filename, const Attributes *a) /*****************************************************************************/ void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, const Promise *pp, - PromiseResult *result) + PromiseResult *result, bool save_file) { - if ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE)) + if (!save_file || ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE))) { // Failure or skipped. Don't update the file. goto end; diff --git a/cf-agent/files_edit.h b/cf-agent/files_edit.h index 0b7dd3b6f3..6bb8551b3e 100644 --- a/cf-agent/files_edit.h +++ b/cf-agent/files_edit.h @@ -57,7 +57,7 @@ typedef struct EditContext *NewEditContext(char *filename, const Attributes *a); void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, const Promise *pp, - PromiseResult *result); + PromiseResult *result, bool save_file); #ifdef HAVE_LIBXML2 bool LoadFileAsXmlDoc(xmlDocPtr *doc, const char *file, EditDefaults ed, bool only_checks); diff --git a/cf-agent/verify_files.c b/cf-agent/verify_files.c index bc65c046a0..c2ca31de6f 100644 --- a/cf-agent/verify_files.c +++ b/cf-agent/verify_files.c @@ -59,6 +59,7 @@ #include #include #include /* PrepareChangesChroot(), RecordFileChangedInChroot() */ +#include static PromiseResult FindFilePromiserObjects(EvalContext *ctx, const Promise *pp); static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promise *pp); @@ -735,7 +736,8 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx, const Rlist *bundle_args, const Attributes *attr, EditContext *edcontext, - bool file_exists) + bool file_exists, + bool *save_file) { assert(edcontext != NULL); assert(attr != NULL); @@ -748,7 +750,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx, { if (!file_exists && !CfCreateFile(ctx, edcontext->changes_filename, pp, attr, &result)) - { + { RecordFailure(ctx, pp, attr, "Failed to create file '%s' for rendering cfengine template '%s'", edcontext->filename, attr->edit_template); @@ -760,7 +762,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx, EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit); BundleResolve(ctx, bp); - ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext); + *save_file = ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext); EvalContextStackPopFrame(ctx); @@ -860,7 +862,7 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx, if (!file_exists && !CfCreateFile(ctx, edcontext->changes_filename, pp, attr, &result)) - { + { RecordFailure(ctx, pp, attr, "Failed to create file '%s' for rendering mustache template '%s'", edcontext->filename, message); @@ -971,6 +973,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename, Rlist *args = NULL; char edit_bundle_name[CF_BUFSIZE], lockname[CF_BUFSIZE]; CfLock thislock; + bool save_file = true; snprintf(lockname, CF_BUFSIZE - 1, "fileedit-%s", filename); thislock = AcquireLock(ctx, lockname, VUQNAME, CFSTARTTIME, a->transaction.ifelapsed, a->transaction.expireafter, pp, false); @@ -1073,7 +1076,8 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename, PromiseResult render_result = RenderTemplateCFEngine(ctx, pp, args, a, edcontext, - file_exists); + file_exists, + &save_file); result = PromiseResultUpdate(result, render_result); } } @@ -1105,7 +1109,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename, } exit: - FinishEditContext(ctx, edcontext, a, pp, &result); + FinishEditContext(ctx, edcontext, a, pp, &result, save_file); YieldCurrentLock(thislock); if (result == PROMISE_RESULT_CHANGE) {