EaseFilter Demo Project
EaseFltCSConsoleDemo/FileIOControlService.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2012 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.Collections.Generic;
13 using System.Linq;
14 using System.Text;
15 using System.Runtime.InteropServices;
16 
18 
19 namespace EaseFltCSConsoleDemo
20 {
22  {
23  public static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
24  {
25  bool ret = true;
26 
27  try
28  {
29 
30  messageReply.MessageId = messageSend.MessageId;
31  messageReply.MessageType = messageSend.MessageType;
32 
33  //
34  //here you can control all the registered IO requests,block the access or modify the I/O data base on the file IO information from MessageSend struture
35  //
36  //
37 
38  //if you don't want to change anything to this IO request, just let it pass through as below setting:
39  //messageReply.FilterStatus = 0;
40  //messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
41 
42  //if you want to block the access this IO request before it goes down to the file system, you can return the status as below,
43  //it is only for pre IO requests, it means the user IO reuqests will be completed here instead of going down to the file system.
44  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
45  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
46 
47  //if you want to modify the IO data and complete the pre IO with your own data, you can return status as below:
48  // messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION | (uint)FilterAPI.FilterStatus.FILTER_DATA_BUFFER_IS_UPDATED;
49  // messageReply.DataBufferLength = the return data buffer length.
50  // messageReply.DataBuffer = the data you want to return.
51  // messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
52 
53  FilterAPI.MessageType messageType = (FilterAPI.MessageType)messageSend.MessageType;
54  WinData.FileInfomationClass infoClass = (WinData.FileInfomationClass)messageSend.InfoClass;
55 
56  uint dataLength = messageSend.DataBufferLength;
57  byte[] data = messageSend.DataBuffer;
58 
59  //here is some IO information for your reference:
60  if ((messageSend.CreateOptions & (uint)WinData.CreateOptions.FO_REMOTE_ORIGIN) > 0)
61  {
62  //this is file access request comes from remote network server
63  }
64 
65  //you can check the file create option with this data:
66  //"DesiredAccess: messageSend.DesiredAccess
67  //"Disposition:" + ((WinData.Disposition)messageSend.Disposition).ToString();
68  //"ShareAccess:" + ((WinData.ShareAccess)messageSend.SharedAccess).ToString();
69  //"CreateOptions:"messageSend.CreateOptions
70 
71  switch (messageType)
72  {
73  case FilterAPI.MessageType.PRE_CREATE:
74  {
75 
76  //here you reparse the file open to another new file name
77 
78  //string newReparseFileName = "\\??\\c:\\myNewFile.txt";
79  //byte[] returnData = Encoding.Unicode.GetBytes(newReparseFileName);
80  //Array.Copy(returnData, messageReply.DataBuffer, returnData.Length);
81  //messageReply.DataBufferLength = (uint)returnData.Length;
82  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
83  //messageReply.ReturnStatus = (uint)NtStatus.Status.Reparse;
84 
85  break;
86  }
87 
88 
89  case FilterAPI.MessageType.PRE_CACHE_READ:
90  case FilterAPI.MessageType.POST_CACHE_READ:
91  case FilterAPI.MessageType.PRE_NOCACHE_READ:
92  case FilterAPI.MessageType.POST_NOCACHE_READ:
93  case FilterAPI.MessageType.PRE_PAGING_IO_READ:
94  case FilterAPI.MessageType.POST_PAGING_IO_READ:
95  case FilterAPI.MessageType.PRE_CACHE_WRITE:
96  case FilterAPI.MessageType.POST_CACHE_WRITE:
97  case FilterAPI.MessageType.PRE_NOCACHE_WRITE:
98  case FilterAPI.MessageType.POST_NOCACHE_WRITE:
99  case FilterAPI.MessageType.PRE_PAGING_IO_WRITE:
100  case FilterAPI.MessageType.POST_PAGING_IO_WRITE:
101  {
102 
103 
104  //byte[] returnData = //new data you want to modify the read/write data;
105  //Array.Copy(returnData, messageReply.DataBuffer, returnData.Length);
106  //messageReply.DataBufferLength = (uint)returnData.Length;
107 
110 
111  // messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_DATA_BUFFER_IS_UPDATED;
112  // messageReply.ReturnStatus = (uint)NtStatus.Status.Success;
113 
114 
115  break;
116  }
117  case FilterAPI.MessageType.PRE_SET_INFORMATION:
118  case FilterAPI.MessageType.POST_SET_INFORMATION:
119  case FilterAPI.MessageType.PRE_QUERY_INFORMATION:
120  case FilterAPI.MessageType.POST_QUERY_INFORMATION:
121  {
122  ret = true;
123  switch (infoClass)
124  {
125  case WinData.FileInfomationClass.FileRenameInformation:
126  {
127  //you can block file rename as below
128  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
129  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
130  break;
131  }
132  case WinData.FileInfomationClass.FileDispositionInformation:
133  {
134  //you can block file delete as below
135  //messageReply.FilterStatus = (uint)FilterAPI.FilterStatus.FILTER_COMPLETE_PRE_OPERATION;
136  //messageReply.ReturnStatus = (uint)NtStatus.Status.AccessDenied;
137  break;
138  }
139  case WinData.FileInfomationClass.FileEndOfFileInformation:
140  {
141  //change file size
142  break;
143  }
144  case WinData.FileInfomationClass.FileBasicInformation:
145  {
146  //file basic information
147  break;
148  }
149 
150  case WinData.FileInfomationClass.FileStandardInformation:
151  {
152  //file standard information
153  break;
154  }
155  case WinData.FileInfomationClass.FileNetworkOpenInformation:
156  {
157  //file network information
158  break;
159  }
160 
161  case WinData.FileInfomationClass.FileInternalInformation:
162  {
163  //file internal inofrmation
164  break;
165  }
166  default:
167  {
168  ret = false;
169  break;
170  }
171  }
172 
173  break;
174  }
175  }
176 
177 
178  }
179  catch (Exception ex)
180  {
181  EventManager.WriteMessage(174, "IOAccessControl", EventLevel.Error, "IOAccessControl failed." + ex.Message);
182  }
183 
184  return ret;
185  }
186 
187  }
188 }
static bool IOAccessControl(FilterAPI.MessageSendData messageSend, ref FilterAPI.MessageReplyData messageReply)
LONGLONG data
Definition: FilterAPI.h:521

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com