Revision 9c1c3c19 lib/storage/filestorage.py
b/lib/storage/filestorage.py | ||
---|---|---|
108 | 108 |
return _ComputeWrongFileStoragePaths(_LoadAllowedFileStoragePaths(_filename)) |
109 | 109 |
|
110 | 110 |
|
111 |
def _CheckFileStoragePath(path, allowed): |
|
111 |
def _CheckFileStoragePath(path, allowed, exact_match_ok=False):
|
|
112 | 112 |
"""Checks if a path is in a list of allowed paths for file storage. |
113 | 113 |
|
114 | 114 |
@type path: string |
115 | 115 |
@param path: Path to check |
116 | 116 |
@type allowed: list |
117 | 117 |
@param allowed: List of allowed paths |
118 |
@type exact_match_ok: bool |
|
119 |
@param exact_match_ok: whether or not it is okay when the path is exactly |
|
120 |
equal to an allowed path and not a subdir of it |
|
118 | 121 |
@raise errors.FileStoragePathError: If the path is not allowed |
119 | 122 |
|
120 | 123 |
""" |
... | ... | |
127 | 130 |
logging.info("Ignoring relative path '%s' for file storage", i) |
128 | 131 |
continue |
129 | 132 |
|
133 |
if exact_match_ok: |
|
134 |
if os.path.normpath(i) == os.path.normpath(path): |
|
135 |
break |
|
136 |
|
|
130 | 137 |
if utils.IsBelowDir(i, path): |
131 | 138 |
break |
132 | 139 |
else: |
... | ... | |
150 | 157 |
|
151 | 158 |
|
152 | 159 |
def CheckFileStoragePathAcceptance( |
153 |
path, _filename=pathutils.FILE_STORAGE_PATHS_FILE): |
|
160 |
path, _filename=pathutils.FILE_STORAGE_PATHS_FILE, |
|
161 |
exact_match_ok=False): |
|
154 | 162 |
"""Checks if a path is allowed for file storage. |
155 | 163 |
|
156 | 164 |
@type path: string |
... | ... | |
164 | 172 |
raise errors.FileStoragePathError("Path '%s' uses a forbidden prefix" % |
165 | 173 |
path) |
166 | 174 |
|
167 |
_CheckFileStoragePath(path, allowed) |
|
175 |
_CheckFileStoragePath(path, allowed, exact_match_ok=exact_match_ok) |
|
176 |
|
|
177 |
|
|
178 |
def _CheckFileStoragePathExistance(path): |
|
179 |
"""Checks whether the given path is usable on the file system. |
|
180 |
|
|
181 |
This checks wether the path is existing, a directory and writable. |
|
182 |
|
|
183 |
@type path: string |
|
184 |
@param path: path to check |
|
185 |
|
|
186 |
""" |
|
187 |
if not os.path.isdir(path): |
|
188 |
raise errors.FileStoragePathError("Path '%s' is not exisiting or not a" |
|
189 |
" directory." % path) |
|
190 |
if not os.access(path, os.W_OK): |
|
191 |
raise errors.FileStoragePathError("Path '%s' is not writable" % path) |
|
192 |
|
|
193 |
|
|
194 |
def CheckFileStoragePath( |
|
195 |
path, _allowed_paths_file=pathutils.FILE_STORAGE_PATHS_FILE): |
|
196 |
"""Checks whether the path exists and is acceptable to use. |
|
197 |
|
|
198 |
@type path: string |
|
199 |
@param path: path to check |
|
200 |
@rtype: string |
|
201 |
@returns: error message if the path is not ready to use |
|
202 |
|
|
203 |
""" |
|
204 |
try: |
|
205 |
CheckFileStoragePathAcceptance(path, _filename=_allowed_paths_file, |
|
206 |
exact_match_ok=True) |
|
207 |
except errors.FileStoragePathError, e: |
|
208 |
return e.message |
|
209 |
if not os.path.isdir(path): |
|
210 |
return "Path '%s' is not exisiting or not a directory." % path |
|
211 |
if not os.access(path, os.W_OK): |
|
212 |
return "Path '%s' is not writable" % path |
Also available in: Unified diff