@@ -179,6 +179,12 @@ def xstrip(filename):
179
179
filename = re .sub (b'^[\\ \\ /]+' , b'' , filename )
180
180
return filename
181
181
182
+
183
+ def safe_unlink (filepath ):
184
+ os .chmod (filepath , stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH )
185
+ os .unlink (filepath )
186
+
187
+
182
188
#-----------------------------------------------
183
189
# Main API functions
184
190
@@ -977,7 +983,7 @@ def apply(self, strip=0, root=None, fuzz=False):
977
983
save (target , new_file )
978
984
elif "dev/null" in target :
979
985
source = self .strip_path (source , root , strip )
980
- os . unlink (source )
986
+ safe_unlink (source )
981
987
else :
982
988
items .append (item )
983
989
self .items = items
@@ -1107,14 +1113,12 @@ def apply(self, strip=0, root=None, fuzz=False):
1107
1113
shutil .move (filenamen , backupname )
1108
1114
if self .write_hunks (backupname if filenameo == filenamen else filenameo , filenamen , p .hunks ):
1109
1115
info ("successfully patched %d/%d:\t %s" % (i + 1 , total , filenamen ))
1110
- os .chmod (backupname , stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH )
1111
- os .unlink (backupname )
1116
+ safe_unlink (backupname )
1112
1117
if new == b'/dev/null' :
1113
1118
# check that filename is of size 0 and delete it.
1114
1119
if os .path .getsize (filenamen ) > 0 :
1115
1120
warning ("expected patched file to be empty as it's marked as deletion:\t %s" % filenamen )
1116
- os .chmod (backupname , stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH )
1117
- os .unlink (filenamen )
1121
+ safe_unlink (filenamen )
1118
1122
else :
1119
1123
errors += 1
1120
1124
warning ("error patching file %s" % filenamen )
0 commit comments