fn insert(
        &mut self,
        path: &Path,
        events: &mut Vec<Event>,
        _entries: &mut EntryMap,
    ) -> FsResult<Option<EntryKey>> {
        if !self.passes(path, _entries) {
            info!("ignoring {:?}", path);
            return Ok(None);
        }

        if !(path.exists() || path.read_link().is_ok()) {
            warn!("attempted to insert non existent path {:?}", path);
            return Ok(None);
        }

        let parent_ref = self.create_dir(path.parent().unwrap(), _entries)?;
        let parent_ref = self.follow_links(parent_ref, _entries);

        if parent_ref.is_none() {
            return Ok(None);
        }

        let parent_ref = parent_ref.unwrap();

        // We only need the last component, the parents are already inserted.
        let component = into_components(path)
            .pop()
            .ok_or_else(|| Error::PathNotValid(path.into()))?;

        // If the path is a dir, we can use create_dir to do the insert
        if path.is_dir() {
            return self.create_dir(path, _entries).map(Some);
        }

        enum Action {
            Return(EntryKey),
            CreateSymlink(PathBuf),
            CreateFile,
        }

        let parent = _entries.get_mut(parent_ref).ok_or(Error::Lookup)?;
        let action = match parent
            .children_mut()
            .ok_or(Error::ParentNotValid)?
            .entry(component.clone())
        {
            HashMapEntry::Occupied(v) => Action::Return(*v.get()),
            HashMapEntry::Vacant(_) => match path.read_link() {
                Ok(real) => Action::CreateSymlink(real),
                Err(_) => Action::CreateFile,
            },
        };

        match action {
            Action::Return(key) => Ok(Some(key)),
            Action::CreateFile => {
                let wd = self
                    .watcher
                    .watch(path)
                    .map_err(|e| Error::Watch(path.to_owned(), e))?;

                let inode = path.metadata().map_err(Error::File)?.ino();
                self.wd_by_inode.insert(inode, wd.clone());
                let new_entry = Entry::File {
                    name: component,
                    parent: parent_ref,
                    wd,
                    data: RefCell::new(
                        TailedFile::new(path, Some(self.resume_events_send.clone()))
                            .map_err(Error::File)?,
                    ),
                };

                Metrics::fs().increment_tracked_files();

                let new_key = self.register_as_child(parent_ref, new_entry, _entries)?;
                events.push(Event::New(new_key));
                Ok(Some(new_key))
            }
            Action::CreateSymlink(real) => {
                let wd = self
                    .watcher
                    .watch(path)
                    .map_err(|e| Error::Watch(path.to_owned(), e))?;

                let new_entry = Entry::Symlink {
                    name: component,
                    parent: parent_ref,
                    link: real.clone(),
                    wd,
                    rules: into_rules(real.clone()),
                };

                let new_key = self.register_as_child(parent_ref, new_entry, _entries)?;

                if self
                    .insert(&real, events, _entries)
                    .unwrap_or(None)
                    .is_none()
                {
                    debug!(
                        "inserting symlink {:?} which points to invalid path {:?}",
                        path, real
                    );
                }

                events.push(Event::New(new_key));
                Ok(Some(new_key))
            }
        }
    }
/// Inserts a new entry when the path validates the inclusion/exclusion rules.
///
/// Returns `Ok(Some(entry))` pointing to the newly created entry.
///
/// When the path doesn't pass the rules or the path is invalid, it returns `Ok(None)`.
/// When the file watcher can't be added or the parent dir can not be created, it
/// returns an `Err`.
Inserts a new entry when the path validates the inclusion/exclusion rules.
Returns `Ok(Some(entry))` pointing to the newly created entry.

