Skip to content
Snippets Groups Projects
Commit a5486ebd authored by Chris Lamb's avatar Chris Lamb :eyes:
Browse files

Do not exit with a traceback if paths are inaccessible, either directly, via...

Do not exit with a traceback if paths are inaccessible, either directly, via symbolic links or within a directory. (Closes: #367, Debian:#1065498)
parent 9a0faeed
No related branches found
No related tags found
No related merge requests found
Pipeline #802219 passed with warnings
......@@ -25,7 +25,7 @@ import subprocess
from diffoscope.tools import tool_required
from diffoscope.exc import RequiredToolNotFound
from diffoscope.utils import exit_if_paths_do_not_exist
from diffoscope.utils import exit_if_paths_inaccessible
from diffoscope.config import Config
from diffoscope.excludes import any_excluded
from diffoscope.profiling import profile
......@@ -55,7 +55,7 @@ def compare_root_paths(path1, path2):
)
if not Config().new_file:
exit_if_paths_do_not_exist(path1, path2)
exit_if_paths_inaccessible(path1, path2)
if any_excluded(path1, path2):
return None
......
......@@ -198,9 +198,13 @@ class Container(metaclass=abc.ABCMeta):
difference.add_comment(msg)
return difference
difference = compare_files(
file1, file2, source=None, diff_content_only=no_recurse
)
try:
difference = compare_files(
file1, file2, source=None, diff_content_only=no_recurse
)
except PermissionError as exc:
logger.warning(f"Skipping {exc.filename} ({exc.strerror})")
return
if isinstance(file1, AbstractMissingType) or isinstance(
file2, AbstractMissingType
......
......@@ -19,13 +19,13 @@
import codecs
from diffoscope.utils import exit_if_paths_do_not_exist
from diffoscope.utils import exit_if_paths_inaccessible
from .json import JSONReaderV1
def load_diff_from_path(path):
exit_if_paths_do_not_exist(path)
exit_if_paths_inaccessible(path)
with open(path, "rb") as fp:
return load_diff(codecs.getreader("utf-8")(fp), path)
......
......@@ -58,16 +58,29 @@ def format_bytes(size, decimal_places=2):
return f"{size:.{decimal_places}f} {unit}"
def exit_if_paths_do_not_exist(*paths):
def exit_if_paths_inaccessible(*paths):
"""
Exit if the specified *paths are inaccessible, either by:
a) simply being missing
b) being a dangling symbolic links
c) being inaccessible (directly or via a symbolic link
"""
flag = False
for path in paths:
if os.path.lexists(path):
continue
flag = True
print(
f"{sys.argv[0]}: {path}: No such file or directory",
file=sys.stderr,
)
if not os.path.lexists(path):
flag = True
print(
f"{sys.argv[0]}: {path}: No such file or directory",
file=sys.stderr,
)
elif not os.access(path, os.R_OK):
flag = True
print(
f"{sys.argv[0]}: {path}: Permission denied",
file=sys.stderr,
)
if flag:
sys.exit(2)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment