Skip to content

Commit 4a1da27

Browse files
committed
Made functions for temporarily clear/reset immutable bit public
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
1 parent 9446931 commit 4a1da27

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

libpromises/override_fsattrs.c

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <override_fsattrs.h>
22
#include <platform.h>
3-
#include <fsattrs.h>
43
#include <logging.h>
54
#include <stdlib.h>
65
#include <files_copy.h>
@@ -89,36 +88,33 @@ bool OverrideImmutableCommit(
8988
return OverrideImmutableRename(copy, orig, override);
9089
}
9190

92-
static void TemporarilyClearImmutableBit(
93-
const char *filename,
94-
bool override,
95-
FSAttrsResult *res,
96-
bool *is_immutable)
91+
FSAttrsResult TemporarilyClearImmutableBit(
92+
const char *filename, bool override, bool *was_immutable)
9793
{
9894
if (!override)
9995
{
100-
return;
96+
return FS_ATTRS_FAILURE;
10197
}
10298

103-
*res = FSAttrsGetImmutableFlag(filename, is_immutable);
104-
if (*res == FS_ATTRS_SUCCESS)
99+
FSAttrsResult res = FSAttrsGetImmutableFlag(filename, was_immutable);
100+
if (res == FS_ATTRS_SUCCESS)
105101
{
106-
if (*is_immutable)
102+
if (*was_immutable)
107103
{
108-
*res = FSAttrsUpdateImmutableFlag(filename, false);
109-
if (*res == FS_ATTRS_SUCCESS)
104+
res = FSAttrsUpdateImmutableFlag(filename, false);
105+
if (res == FS_ATTRS_SUCCESS)
110106
{
111107
Log(LOG_LEVEL_VERBOSE,
112108
"Temporarily cleared immutable bit for file '%s'",
113109
filename);
114110
}
115111
else
116112
{
117-
Log((*res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR
118-
: LOG_LEVEL_VERBOSE,
113+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR
114+
: LOG_LEVEL_VERBOSE,
119115
"Failed to temporarily clear immutable bit for file '%s': %s",
120116
filename,
121-
FSAttrsErrorCodeToString(*res));
117+
FSAttrsErrorCodeToString(res));
122118
}
123119
}
124120
else
@@ -130,22 +126,24 @@ static void TemporarilyClearImmutableBit(
130126
}
131127
else
132128
{
133-
Log((*res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE,
129+
Log((res == FS_ATTRS_FAILURE) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE,
134130
"Failed to get immutable bit from file '%s': %s",
135131
filename,
136-
FSAttrsErrorCodeToString(*res));
132+
FSAttrsErrorCodeToString(res));
137133
}
134+
135+
return res;
138136
}
139137

140-
static void ResetTemporarilyClearedImmutableBit(
141-
const char *filename, bool override, FSAttrsResult res, bool is_immutable)
138+
void ResetTemporarilyClearedImmutableBit(
139+
const char *filename, bool override, FSAttrsResult res, bool was_immutable)
142140
{
143141
if (!override)
144142
{
145143
return;
146144
}
147145

148-
if ((res == FS_ATTRS_SUCCESS) && is_immutable)
146+
if ((res == FS_ATTRS_SUCCESS) && was_immutable)
149147
{
150148
res = FSAttrsUpdateImmutableFlag(filename, true);
151149
if (res == FS_ATTRS_SUCCESS)
@@ -170,10 +168,9 @@ bool OverrideImmutableRename(
170168
assert(old_filename != NULL);
171169
assert(new_filename != NULL);
172170

173-
FSAttrsResult res;
174171
bool is_immutable;
175-
176-
TemporarilyClearImmutableBit(new_filename, override, &res, &is_immutable);
172+
FSAttrsResult res =
173+
TemporarilyClearImmutableBit(new_filename, override, &is_immutable);
177174

178175
if (rename(old_filename, new_filename) == -1)
179176
{
@@ -195,10 +192,8 @@ bool OverrideImmutableDelete(const char *filename, bool override)
195192
{
196193
assert(filename != NULL);
197194

198-
FSAttrsResult res;
199195
bool is_immutable = false;
200-
201-
TemporarilyClearImmutableBit(filename, override, &res, &is_immutable);
196+
TemporarilyClearImmutableBit(filename, override, &is_immutable);
202197

203198
return unlink(filename) == 0;
204199
}
@@ -208,10 +203,9 @@ bool OverrideImmutableUtime(
208203
{
209204
assert(filename != NULL);
210205

211-
FSAttrsResult res;
212206
bool is_immutable;
213-
214-
TemporarilyClearImmutableBit(filename, override, &res, &is_immutable);
207+
FSAttrsResult res =
208+
TemporarilyClearImmutableBit(filename, override, &is_immutable);
215209

216210
int ret = utime(filename, times);
217211
if (ret == -1)

libpromises/override_fsattrs.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#ifndef CFENGINE_OVERRIDE_FSATTRS_H
2626
#define CFENGINE_OVERRIDE_FSATTRS_H
2727

28+
#include <fsattrs.h>
2829
#include <stdbool.h>
2930
#include <stddef.h>
3031
#include <utime.h>
@@ -84,4 +85,28 @@ bool OverrideImmutableDelete(const char *filename, bool override);
8485
bool OverrideImmutableUtime(
8586
const char *filename, bool override, const struct utimbuf *times);
8687

88+
/**
89+
* @brief Temporarily clears the immutable bit (best effort / no guarantees)
90+
* @param filename Name of the file to clear the immutable bit on
91+
* @param override Whether to actually do override
92+
* @param is_immutable Whether or not the file actually was immutable
93+
* @return Result of clearing the immutable bit (no to be interpreted by the
94+
* caller)
95+
*/
96+
FSAttrsResult TemporarilyClearImmutableBit(
97+
const char *filename, bool override, bool *was_immutable);
98+
99+
/**
100+
* @brief Reset temporarily cleared immutable bit
101+
* @param filename Name of the file to clear the immutable bit on
102+
* @param override Whether to actually do override
103+
* @param res The result from previously clearing it
104+
* @param is_immutable Whether or not the file actually was immutable
105+
*/
106+
void ResetTemporarilyClearedImmutableBit(
107+
const char *filename,
108+
bool override,
109+
FSAttrsResult res,
110+
bool was_immutable);
111+
87112
#endif /* CFENGINE_OVERRIDE_FSATTRS_H */

0 commit comments

Comments
 (0)