Refactor get_mount_points

This commit is contained in:
shivansh02 2024-07-22 19:19:27 +05:30 committed by Manu
parent 4b1665daca
commit 0b937bf44e
1 changed files with 62 additions and 46 deletions

View File

@ -431,26 +431,27 @@ def format_archive_name(profile, archive_name_tpl):
SHELL_PATTERN_ELEMENT = re.compile(r'([?\[\]*])') SHELL_PATTERN_ELEMENT = re.compile(r'([?\[\]*])')
def get_mount_points(repo_url): def is_mount_command(proc):
mount_points = {}
repo_mounts = []
for proc in psutil.process_iter():
try: try:
name = proc.name() name = proc.name()
if name == 'borg' or name.startswith('python'): if name == 'borg' or name.startswith('python'):
if 'mount' not in proc.cmdline(): if 'mount' in proc.cmdline():
continue return True
except (psutil.ZombieProcess, psutil.AccessDenied, psutil.NoSuchProcess):
pass
return False
def extract_mount_points_v2(proc, repo_url):
mount_points = {}
repo_mounts = []
if borg_compat.check('V2'):
# command line syntax:
# `borg mount -r <repo> <mountpoint> <path> (-a <archive_pattern>)`
cmd = proc.cmdline() cmd = proc.cmdline()
if repo_url in cmd: if repo_url in cmd:
i = cmd.index(repo_url) i = cmd.index(repo_url)
if len(cmd) > i + 1: if len(cmd) > i + 1:
mount_point = cmd[i + 1] mount_point = cmd[i + 1]
# Archive mount?
ao = '-a' in cmd ao = '-a' in cmd
if ao or '--match-archives' in cmd: if ao or '--match-archives' in cmd:
i = cmd.index('-a' if ao else '--match-archives') i = cmd.index('-a' if ao else '--match-archives')
@ -458,31 +459,46 @@ def get_mount_points(repo_url):
mount_points[mount_point] = cmd[i + 1] mount_points[mount_point] = cmd[i + 1]
else: else:
repo_mounts.append(mount_point) repo_mounts.append(mount_point)
else:
return mount_points, repo_mounts
def extract_mount_points_v1(proc, repo_url):
mount_points = {}
repo_mounts = []
for idx, parameter in enumerate(proc.cmdline()): for idx, parameter in enumerate(proc.cmdline()):
if parameter.startswith(repo_url): if parameter.startswith(repo_url):
# mount from this repo
# The borg mount command specifies that the mount_point
# parameter comes after the archive name
if len(proc.cmdline()) > idx + 1: if len(proc.cmdline()) > idx + 1:
mount_point = proc.cmdline()[idx + 1] mount_point = proc.cmdline()[idx + 1]
# archive or full mount?
if parameter[len(repo_url) :].startswith('::'): if parameter[len(repo_url) :].startswith('::'):
archive_name = parameter[len(repo_url) + 2 :] archive_name = parameter[len(repo_url) + 2 :]
mount_points[archive_name] = mount_point mount_points[archive_name] = mount_point
break
else: else:
# repo mount point
repo_mounts.append(mount_point) repo_mounts.append(mount_point)
except (psutil.ZombieProcess, psutil.AccessDenied, psutil.NoSuchProcess): return mount_points, repo_mounts
# Getting process details may fail (e.g. zombie process on macOS)
# or because the process is owned by another user.
# Also see https://github.com/giampaolo/psutil/issues/783 def get_mount_points(repo_url):
mount_points = {}
repo_mounts = []
for proc in psutil.process_iter():
if not is_mount_command(proc):
continue continue
if borg_compat.check('V2'):
v2_mount_points, v2_repo_mounts = extract_mount_points_v2(proc, repo_url)
mount_points.update(v2_mount_points)
repo_mounts.extend(v2_repo_mounts)
else:
v1_mount_points, v1_repo_mounts = extract_mount_points_v1(proc, repo_url)
mount_points.update(v1_mount_points)
repo_mounts.extend(v1_repo_mounts)
return mount_points, repo_mounts return mount_points, repo_mounts