@@ -86,74 +86,92 @@ bool OverrideImmutableCommit(
86
86
return false;
87
87
}
88
88
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
+
89
98
/* If the operations on the file system attributes fails for any reason,
90
99
* we can still proceed to try to replace the original file. We will only
91
100
* log an actual error in case of an unexpected failure (i.e., when
92
101
* FS_ATTRS_FAILURE is returned). Other failures will be logged as verbose
93
102
* messages because they can be useful, but are be quite verbose. */
94
103
104
+ FSAttrsResult res ;
95
105
bool is_immutable ;
96
- FSAttrsResult res = FSAttrsGetImmutableFlag ( orig , & is_immutable );
97
- if (res == FS_ATTRS_SUCCESS )
106
+
107
+ if (override )
98
108
{
99
- if (is_immutable )
109
+ res = FSAttrsGetImmutableFlag (new_filename , & is_immutable );
110
+ if (res == FS_ATTRS_SUCCESS )
100
111
{
101
- res = FSAttrsUpdateImmutableFlag (orig , false);
102
- if (res == FS_ATTRS_SUCCESS )
112
+ if (is_immutable )
103
113
{
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
+ }
107
129
}
108
130
else
109
131
{
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 );
115
135
}
116
136
}
117
137
else
118
138
{
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 ));
122
143
}
123
144
}
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
- }
131
145
132
- if (rename (copy , orig ) == -1 )
146
+ if (rename (old_filename , new_filename ) == -1 )
133
147
{
134
148
Log (LOG_LEVEL_ERR ,
135
149
"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 );
139
153
return false;
140
154
}
141
155
142
- if (( res == FS_ATTRS_SUCCESS ) && is_immutable )
156
+ if (override )
143
157
{
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 )
152
159
{
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
+ }
157
175
}
158
176
}
159
177
0 commit comments