@@ -179,6 +179,12 @@ def xstrip(filename):
179179 filename = re .sub (b'^[\\ \\ /]+' , b'' , filename )
180180 return filename
181181
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+
182188#-----------------------------------------------
183189# Main API functions
184190
@@ -977,7 +983,7 @@ def apply(self, strip=0, root=None, fuzz=False):
977983 save (target , new_file )
978984 elif "dev/null" in target :
979985 source = self .strip_path (source , root , strip )
980- os . unlink (source )
986+ safe_unlink (source )
981987 else :
982988 items .append (item )
983989 self .items = items
@@ -1107,14 +1113,12 @@ def apply(self, strip=0, root=None, fuzz=False):
11071113 shutil .move (filenamen , backupname )
11081114 if self .write_hunks (backupname if filenameo == filenamen else filenameo , filenamen , p .hunks ):
11091115 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 )
11121117 if new == b'/dev/null' :
11131118 # check that filename is of size 0 and delete it.
11141119 if os .path .getsize (filenamen ) > 0 :
11151120 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 )
11181122 else :
11191123 errors += 1
11201124 warning ("error patching file %s" % filenamen )
0 commit comments