From 0e02abbe5b8f218f6d429be44759322dc086bca9 Mon Sep 17 00:00:00 2001 From: toonn Date: Thu, 31 Dec 2020 15:44:21 +0100 Subject: Report errors if tag file does not exist A non-existent tag file leads to an opaque crash without indication to the user of what went wrong as in #2200. These errors are now reported through ranger's notify mechanism. The tag file is no longer created conditionally on `__init__`, only upon saving tags. This changes the behavior somewhat in that an empty "tagged" file should never be created. --- ranger/container/tags.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ranger/container/tags.py b/ranger/container/tags.py index 50d5ff72..d73b1cf1 100644 --- a/ranger/container/tags.py +++ b/ranger/container/tags.py @@ -13,16 +13,17 @@ from ranger import PY3 ALLOWED_KEYS = string.ascii_letters + string.digits + string.punctuation -class Tags(object): +class Tags(FileManagerAware): default_tag = '*' def __init__(self, filename): + # COMPAT: The intent is to get abspath/normpath's behavior of + # collapsing `symlink/..`, abspath is retained for historical reasons + # because the documentation states its behavior isn't necessarily in + # line with normpath's. self._filename = realpath(abspath(expanduser(filename))) - if isdir(dirname(self._filename)) and not exists(self._filename): - open(self._filename, 'w') - self.sync() def __contains__(self, item): @@ -71,8 +72,11 @@ class Tags(object): fobj = open(self._filename, 'r', errors='replace') else: fobj = open(self._filename, 'r') - except OSError: - pass + except OSError as err: + if exists(self._filename): + self.fm.notify(err, bad=True) + else: + self.tags = dict() else: self.tags = self._parse(fobj) fobj.close() @@ -80,8 +84,8 @@ class Tags(object): def dump(self): try: fobj = open(self._filename, 'w') - except OSError: - pass + except OSError as err: + self.fm.notify(err, bad=True) else: self._compile(fobj) fobj.close() -- cgit 1.4.1-2-gfad0