Exemple #1
0
 public static bool GetInstance(out IFileWatcher watcher, bool gamin)
 {
     if (InotifyWatcher.failed)
     {
         watcher = null;
         return(false);
     }
     if (InotifyWatcher.instance != null)
     {
         watcher = InotifyWatcher.instance;
         return(true);
     }
     InotifyWatcher.FD = InotifyWatcher.GetInotifyInstance();
     if ((long)InotifyWatcher.FD == -1L)
     {
         InotifyWatcher.failed = true;
         watcher = null;
         return(false);
     }
     InotifyWatcher.watches  = Hashtable.Synchronized(new Hashtable());
     InotifyWatcher.requests = Hashtable.Synchronized(new Hashtable());
     InotifyWatcher.instance = new InotifyWatcher();
     watcher = InotifyWatcher.instance;
     return(true);
 }
Exemple #2
0
        // Locked by caller
        public static bool GetInstance(out IFileWatcher watcher, bool gamin)
        {
            if (failed == true)
            {
                watcher = null;
                return(false);
            }

            if (instance != null)
            {
                watcher = instance;
                return(true);
            }

            FD = GetInotifyInstance();
            if ((long)FD == -1)
            {
                failed  = true;
                watcher = null;
                return(false);
            }

            watches  = Hashtable.Synchronized(new Hashtable());
            requests = Hashtable.Synchronized(new Hashtable());
            instance = new InotifyWatcher();
            watcher  = instance;
            return(true);
        }
Exemple #3
0
 public void StopDispatching(FileSystemWatcher fsw)
 {
     lock (this)
     {
         ParentInotifyData parentInotifyData = (ParentInotifyData)InotifyWatcher.watches[fsw];
         if (parentInotifyData != null)
         {
             if (InotifyWatcher.RemoveRequestData(parentInotifyData.data))
             {
                 InotifyWatcher.StopMonitoringDirectory(parentInotifyData.data);
             }
             InotifyWatcher.watches.Remove(fsw);
             if (InotifyWatcher.watches.Count == 0)
             {
                 InotifyWatcher.stop = true;
                 IntPtr fd = InotifyWatcher.FD;
                 InotifyWatcher.FD = (IntPtr)(-1);
                 InotifyWatcher.Close(fd);
             }
             if (parentInotifyData.IncludeSubdirs)
             {
                 foreach (object obj in parentInotifyData.children)
                 {
                     InotifyData data = (InotifyData)obj;
                     if (InotifyWatcher.RemoveRequestData(data))
                     {
                         InotifyWatcher.StopMonitoringDirectory(data);
                     }
                 }
             }
         }
     }
 }
        void InitWatcher()
        {
            lock (lockobj)
            {
                if (watcher != null)
                {
                    return;
                }

                string managed = Environment.GetEnvironmentVariable("MONO_MANAGED_WATCHER");
                int    mode    = 0;
                if (managed == null)
                {
                    mode = InternalSupportsFSW();
                }

                bool ok = false;
                switch (mode)
                {
                case 1: // windows
                    ok = DefaultWatcher.GetInstance(out watcher);
                    //ok = WindowsWatcher.GetInstance (out watcher);
                    break;

                case 2: // libfam
                    ok = FAMWatcher.GetInstance(out watcher, false);
                    break;

                case 3: // kevent
                    ok = KeventWatcher.GetInstance(out watcher);
                    break;

                case 4: // libgamin
                    ok = FAMWatcher.GetInstance(out watcher, true);
                    break;

                case 5: // inotify
                    ok = InotifyWatcher.GetInstance(out watcher, true);
                    break;
                }

                if (mode == 0 || !ok)
                {
                    if (String.Compare(managed, "disabled", true) == 0)
                    {
                        NullFileWatcher.GetInstance(out watcher);
                    }
                    else
                    {
                        DefaultWatcher.GetInstance(out watcher);
                    }
                }

                ShowWatcherInfo();
            }
        }
Exemple #5
0
        private void InitWatcher()
        {
            object obj = FileSystemWatcher.lockobj;

            lock (obj)
            {
                if (FileSystemWatcher.watcher == null)
                {
                    string environmentVariable = Environment.GetEnvironmentVariable("MONO_MANAGED_WATCHER");
                    int    num = 0;
                    if (environmentVariable == null)
                    {
                        num = FileSystemWatcher.InternalSupportsFSW();
                    }
                    bool flag = false;
                    switch (num)
                    {
                    case 1:
                        flag = DefaultWatcher.GetInstance(out FileSystemWatcher.watcher);
                        break;

                    case 2:
                        flag = FAMWatcher.GetInstance(out FileSystemWatcher.watcher, false);
                        break;

                    case 3:
                        flag = KeventWatcher.GetInstance(out FileSystemWatcher.watcher);
                        break;

                    case 4:
                        flag = FAMWatcher.GetInstance(out FileSystemWatcher.watcher, true);
                        break;

                    case 5:
                        flag = InotifyWatcher.GetInstance(out FileSystemWatcher.watcher, true);
                        break;
                    }
                    if (num == 0 || !flag)
                    {
                        if (string.Compare(environmentVariable, "disabled", true) == 0)
                        {
                            NullFileWatcher.GetInstance(out FileSystemWatcher.watcher);
                        }
                        else
                        {
                            DefaultWatcher.GetInstance(out FileSystemWatcher.watcher);
                        }
                    }
                }
            }
        }
