mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-26 17:57:59 +00:00
support hardlinks via create_helper context manager
also: reduce code duplication
This commit is contained in:
parent
e5d094d0ce
commit
9478e8abd0
1 changed files with 88 additions and 83 deletions
|
@ -834,34 +834,54 @@ def stat_attrs(self, st, path):
|
|||
attrs.update(self.stat_ext_attrs(st, path))
|
||||
return attrs
|
||||
|
||||
def process_dir(self, path, st):
|
||||
item = Item(path=make_path_safe(path))
|
||||
item.update(self.stat_attrs(st, path))
|
||||
@contextmanager
|
||||
def create_helper(self, path, st, status=None):
|
||||
safe_path = make_path_safe(path)
|
||||
item = Item(path=safe_path)
|
||||
hardlink_master = False
|
||||
hardlinked = st.st_nlink > 1
|
||||
if hardlinked:
|
||||
source = self.hard_links.get((st.st_ino, st.st_dev))
|
||||
if source is not None:
|
||||
item.source = source
|
||||
status = 'h' # hardlink (to already seen inodes)
|
||||
else:
|
||||
hardlink_master = True
|
||||
yield item, status, hardlinked, hardlink_master
|
||||
# if we get here, "with"-block worked ok without error/exception, the item was processed ok...
|
||||
self.add_item(item)
|
||||
return 'd' # directory
|
||||
# ... and added to the archive, so we can remember it to refer to it later in the archive:
|
||||
if hardlink_master:
|
||||
self.hard_links[(st.st_ino, st.st_dev)] = safe_path
|
||||
|
||||
def process_dir(self, path, st):
|
||||
with self.create_helper(path, st, 'd') as (item, status, hardlinked, hardlink_master): # directory
|
||||
item.update(self.stat_attrs(st, path))
|
||||
return status
|
||||
|
||||
def process_fifo(self, path, st):
|
||||
item = Item(path=make_path_safe(path))
|
||||
with self.create_helper(path, st, 'f') as (item, status, hardlinked, hardlink_master): # fifo
|
||||
item.update(self.stat_attrs(st, path))
|
||||
self.add_item(item)
|
||||
return 'f' # fifo
|
||||
return status
|
||||
|
||||
def process_dev(self, path, st):
|
||||
item = Item(path=make_path_safe(path), rdev=st.st_rdev)
|
||||
with self.create_helper(path, st, None) as (item, status, hardlinked, hardlink_master): # no status yet
|
||||
item.rdev = st.st_rdev
|
||||
item.update(self.stat_attrs(st, path))
|
||||
self.add_item(item)
|
||||
if stat.S_ISCHR(st.st_mode):
|
||||
return 'c' # char device
|
||||
elif stat.S_ISBLK(st.st_mode):
|
||||
return 'b' # block device
|
||||
|
||||
def process_symlink(self, path, st):
|
||||
with self.create_helper(path, st, 's') as (item, status, hardlinked, hardlink_master): # symlink
|
||||
with backup_io('readlink'):
|
||||
source = os.readlink(path)
|
||||
item = Item(path=make_path_safe(path), source=source)
|
||||
item.source = source # XXX this overwrites hardlink slave's usage of item.source
|
||||
if hardlinked:
|
||||
logger.warning('hardlinked symlinks will be extracted as non-hardlinked symlinks!')
|
||||
item.update(self.stat_attrs(st, path))
|
||||
self.add_item(item)
|
||||
return 's' # symlink
|
||||
return status
|
||||
|
||||
def write_part_file(self, item, from_chunk, number):
|
||||
item = Item(internal_dict=item.as_dict())
|
||||
|
@ -925,18 +945,7 @@ def process_stdin(self, path, cache):
|
|||
return 'i' # stdin
|
||||
|
||||
def process_file(self, path, st, cache, ignore_inode=False):
|
||||
status = None
|
||||
safe_path = make_path_safe(path)
|
||||
item = Item(path=safe_path)
|
||||
hardlink_master = False
|
||||
hardlinked = st.st_nlink > 1
|
||||
if hardlinked:
|
||||
source = self.hard_links.get((st.st_ino, st.st_dev))
|
||||
if source is not None:
|
||||
item.source = source
|
||||
status = 'h' # regular file, hardlink (to already seen inodes)
|
||||
else:
|
||||
hardlink_master = True
|
||||
with self.create_helper(path, st, None) as (item, status, hardlinked, hardlink_master): # no status yet
|
||||
is_special_file = is_special(st.st_mode)
|
||||
if not hardlinked or hardlink_master:
|
||||
if not is_special_file:
|
||||
|
@ -983,10 +992,6 @@ def process_file(self, path, st, cache, ignore_inode=False):
|
|||
# we processed a special file like a regular file. reflect that in mode,
|
||||
# so it can be extracted / accessed in FUSE mount like a regular file:
|
||||
item.mode = stat.S_IFREG | stat.S_IMODE(item.mode)
|
||||
self.add_item(item)
|
||||
if hardlink_master:
|
||||
# Add the hard link reference *after* the file has been added to the archive.
|
||||
self.hard_links[st.st_ino, st.st_dev] = safe_path
|
||||
return status
|
||||
|
||||
@staticmethod
|
||||
|
|
Loading…
Reference in a new issue