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.

410 lines
12 KiB

  1. #include "pch.cxx"
  2. #pragma hdrstop
  3. #include "teststub.cxx"
  4. #define TRKDATA_ALLOCATE
  5. #include "trksvr.hxx"
  6. #undef TRKDATA_ALLOCATE
  7. #import "itrkadmn.tlb" no_namespace
  8. #include "itrkadmn.hxx"
  9. const TCHAR tszKeyNameLinkTrack[] = TEXT("System\\CurrentControlSet\\Services\\TrkWks\\Parameters");
  10. DWORD g_Debug = TRKDBG_ERROR;
  11. void Usage()
  12. {
  13. _tprintf( TEXT("\n")
  14. TEXT("Purpose: Use this test to check the ownership status - or take ownership - of\n")
  15. TEXT(" file(s) or volume(s).\n")
  16. TEXT("Usage: TForceOwn <options> <file(s) or volume(s)>\n")
  17. TEXT("Options: -f<scope> Take ownership of file(s)\n")
  18. TEXT(" -f<scope>s Check file ownership status\n")
  19. TEXT(" -v<scope> Take ownership of volume(s)\n")
  20. TEXT(" -v<scope>s Check volume ownership status\n")
  21. TEXT("Where <scope> is:\n")
  22. TEXT(" f One file\n")
  23. TEXT(" v One volume\n")
  24. TEXT(" m Whole machine\n")
  25. TEXT("E.g.:\n")
  26. TEXT(" TForceOwn -vs \\\\machine\\share\\path\\file.txt\n")
  27. TEXT(" // Gets status for the volume containing file.txt\n")
  28. TEXT(" TForceOwn -ffs \\\\machine\\share\\path\\file.txt\n")
  29. TEXT(" // Gets status for a single file\n")
  30. TEXT(" TForceOwn -fm\n")
  31. TEXT(" // Forces all files on \\\\machine to be owned by that machine\n") );
  32. }
  33. void
  34. DoVolumeStatus( TrkInfoScope scope, TCHAR *ptszUncPath )
  35. {
  36. HRESULT hr = E_FAIL;
  37. LONG cVols = 0;
  38. LONG iVol;
  39. BSTR bstr = NULL;
  40. LONG lLowerBound, lUpperBound;
  41. // BUGBUG: we should have a try/catch(_com_error) here, but to do that
  42. // we need to set USE_NATIVE_EH in the sources file, and we can't do
  43. // that yet because the itrkadmn dll is still using __try.
  44. _bstr_t bstrPath( ptszUncPath );
  45. VARIANT varrglongIndex;
  46. VARIANT varrgbstrVolId;
  47. VARIANT varrglongStatus;
  48. ITrkForceOwnershipPtr pForceOwn( TEXT("LinkTrack.TrkForceOwnership.1") );
  49. __try
  50. {
  51. SAFEARRAYBOUND sabound;
  52. hr = pForceOwn->VolumeStatus( bstrPath, scope, &varrglongIndex, &varrgbstrVolId, &varrglongStatus );
  53. if( FAILED(hr) ) TrkRaiseException( hr );
  54. TrkAssert( (VT_ARRAY|VT_I4) == varrglongIndex.vt );
  55. TrkAssert( (VT_ARRAY|VT_BSTR) == varrgbstrVolId.vt );
  56. TrkAssert( (VT_ARRAY|VT_I4) == varrglongStatus.vt );
  57. TrkAssert( 1 == SafeArrayGetDim( varrglongIndex.parray ));
  58. TrkAssert( 1 == SafeArrayGetDim( varrgbstrVolId.parray ));
  59. TrkAssert( 1 == SafeArrayGetDim( varrglongStatus.parray ));
  60. hr = SafeArrayGetLBound( varrglongIndex.parray, 1, &lLowerBound );
  61. TrkAssert( SUCCEEDED(hr) && 0 == lLowerBound );
  62. hr = SafeArrayGetLBound( varrgbstrVolId.parray, 1, &lLowerBound );
  63. TrkAssert( SUCCEEDED(hr) && 0 == lLowerBound );
  64. hr = SafeArrayGetLBound( varrglongStatus.parray, 1, &lLowerBound );
  65. TrkAssert( SUCCEEDED(hr) && 0 == lLowerBound );
  66. hr = SafeArrayGetUBound( varrglongIndex.parray, 1, &lUpperBound );
  67. if( FAILED(hr) ) TrkRaiseException( hr );
  68. cVols = lUpperBound + 1;
  69. hr = SafeArrayGetUBound( varrgbstrVolId.parray, 1, &lUpperBound );
  70. TrkAssert( SUCCEEDED(hr) && cVols == lUpperBound + 1 );
  71. hr = SafeArrayGetUBound( varrglongStatus.parray, 1, &lUpperBound );
  72. TrkAssert( SUCCEEDED(hr) && cVols == lUpperBound + 1 );
  73. _tprintf( TEXT("\n")
  74. TEXT("Volume ownership status for %s\n"),
  75. ptszUncPath );
  76. for( iVol = 0; iVol < cVols; iVol++ )
  77. {
  78. LONG lVolIndex, lStatus;
  79. BSTR bstr = NULL;
  80. _tprintf( TEXT("\n") );
  81. hr = SafeArrayGetElement( varrglongIndex.parray, &iVol, &lVolIndex );
  82. if( FAILED(hr) ) TrkRaiseException( hr );
  83. _tprintf( TEXT("%10s %c\n"), TEXT("Volume:"), TEXT('A')+static_cast<TCHAR>(lVolIndex) );
  84. hr = SafeArrayGetElement( varrgbstrVolId.parray, &iVol, &bstr );
  85. if( FAILED(hr) ) TrkRaiseException( hr );
  86. _tprintf( TEXT("%10s %s\n"), TEXT("ID:"), bstr );
  87. SysFreeString( bstr ); bstr = NULL;
  88. hr = SafeArrayGetElement( varrglongStatus.parray, &iVol, &lStatus );
  89. if( FAILED(hr) ) TrkRaiseException( hr );
  90. _tprintf( TEXT("%10s %s\n"), TEXT("Status:"), (TCHAR*) CObjectOwnershipString(lStatus) );
  91. }
  92. }
  93. __except( BreakOnDebuggableException() )
  94. {
  95. hr = GetExceptionCode();
  96. TrkAssert( STATUS_ACCESS_VIOLATION != hr );
  97. }
  98. Exit:
  99. VariantClear( &varrglongIndex );
  100. VariantClear( &varrgbstrVolId );
  101. VariantClear( &varrglongStatus );
  102. if( bstr ) SysFreeString( bstr );
  103. if( FAILED(hr) )
  104. _tprintf( TEXT("DoVolumeStatus failed: %08x\n"), hr );
  105. }
  106. void
  107. DoVolumes( TrkInfoScope scope, TCHAR *ptszUncPath )
  108. {
  109. HRESULT hr = E_FAIL;
  110. // BUGBUG: we should have a try/catch(_com_error) here, but to do that
  111. // we need to set USE_NATIVE_EH in the sources file, and we can't do
  112. // that yet because the itrkadmn dll is still using __try.
  113. _bstr_t bstrPath( ptszUncPath );
  114. ITrkForceOwnershipPtr pForceOwn( TEXT("LinkTrack.TrkForceOwnership.1") );
  115. __try
  116. {
  117. hr = pForceOwn->Volumes( bstrPath, scope );
  118. if( FAILED(hr) ) TrkRaiseException( hr );
  119. }
  120. __except( BreakOnDebuggableException() )
  121. {
  122. hr = GetExceptionCode();
  123. TrkAssert( STATUS_ACCESS_VIOLATION != hr );
  124. }
  125. Exit:
  126. if( FAILED(hr) )
  127. _tprintf( TEXT("DoVolumes failed: %08x\n"), hr );
  128. }
  129. void
  130. DoFileStatus( TrkInfoScope scope, TCHAR *ptszUncPath )
  131. {
  132. HRESULT hr = E_FAIL;
  133. LONG cFiles = 0;
  134. LONG iFile;
  135. BSTR bstr = NULL;
  136. LONG lLowerBound, lUpperBound;
  137. // BUGBUG: we should have a try/catch(_com_error) here, but to do that
  138. // we need to set USE_NATIVE_EH in the sources file, and we can't do
  139. // that yet because the itrkadmn dll is still using __try.
  140. _bstr_t bstrPath( ptszUncPath );
  141. VARIANT varrgbstrFileName;
  142. VARIANT varrgbstrFileId;
  143. VARIANT varrglongStatus;
  144. ITrkForceOwnershipPtr pForceOwn( TEXT("LinkTrack.TrkForceOwnership.1") );
  145. __try
  146. {
  147. SAFEARRAYBOUND sabound;
  148. hr = pForceOwn->FileStatus( bstrPath, scope, &varrgbstrFileName, &varrgbstrFileId, &varrglongStatus );
  149. if( FAILED(hr) ) TrkRaiseException( hr );
  150. TrkAssert( (VT_ARRAY|VT_BSTR) == varrgbstrFileName.vt );
  151. TrkAssert( (VT_ARRAY|VT_BSTR) == varrgbstrFileId.vt );
  152. TrkAssert( (VT_ARRAY|VT_I4) == varrglongStatus.vt );
  153. TrkAssert( 1 == SafeArrayGetDim( varrgbstrFileName.parray ));
  154. TrkAssert( 1 == SafeArrayGetDim( varrgbstrFileId.parray ));
  155. TrkAssert( 1 == SafeArrayGetDim( varrglongStatus.parray ));
  156. hr = SafeArrayGetLBound( varrgbstrFileName.parray, 1, &lLowerBound );
  157. TrkAssert( SUCCEEDED(hr) && 0 == lLowerBound );
  158. hr = SafeArrayGetLBound( varrgbstrFileId.parray, 1, &lLowerBound );
  159. TrkAssert( SUCCEEDED(hr) && 0 == lLowerBound );
  160. hr = SafeArrayGetLBound( varrglongStatus.parray, 1, &lLowerBound );
  161. TrkAssert( SUCCEEDED(hr) && 0 == lLowerBound );
  162. hr = SafeArrayGetUBound( varrgbstrFileName.parray, 1, &lUpperBound );
  163. if( FAILED(hr) ) TrkRaiseException( hr );
  164. cFiles = lUpperBound + 1;
  165. hr = SafeArrayGetUBound( varrgbstrFileId.parray, 1, &lUpperBound );
  166. TrkAssert( SUCCEEDED(hr) && cFiles == lUpperBound + 1 );
  167. hr = SafeArrayGetUBound( varrglongStatus.parray, 1, &lUpperBound );
  168. TrkAssert( SUCCEEDED(hr) && cFiles == lUpperBound + 1 );
  169. _tprintf( TEXT("\n")
  170. TEXT("File ownership status for %s\n"),
  171. ptszUncPath );
  172. for( iFile = 0; iFile < cFiles; iFile++ )
  173. {
  174. BSTR bstr = NULL;
  175. long lStatus;
  176. _tprintf( TEXT("\n") );
  177. hr = SafeArrayGetElement( varrgbstrFileName.parray, &iFile, &bstr );
  178. if( FAILED(hr) ) TrkRaiseException( hr );
  179. _tprintf( TEXT("%10s %s\n"), TEXT("File:"), bstr );
  180. SysFreeString( bstr ); bstr = NULL;
  181. hr = SafeArrayGetElement( varrgbstrFileId.parray, &iFile, &bstr );
  182. if( FAILED(hr) ) TrkRaiseException( hr );
  183. _tprintf( TEXT("%10s %s\n"), TEXT("ID:"), bstr );
  184. SysFreeString( bstr ); bstr = NULL;
  185. hr = SafeArrayGetElement( varrglongStatus.parray, &iFile, &lStatus );
  186. if( FAILED(hr) ) TrkRaiseException( hr );
  187. _tprintf( TEXT("%10s %s\n"), TEXT("Status:"), (TCHAR*) CObjectOwnershipString(lStatus) );
  188. }
  189. }
  190. __except( BreakOnDebuggableException() )
  191. {
  192. hr = GetExceptionCode();
  193. TrkAssert( STATUS_ACCESS_VIOLATION != hr );
  194. }
  195. Exit:
  196. VariantClear( &varrgbstrFileName );
  197. VariantClear( &varrgbstrFileId );
  198. VariantClear( &varrglongStatus );
  199. if( bstr ) SysFreeString( bstr );
  200. if( FAILED(hr) )
  201. _tprintf( TEXT("DoFileStatus failed: %08x\n"), hr );
  202. }
  203. void
  204. DoFiles( TrkInfoScope scope, TCHAR *ptszUncPath )
  205. {
  206. HRESULT hr = E_FAIL;
  207. // BUGBUG: we should have a try/catch(_com_error) here, but to do that
  208. // we need to set USE_NATIVE_EH in the sources file, and we can't do
  209. // that yet because the itrkadmn dll is still using __try.
  210. _bstr_t bstrPath( ptszUncPath );
  211. ITrkForceOwnershipPtr pForceOwn( TEXT("LinkTrack.TrkForceOwnership.1") );
  212. __try
  213. {
  214. hr = pForceOwn->Files( bstrPath, scope );
  215. if( FAILED(hr) ) TrkRaiseException( hr );
  216. }
  217. __except( BreakOnDebuggableException() )
  218. {
  219. hr = GetExceptionCode();
  220. TrkAssert( STATUS_ACCESS_VIOLATION != hr );
  221. }
  222. Exit:
  223. if( FAILED(hr) )
  224. _tprintf( TEXT("DoFiles failed: %08x\n"), hr );
  225. }
  226. struct tagStartOle
  227. {
  228. tagStartOle() { CoInitialize( NULL ); }
  229. ~tagStartOle() { CoUninitialize(); }
  230. } StartOle;
  231. EXTERN_C void __cdecl _tmain( ULONG cArgs, TCHAR *rgtszArgs[] )
  232. {
  233. TrkInfoScope scope;
  234. TrkDebugCreate( TRK_DBG_FLAGS_WRITE_TO_DBG | TRK_DBG_FLAGS_WRITE_TO_STDOUT, "TForceOwn" );
  235. if( 3 > cArgs
  236. ||
  237. ( rgtszArgs[1][0] != TEXT('-')
  238. &&
  239. rgtszArgs[1][0] != TEXT('/')
  240. )
  241. )
  242. {
  243. Usage();
  244. goto Exit;
  245. }
  246. _tcsupr( rgtszArgs[1] );
  247. switch( rgtszArgs[ 1 ][ 1 ] )
  248. {
  249. case TEXT('V'):
  250. switch( rgtszArgs[1][2] )
  251. {
  252. case TEXT('V'):
  253. scope = TRKINFOSCOPE_VOLUME;
  254. break;
  255. case TEXT('M'):
  256. scope = TRKINFOSCOPE_MACHINE;
  257. break;
  258. default:
  259. _tprintf( TEXT("Unsupported option: %s\n"), rgtszArgs[1] );
  260. goto Exit;
  261. }
  262. if( TEXT('S') == rgtszArgs[1][3] )
  263. DoVolumeStatus( scope, rgtszArgs[2] );
  264. else
  265. DoVolumes( scope, rgtszArgs[2] );
  266. break;
  267. case TEXT('F'):
  268. switch( rgtszArgs[1][2] )
  269. {
  270. case TEXT('F'):
  271. scope = TRKINFOSCOPE_ONE_FILE;
  272. break;
  273. case TEXT('V'):
  274. scope = TRKINFOSCOPE_VOLUME;
  275. break;
  276. case TEXT('M'):
  277. scope = TRKINFOSCOPE_MACHINE;
  278. break;
  279. default:
  280. _tprintf( TEXT("Unsupported option: %s\n"), rgtszArgs[1] );
  281. goto Exit;
  282. }
  283. if( TEXT('S') == rgtszArgs[1][3] )
  284. DoFileStatus( scope, rgtszArgs[2] );
  285. else
  286. DoFiles( scope, rgtszArgs[2] );
  287. break;
  288. default:
  289. _tprintf( TEXT("Unsupported option: %s\n"), rgtszArgs[1] );
  290. break;
  291. }
  292. Exit:
  293. return;
  294. }