diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py index 5566013..4cb674b 100644 --- a/IPython/html/base/handlers.py +++ b/IPython/html/base/handlers.py @@ -265,7 +265,7 @@ class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler): """ abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path) abs_root = os.path.abspath(root) - if is_hidden(abs_root, abs_path): + if is_hidden(abs_path, abs_root): raise web.HTTPError(404) return abs_path diff --git a/IPython/html/services/notebooks/filenbmanager.py b/IPython/html/services/notebooks/filenbmanager.py index 5e8bd35..33606be 100644 --- a/IPython/html/services/notebooks/filenbmanager.py +++ b/IPython/html/services/notebooks/filenbmanager.py @@ -127,7 +127,7 @@ class FileNotebookManager(NotebookManager): """ path = path.strip('/') os_path = self.get_os_path(path=path) - return is_hidden(self.notebook_dir, os_path) + return is_hidden(os_path, self.notebook_dir) def get_os_path(self, name=None, path=''): """Given a notebook name and a URL path, return its file system @@ -179,13 +179,13 @@ class FileNotebookManager(NotebookManager): """List the directories for a given API style path.""" path = path.strip('/') os_path = self.get_os_path('', path) - if not os.path.isdir(os_path) or is_hidden(self.notebook_dir, os_path): + if not os.path.isdir(os_path) or is_hidden(os_path, self.notebook_dir): raise web.HTTPError(404, u'directory does not exist: %r' % os_path) dir_names = os.listdir(os_path) dirs = [] for name in dir_names: os_path = self.get_os_path(name, path) - if os.path.isdir(os_path) and not is_hidden(self.notebook_dir, os_path): + if os.path.isdir(os_path) and not is_hidden(os_path, self.notebook_dir): try: model = self.get_dir_model(name, path) except IOError: diff --git a/IPython/html/tests/test_utils.py b/IPython/html/tests/test_utils.py index a58d372..0eb6166 100644 --- a/IPython/html/tests/test_utils.py +++ b/IPython/html/tests/test_utils.py @@ -66,10 +66,11 @@ def test_is_hidden(): with TemporaryDirectory() as root: subdir1 = os.path.join(root, 'subdir') os.makedirs(subdir1) - nt.assert_equal(is_hidden(root, subdir1), False) + nt.assert_equal(is_hidden(subdir1, root), False) subdir2 = os.path.join(root, '.subdir2') os.makedirs(subdir2) - nt.assert_equal(is_hidden(root, subdir2), True) + nt.assert_equal(is_hidden(subdir2, root), True) subdir34 = os.path.join(root, 'subdir3', '.subdir4') os.makedirs(subdir34) - nt.assert_equal(is_hidden(root, subdir34), True) + nt.assert_equal(is_hidden(subdir34, root), True) + nt.assert_equal(is_hidden(subdir34), True) diff --git a/IPython/html/utils.py b/IPython/html/utils.py index 75d0425..d0442a2 100644 --- a/IPython/html/utils.py +++ b/IPython/html/utils.py @@ -80,24 +80,31 @@ def url_unescape(path): for p in py3compat.unicode_to_str(path).split('/') ]) -def is_hidden(absolute_root, absolute_path): +def is_hidden(abs_path, abs_root=''): """Is a file is hidden or contained in a hidden directory. - Hidden is determined by either name starting with '.' or the UF_HIDDEN - flag as reported by stat. + This will start with the rightmost path element and work backwards to the + given root to see if a path is hidden or in a hidden directory. Hidden is + determined by either name starting with '.' or the UF_HIDDEN flag as + reported by stat. Parameters ---------- - absolute_root : unicode - absolute_path : unicode + abs_path : unicode + The absolute path to check for hidden directories. + abs_root : unicode + The absolute path of the root directory in which hidden directories + should be check for. """ - inside_root = absolute_path[len(absolute_root):] + if not abs_root: + abs_root = abs_path.split(os.sep, 1)[0] + os.sep + inside_root = abs_path[len(abs_root):] if any(part.startswith('.') for part in inside_root.split(os.sep)): return True # check UF_HIDDEN on any location up to root - path = absolute_path - while path and path.startswith(absolute_root) and path != absolute_root: + path = abs_path + while path and path.startswith(abs_root) and path != abs_root: st = os.stat(path) if getattr(st, 'st_flags', 0) & UF_HIDDEN: return True