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.

401 lines
8.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: util.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "precomp.h"
  11. #include "shlguid.h"
  12. // FileExists function
  13. #define _dwGFAFail 0xFFFFFFFF
  14. #define _nMaxLines 1000
  15. // Reg functions
  16. #define nMAX_REGROOT 7
  17. #define nREGROOT_SZ 24
  18. BOOL DirectoryExists( LPWSTR szDir )
  19. {
  20. DWORD dwAttr = GetFileAttributes( szDir );
  21. if( _dwGFAFail == dwAttr )
  22. return FALSE;
  23. return (BOOL)( dwAttr & FILE_ATTRIBUTE_DIRECTORY );
  24. }
  25. BOOL FileExists( LPWSTR szFile )
  26. {
  27. DWORD dwAttr = GetFileAttributes( szFile );
  28. if( _dwGFAFail == dwAttr )
  29. return FALSE;
  30. return (BOOL)( !(dwAttr & FILE_ATTRIBUTE_DIRECTORY) );
  31. }
  32. VOID StripPath( LPWSTR szFullPath, LPWSTR szReturnPath, ULONG PathSize )
  33. {
  34. LPWSTR lpc = GetFileName( szFullPath );
  35. if( !lpc )
  36. return;
  37. StringCchCopy(szReturnPath,PathSize,lpc);
  38. }
  39. VOID StripFile( LPWSTR szFullPath )
  40. {
  41. LPWSTR lpc = GetFileName( szFullPath );
  42. if( !lpc )
  43. return;
  44. *lpc = cNIL;
  45. }
  46. LPWSTR GetFileName( LPWSTR lpszFullPath )
  47. {
  48. LPWSTR lpszFileName;
  49. if( !lpszFullPath)
  50. return lpNIL;
  51. for (lpszFileName = lpszFullPath; *lpszFullPath; lpszFullPath++)
  52. {
  53. if (*lpszFullPath == cBACKSLASH)
  54. lpszFileName = lpszFullPath + 1;
  55. }
  56. return lpszFileName;
  57. }
  58. LPWSTR GetFileNameW( LPWSTR wszFullPath )
  59. {
  60. LPWSTR lpwc;
  61. if( !wszFullPath || wszFullPath[0] == 0)
  62. return lpNIL;
  63. lpwc = wszFullPath + CchWsz(wszFullPath) - 1;
  64. while( *lpwc != (WCHAR)cBACKSLASH && lpwc != wszFullPath )
  65. lpwc--;
  66. if( lpwc == wszFullPath )
  67. return wszFullPath;
  68. return ++lpwc;
  69. }
  70. BOOL bNoTrailingSlash( LPWSTR szPath )
  71. {
  72. LPWSTR lpc;
  73. WCHAR cLast=L'\0';
  74. for( lpc = szPath; *lpc; cLast = *lpc, lpc++ )
  75. ;
  76. return ( cLast != cBACKSLASH );
  77. }
  78. BOOL GetUniqueName( LPWSTR szPath, ULONG PathSize, LPWSTR szBase, BOOL fFile )
  79. {
  80. INT nTry;
  81. LPWSTR lpFileName;
  82. WCHAR szFormat[cbMAX_SZ];
  83. ULONG NewPathLength;
  84. if( bNoTrailingSlash(szPath) ) {
  85. StringCchCat(szPath, PathSize,szBACKSLASH );
  86. }
  87. NewPathLength = lstrlen(szPath);
  88. lpFileName = szPath + NewPathLength;
  89. StringCchCat(szPath, PathSize, szBase);
  90. StringCbCopy(szFormat, sizeof(szFormat), g_DuplicateFileTemplate );
  91. nTry = 0;
  92. while( 0xffffffff != GetFileAttributes(szPath) ) {
  93. StringCchPrintf(lpFileName, PathSize-NewPathLength, szFormat, ++nTry, szBase );
  94. }
  95. return TRUE;
  96. }
  97. DWORD GetDirectorySize( LPWSTR szFolder )
  98. {
  99. BOOL bRet;
  100. HANDLE hFind;
  101. DWORD dwSize = 0L;
  102. WCHAR szBaseDir[MAX_PATH];
  103. WCHAR szSpec[MAX_PATH*2];
  104. WIN32_FIND_DATA findData;
  105. //
  106. // get base directory ending with backslash
  107. //
  108. StringCbCopy(szBaseDir,sizeof(szBaseDir), szFolder );
  109. if ( bNoTrailingSlash(szBaseDir) ) {
  110. StringCbCat(szBaseDir, sizeof(szBaseDir), szBACKSLASH );
  111. }
  112. //
  113. // form search string
  114. //
  115. StringCbCopy(szSpec, sizeof(szSpec), szBaseDir );
  116. StringCbCat(szSpec, sizeof(szSpec), L"*.*" );
  117. hFind = FindFirstFile(
  118. szSpec,
  119. &findData
  120. );
  121. bRet = ( hFind != INVALID_HANDLE_VALUE );
  122. while( bRet )
  123. {
  124. WCHAR szObj[MAX_PATH*2];
  125. StringCbCopy(szObj, sizeof(szObj), szBaseDir );
  126. StringCbCat(szObj, sizeof(szObj), findData.cFileName );
  127. if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  128. {
  129. // weed out "." and ".."
  130. if (0 != lstrcmp(findData.cFileName, szPERIOD) && 0 != lstrcmp(findData.cFileName, szPREVDIR))
  131. dwSize += GetDirectorySize( szObj );
  132. }
  133. else
  134. {
  135. HANDLE hFile = CreateFile(
  136. szObj,
  137. GENERIC_READ,
  138. FILE_SHARE_READ,
  139. NULL,
  140. OPEN_EXISTING,
  141. FILE_ATTRIBUTE_NORMAL,
  142. NULL
  143. );
  144. if( INVALID_HANDLE_VALUE != hFile )
  145. {
  146. //REVIEW: won't work for REALLY large files
  147. dwSize += GetFileSize( hFile, NULL );
  148. CloseHandle( hFile );
  149. }
  150. }
  151. bRet = FindNextFile( hFind, &findData );
  152. }
  153. if (hFind != INVALID_HANDLE_VALUE)
  154. {
  155. FindClose(hFind);
  156. }
  157. return dwSize;
  158. }
  159. LPWSTR SzToWsz( LPCSTR lpsz )
  160. {
  161. INT nSzLen = lstrlenA(lpsz);
  162. LPWSTR lpwsz = (LPWSTR) MemAlloc((nSzLen+1)*sizeof(WCHAR));
  163. if (lpwsz){
  164. MultiByteToWideChar(
  165. CP_ACP,
  166. 0L,
  167. lpsz,
  168. nSzLen,
  169. lpwsz,
  170. nSzLen+1
  171. );
  172. }
  173. return lpwsz;
  174. }
  175. LPSTR WszToSz( LPCWSTR lpwsz )
  176. {
  177. int err;
  178. int Length;
  179. LPSTR lpsz;
  180. Length = WideCharToMultiByte(CP_ACP,
  181. 0L,
  182. lpwsz,
  183. -1,
  184. 0,
  185. 0,
  186. NULL,
  187. NULL
  188. );
  189. lpsz = (LPSTR) MemAlloc(1+Length);
  190. if (!lpsz)
  191. {
  192. return 0;
  193. }
  194. if ( !WideCharToMultiByte(CP_ACP,
  195. 0L,
  196. lpwsz,
  197. Length,
  198. lpsz,
  199. 1+Length,
  200. NULL,
  201. NULL
  202. ))
  203. {
  204. MemFree( lpsz );
  205. return 0;
  206. }
  207. return lpsz;
  208. }
  209. INT SzLenW( LPCWSTR lpwsz )
  210. {
  211. WCHAR *pwc = (WCHAR *)lpwsz;
  212. if( !lpwsz )
  213. return 0;
  214. while( *pwc++ != 0 )
  215. ;
  216. return (INT)(--pwc - lpwsz);
  217. }
  218. LPWSTR SzCpyW( LPWSTR lpsz1, LPCWSTR lpsz2 )
  219. {
  220. LPWSTR lpwsz = lpsz1;
  221. while( *lpsz2 )
  222. *lpsz1++ = *lpsz2++;
  223. *lpsz1 = 0;
  224. return lpwsz;
  225. }
  226. INT SzCmpN( LPCSTR lpsz1, LPCSTR lpsz2, INT nLen )
  227. {
  228. while( *lpsz1 && *lpsz2 && *lpsz1 == *lpsz2 && --nLen > 0 )
  229. {
  230. lpsz1++;
  231. lpsz2++;
  232. }
  233. return ( *lpsz1 - *lpsz2 );
  234. }
  235. BOOL IsRoomForFile( __int64 dwFileSize, LPWSTR szPath )
  236. {
  237. BOOL fRet;
  238. ULARGE_INTEGER MyDiskFreeBytes;
  239. ULARGE_INTEGER DiskFreeBytes;
  240. ULARGE_INTEGER DiskTotalBytes;
  241. fRet = GetDiskFreeSpaceEx(
  242. szPath,
  243. &MyDiskFreeBytes,
  244. &DiskTotalBytes,
  245. &DiskFreeBytes
  246. );
  247. if( fRet )
  248. {
  249. fRet = ( MyDiskFreeBytes.QuadPart >= (ULONGLONG) dwFileSize );
  250. }
  251. return fRet;
  252. }
  253. __int64 _Get100nsIntervalsFrom1601To1970( VOID )
  254. {
  255. __int64 ilrg100nsPerSec = 10000000; // 100-ns intervals per second (10^7)
  256. __int64 ilrg100nsPerMin = 60*ilrg100nsPerSec;
  257. __int64 ilrgDays = 369*365 + 89; // 369 years (89 are leap years).
  258. __int64 ilrgMin = ilrgDays*24*60;
  259. __int64 ilrgRet;
  260. ilrgRet = ilrgMin * ilrg100nsPerMin;
  261. return ilrgRet;
  262. }
  263. BOOL FileTimeToUnixTime( IN LPFILETIME lpFileTime,
  264. OUT LPDWORD pdwUnixTime )
  265. {
  266. LARGE_INTEGER lrgTime
  267. = { lpFileTime->dwLowDateTime, lpFileTime->dwHighDateTime };
  268. __int64 ilrgFileTime = *((__int64*)lpFileTime);
  269. __int64 ilrgIntervalsTil1970 = _Get100nsIntervalsFrom1601To1970();
  270. __int64 ilrgIntsSince1970;
  271. __int64 ilrgSecSince1970;
  272. __int64 ilrg100nsPerSec = 10000000;
  273. __int64 ilrgRem;
  274. // Get the intervals since 1970
  275. ilrgIntsSince1970 = ilrgFileTime - ilrgIntervalsTil1970;
  276. // Convert to seconds since 1970
  277. ilrgSecSince1970 = ilrgIntsSince1970/ilrg100nsPerSec;
  278. if (ilrgSecSince1970 >= 0xffffffff)
  279. {
  280. return FALSE;
  281. }
  282. *pdwUnixTime = (DWORD)ilrgSecSince1970;
  283. return TRUE;
  284. }
  285. BOOL UnixTimeToFileTime( DWORD dwUnixTime, LPFILETIME lpFileTime )
  286. {
  287. __int64 ilrg100nsPerSec = 10000000; // 100-ns intervals/second (10^7)
  288. __int64 ilrgIntervalsSince1601;
  289. __int64 ilrgIntervalsSince1970;
  290. __int64 ilrgIntervalsTil1970 = _Get100nsIntervalsFrom1601To1970();
  291. // Get the intervals since 1970
  292. ilrgIntervalsSince1970 = dwUnixTime * ilrg100nsPerSec;
  293. // Get the intervals since 1601
  294. ilrgIntervalsSince1601 = ilrgIntervalsTil1970 + ilrgIntervalsSince1970;
  295. *lpFileTime = *((FILETIME*)&ilrgIntervalsSince1601);
  296. return TRUE;
  297. }