Source code of Windows XP (NT5)
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.

327 lines
8.3 KiB

  1. /****************************************************************************
  2. Copyright (c) Microsoft Corporation 1998
  3. All rights reserved
  4. ***************************************************************************/
  5. #include "pch.h"
  6. DEFINE_MODULE("RIPREP")
  7. #define LOG_OUTPUT_BUFFER_SIZE 256
  8. //
  9. // CreateDirectoryPath( )
  10. //
  11. // Creates the directory tree.
  12. //
  13. HRESULT
  14. CreateDirectoryPath(
  15. LPWSTR DirectoryPath )
  16. {
  17. PWCHAR p;
  18. BOOL f;
  19. DWORD attributes;
  20. HRESULT hr = S_OK;
  21. //
  22. // Find the \ that indicates the root directory. There should be at least
  23. // one \, but if there isn't, we just fall through.
  24. //
  25. // skip \\server\reminst\ part
  26. p = wcschr( DirectoryPath, L'\\' );
  27. Assert(p);
  28. p = wcschr( p + 1, L'\\' );
  29. Assert(p);
  30. p = wcschr( p + 1, L'\\' );
  31. Assert(p);
  32. p = wcschr( p + 1, L'\\' );
  33. Assert(p);
  34. p = wcschr( p + 1, L'\\' );
  35. if ( p != NULL ) {
  36. //
  37. // Find the \ that indicates the end of the first level directory. It's
  38. // probable that there won't be another \, in which case we just fall
  39. // through to creating the entire path.
  40. //
  41. p = wcschr( p + 1, L'\\' );
  42. while ( p != NULL ) {
  43. //
  44. // Terminate the directory path at the current level.
  45. //
  46. *p = 0;
  47. //
  48. // Create a directory at the current level.
  49. //
  50. attributes = GetFileAttributes( DirectoryPath );
  51. if ( 0xFFFFffff == attributes ) {
  52. DebugMsg( "Creating %s\n", DirectoryPath );
  53. f = CreateDirectory( DirectoryPath, NULL );
  54. if ( !f ) {
  55. hr = THR( HRESULT_FROM_WIN32( GetLastError( ) ) );
  56. goto Error;
  57. }
  58. } else if ( (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0 ) {
  59. hr = THR(E_FAIL);
  60. goto Error;
  61. }
  62. //
  63. // Restore the \ and find the next one.
  64. //
  65. *p = L'\\';
  66. p = wcschr( p + 1, L'\\' );
  67. }
  68. }
  69. //
  70. // Create the target directory.
  71. //
  72. attributes = GetFileAttributes( DirectoryPath );
  73. if ( 0xFFFFffff == attributes ) {
  74. f = CreateDirectory( DirectoryPath, NULL );
  75. if ( !f ) {
  76. hr = THR( HRESULT_FROM_WIN32( GetLastError( ) ) );
  77. }
  78. }
  79. Error:
  80. return hr;
  81. }
  82. //
  83. // LogOpen( )
  84. //
  85. // This function:
  86. // - initializes the log critical section
  87. // - enters the log critical section assuring only one thread is
  88. // writing to the log at a time
  89. // - creates the directory tree to the log file (if needed)
  90. // - initializes the log file by:
  91. // - creating a new log file if one doesn't exist.
  92. // - opens an existing log file (for append)
  93. // - appends a time/date stamp that the log was (re)openned.
  94. //
  95. // Use LogClose() to exit the log critical section.
  96. //
  97. // If there is a failure inside this function, the log critical
  98. // section will be released before returning.
  99. //
  100. // Returns: S_OK - log critical section held and log open successfully
  101. // Otherwize HRESULT error code.
  102. //
  103. HRESULT
  104. LogOpen( )
  105. {
  106. TCHAR szFilePath[ MAX_PATH ];
  107. CHAR szBuffer[ LOG_OUTPUT_BUFFER_SIZE ];
  108. DWORD dwWritten;
  109. HANDLE hTemp;
  110. HRESULT hr;
  111. SYSTEMTIME SystemTime;
  112. BOOL CloseLog = FALSE;
  113. if ( !g_pLogCritSect ) {
  114. PCRITICAL_SECTION pNewCritSect =
  115. (PCRITICAL_SECTION) LocalAlloc( LPTR, sizeof(CRITICAL_SECTION) );
  116. if ( !pNewCritSect ) {
  117. DebugMsg( "Out of Memory. Logging disabled.\n " );
  118. hr = E_OUTOFMEMORY;
  119. goto Cleanup;
  120. }
  121. InitializeCriticalSection( pNewCritSect );
  122. // Make sure we only have one log critical section
  123. InterlockedCompareExchangePointer( (PVOID *)&g_pLogCritSect, pNewCritSect, 0 );
  124. if ( g_pLogCritSect != pNewCritSect ) {
  125. DebugMsg( "Another thread already created the CS. Deleting this one.\n ");
  126. DeleteCriticalSection( pNewCritSect );
  127. LocalFree( pNewCritSect );
  128. }
  129. }
  130. Assert( g_pLogCritSect );
  131. EnterCriticalSection( g_pLogCritSect );
  132. // Make sure the log file is open
  133. if ( g_hLogFile == INVALID_HANDLE_VALUE ) {
  134. if (!*g_ServerName) {
  135. wsprintf(
  136. szFilePath,
  137. L"%s\\%s",
  138. g_WinntDirectory,
  139. L"riprep.log");
  140. CloseLog = TRUE;
  141. } else {
  142. // Place
  143. wsprintf( szFilePath,
  144. TEXT("\\\\%s\\REMINST\\Setup\\%s\\%s\\%s\\%s"),
  145. g_ServerName,
  146. g_Language,
  147. REMOTE_INSTALL_IMAGE_DIR_W,
  148. g_MirrorDir,
  149. g_Architecture );
  150. // Create the directory tree
  151. DebugMsg( "Creating log at %s\n", szFilePath );
  152. hr = CreateDirectoryPath( szFilePath );
  153. if (FAILED( hr )) goto Error;
  154. wcscat( szFilePath, L"\\riprep.log" );
  155. }
  156. g_hLogFile = CreateFile( szFilePath,
  157. GENERIC_WRITE | GENERIC_READ,
  158. FILE_SHARE_READ,
  159. NULL,
  160. OPEN_ALWAYS,
  161. NULL,
  162. NULL );
  163. if ( g_hLogFile == INVALID_HANDLE_VALUE ) {
  164. hr = THR( HRESULT_FROM_WIN32( GetLastError( ) ) );
  165. goto Error;
  166. }
  167. // Seek to the end
  168. SetFilePointer( g_hLogFile, 0, NULL, FILE_END );
  169. g_dwLogFileStartLow = GetFileSize( g_hLogFile, &g_dwLogFileStartHigh );
  170. // Write the time/date the log was (re)openned.
  171. GetLocalTime( &SystemTime );
  172. wsprintfA( szBuffer,
  173. "*\r\n* %02u/%02u/%04u %02u:%02u:%02u\r\n*\r\n",
  174. SystemTime.wMonth,
  175. SystemTime.wDay,
  176. SystemTime.wYear,
  177. SystemTime.wHour,
  178. SystemTime.wMinute,
  179. SystemTime.wSecond );
  180. if ( !WriteFile( g_hLogFile, szBuffer, lstrlenA(szBuffer), &dwWritten, NULL ) ) {
  181. hr = THR( HRESULT_FROM_WIN32( GetLastError( ) ) );
  182. goto Error;
  183. }
  184. }
  185. hr = S_OK;
  186. Cleanup:
  187. if (CloseLog) {
  188. CloseHandle( g_hLogFile );
  189. g_hLogFile = INVALID_HANDLE_VALUE;
  190. }
  191. return hr;
  192. Error:
  193. DebugMsg( "LogOpen: Failed hr = 0x%08x\n", hr );
  194. if ( g_hLogFile != INVALID_HANDLE_VALUE ) {
  195. CloseHandle( g_hLogFile );
  196. g_hLogFile = INVALID_HANDLE_VALUE;
  197. }
  198. LeaveCriticalSection( g_pLogCritSect );
  199. goto Cleanup;
  200. }
  201. //
  202. // LogClose( )
  203. //
  204. // This actually just leaves the log critical section.
  205. //
  206. HRESULT
  207. LogClose( )
  208. {
  209. Assert( g_pLogCritSect );
  210. LeaveCriticalSection( g_pLogCritSect );
  211. return S_OK;
  212. }
  213. //
  214. // LogMsg()
  215. //
  216. void
  217. LogMsg(
  218. LPCSTR pszFormat,
  219. ... )
  220. {
  221. va_list valist;
  222. CHAR szBuf[ LOG_OUTPUT_BUFFER_SIZE ];
  223. DWORD dwWritten;
  224. #ifdef UNICODE
  225. WCHAR szFormat[ LOG_OUTPUT_BUFFER_SIZE ];
  226. WCHAR szTmpBuf[ LOG_OUTPUT_BUFFER_SIZE ];
  227. mbstowcs( szFormat, pszFormat, lstrlenA( pszFormat ) + 1 );
  228. va_start( valist, pszFormat );
  229. wvsprintf( szTmpBuf, szFormat, valist);
  230. va_end( valist );
  231. wcstombs( szBuf, szTmpBuf, wcslen( szTmpBuf ) + 1 );
  232. #else
  233. va_start( valist, pszFormat );
  234. wvsprintf( szBuf, pszFormat, valist);
  235. va_end( valist );
  236. #endif // UNICODE
  237. if ( LogOpen( ) ) {
  238. return;
  239. }
  240. WriteFile( g_hLogFile, szBuf, lstrlenA(szBuf), &dwWritten, NULL );
  241. LogClose( );
  242. }
  243. //
  244. // LogMsg()
  245. //
  246. void
  247. LogMsg(
  248. LPCWSTR pszFormat,
  249. ... )
  250. {
  251. va_list valist;
  252. CHAR szBuf[ LOG_OUTPUT_BUFFER_SIZE ];
  253. DWORD dwWritten;
  254. #ifdef UNICODE
  255. WCHAR szTmpBuf[ LOG_OUTPUT_BUFFER_SIZE ];
  256. va_start( valist, pszFormat );
  257. wvsprintf( szTmpBuf, pszFormat, valist);
  258. va_end( valist );
  259. wcstombs( szBuf, szTmpBuf, wcslen( szTmpBuf ) + 1 );
  260. #else
  261. CHAR szFormat[ LOG_OUTPUT_BUFFER_SIZE ];
  262. wcstombs( szFormat, pszFormat, wcslen( pszFormat ) + 1 );
  263. va_start( valist, pszFormat );
  264. wvsprintf( szBuf, szFormat, valist);
  265. va_end( valist );
  266. #endif // UNICODE
  267. if ( LogOpen( ) ) {
  268. return;
  269. }
  270. WriteFile( g_hLogFile, szBuf, lstrlenA(szBuf), &dwWritten, NULL );
  271. LogClose( );
  272. }