Exemple #1
0
        private static void StartMonitoringDirectory(FAMData data, bool justcreated)
        {
            FAMRequest request;

            if (FAMWatcher.FAMMonitorDirectory(ref FAMWatcher.conn, data.Directory, out request, IntPtr.Zero) == -1)
            {
                throw new System.ComponentModel.Win32Exception();
            }
            FileSystemWatcher fsw = data.FSW;

            data.Request = request;
            if (data.IncludeSubdirs)
            {
                foreach (string text in Directory.GetDirectories(data.Directory))
                {
                    FAMData famdata = new FAMData();
                    famdata.FSW            = data.FSW;
                    famdata.Directory      = text;
                    famdata.FileMask       = data.FSW.MangledFilter;
                    famdata.IncludeSubdirs = true;
                    famdata.SubDirs        = new Hashtable();
                    famdata.Enabled        = true;
                    if (justcreated)
                    {
                        FileSystemWatcher obj = fsw;
                        lock (obj)
                        {
                            RenamedEventArgs renamedEventArgs = null;
                            fsw.DispatchEvents(FileAction.Added, text, ref renamedEventArgs);
                            if (fsw.Waiting)
                            {
                                fsw.Waiting = false;
                                System.Threading.Monitor.PulseAll(fsw);
                            }
                        }
                    }
                    FAMWatcher.StartMonitoringDirectory(famdata, justcreated);
                    data.SubDirs[text] = famdata;
                    FAMWatcher.requests[famdata.Request.ReqNum] = famdata;
                }
            }
            if (justcreated)
            {
                foreach (string filename in Directory.GetFiles(data.Directory))
                {
                    FileSystemWatcher obj2 = fsw;
                    lock (obj2)
                    {
                        RenamedEventArgs renamedEventArgs2 = null;
                        fsw.DispatchEvents(FileAction.Added, filename, ref renamedEventArgs2);
                        fsw.DispatchEvents(FileAction.Modified, filename, ref renamedEventArgs2);
                        if (fsw.Waiting)
                        {
                            fsw.Waiting = false;
                            System.Threading.Monitor.PulseAll(fsw);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private static void StartMonitoringDirectory(FAMData data, bool justcreated)
        {
            if (FAMMonitorDirectory(ref conn, data.Directory, out FAMRequest fr, IntPtr.Zero) == -1)
            {
                throw new Win32Exception();
            }
            FileSystemWatcher fSW = data.FSW;

            data.Request = fr;
            if (data.IncludeSubdirs)
            {
                string[] directories = Directory.GetDirectories(data.Directory);
                foreach (string text in directories)
                {
                    FAMData fAMData = new FAMData();
                    fAMData.FSW            = data.FSW;
                    fAMData.Directory      = text;
                    fAMData.FileMask       = data.FSW.MangledFilter;
                    fAMData.IncludeSubdirs = true;
                    fAMData.SubDirs        = new Hashtable();
                    fAMData.Enabled        = true;
                    if (justcreated)
                    {
                        lock (fSW)
                        {
                            RenamedEventArgs renamed = null;
                            fSW.DispatchEvents(FileAction.Added, text, ref renamed);
                            if (fSW.Waiting)
                            {
                                fSW.Waiting = false;
                                System.Threading.Monitor.PulseAll(fSW);
                            }
                        }
                    }
                    StartMonitoringDirectory(fAMData, justcreated);
                    data.SubDirs[text] = fAMData;
                    requests[fAMData.Request.ReqNum] = fAMData;
                }
            }
            if (justcreated)
            {
                string[] files = Directory.GetFiles(data.Directory);
                foreach (string filename in files)
                {
                    lock (fSW)
                    {
                        RenamedEventArgs renamed2 = null;
                        fSW.DispatchEvents(FileAction.Added, filename, ref renamed2);
                        fSW.DispatchEvents(FileAction.Modified, filename, ref renamed2);
                        if (fSW.Waiting)
                        {
                            fSW.Waiting = false;
                            System.Threading.Monitor.PulseAll(fSW);
                        }
                    }
                }
            }
        }
Exemple #3
0
        private void PostEvent(string filename, FileSystemWatcher fsw, FileAction fa, FileSystemInfo changedFsi)
        {
            RenamedEventArgs renamed = null;

            if (fa != 0)
            {
                if (fsw.IncludeSubdirectories && fa == FileAction.Added && changedFsi is DirectoryInfo)
                {
                    KeventData keventData = new KeventData();
                    keventData.FSW            = fsw;
                    keventData.Directory      = changedFsi.FullName;
                    keventData.FileMask       = fsw.MangledFilter;
                    keventData.IncludeSubdirs = fsw.IncludeSubdirectories;
                    keventData.Enabled        = true;
                    lock (this)
                    {
                        StartMonitoringDirectory(keventData);
                    }
                }
                if (fsw.Pattern.IsMatch(filename, ignorecase: true))
                {
                    lock (fsw)
                    {
                        fsw.DispatchEvents(fa, filename, ref renamed);
                        if (fsw.Waiting)
                        {
                            fsw.Waiting = false;
                            System.Threading.Monitor.PulseAll(fsw);
                        }
                    }
                }
            }
        }
        static void DispatchEvents(FileSystemWatcher fsw, FileAction action, string filename)
        {
            RenamedEventArgs renamed = null;

            lock (fsw) {
                fsw.DispatchEvents(action, filename, ref renamed);
                if (fsw.Waiting)
                {
                    fsw.Waiting = false;
                    System.Threading.Monitor.PulseAll(fsw);
                }
            }
        }
Exemple #5
0
        private void PostEvent(string filename, FileSystemWatcher fsw, FileAction fa, FileSystemInfo changedFsi)
        {
            RenamedEventArgs renamed = null;

            if (fa == 0)
            {
                return;
            }

            if (fsw.IncludeSubdirectories && fa == FileAction.Added)
            {
                if (changedFsi is DirectoryInfo)
                {
                    KeventData newdirdata = new KeventData();
                    newdirdata.FSW            = fsw;
                    newdirdata.Directory      = changedFsi.FullName;
                    newdirdata.FileMask       = fsw.MangledFilter;
                    newdirdata.IncludeSubdirs = fsw.IncludeSubdirectories;

                    newdirdata.Enabled = true;
                    lock (this) {
                        StartMonitoringDirectory(newdirdata);
                    }
                }
            }

            if (!fsw.Pattern.IsMatch(filename, true))
            {
                return;
            }

            lock (fsw) {
                if (changedFsi.FullName.StartsWith(fsw.FullPath, StringComparison.Ordinal))
                {
                    if (fsw.FullPath.EndsWith("/", StringComparison.Ordinal))
                    {
                        filename = changedFsi.FullName.Substring(fsw.FullPath.Length);
                    }
                    else
                    {
                        filename = changedFsi.FullName.Substring(fsw.FullPath.Length + 1);
                    }
                }
                fsw.DispatchEvents(fa, filename, ref renamed);
                if (fsw.Waiting)
                {
                    fsw.Waiting = false;
                    System.Threading.Monitor.PulseAll(fsw);
                }
            }
        }
Exemple #6
0
        private void ProcessEvents(byte[] buffer, int length)
        {
            ArrayList        arrayList        = null;
            int              num              = 0;
            RenamedEventArgs renamedEventArgs = null;

            while (length > num)
            {
                InotifyEvent inotifyEvent;
                int          num2 = InotifyWatcher.ReadEvent(buffer, num, length, out inotifyEvent);
                if (num2 <= 0)
                {
                    break;
                }
                num += num2;
                InotifyMask inotifyMask = inotifyEvent.Mask;
                bool        flag        = (inotifyMask & InotifyMask.Directory) != (InotifyMask)0u;
                inotifyMask &= InotifyWatcher.Interesting;
                if (inotifyMask != (InotifyMask)0u)
                {
                    foreach (object obj in InotifyWatcher.GetEnumerator(InotifyWatcher.requests[inotifyEvent.WatchDescriptor]))
                    {
                        InotifyData       inotifyData       = (InotifyData)obj;
                        ParentInotifyData parentInotifyData = (ParentInotifyData)InotifyWatcher.watches[inotifyData.FSW];
                        if (inotifyData != null && parentInotifyData.Enabled)
                        {
                            string directory = inotifyData.Directory;
                            string text      = inotifyEvent.Name;
                            if (text == null)
                            {
                                text = directory;
                            }
                            FileSystemWatcher fsw        = inotifyData.FSW;
                            FileAction        fileAction = (FileAction)0;
                            if ((inotifyMask & (InotifyMask.Modify | InotifyMask.Attrib)) != (InotifyMask)0u)
                            {
                                fileAction = FileAction.Modified;
                            }
                            else if ((inotifyMask & InotifyMask.Create) != (InotifyMask)0u)
                            {
                                fileAction = FileAction.Added;
                            }
                            else if ((inotifyMask & InotifyMask.Delete) != (InotifyMask)0u)
                            {
                                fileAction = FileAction.Removed;
                            }
                            else if ((inotifyMask & InotifyMask.DeleteSelf) != (InotifyMask)0u)
                            {
                                if (inotifyData.Watch != parentInotifyData.data.Watch)
                                {
                                    continue;
                                }
                                fileAction = FileAction.Removed;
                            }
                            else
                            {
                                if ((inotifyMask & InotifyMask.MoveSelf) != (InotifyMask)0u)
                                {
                                    continue;
                                }
                                if ((inotifyMask & InotifyMask.MovedFrom) != (InotifyMask)0u)
                                {
                                    InotifyEvent inotifyEvent2;
                                    int          num3 = InotifyWatcher.ReadEvent(buffer, num, length, out inotifyEvent2);
                                    if (num3 == -1 || (inotifyEvent2.Mask & InotifyMask.MovedTo) == (InotifyMask)0u || inotifyEvent.WatchDescriptor != inotifyEvent2.WatchDescriptor)
                                    {
                                        fileAction = FileAction.Removed;
                                    }
                                    else
                                    {
                                        num             += num3;
                                        fileAction       = FileAction.RenamedNewName;
                                        renamedEventArgs = new RenamedEventArgs(WatcherChangeTypes.Renamed, inotifyData.Directory, inotifyEvent2.Name, inotifyEvent.Name);
                                        if (inotifyEvent.Name != inotifyData.Directory && !fsw.Pattern.IsMatch(inotifyEvent.Name))
                                        {
                                            text = inotifyEvent2.Name;
                                        }
                                    }
                                }
                                else if ((inotifyMask & InotifyMask.MovedTo) != (InotifyMask)0u)
                                {
                                    fileAction = FileAction.Added;
                                }
                            }
                            if (fsw.IncludeSubdirectories)
                            {
                                string fullPath = fsw.FullPath;
                                string text2    = inotifyData.Directory;
                                if (text2 != fullPath)
                                {
                                    int length2 = fullPath.Length;
                                    int num4    = 1;
                                    if (length2 > 1 && fullPath[length2 - 1] == Path.DirectorySeparatorChar)
                                    {
                                        num4 = 0;
                                    }
                                    string path = text2.Substring(fullPath.Length + num4);
                                    text2 = Path.Combine(text2, text);
                                    text  = Path.Combine(path, text);
                                }
                                else
                                {
                                    text2 = Path.Combine(fullPath, text);
                                }
                                if (fileAction == FileAction.Added && flag)
                                {
                                    if (arrayList == null)
                                    {
                                        arrayList = new ArrayList(2);
                                    }
                                    arrayList.Add(new InotifyData
                                    {
                                        FSW       = fsw,
                                        Directory = text2
                                    });
                                }
                                if (fileAction == FileAction.RenamedNewName && flag)
                                {
                                    string oldFullPath = renamedEventArgs.OldFullPath;
                                    string fullPath2   = renamedEventArgs.FullPath;
                                    int    length3     = oldFullPath.Length;
                                    foreach (object obj2 in parentInotifyData.children)
                                    {
                                        InotifyData inotifyData2 = (InotifyData)obj2;
                                        if (inotifyData2.Directory.StartsWith(oldFullPath, StringComparison.Ordinal))
                                        {
                                            inotifyData2.Directory = fullPath2 + inotifyData2.Directory.Substring(length3);
                                        }
                                    }
                                }
                            }
                            if (fileAction == FileAction.Removed && text == inotifyData.Directory)
                            {
                                int num5 = parentInotifyData.children.IndexOf(inotifyData);
                                if (num5 != -1)
                                {
                                    parentInotifyData.children.RemoveAt(num5);
                                    if (!fsw.Pattern.IsMatch(Path.GetFileName(text)))
                                    {
                                        continue;
                                    }
                                }
                            }
                            if (!(text != inotifyData.Directory) || fsw.Pattern.IsMatch(Path.GetFileName(text)))
                            {
                                FileSystemWatcher obj3 = fsw;
                                lock (obj3)
                                {
                                    fsw.DispatchEvents(fileAction, text, ref renamedEventArgs);
                                    if (fileAction == FileAction.RenamedNewName)
                                    {
                                        renamedEventArgs = null;
                                    }
                                    if (fsw.Waiting)
                                    {
                                        fsw.Waiting = false;
                                        System.Threading.Monitor.PulseAll(fsw);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (arrayList != null)
            {
                foreach (object obj4 in arrayList)
                {
                    InotifyData inotifyData3 = (InotifyData)obj4;
                    try
                    {
                        InotifyWatcher.StartMonitoringDirectory(inotifyData3, true);
                        InotifyWatcher.AppendRequestData(inotifyData3);
                        ((ParentInotifyData)InotifyWatcher.watches[inotifyData3.FSW]).children.Add(inotifyData3);
                    }
                    catch
                    {
                    }
                }
                arrayList.Clear();
            }
        }
Exemple #7
0
        private static void StartMonitoringDirectory(InotifyData data, bool justcreated)
        {
            InotifyMask maskFromFilters = InotifyWatcher.GetMaskFromFilters(data.FSW.NotifyFilter);
            int         num             = InotifyWatcher.AddDirectoryWatch(InotifyWatcher.FD, data.Directory, maskFromFilters);

            if (num != -1)
            {
                FileSystemWatcher fsw = data.FSW;
                data.Watch = num;
                ParentInotifyData parentInotifyData = (ParentInotifyData)InotifyWatcher.watches[fsw];
                if (parentInotifyData.IncludeSubdirs)
                {
                    foreach (string text in Directory.GetDirectories(data.Directory))
                    {
                        InotifyData inotifyData = new InotifyData();
                        inotifyData.FSW       = fsw;
                        inotifyData.Directory = text;
                        if (justcreated)
                        {
                            FileSystemWatcher obj = fsw;
                            lock (obj)
                            {
                                RenamedEventArgs renamedEventArgs = null;
                                if (fsw.Pattern.IsMatch(text))
                                {
                                    fsw.DispatchEvents(FileAction.Added, text, ref renamedEventArgs);
                                    if (fsw.Waiting)
                                    {
                                        fsw.Waiting = false;
                                        System.Threading.Monitor.PulseAll(fsw);
                                    }
                                }
                            }
                        }
                        try
                        {
                            InotifyWatcher.StartMonitoringDirectory(inotifyData, justcreated);
                            InotifyWatcher.AppendRequestData(inotifyData);
                            parentInotifyData.children.Add(inotifyData);
                        }
                        catch
                        {
                        }
                    }
                }
                if (justcreated)
                {
                    foreach (string text2 in Directory.GetFiles(data.Directory))
                    {
                        FileSystemWatcher obj2 = fsw;
                        lock (obj2)
                        {
                            RenamedEventArgs renamedEventArgs2 = null;
                            if (fsw.Pattern.IsMatch(text2))
                            {
                                fsw.DispatchEvents(FileAction.Added, text2, ref renamedEventArgs2);
                                fsw.DispatchEvents(FileAction.Modified, text2, ref renamedEventArgs2);
                                if (fsw.Waiting)
                                {
                                    fsw.Waiting = false;
                                    System.Threading.Monitor.PulseAll(fsw);
                                }
                            }
                        }
                    }
                }
                return;
            }
            int lastWin32Error = Marshal.GetLastWin32Error();

            if (lastWin32Error == 4)
            {
                string arg = "(unknown)";
                try
                {
                    using (StreamReader streamReader = new StreamReader("/proc/sys/fs/inotify/max_user_watches"))
                    {
                        arg = streamReader.ReadLine();
                    }
                }
                catch
                {
                }
                string message = string.Format("The per-user inotify watches limit of {0} has been reached. If you're experiencing problems with your application, increase that limit in /proc/sys/fs/inotify/max_user_watches.", arg);
                throw new System.ComponentModel.Win32Exception(lastWin32Error, message);
            }
            throw new System.ComponentModel.Win32Exception(lastWin32Error);
        }
Exemple #8
0
        void ProcessEvents(byte [] buffer, int length)
        {
            ArrayList        newdirs = null;
            InotifyEvent     evt;
            int              nread   = 0;
            RenamedEventArgs renamed = null;

            while (length > nread)
            {
                int bytes_read = ReadEvent(buffer, nread, length, out evt);
                if (bytes_read <= 0)
                {
                    break;
                }

                nread += bytes_read;

                InotifyMask mask         = evt.Mask;
                bool        is_directory = (mask & InotifyMask.Directory) != 0;
                mask = (mask & Interesting);                 // Clear out all the bits that we don't need
                if (mask == 0)
                {
                    continue;
                }

                foreach (InotifyData data in GetEnumerator(requests [evt.WatchDescriptor]))
                {
                    ParentInotifyData parent = (ParentInotifyData)watches[data.FSW];

                    if (data == null || parent.Enabled == false)
                    {
                        continue;
                    }

                    string directory = data.Directory;
                    string filename  = evt.Name;
                    if (filename == null)
                    {
                        filename = directory;
                    }

                    FileSystemWatcher fsw    = data.FSW;
                    FileAction        action = 0;
                    if ((mask & (InotifyMask.Modify | InotifyMask.Attrib)) != 0)
                    {
                        action = FileAction.Modified;
                    }
                    else if ((mask & InotifyMask.Create) != 0)
                    {
                        action = FileAction.Added;
                    }
                    else if ((mask & InotifyMask.Delete) != 0)
                    {
                        action = FileAction.Removed;
                    }
                    else if ((mask & InotifyMask.DeleteSelf) != 0)
                    {
                        if (data.Watch != parent.data.Watch)
                        {
                            // To avoid duplicate events handle DeleteSelf only for the top level directory.
                            continue;
                        }
                        action = FileAction.Removed;
                    }
                    else if ((mask & InotifyMask.MoveSelf) != 0)
                    {
                        //action = FileAction.Removed;
                        continue;                         // Ignore this one
                    }
                    else if ((mask & InotifyMask.MovedFrom) != 0)
                    {
                        InotifyEvent to;
                        int          i = ReadEvent(buffer, nread, length, out to);
                        if (i == -1 || (to.Mask & InotifyMask.MovedTo) == 0 || evt.WatchDescriptor != to.WatchDescriptor)
                        {
                            action = FileAction.Removed;
                        }
                        else
                        {
                            nread  += i;
                            action  = FileAction.RenamedNewName;
                            renamed = new RenamedEventArgs(WatcherChangeTypes.Renamed, data.Directory, to.Name, evt.Name);
                            if (evt.Name != data.Directory && !fsw.Pattern.IsMatch(evt.Name))
                            {
                                filename = to.Name;
                            }
                        }
                    }
                    else if ((mask & InotifyMask.MovedTo) != 0)
                    {
                        action = FileAction.Added;
                    }
                    if (fsw.IncludeSubdirectories)
                    {
                        string full    = fsw.FullPath;
                        string datadir = data.Directory;
                        if (datadir != full)
                        {
                            int len   = full.Length;
                            int slash = 1;
                            if (len > 1 && full [len - 1] == Path.DirectorySeparatorChar)
                            {
                                slash = 0;
                            }
                            string reldir = datadir.Substring(full.Length + slash);
                            datadir  = Path.Combine(datadir, filename);
                            filename = Path.Combine(reldir, filename);
                        }
                        else
                        {
                            datadir = Path.Combine(full, filename);
                        }

                        if (action == FileAction.Added && is_directory)
                        {
                            if (newdirs == null)
                            {
                                newdirs = new ArrayList(2);
                            }

                            InotifyData fd = new InotifyData();
                            fd.FSW       = fsw;
                            fd.Directory = datadir;
                            newdirs.Add(fd);
                        }

                        if (action == FileAction.RenamedNewName && is_directory)
                        {
                            string renamedOldFullPath       = renamed.OldFullPath;
                            string renamedFullPath          = renamed.FullPath;
                            int    renamedOldFullPathLength = renamedOldFullPath.Length;

                            foreach (InotifyData child in parent.children)
                            {
                                if (child.Directory.StartsWith(renamedOldFullPath
#if NET_2_0
                                                               , StringComparison.Ordinal
#endif
                                                               ))
                                {
                                    child.Directory = renamedFullPath +
                                                      child.Directory.Substring(renamedOldFullPathLength);
                                }
                            }
                        }
                    }

                    if (action == FileAction.Removed && filename == data.Directory)
                    {
                        int idx = parent.children.IndexOf(data);
                        if (idx != -1)
                        {
                            parent.children.RemoveAt(idx);
                            if (!fsw.Pattern.IsMatch(Path.GetFileName(filename)))
                            {
                                continue;
                            }
                        }
                    }

                    if (filename != data.Directory && !fsw.Pattern.IsMatch(Path.GetFileName(filename)))
                    {
                        continue;
                    }

                    lock (fsw) {
                        fsw.DispatchEvents(action, filename, ref renamed);
                        if (action == FileAction.RenamedNewName)
                        {
                            renamed = null;
                        }
                        if (fsw.Waiting)
                        {
                            fsw.Waiting = false;
                            System.Threading.Monitor.PulseAll(fsw);
                        }
                    }
                }
            }

            if (newdirs != null)
            {
                foreach (InotifyData newdir in newdirs)
                {
                    try {
                        StartMonitoringDirectory(newdir, true);
                        AppendRequestData(newdir);
                        ((ParentInotifyData)watches[newdir.FSW]).children.Add(newdir);
                    } catch {}                     // ignore the given directory
                }
                newdirs.Clear();
            }
        }
Exemple #9
0
        static void StartMonitoringDirectory(InotifyData data, bool justcreated)
        {
            InotifyMask mask = GetMaskFromFilters(data.FSW.NotifyFilter);
            int         wd   = AddDirectoryWatch(FD, data.Directory, mask);

            if (wd == -1)
            {
                int error = Marshal.GetLastWin32Error();
                if (error == 4)                   // Too many open watches
                {
                    string nr_watches = "(unknown)";
                    try {
                        using (StreamReader reader = new StreamReader("/proc/sys/fs/inotify/max_user_watches")) {
                            nr_watches = reader.ReadLine();
                        }
                    } catch {}

                    string msg = String.Format("The per-user inotify watches limit of {0} has been reached. " +
                                               "If you're experiencing problems with your application, increase that limit " +
                                               "in /proc/sys/fs/inotify/max_user_watches.", nr_watches);

                    throw new Win32Exception(error, msg);
                }
                throw new Win32Exception(error);
            }

            FileSystemWatcher fsw = data.FSW;

            data.Watch = wd;

            ParentInotifyData parent = (ParentInotifyData)watches[fsw];

            if (parent.IncludeSubdirs)
            {
                foreach (string directory in Directory.GetDirectories(data.Directory))
                {
                    InotifyData fd = new InotifyData();
                    fd.FSW       = fsw;
                    fd.Directory = directory;

                    if (justcreated)
                    {
                        lock (fsw) {
                            RenamedEventArgs renamed = null;
                            if (fsw.Pattern.IsMatch(directory))
                            {
                                fsw.DispatchEvents(FileAction.Added, directory, ref renamed);
                                if (fsw.Waiting)
                                {
                                    fsw.Waiting = false;
                                    System.Threading.Monitor.PulseAll(fsw);
                                }
                            }
                        }
                    }

                    try {
                        StartMonitoringDirectory(fd, justcreated);
                        AppendRequestData(fd);
                        parent.children.Add(fd);
                    } catch {}                     // ignore errors and don't add directory.
                }
            }

            if (justcreated)
            {
                foreach (string filename in Directory.GetFiles(data.Directory))
                {
                    lock (fsw) {
                        RenamedEventArgs renamed = null;
                        if (fsw.Pattern.IsMatch(filename))
                        {
                            fsw.DispatchEvents(FileAction.Added, filename, ref renamed);
                            /* If a file has been created, then it has been written to */
                            fsw.DispatchEvents(FileAction.Modified, filename, ref renamed);

                            if (fsw.Waiting)
                            {
                                fsw.Waiting = false;
                                System.Threading.Monitor.PulseAll(fsw);
                            }
                        }
                    }
                }
            }
        }
Exemple #10
0
		private void PostEvent (string filename, FileSystemWatcher fsw, FileAction fa, FileSystemInfo changedFsi) {
			RenamedEventArgs renamed = null;
			if (fa == 0)
				return;
			
			if (fsw.IncludeSubdirectories && fa == FileAction.Added) {
				if (changedFsi is DirectoryInfo) {
					KeventData newdirdata = new KeventData ();
					newdirdata.FSW = fsw;
					newdirdata.Directory = changedFsi.FullName;
					newdirdata.FileMask = fsw.MangledFilter;
					newdirdata.IncludeSubdirs = fsw.IncludeSubdirectories;
	
					newdirdata.Enabled = true;
					lock (this) {
						StartMonitoringDirectory (newdirdata);
					}
				}
			}
		
			if (!fsw.Pattern.IsMatch(filename, true))
				return;

			lock (fsw) {
				if (changedFsi.FullName.StartsWith (fsw.FullPath, StringComparison.Ordinal))
					filename = changedFsi.FullName.Substring (fsw.FullPath.Length + 1);
				fsw.DispatchEvents (fa, filename, ref renamed);
				if (fsw.Waiting) {
					fsw.Waiting = false;
					System.Threading.Monitor.PulseAll (fsw);
				}
			}
		}
Exemple #11
0
        private void ProcessEvents()
        {
            ArrayList arrayList = null;

            lock (this)
            {
                string text;
                int    num;
                int    num2;
                while (FAMWatcher.InternalFAMNextEvent(ref FAMWatcher.conn, out text, out num, out num2) == 1)
                {
                    bool flag;
                    switch (num)
                    {
                    case 1:
                    case 2:
                    case 5:
                        flag = FAMWatcher.requests.ContainsKey(num2);
                        break;

                    case 3:
                    case 4:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        goto IL_75;

                    default:
                        goto IL_75;
                    }
IL_7D:
                    if (flag)
                    {
                        FAMData famdata = (FAMData)FAMWatcher.requests[num2];
                        if (famdata.Enabled)
                        {
                            FileSystemWatcher fsw              = famdata.FSW;
                            NotifyFilters     notifyFilter     = fsw.NotifyFilter;
                            RenamedEventArgs  renamedEventArgs = null;
                            FileAction        fileAction       = (FileAction)0;
                            if (num == 1 && (notifyFilter & (NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size)) != (NotifyFilters)0)
                            {
                                fileAction = FileAction.Modified;
                            }
                            else if (num == 2)
                            {
                                fileAction = FileAction.Removed;
                            }
                            else if (num == 5)
                            {
                                fileAction = FileAction.Added;
                            }
                            if (fileAction != (FileAction)0)
                            {
                                if (fsw.IncludeSubdirectories)
                                {
                                    string fullPath = fsw.FullPath;
                                    string text2    = famdata.Directory;
                                    if (text2 != fullPath)
                                    {
                                        int length = fullPath.Length;
                                        int num3   = 1;
                                        if (length > 1 && fullPath[length - 1] == Path.DirectorySeparatorChar)
                                        {
                                            num3 = 0;
                                        }
                                        string path = text2.Substring(fullPath.Length + num3);
                                        text2 = Path.Combine(text2, text);
                                        text  = Path.Combine(path, text);
                                    }
                                    else
                                    {
                                        text2 = Path.Combine(fullPath, text);
                                    }
                                    if (fileAction == FileAction.Added && Directory.Exists(text2))
                                    {
                                        if (arrayList == null)
                                        {
                                            arrayList = new ArrayList(4);
                                        }
                                        arrayList.Add(new FAMData
                                        {
                                            FSW            = fsw,
                                            Directory      = text2,
                                            FileMask       = fsw.MangledFilter,
                                            IncludeSubdirs = true,
                                            SubDirs        = new Hashtable(),
                                            Enabled        = true
                                        });
                                        arrayList.Add(famdata);
                                    }
                                }
                                if (!(text != famdata.Directory) || fsw.Pattern.IsMatch(text))
                                {
                                    FileSystemWatcher obj = fsw;
                                    lock (obj)
                                    {
                                        fsw.DispatchEvents(fileAction, text, ref renamedEventArgs);
                                        if (fsw.Waiting)
                                        {
                                            fsw.Waiting = false;
                                            System.Threading.Monitor.PulseAll(fsw);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (FAMWatcher.FAMPending(ref FAMWatcher.conn) <= 0)
                    {
                        goto IL_28F;
                    }
                    continue;
IL_75:
                    flag = false;
                    goto IL_7D;
                }
                return;
            }
IL_28F:
            if (arrayList != null)
            {
                int count = arrayList.Count;
                for (int i = 0; i < count; i += 2)
                {
                    FAMData famdata2 = (FAMData)arrayList[i];
                    FAMData famdata3 = (FAMData)arrayList[i + 1];
                    FAMWatcher.StartMonitoringDirectory(famdata2, true);
                    FAMWatcher.requests[famdata2.Request.ReqNum] = famdata2;
                    FAMData obj2 = famdata3;
                    lock (obj2)
                    {
                        famdata3.SubDirs[famdata2.Directory] = famdata2;
                    }
                }
                arrayList.Clear();
            }
        }
		static void DispatchEvents (FileSystemWatcher fsw, FileAction action, string filename)
		{
			RenamedEventArgs renamed = null;

			lock (fsw) {
				fsw.DispatchEvents (action, filename, ref renamed);
				if (fsw.Waiting) {
					fsw.Waiting = false;
					System.Threading.Monitor.PulseAll (fsw);
				}
			}
		}
Exemple #13
0
        void ProcessEvents(byte [] buffer, int length)
        {
            ArrayList        newdirs = null;
            InotifyEvent     evt;
            int              nread           = 0;
            bool             new_name_needed = false;
            RenamedEventArgs renamed         = null;

            while (length > nread)
            {
                int bytes_read = ReadEvent(buffer, nread, length, out evt);
                if (bytes_read <= 0)
                {
                    break;
                }

                nread += bytes_read;

                InotifyMask mask         = evt.Mask;
                bool        is_directory = (mask & InotifyMask.Directory) != 0;
                mask = (mask & Interesting);                 // Clear out all the bits that we don't need
                if (mask == 0)
                {
                    continue;
                }

                foreach (InotifyData data in GetEnumerator(requests [evt.WatchDescriptor]))
                {
                    if (data == null || data.Enabled == false)
                    {
                        continue;
                    }

                    string directory = data.Directory;
                    string filename  = evt.Name;
                    if (filename == null)
                    {
                        filename = directory;
                    }

                    FileSystemWatcher fsw    = data.FSW;
                    FileAction        action = 0;
                    if ((mask & (InotifyMask.Modify | InotifyMask.CloseWrite | InotifyMask.Attrib)) != 0)
                    {
                        action = FileAction.Modified;
                    }
                    else if ((mask & InotifyMask.Create) != 0)
                    {
                        action = FileAction.Added;
                    }
                    else if ((mask & InotifyMask.Delete) != 0)
                    {
                        action = FileAction.Removed;
                    }
                    else if ((mask & InotifyMask.DeleteSelf) != 0)
                    {
                        action = FileAction.Removed;
                    }
                    else if ((mask & InotifyMask.MoveSelf) != 0)
                    {
                        //action = FileAction.Removed;
                        continue;                         // Ignore this one
                    }
                    else if ((mask & InotifyMask.MovedFrom) != 0)
                    {
                        InotifyEvent to;
                        int          i = ReadEvent(buffer, nread, length, out to);
                        if (i == -1 || (to.Mask & InotifyMask.MovedTo) == 0)
                        {
                            action = FileAction.Removed;
                        }
                        else
                        {
                            nread += i;
                            action = FileAction.RenamedNewName;
                            if (evt.Name == data.Directory || fsw.Pattern.IsMatch(evt.Name))
                            {
                                renamed = new RenamedEventArgs(WatcherChangeTypes.Renamed, data.Directory, to.Name, evt.Name);
                            }
                            else
                            {
                                renamed  = new RenamedEventArgs(WatcherChangeTypes.Renamed, data.Directory, evt.Name, to.Name);
                                filename = to.Name;
                            }
                        }
                    }
                    else if ((mask & InotifyMask.MovedTo) != 0)
                    {
                        action          = (new_name_needed) ? FileAction.RenamedNewName : FileAction.Added;
                        new_name_needed = false;
                    }

                    if (fsw.IncludeSubdirectories)
                    {
                        string full    = fsw.FullPath;
                        string datadir = data.Directory;
                        if (datadir != full)
                        {
                            int len   = full.Length;
                            int slash = 1;
                            if (len > 1 && full [len - 1] == Path.DirectorySeparatorChar)
                            {
                                slash = 0;
                            }
                            string reldir = datadir.Substring(full.Length + slash);
                            datadir  = Path.Combine(datadir, filename);
                            filename = Path.Combine(reldir, filename);
                        }
                        else
                        {
                            datadir = Path.Combine(full, filename);
                        }

                        if (action == FileAction.Added && is_directory)
                        {
                            if (newdirs == null)
                            {
                                newdirs = new ArrayList(4);
                            }

                            InotifyData fd = new InotifyData();
                            fd.FSW            = fsw;
                            fd.Directory      = datadir;
                            fd.FileMask       = fsw.MangledFilter;
                            fd.IncludeSubdirs = true;
                            fd.SubDirs        = new Hashtable();
                            fd.Enabled        = true;
                            newdirs.Add(fd);
                            newdirs.Add(data);
                        }
                    }

                    if (filename != data.Directory && !fsw.Pattern.IsMatch(filename))
                    {
                        continue;
                    }

                    lock (fsw) {
                        fsw.DispatchEvents(action, filename, ref renamed);
                        if (action == FileAction.RenamedNewName)
                        {
                            renamed = null;
                        }
                        if (fsw.Waiting)
                        {
                            fsw.Waiting = false;
                            System.Threading.Monitor.PulseAll(fsw);
                        }
                    }
                }
            }

            if (newdirs != null)
            {
                int count = newdirs.Count;
                for (int n = 0; n < count; n += 2)
                {
                    InotifyData newdir = (InotifyData)newdirs [n];
                    InotifyData parent = (InotifyData)newdirs [n + 1];
                    try {
                        StartMonitoringDirectory(newdir, true);
                        AppendRequestData(newdir);
                        lock (parent) {
                            parent.SubDirs [newdir.Directory] = newdir;
                        }
                    } catch {}                     // ignore the given directory
                }
                newdirs.Clear();
            }
        }
Exemple #14
0
        private void ProcessEvents(byte[] buffer, int length)
        {
            ArrayList        arrayList = null;
            int              num       = 0;
            RenamedEventArgs renamed   = null;

            while (length > num)
            {
                InotifyEvent evt;
                int          num2 = ReadEvent(buffer, num, length, out evt);
                if (num2 <= 0)
                {
                    break;
                }
                num += num2;
                InotifyMask mask = evt.Mask;
                bool        flag = (mask & InotifyMask.Directory) != (InotifyMask)0u;
                mask &= Interesting;
                if (mask != 0)
                {
                    foreach (InotifyData item in GetEnumerator(requests[evt.WatchDescriptor]))
                    {
                        ParentInotifyData parentInotifyData = (ParentInotifyData)watches[item.FSW];
                        if (item != null && parentInotifyData.Enabled)
                        {
                            string directory = item.Directory;
                            string text      = evt.Name;
                            if (text == null)
                            {
                                text = directory;
                            }
                            FileSystemWatcher fSW        = item.FSW;
                            FileAction        fileAction = (FileAction)0;
                            if ((mask & (InotifyMask.Modify | InotifyMask.Attrib)) != 0)
                            {
                                fileAction = FileAction.Modified;
                            }
                            else if ((mask & InotifyMask.Create) != 0)
                            {
                                fileAction = FileAction.Added;
                            }
                            else if ((mask & InotifyMask.Delete) != 0)
                            {
                                fileAction = FileAction.Removed;
                            }
                            else if ((mask & InotifyMask.DeleteSelf) != 0)
                            {
                                if (item.Watch != parentInotifyData.data.Watch)
                                {
                                    continue;
                                }
                                fileAction = FileAction.Removed;
                            }
                            else
                            {
                                if ((mask & InotifyMask.MoveSelf) != 0)
                                {
                                    continue;
                                }
                                if ((mask & InotifyMask.MovedFrom) != 0)
                                {
                                    InotifyEvent evt2;
                                    int          num3 = ReadEvent(buffer, num, length, out evt2);
                                    if (num3 == -1 || (evt2.Mask & InotifyMask.MovedTo) == (InotifyMask)0u || evt.WatchDescriptor != evt2.WatchDescriptor)
                                    {
                                        fileAction = FileAction.Removed;
                                    }
                                    else
                                    {
                                        num       += num3;
                                        fileAction = FileAction.RenamedNewName;
                                        renamed    = new RenamedEventArgs(WatcherChangeTypes.Renamed, item.Directory, evt2.Name, evt.Name);
                                        if (evt.Name != item.Directory && !fSW.Pattern.IsMatch(evt.Name))
                                        {
                                            text = evt2.Name;
                                        }
                                    }
                                }
                                else if ((mask & InotifyMask.MovedTo) != 0)
                                {
                                    fileAction = FileAction.Added;
                                }
                            }
                            if (fSW.IncludeSubdirectories)
                            {
                                string fullPath   = fSW.FullPath;
                                string directory2 = item.Directory;
                                if (directory2 != fullPath)
                                {
                                    int length2 = fullPath.Length;
                                    int num4    = 1;
                                    if (length2 > 1 && fullPath[length2 - 1] == Path.DirectorySeparatorChar)
                                    {
                                        num4 = 0;
                                    }
                                    string path = directory2.Substring(fullPath.Length + num4);
                                    directory2 = Path.Combine(directory2, text);
                                    text       = Path.Combine(path, text);
                                }
                                else
                                {
                                    directory2 = Path.Combine(fullPath, text);
                                }
                                if (fileAction == FileAction.Added && flag)
                                {
                                    if (arrayList == null)
                                    {
                                        arrayList = new ArrayList(2);
                                    }
                                    InotifyData inotifyData2 = new InotifyData();
                                    inotifyData2.FSW       = fSW;
                                    inotifyData2.Directory = directory2;
                                    arrayList.Add(inotifyData2);
                                }
                                if (fileAction == FileAction.RenamedNewName && flag)
                                {
                                    string oldFullPath = renamed.OldFullPath;
                                    string fullPath2   = renamed.FullPath;
                                    int    length3     = oldFullPath.Length;
                                    foreach (InotifyData child in parentInotifyData.children)
                                    {
                                        if (child.Directory.StartsWith(oldFullPath, StringComparison.Ordinal))
                                        {
                                            child.Directory = fullPath2 + child.Directory.Substring(length3);
                                        }
                                    }
                                }
                            }
                            if (fileAction == FileAction.Removed && text == item.Directory)
                            {
                                int num5 = parentInotifyData.children.IndexOf(item);
                                if (num5 != -1)
                                {
                                    parentInotifyData.children.RemoveAt(num5);
                                    if (!fSW.Pattern.IsMatch(Path.GetFileName(text)))
                                    {
                                        continue;
                                    }
                                }
                            }
                            if (!(text != item.Directory) || fSW.Pattern.IsMatch(Path.GetFileName(text)))
                            {
                                lock (fSW)
                                {
                                    fSW.DispatchEvents(fileAction, text, ref renamed);
                                    if (fileAction == FileAction.RenamedNewName)
                                    {
                                        renamed = null;
                                    }
                                    if (fSW.Waiting)
                                    {
                                        fSW.Waiting = false;
                                        System.Threading.Monitor.PulseAll(fSW);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (arrayList != null)
            {
                foreach (InotifyData item2 in arrayList)
                {
                    try
                    {
                        StartMonitoringDirectory(item2, justcreated: true);
                        AppendRequestData(item2);
                        ((ParentInotifyData)watches[item2.FSW]).children.Add(item2);
                    }
                    catch
                    {
                    }
                }
                arrayList.Clear();
            }
        }
Exemple #15
0
        private static void StartMonitoringDirectory(InotifyData data, bool justcreated)
        {
            InotifyMask maskFromFilters = GetMaskFromFilters(data.FSW.NotifyFilter);
            int         num             = AddDirectoryWatch(FD, data.Directory, maskFromFilters);

            if (num == -1)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error == 4)
                {
                    string arg = "(unknown)";
                    try
                    {
                        using (StreamReader streamReader = new StreamReader("/proc/sys/fs/inotify/max_user_watches"))
                        {
                            arg = streamReader.ReadLine();
                        }
                    }
                    catch
                    {
                    }
                    string message = $"The per-user inotify watches limit of {arg} has been reached. If you're experiencing problems with your application, increase that limit in /proc/sys/fs/inotify/max_user_watches.";
                    throw new Win32Exception(lastWin32Error, message);
                }
                throw new Win32Exception(lastWin32Error);
            }
            FileSystemWatcher fSW = data.FSW;

            data.Watch = num;
            ParentInotifyData parentInotifyData = (ParentInotifyData)watches[fSW];

            if (parentInotifyData.IncludeSubdirs)
            {
                string[] directories = Directory.GetDirectories(data.Directory);
                foreach (string text in directories)
                {
                    InotifyData inotifyData = new InotifyData();
                    inotifyData.FSW       = fSW;
                    inotifyData.Directory = text;
                    if (justcreated)
                    {
                        lock (fSW)
                        {
                            RenamedEventArgs renamed = null;
                            if (fSW.Pattern.IsMatch(text))
                            {
                                fSW.DispatchEvents(FileAction.Added, text, ref renamed);
                                if (fSW.Waiting)
                                {
                                    fSW.Waiting = false;
                                    System.Threading.Monitor.PulseAll(fSW);
                                }
                            }
                        }
                    }
                    try
                    {
                        StartMonitoringDirectory(inotifyData, justcreated);
                        AppendRequestData(inotifyData);
                        parentInotifyData.children.Add(inotifyData);
                    }
                    catch
                    {
                    }
                }
            }
            if (justcreated)
            {
                string[] files = Directory.GetFiles(data.Directory);
                foreach (string text2 in files)
                {
                    lock (fSW)
                    {
                        RenamedEventArgs renamed2 = null;
                        if (fSW.Pattern.IsMatch(text2))
                        {
                            fSW.DispatchEvents(FileAction.Added, text2, ref renamed2);
                            fSW.DispatchEvents(FileAction.Modified, text2, ref renamed2);
                            if (fSW.Waiting)
                            {
                                fSW.Waiting = false;
                                System.Threading.Monitor.PulseAll(fSW);
                            }
                        }
                    }
                }
            }
        }
Exemple #16
0
        private void ProcessEvents()
        {
            ArrayList arrayList = null;

            lock (this)
            {
                do
                {
                    if (InternalFAMNextEvent(ref conn, out string filename, out int code, out int reqnum) != 1)
                    {
                        return;
                    }
                    bool flag = false;
                    switch (code)
                    {
                    case 1:
                    case 2:
                    case 5:
                        flag = requests.ContainsKey(reqnum);
                        break;

                    default:
                        flag = false;
                        break;
                    }
                    if (flag)
                    {
                        FAMData fAMData = (FAMData)requests[reqnum];
                        if (fAMData.Enabled)
                        {
                            FileSystemWatcher fSW          = fAMData.FSW;
                            NotifyFilters     notifyFilter = fSW.NotifyFilter;
                            RenamedEventArgs  renamed      = null;
                            FileAction        fileAction   = (FileAction)0;
                            if (code == 1 && (notifyFilter & (NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size)) != 0)
                            {
                                fileAction = FileAction.Modified;
                            }
                            else
                            {
                                switch (code)
                                {
                                case 2:
                                    fileAction = FileAction.Removed;
                                    break;

                                case 5:
                                    fileAction = FileAction.Added;
                                    break;
                                }
                            }
                            if (fileAction != 0)
                            {
                                if (fSW.IncludeSubdirectories)
                                {
                                    string fullPath  = fSW.FullPath;
                                    string directory = fAMData.Directory;
                                    if (directory != fullPath)
                                    {
                                        int length = fullPath.Length;
                                        int num    = 1;
                                        if (length > 1 && fullPath[length - 1] == Path.DirectorySeparatorChar)
                                        {
                                            num = 0;
                                        }
                                        string path = directory.Substring(fullPath.Length + num);
                                        directory = Path.Combine(directory, filename);
                                        filename  = Path.Combine(path, filename);
                                    }
                                    else
                                    {
                                        directory = Path.Combine(fullPath, filename);
                                    }
                                    if (fileAction == FileAction.Added && Directory.Exists(directory))
                                    {
                                        if (arrayList == null)
                                        {
                                            arrayList = new ArrayList(4);
                                        }
                                        FAMData fAMData2 = new FAMData();
                                        fAMData2.FSW            = fSW;
                                        fAMData2.Directory      = directory;
                                        fAMData2.FileMask       = fSW.MangledFilter;
                                        fAMData2.IncludeSubdirs = true;
                                        fAMData2.SubDirs        = new Hashtable();
                                        fAMData2.Enabled        = true;
                                        arrayList.Add(fAMData2);
                                        arrayList.Add(fAMData);
                                    }
                                }
                                if (!(filename != fAMData.Directory) || fSW.Pattern.IsMatch(filename))
                                {
                                    lock (fSW)
                                    {
                                        fSW.DispatchEvents(fileAction, filename, ref renamed);
                                        if (fSW.Waiting)
                                        {
                                            fSW.Waiting = false;
                                            System.Threading.Monitor.PulseAll(fSW);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }while (FAMPending(ref conn) > 0);
            }
            if (arrayList != null)
            {
                int count = arrayList.Count;
                for (int i = 0; i < count; i += 2)
                {
                    FAMData fAMData3 = (FAMData)arrayList[i];
                    FAMData fAMData4 = (FAMData)arrayList[i + 1];
                    StartMonitoringDirectory(fAMData3, justcreated: true);
                    requests[fAMData3.Request.ReqNum] = fAMData3;
                    lock (fAMData4)
                    {
                        fAMData4.SubDirs[fAMData3.Directory] = fAMData3;
                    }
                }
                arrayList.Clear();
            }
        }