Skip to content

Commit

Permalink
Refactor get_mount_points
Browse files Browse the repository at this point in the history
  • Loading branch information
shivansh02 authored and m3nu committed Jul 24, 2024
1 parent 4b1665d commit 0b937bf
Showing 1 changed file with 62 additions and 46 deletions.
108 changes: 62 additions & 46 deletions src/vorta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,58 +431,74 @@ def format_archive_name(profile, archive_name_tpl):
SHELL_PATTERN_ELEMENT = re.compile(r'([?\[\]*])')


def get_mount_points(repo_url):
def is_mount_command(proc):
try:
name = proc.name()
if name == 'borg' or name.startswith('python'):
if 'mount' in proc.cmdline():
return True
except (psutil.ZombieProcess, psutil.AccessDenied, psutil.NoSuchProcess):
pass
return False


def extract_mount_points_v2(proc, repo_url):
mount_points = {}
repo_mounts = []
for proc in psutil.process_iter():
try:
name = proc.name()
if name == 'borg' or name.startswith('python'):
if 'mount' not in proc.cmdline():
continue

if borg_compat.check('V2'):
# command line syntax:
# `borg mount -r <repo> <mountpoint> <path> (-a <archive_pattern>)`
cmd = proc.cmdline()
if repo_url in cmd:
i = cmd.index(repo_url)
if len(cmd) > i + 1:
mount_point = cmd[i + 1]

# Archive mount?
ao = '-a' in cmd
if ao or '--match-archives' in cmd:
i = cmd.index('-a' if ao else '--match-archives')
if len(cmd) >= i + 1 and not SHELL_PATTERN_ELEMENT.search(cmd[i + 1]):
mount_points[mount_point] = cmd[i + 1]
else:
repo_mounts.append(mount_point)
cmd = proc.cmdline()
if repo_url in cmd:
i = cmd.index(repo_url)
if len(cmd) > i + 1:
mount_point = cmd[i + 1]

ao = '-a' in cmd
if ao or '--match-archives' in cmd:
i = cmd.index('-a' if ao else '--match-archives')
if len(cmd) >= i + 1 and not SHELL_PATTERN_ELEMENT.search(cmd[i + 1]):
mount_points[mount_point] = cmd[i + 1]
else:
repo_mounts.append(mount_point)

return mount_points, repo_mounts


def extract_mount_points_v1(proc, repo_url):
mount_points = {}
repo_mounts = []

for idx, parameter in enumerate(proc.cmdline()):
if parameter.startswith(repo_url):

if len(proc.cmdline()) > idx + 1:
mount_point = proc.cmdline()[idx + 1]

if parameter[len(repo_url) :].startswith('::'):
archive_name = parameter[len(repo_url) + 2 :]
mount_points[archive_name] = mount_point
else:
for idx, parameter in enumerate(proc.cmdline()):
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:
mount_point = proc.cmdline()[idx + 1]

# archive or full mount?
if parameter[len(repo_url) :].startswith('::'):
archive_name = parameter[len(repo_url) + 2 :]
mount_points[archive_name] = mount_point
break
else:
# repo mount point
repo_mounts.append(mount_point)

except (psutil.ZombieProcess, psutil.AccessDenied, psutil.NoSuchProcess):
# 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
repo_mounts.append(mount_point)

return mount_points, repo_mounts


def get_mount_points(repo_url):
mount_points = {}
repo_mounts = []

for proc in psutil.process_iter():
if not is_mount_command(proc):
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


Expand Down

0 comments on commit 0b937bf

Please sign in to comment.