Skip to content

Commit 0847f0f

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

File tree

2 files changed

+69
-40
lines changed

2 files changed

+69
-40
lines changed

libpromises/override_fsattrs.c

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -86,74 +86,92 @@ bool OverrideImmutableCommit(
8686
return false;
8787
}
8888

89+
return OverrideImmutableRename(copy, orig, override);
90+
}
91+
92+
bool OverrideImmutableRename(
93+
const char *old_filename, const char *new_filename, bool override)
94+
{
95+
assert(old_filename != NULL);
96+
assert(new_filename != NULL);
97+
8998
/* If the operations on the file system attributes fails for any reason,
9099
* we can still proceed to try to replace the original file. We will only
91100
* log an actual error in case of an unexpected failure (i.e., when
92101
* FS_ATTRS_FAILURE is returned). Other failures will be logged as verbose
93102
* messages because they can be useful, but are be quite verbose. */
94103

104+
FSAttrsResult res;
95105
bool is_immutable;
96-
FSAttrsResult res = FSAttrsGetImmutableFlag(orig, &is_immutable);
97-
if (res == FS_ATTRS_SUCCESS)
106+
107+
if (override)
98108
{
99-
if (is_immutable)
109+
res = FSAttrsGetImmutableFlag(new_filename, &is_immutable);
110+
if (res == FS_ATTRS_SUCCESS)
100111
{
101-
res = FSAttrsUpdateImmutableFlag(orig, false);
102-
if (res == FS_ATTRS_SUCCESS)
112+
if (is_immutable)
103113
{
104-
Log(LOG_LEVEL_VERBOSE,
105-
"Temporarily cleared immutable bit for file '%s'",
106-
orig);
114+
res = FSAttrsUpdateImmutableFlag(new_filename, false);
115+
if (res == FS_ATTRS_SUCCESS)
116+
{
117+
Log(LOG_LEVEL_VERBOSE,
118+
"Temporarily cleared immutable bit for file '%s'",
119+
new_filename);
120+
}
121+
else
122+
{
123+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR
124+
: LOG_LEVEL_VERBOSE,
125+
"Failed to temporarily clear immutable bit for file '%s': %s",
126+
new_filename,
127+
FSAttrsErrorCodeToString(res));
128+
}
107129
}
108130
else
109131
{
110-
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR
111-
: LOG_LEVEL_VERBOSE,
112-
"Failed to temporarily clear immutable bit for file '%s': %s",
113-
orig,
114-
FSAttrsErrorCodeToString(res));
132+
Log(LOG_LEVEL_DEBUG,
133+
"The immutable bit is not set on file '%s'",
134+
new_filename);
115135
}
116136
}
117137
else
118138
{
119-
Log(LOG_LEVEL_DEBUG,
120-
"The immutable bit is not set on file '%s'",
121-
orig);
139+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE,
140+
"Failed to get immutable bit from file '%s': %s",
141+
new_filename,
142+
FSAttrsErrorCodeToString(res));
122143
}
123144
}
124-
else
125-
{
126-
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE,
127-
"Failed to get immutable bit from file '%s': %s",
128-
orig,
129-
FSAttrsErrorCodeToString(res));
130-
}
131145

132-
if (rename(copy, orig) == -1)
146+
if (rename(old_filename, new_filename) == -1)
133147
{
134148
Log(LOG_LEVEL_ERR,
135149
"Failed to replace original file '%s' with copy '%s'",
136-
orig,
137-
copy);
138-
unlink(copy);
150+
new_filename,
151+
old_filename);
152+
unlink(old_filename);
139153
return false;
140154
}
141155

142-
if ((res == FS_ATTRS_SUCCESS) && is_immutable)
156+
if (override)
143157
{
144-
res = FSAttrsUpdateImmutableFlag(orig, true);
145-
if (res == FS_ATTRS_SUCCESS)
146-
{
147-
Log(LOG_LEVEL_VERBOSE,
148-
"Reset immutable bit after temporarily clearing it from file '%s'",
149-
orig);
150-
}
151-
else
158+
if ((res == FS_ATTRS_SUCCESS) && is_immutable)
152159
{
153-
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE,
154-
"Failed to reset immutable bit after temporarily clearing it from file '%s': %s",
155-
orig,
156-
FSAttrsErrorCodeToString(res));
160+
res = FSAttrsUpdateImmutableFlag(new_filename, true);
161+
if (res == FS_ATTRS_SUCCESS)
162+
{
163+
Log(LOG_LEVEL_VERBOSE,
164+
"Reset immutable bit after temporarily clearing it from file '%s'",
165+
new_filename);
166+
}
167+
else
168+
{
169+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR
170+
: LOG_LEVEL_VERBOSE,
171+
"Failed to reset immutable bit after temporarily clearing it from file '%s': %s",
172+
new_filename,
173+
FSAttrsErrorCodeToString(res));
174+
}
157175
}
158176
}
159177

libpromises/override_fsattrs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,15 @@ bool OverrideImmutableBegin(
5353
bool OverrideImmutableCommit(
5454
const char *orig, const char *copy, bool override, bool abort);
5555

56+
/**
57+
* @brief Temporarily clears the immutable bit of the old file and renames the
58+
* new to the old
59+
* @param old_filename Filename of the old file
60+
* @param new_filename Filename of the new file
61+
* @param override Whether to actually do override
62+
* @return false in case of failure
63+
*/
64+
bool OverrideImmutableRename(
65+
const char *old_filename, const char *new_filename, bool override);
66+
5667
#endif /* CFENGINE_OVERRIDE_FSATTRS_H */

0 commit comments

Comments
 (0)