def test_is_hidden(): with tempfile.TemporaryDirectory() as root: subdir1 = os.path.join(root, 'subdir') os.makedirs(subdir1) assert not is_hidden(subdir1, root) assert not is_file_hidden(subdir1) subdir2 = os.path.join(root, '.subdir2') os.makedirs(subdir2) assert is_hidden(subdir2, root) assert is_file_hidden(subdir2) # root dir is always visible assert not is_hidden(subdir2, subdir2) subdir34 = os.path.join(root, 'subdir3', '.subdir4') os.makedirs(subdir34) assert is_hidden(subdir34, root) assert is_hidden(subdir34) subdir56 = os.path.join(root, '.subdir5', 'subdir6') os.makedirs(subdir56) assert is_hidden(subdir56, root) assert is_hidden(subdir56) assert not is_file_hidden(subdir56) assert not is_file_hidden(subdir56, os.stat(subdir56))
def test_is_hidden_win32_pypy(): import ctypes with tempfile.TemporaryDirectory() as root: subdir1 = os.path.join(root, 'subdir') os.makedirs(subdir1) assert not is_hidden(subdir1, root) subprocess.check_call(["attrib", "+h", subdir1]) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. assert not is_hidden(subdir1, root) # Verify the warning was triggered assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) assert "hidden files are not detectable on this system" in str(w[-1].message) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. assert not is_file_hidden(subdir1) # Verify the warning was triggered assert len(w) == 1 assert issubclass(w[-1].category, UserWarning) assert "hidden files are not detectable on this system" in str(w[-1].message)
async def _dir_model(self, path, content=True): """Build a model for a directory if content is requested, will include a listing of the directory """ os_path = self._get_os_path(path) four_o_four = u'directory does not exist: %r' % path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir) and not self.allow_hidden: self.log.info( "Refusing to serve hidden directory %r, via 404 Error", os_path) raise web.HTTPError(404, four_o_four) model = self._base_model(path) model['type'] = 'directory' model['size'] = None if content: model['content'] = contents = [] os_dir = self._get_os_path(path) dir_contents = await run_sync_in_worker_thread(os.listdir, os_dir) for name in dir_contents: try: os_path = os.path.join(os_dir, name) except UnicodeDecodeError as e: self.log.warning("failed to decode filename '%s': %s", name, e) continue try: st = await run_sync_in_worker_thread(os.lstat, os_path) except OSError as e: # skip over broken symlinks in listing if e.errno == errno.ENOENT: self.log.warning("%s doesn't exist", os_path) else: self.log.warning("Error stat-ing %s: %s", os_path, e) continue if (not stat.S_ISLNK(st.st_mode) and not stat.S_ISREG(st.st_mode) and not stat.S_ISDIR(st.st_mode)): self.log.debug("%s not a regular file", os_path) continue if self.should_list(name): if self.allow_hidden or not is_file_hidden(os_path, stat_res=st): contents.append(await self.get(path='%s/%s' % (path, name), content=False)) model['format'] = 'json' return model
def test_is_hidden_win32_cpython(): import ctypes with tempfile.TemporaryDirectory() as root: subdir1 = os.path.join(root, 'subdir') os.makedirs(subdir1) assert not is_hidden(subdir1, root) subprocess.check_call(["attrib", "+h", subdir1]) assert is_hidden(subdir1, root) assert is_file_hidden(subdir1)
def test_is_hidden_win32(): import ctypes with tempfile.TemporaryDirectory() as root: subdir1 = os.path.join(root, 'subdir') os.makedirs(subdir1) assert not is_hidden(subdir1, root) r = ctypes.windll.kernel32.SetFileAttributesW(subdir1, 0x02) print(r) # Helps debugging assert is_hidden(subdir1, root) assert is_file_hidden(subdir1)
def _dir_model(self, path, content=True): """Build a model for a directory if content is requested, will include a listing of the directory """ os_path = self._get_os_path(path) four_o_four = u'directory does not exist: %r' % path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir) and not self.allow_hidden: self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path ) raise web.HTTPError(404, four_o_four) model = self._base_model(path) model['type'] = 'directory' model['size'] = None if content: model['content'] = contents = [] os_dir = self._get_os_path(path) for name in os.listdir(os_dir): try: os_path = os.path.join(os_dir, name) except UnicodeDecodeError as e: self.log.warning( "failed to decode filename '%s': %s", name, e) continue try: st = os.lstat(os_path) except OSError as e: # skip over broken symlinks in listing if e.errno == errno.ENOENT: self.log.warning("%s doesn't exist", os_path) elif e.errno != errno.EACCES: # Don't provide clues about protected files self.log.warning("Error stat-ing %s: %s", os_path, e) continue if (not stat.S_ISLNK(st.st_mode) and not stat.S_ISREG(st.st_mode) and not stat.S_ISDIR(st.st_mode)): self.log.debug("%s not a regular file", os_path) continue try: if self.should_list(name): if self.allow_hidden or not is_file_hidden(os_path, stat_res=st): contents.append( self.get(path='%s/%s' % (path, name), content=False) ) except OSError as e: # ELOOP: recursive symlink, also don't show failure due to permissions if e.errno not in [errno.ELOOP, errno.EACCES]: self.log.warning( "Unknown error checking if file %r is hidden", os_path, exc_info=True, ) model['format'] = 'json' return model