Skip to content

Commit 0f662bf

Browse files
authored
Merge pull request #5815 from larsewi/render
ENT-9980: Properly handle promise locking with edit line
2 parents a011b1f + c947544 commit 0f662bf

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

cf-agent/files_edit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <files_editxml.h>
3434
#include <item_lib.h>
3535
#include <policy.h>
36+
#include <cf3.defs.h>
3637

3738
/*****************************************************************************/
3839

@@ -102,9 +103,9 @@ EditContext *NewEditContext(char *filename, const Attributes *a)
102103
/*****************************************************************************/
103104

104105
void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, const Promise *pp,
105-
PromiseResult *result)
106+
PromiseResult *result, bool save_file)
106107
{
107-
if ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE))
108+
if (!save_file || ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE)))
108109
{
109110
// Failure or skipped. Don't update the file.
110111
goto end;

cf-agent/files_edit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ typedef struct
5757
EditContext *NewEditContext(char *filename, const Attributes *a);
5858
void FinishEditContext(EvalContext *ctx, EditContext *ec,
5959
const Attributes *a, const Promise *pp,
60-
PromiseResult *result);
60+
PromiseResult *result, bool save_file);
6161

6262
#ifdef HAVE_LIBXML2
6363
bool LoadFileAsXmlDoc(xmlDocPtr *doc, const char *file, EditDefaults ed, bool only_checks);

cf-agent/verify_files.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <known_dirs.h>
6060
#include <evalfunction.h>
6161
#include <changes_chroot.h> /* PrepareChangesChroot(), RecordFileChangedInChroot() */
62+
#include <cf3.defs.h>
6263

6364
static PromiseResult FindFilePromiserObjects(EvalContext *ctx, const Promise *pp);
6465
static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promise *pp);
@@ -728,7 +729,8 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
728729
const Rlist *bundle_args,
729730
const Attributes *attr,
730731
EditContext *edcontext,
731-
bool file_exists)
732+
bool file_exists,
733+
bool *save_file)
732734
{
733735
assert(edcontext != NULL);
734736
assert(attr != NULL);
@@ -741,7 +743,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
741743
{
742744
if (!file_exists && !CfCreateFile(ctx, edcontext->changes_filename,
743745
pp, attr, &result))
744-
{
746+
{
745747
RecordFailure(ctx, pp, attr,
746748
"Failed to create file '%s' for rendering cfengine template '%s'",
747749
edcontext->filename, attr->edit_template);
@@ -753,7 +755,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
753755
EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit);
754756
BundleResolve(ctx, bp);
755757

756-
ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext);
758+
*save_file = ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext);
757759

758760
EvalContextStackPopFrame(ctx);
759761

@@ -853,7 +855,7 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx,
853855
if (!file_exists && !CfCreateFile(ctx,
854856
edcontext->changes_filename,
855857
pp, attr, &result))
856-
{
858+
{
857859
RecordFailure(ctx, pp, attr,
858860
"Failed to create file '%s' for rendering mustache template '%s'",
859861
edcontext->filename, message);
@@ -964,6 +966,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
964966
Rlist *args = NULL;
965967
char edit_bundle_name[CF_BUFSIZE], lockname[CF_BUFSIZE];
966968
CfLock thislock;
969+
bool save_file = true;
967970

968971
snprintf(lockname, CF_BUFSIZE - 1, "fileedit-%s", filename);
969972
thislock = AcquireLock(ctx, lockname, VUQNAME, CFSTARTTIME, a->transaction.ifelapsed, a->transaction.expireafter, pp, false);
@@ -1066,7 +1069,8 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
10661069
PromiseResult render_result = RenderTemplateCFEngine(ctx, pp,
10671070
args, a,
10681071
edcontext,
1069-
file_exists);
1072+
file_exists,
1073+
&save_file);
10701074
result = PromiseResultUpdate(result, render_result);
10711075
}
10721076
}
@@ -1098,7 +1102,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
10981102
}
10991103

11001104
exit:
1101-
FinishEditContext(ctx, edcontext, a, pp, &result);
1105+
FinishEditContext(ctx, edcontext, a, pp, &result, save_file);
11021106
YieldCurrentLock(thislock);
11031107
if (result == PROMISE_RESULT_CHANGE)
11041108
{

0 commit comments

Comments
 (0)