Exemple #6
0
        public void StartDispatching(FileSystemWatcher fsw)
        {
            ParentInotifyData parentInotifyData;

            lock (this)
            {
                if ((long)InotifyWatcher.FD == -1L)
                {
                    InotifyWatcher.FD = InotifyWatcher.GetInotifyInstance();
                }
                if (InotifyWatcher.thread == null)
                {
                    InotifyWatcher.thread = new Thread(new ThreadStart(this.Monitor));
                    InotifyWatcher.thread.IsBackground = true;
                    InotifyWatcher.thread.Start();
                }
                parentInotifyData = (ParentInotifyData)InotifyWatcher.watches[fsw];
            }
            if (parentInotifyData == null)
            {
                InotifyData inotifyData = new InotifyData();
                inotifyData.FSW                  = fsw;
                inotifyData.Directory            = fsw.FullPath;
                parentInotifyData                = new ParentInotifyData();
                parentInotifyData.IncludeSubdirs = fsw.IncludeSubdirectories;
                parentInotifyData.Enabled        = true;
                parentInotifyData.children       = new ArrayList();
                parentInotifyData.data           = inotifyData;
                InotifyWatcher.watches[fsw]      = parentInotifyData;
                try
                {
                    InotifyWatcher.StartMonitoringDirectory(inotifyData, false);
                    lock (this)
                    {
                        InotifyWatcher.AppendRequestData(inotifyData);
                        InotifyWatcher.stop = false;
                    }
                }
                catch
                {
                }
            }
        }
Exemple #7
0
 private void Monitor()
 {
     byte[] array = new byte[4096];
     while (!InotifyWatcher.stop)
     {
         int num = InotifyWatcher.ReadFromFD(InotifyWatcher.FD, array, (IntPtr)array.Length);
         if (num != -1)
         {
             lock (this)
             {
                 this.ProcessEvents(array, num);
             }
         }
     }
     lock (this)
     {
         InotifyWatcher.thread = null;
         InotifyWatcher.stop   = false;
     }
 }
Exemple #8
0
 private static int AddDirectoryWatch(IntPtr fd, string directory, InotifyMask mask)
 {
     mask |= InotifyMask.Directory;
     return(InotifyWatcher.AddWatch(fd, directory, mask));
 }
Exemple #9
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 #10
0
 private static void StopMonitoringDirectory(InotifyData data)
 {
     InotifyWatcher.RemoveWatch(InotifyWatcher.FD, data.Watch);
 }
Exemple #11
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);
        }
        void InitWatcher()
        {
            lock (lockobj) {
                if (watcher_handle != null)
                {
                    return;
                }

                string managed = Environment.GetEnvironmentVariable("MONO_MANAGED_WATCHER");
                int    mode    = 0;
                if (managed == null)
                {
                    mode = InternalSupportsFSW();
                }

                bool ok = false;
                switch (mode)
                {
                case 1:                 // windows
                    ok             = DefaultWatcher.GetInstance(out watcher);
                    watcher_handle = this;
                    break;

                case 2:                 // libfam
                    ok             = FAMWatcher.GetInstance(out watcher, false);
                    watcher_handle = this;
                    break;

                case 3:                 // kevent
                    ok             = KeventWatcher.GetInstance(out watcher);
                    watcher_handle = this;
                    break;

                case 4:                 // libgamin
                    ok             = FAMWatcher.GetInstance(out watcher, true);
                    watcher_handle = this;
                    break;

                case 5:                 // inotify
                    ok             = InotifyWatcher.GetInstance(out watcher, true);
                    watcher_handle = this;
                    break;

                case 6:                 // CoreFX
                    ok             = CoreFXFileSystemWatcherProxy.GetInstance(out watcher);
                    watcher_handle = (watcher as CoreFXFileSystemWatcherProxy).NewWatcher(this);
                    break;
                }

                if (mode == 0 || !ok)
                {
                    if (String.Compare(managed, "disabled", true) == 0)
                    {
                        NullFileWatcher.GetInstance(out watcher);
                    }
                    else
                    {
                        DefaultWatcher.GetInstance(out watcher);
                        watcher_handle = this;
                    }
                }
                this.inited = true;

                ShowWatcherInfo();
            }
        }
Exemple #13
0
		// Locked by caller
		public static bool GetInstance (out IFileWatcher watcher, bool gamin)
		{
			if (failed == true) {
				watcher = null;
				return false;
			}

			if (instance != null) {
				watcher = instance;
				return true;
			}

			FD = GetInotifyInstance ();
			if ((long) FD == -1) {
				failed = true;
				watcher = null;
				return false;
			}

			watches = Hashtable.Synchronized (new Hashtable ());
			requests = Hashtable.Synchronized (new Hashtable ());
			instance = new InotifyWatcher ();
			watcher = instance;
			return true;
		}