Skip to content

Commit 3411b44

Browse files
committed
edit_line and edit_xml attributes can now override immutable bit
The edit_line and edit_xml attributes of the files promise can now override the immutable bit. Ticket: ENT-10961, CFE-1840 Changelog: Commit Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 2818e3a commit 3411b44

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

cf-agent/files_edit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, c
127127
RecordNoChange(ctx, pp, a, "No edit changes to file '%s' need saving",
128128
ec->filename);
129129
}
130-
else if (SaveItemListAsFile(ec->file_start, ec->changes_filename, a, ec->new_line_mode))
130+
else if (SaveItemListAsFile(ctx, ec->file_start, ec->changes_filename, a, ec->new_line_mode))
131131
{
132132
RecordChange(ctx, pp, a, "Edited file '%s'", ec->filename);
133133
*result = PromiseResultUpdate(*result, PROMISE_RESULT_CHANGE);
@@ -150,7 +150,7 @@ void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, c
150150
ec->filename);
151151
}
152152
}
153-
else if (SaveXmlDocAsFile(ec->xmldoc, ec->changes_filename, a, ec->new_line_mode))
153+
else if (SaveXmlDocAsFile(ctx, ec->xmldoc, ec->changes_filename, a, ec->new_line_mode))
154154
{
155155
RecordChange(ctx, pp, a, "Edited xml file '%s'", ec->filename);
156156
*result = PromiseResultUpdate(*result, PROMISE_RESULT_CHANGE);
@@ -253,8 +253,8 @@ static bool SaveXmlCallback(const char *dest_filename, void *param,
253253

254254
/*********************************************************************/
255255

256-
bool SaveXmlDocAsFile(xmlDocPtr doc, const char *file, const Attributes *a, NewLineMode new_line_mode)
256+
bool SaveXmlDocAsFile(EvalContext *ctx, xmlDocPtr doc, const char *file, const Attributes *a, NewLineMode new_line_mode)
257257
{
258-
return SaveAsFile(&SaveXmlCallback, doc, file, a, new_line_mode);
258+
return SaveAsFile(ctx, &SaveXmlCallback, doc, file, a, new_line_mode);
259259
}
260260
#endif /* HAVE_LIBXML2 */

cf-agent/files_edit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void FinishEditContext(EvalContext *ctx, EditContext *ec,
6161

6262
#ifdef HAVE_LIBXML2
6363
bool LoadFileAsXmlDoc(xmlDocPtr *doc, const char *file, EditDefaults ed, bool only_checks);
64-
bool SaveXmlDocAsFile(xmlDocPtr doc, const char *file,
64+
bool SaveXmlDocAsFile(EvalContext *ctx, xmlDocPtr doc, const char *file,
6565
const Attributes *a, NewLineMode new_line_mode);
6666
#endif
6767

cf-agent/verify_files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx,
986986
edcontext->filename, message);
987987
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
988988
}
989-
else if (SaveAsFile(SaveBufferCallback, output_buffer,
989+
else if (SaveAsFile(ctx, SaveBufferCallback, output_buffer,
990990
edcontext->changes_filename, attr,
991991
edcontext->new_line_mode))
992992
{

libpromises/files_operators.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <files_repository.h>
4848
#include <files_lib.h>
4949
#include <buffer.h>
50+
#include <override_fsattrs.h>
5051

5152

5253
bool MoveObstruction(EvalContext *ctx, char *from, const Attributes *attr, const Promise *pp, PromiseResult *result)
@@ -149,7 +150,7 @@ bool MoveObstruction(EvalContext *ctx, char *from, const Attributes *attr, const
149150

150151
/*********************************************************************/
151152

152-
bool SaveAsFile(SaveCallbackFn callback, void *param, const char *file, const Attributes *a, NewLineMode new_line_mode)
153+
bool SaveAsFile(EvalContext *ctx, SaveCallbackFn callback, void *param, const char *file, const Attributes *a, NewLineMode new_line_mode)
153154
{
154155
assert(a != NULL);
155156
struct stat statbuf;
@@ -277,7 +278,8 @@ bool SaveAsFile(SaveCallbackFn callback, void *param, const char *file, const At
277278
unlink(backup);
278279
}
279280

280-
if (rename(new, BufferData(deref_file)) == -1)
281+
const bool override_immutable = EvalContextOverrideImmutableGet(ctx);
282+
if (!OverrideImmutableRename(new, BufferData(deref_file), override_immutable))
281283
{
282284
Log(LOG_LEVEL_ERR, "Can't rename '%s' to %s - so promised edits could not be moved into place. (rename: %s)",
283285
new, BufferData(pretty_file), GetErrorStr());
@@ -331,10 +333,10 @@ static bool SaveItemListCallback(const char *dest_filename, void *param, NewLine
331333

332334
/*********************************************************************/
333335

334-
bool SaveItemListAsFile(Item *liststart, const char *file, const Attributes *a, NewLineMode new_line_mode)
336+
bool SaveItemListAsFile(EvalContext *ctx, Item *liststart, const char *file, const Attributes *a, NewLineMode new_line_mode)
335337
{
336338
assert(a != NULL);
337-
return SaveAsFile(&SaveItemListCallback, liststart, file, a, new_line_mode);
339+
return SaveAsFile(ctx, &SaveItemListCallback, liststart, file, a, new_line_mode);
338340
}
339341

340342
// Some complex logic here to enable warnings of diffs to be given

libpromises/files_operators.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
bool MoveObstruction(EvalContext *ctx, char *from, const Attributes *attr, const Promise *pp, PromiseResult *result);
3232

3333
typedef bool (*SaveCallbackFn)(const char *dest_filename, void *param, NewLineMode new_line_mode);
34-
bool SaveAsFile(SaveCallbackFn callback, void *param, const char *file, const Attributes *a, NewLineMode new_line_mode);
35-
bool SaveItemListAsFile(Item *liststart, const char *file, const Attributes *a, NewLineMode new_line_mode);
34+
bool SaveAsFile(EvalContext *ctx, SaveCallbackFn callback, void *param, const char *file, const Attributes *a, NewLineMode new_line_mode);
35+
bool SaveItemListAsFile(EvalContext *ctx, Item *liststart, const char *file, const Attributes *a, NewLineMode new_line_mode);
3636

3737
bool CompareToFile(EvalContext *ctx, const Item *liststart, const char *file, const Attributes *a, const Promise *pp, PromiseResult *result);
3838

0 commit comments

Comments
 (0)