12 using System.Collections.Generic;
13 using System.ComponentModel;
16 using System.Runtime.InteropServices;
17 using System.Security.Principal;
28 public bool Equals(byte[] left, byte[] right)
30 if (left == null || right == null)
34 if (left.Length != right.Length)
38 for (
int i = 0; i < left.Length; i++)
40 if (left[i] != right[i])
51 throw new ArgumentNullException(
"key");
53 foreach (byte cur
in key)
63 Thread messageThread = null;
64 Queue<FilterAPI.MessageSendData> messageQueue =
new Queue<FilterAPI.MessageSendData>();
66 static Dictionary<string, DateTime> readFileCacheTable =
new Dictionary<string, DateTime>();
67 static Dictionary<string, DateTime> writeFileCacheTable =
new Dictionary<string, DateTime>();
68 static int cacheTimeOutInSeconds = 30;
69 static System.Timers.Timer deleteCachedItemTimer =
new System.Timers.Timer();
71 delegate
void LogFileEventDlg(
FileEvent fileEvent);
72 static event LogFileEventDlg OnFileEvent =
new LogFileEventDlg(FileEventHandler.LogFileEvent);
74 AutoResetEvent autoEvent =
new AutoResetEvent(
false);
75 bool disposed =
false;
80 deleteCachedItemTimer.Interval = cacheTimeOutInSeconds * 1000 / 4;
81 deleteCachedItemTimer.Start();
82 deleteCachedItemTimer.Enabled =
true;
83 deleteCachedItemTimer.Elapsed +=
new System.Timers.ElapsedEventHandler(deleteCachedItemTimer_Elapsed);
85 messageThread =
new Thread(
new ThreadStart(ProcessMessage));
86 messageThread.Start();
92 GC.SuppressFinalize(
this);
95 private void Dispose(
bool disposing)
102 messageThread.Abort();
112 private static void deleteCachedItemTimer_Elapsed(
object sender,
System.Timers.ElapsedEventArgs e)
117 List<string> keysToRemove =
new List<string>();
119 foreach (KeyValuePair<string, DateTime> userItem
in readFileCacheTable)
122 TimeSpan tsSinceLastAccess = DateTime.Now - userItem.Value;
124 if (tsSinceLastAccess.TotalSeconds >= cacheTimeOutInSeconds)
126 EventManager.WriteMessage(124,
"deleteCachedItemTimer_Elapsed",
EventLevel.Verbose,
"Remove read key " + userItem.Key);
127 keysToRemove.Add(userItem.Key);
131 foreach (
string key in keysToRemove)
133 lock (readFileCacheTable)
135 readFileCacheTable.Remove(key);
139 keysToRemove.Clear();
141 foreach (KeyValuePair<string, DateTime> userItem
in writeFileCacheTable)
144 TimeSpan tsSinceLastAccess = DateTime.Now - userItem.Value;
146 if (tsSinceLastAccess.TotalSeconds >= cacheTimeOutInSeconds)
148 EventManager.WriteMessage(145,
"deleteCachedItemTimer_Elapsed",
EventLevel.Verbose,
"Remove write key " + userItem.Key );
150 keysToRemove.Add(userItem.Key);
154 foreach (
string key
in keysToRemove)
156 lock (writeFileCacheTable)
158 writeFileCacheTable.Remove(key);
162 catch (
System.Exception ex)
164 EventManager.WriteMessage(46,
"deleteCachedItemTimer_Elapsed",
EventLevel.Error,
"Delete cached item failed with error:" + ex.Message);
169 public void AddMessage(FilterAPI.MessageSendData messageSend)
175 messageQueue.Clear();
178 messageQueue.Enqueue(messageSend);
187 void ProcessMessage()
194 if (messageQueue.Count == 0)
196 int result = WaitHandle.WaitAny(waitHandles);
203 while (messageQueue.Count > 0)
205 FilterAPI.MessageSendData messageSend;
209 messageSend = (FilterAPI.MessageSendData)messageQueue.Dequeue();
212 var fileEvent = DecodeFilterMessage(messageSend);
214 if (null != fileEvent && null != OnFileEvent)
216 OnFileEvent(fileEvent);
227 FileEvent DecodeFilterMessage(FilterAPI.MessageSendData messageSend)
236 string fileName = messageSend.FileName;
237 string description =
string.Empty;
238 FileAttributes
fileAttributes = (FileAttributes)messageSend.FileAttributes;
239 DateTime timestamp = DateTime.FromFileTime(messageSend.TransactionTime);
242 FilterAPI.DecodeUserName(messageSend.Sid, out userName);
243 FilterAPI.DecodeProcessName(messageSend.ProcessId, out processName);
245 FilterAPI.EVENTTYPE
eventType = (FilterAPI.EVENTTYPE)messageSend.InfoClass;
247 if ((
eventType & FilterAPI.EVENTTYPE.RENAMED) == FilterAPI.EVENTTYPE.RENAMED)
249 description =
"file was renamed to " + Encoding.Unicode.GetString(messageSend.DataBuffer);
250 description = description.Substring(0, description.IndexOf(
'\0'));
254 if (
eventType != FilterAPI.EVENTTYPE.NONE)
261 fileEvent.
Result = result;
274 EventManager.WriteMessage(296,
"DecodeFilterMessage",
EventLevel.Error,
"Decode filter message failed because of error:" + ex.Message);
string Process
The process name
bool Equals(byte[] left, byte[] right)
string Resource
Full path of the file name
FilterAPI.EVENTTYPE Type
type of the event -can be an enum
LONGLONG ULONG fileAttributes
FileAttributes Attributes
the file attributes
DateTime Timestamp
// timestamp of event
void AddMessage(FilterAPI.MessageSendData messageSend)
int GetHashCode(byte[] key)
FileEventResult Result
The status of the result
string Description
The description of the IO
static int MaximumFilterMessages
static ManualResetEvent stopEvent