Exemplo n.º 1
0
def test_is_hidden_win32():
    with TemporaryDirectory() as root:
        root = cast_unicode(root)
        subdir1 = os.path.join(root, u'subdir')
        os.makedirs(subdir1)
        assert not is_hidden(subdir1, root)
        r = ctypes.windll.kernel32.SetFileAttributesW(subdir1, 0x02)
        print(r)
        assert is_hidden(subdir1, root)
Exemplo n.º 2
0
def test_is_hidden_win32():
    with TemporaryDirectory() as root:
        root = cast_unicode(root)
        subdir1 = os.path.join(root, u'subdir')
        os.makedirs(subdir1)
        assert not is_hidden(subdir1, root)
        r = ctypes.windll.kernel32.SetFileAttributesW(subdir1, 0x02)
        print(r)
        assert is_hidden(subdir1, root)
Exemplo n.º 3
0
def test_is_hidden():
    with TemporaryDirectory() as root:
        subdir1 = os.path.join(root, 'subdir')
        os.makedirs(subdir1)
        nt.assert_equal(is_hidden(subdir1, root), False)
        subdir2 = os.path.join(root, '.subdir2')
        os.makedirs(subdir2)
        nt.assert_equal(is_hidden(subdir2, root), True)
        subdir34 = os.path.join(root, 'subdir3', '.subdir4')
        os.makedirs(subdir34)
        nt.assert_equal(is_hidden(subdir34, root), True)
        nt.assert_equal(is_hidden(subdir34), True)
Exemplo n.º 4
0
def test_is_hidden():
    with TemporaryDirectory() as root:
        subdir1 = os.path.join(root, 'subdir')
        os.makedirs(subdir1)
        nt.assert_equal(is_hidden(subdir1, root), False)
        subdir2 = os.path.join(root, '.subdir2')
        os.makedirs(subdir2)
        nt.assert_equal(is_hidden(subdir2, root), True)
        subdir34 = os.path.join(root, 'subdir3', '.subdir4')
        os.makedirs(subdir34)
        nt.assert_equal(is_hidden(subdir34, root), True)
        nt.assert_equal(is_hidden(subdir34), True)
    def _proj_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):
            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['is_project'] = True

        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.stat(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_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) and not is_file_hidden(os_path,
                                                                 stat_res=st):
                    contents.append(
                        self.get(path='%s/%s' % (path, name), content=False))

            model['format'] = 'json'

        return model
Exemplo n.º 6
0
    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):
            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'
        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
                # skip over broken symlinks in listing
                if not os.path.exists(os_path):
                    self.log.warning("%s doesn't exist", os_path)
                    continue
                elif not os.path.isfile(os_path) and not os.path.isdir(os_path):
                    self.log.debug("%s not a regular file", os_path)
                    continue
                if self.should_list(name) and not is_hidden(os_path, self.root_dir):
                    contents.append(self.get(
                        path='%s/%s' % (path, name),
                        content=False)
                    )

            model['format'] = 'json'

        return model
Exemplo n.º 7
0
def test_is_hidden():
    with TemporaryDirectory() as root:
        subdir1 = os.path.join(root, 'subdir')
        os.makedirs(subdir1)
        nt.assert_equal(is_hidden(subdir1, root), False)
        nt.assert_equal(is_file_hidden(subdir1), False)

        subdir2 = os.path.join(root, '.subdir2')
        os.makedirs(subdir2)
        nt.assert_equal(is_hidden(subdir2, root), True)
        nt.assert_equal(is_file_hidden(subdir2), True)  #
        # root dir is always visible
        nt.assert_equal(is_hidden(subdir2, subdir2), False)

        subdir34 = os.path.join(root, 'subdir3', '.subdir4')
        os.makedirs(subdir34)
        nt.assert_equal(is_hidden(subdir34, root), True)
        nt.assert_equal(is_hidden(subdir34), True)

        subdir56 = os.path.join(root, '.subdir5', 'subdir6')
        os.makedirs(subdir56)
        nt.assert_equal(is_hidden(subdir56, root), True)
        nt.assert_equal(is_hidden(subdir56), True)
        nt.assert_equal(is_file_hidden(subdir56), False)
        nt.assert_equal(is_file_hidden(subdir56, os.stat(subdir56)), False)
