Skip to content

Commit b46f991

Browse files
committed
Added function to override immutable bit and delete file
Ticket: ENT-10961, CFE-1840 Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 66f78ef commit b46f991

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

libpromises/override_fsattrs.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,50 @@ bool OverrideImmutableRename(
177177

178178
return true;
179179
}
180+
181+
bool OverrideImmutableDelete(const char *filename, bool override)
182+
{
183+
assert(filename != NULL);
184+
185+
bool is_immutable = false;
186+
if (override)
187+
{
188+
FSAttrsResult res = FSAttrsGetImmutableFlag(filename, &is_immutable);
189+
if (res == FS_ATTRS_SUCCESS)
190+
{
191+
if (is_immutable)
192+
{
193+
res = FSAttrsUpdateImmutableFlag(filename, false);
194+
if (res == FS_ATTRS_SUCCESS)
195+
{
196+
Log(LOG_LEVEL_VERBOSE,
197+
"Cleared immutable bit for file '%s'",
198+
filename);
199+
}
200+
else
201+
{
202+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR
203+
: LOG_LEVEL_VERBOSE,
204+
"Failed to clear immutable bit for file '%s': %s",
205+
filename,
206+
FSAttrsErrorCodeToString(res));
207+
}
208+
}
209+
else
210+
{
211+
Log(LOG_LEVEL_DEBUG,
212+
"The immutable bit is not set on file '%s'",
213+
filename);
214+
}
215+
}
216+
else
217+
{
218+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE,
219+
"Failed to get immutable bit from file '%s': %s",
220+
filename,
221+
FSAttrsErrorCodeToString(res));
222+
}
223+
}
224+
225+
return unlink(filename) == 0;
226+
}

libpromises/override_fsattrs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@ bool OverrideImmutableCommit(
6464
bool OverrideImmutableRename(
6565
const char *old_filename, const char *new_filename, bool override);
6666

67+
/**
68+
* @brief Delete immutable file
69+
* @param filename Name of the file to delete
70+
* @param override Whether to actually do override
71+
* @return false in case of failure
72+
*/
73+
bool OverrideImmutableDelete(const char *filename, bool override);
74+
6775
#endif /* CFENGINE_OVERRIDE_FSATTRS_H */

0 commit comments

Comments
 (0)