PICARD-2425: Handle possible exceptions with os.path.realpath

E.g. on Windows this can cause exceptions if drives are mounted without mount manager.
This commit is contained in:
Philipp Wolfer
2022-02-27 12:10:42 +01:00
parent 735a4416f4
commit 3ae2bd2f59
4 changed files with 19 additions and 10 deletions

View File

@@ -78,6 +78,7 @@ from picard.util import (
find_best_match,
format_time,
is_absolute_path,
normpath,
thread,
tracknum_and_title_from_filename,
)
@@ -506,14 +507,10 @@ class File(QtCore.QObject, Item):
if settings["move_files"]:
new_dirname = settings["move_files_to"]
if not is_absolute_path(new_dirname):
new_dirname = os.path.normpath(os.path.join(os.path.dirname(filename), new_dirname))
new_dirname = os.path.join(os.path.dirname(filename), new_dirname)
else:
new_dirname = os.path.dirname(filename)
try:
new_dirname = os.path.realpath(new_dirname)
except FileNotFoundError:
# os.path.realpath can fail if cwd does not exist and path is relative
pass
new_dirname = normpath(new_dirname)
new_filename = os.path.basename(filename)
if settings["rename_files"] or settings["move_files"]:

View File

@@ -55,7 +55,10 @@ from picard.coverart.image import (
)
from picard.file import File
from picard.track import Track
from picard.util import imageinfo
from picard.util import (
imageinfo,
normpath,
)
from picard.util.lrucache import LRUCache
from picard.ui.item import FileListItem
@@ -411,7 +414,7 @@ class CoverArtBox(QtWidgets.QGroupBox):
parse_response_type=None, queryargs=queryargs,
priority=True, important=True)
elif url.scheme() == 'file':
path = os.path.normpath(os.path.realpath(url.toLocalFile().rstrip("\0")))
path = normpath(url.toLocalFile().rstrip("\0"))
if path and os.path.exists(path):
with open(path, 'rb') as f:
data = f.read()

View File

@@ -170,6 +170,8 @@ def normpath(path):
path = os.path.realpath(path)
except OSError as why:
# realpath can fail if path does not exist or is not accessible
# or on Windows if drives are mounted without mount manager
# (see https://tickets.metabrainz.org/browse/PICARD-2425).
log.warning('Failed getting realpath for "%s": %s', path, why)
return os.path.normpath(path)

View File

@@ -400,8 +400,15 @@ def samefile_different_casing(path1, path2):
path2 = os.path.normpath(path2)
if path1 == path2 or not os.path.exists(path1) or not os.path.exists(path2):
return False
dir1 = os.path.realpath(os.path.normcase(os.path.dirname(path1)))
dir2 = os.path.realpath(os.path.normcase(os.path.dirname(path2)))
dir1 = os.path.normcase(os.path.dirname(path1))
dir2 = os.path.normcase(os.path.dirname(path2))
try:
dir1 = os.path.realpath(dir1)
dir2 = os.path.realpath(dir2)
except OSError:
# os.path.realpath can fail if cwd does not exist and path is relative
# or on Windows if drives are mounted without mount manager.
pass
if dir1 != dir2 or not samefile(path1, path2):
return False
file1 = os.path.basename(path1)