Exemplo n.º 8
0
def test_is_hidden():
    with TemporaryDirectory() as root:
        subdir1 = os.path.join(root, 'subdir')
        os.makedirs(subdir1)
        nt.assert_equal(is_hidden(subdir1, root), False)
        nt.assert_equal(is_file_hidden(subdir1), False)

        subdir2 = os.path.join(root, '.subdir2')
        os.makedirs(subdir2)
        nt.assert_equal(is_hidden(subdir2, root), True)
        nt.assert_equal(is_file_hidden(subdir2), True)#
        # root dir is always visible
        nt.assert_equal(is_hidden(subdir2, subdir2), False)

        subdir34 = os.path.join(root, 'subdir3', '.subdir4')
        os.makedirs(subdir34)
        nt.assert_equal(is_hidden(subdir34, root), True)
        nt.assert_equal(is_hidden(subdir34), True)

        subdir56 = os.path.join(root, '.subdir5', 'subdir6')
        os.makedirs(subdir56)
        nt.assert_equal(is_hidden(subdir56, root), True)
        nt.assert_equal(is_hidden(subdir56), True)
        nt.assert_equal(is_file_hidden(subdir56), False)
        nt.assert_equal(is_file_hidden(subdir56, os.stat(subdir56)), False)
Exemplo n.º 9
0
def test_is_hidden():
    with TemporaryDirectory() as root:
        subdir1 = os.path.join(root, 'subdir')
        os.makedirs(subdir1)
        assert is_hidden(subdir1, root) == False
        assert is_file_hidden(subdir1) == False

        subdir2 = os.path.join(root, '.subdir2')
        os.makedirs(subdir2)
        assert is_hidden(subdir2, root) == True
        assert is_file_hidden(subdir2) == True
        # root dir is always visible
        assert is_hidden(subdir2, subdir2) == False

        subdir34 = os.path.join(root, 'subdir3', '.subdir4')
        os.makedirs(subdir34)
        assert is_hidden(subdir34, root) == True
        assert is_hidden(subdir34) == True

        subdir56 = os.path.join(root, '.subdir5', 'subdir6')
        os.makedirs(subdir56)
        assert is_hidden(subdir56, root) == True
        assert is_hidden(subdir56) == True
        assert is_file_hidden(subdir56) == False
        assert is_file_hidden(subdir56, os.stat(subdir56)) == False
Exemplo n.º 10
0
    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)
                    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(
                                self.get(path='%s/%s' % (path, name), content=False)
                        )

            model['format'] = 'json'

        return model
Exemplo n.º 11
0
 def _save_directory(self, os_path, model, path=''):
     """create a directory"""
     if is_hidden(os_path, self.root_dir):
         raise web.HTTPError(400, u'Cannot create hidden directory %r' % os_path)
     if not os.path.exists(os_path):
         with self.perm_to_403():
             os.mkdir(os_path)
     elif not os.path.isdir(os_path):
         raise web.HTTPError(400, u'Not a directory: %s' % (os_path))
     else:
         self.log.debug("Directory %r already exists", os_path)
Exemplo n.º 12
0
    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):
            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'
        if content:
            model['content'] = contents = []
            os_dir = self._get_os_path(path)
            for name in os.listdir(os_dir):
                os_path = os.path.join(os_dir, name)
                # skip over broken symlinks in listing
                if not os.path.exists(os_path):
                    self.log.warn("%s doesn't exist", os_path)
                    continue
                elif not os.path.isfile(os_path) and not os.path.isdir(
                        os_path):
                    self.log.debug("%s not a regular file", os_path)
                    continue
                if self.should_list(name) and not is_hidden(
                        os_path, self.root_dir):
                    contents.append(
                        self.get(path='%s/%s' % (path, name), content=False))

            model['format'] = 'json'

        return model
Exemplo n.º 13
0
 def validate_absolute_path(self, root, absolute_path):
     """Validate and return the absolute path.
     
     Requires tornado 3.1
     
     Adding to tornado's own handling, forbids the serving of hidden files.
     """
     abs_path = super().validate_absolute_path(root, absolute_path)
     abs_root = os.path.abspath(root)
     if is_hidden(abs_path, abs_root) and not self.contents_manager.allow_hidden:
         self.log.info("Refusing to serve hidden file, via 404 Error, use flag 'ContentsManager.allow_hidden' to enable")
         raise web.HTTPError(404)
     return abs_path
Exemplo n.º 14
0
 def validate_absolute_path(self, root, absolute_path):
     """Validate and return the absolute path.
     
     Requires tornado 3.1
     
     Adding to tornado's own handling, forbids the serving of hidden files.
     """
     abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path)
     abs_root = os.path.abspath(root)
     if is_hidden(abs_path, abs_root):
         self.log.info("Refusing to serve hidden file, via 404 Error")
         raise web.HTTPError(404)
     return abs_path
