internal void StopMonitoringPath(string alias, object target)
 {
     if (!this.IsFCNDisabled)
     {
         DirectoryMonitor directoryMonitor = null;
         string           fileNameLong     = null;
         if (alias == null)
         {
             throw new HttpException(System.Web.SR.GetString("Invalid_file_name_for_monitoring", new object[] { string.Empty }));
         }
         using (new ApplicationImpersonationContext())
         {
             this._lockDispose.AcquireReaderLock();
             try
             {
                 if (!this._disposed)
                 {
                     FileMonitor monitor = (FileMonitor)this._aliases[alias];
                     if (monitor != null)
                     {
                         directoryMonitor = monitor.DirectoryMonitor;
                         fileNameLong     = monitor.FileNameLong;
                     }
                     else
                     {
                         if ((alias.Length == 0) || !UrlPath.IsAbsolutePhysicalPath(alias))
                         {
                             throw new HttpException(System.Web.SR.GetString("Invalid_file_name_for_monitoring", new object[] { HttpRuntime.GetSafePath(alias) }));
                         }
                         string fullPath = GetFullPath(alias);
                         directoryMonitor = this.FindDirectoryMonitor(fullPath, false, false);
                         if (directoryMonitor == null)
                         {
                             string directoryOrRootName = UrlPath.GetDirectoryOrRootName(fullPath);
                             fileNameLong = Path.GetFileName(fullPath);
                             if (!string.IsNullOrEmpty(fileNameLong))
                             {
                                 directoryMonitor = this.FindDirectoryMonitor(directoryOrRootName, false, false);
                             }
                         }
                     }
                     if (directoryMonitor != null)
                     {
                         directoryMonitor.StopMonitoringFile(fileNameLong, target);
                     }
                 }
             }
             finally
             {
                 this._lockDispose.ReleaseReaderLock();
             }
         }
     }
 }
 internal void RemoveAliases(FileMonitor fileMon)
 {
     if (!this.IsFCNDisabled)
     {
         foreach (DictionaryEntry entry in fileMon.Aliases)
         {
             if (this._aliases[entry.Key] == fileMon)
             {
                 this._aliases.Remove(entry.Key);
             }
         }
     }
 }
 internal void StopMonitoringFile(string file, object target)
 {
     lock (this)
     {
         FileMonitor fileMon = this.FindFileMonitor(file);
         if ((fileMon != null) && (fileMon.RemoveTarget(target) == 0))
         {
             this.RemoveFileMonitor(fileMon);
             if (this.GetFileMonitorsCount() == 0)
             {
                 ((IDisposable)this).Dispose();
             }
         }
     }
 }
        internal bool GetFileAttributes(string file, out FileAttributesData fad)
        {
            FileMonitor monitor = null;

            fad = null;
            lock (this)
            {
                monitor = this.FindFileMonitor(file);
                if (monitor != null)
                {
                    fad = monitor.Attributes;
                    return(true);
                }
            }
            return(false);
        }
        private FileMonitor AddFileMonitor(string file)
        {
            FileMonitor  monitor;
            FindFileData data = null;
            int          num;

            if (string.IsNullOrEmpty(file))
            {
                monitor          = new FileMonitor(this, null, null, true, null, null);
                this._anyFileMon = monitor;
                return(monitor);
            }
            string fullPath = Path.Combine(this.Directory, file);

            if (this._isDirMonAppPathInternal)
            {
                num = FindFileData.FindFile(fullPath, this.Directory, out data);
            }
            else
            {
                num = FindFileData.FindFile(fullPath, out data);
            }
            if (num == 0)
            {
                if (!this._isDirMonAppPathInternal && ((data.FileAttributesData.FileAttributes & FileAttributes.Directory) != 0))
                {
                    throw FileChangesMonitor.CreateFileMonitoringException(-2147024809, fullPath);
                }
                byte[] dacl = FileSecurity.GetDacl(fullPath);
                monitor = new FileMonitor(this, data.FileNameLong, data.FileNameShort, true, data.FileAttributesData, dacl);
                this._fileMons.Add(data.FileNameLong, monitor);
                this.UpdateFileNameShort(monitor, null, data.FileNameShort);
                return(monitor);
            }
            if ((num != -2147024893) && (num != -2147024894))
            {
                throw FileChangesMonitor.CreateFileMonitoringException(num, fullPath);
            }
            if (file.IndexOf('~') != -1)
            {
                throw FileChangesMonitor.CreateFileMonitoringException(-2147024809, fullPath);
            }
            monitor = new FileMonitor(this, file, null, false, null, null);
            this._fileMons.Add(file, monitor);
            return(monitor);
        }
 private void RemoveFileMonitor(FileMonitor fileMon)
 {
     if (fileMon == this._anyFileMon)
     {
         this._anyFileMon = null;
     }
     else
     {
         this._fileMons.Remove(fileMon.FileNameLong);
         if (fileMon.FileNameShort != null)
         {
             this._fileMons.Remove(fileMon.FileNameShort);
             this._cShortNames--;
         }
     }
     HttpRuntime.FileChangesMonitor.RemoveAliases(fileMon);
 }
 private FileMonitor AddFileMonitor(string file)
 {
     FileMonitor monitor;
     FindFileData data = null;
     int num;
     if (string.IsNullOrEmpty(file))
     {
         monitor = new FileMonitor(this, null, null, true, null, null);
         this._anyFileMon = monitor;
         return monitor;
     }
     string fullPath = Path.Combine(this.Directory, file);
     if (this._isDirMonAppPathInternal)
     {
         num = FindFileData.FindFile(fullPath, this.Directory, out data);
     }
     else
     {
         num = FindFileData.FindFile(fullPath, out data);
     }
     if (num == 0)
     {
         if (!this._isDirMonAppPathInternal && ((data.FileAttributesData.FileAttributes & FileAttributes.Directory) != 0))
         {
             throw FileChangesMonitor.CreateFileMonitoringException(-2147024809, fullPath);
         }
         byte[] dacl = FileSecurity.GetDacl(fullPath);
         monitor = new FileMonitor(this, data.FileNameLong, data.FileNameShort, true, data.FileAttributesData, dacl);
         this._fileMons.Add(data.FileNameLong, monitor);
         this.UpdateFileNameShort(monitor, null, data.FileNameShort);
         return monitor;
     }
     if ((num != -2147024893) && (num != -2147024894))
     {
         throw FileChangesMonitor.CreateFileMonitoringException(num, fullPath);
     }
     if (file.IndexOf('~') != -1)
     {
         throw FileChangesMonitor.CreateFileMonitoringException(-2147024809, fullPath);
     }
     monitor = new FileMonitor(this, file, null, false, null, null);
     this._fileMons.Add(file, monitor);
     return monitor;
 }
 private void UpdateFileNameShort(FileMonitor fileMon, string oldFileNameShort, string newFileNameShort)
 {
     if (oldFileNameShort != null)
     {
         FileMonitor monitor = (FileMonitor)this._fileMons[oldFileNameShort];
         if (monitor != null)
         {
             if (monitor != fileMon)
             {
                 monitor.RemoveFileNameShort();
             }
             this._fileMons.Remove(oldFileNameShort);
             this._cShortNames--;
         }
     }
     if (newFileNameShort != null)
     {
         this._fileMons.Add(newFileNameShort, fileMon);
         this._cShortNames++;
     }
 }
        internal FileMonitor StartMonitoringFileWithAssert(string file, FileChangeEventHandler callback, string alias)
        {
            FileMonitor monitor = null;
            bool        flag    = false;

            lock (this)
            {
                monitor = this.FindFileMonitor(file);
                if (monitor == null)
                {
                    monitor = this.AddFileMonitor(file);
                    if (this.GetFileMonitorsCount() == 1)
                    {
                        flag = true;
                    }
                }
                monitor.AddTarget(callback, alias, true);
                if (flag)
                {
                    this.StartMonitoring();
                }
            }
            return(monitor);
        }
        private bool GetFileMonitorForSpecialDirectory(string fileName, ref FileMonitor fileMon)
        {
            for (int i = 0; i < FileChangesMonitor.s_dirsToMonitor.Length; i++)
            {
                if (StringUtil.StringStartsWithIgnoreCase(fileName, FileChangesMonitor.s_dirsToMonitor[i]))
                {
                    fileMon = (FileMonitor)this._fileMons[FileChangesMonitor.s_dirsToMonitor[i]];
                    return(fileMon != null);
                }
            }
            int index = fileName.IndexOf("App_LocalResources", StringComparison.OrdinalIgnoreCase);

            if (index > -1)
            {
                int length = index + "App_LocalResources".Length;
                if ((fileName.Length == length) || (fileName[length] == Path.DirectorySeparatorChar))
                {
                    string str = fileName.Substring(0, length);
                    fileMon = (FileMonitor)this._fileMons[str];
                    return(fileMon != null);
                }
            }
            return(false);
        }
 void IDisposable.Dispose()
 {
     if (this._dirMonCompletion != null)
     {
         ((IDisposable)this._dirMonCompletion).Dispose();
         this._dirMonCompletion = null;
     }
     if (this._anyFileMon != null)
     {
         HttpRuntime.FileChangesMonitor.RemoveAliases(this._anyFileMon);
         this._anyFileMon = null;
     }
     foreach (DictionaryEntry entry in this._fileMons)
     {
         string      key     = (string)entry.Key;
         FileMonitor fileMon = (FileMonitor)entry.Value;
         if (fileMon.FileNameLong == key)
         {
             HttpRuntime.FileChangesMonitor.RemoveAliases(fileMon);
         }
     }
     this._fileMons.Clear();
     this._cShortNames = 0;
 }
 private void RemoveFileMonitor(FileMonitor fileMon)
 {
     if (fileMon == this._anyFileMon)
     {
         this._anyFileMon = null;
     }
     else
     {
         this._fileMons.Remove(fileMon.FileNameLong);
         if (fileMon.FileNameShort != null)
         {
             this._fileMons.Remove(fileMon.FileNameShort);
             this._cShortNames--;
         }
     }
     HttpRuntime.FileChangesMonitor.RemoveAliases(fileMon);
 }
 private bool GetFileMonitorForSpecialDirectory(string fileName, ref FileMonitor fileMon)
 {
     for (int i = 0; i < FileChangesMonitor.s_dirsToMonitor.Length; i++)
     {
         if (StringUtil.StringStartsWithIgnoreCase(fileName, FileChangesMonitor.s_dirsToMonitor[i]))
         {
             fileMon = (FileMonitor) this._fileMons[FileChangesMonitor.s_dirsToMonitor[i]];
             return (fileMon != null);
         }
     }
     int index = fileName.IndexOf("App_LocalResources", StringComparison.OrdinalIgnoreCase);
     if (index > -1)
     {
         int length = index + "App_LocalResources".Length;
         if ((fileName.Length == length) || (fileName[length] == Path.DirectorySeparatorChar))
         {
             string str = fileName.Substring(0, length);
             fileMon = (FileMonitor) this._fileMons[str];
             return (fileMon != null);
         }
     }
     return false;
 }
 internal void RemoveAliases(FileMonitor fileMon)
 {
     if (!this.IsFCNDisabled)
     {
         foreach (DictionaryEntry entry in fileMon.Aliases)
         {
             if (this._aliases[entry.Key] == fileMon)
             {
                 this._aliases.Remove(entry.Key);
             }
         }
     }
 }
 internal void OnFileChange(FileAction action, string fileName, DateTime utcCompletion)
 {
     try
     {
         FileMonitor        fileMon          = null;
         ArrayList          list             = null;
         FileAttributesData attributes       = null;
         FileAttributesData fad              = null;
         byte[]             dacl             = null;
         byte[]             buffer2          = null;
         FileAction         error            = FileAction.Error;
         DateTime           minValue         = DateTime.MinValue;
         bool fileMonitorForSpecialDirectory = false;
         if (this._dirMonCompletion != null)
         {
             lock (this)
             {
                 ICollection targets;
                 if (this._fileMons.Count > 0)
                 {
                     if ((action == FileAction.Error) || (action == FileAction.Overwhelming))
                     {
                         if (action == FileAction.Overwhelming)
                         {
                             HttpRuntime.SetShutdownMessage("Overwhelming Change Notification in " + this.Directory);
                             if (Interlocked.Increment(ref s_notificationBufferSizeIncreased) == 1)
                             {
                                 System.Web.UnsafeNativeMethods.GrowFileNotificationBuffer(HttpRuntime.AppDomainAppIdInternal, this._watchSubtree);
                             }
                         }
                         else if (action == FileAction.Error)
                         {
                             HttpRuntime.SetShutdownMessage("File Change Notification Error in " + this.Directory);
                         }
                         list = new ArrayList();
                         foreach (DictionaryEntry entry in this._fileMons)
                         {
                             string key = (string)entry.Key;
                             fileMon = (FileMonitor)entry.Value;
                             if ((fileMon.FileNameLong == key) && fileMon.Exists)
                             {
                                 fileMon.ResetCachedAttributes();
                                 fileMon.LastAction        = action;
                                 fileMon.UtcLastCompletion = utcCompletion;
                                 targets = fileMon.Targets;
                                 list.AddRange(targets);
                             }
                         }
                         fileMon = null;
                     }
                     else
                     {
                         fileMon = (FileMonitor)this._fileMons[fileName];
                         if (this._isDirMonAppPathInternal && (fileMon == null))
                         {
                             fileMonitorForSpecialDirectory = this.GetFileMonitorForSpecialDirectory(fileName, ref fileMon);
                         }
                         if (fileMon != null)
                         {
                             list                      = new ArrayList(fileMon.Targets);
                             attributes                = fileMon.Attributes;
                             dacl                      = fileMon.Dacl;
                             error                     = fileMon.LastAction;
                             minValue                  = fileMon.UtcLastCompletion;
                             fileMon.LastAction        = action;
                             fileMon.UtcLastCompletion = utcCompletion;
                             if ((action == FileAction.Removed) || (action == FileAction.RenamedOldName))
                             {
                                 fileMon.MakeExtinct();
                             }
                             else if (fileMon.Exists)
                             {
                                 if (minValue != utcCompletion)
                                 {
                                     fileMon.UpdateCachedAttributes();
                                 }
                             }
                             else
                             {
                                 int          num3;
                                 FindFileData data     = null;
                                 string       fullPath = Path.Combine(this.Directory, fileMon.FileNameLong);
                                 if (this._isDirMonAppPathInternal)
                                 {
                                     num3 = FindFileData.FindFile(fullPath, this.Directory, out data);
                                 }
                                 else
                                 {
                                     num3 = FindFileData.FindFile(fullPath, out data);
                                 }
                                 if (num3 == 0)
                                 {
                                     string fileNameShort = fileMon.FileNameShort;
                                     byte[] buffer3       = FileSecurity.GetDacl(fullPath);
                                     fileMon.MakeExist(data, buffer3);
                                     this.UpdateFileNameShort(fileMon, fileNameShort, data.FileNameShort);
                                 }
                             }
                             fad     = fileMon.Attributes;
                             buffer2 = fileMon.Dacl;
                         }
                     }
                 }
                 if (this._anyFileMon != null)
                 {
                     targets = this._anyFileMon.Targets;
                     if (list != null)
                     {
                         list.AddRange(targets);
                     }
                     else
                     {
                         list = new ArrayList(targets);
                     }
                 }
                 if ((action == FileAction.Error) || (action == FileAction.Overwhelming))
                 {
                     ((IDisposable)this).Dispose();
                 }
             }
             bool flag2 = false;
             if ((!fileMonitorForSpecialDirectory && (fileName != null)) && (action == FileAction.Modified))
             {
                 FileAttributesData data4 = fad;
                 if (data4 == null)
                 {
                     FileAttributesData.GetFileAttributes(Path.Combine(this.Directory, fileName), out data4);
                 }
                 if ((data4 != null) && ((data4.FileAttributes & FileAttributes.Directory) != 0))
                 {
                     flag2 = true;
                 }
             }
             if ((this._ignoreSubdirChange && ((action == FileAction.Removed) || (action == FileAction.RenamedOldName))) && (fileName != null))
             {
                 string str5 = Path.Combine(this.Directory, fileName);
                 if (!HttpRuntime.FileChangesMonitor.IsDirNameMonitored(str5, fileName))
                 {
                     flag2 = true;
                 }
             }
             if ((list != null) && !flag2)
             {
                 lock (s_notificationQueue.SyncRoot)
                 {
                     int num   = 0;
                     int count = list.Count;
                     while (num < count)
                     {
                         bool flag3;
                         FileMonitorTarget target = (FileMonitorTarget)list[num];
                         if (((action != FileAction.Added) && (action != FileAction.Modified)) || (fad == null))
                         {
                             flag3 = true;
                         }
                         else if (action == FileAction.Added)
                         {
                             flag3 = this.IsChangeAfterStartMonitoring(fad, target, utcCompletion);
                         }
                         else if (utcCompletion == minValue)
                         {
                             flag3 = error != FileAction.Modified;
                         }
                         else if (attributes == null)
                         {
                             flag3 = true;
                         }
                         else if ((dacl == null) || (dacl != buffer2))
                         {
                             flag3 = true;
                         }
                         else
                         {
                             flag3 = this.IsChangeAfterStartMonitoring(fad, target, utcCompletion);
                         }
                         if (flag3)
                         {
                             s_notificationQueue.Enqueue(new NotificationQueueItem(target.Callback, action, target.Alias));
                         }
                         num++;
                     }
                 }
                 if (((s_notificationQueue.Count > 0) && (s_inNotificationThread == 0)) && (Interlocked.Exchange(ref s_inNotificationThread, 1) == 0))
                 {
                     WorkItem.PostInternal(s_notificationCallback);
                 }
             }
         }
     }
     catch (Exception)
     {
     }
 }
        internal DateTime StartMonitoringPath(string alias, FileChangeEventHandler callback, out FileAttributesData fad)
        {
            FileMonitor      monitor  = null;
            DirectoryMonitor monitor2 = null;
            string           fullPath;
            string           file = null;
            bool             flag = false;

            fad = null;
            if (alias == null)
            {
                throw new HttpException(System.Web.SR.GetString("Invalid_file_name_for_monitoring", new object[] { string.Empty }));
            }
            if (this.IsFCNDisabled)
            {
                fullPath = GetFullPath(alias);
                FindFileData data = null;
                if (FindFileData.FindFile(fullPath, out data) == 0)
                {
                    fad = data.FileAttributesData;
                    return(data.FileAttributesData.UtcLastWriteTime);
                }
                return(DateTime.MinValue);
            }
            using (new ApplicationImpersonationContext())
            {
                this._lockDispose.AcquireReaderLock();
                try
                {
                    if (this._disposed)
                    {
                        return(DateTime.MinValue);
                    }
                    monitor = (FileMonitor)this._aliases[alias];
                    if (monitor != null)
                    {
                        file    = monitor.FileNameLong;
                        monitor = monitor.DirectoryMonitor.StartMonitoringFileWithAssert(file, callback, alias);
                    }
                    else
                    {
                        flag = true;
                        if ((alias.Length == 0) || !UrlPath.IsAbsolutePhysicalPath(alias))
                        {
                            throw new HttpException(System.Web.SR.GetString("Invalid_file_name_for_monitoring", new object[] { HttpRuntime.GetSafePath(alias) }));
                        }
                        fullPath = GetFullPath(alias);
                        if (this.IsBeneathAppPathInternal(fullPath))
                        {
                            monitor2 = this._dirMonAppPathInternal;
                            file     = fullPath.Substring(this._appPathInternal.Length + 1);
                            monitor  = monitor2.StartMonitoringFileWithAssert(file, callback, alias);
                        }
                        else
                        {
                            monitor2 = this.FindDirectoryMonitor(fullPath, false, false);
                            if (monitor2 != null)
                            {
                                monitor = monitor2.StartMonitoringFileWithAssert(null, callback, alias);
                            }
                            else
                            {
                                string directoryOrRootName = UrlPath.GetDirectoryOrRootName(fullPath);
                                file = Path.GetFileName(fullPath);
                                if (!string.IsNullOrEmpty(file))
                                {
                                    monitor2 = this.FindDirectoryMonitor(directoryOrRootName, false, false);
                                    if (monitor2 != null)
                                    {
                                        try
                                        {
                                            monitor = monitor2.StartMonitoringFileWithAssert(file, callback, alias);
                                        }
                                        catch
                                        {
                                        }
                                        if (monitor != null)
                                        {
                                            goto Label_01C7;
                                        }
                                    }
                                }
                                monitor2 = this.FindDirectoryMonitor(fullPath, true, false);
                                if (monitor2 != null)
                                {
                                    file = null;
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(file))
                                    {
                                        throw CreateFileMonitoringException(-2147024809, alias);
                                    }
                                    monitor2 = this.FindDirectoryMonitor(directoryOrRootName, true, true);
                                }
                                monitor = monitor2.StartMonitoringFileWithAssert(file, callback, alias);
                            }
                        }
                    }
Label_01C7:
                    if (!monitor.IsDirectory)
                    {
                        monitor.DirectoryMonitor.GetFileAttributes(file, out fad);
                    }
                    if (flag)
                    {
                        this._aliases[alias] = monitor;
                    }
                }
                finally
                {
                    this._lockDispose.ReleaseReaderLock();
                }
                if (fad != null)
                {
                    return(fad.UtcLastWriteTime);
                }
                return(DateTime.MinValue);
            }
        }
 void IDisposable.Dispose()
 {
     if (this._dirMonCompletion != null)
     {
         ((IDisposable) this._dirMonCompletion).Dispose();
         this._dirMonCompletion = null;
     }
     if (this._anyFileMon != null)
     {
         HttpRuntime.FileChangesMonitor.RemoveAliases(this._anyFileMon);
         this._anyFileMon = null;
     }
     foreach (DictionaryEntry entry in this._fileMons)
     {
         string key = (string) entry.Key;
         FileMonitor fileMon = (FileMonitor) entry.Value;
         if (fileMon.FileNameLong == key)
         {
             HttpRuntime.FileChangesMonitor.RemoveAliases(fileMon);
         }
     }
     this._fileMons.Clear();
     this._cShortNames = 0;
 }
 private void UpdateFileNameShort(FileMonitor fileMon, string oldFileNameShort, string newFileNameShort)
 {
     if (oldFileNameShort != null)
     {
         FileMonitor monitor = (FileMonitor) this._fileMons[oldFileNameShort];
         if (monitor != null)
         {
             if (monitor != fileMon)
             {
                 monitor.RemoveFileNameShort();
             }
             this._fileMons.Remove(oldFileNameShort);
             this._cShortNames--;
         }
     }
     if (newFileNameShort != null)
     {
         this._fileMons.Add(newFileNameShort, fileMon);
         this._cShortNames++;
     }
 }
        internal FileAttributesData GetFileAttributes(string alias)
        {
            DirectoryMonitor   directoryMonitor = null;
            string             fullPath;
            string             file = null;
            FileAttributesData fad  = null;

            if (alias == null)
            {
                throw CreateFileMonitoringException(-2147024809, alias);
            }
            if (this.IsFCNDisabled)
            {
                if ((alias.Length == 0) || !UrlPath.IsAbsolutePhysicalPath(alias))
                {
                    throw CreateFileMonitoringException(-2147024809, alias);
                }
                fullPath = GetFullPath(alias);
                FindFileData data = null;
                if (FindFileData.FindFile(fullPath, out data) == 0)
                {
                    return(data.FileAttributesData);
                }
                return(null);
            }
            using (new ApplicationImpersonationContext())
            {
                this._lockDispose.AcquireReaderLock();
                try
                {
                    if (!this._disposed)
                    {
                        FileMonitor monitor = (FileMonitor)this._aliases[alias];
                        if ((monitor != null) && !monitor.IsDirectory)
                        {
                            directoryMonitor = monitor.DirectoryMonitor;
                            file             = monitor.FileNameLong;
                        }
                        else
                        {
                            if ((alias.Length == 0) || !UrlPath.IsAbsolutePhysicalPath(alias))
                            {
                                throw CreateFileMonitoringException(-2147024809, alias);
                            }
                            fullPath = GetFullPath(alias);
                            string directoryOrRootName = UrlPath.GetDirectoryOrRootName(fullPath);
                            file = Path.GetFileName(fullPath);
                            if ((file != null) || (file.Length > 0))
                            {
                                directoryMonitor = this.FindDirectoryMonitor(directoryOrRootName, false, false);
                            }
                        }
                    }
                }
                finally
                {
                    this._lockDispose.ReleaseReaderLock();
                }
                if ((directoryMonitor == null) || !directoryMonitor.GetFileAttributes(file, out fad))
                {
                    FileAttributesData.GetFileAttributes(alias, out fad);
                }
                return(fad);
            }
        }