Skip to content

Commit c947544

Browse files
committed
Properly handle promise locking with edit line
Fixed bug causing rendered files can result in erroneously empty files as a result of promise locking. Ticket: ENT-9980 Changelog: Commit Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 7fee030 commit c947544

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

cf-agent/files_edit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ EditContext *NewEditContext(char *filename, const Attributes *a)
103103
/*****************************************************************************/
104104

105105
void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, const Promise *pp,
106-
PromiseResult *result)
106+
PromiseResult *result, bool save_file)
107107
{
108-
if ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE))
108+
if (!save_file || ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE)))
109109
{
110110
// Failure or skipped. Don't update the file.
111111
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
729729
const Rlist *bundle_args,
730730
const Attributes *attr,
731731
EditContext *edcontext,
732-
bool file_exists)
732+
bool file_exists,
733+
bool *save_file)
733734
{
734735
assert(edcontext != NULL);
735736
assert(attr != NULL);
@@ -754,7 +755,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
754755
EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit);
755756
BundleResolve(ctx, bp);
756757

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

759760
EvalContextStackPopFrame(ctx);
760761

@@ -965,6 +966,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
965966
Rlist *args = NULL;
966967
char edit_bundle_name[CF_BUFSIZE], lockname[CF_BUFSIZE];
967968
CfLock thislock;
969+
bool save_file = true;
968970

969971
snprintf(lockname, CF_BUFSIZE - 1, "fileedit-%s", filename);
970972
thislock = AcquireLock(ctx, lockname, VUQNAME, CFSTARTTIME, a->transaction.ifelapsed, a->transaction.expireafter, pp, false);
@@ -1067,7 +1069,8 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
10671069
PromiseResult render_result = RenderTemplateCFEngine(ctx, pp,
10681070
args, a,
10691071
edcontext,
1070-
file_exists);
1072+
file_exists,
1073+
&save_file);
10711074
result = PromiseResultUpdate(result, render_result);
10721075
}
10731076
}
@@ -1099,7 +1102,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
10991102
}
11001103

11011104
exit:
1102-
FinishEditContext(ctx, edcontext, a, pp, &result);
1105+
FinishEditContext(ctx, edcontext, a, pp, &result, save_file);
11031106
YieldCurrentLock(thislock);
11041107
if (result == PROMISE_RESULT_CHANGE)
11051108
{

0 commit comments

Comments
 (0)