public void NotifyFileRename(string oPath, string nPath) { ProgramStatus.Instance.IncFileAccessRate(); string oldPath = null; string newPath = null; FileAccessInfo t = null; lock (_filesLock) { if (String.IsNullOrEmpty(oPath)) { return; } if (String.IsNullOrEmpty(nPath)) { return; } // Purge the file names if (oPath.StartsWith(@"\??\")) { oldPath = oPath.Substring(4); } else { oldPath = oPath; } if (nPath.StartsWith(@"\??\")) { newPath = nPath.Substring(4); } else { newPath = nPath; } // TODO: wildcard? Can they appear here? bool knownFile = _fileMap.ContainsKey(oldPath); try { // Get the file log associated to that path or create a new one. if (knownFile) { t = _fileMap[oldPath]; // At this point it is necessary to update the mapping dictionary _fileMap.Remove(oldPath); if (_fileMap.ContainsKey(newPath)) { // Swap it. _fileMap[newPath] = t; } else { _fileMap.Add(newPath, t); } } else { t = new FileAccessInfo(); t.Path = oPath; _fileMap.Add(oldPath, t); } } catch (Exception e) { throw e; } } // Release the lock on collection // Let the object register the file access. This will acquire the lock t.NotifyRenamedTo(newPath); }
public void NotifyFileAccess(string ss) { ProgramStatus.Instance.IncFileAccessRate(); string wild_path = ""; if (ss.StartsWith(@"\??\")) { wild_path = ss.Substring(4); } var toNotify = new List <FileAccessInfo>(); // Lock the collection lock (_filesLock) { string[] files = null; // File path may contain wildcard. It is necessary to expand them here and perform analysis on each file. // This, of course, KILLs performance, but gives to us much more interesting data. if (wild_path.Contains('*')) { try { var dir = Path.GetDirectoryName(wild_path); var fname = Path.GetFileName(wild_path); if (Directory.Exists(dir)) { // Try to expand the wildcard, if any files = System.IO.Directory.GetFiles(dir, fname); } } catch (Exception e) { // An error occurred. Just take into account the root file files = new String[] { wild_path }; } } else { // If the file does not contain any wildcard, just add it. files = new String[] { wild_path }; } if (files == null) { return; } foreach (var s in files) { if (String.IsNullOrEmpty(s)) { continue; } bool knownFile = _fileMap.ContainsKey(s); FileAccessInfo t = null; // Get the file log associated to that path or create a new one. if (knownFile) { t = _fileMap[s]; } else { t = new FileAccessInfo(); t.Path = s; _fileMap.Add(s, t); } toNotify.Add(t); } } // Release the lock on the collection // Let the object register the file access. This will acquire the lock on each file and eventually will require some time foreach (var t in toNotify) { t.NotifyAccess(); } return; }