Beispiel #1
0
        internal void UpdateCachedAttributes()
        {
            string path = Path.Combine(this.DirectoryMonitor.Directory, this.FileNameLong);

            FileAttributesData.GetFileAttributes(path, out this._fad);
            this._dacl = FileSecurity.GetDacl(path);
        }
Beispiel #2
0
 internal void MakeExist(FindFileData ffd, byte[] dacl)
 {
     this._fileNameLong  = ffd.FileNameLong;
     this._fileNameShort = ffd.FileNameShort;
     this._fad           = ffd.FileAttributesData;
     this._dacl          = dacl;
     this._exists        = true;
 }
Beispiel #3
0
 internal FileMonitor(System.Web.DirectoryMonitor dirMon, string fileNameLong, string fileNameShort, bool exists, FileAttributesData fad, byte[] dacl)
 {
     this.DirectoryMonitor = dirMon;
     this._fileNameLong    = fileNameLong;
     this._fileNameShort   = fileNameShort;
     this._exists          = exists;
     this._fad             = fad;
     this._dacl            = dacl;
     this._targets         = new HybridDictionary();
     this.Aliases          = new HybridDictionary(true);
 }
        internal void AppendFileUniqueId(DepFileInfo depFileInfo, StringBuilder sb)
        {
            FileAttributesData nonExistantAttributesData = depFileInfo._fad;

            if (nonExistantAttributesData == null)
            {
                nonExistantAttributesData = FileAttributesData.NonExistantAttributesData;
            }
            sb.Append(depFileInfo._filename);
            sb.Append(nonExistantAttributesData.UtcLastWriteTime.Ticks.ToString("d", NumberFormatInfo.InvariantInfo));
            sb.Append(nonExistantAttributesData.FileSize.ToString(CultureInfo.InvariantCulture));
        }
        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);
        }
Beispiel #6
0
        /*
         * FileChange is called when a file we are monitoring has changed.
         */
        void FileChange(Object sender, FileChangeEvent e)
        {
            // Ignore notifications of events occured in the past, before we started monitoring.
            // This is to avoid the race with a delayed change notification about the file that
            // the user code just changed before creating the dependency:
            //    1)  User writes to the file
            //    2)  User creates dependency
            //    3)  Change notification comes and needs to be ignored if the timestamp
            //            of the file last access time is earlier than dependency creation time.
            //
            // However, if the cache dependency is marked as sensitive, the above check won't be
            // done.

            Debug.Trace("CacheDependencyFileChange", "FileChange file=" + e.FileName + ";Action=" + e.Action);
            if ((_bits & SENSITIVE) == 0 &&
                (e.Action == FileAction.Modified || e.Action == FileAction.Added))
            {
                // only check within some window
                const int raceWindowSeconds = 5;

                if (DateTime.UtcNow <= _utcInitTime.AddSeconds(raceWindowSeconds))
                {
                    int hr;
                    FileAttributesData fa;

                    hr = FileAttributesData.GetFileAttributes(e.FileName, out fa);
                    if (hr == HResults.S_OK &&
                        (fa.FileAttributes & UnsafeNativeMethods.FILE_ATTRIBUTE_DIRECTORY) == 0 &&
                        fa.UtcLastAccessTime >= fa.UtcLastWriteTime &&     // on FAT LastAccessTime is wrong
                        fa.UtcLastAccessTime <= _utcInitTime)
                    {
                        Debug.Trace("CacheDependencyFileChange", "FileChange ignored; fi.LastAccessTime = " +
                                    fa.UtcLastAccessTime.ToLocalTime() + "; fi.LastWriteTime = " + fa.UtcLastWriteTime.ToLocalTime() +
                                    "; _utcInitTime = " + _utcInitTime.ToLocalTime());

                        return; // ignore this notification
                    }
                }
            }

            OnChanged(sender, e);
        }
        private DirectoryMonitor FindDirectoryMonitor(string dir, bool addIfNotFound, bool throwOnError)
        {
            FileAttributesData fad     = null;
            DirectoryMonitor   monitor = (DirectoryMonitor)this._dirs[dir];

            if (((monitor != null) && !monitor.IsMonitoring()) && ((FileAttributesData.GetFileAttributes(dir, out fad) != 0) || ((fad.FileAttributes & FileAttributes.Directory) == 0)))
            {
                monitor = null;
            }
            if ((monitor == null) && addIfNotFound)
            {
                lock (this._dirs.SyncRoot)
                {
                    int fileAttributes;
                    monitor = (DirectoryMonitor)this._dirs[dir];
                    if (monitor != null)
                    {
                        if (monitor.IsMonitoring())
                        {
                            return(monitor);
                        }
                        fileAttributes = FileAttributesData.GetFileAttributes(dir, out fad);
                        if ((fileAttributes == 0) && ((fad.FileAttributes & FileAttributes.Directory) == 0))
                        {
                            fileAttributes = -2147024809;
                        }
                        if (fileAttributes == 0)
                        {
                            return(monitor);
                        }
                        this._dirs.Remove(dir);
                        monitor.StopMonitoring();
                        if (addIfNotFound && throwOnError)
                        {
                            throw CreateFileMonitoringException(fileAttributes, dir);
                        }
                        return(null);
                    }
                    if (!addIfNotFound)
                    {
                        return(monitor);
                    }
                    fileAttributes = FileAttributesData.GetFileAttributes(dir, out fad);
                    if ((fileAttributes == 0) && ((fad.FileAttributes & FileAttributes.Directory) == 0))
                    {
                        fileAttributes = -2147024809;
                    }
                    if (fileAttributes == 0)
                    {
                        monitor = new DirectoryMonitor(dir, false, 0x15b);
                        this._dirs.Add(dir, monitor);
                        return(monitor);
                    }
                    if (throwOnError)
                    {
                        throw CreateFileMonitoringException(fileAttributes, dir);
                    }
                }
            }
            return(monitor);
        }
        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);
            }
        }
        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);
            }
        }
 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)
     {
     }
 }
 private bool IsChangeAfterStartMonitoring(FileAttributesData fad, FileMonitorTarget target, DateTime utcCompletion)
 {
     return((fad.UtcLastAccessTime.AddSeconds(60.0) < target.UtcStartMonitoring) || ((utcCompletion > target.UtcStartMonitoring) || ((fad.UtcLastAccessTime < fad.UtcLastWriteTime) || ((fad.UtcLastAccessTime.TimeOfDay == TimeSpan.Zero) || (fad.UtcLastAccessTime >= target.UtcStartMonitoring)))));
 }
Beispiel #12
0
 internal void ResetCachedAttributes()
 {
     this._fad  = null;
     this._dacl = null;
 }
Beispiel #13
0
 internal void MakeExtinct()
 {
     this._fad    = null;
     this._dacl   = null;
     this._exists = false;
 }