@@ -89,59 +89,91 @@ bool OverrideImmutableCommit(
89
89
return OverrideImmutableRename (copy , orig , override );
90
90
}
91
91
92
- bool OverrideImmutableRename (
93
- const char * old_filename , const char * new_filename , bool override )
92
+ static void TemporarilyClearImmutableBit (
93
+ const char * filename ,
94
+ bool override ,
95
+ FSAttrsResult * res ,
96
+ bool * is_immutable )
94
97
{
95
- assert (old_filename != NULL );
96
- assert (new_filename != NULL );
97
-
98
- /* If the operations on the file system attributes fails for any reason,
99
- * we can still proceed to try to replace the original file. We will only
100
- * log an actual error in case of an unexpected failure (i.e., when
101
- * FS_ATTRS_FAILURE is returned). Other failures will be logged as verbose
102
- * messages because they can be useful, but are be quite verbose. */
103
-
104
- FSAttrsResult res ;
105
- bool is_immutable ;
98
+ if (!override )
99
+ {
100
+ return ;
101
+ }
106
102
107
- if (override )
103
+ * res = FSAttrsGetImmutableFlag (filename , is_immutable );
104
+ if (* res == FS_ATTRS_SUCCESS )
108
105
{
109
- res = FSAttrsGetImmutableFlag (new_filename , & is_immutable );
110
- if (res == FS_ATTRS_SUCCESS )
106
+ if (* is_immutable )
111
107
{
112
- if (is_immutable )
108
+ * res = FSAttrsUpdateImmutableFlag (filename , false);
109
+ if (* res == FS_ATTRS_SUCCESS )
113
110
{
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
- }
111
+ Log (LOG_LEVEL_VERBOSE ,
112
+ "Temporarily cleared immutable bit for file '%s'" ,
113
+ filename );
129
114
}
130
115
else
131
116
{
132
- Log (LOG_LEVEL_DEBUG ,
133
- "The immutable bit is not set on file '%s'" ,
134
- new_filename );
117
+ Log ((* res == FS_ATTRS_FAILURE ) ? LOG_LEVEL_ERR
118
+ : LOG_LEVEL_VERBOSE ,
119
+ "Failed to temporarily clear immutable bit for file '%s': %s" ,
120
+ filename ,
121
+ FSAttrsErrorCodeToString (* res ));
135
122
}
136
123
}
137
124
else
125
+ {
126
+ Log (LOG_LEVEL_DEBUG ,
127
+ "The immutable bit is not set on file '%s'" ,
128
+ filename );
129
+ }
130
+ }
131
+ else
132
+ {
133
+ Log ((* res == FS_ATTRS_FAILURE ) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE ,
134
+ "Failed to get immutable bit from file '%s': %s" ,
135
+ filename ,
136
+ FSAttrsErrorCodeToString (* res ));
137
+ }
138
+ }
139
+
140
+ static void ResetTemporarilyClearedImmutableBit (
141
+ const char * filename , bool override , FSAttrsResult res , bool is_immutable )
142
+ {
143
+ if (!override )
144
+ {
145
+ return ;
146
+ }
147
+
148
+ if ((res == FS_ATTRS_SUCCESS ) && is_immutable )
149
+ {
150
+ res = FSAttrsUpdateImmutableFlag (filename , true);
151
+ if (res == FS_ATTRS_SUCCESS )
152
+ {
153
+ Log (LOG_LEVEL_VERBOSE ,
154
+ "Reset immutable bit after temporarily clearing it from file '%s'" ,
155
+ filename );
156
+ }
157
+ else
138
158
{
139
159
Log ((res == FS_ATTRS_FAILURE ) ? LOG_LEVEL_ERR : LOG_LEVEL_VERBOSE ,
140
- "Failed to get immutable bit from file '%s': %s" ,
141
- new_filename ,
160
+ "Failed to reset immutable bit after temporarily clearing it from file '%s': %s" ,
161
+ filename ,
142
162
FSAttrsErrorCodeToString (res ));
143
163
}
144
164
}
165
+ }
166
+
167
+ bool OverrideImmutableRename (
168
+ const char * old_filename , const char * new_filename , bool override )
169
+ {
170
+ assert (old_filename != NULL );
171
+ assert (new_filename != NULL );
172
+
173
+ FSAttrsResult res ;
174
+ bool is_immutable ;
175
+
176
+ TemporarilyClearImmutableBit (new_filename , override , & res , & is_immutable );
145
177
146
178
if (rename (old_filename , new_filename ) == -1 )
147
179
{
@@ -153,27 +185,8 @@ bool OverrideImmutableRename(
153
185
return false;
154
186
}
155
187
156
- if (override )
157
- {
158
- if ((res == FS_ATTRS_SUCCESS ) && is_immutable )
159
- {
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
- }
175
- }
176
- }
188
+ ResetTemporarilyClearedImmutableBit (
189
+ new_filename , override , res , is_immutable );
177
190
178
191
return true;
179
192
}
@@ -182,45 +195,10 @@ bool OverrideImmutableDelete(const char *filename, bool override)
182
195
{
183
196
assert (filename != NULL );
184
197
198
+ FSAttrsResult res ;
185
199
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
- }
200
+
201
+ TemporarilyClearImmutableBit (filename , override , & res , & is_immutable );
224
202
225
203
return unlink (filename ) == 0 ;
226
204
}
0 commit comments