When the path doesn't pass the rules or the path is invalid, it returns `Ok(None)`.
When the file watcher can't be added or the parent dir can not be created, it
returns an `Err`.
Inserts a new entry when the path validates the inclusion / exclusion rules . Returns ` Ok ( Some ( entry )) ` pointing to the newly created entry . When the path doesn ' t pass the rules or the path is invalid it returns ` Ok ( None ) ` . When the file watcher can ' t be added or the parent dir can not be created it returns an ` Err ` .
fn insert(
        &mut self,
        path: &Path,
        events: &mut Vec<Event>,
        _entries: &mut EntryMap,
    ) -> FsResult<Option<EntryKey>> {
        if !self.passes(path, _entries) {
            info!("ignoring {:?}", path);
            return Ok(None);
        }
        if !(path.exists() || path.read_link().is_ok()) {
            warn!("attempted to insert non existent path {:?}", path);
            return Ok(None);
        }
        let parent_ref = self.create_dir(path.parent().unwrap(), _entries)?;
        let parent_ref = self.follow_links(parent_ref, _entries);
        if parent_ref.is_none() {
            return Ok(None);
        }
        let parent_ref = parent_ref.unwrap();
        // We only need the last component, the parents are already inserted.
        let component = into_components(path)
            .pop()
            .ok_or_else(|| Error::PathNotValid(path.into()))?;
        // If the path is a dir, we can use create_dir to do the insert
        if path.is_dir() {
            return self.create_dir(path, _entries).map(Some);
        }
        enum Action {
            Return(EntryKey),
            CreateSymlink(PathBuf),
            CreateFile,
        }
        let parent = _entries.get_mut(parent_ref).ok_or(Error::Lookup)?;
        let action = match parent
            .children_mut()
            .ok_or(Error::ParentNotValid)?
            .entry(component.clone())
        {
            HashMapEntry::Occupied(v) => Action::Return(*v.get()),
            HashMapEntry::Vacant(_) => match path.read_link() {
                Ok(real) => Action::CreateSymlink(real),
                Err(_) => Action::CreateFile,
            },
        };
        match action {
            Action::Return(key) => Ok(Some(key)),
            Action::CreateFile => {
                let wd = self
                    .watcher
                    .watch(path)
                    .map_err(|e| Error::Watch(path.to_owned(), e))?;
                let inode = path.metadata().map_err(Error::File)?.ino();
                self.wd_by_inode.insert(inode, wd.clone());
                let new_entry = Entry::File {
                    name: component,
                    parent: parent_ref,
                    wd,
                    data: RefCell::new(
                        TailedFile::new(path, Some(self.resume_events_send.clone()))
                            .map_err(Error::File)?,
                    ),
                };
                Metrics::fs().increment_tracked_files();
                let new_key = self.register_as_child(parent_ref, new_entry, _entries)?;
                events.push(Event::New(new_key));
                Ok(Some(new_key))
            }
            Action::CreateSymlink(real) => {
                let wd = self
                    .watcher
                    .watch(path)
                    .map_err(|e| Error::Watch(path.to_owned(), e))?;
                let new_entry = Entry::Symlink {
                    name: component,
                    parent: parent_ref,
                    link: real.clone(),
                    wd,
                    rules: into_rules(real.clone()),
                };
                let new_key = self.register_as_child(parent_ref, new_entry, _entries)?;
                if self
                    .insert(&real, events, _entries)
                    .unwrap_or(None)
                    .is_none()
                {
                    debug!(
                        "inserting symlink {:?} which points to invalid path {:?}",
                        path, real
                    );
                }
                events.push(Event::New(new_key));
                Ok(Some(new_key))
            }
        }
    }
fn insert ( & mut self , path : & Path , events : & mut Vec < Event > , _entries : & mut EntryMap , ) -> FsResult < Option < EntryKey > > { if ! self . passes ( path , _entries ) { info ! ( " " path ) ; return Ok ( None ) ; } if ! ( path . exists ( ) || path . read_link ( ) . is_ok ( ) ) { warn ! ( " " path ) ; return Ok ( None ) ; } let parent_ref = self . create_dir ( path . parent ( ) . unwrap ( ) , _entries ) ? ; let parent_ref = self . follow_links ( parent_ref , _entries ) ; if parent_ref . is_none ( ) { return Ok ( None ) ; } let parent_ref = parent_ref . unwrap ( ) ; // We only need the last component, the parents are already inserted. let component = into_components ( path ) . pop ( ) . ok_or_else ( | | Error :: PathNotValid ( path . into ( ) ) ) ? ; // If the path is a dir, we can use create_dir to do the insert if path . is_dir ( ) { return self . create_dir ( path , _entries ) . map ( Some ) ; } enum Action { Return ( EntryKey ) , CreateSymlink ( PathBuf ) , CreateFile , } let parent = _entries . get_mut ( parent_ref ) . ok_or ( Error :: Lookup ) ? ; let action = match parent . children_mut ( ) . ok_or ( Error :: ParentNotValid ) ? . entry ( component . clone ( ) ) { HashMapEntry :: Occupied ( v ) => Action :: Return ( * v . get ( ) ) , HashMapEntry :: Vacant ( _ ) => match path . read_link ( ) { Ok ( real ) => Action :: CreateSymlink ( real ) , Err ( _ ) => Action :: CreateFile , } , } ; match action { Action :: Return ( key ) => Ok ( Some ( key ) ) , Action :: CreateFile => { let wd = self . watcher . watch ( path ) . map_err ( | e | Error :: Watch ( path . to_owned ( ) , e ) ) ? ; let inode = path . metadata ( ) . map_err ( Error :: File ) ? . ino ( ) ; self . wd_by_inode . insert ( inode , wd . clone ( ) ) ; let new_entry = Entry :: File { name : component , parent : parent_ref , wd , data : RefCell :: new ( TailedFile :: new ( path , Some ( self . resume_events_send . clone ( ) ) ) . map_err ( Error :: File ) ? , ) , } ; Metrics :: fs ( ) . increment_tracked_files ( ) ; let new_key = self . register_as_child ( parent_ref , new_entry , _entries ) ? ; events . push ( Event :: New ( new_key ) ) ; Ok ( Some ( new_key ) ) } Action :: CreateSymlink ( real ) => { let wd = self . watcher . watch ( path ) . map_err ( | e | Error :: Watch ( path . to_owned ( ) , e ) ) ? ; let new_entry = Entry :: Symlink { name : component , parent : parent_ref , link : real . clone ( ) , wd , rules : into_rules ( real . clone ( ) ) , } ; let new_key = self . register_as_child ( parent_ref , new_entry , _entries ) ? ; if self . insert ( & real , events , _entries ) . unwrap_or ( None ) . is_none ( ) { debug ! ( " " path real ) ; } events . push ( Event :: New ( new_key ) ) ; Ok ( Some ( new_key ) ) } } }
Inserts a new entry when the path validates the inclusion/exclusion rules.
Inserts a new entry when the path validates the inclusion / exclusion rules .
&mut self
path
&Path
events
&mut Vec<Event>
_entries
&mut EntryMap
&mut self
path
&Path
events
&mut Vec<Event>
_entries
&mut EntryMap