Skip to content

Commit 17ffef9

Browse files
committed
Fix file state tracking with st = None after unlink/rmdir
1 parent 1019f36 commit 17ffef9

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/borg/archive.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,9 @@ def extract_helper(self, item, path, hlm, *, dry_run=False):
719719
# In this case, we *want* to extract twice, because there is no other way.
720720
pass
721721

722-
def compare_and_extract_chunks(self, item, fs_path, *, pi=None, sparse=False):
722+
def compare_and_extract_chunks(self, item, fs_path, *, st, pi=None):
723723
"""Compare file chunks and patch if needed. Returns True if patching succeeded."""
724-
if not os.path.exists(fs_path):
724+
if st is None:
725725
return False
726726
try:
727727
# First pass: Build fs chunks list
@@ -755,12 +755,8 @@ def compare_and_extract_chunks(self, item, fs_path, *, pi=None, sparse=False):
755755
else:
756756
chunk_data = next(chunk_data_iter)
757757

758-
with backup_io("seek"):
759-
fs_file.seek(fs_file.tell())
760-
761758
with backup_io("write"):
762-
data = b"\0" * len(chunk_data) if sparse and not chunk_data.strip(b"\0") else chunk_data
763-
fs_file.write(data)
759+
fs_file.write(chunk_data)
764760
if pi:
765761
pi.show(increase=len(chunk_data), info=[remove_surrogates(item.path)])
766762

@@ -863,12 +859,14 @@ def same_item(item, st):
863859
return # done! we already have fully extracted this file in a previous run.
864860
elif stat.S_ISDIR(st.st_mode):
865861
os.rmdir(path)
862+
st = None
866863
else:
867864
os.unlink(path)
865+
st = None
868866
except UnicodeEncodeError:
869867
raise self.IncompatibleFilesystemEncodingError(path, sys.getfilesystemencoding()) from None
870868
except OSError:
871-
pass
869+
st = None
872870

873871
def make_parent(path):
874872
parent_dir = os.path.dirname(path)
@@ -882,7 +880,7 @@ def make_parent(path):
882880
with self.extract_helper(item, path, hlm) as hardlink_set:
883881
if hardlink_set:
884882
return
885-
if self.compare_and_extract_chunks(item, path, pi=pi, sparse=sparse):
883+
if self.compare_and_extract_chunks(item, path, st=st, pi=pi):
886884
return
887885

888886
with backup_io("open"):

src/borg/testsuite/archive_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ def test_compare_and_extract_chunks(setup_extractor, name, item_data, fs_data, e
545545
with open(target_path, "wb") as f:
546546
f.write(fs_data)
547547

548-
result = extractor.compare_and_extract_chunks(item, target_path)
548+
st = os.stat(target_path)
549+
result = extractor.compare_and_extract_chunks(item, target_path, st=st)
549550
assert result
550551

551552
fetched_chunks = get_fetched_chunks()

0 commit comments

Comments
 (0)