Exemplo n.º 1
0
 def _hide_if_starts_with_dot(self, path):
     logger.debug('Hiding ? %s', path)
     if path and path[-1][0] == '.':
         logger.debug('Hiding %s', path)
         with contextlib.suppress(PermissionError):
             winapi.SetFileAttributes(cc_path_to_fs(path, self.root),
                                      winapi.FILE_ATTRIBUTE_HIDDEN)
Exemplo n.º 2
0
    def open_read(self, path, expected_version_id=None):
        fs_path = cc_path_to_fs(path, self.root)
        # self.explorer_notifier.in_queue.put(fs_path)
        props = get_eventprops_from_stat(fs_path)
        if expected_version_id is not None and props[
                'version_id'] != expected_version_id:
            raise VersionIdNotMatchingError(storage_id=self.storage_id,
                                            path=path,
                                            version_b=expected_version_id,
                                            version_a=props['version_id'])

        try:
            file_obj = WindowsFileReader(fs_path, self.tempdir)
            return file_obj
        except WindowsError as error:
            # if error code is 32 there is a sharing violation -> currently not possible
            if error.winerror == 32:
                raise CurrentlyNotPossibleError(self.storage_id,
                                                origin_error=error)
            else:
                raise
Exemplo n.º 3
0
    def start_events(self):
        """ For windows we are not going to use the watchdog class"""

        if self.watch_thread is None or \
                (self.watch_thread is not None and not self.watch_thread.is_alive()):
            self.watch_thread = WatchThread(
                error_callback=self.watcher_error_callback)
            self.watch_thread.start()
            time.sleep(0.1)

            logger.debug('Enabling watchers')
            for filter_node in self.filter_tree:
                # add a flat watcher for every filter node with children to observe the
                # files in the same directory and a recursive one to all children if none
                # to watch them recursively

                recursive = not bool(filter_node.children)
                path = cc_path_to_fs(filter_node.path, self.root)
                logger.debug('Adding watcher for "%s", recursive: %s', path,
                             recursive)
                self.watch_thread.add_watcher(self.watcher_callback,
                                              path=path,
                                              recursive=recursive)
Exemplo n.º 4
0
def test_cc_path_to_fs_root():
    """
    checks if elements with '..' are rejected. But names with '..' should be ok
    """

    assert cc_path_to_fs([], r'c:\hello') == 'c:\\hello\\'
Exemplo n.º 5
0
 def _filter_tree_create(self, node):
     # pass
     if self.watch_thread is not None and self.watch_thread.is_alive():
         fs_path = cc_path_to_fs(node.path, self.root)
         logger.debug('Adding watcher for "%s"', fs_path)
Exemplo n.º 6
0
    def _update(self, cc_path):
        """Inspect the directory to detect what has changed since the last call to `_update`.

        Args:
            :param cc_path: path of directory to update.
            :type cc_path: list of strings.
        """
        logger.info('_update for %s for tree %s', cc_path, self.model)

        # ignore event if parent directory no longer exists on the fs
        parent_folder = cc_path_to_fs(cc_path[:-1], self.real_root)
        if not os.path.exists(parent_folder):
            logger.debug('Event ignored: parent folder no longer exist')
            return

        with TreeToSyncEngineEngineAdapter(node=self.model,
                                           storage_id=self.storage_id,
                                           sync_engine=self._event_sink):
            # Ensure that the path exists in the model.
            parent = self.model
            for idx, name in enumerate(cc_path):
                if parent.has_child(name):
                    parent = parent.get_node([name])
                else:
                    partial_cc_path = cc_path[:idx + 1]
                    parent = parent.add_child(
                        name, props=self.get_props(partial_cc_path))

            directory = cc_path_to_fs(cc_path, self.real_root)

            new_inodes = {
                props['_inode']: (name, props)
                for name, props in self.get_tree_children(cc_path)
            }
            old_inodes = {
                node.props['_inode']: node
                for node in parent.children
            }

            new_inodes_set = new_inodes.keys()
            old_inodes_set = old_inodes.keys()

            inode_intersection = new_inodes_set & old_inodes_set
            removed_inodes = old_inodes_set - new_inodes_set
            added_inodes = new_inodes_set - old_inodes_set

            for inode in inode_intersection:
                old_node = old_inodes[inode]
                new_node_name, new_node_props = new_inodes[inode]

                old_node.props.update(new_node_props)
                old_node.name = new_node_name

            for inode in removed_inodes:
                # TODO: might be moved to a different dir, might be deleted
                old_inodes[inode].delete()

            for inode in added_inodes:
                new_node_name, new_node_props = new_inodes[inode]
                new_node = parent.add_child(new_node_name, new_node_props)
                if new_node_props[jars.IS_DIR]:
                    self._update(new_node.path)