Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
8.3 KiB

  1. // genoid.c
  2. #include "oidtst.h"
  3. #define VOLUME_PATH L"\\\\.\\H:"
  4. #define VOLUME_DRIVE_LETTER_INDEX 4
  5. #define FULL_PATH L"\\??\\H:\\1234567890123456"
  6. #define FULL_DRIVE_LETTER_INDEX 4
  7. #define DEVICE_PREFIX_LEN 14
  8. #define RELATIVE_OPEN
  9. int
  10. FsTestGenOid(
  11. IN HANDLE hFile,
  12. IN FILE_OBJECTID_BUFFER *ObjectIdBuffer
  13. )
  14. {
  15. IO_STATUS_BLOCK IoStatusBlock;
  16. NTSTATUS Status;
  17. Status = NtFsControlFile( hFile, // file handle
  18. NULL, // event
  19. NULL, // apc routine
  20. NULL, // apc context
  21. &IoStatusBlock, // iosb
  22. FSCTL_CREATE_OR_GET_OBJECT_ID, // FsControlCode
  23. &hFile, // input buffer
  24. sizeof(HANDLE), // input buffer length
  25. ObjectIdBuffer, // OutputBuffer for data from the FS
  26. sizeof(FILE_OBJECTID_BUFFER) ); // OutputBuffer Length
  27. if (Status == STATUS_SUCCESS) {
  28. printf( "\nOid for this file is:" );
  29. FsTestHexDump( ObjectIdBuffer->ObjectId, 16 );
  30. printf( "\nExtended info is:" );
  31. FsTestHexDump( ObjectIdBuffer->ObjectId, 64 );
  32. }
  33. return FsTestDecipherStatus( Status );
  34. }
  35. int
  36. FsTestGetFileId(
  37. IN HANDLE hFile,
  38. IN PFILE_INTERNAL_INFORMATION FileId
  39. )
  40. {
  41. IO_STATUS_BLOCK IoStatusBlock;
  42. NTSTATUS Status;
  43. Status = NtQueryInformationFile( hFile, // file handle
  44. &IoStatusBlock, // iosb
  45. FileId,
  46. sizeof( FILE_INTERNAL_INFORMATION ),
  47. FileInternalInformation );
  48. if (Status == STATUS_SUCCESS) {
  49. printf( "\nFile id for this file is:" );
  50. FsTestHexDump( (PCHAR)FileId, 8 );
  51. }
  52. return FsTestDecipherStatus( Status );
  53. }
  54. int
  55. FsTestGetName (
  56. HANDLE File
  57. )
  58. {
  59. NTSTATUS Status;
  60. IO_STATUS_BLOCK IoStatusBlock;
  61. PFILE_NAME_INFORMATION FileName;
  62. WCHAR buffer[100];
  63. FileName = (PFILE_NAME_INFORMATION) buffer;
  64. Status = NtQueryInformationFile( File,
  65. &IoStatusBlock,
  66. FileName,
  67. sizeof( buffer ),
  68. FileNameInformation );
  69. if (Status == STATUS_SUCCESS)
  70. {
  71. printf( "\nFilename is: %.*lS",
  72. FileName->FileNameLength/sizeof(WCHAR),
  73. FileName->FileName );
  74. }
  75. return FsTestDecipherStatus( Status );
  76. }
  77. DWORD
  78. FsTestOpenVolumeHandle (
  79. PWCHAR DriveLetter,
  80. HANDLE *VolumeHandle
  81. )
  82. {
  83. WCHAR Volume[] = VOLUME_PATH;
  84. DWORD Status = 0;
  85. //
  86. // Open the volume for relative opens.
  87. //
  88. RtlCopyMemory( &Volume[VOLUME_DRIVE_LETTER_INDEX], DriveLetter, sizeof(WCHAR) );
  89. *VolumeHandle = CreateFileW( (PUSHORT) &Volume,
  90. GENERIC_READ | GENERIC_WRITE,
  91. FILE_SHARE_READ | FILE_SHARE_WRITE,
  92. NULL,
  93. OPEN_EXISTING,
  94. 0,
  95. NULL );
  96. if (*VolumeHandle == INVALID_HANDLE_VALUE) {
  97. Status = GetLastError();
  98. printf( "Unable to open %ws volume\n", &Volume );
  99. printf( "Error from CreateFile", Status );
  100. return Status;
  101. }
  102. return Status;
  103. }
  104. NTSTATUS
  105. FsTestOpenFileById (
  106. IN HANDLE VolumeHandle,
  107. IN PUNICODE_STRING FileId,
  108. OUT HANDLE *File
  109. )
  110. {
  111. NTSTATUS Status;
  112. OBJECT_ATTRIBUTES ObjectAttributes;
  113. IO_STATUS_BLOCK IoStatusBlock;
  114. InitializeObjectAttributes( &ObjectAttributes,
  115. FileId,
  116. 0,
  117. VolumeHandle,
  118. NULL );
  119. Status = NtCreateFile( File,
  120. GENERIC_READ,
  121. &ObjectAttributes,
  122. &IoStatusBlock,
  123. NULL,
  124. 0,
  125. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  126. FILE_OVERWRITE,
  127. FILE_OPEN_BY_FILE_ID,
  128. NULL,
  129. 0 );
  130. if (!NT_SUCCESS( Status )) {
  131. FsTestDecipherStatus( Status );
  132. return Status;
  133. }
  134. return Status;
  135. }
  136. VOID
  137. FsTestGenerateFullName (
  138. IN PWCHAR DriveLetter,
  139. IN PUNICODE_STRING FileId,
  140. OUT PUNICODE_STRING FullName
  141. )
  142. {
  143. WCHAR FullPath[] = FULL_PATH;
  144. UNICODE_STRING DeviceName;
  145. ASSERT( FullName->MaximumLength >= (DEVICE_PREFIX_LEN + FileId->Length) );
  146. RtlCopyMemory( &FullPath[FULL_DRIVE_LETTER_INDEX], DriveLetter, sizeof(WCHAR) );
  147. DeviceName.Length = DeviceName.MaximumLength = DEVICE_PREFIX_LEN;
  148. DeviceName.Buffer = FullPath;
  149. RtlCopyUnicodeString( FullName, &DeviceName );
  150. RtlAppendUnicodeStringToString( FullName, FileId );
  151. }
  152. VOID
  153. _cdecl
  154. main(
  155. int argc,
  156. char *argv[]
  157. )
  158. {
  159. HANDLE File;
  160. HANDLE VolumeHandle = NULL;
  161. FILE_OBJECTID_BUFFER ObjectIdBuffer;
  162. FILE_INTERNAL_INFORMATION FileId;
  163. OBJECT_ATTRIBUTES ObjectAttributes;
  164. IO_STATUS_BLOCK IoStatusBlock;
  165. UNICODE_STRING IdString;
  166. NTSTATUS Status;
  167. WCHAR DriveLetter;
  168. WCHAR FullNameBuffer [100];
  169. UNICODE_STRING FullName;
  170. //
  171. // Get parameters
  172. //
  173. if (argc < 3) {
  174. printf("This program finds the object id of a file and generates one if necessary (ntfs only), then prints out the file name once that file is opened by the ids.\n\n");
  175. printf("usage: %s drive filename\n", argv[0]);
  176. return;
  177. }
  178. File = CreateFile( argv[2],
  179. 0,
  180. FILE_SHARE_READ | FILE_SHARE_WRITE,
  181. NULL,
  182. OPEN_EXISTING,
  183. 0,
  184. NULL );
  185. if ( File == INVALID_HANDLE_VALUE ) {
  186. printf( "Error opening file %s %x\n", argv[2], GetLastError() );
  187. return;
  188. }
  189. FsTestGetName( File );
  190. RtlZeroBytes( &FileId, sizeof( FileId ) );
  191. FsTestGetFileId( File, &FileId );
  192. RtlZeroBytes( &ObjectIdBuffer, sizeof( ObjectIdBuffer ) );
  193. FsTestGenOid( File, &ObjectIdBuffer );
  194. CloseHandle( File );
  195. DriveLetter = *argv[1];
  196. #ifdef RELATIVE_OPEN
  197. FsTestOpenVolumeHandle( &DriveLetter, &VolumeHandle );
  198. if (VolumeHandle == INVALID_HANDLE_VALUE) {
  199. goto main_exit;
  200. }
  201. #endif
  202. RtlInitEmptyUnicodeString( &FullName, FullNameBuffer, sizeof( FullNameBuffer ) );
  203. printf( "\nReopening file by file id....\n" );
  204. IdString.Length = IdString.MaximumLength = sizeof( LARGE_INTEGER );
  205. IdString.Buffer = (PWSTR) &FileId;
  206. #ifdef RELATIVE_OPEN
  207. Status = FsTestOpenFileById( VolumeHandle, &IdString , &File );
  208. #else
  209. FsTestGenerateFullName( &DriveLetter, &IdString, &FullName );
  210. Status = FsTestOpenFileById( VolumeHandle, &FullName , &File );
  211. #endif
  212. if (!NT_SUCCESS( Status )) {
  213. goto main_exit;
  214. }
  215. FsTestGetName( File );
  216. NtClose( File );
  217. printf( "\nReopening file by object id....\n" );
  218. IdString.Length = IdString.MaximumLength = 16 * sizeof( UCHAR );
  219. IdString.Buffer = (PWSTR) &(ObjectIdBuffer.ObjectId);
  220. #ifdef RELATIVE_OPEN
  221. Status = FsTestOpenFileById( VolumeHandle, &IdString , &File );
  222. #else
  223. FsTestGenerateFullName( &DriveLetter, &IdString, &FullName );
  224. Status = FsTestOpenFileById( VolumeHandle, &FullName , &File );
  225. #endif
  226. if (!NT_SUCCESS( Status )) {
  227. goto main_exit;
  228. }
  229. FsTestGetName( File );
  230. NtClose( File );
  231. main_exit:
  232. if (VolumeHandle != NULL) {
  233. CloseHandle( VolumeHandle );
  234. }
  235. }