ReactOS  0.4.12-dev-916-gffc4e30
ntobjfs.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2004, 2005 Martin Fuchs
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 
20  //
21  // Explorer clone
22  //
23  // ntobjfs.cpp
24  //
25  // Martin Fuchs, 31.01.2004
26  //
27 
28 
29 #include <precomp.h>
30 
31 #include "ntobjfs.h"
32 //#include "winfs.h"
33 #include "regfs.h"
34 
35 
36 #define CONSTRUCT_NTDLLFCT(x) x(TEXT("NTDLL"), #x)
37 
39 
40 struct NTDLL {
65  {
67 
70  *p++ = *NtOpenMutant;
71  *p++ = *NtOpenSection;
72  *p++ = *NtOpenEvent;
73  *p++ = *NtOpenSemaphore;
74  *p++ = *NtOpenTimer;
75  *p++ = *NtOpenKey;
76  *p++ = *NtOpenEventPair;
77  *p++ = *NtOpenIoCompletion;
78  *p++ = 0/*Device Object*/;
79  *p++ = 0/*NtOpenFile*/;
80  *p++ = 0/*CONTROLLER_OBJECT*/;
81  *p++ = 0/*PROFILE_OBJECT*/;
82  *p++ = 0/*TYPE_OBJECT*/;
83  *p++ = 0/*DESKTOP_OBJECT*/;
84  *p++ = 0/*WINDOWSTATION_OBJECT*/;
85  *p++ = 0/*DRIVER_OBJECT*/;
86  *p++ = 0/*TOKEN_OBJECT*/;
87  *p++ = 0/*PROCESS_OBJECT*/;
88  *p++ = 0/*THREAD_OBJECT*/;
89  *p++ = 0/*ADAPTER_OBJECT*/;
90  *p++ = 0/*PORT_OBJECT*/;
91  }
92 
94  static const LPCWSTR s_ObjectTypes[];
95 
102 
105  DynamicFct<DWORD (__stdcall*)(HANDLE*, DWORD, void*, DWORD*, DWORD, OpenStruct*)> NtOpenFile;
117  DynamicFct<DWORD (__stdcall*)(HANDLE)> NtClose;
120 };
121 
122 const LPCWSTR NTDLL::s_ObjectTypes[] = {
123  L"Directory", L"SymbolicLink",
124  L"Mutant", L"Section", L"Event", L"Semaphore",
125  L"Timer", L"Key", L"EventPair", L"IoCompletion",
126  L"Device", L"File", L"Controller", L"Profile",
127  L"Type", L"Desktop", L"WindowStatiom", L"Driver",
128  L"Token", L"Process", L"Thread", L"Adapter", L"Port",
129  0
130 };
131 
133 
134 
137  {
138  (*g_NTDLL->RtlInitUnicodeString)(this, str);
139  }
140 
142  {
143  alloc_len = len;
144  string_ptr = buffer;
145  }
146 
147  operator LPCWSTR() const {return string_ptr;}
148 };
149 
150 
151 static DWORD NtOpenObject(OBJECT_TYPE type, HANDLE* phandle, DWORD access, LPCWSTR path/*, BOOL xflag=FALSE*/)
152 {
153  UnicodeString ustr(path);
154  OpenStruct open_struct = {sizeof(OpenStruct), 0x00, &ustr, 0x40};
155 
158 
159  /* if (xflag)
160  access |= GENERIC_READ; */
161 
162  DWORD ioStatusBlock[2]; // IO_STATUS_BLOCK
163 
165  return g_NTDLL->_ObjectOpenFunctions[type](phandle, access|STANDARD_RIGHTS_READ, &open_struct);
166  else if (type == FILE_OBJECT)
167  return (*g_NTDLL->NtOpenFile)(phandle, access, &open_struct, ioStatusBlock, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 0/*OpenOptions*/);
168  else
169  return ERROR_INVALID_FUNCTION;
170 }
171 
172 
173 void NtObjDirectory::read_directory(int scan_flags)
174 {
175  CONTEXT("NtObjDirectory::read_directory()");
176 
177  if (!g_NTDLL)
178  g_NTDLL = new NTDLL();
179 
180  Entry* first_entry = NULL;
181  int level = _level + 1;
182 
184 
186 #ifndef UNICODE
187  WCHAR wbuffer[MAX_PATH], *w=wbuffer;
188 #endif
189 
190  do {
191  *p++ = *path;
192 #ifndef UNICODE
193  *w++ = *path;
194 #endif
195  } while(*path++);
196  --p;
197 #ifndef UNICODE
198  --w;
199 #endif
200 
201  DWORD idx;
202  HANDLE dir_handle;
203 
204 #ifdef UNICODE
205  if (NtOpenObject(_type, &dir_handle, 0, buffer))
206 #else
207  if (NtOpenObject(_type, &dir_handle, 0, wbuffer))
208 #endif
209  return;
210 
211 #ifdef UNICODE
212  if (p[-1] != '\\')
213  *p++ = '\\';
214 #else
215  if (w[-1] != '\\')
216  *w++ = '\\';
217 #endif
218 
220 
221  if (!(*g_NTDLL->NtQueryDirectoryObject)(dir_handle, info, 2048, TRUE, TRUE, &idx, NULL)) {
222  WIN32_FIND_DATA w32fd;
223  Entry* last = NULL;
224  Entry* entry;
225 
226  do {
227  memset(&w32fd, 0, sizeof(WIN32_FIND_DATA));
228 
229 #ifdef UNICODE
230  if (info->name.string_ptr) {
231  info->name.string_ptr[info->name.string_len / sizeof(WCHAR)] = 0;
232  } else {
233  TCHAR empty_string_ptr[] = _T("");
234  info->name.string_ptr = empty_string_ptr;
235  }
236  if (info->type.string_ptr) {
237  info->type.string_ptr[info->type.string_len / sizeof(WCHAR)] = 0;
238  } else {
239  TCHAR empty_string_ptr[] = _T("");
240  info->type.string_ptr = empty_string_ptr;
241  }
242  lstrcpynW(p, info->name.string_ptr, COUNTOF(buffer));
243 #else
244  WideCharToMultiByte(CP_ACP, 0, info->name.string_ptr, info->name.string_len, p, COUNTOF(buffer), 0, 0);
245 #endif
246 
247  lstrcpyn(w32fd.cFileName, p, sizeof(w32fd.cFileName) / sizeof(0[w32fd.cFileName]));
248 
249  const LPCWSTR* tname = NTDLL::s_ObjectTypes;
251 
252  for(; *tname; tname++)
253  if (!wcsncmp(info->type.string_ptr, *tname, 32))
254  {type=OBJECT_TYPE(tname-NTDLL::s_ObjectTypes); break;}
255 
256  if (type == DIRECTORY_OBJECT) {
257  w32fd.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
258 
259  entry = new NtObjDirectory(this, buffer);
260  }
261 
262  else if (type == SYMBOLICLINK_OBJECT) {
263  w32fd.dwFileAttributes |= ATTRIBUTE_SYMBOLIC_LINK;
264 
265  entry = NULL;
266 
267 #ifndef _NO_WIN_FS
268  if (*w32fd.cFileName>='A' &&*w32fd.cFileName<='Z' && w32fd.cFileName[1]==':')
269  if (!_tcsncmp(buffer,TEXT("\\??\\"),4) || // NT4
270  !_tcsncmp(buffer,TEXT("\\GLOBAL??"),9)) { // XP
271  w32fd.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
272  entry = new WinDirectory(this, w32fd.cFileName);
273  }
274 #endif
275 
276  if (!entry)
277  entry = new NtObjDirectory(this, buffer);
278  }
279 
280  else if (type == KEY_OBJECT) {
281  w32fd.dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
282 
283  entry = new RegistryRoot(this, buffer);
284  }
285  else
286  entry = new NtObjEntry(this, type);
287 
288  HANDLE handle;
289 
290 #ifdef UNICODE
291  lstrcpyW(p, info->name.string_ptr);
292  if (!NtOpenObject(type, &handle, 0, buffer))
293 #else
294  lstrcpyW(w, info->name.string_ptr);
295  if (!NtOpenObject(type, &handle, 0, wbuffer))
296 #endif
297  {
299  DWORD read;
300 
301  if (!(*g_NTDLL->NtQueryObject)(handle, 0/*ObjectBasicInformation*/, &object, sizeof(NtObject), &read)) {
302  memcpy(&w32fd.ftCreationTime, &object.creation_time, sizeof(FILETIME));
303 
304  memset(&entry->_bhfi, 0, sizeof(BY_HANDLE_FILE_INFORMATION));
305  entry->_bhfi.nNumberOfLinks = object.reference_count - 1;
306  entry->_bhfi_valid = true;
307  }
308 
309  if (type == SYMBOLICLINK_OBJECT) {
310  WCHAR wbuffer[_MAX_PATH];
311  UnicodeString link(_MAX_PATH, wbuffer);
312 
314  int len = link.string_len/sizeof(WCHAR);
315  entry->_content = (LPTSTR) malloc((len+1)*sizeof(TCHAR));
316 #ifdef UNICODE
317  wcsncpy_s(entry->_content, len+1, link, len);
318 #else
319  U2nA(link, entry->_content, len);
320 #endif
321  entry->_content[len] = '\0';
322  }
323  }
324 
325  (*g_NTDLL->NtClose)(handle);
326  }
327 
328  memcpy(&entry->_data, &w32fd, sizeof(WIN32_FIND_DATA));
329 
330 #ifdef UNICODE
331  entry->_type_name = _wcsdup(info->type.string_ptr);
332 #else
333  char type_name[32];
334  WideCharToMultiByte(CP_ACP, 0, info->type.string_ptr, info->type.string_len, type_name, 32, 0, 0);
335  entry->_type_name = _strdup(type_name);
336 #endif
337 
338  if (!first_entry)
339  first_entry = entry;
340 
341  if (last)
342  last->_next = entry;
343 
344  entry->_level = level;
345 
346  last = entry;
347  } while(!(*g_NTDLL->NtQueryDirectoryObject)(dir_handle, info, 2048, TRUE, FALSE, &idx, NULL));
348 
349  last->_next = NULL;
350  }
351 
352  (*g_NTDLL->NtClose)(dir_handle);
353 
354  _down = first_entry;
355  _scanned = true;
356 }
357 
358 
360 {
361  LPCTSTR name = (LPCTSTR)p;
362 
363  for(Entry*entry=_down; entry; entry=entry->_next) {
364  LPCTSTR p = name;
365  LPCTSTR q = entry->_data.cFileName;
366 
367  do {
368  if (!*p || *p==TEXT('\\') || *p==TEXT('/'))
369  return entry;
370  } while(tolower(*p++) == tolower(*q++));
371 
372  p = name;
373  q = entry->_data.cAlternateFileName;
374 
375  do {
376  if (!*p || *p==TEXT('\\') || *p==TEXT('/'))
377  return entry;
378  } while(tolower(*p++) == tolower(*q++));
379  }
380 
381  return NULL;
382 }
383 
384 
385  // get full path of specified directory entry
386 bool NtObjEntry::get_path(PTSTR path, size_t path_count) const
387 {
388  return get_path_base ( path, path_count, ET_NTOBJS );
389 }
390 
392 {
393  return FALSE;
394 }
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenTimer
Definition: ntobjfs.cpp:115
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
GLint level
Definition: gl.h:1546
struct _CONTEXT CONTEXT
namespace GUID const ADDRINFOEXW ADDRINFOEXW struct timeval OVERLAPPED LPLOOKUPSERVICE_COMPLETION_ROUTINE HANDLE * handle
Definition: sock.c:82
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
DynamicFct< DWORD(__stdcall *)(HANDLE, DWORD, NtObject *, DWORD size, DWORD *read)> NtQueryObject
Definition: ntobjfs.cpp:108
DynamicFct< void(__stdcall *)(RtlUnicodeString *, LPCWSTR)> RtlInitUnicodeString
Definition: ntobjfs.cpp:97
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define WideCharToMultiByte
Definition: compat.h:101
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define ERROR_INVALID_FUNCTION
Definition: dderror.h:6
UnicodeString(size_t len, LPWSTR buffer)
Definition: ntobjfs.cpp:141
POINT last
Definition: font.c:46
DynamicFct< DWORD(__stdcall *)(RtlUnicodeString *)> RtlFreeUnicodeString
Definition: ntobjfs.cpp:99
GLsizei const GLchar ** path
Definition: glext.h:7234
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenSemaphore
Definition: ntobjfs.cpp:114
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenEvent
Definition: ntobjfs.cpp:111
#define CP_ACP
Definition: compat.h:99
uint8_t entry
Definition: isohybrid.c:63
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenSymbolicLinkObject
Definition: ntobjfs.cpp:106
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
DynamicFct< void(__stdcall *)(RtlAnsiString *, LPCSTR)> RtlInitAnsiString
Definition: ntobjfs.cpp:96
GLuint buffer
Definition: glext.h:5915
virtual bool get_path(PTSTR path, size_t path_count) const
Definition: ntobjfs.cpp:386
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define _strdup
Definition: debug_ros.c:7
DynamicFct< DWORD(__stdcall *)(HANDLE, RtlUnicodeString *, DWORD *)> NtQuerySymbolicLinkObject
Definition: ntobjfs.cpp:107
#define BOOL
Definition: nt_native.h:43
NtObjDirectory(LPCTSTR root_path)
Definition: ntobjfs.h:102
#define DWORD
Definition: nt_native.h:44
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define FILE_SHARE_READ
Definition: compat.h:125
#define alloca
Definition: malloc.h:361
#define lstrcpynW
Definition: compat.h:397
struct _test_info info[]
Definition: SetCursorPos.c:19
#define U2nA(s, d, l)
Definition: utility.h:129
NTDLL * g_NTDLL
Definition: ntobjfs.cpp:132
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenThread
Definition: ntobjfs.cpp:119
DynamicFct< DWORD(__stdcall *)(RtlUnicodeString *, const RtlAnsiString *, BOOL)> RtlAnsiStringToUnicodeString
Definition: ntobjfs.cpp:100
unsigned int BOOL
Definition: ntddk_ex.h:94
#define _MAX_PATH
Definition: utility.h:77
unsigned int idx
Definition: utils.c:41
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
WORD alloc_len
Definition: ntobjfs.h:48
const char * LPCSTR
Definition: xmlstorage.h:183
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenKey
Definition: ntobjfs.cpp:116
CHAR * PTSTR
Definition: xmlstorage.h:191
OBJECT_TYPE
Definition: ntobjenum.h:27
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
char TCHAR
Definition: xmlstorage.h:189
NtObjEntry(Entry *parent, OBJECT_TYPE type)
Definition: ntobjfs.h:87
#define _T(x)
Definition: vfdio.h:22
#define FILE_LIST_DIRECTORY
Definition: nt_native.h:629
GLsizeiptr size
Definition: glext.h:5919
LPWSTR string_ptr
Definition: ntobjfs.h:49
__wchar_t WCHAR
Definition: xmlstorage.h:180
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenDirectoryObject
Definition: ntobjfs.cpp:103
virtual BOOL launch_entry(HWND hwnd, UINT nCmdShow)
Definition: ntobjfs.cpp:391
Entry * _down
Definition: entries.h:93
UnicodeString(LPCWSTR str)
Definition: ntobjfs.cpp:136
DWORD(__stdcall * NTOBJECTOPENFUNCTIONS)(HANDLE *, DWORD, OpenStruct *)
Definition: ntobjfs.cpp:38
#define MAX_PATH
Definition: compat.h:26
OBJECT_TYPE _type
Definition: ntobjfs.h:89
unsigned long DWORD
Definition: ntddk_ex.h:95
PVOID HANDLE
Definition: typedefs.h:71
DynamicFct< DWORD(__stdcall *)(RtlAnsiString *, const RtlUnicodeString *, BOOL)> RtlUnicodeStringToAnsiString
Definition: ntobjfs.cpp:101
#define __stdcall
Definition: typedefs.h:25
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
_Check_return_ _CRTIMP int __cdecl wcsncmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
#define wcsncpy_s(d, l, s, n)
Definition: utility.h:202
DynamicFct< DWORD(__stdcall *)(RtlAnsiString *)> RtlFreeAnsiString
Definition: ntobjfs.cpp:98
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
static const WCHAR L[]
Definition: oid.c:1250
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenSection
Definition: ntobjfs.cpp:110
NTDLL()
Definition: ntobjfs.cpp:41
#define STANDARD_RIGHTS_READ
Definition: nt_native.h:65
DynamicFct< DWORD(__stdcall *)(HANDLE)> NtClose
Definition: ntobjfs.cpp:117
int _level
Definition: entries.h:98
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenMutant
Definition: ntobjfs.cpp:109
static DWORD NtOpenObject(OBJECT_TYPE type, HANDLE *phandle, DWORD access, LPCWSTR path)
Definition: ntobjfs.cpp:151
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenProcess
Definition: ntobjfs.cpp:118
Registry key entry.
Definition: regfs.h:63
#define TEXT(s)
Definition: k32.h:26
bool get_path_base(PTSTR path, size_t path_count, ENTRY_TYPE etype) const
Definition: entries.cpp:639
void * _path
Definition: entries.h:143
NTOBJECTOPENFUNCTIONS _ObjectOpenFunctions[23]
Definition: ntobjfs.cpp:93
#define lstrcpyW
Definition: compat.h:406
GLuint GLint GLboolean GLint GLenum access
Definition: glext.h:7866
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenEventPair
Definition: ntobjfs.cpp:112
Definition: services.c:325
unsigned int UINT
Definition: ndis.h:50
static const LPCWSTR s_ObjectTypes[]
Definition: ntobjfs.cpp:94
bool _scanned
Definition: entries.h:97
virtual void read_directory(int scan_flags=0)
Definition: ntobjfs.cpp:173
DynamicFct< DWORD(__stdcall *)(HANDLE, NtObjectInfo *, DWORD size, BOOL, BOOL, void *, void *)> NtQueryDirectoryObject
Definition: ntobjfs.cpp:104
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, OpenStruct *)> NtOpenIoCompletion
Definition: ntobjfs.cpp:113
Definition: name.c:36
#define malloc
Definition: debug_ros.c:4
#define COUNTOF(x)
Definition: utility.h:93
Windows file system directory-entry.
Definition: winfs.h:45
const WCHAR * link
Definition: db.cpp:985
GLfloat GLfloat p
Definition: glext.h:8902
WCHAR * LPWSTR
Definition: xmlstorage.h:184
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)
#define memset(x, y, z)
Definition: compat.h:39
int tolower(int c)
Definition: utclib.c:902
virtual Entry * find_entry(const void *)
Definition: ntobjfs.cpp:359
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9
#define ATTRIBUTE_SYMBOLIC_LINK
Definition: entries.h:62
base of all file and directory entries
Definition: entries.h:82
void * object
Definition: jmemsys.h:48
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
#define CONSTRUCT_NTDLLFCT(x)
Definition: ntobjfs.cpp:36
#define lstrcpyn
Definition: winbase.h:3689
DynamicFct< DWORD(__stdcall *)(HANDLE *, DWORD, void *, DWORD *, DWORD, OpenStruct *)> NtOpenFile
Definition: ntobjfs.cpp:105
GLuint const GLchar * name
Definition: glext.h:6031