Skip to content

Commit

Permalink
import-tar/export-tar: add xattr support for PAX format, borgbackup#2521
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Oct 8, 2024
1 parent 2ed5d6b commit 35f78ec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/borg/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,17 @@ def s_to_ns(s):
if name in ph:
ns = s_to_ns(ph[name])
setattr(item, name, ns)
xattrs = StableDict()
for key, value in ph.items():
if key.startswith(SCHILY_XATTR):
key = key.removeprefix(SCHILY_XATTR)
# the tarfile code gives us str keys and str values,
# but we need bytes keys and bytes values.
bkey = key.encode("utf-8", errors="surrogateescape")
bvalue = value.encode("utf-8", errors="surrogateescape")
xattrs[bkey] = bvalue
if xattrs:
item.xattrs = xattrs
yield item, status
# if we get here, "with"-block worked ok without error/exception, the item was processed ok...
self.add_item(item, stats=self.stats)
Expand Down
8 changes: 8 additions & 0 deletions src/borg/archiver/tar_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ def item_to_paxheaders(format, item):
if hasattr(item, name):
ns = getattr(item, name)
ph[name] = str(ns / 1e9)
if hasattr(item, "xattrs"):
for bkey, bvalue in item.xattrs.items():
# we have bytes key and bytes value, but the tarfile code
# expects str key and str value.
key = SCHILY_XATTR + bkey.decode("utf-8", errors="surrogateescape")
value = bvalue.decode("utf-8", errors="surrogateescape")
ph[key] = value
if format == "BORG": # BORG format additions
ph["BORG.item.version"] = "1"
# BORG.item.meta - just serialize all metadata we have:
Expand Down Expand Up @@ -355,6 +362,7 @@ def build_parser_tar(self, subparsers, common_parser, mid_common_parser):
| BORG | BORG specific, like PAX | all as supported by borg |
+--------------+---------------------------+----------------------------+
| PAX | POSIX.1-2001 (pax) format | GNU + atime/ctime/mtime ns |
| | | + xattrs |
+--------------+---------------------------+----------------------------+
| GNU | GNU tar format | mtime s, no atime/ctime, |
| | | no ACLs/xattrs/bsdflags |
Expand Down
3 changes: 3 additions & 0 deletions src/borg/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
# similar to above, but for bigger granularity / clock differences
TIME_DIFFERS2_NS = 3000000000

# tar related
SCHILY_XATTR = "SCHILY.xattr." # xattr key prefix in tar PAX headers

# return codes returned by borg command
EXIT_SUCCESS = 0 # everything done, no problems
EXIT_WARNING = 1 # reached normal end of operation, but there were issues (generic warning)
Expand Down

0 comments on commit 35f78ec

Please sign in to comment.