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.

225 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. thunks.cpp
  5. Abstract:
  6. This file implements the API thunks for the BITS server extensions
  7. --*/
  8. #include "precomp.h"
  9. // API thunks
  10. UINT64 BITSGetFileSize(
  11. HANDLE Handle )
  12. {
  13. LARGE_INTEGER FileSize;
  14. if (!GetFileSizeEx( Handle, &FileSize ) )
  15. throw ServerException( HRESULT_FROM_WIN32( GetLastError() ) );
  16. return (UINT64)FileSize.QuadPart;
  17. }
  18. UINT64 BITSSetFilePointer(
  19. HANDLE Handle,
  20. INT64 Distance,
  21. DWORD MoveMethod )
  22. {
  23. LARGE_INTEGER DistanceToMove;
  24. DistanceToMove.QuadPart = (LONGLONG)Distance;
  25. LARGE_INTEGER NewFilePointer;
  26. BOOL Result =
  27. SetFilePointerEx(
  28. Handle,
  29. DistanceToMove,
  30. &NewFilePointer,
  31. MoveMethod );
  32. if ( !Result )
  33. {
  34. throw ServerException( HRESULT_FROM_WIN32( GetLastError() ) );
  35. }
  36. return (UINT64)NewFilePointer.QuadPart;
  37. }
  38. DWORD
  39. BITSWriteFile(
  40. HANDLE Handle,
  41. LPCVOID Buffer,
  42. DWORD NumberOfBytesToWrite)
  43. {
  44. DWORD BytesWritten;
  45. BOOL Result =
  46. WriteFile(
  47. Handle,
  48. Buffer,
  49. NumberOfBytesToWrite,
  50. &BytesWritten,
  51. NULL );
  52. if ( !Result )
  53. throw ServerException( HRESULT_FROM_WIN32( GetLastError() ) );
  54. return BytesWritten;
  55. }
  56. void
  57. BITSCreateDirectory(
  58. LPCTSTR DirectoryName
  59. )
  60. {
  61. BOOL Result =
  62. CreateDirectory( DirectoryName, NULL );
  63. if ( Result )
  64. return;
  65. DWORD Status = GetLastError();
  66. // ignore the error if the directory already exists
  67. if ( ERROR_ALREADY_EXISTS == Status )
  68. return;
  69. throw ServerException( HRESULT_FROM_WIN32( Status ) );
  70. }
  71. void
  72. BITSRenameFile(
  73. LPCTSTR ExistingName,
  74. LPCTSTR NewName,
  75. bool AllowOverwrites )
  76. {
  77. BOOL Result =
  78. MoveFileEx( ExistingName, NewName,
  79. AllowOverwrites ? MOVEFILE_REPLACE_EXISTING : 0 );
  80. if ( !Result )
  81. throw ServerException( HRESULT_FROM_WIN32( GetLastError() ) );
  82. }
  83. void
  84. BITSDeleteFile(
  85. LPCTSTR FileName )
  86. {
  87. BOOL Result =
  88. DeleteFile( FileName );
  89. if ( Result )
  90. return;
  91. DWORD Status = GetLastError();
  92. if ( ERROR_FILE_NOT_FOUND == Status ||
  93. ERROR_PATH_NOT_FOUND == Status )
  94. return;
  95. throw ServerException( HRESULT_FROM_WIN32( GetLastError() ) );
  96. }
  97. GUID
  98. BITSCreateGuid()
  99. {
  100. GUID guid;
  101. HRESULT Hr = CoCreateGuid( &guid );
  102. if ( FAILED( Hr ) )
  103. throw ServerException( Hr );
  104. return guid;
  105. }
  106. GUID
  107. BITSGuidFromString( const char *String )
  108. {
  109. // 38 chars {c200e360-38c5-11ce-ae62-08002b2b79ef}
  110. if ( 38 != strlen( String ) )
  111. throw ServerException( E_INVALIDARG );
  112. WCHAR StringW[ 80 ];
  113. StringCchPrintfW(
  114. StringW,
  115. ARRAY_ELEMENTS( StringW ),
  116. L"%S",
  117. String );
  118. GUID Guid;
  119. HRESULT Hr =
  120. IIDFromString( StringW, &Guid );
  121. if ( FAILED( Hr ) )
  122. throw ServerException( Hr );
  123. return Guid;
  124. }
  125. StringHandle
  126. BITSStringFromGuid(
  127. GUID Guid )
  128. {
  129. WCHAR StringW[ 80 ];
  130. StringFromGUID2( Guid, StringW, ARRAY_ELEMENTS( StringW ) );
  131. StringHandle WorkString;
  132. char *WorkBuffer = WorkString.AllocBuffer( ARRAY_ELEMENTS( StringW ) );
  133. StringCchPrintfA(
  134. WorkBuffer,
  135. ARRAY_ELEMENTS( StringW ),
  136. "%S",
  137. StringW );
  138. WorkString.SetStringSize();
  139. return WorkString;
  140. }
  141. void
  142. BITSSetCurrentThreadToken(
  143. HANDLE hToken )
  144. {
  145. if ( !SetThreadToken( NULL, hToken ) )
  146. {
  147. for( unsigned int i = 0; i < 100; i ++ )
  148. {
  149. Sleep( 10 );
  150. if ( SetThreadToken( NULL, hToken ) )
  151. return;
  152. }
  153. ASSERT( 0 );
  154. TerminateProcess( NULL, GetLastError() );
  155. }
  156. }