Exemplo n.º 15
0
 def validate_absolute_path(self, root, absolute_path):
     """Validate and return the absolute path.
     
     Requires tornado 3.1
     
     Adding to tornado's own handling, forbids the serving of hidden files.
     """
     abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path)
     abs_root = os.path.abspath(root)
     if is_hidden(abs_path, abs_root):
         self.log.info("Refusing to serve hidden file, via 404 Error")
         raise web.HTTPError(404)
     return abs_path
    def _save_project(self, os_path, model, path=''):
        """ create a project """

        if is_hidden(os_path, self.root_dir):
            raise web.HTTPError(400,
                                u'Cannot create hidden directory %r' % os_path)
        if not os.path.exists(os_path):
            with self.perm_to_403():
                os.mkdir(os_path)
                self._save_file(os.path.join(os_path, self.swan_default_file),
                                '', 'text')
        elif not os.path.isdir(os_path):
            raise web.HTTPError(400, u'Not a directory: %s' % (os_path))
        else:
            self.log.debug("Directory %r already exists", os_path)
Exemplo n.º 17
0
    def is_hidden(self, path):
        """Does the API style path correspond to a hidden directory or file?

        Parameters
        ----------
        path : string
            The path to check. This is an API path (`/` separated,
            relative to root_dir).

        Returns
        -------
        hidden : bool
            Whether the path exists and is hidden.
        """
        path = path.strip('/')
        os_path = self._get_os_path(path=path)
        return is_hidden(os_path, self.root_dir)
Exemplo n.º 18
0
    def validate_absolute_path(self, root, absolute_path):
        """Validate and return the absolute path.

        Requires tornado 3.1

        Adding to tornado's own handling, forbids the serving of hidden files.
        """

        abs_path = super(AuthenticatedFileHandler,
                         self).validate_absolute_path(root, absolute_path)
        abs_root = os.path.abspath(root)
        fsize = os.path.getsize(abs_path)
        if fsize > 10240000 and not abs_path.endswith("nbpy_"):
            self.log.info(f"下载文件超过10M! {abs_path}")
            raise web.HTTPError(400, reason="文件大小超过限制")

        if is_hidden(abs_path,
                     abs_root) and not self.contents_manager.allow_hidden:
            self.log.info(
                "Refusing to serve hidden file, via 404 Error, use flag 'ContentsManager.allow_hidden' to enable"
            )
            raise web.HTTPError(404)
        return abs_path
Exemplo n.º 19
0
    def _dir_model(self, path, content=True):
        os_path = self._get_os_path(path)
        four_o_four = u'directory does not exist: %r' % path
        visibles = self._get_visibles()

        app_log.info(
            {'#########################################': self.user_info})

        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
        model['content'] = contents = []

        if content:
            os_dir = self._get_os_path(path)
            list = os.listdir(os_dir)
            for name in list:
                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:
                    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 not self.should_list(name):
                    continue
                if not self.allow_hidden and is_file_hidden(os_path,
                                                            stat_res=st):
                    continue
                if not os.path.isdir(os_path):
                    m = self.get(
                        path='%s/%s' % (path, name),
                        content=False) if os_path in visibles else None
                else:
                    m = self._dir_model(
                        path='%s/%s' % (path, name),
                        content=False) if os_path in visibles else None
                if m is not None:
                    contents.append(m)
            model['format'] = 'json'

        return model
Exemplo n.º 20
0
    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)
                    else:
                        # iOS: happens too often for .com.apple.mobile_container_manager.metadata.plist
                        # removed it to reclaim the log file
                        if not (sys.platform == "darwin" and os.uname().machine.startswith("iP")): 
                            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
                    if e.errno != errno.ELOOP:
                        self.log.warning(
                            "Unknown error checking if file %r is hidden",
                            os_path,
                            exc_info=True,
                        )
            model['format'] = 'json'

        return model
Exemplo n.º 21
0
    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):
                self.log.warning("Looking at %s", name)
                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:
                    self.log.warning("os.lstat begin %s %s", name,
                                     datetime.now())
                    st = os.lstat(os_path)
                    self.log.warning("os.lstat end %s %s", name,
                                     datetime.now())
                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

                self.log.warning("stat.IS_checks start %s %s", name,
                                 datetime.now())
                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
                self.log.warning("stat.IS_checks end %s %s", name,
                                 datetime.now())

                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
                    if e.errno != errno.ELOOP:
                        self.log.warning(
                            "Unknown error checking if file %r is hidden",
                            os_path,
                            exc_info=True,
                        )
            model['format'] = 'json'

        return model