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.

424 lines
8.8 KiB

  1. /*++
  2. Program Description:
  3. prints out rptag info..
  4. Modification History:
  5. 09/29/97 anandn created
  6. modified from rptag_stress.c code..
  7. --*/
  8. #include "rpget.h"
  9. THREAD CHAR gszLogMsg[LINE_LENGTH]; // log message string, local to thread
  10. USHORT gMaxDataBuffSize = MAXIMUM_REPARSE_DATA_BUFFER_SIZE -
  11. FIELD_OFFSET(REPARSE_DATA_BUFFER, GenericReparseBuffer);
  12. //
  13. // main
  14. //
  15. VOID __cdecl main(INT argc, CHAR* argv[]) {
  16. BOOL fSuccess;
  17. OPTIONS Options; // command line options
  18. PBYTE pOutBuff = NULL;
  19. PUCHAR pDataBuff;
  20. USHORT usDataBuffSize;
  21. if (argc<2) {
  22. printf("Usage: %s filename\n",argv[0]);
  23. ExitProcess(1);
  24. }
  25. fSuccess = RPGet( argv[1], &pOutBuff );
  26. if ( ! fSuccess ) {
  27. printf("ERROR: RPGet failed\n");
  28. ExitProcess(1);
  29. }
  30. //
  31. // print out rp buffer
  32. //
  33. printf("\n");
  34. printf("ReparseTag = %Xh\n",
  35. (((PREPARSE_DATA_BUFFER) pOutBuff)->ReparseTag));
  36. printf("ReparseDataLength = %d\n",
  37. ((PREPARSE_DATA_BUFFER) pOutBuff)->ReparseDataLength);
  38. printf("Reserved = %d\n",
  39. ((PREPARSE_DATA_BUFFER) pOutBuff)->Reserved);
  40. printf("Dumping GenericReparseBuffer:\n");
  41. DumpBuff((PBYTE) (((PREPARSE_DATA_BUFFER) pOutBuff)->GenericReparseBuffer.DataBuffer),
  42. (WORD) ((PREPARSE_DATA_BUFFER) pOutBuff)->ReparseDataLength);
  43. }
  44. BOOL
  45. RPGet( CHAR szFileName[], BYTE **ppOutBuff)
  46. {
  47. BOOL fSuccess;
  48. DWORD dwRc = 0;
  49. DWORD gle;
  50. DWORD dwOutBuffLen;
  51. HANDLE hFile;
  52. __try {
  53. hFile = RPOpen( szFileName, "rpf" );
  54. // if open fails..try as directory..
  55. if (hFile == INVALID_HANDLE_VALUE) {
  56. hFile = RPOpen( szFileName, "rpd" );
  57. }
  58. if (hFile == INVALID_HANDLE_VALUE) {
  59. printf("error opening %s\n", szFileName);
  60. return FALSE;
  61. }
  62. dwOutBuffLen = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
  63. *ppOutBuff = (PBYTE) calloc( dwOutBuffLen, 1);
  64. if (NULL == *ppOutBuff) {
  65. printf("error callocing OutBuff in RPGet\n");
  66. return FALSE;
  67. }
  68. SetLastError( ERROR_SUCCESS );
  69. //
  70. // try the fsctl..
  71. //
  72. fSuccess = DeviceIoControl( hFile,
  73. FSCTL_GET_REPARSE_POINT,
  74. NULL,
  75. 0,
  76. *ppOutBuff,
  77. dwOutBuffLen,
  78. &dwRc,
  79. NULL);
  80. printf("dwRc= %ld\n", dwRc);
  81. if ( ! fSuccess ) {
  82. gle = GetLastError();
  83. printf("ERROR: FSCTL_GET_REPARSE_POINT in RPGet\n");
  84. printf("GLE : %ld\n",GetLastError());
  85. return FALSE;
  86. }
  87. return TRUE;
  88. } __finally {
  89. }
  90. }
  91. VOID
  92. DumpBuff( PBYTE pData, WORD wSize )
  93. {
  94. WORD i=0;
  95. WORD j=0;
  96. CHAR szData[LINE_LENGTH];
  97. WORD NumLines;
  98. NumLines = wSize/8;
  99. if (NumLines) {
  100. for( i=0; i < NumLines; i++) {
  101. szData[0] = '\0';
  102. for( j=0; j<8; j++) {
  103. sprintf(szData,TEXT("%s %02X"), szData, *(pData + i*8 + j));
  104. if (j==3) {
  105. sprintf(szData, TEXT("%s -"), szData);
  106. }
  107. }
  108. sprintf(szData, TEXT("%s "), szData);
  109. for( j=0; j<8; j++) {
  110. if (*(pData + i*8 + j) > ' ') {
  111. sprintf(szData,TEXT("%s%c"), szData, *(pData + i*8 + j));
  112. } else {
  113. sprintf(szData,TEXT("%s."), szData);
  114. }
  115. }
  116. sprintf(gszLogMsg, TEXT("%05d:%s"), i*8, szData);
  117. printf("%s\n", gszLogMsg );
  118. }
  119. }
  120. wSize %= 8;
  121. if (wSize) {
  122. szData[0] = '\0';
  123. for( j=0; j<8; j++) {
  124. if (j >= wSize) {
  125. sprintf(szData,TEXT("%s "), szData);
  126. } else {
  127. sprintf(szData,TEXT("%s %02X"), szData, *(pData + i*8 + j));
  128. }
  129. if (j==3) {
  130. sprintf(szData, TEXT("%s -"), szData);
  131. }
  132. }
  133. sprintf(szData, TEXT("%s "), szData);
  134. for( j=0; j<8; j++) {
  135. if (j >= wSize) {
  136. sprintf(szData,TEXT("%s "), szData);
  137. } else {
  138. if (*(pData + i*8 + j) > ' ') {
  139. sprintf(szData,TEXT("%s%c"), szData, *(pData + i*8 + j));
  140. } else {
  141. sprintf(szData,TEXT("%s."), szData);
  142. }
  143. }
  144. }
  145. sprintf(gszLogMsg, TEXT("%05d:%s"), i*8, szData);
  146. printf("%s\n", gszLogMsg );
  147. }
  148. }
  149. //
  150. // Simple wrapper for NtCreateFile
  151. //
  152. NTSTATUS
  153. OpenObject (
  154. WCHAR *pwszFile,
  155. ULONG CreateOptions,
  156. ULONG DesiredAccess,
  157. ULONG ShareAccess,
  158. ULONG CreateDisposition,
  159. IO_STATUS_BLOCK *IoStatusBlock,
  160. HANDLE *ObjectHandle)
  161. {
  162. NTSTATUS Status;
  163. OBJECT_ATTRIBUTES ObjectAttributes;
  164. UNICODE_STRING str;
  165. RtlDosPathNameToNtPathName_U(
  166. pwszFile,
  167. &str,
  168. NULL,
  169. NULL);
  170. InitializeObjectAttributes(
  171. &ObjectAttributes,
  172. &str,
  173. OBJ_CASE_INSENSITIVE,
  174. NULL,
  175. NULL);
  176. Status = NtCreateFile(
  177. ObjectHandle,
  178. DesiredAccess | SYNCHRONIZE,
  179. &ObjectAttributes,
  180. IoStatusBlock,
  181. NULL, // pallocationsize (none!)
  182. FILE_ATTRIBUTE_NORMAL,
  183. ShareAccess,
  184. CreateDisposition,
  185. CreateOptions,
  186. NULL, // EA buffer (none!)
  187. 0);
  188. RtlFreeHeap (RtlProcessHeap(), 0, str.Buffer);
  189. if (!NT_SUCCESS( Status )) {
  190. printf("NtCreateFile Status %Xh\n", Status);
  191. }
  192. return (Status);
  193. }
  194. //----------------------------------------------------------------------------
  195. //
  196. // RPOpen
  197. //
  198. // This function opens a file / directory in the specified mode
  199. // and if unsuccessful returns NULL
  200. // INPUT - NAME and OPTION
  201. //
  202. //----------------------------------------------------------------------------
  203. HANDLE
  204. RPOpen (LPSTR szFileName,
  205. LPSTR szOption )
  206. {
  207. DWORD dwOption = 0;
  208. DWORD dwAccess = 0;
  209. DWORD dwDisposition = 0;
  210. HANDLE hFile;
  211. NTSTATUS Status ;
  212. DWORD dwLastErr ;
  213. IO_STATUS_BLOCK IoStatusBlock ;
  214. WCHAR wszFileName[MAX_PATH];
  215. if (!_stricmp (szOption, "rpf")) {
  216. dwOption = FILE_NON_DIRECTORY_FILE |
  217. FILE_OPEN_REPARSE_POINT;
  218. dwAccess = FILE_READ_DATA |
  219. FILE_WRITE_DATA |
  220. FILE_WRITE_ATTRIBUTES |
  221. FILE_READ_ATTRIBUTES;
  222. dwDisposition = FILE_OPEN_IF ;
  223. } else if (!_stricmp (szOption, "rpd")) {
  224. dwOption = FILE_OPEN_REPARSE_POINT;
  225. dwAccess = FILE_WRITE_DATA ;
  226. dwDisposition = FILE_OPEN_IF ;
  227. } else if (!_stricmp (szOption, "d")) {
  228. dwOption = FILE_OPEN_REPARSE_POINT ;
  229. dwAccess = FILE_WRITE_DATA ;
  230. dwDisposition = FILE_OPEN_IF ;
  231. } else if (!_stricmp (szOption, "f")) {
  232. dwOption = FILE_NON_DIRECTORY_FILE;
  233. dwAccess = FILE_READ_DATA |
  234. FILE_WRITE_DATA |
  235. FILE_LIST_DIRECTORY |
  236. FILE_WRITE_ATTRIBUTES |
  237. FILE_READ_ATTRIBUTES;
  238. dwDisposition = OPEN_ALWAYS ;
  239. }
  240. SzToWsz (wszFileName, szFileName);
  241. if ( (0 == lstrcmp ( szOption , "f") ) ||
  242. (0 == lstrcmp ( szOption , "rpf") ) ) {
  243. hFile = CreateFile ( szFileName ,
  244. GENERIC_READ | GENERIC_WRITE ,
  245. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  246. NULL ,
  247. dwDisposition ,
  248. dwOption | FILE_OPEN_NO_RECALL,
  249. NULL ) ;
  250. if ( INVALID_HANDLE_VALUE == hFile ) {
  251. // printf( TEXT("ERROR: CreateFile in RPOpen\n") );
  252. }
  253. } else {
  254. Status = OpenObject (wszFileName,
  255. dwOption,
  256. dwAccess,
  257. FILE_SHARE_READ |
  258. FILE_SHARE_WRITE |
  259. FILE_SHARE_DELETE,
  260. dwDisposition,
  261. &IoStatusBlock,
  262. &hFile);
  263. if (!NT_SUCCESS (Status)) {
  264. printf( TEXT("OpenObject in RPOpen\n") );
  265. }
  266. }
  267. return hFile;
  268. }
  269. VOID
  270. SzToWsz (
  271. OUT WCHAR *Unicode,
  272. IN char *Ansi
  273. )
  274. {
  275. while (*Unicode++ = *Ansi++)
  276. ;
  277. }
  278. VOID
  279. WszToSz (
  280. OUT char *Ansi,
  281. IN WCHAR *Unicode
  282. )
  283. {
  284. while (*Ansi++ = (char) *Unicode++)
  285. ;
  286. }