From 66f10712b4c7e8914ef6dde7beef1333ad06e68d Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 4 Jun 2025 13:28:32 +0200 Subject: [PATCH 1/2] chunker: use safe_fadvise also: refactor safe_advise a bit, use has_posix_fadvise. --- src/borg/chunker.pyx | 7 ++----- src/borg/platform/base.py | 5 ++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/borg/chunker.pyx b/src/borg/chunker.pyx index 3187a223a..133186d5d 100644 --- a/src/borg/chunker.pyx +++ b/src/borg/chunker.pyx @@ -13,6 +13,7 @@ from libc.stdlib cimport malloc, free from libc.string cimport memcpy, memmove from .constants import CH_DATA, CH_ALLOC, CH_HOLE, zeros +from .platform import safe_fadvise # this will be True if Python's seek implementation supports data/holes seeking. # this does not imply that it will actually work on the filesystem, @@ -47,11 +48,7 @@ def dread(offset, size, fd=None, fh=-1): use_fh = fh >= 0 if use_fh: data = os.read(fh, size) - if hasattr(os, 'posix_fadvise'): - # UNIX only and, in case of block sizes that are not a multiple of the - # system's page size, better be used with a bug fixed linux kernel > 4.6.0, - # see comment/workaround in _chunker.c and borgbackup issue #907. - os.posix_fadvise(fh, offset, len(data), os.POSIX_FADV_DONTNEED) + safe_fadvise(fh, offset, len(data), "DONTNEED") return data else: return fd.read(size) diff --git a/src/borg/platform/base.py b/src/borg/platform/base.py index 70cc933f5..38328a50b 100644 --- a/src/borg/platform/base.py +++ b/src/borg/platform/base.py @@ -21,6 +21,7 @@ are correctly composed into the base functionality. API_VERSION = "1.2_05" fdatasync = getattr(os, "fdatasync", os.fsync) +has_posix_fadvise = hasattr(os, "posix_fadvise") from .xattr import ENOATTR @@ -114,9 +115,11 @@ def sync_dir(path): def safe_fadvise(fd, offset, len, advice): - if hasattr(os, "posix_fadvise"): + if has_posix_fadvise: advice = getattr(os, "POSIX_FADV_" + advice) try: + # UNIX only and, in case of block sizes that are not a multiple of the system's page size, + # better be used with a bug fixed linux kernel > 4.6.0, see borgbackup issue #907. os.posix_fadvise(fd, offset, len, advice) except OSError: # usually, posix_fadvise can't fail for us, but there seem to From f58d26671d3482553908bcf77c1266c53596278d Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 4 Jun 2025 13:40:16 +0200 Subject: [PATCH 2/2] ChunkerFixed: do not assert on short header read ChunkerFixed can be configured to support files with a specific header size. But we do not want to get an AssertionError if we encounter a 0-byte file or a file that is shorter than the header size. --- src/borg/chunker.pyx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/borg/chunker.pyx b/src/borg/chunker.pyx index 133186d5d..bdbb4a16e 100644 --- a/src/borg/chunker.pyx +++ b/src/borg/chunker.pyx @@ -433,7 +433,6 @@ class ChunkerFixed: self.chunking_time += time.monotonic() - started_chunking if header_chunk.meta["size"] > 0: - assert self.header_size == header_chunk.meta["size"] # Yield the header chunk yield header_chunk