Skip to content
This repository was archived by the owner on Jun 15, 2025. It is now read-only.

Commit 709e995

Browse files
committed
Move directory check from _isRoot into _findRoot
The check being performed in the _IsRoot function to see whether the directory given is an actually existing one does not have to be performed for every directory in traversed using _findRoot. We only traverse the list upward (that is, from some directory towards the root of the file system) and once we convinced ourselve that the initial directory is valid all subsequent ones should be so as well. To that end, this change moves the directory check into the _findRoot function in order to get rid of some unnecessary work but also in anticipation of a future change which will repurpose _findRoot.
1 parent 39cfd94 commit 709e995

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

btrfs-backup/src/deso/btrfs/repository.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# repository.py
22

33
#/***************************************************************************
4-
# * Copyright (C) 2015 Daniel Mueller (deso@posteo.net) *
4+
# * Copyright (C) 2015-2016 Daniel Mueller (deso@posteo.net) *
55
# * *
66
# * This program is free software: you can redistribute it and/or modify *
77
# * it under the terms of the GNU General Public License as published by *
@@ -211,29 +211,24 @@ def _snapshotFiles(directory, extension, repository):
211211
return files
212212

213213

214-
def _isRoot(directory, repository):
215-
"""Check if a given directory represents the root of a btrfs file system."""
216-
def isDir(directory):
217-
"""Check if a directory exists."""
218-
try:
219-
# Append a trailing separator here to indicate that we are
220-
# checking for a directory. This way ls will fail if it is not. If
221-
# we left out the trailing separator and the directory actually
222-
# points to a file, ls would still succeed.
223-
func = lambda: ["/bin/ls", _trail(directory)]
224-
cmd = repository.command(func)
214+
def _isDir(directory, repository):
215+
"""Check if a directory exists."""
216+
try:
217+
# Append a trailing separator here to indicate that we are
218+
# checking for a directory. This way ls will fail if it is not. If
219+
# we left out the trailing separator and the directory actually
220+
# points to a file, ls would still succeed.
221+
func = lambda: ["/bin/ls", _trail(directory)]
222+
cmd = repository.command(func)
223+
224+
execute(*cmd, stderr=repository.stderr)
225+
return True
226+
except ProcessError:
227+
return False
225228

226-
execute(*cmd, stderr=repository.stderr)
227-
return True
228-
except ProcessError:
229-
return False
230-
231-
# TODO: We might want to move this invocation into _findRoot to avoid
232-
# unnecessary invocations, especially since they might be
233-
# remote.
234-
if not isDir(directory):
235-
raise FileNotFoundError("Directory \"%s\" not found." % directory)
236229

230+
def _isRoot(directory, repository):
231+
"""Check if a given directory represents the root of a btrfs file system."""
237232
try:
238233
cmd = repository.command(show, directory)
239234
output, _ = execute(*cmd, stdout=b"", stderr=repository.stderr)
@@ -256,6 +251,9 @@ def _findRoot(directory, repository):
256251
"""Find the root of the btrfs file system containing the given directory."""
257252
assert directory
258253

254+
if not _isDir(directory, repository):
255+
raise FileNotFoundError("Directory \"%s\" not found." % directory)
256+
259257
cur_directory = directory
260258
# Note that we have no guard here against an empty directory as input
261259
# or later because of a dirname invocation. However, the show command

btrfs-backup/src/deso/btrfs/test/testRepository.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# testRepository.py
22

33
#/***************************************************************************
4-
# * Copyright (C) 2015 Daniel Mueller (deso@posteo.net) *
4+
# * Copyright (C) 2015-2016 Daniel Mueller (deso@posteo.net) *
55
# * *
66
# * This program is free software: you can redistribute it and/or modify *
77
# * it under the terms of the GNU General Public License as published by *
@@ -138,7 +138,7 @@ def doTest(path, expected):
138138
def testFindRootCorrectDirectory(self):
139139
"""Verify that in case of an error _findRoot reports the proper directory."""
140140
directory = self._mount.path("non-existent-directory")
141-
regex = r"Root of btrfs.*not found.*: \"%s\"" % directory
141+
regex = r"Directory.* \"%s\".*not found.*" % directory
142142
repo = Repository(self._mount.path())
143143

144144
with patch("deso.btrfs.repository._isRoot") as mock_isroot:

0 commit comments

Comments
 (0)