Skip to content

Commit bdd1918

Browse files
committed
Content attribute can now override immutable bit
The content attribute 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 501244c commit bdd1918

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

cf-agent/verify_files.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@
6060
#include <evalfunction.h>
6161
#include <changes_chroot.h> /* PrepareChangesChroot(), RecordFileChangedInChroot() */
6262
#include <fsattrs.h>
63+
#include <override_fsattrs.h>
6364

6465
static PromiseResult FindFilePromiserObjects(EvalContext *ctx, const Promise *pp);
6566
static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promise *pp);
6667
static PromiseResult WriteContentFromString(EvalContext *ctx, const char *path, const Attributes *attr,
67-
const Promise *pp);
68+
const Promise *pp, bool override_immutable);
6869

6970
/*****************************************************************************/
7071

@@ -401,6 +402,11 @@ static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promi
401402
}
402403
}
403404

405+
/* If we encounter any promises to mutate the file and the immutable
406+
* attribute in body fsattrs is "true", we will override the immutable bit
407+
* by temporarily clearing it when ever needed. */
408+
const bool override_immutable = a.havefsattrs && a.fsattrs.haveimmutable && a.fsattrs.immutable && is_immutable;
409+
404410
if (lstat(changes_path, &oslb) == -1) /* Careful if the object is a link */
405411
{
406412
if ((a.create) || (a.touch))
@@ -610,7 +616,7 @@ static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promi
610616
Log(LOG_LEVEL_VERBOSE, "Replacing '%s' with content '%s'",
611617
path, a.content);
612618

613-
PromiseResult render_result = WriteContentFromString(ctx, path, &a, pp);
619+
PromiseResult render_result = WriteContentFromString(ctx, path, &a, pp, override_immutable);
614620
result = PromiseResultUpdate(result, render_result);
615621

616622
goto exit;
@@ -759,7 +765,7 @@ static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promi
759765
/*****************************************************************************/
760766

761767
static PromiseResult WriteContentFromString(EvalContext *ctx, const char *path, const Attributes *attr,
762-
const Promise *pp)
768+
const Promise *pp, bool override_immutable)
763769
{
764770
assert(path != NULL);
765771
assert(attr != NULL);
@@ -794,30 +800,45 @@ static PromiseResult WriteContentFromString(EvalContext *ctx, const char *path,
794800
return result;
795801
}
796802

797-
FILE *f = safe_fopen(changes_path, "w");
803+
char override_path[PATH_MAX];
804+
if (!OverrideImmutableBegin(changes_path, override_path, sizeof(override_path), override_immutable))
805+
{
806+
RecordFailure(ctx, pp, attr, "Failed to override immutable bit on file '%s'", changes_path);
807+
return PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
808+
}
809+
810+
FILE *f = safe_fopen(override_path, "w");
798811
if (f == NULL)
799812
{
800813
RecordFailure(ctx, pp, attr, "Cannot open file '%s' for writing", path);
814+
OverrideImmutableAbort(changes_path, override_path, override_immutable, true);
801815
return PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
802816
}
803817

818+
bool override_abort = false;
804819
Writer *w = FileWriter(f);
805820
if (WriterWriteLen(w, attr->content, bytes_to_write) == bytes_to_write )
806821
{
807822
RecordChange(ctx, pp, attr,
808823
"Updated file '%s' with content '%s'",
809824
path, attr->content);
810-
811825
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
812826
}
813827
else
814828
{
815829
RecordFailure(ctx, pp, attr,
816830
"Failed to update file '%s' with content '%s'",
817831
path, attr->content);
832+
override_abort = true;
818833
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
819834
}
820835
WriterClose(w);
836+
837+
if (!OverrideImmutableCommit(changes_path, override_path, override_immutable, override_abort))
838+
{
839+
RecordFailure(ctx, pp, attr, "Failed to override immutable bit on file '%s'", changes_path);
840+
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
841+
}
821842
}
822843

823844
return result;

0 commit comments

Comments
 (0)