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.

159 lines
4.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998
  5. //
  6. // File: idtopath.cxx
  7. //
  8. // Contents: Convert file ID to path name
  9. //
  10. // History: 16 Jul 1997 DLee Created
  11. //
  12. //--------------------------------------------------------------------------
  13. extern "C"
  14. {
  15. #include <nt.h>
  16. #include <ntioapi.h>
  17. #include <ntrtl.h>
  18. #include <nturtl.h>
  19. }
  20. #include <windows.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <memory.h>
  24. #include <process.h>
  25. #include <fcntl.h>
  26. #include <string.h>
  27. void die( char * pc, NTSTATUS s )
  28. {
  29. printf( "fail: 0x%x, '%s'\n", s, pc );
  30. exit( 1 );
  31. } //die
  32. void OpenById( HANDLE hVol, LONGLONG ll, WCHAR wcVol )
  33. {
  34. UNICODE_STRING uScope;
  35. uScope.Buffer = (WCHAR *) &ll;
  36. uScope.Length = sizeof ll;
  37. uScope.MaximumLength = sizeof ll;
  38. OBJECT_ATTRIBUTES ObjectAttr;
  39. InitializeObjectAttributes( &ObjectAttr, // Structure
  40. &uScope, // Name
  41. OBJ_CASE_INSENSITIVE, // Attributes
  42. hVol, // Root
  43. 0 ); // Security
  44. IO_STATUS_BLOCK IoStatus;
  45. HANDLE h = INVALID_HANDLE_VALUE;
  46. NTSTATUS Status = NtOpenFile( &h,
  47. FILE_READ_ATTRIBUTES,
  48. &ObjectAttr,
  49. &IoStatus,
  50. FILE_SHARE_READ |
  51. FILE_SHARE_WRITE |
  52. FILE_SHARE_DELETE,
  53. FILE_OPEN_BY_FILE_ID );
  54. if ( NT_ERROR( Status ) )
  55. {
  56. if ( STATUS_INVALID_PARAMETER == Status )
  57. {
  58. printf( "no file exists with fileid %#I64x on volume %wc\n", ll, wcVol );
  59. exit( 1 );
  60. }
  61. else
  62. die( "can't open file", Status );
  63. }
  64. unsigned cbMax = 32768 * sizeof WCHAR + sizeof FILE_NAME_INFORMATION;
  65. BYTE * pBuf = new BYTE[cbMax];
  66. if ( 0 == pBuf )
  67. return;
  68. PFILE_NAME_INFORMATION FileName = (PFILE_NAME_INFORMATION) pBuf;
  69. FileName->FileNameLength = cbMax - sizeof FILE_NAME_INFORMATION;
  70. Status = NtQueryInformationFile( h,
  71. &IoStatus,
  72. FileName,
  73. cbMax,
  74. FileNameInformation );
  75. if ( NT_ERROR( Status ) )
  76. die( "can't get filename", Status );
  77. // This is actually the full path, not the filename
  78. FileName->FileName[ FileName->FileNameLength / sizeof WCHAR ] = 0;
  79. printf( "fileid %#I64x: '%wc:%ws'\n", ll, wcVol, FileName->FileName );
  80. delete [] pBuf;
  81. NtClose( h );
  82. } //OpenById
  83. void Usage()
  84. {
  85. printf( "usage: idtopath /v:volume /i:fileid\n" );
  86. printf( " e.g.: idtopath /v:c /i:2000000001a99\n" );
  87. exit( 1 );
  88. } //Usage
  89. extern "C" int __cdecl wmain( int argc, WCHAR * argv[] )
  90. {
  91. WCHAR const *pwcId = 0;
  92. WCHAR wcVol = 0;
  93. for ( int i = 1; i < argc; i++ )
  94. {
  95. if ( L'-' == argv[i][0] || L'/' == argv[i][0] )
  96. {
  97. WCHAR wc = (WCHAR) toupper( argv[i][1] );
  98. if ( ':' != argv[i][2] )
  99. Usage();
  100. if ( 'V' == wc )
  101. wcVol = argv[i][3];
  102. else if ( 'I' == wc )
  103. pwcId = argv[i] + 3;
  104. else
  105. Usage();
  106. }
  107. else
  108. Usage();
  109. }
  110. if ( 0 == wcVol || 0 == pwcId )
  111. Usage();
  112. LONGLONG ll = 0;
  113. swscanf( pwcId, L"%I64x", &ll );
  114. WCHAR awcVol[20];
  115. wcscpy( awcVol, L"\\\\.\\k:" );
  116. awcVol[4] = wcVol;
  117. HANDLE h = CreateFileW( awcVol,
  118. GENERIC_READ,
  119. FILE_SHARE_READ | FILE_SHARE_WRITE,
  120. 0,
  121. OPEN_EXISTING,
  122. 0, 0 );
  123. if ( INVALID_HANDLE_VALUE == h )
  124. die( "can't open volume", GetLastError() );
  125. OpenById( h, ll, wcVol );
  126. CloseHandle( h );
  127. return 0;
  128. } //main