def _get_dir_content_recursively(path): """ignores dir links""" dirs = set() files = set() names = os.listdir(path) for name in names: pathname = os.path.join(path, name) try: mode = os.stat(pathname).st_mode except os.error: logger.debug(u'Path disappeared: %s', pathname) continue # the path promptly disappeared if stat.S_ISDIR(mode): if not host_system.is_link_to_dir(pathname): inner_dirs, inner_files = \ _get_dir_content_recursively(pathname) dirs.update(inner_dirs, [pathname]) files.update(inner_files) elif stat.S_ISREG(mode): files.add(pathname) else: logger.debug(u'Skipping unknown fd mode type: %s, path: %s', mode, pathname) return dirs, files
def _watch_dir(self, path, do_event_sending=False): """Internal implementation of starting watching a directory. Runs in threadpool thread only. """ assert not in_main_thread() path = unicode(path) # ensure that all paths are unicode # check if this path exists try: mode = os.stat(path).st_mode except OSError: logger.debug('Path does not exist, %r', path) return else: if host_system.is_link_to_dir(path): logger.warning('This watcher does not support links %r', path) return assert stat.S_ISDIR(mode), (path, mode) # check if this path already watched if path in self._dirs: logger.debug('Dir already added %s', path) return logger.verbose('Watch dir %r', path) dir_contents = self._get_dir_contents(path) dir_data = DirData(path, dir_contents.dirs, dir_contents.files) with self._dirs_lock: if path not in self._dirs: self._dirs[path] = dir_data else: return if dir_contents.files: logger.verbose('Watch files: %r', dir_contents.files) self._add_paths_to_watcher(dir_contents.files | {path}) if do_event_sending: self._send_event(CreateEvent, path) for file_path in dir_contents.files: self._send_event(CreateEvent, file_path) for dir_path in dir_contents.dirs: self._watch_dir(dir_path, do_event_sending)
def __rescan_watched_dir_for_map(self, watched_dir): """ Rescan anew the given directory and get the bidirectional mapping of all the found symlinks. @rtype: LinksMapPair """ pair = LinksMapPair({}, {}) logger.debug(u'Rescanning %r for symlinks', watched_dir) try: # Make sure the result is in Unicode dir_contents = [ os.path.join(watched_dir, rel) for rel in os.listdir(unicode(watched_dir)) ] except: logger.exception(u'Cannot list %r', watched_dir) else: # For every item inside the directory, check whether it is # a symlink/junction point for item in dir_contents: try: if host_system.is_link_to_dir(item): dest = os.path.abspath( host_system.read_link_to_dir(item)) logger.debug(u'Found %r -> %r', item, dest) SyncDirFSNotifyProxy._inmerge_link_map_pair( into=pair, merge=LinksMapPair(links={ item: SymlinkInfo(base_dir=watched_dir, linked_path=dest) }, reverse_links={dest: {item}})) except: logger.exception(u'Cannot test %r in %r', item, watched_dir) return pair
def __rescan_watched_dir_for_map(self, watched_dir): """ Rescan anew the given directory and get the bidirectional mapping of all the found symlinks. @rtype: LinksMapPair """ pair = LinksMapPair({}, {}) logger.debug(u'Rescanning %r for symlinks', watched_dir) try: # Make sure the result is in Unicode dir_contents = [os.path.join(watched_dir, rel) for rel in os.listdir(unicode(watched_dir))] except: logger.exception(u'Cannot list %r', watched_dir) else: # For every item inside the directory, check whether it is # a symlink/junction point for item in dir_contents: try: if host_system.is_link_to_dir(item): dest = os.path.abspath( host_system.read_link_to_dir(item)) logger.debug(u'Found %r -> %r', item, dest) SyncDirFSNotifyProxy._inmerge_link_map_pair( into=pair, merge=LinksMapPair( links={item: SymlinkInfo( base_dir=watched_dir, linked_path=dest)}, reverse_links={dest: {item}})) except: logger.exception(u'Cannot test %r in %r', item, watched_dir) return pair