summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornfnty <git@nfnty.se>2017-01-23 21:50:40 +0100
committernfnty <git@nfnty.se>2017-01-23 21:56:32 +0100
commit630884165a8af388983eb39c0976f58506d1a0ea (patch)
treef99f0815e10bc8fb3cfbd3b6e26bd41762defb7a
parentd07c60e464a5e70ae8a762f828221b235ce11818 (diff)
downloadranger-630884165a8af388983eb39c0976f58506d1a0ea.tar.gz
container.bookmarks: Notify on errors, Disallow files
Fixes #760
-rw-r--r--ranger/container/bookmarks.py84
1 files changed, 46 insertions, 38 deletions
diff --git a/ranger/container/bookmarks.py b/ranger/container/bookmarks.py
index 305ac4c2..23606028 100644
--- a/ranger/container/bookmarks.py
+++ b/ranger/container/bookmarks.py
@@ -6,10 +6,13 @@ from __future__ import (absolute_import, division, print_function)
 import string
 import re
 import os
+
+from ranger.core.shared import FileManagerAware
+
 ALLOWED_KEYS = string.ascii_letters + string.digits + "`'"
 
 
-class Bookmarks(object):
+class Bookmarks(FileManagerAware):
     """Bookmarks is a container which associates keys with bookmarks.
 
     A key is a string with: len(key) == 1 and key in ALLOWED_KEYS.
@@ -39,9 +42,8 @@ class Bookmarks(object):
 
     def load(self):
         """Load the bookmarks from path/bookmarks"""
-        try:
-            new_dict = self._load_dict()
-        except OSError:
+        new_dict = self._load_dict()
+        if new_dict is None:
             return
 
         self._set_dict(new_dict, original=new_dict)
@@ -110,12 +112,10 @@ class Bookmarks(object):
 
         Useful if two instances are running which define different bookmarks.
         """
-
-        try:
-            real_dict = self._load_dict()
-            real_dict_copy = real_dict.copy()
-        except OSError:
+        real_dict = self._load_dict()
+        if real_dict is None:
             return
+        real_dict_copy = real_dict.copy()
 
         for key in set(self.dct) | set(real_dict):
             # set some variables
@@ -152,46 +152,54 @@ class Bookmarks(object):
         self.update()
         if self.path is None:
             return
-        if os.access(self.path, os.W_OK):
-            fobj = open(self.path + ".new", 'w')
-            for key, value in self.dct.items():
-                if isinstance(key, str) and key in ALLOWED_KEYS:
-                    fobj.write("{0}:{1}\n".format(str(key), str(value)))
 
-            fobj.close()
+        path_new = self.path + '.new'
+        try:
+            fobj = open(path_new, 'w')
+        except OSError as ex:
+            self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+            return
+        for key, value in self.dct.items():
+            if isinstance(key, str) and key in ALLOWED_KEYS:
+                fobj.write("{0}:{1}\n".format(str(key), str(value)))
+        fobj.close()
+
+        try:
             old_perms = os.stat(self.path)
-            try:
-                os.chown(self.path + ".new", old_perms.st_uid, old_perms.st_gid)
-                os.chmod(self.path + ".new", old_perms.st_mode)
-            except OSError:
-                pass
-            os.rename(self.path + ".new", self.path)
+            os.chown(path_new, old_perms.st_uid, old_perms.st_gid)
+            os.chmod(path_new, old_perms.st_mode)
+            os.rename(path_new, self.path)
+        except OSError as ex:
+            self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+            return
+
         self._update_mtime()
 
     def _load_dict(self):
-        dct = {}
-
         if self.path is None:
-            return dct
+            return {}
 
         if not os.path.exists(self.path):
             try:
-                fobj = open(self.path, 'w')
-            except OSError:
-                raise
-            fobj.close()
+                with open(self.path, 'w') as fobj:
+                    pass
+            except OSError as ex:
+                self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+                return None
 
-        if os.access(self.path, os.R_OK):
+        try:
             fobj = open(self.path, 'r')
-            for line in fobj:
-                if self.load_pattern.match(line):
-                    key, value = line[0], line[2:-1]
-                    if key in ALLOWED_KEYS:
-                        dct[key] = self.bookmarktype(value)
-            fobj.close()
-            return dct
-        else:
-            raise OSError('Cannot read the given path')
+        except OSError as ex:
+            self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True)
+            return None
+        dct = {}
+        for line in fobj:
+            if self.load_pattern.match(line):
+                key, value = line[0], line[2:-1]
+                if key in ALLOWED_KEYS and not os.path.isfile(value):
+                    dct[key] = self.bookmarktype(value)
+        fobj.close()
+        return dct
 
     def _set_dict(self, dct, original):
         if original is None:
18-10-24 23:28:03 -0700 committer Kartik Agaram <vc@akkartik.com> 2018-10-24 23:28:03 -0700 4724' href='/akkartik/mu/commit/html/subx/040---tests.cc.html?h=main&id=1f08b541af47398afb3f64968b2fc5e96c58b7bb'>1f08b541 ^
37d53a70 ^
1f08b541 ^


d3a9db3a ^
c504ca56 ^

fcc161e7 ^
d3a9db3a ^

c504ca56 ^









d3a9db3a ^
c504ca56 ^



fcc161e7 ^
c504ca56 ^



d3a9db3a ^
c504ca56 ^
d3a9db3a ^
c504ca56 ^









fcc161e7 ^
c504ca56 ^



d3a9db3a ^
c504ca56 ^
d3a9db3a ^
c504ca56 ^










fcc161e7 ^
c504ca56 ^









fcc161e7 ^
c504ca56 ^





fcc161e7 ^
c504ca56 ^





1a4de9dd ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158