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.

451 lines
12 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: storage.cxx
  7. //
  8. // Contents: Contains generic storage APIs
  9. //
  10. // History: 05-Oct-92 DrewB Created
  11. //
  12. //---------------------------------------------------------------
  13. #include <exphead.cxx>
  14. #pragma hdrstop
  15. #include <dfentry.hxx>
  16. #include <storagep.h>
  17. #include <logfile.hxx>
  18. #include <df32.hxx>
  19. #ifdef COORD
  20. #include <oledb.h>
  21. #endif //COORD
  22. #include <trace.hxx>
  23. #include <ole2sp.h>
  24. //+--------------------------------------------------------------
  25. //
  26. // Function: StgOpenStorage, public
  27. //
  28. // Synopsis: Instantiates a root storage from a file
  29. // by binding to the appropriate implementation
  30. // and starting things up
  31. //
  32. // Arguments: [pwcsName] - Name
  33. // [pstgPriority] - Priority mode reopen IStorage
  34. // [grfMode] - Permissions
  35. // [snbExclude] - Exclusions for priority reopen
  36. // [reserved]
  37. // [ppstgOpen] - Docfile return
  38. //
  39. // Returns: Appropriate status code
  40. //
  41. // Modifies: [ppstgOpen]
  42. //
  43. // History: 05-Oct-92 DrewB Created
  44. //
  45. //---------------------------------------------------------------
  46. STDAPI StgOpenStorage(OLECHAR const *pwcsName,
  47. IStorage *pstgPriority,
  48. DWORD grfMode,
  49. SNB snbExclude,
  50. LPSTGSECURITY reserved,
  51. IStorage **ppstgOpen)
  52. {
  53. return DfOpenDocfile(pwcsName, NULL, pstgPriority, grfMode,
  54. snbExclude, reserved, NULL, 0, ppstgOpen);
  55. }
  56. //+---------------------------------------------------------------------------
  57. //
  58. // Function: CheckSignature, private
  59. //
  60. // Synopsis: Checks the given memory against known signatures
  61. //
  62. // Arguments: [pb] - Pointer to memory to check
  63. //
  64. // Returns: S_OK - Current signature
  65. // S_FALSE - Beta 2 signature, but still successful
  66. // Appropriate status code
  67. //
  68. // History: 23-Jul-93 DrewB Created from header.cxx code
  69. //
  70. //----------------------------------------------------------------------------
  71. //Identifier for first bytes of Beta 1 Docfiles
  72. const BYTE SIGSTG_B1[] = {0xd0, 0xcf, 0x11, 0xe0, 0x0e, 0x11, 0xfc, 0x0d};
  73. const USHORT CBSIGSTG_B1 = sizeof(SIGSTG_B1);
  74. //Identifier for first bytes of Beta 2 Docfiles
  75. const BYTE SIGSTG_B2[] = {0x0e, 0x11, 0xfc, 0x0d, 0xd0, 0xcf, 0x11, 0xe0};
  76. const BYTE CBSIGSTG_B2 = sizeof(SIGSTG_B2);
  77. SCODE CheckSignature(BYTE *pb)
  78. {
  79. SCODE sc;
  80. olDebugOut((DEB_ITRACE, "In CheckSignature(%p)\n", pb));
  81. // Check for ship Docfile signature first
  82. if (memcmp(pb, SIGSTG, CBSIGSTG) == 0)
  83. sc = S_OK;
  84. // Check for Beta 2 Docfile signature
  85. else if (memcmp(pb, SIGSTG_B2, CBSIGSTG_B2) == 0)
  86. sc = S_FALSE;
  87. // Check for Beta 1 Docfile signature
  88. else if (memcmp(pb, SIGSTG_B1, CBSIGSTG_B1) == 0)
  89. sc = STG_E_OLDFORMAT;
  90. else
  91. sc = STG_E_INVALIDHEADER;
  92. olDebugOut((DEB_ITRACE, "Out CheckSignature => %lX\n", sc));
  93. return sc;
  94. }
  95. //+--------------------------------------------------------------
  96. //
  97. // Function: StgIsStorageFileHandle, private
  98. //
  99. // Synopsis: Determines whether a handle is open on a storage file.
  100. // Spun off from StgIsStorageFile. Internaly we use this
  101. //
  102. // Arguments: [hf] - Open File Handle (caller must seek it to 0)
  103. //
  104. // Returns: S_OK, S_FALSE or error codes
  105. //
  106. // History: 07-May-98 MikeHill Created
  107. // 05-June-98 BChapman Return Errors not just S_FALSE.
  108. // Add optional Overlapped pointer.
  109. //
  110. //---------------------------------------------------------------
  111. STDAPI StgIsStorageFileHandle( HANDLE hf, LPOVERLAPPED povlp )
  112. {
  113. DWORD cbRead;
  114. BYTE stgHeader[sizeof(SStorageFile)];
  115. SCODE sc;
  116. LONG status;
  117. OVERLAPPED ovlp;
  118. FillMemory( stgHeader, sizeof(SStorageFile), 0xDE );
  119. if (povlp == NULL)
  120. {
  121. ovlp.Offset = 0;
  122. ovlp.OffsetHigh = 0;
  123. ovlp.hEvent = NULL;
  124. }
  125. if( !ReadFile( hf,
  126. stgHeader,
  127. sizeof( stgHeader ),
  128. &cbRead,
  129. (povlp == NULL) ? &ovlp : povlp ) )
  130. {
  131. if( NULL != povlp )
  132. {
  133. status = GetLastError();
  134. if( ERROR_IO_PENDING == status)
  135. {
  136. status = ERROR_SUCCESS;
  137. if( !GetOverlappedResult( hf, povlp, &cbRead, TRUE ) )
  138. status = GetLastError();
  139. }
  140. if(ERROR_SUCCESS != status && ERROR_HANDLE_EOF != status)
  141. olChk( HRESULT_FROM_WIN32( status ) );
  142. }
  143. else
  144. olErr( EH_Err, S_FALSE );
  145. }
  146. // Don't worry about short reads. If the read is short then
  147. // the signature checks will fail.
  148. sc = CheckSignature( ((SStorageFile*)stgHeader)->abSig );
  149. if(S_OK == sc)
  150. goto EH_Err; // Done, return "Yes"
  151. olChk(sc);
  152. // It didn't error. sc != S_OK then it
  153. // Must be S_FALSE.
  154. olAssert(S_FALSE == sc);
  155. EH_Err:
  156. if( (STG_E_OLDFORMAT == sc) || (STG_E_INVALIDHEADER == sc) )
  157. sc = S_FALSE;
  158. return sc;
  159. }
  160. //+--------------------------------------------------------------
  161. //
  162. // Function: StgIsStorageFile, public
  163. //
  164. // Synopsis: Determines whether the named file is a storage or not
  165. //
  166. // Arguments: [pwcsName] - Filename
  167. //
  168. // Returns: S_OK, S_FALSE or error codes
  169. //
  170. // History: 05-Oct-92 DrewB Created
  171. //
  172. //---------------------------------------------------------------
  173. STDAPI StgIsStorageFile(OLECHAR const *pwcsName)
  174. {
  175. HANDLE hf;
  176. SCODE sc;
  177. olLog(("--------::In StgIsStorageFile(" OLEFMT ")\n", pwcsName));
  178. TRY
  179. {
  180. #ifndef OLEWIDECHAR
  181. if (FAILED(sc = ValidateNameA(pwcsName, _MAX_PATH)))
  182. #else
  183. if (FAILED(sc = ValidateNameW(pwcsName, _MAX_PATH)))
  184. #endif
  185. return ResultFromScode(sc);
  186. #if !defined(UNICODE)
  187. // Chicago - call ANSI CreateFile
  188. char szName[_MAX_PATH + 1];
  189. if (!WideCharToMultiByte(
  190. AreFileApisANSI() ? CP_ACP : CP_OEMCP,
  191. 0,
  192. pwcsName,
  193. -1,
  194. szName,
  195. _MAX_PATH + 1,
  196. NULL,
  197. NULL))
  198. return ResultFromScode(STG_E_INVALIDNAME);
  199. hf = CreateFileA (
  200. szName,
  201. GENERIC_READ,
  202. FILE_SHARE_READ | FILE_SHARE_WRITE,
  203. NULL,
  204. OPEN_EXISTING,
  205. FILE_ATTRIBUTE_NORMAL,
  206. NULL
  207. );
  208. #else
  209. hf = CreateFile (
  210. pwcsName,
  211. GENERIC_READ,
  212. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  213. NULL,
  214. OPEN_EXISTING,
  215. FILE_ATTRIBUTE_NORMAL,
  216. NULL
  217. );
  218. #endif // !defined(UNICODE)
  219. if (hf == INVALID_HANDLE_VALUE)
  220. return ResultFromScode(STG_SCODE(GetLastError()));
  221. sc = StgIsStorageFileHandle( hf, NULL );
  222. CloseHandle (hf);
  223. }
  224. CATCH(CException, e)
  225. {
  226. sc = e.GetErrorCode();
  227. }
  228. END_CATCH
  229. olLog(("--------::Out StgIsStorageFile(). ret == %lx\n", sc));
  230. return(ResultFromScode(sc));
  231. }
  232. //+--------------------------------------------------------------
  233. //
  234. // Function: StgIsStorageILockBytes, public
  235. //
  236. // Synopsis: Determines whether the ILockBytes is a storage or not
  237. //
  238. // Arguments: [plkbyt] - The ILockBytes
  239. //
  240. // Returns: S_OK, S_FALSE or error codes
  241. //
  242. // History: 05-Oct-92 DrewB Created
  243. //
  244. //---------------------------------------------------------------
  245. STDAPI StgIsStorageILockBytes(ILockBytes *plkbyt)
  246. {
  247. OLETRACEIN((API_StgIsStorageILockBytes, PARAMFMT("plkbyt= %p"), plkbyt));
  248. HRESULT hr;
  249. SCODE sc;
  250. SStorageFile stgfile;
  251. ULONG cbRead;
  252. ULARGE_INTEGER ulOffset;
  253. TRY
  254. {
  255. if (FAILED(sc = ValidateInterface(plkbyt, IID_ILockBytes)))
  256. {
  257. hr = ResultFromScode(sc);
  258. goto errRtn;
  259. }
  260. ULISet32(ulOffset, 0);
  261. hr = plkbyt->ReadAt(ulOffset, &stgfile, sizeof(stgfile), &cbRead);
  262. if (FAILED(GetScode(hr)))
  263. {
  264. goto errRtn;
  265. }
  266. }
  267. CATCH(CException, e)
  268. {
  269. hr = ResultFromScode(e.GetErrorCode());
  270. goto errRtn;
  271. }
  272. END_CATCH
  273. if (cbRead == sizeof(stgfile))
  274. {
  275. if ((memcmp((void *)stgfile.abSig, SIGSTG, CBSIGSTG) == 0) ||
  276. (memcmp((void *)stgfile.abSig, SIGSTG_B2, CBSIGSTG_B2) == 0))
  277. {
  278. hr = ResultFromScode(S_OK);
  279. goto errRtn;
  280. }
  281. }
  282. hr = ResultFromScode(S_FALSE);
  283. errRtn:
  284. OLETRACEOUT((API_StgIsStorageILockBytes, hr));
  285. return hr;
  286. }
  287. //+--------------------------------------------------------------
  288. //
  289. // Function: StgSetTimes
  290. //
  291. // Synopsis: Sets file time stamps
  292. //
  293. // Arguments: [lpszName] - Name
  294. // [pctime] - create time
  295. // [patime] - access time
  296. // [pmtime] - modify time
  297. //
  298. // Returns: Appropriate status code
  299. //
  300. // History: 05-Oct-92 AlexT Created
  301. //
  302. //---------------------------------------------------------------
  303. STDAPI StgSetTimes(OLECHAR const *lpszName,
  304. FILETIME const *pctime,
  305. FILETIME const *patime,
  306. FILETIME const *pmtime)
  307. {
  308. SCODE sc;
  309. HANDLE hFile;
  310. TRY
  311. {
  312. #ifndef OLEWIDECHAR
  313. sc = ValidateNameA(lpszName, _MAX_PATH);
  314. #else
  315. sc = ValidateNameW(lpszName, _MAX_PATH);
  316. #endif
  317. if (SUCCEEDED(sc))
  318. {
  319. #if !defined(UNICODE) && defined(OLEWIDECHAR)
  320. //Chicago - call ANSI CreateFile
  321. char szName[_MAX_PATH];
  322. if (!WideCharToMultiByte(
  323. AreFileApisANSI() ? CP_ACP : CP_OEMCP,
  324. 0,
  325. lpszName,
  326. -1,
  327. szName,
  328. _MAX_PATH,
  329. NULL,
  330. NULL))
  331. return ResultFromScode(STG_E_INVALIDNAME);
  332. hFile = CreateFileA(szName, GENERIC_WRITE,
  333. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  334. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  335. NULL);
  336. #else
  337. hFile = CreateFile(lpszName, GENERIC_WRITE,
  338. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  339. OPEN_EXISTING,
  340. FILE_ATTRIBUTE_NORMAL,
  341. NULL);
  342. #endif
  343. if (hFile == INVALID_HANDLE_VALUE)
  344. sc = LAST_STG_SCODE;
  345. else
  346. {
  347. if (!SetFileTime(hFile, (FILETIME *)pctime, (FILETIME *)patime,
  348. (FILETIME *)pmtime))
  349. sc = LAST_STG_SCODE;
  350. CloseHandle(hFile);
  351. }
  352. }
  353. }
  354. CATCH(CException, e)
  355. {
  356. sc = e.GetErrorCode();
  357. }
  358. END_CATCH
  359. return ResultFromScode(sc);
  360. }
  361. #if DBG==1
  362. extern "C" unsigned long filestInfoLevel; // File I/O layer of Docfile
  363. extern "C" unsigned long nffInfoLevel; // NTFS Flat File
  364. extern ULONG GetInfoLevel(CHAR *pszKey, ULONG *pulValue, CHAR *pszdefval);
  365. void
  366. StgDebugInit()
  367. {
  368. GetInfoLevel("filest", &filestInfoLevel, "0x0003");
  369. GetInfoLevel( "nff", &nffInfoLevel, "0x0003");
  370. }
  371. #endif // DBG==1
  372. //+---------------------------------------------------------------------------
  373. //
  374. // Function: DllRegisterServer, public
  375. //
  376. // Returns: Appropriate status code
  377. //
  378. // History: 15-jan-98 BChpaman Created
  379. //
  380. //----------------------------------------------------------------------------
  381. STDAPI Storage32DllRegisterServer(void)
  382. {
  383. HRESULT hrAccum=S_OK;
  384. return hrAccum;
  385. }