Skip to content

Commit 2050882

Browse files
committed
Touch attribute can now override immutable bit
The touch 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 7c3f4fa commit 2050882

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

cf-agent/verify_files_utils.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,15 +2484,16 @@ static PromiseResult TouchFile(EvalContext *ctx, char *path, const Attributes *a
24842484
PromiseResult result = PROMISE_RESULT_NOOP;
24852485
if (MakingChanges(ctx, pp, attr, &result, "update time stamps for '%s'", path))
24862486
{
2487-
if (utime(ToChangesPath(path), NULL) != -1)
2487+
bool override_immutable = EvalContextOverrideImmutableGet(ctx);
2488+
if (OverrideImmutableUtime(ToChangesPath(path), override_immutable, NULL))
24882489
{
24892490
RecordChange(ctx, pp, attr, "Touched (updated time stamps) for path '%s'", path);
24902491
result = PromiseResultUpdate(result, PROMISE_RESULT_CHANGE);
24912492
}
24922493
else
24932494
{
2494-
RecordFailure(ctx, pp, attr, "Touch '%s' failed to update timestamps. (utime: %s)",
2495-
path, GetErrorStr());
2495+
RecordFailure(ctx, pp, attr, "Touch '%s' failed to update timestamps",
2496+
path);
24962497
result = PromiseResultUpdate(result, PROMISE_RESULT_FAIL);
24972498
}
24982499
}

libpromises/override_fsattrs.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,26 @@ bool OverrideImmutableDelete(const char *filename, bool override)
202202

203203
return unlink(filename) == 0;
204204
}
205+
206+
bool OverrideImmutableUtime(
207+
const char *filename, bool override, const struct utimbuf *times)
208+
{
209+
assert(filename != NULL);
210+
211+
FSAttrsResult res;
212+
bool is_immutable;
213+
214+
TemporarilyClearImmutableBit(filename, override, &res, &is_immutable);
215+
216+
int ret = utime(filename, times);
217+
if (ret == -1)
218+
{
219+
Log(LOG_LEVEL_ERR,
220+
"Failed to update access and modification times of file '%s'",
221+
filename);
222+
}
223+
224+
ResetTemporarilyClearedImmutableBit(filename, override, res, is_immutable);
225+
226+
return ret == 0;
227+
}

libpromises/override_fsattrs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <stdbool.h>
2929
#include <stddef.h>
30+
#include <utime.h>
3031

3132
/**
3233
* @brief Creates a mutable copy of the original file
@@ -72,4 +73,15 @@ bool OverrideImmutableRename(
7273
*/
7374
bool OverrideImmutableDelete(const char *filename, bool override);
7475

76+
/**
77+
* @brief Temporarily clears the immutable bit and changes access and
78+
* modification times of the inode
79+
* @param filename Name of the file to touch
80+
* @param override Whether to actually do override
81+
* @param times Modification times (can be NULL)
82+
* @return false in case of failure
83+
*/
84+
bool OverrideImmutableUtime(
85+
const char *filename, bool override, const struct utimbuf *times);
86+
7587
#endif /* CFENGINE_OVERRIDE_FSATTRS_H */

0 commit comments

Comments
 (0)