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.

481 lines
24 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_FileUpload.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_FileUpload class
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 04/27/99
  9. - Created
  10. ********************************************************************/
  11. #include "pchealth.h"
  12. #include "PCH_FileUpload.h"
  13. #include "mpc_utils.h"
  14. // MAX_FILE_SIZE is the limit set on the maximum file size of text files that will be collected.
  15. // If the Filesize is larger than 262144 then the data property is not populated.
  16. // This Number is arrived at by the PM.
  17. #define MAX_FILE_SIZE 262144
  18. #define READONLY "READONLY "
  19. #define HIDDEN "HIDDEN "
  20. #define SYSTEM "SYSTEM "
  21. #define DIRECTORY "DIRECTORY "
  22. #define ARCHIVE "ARCHIVE "
  23. #define NORMAL "NORMAL "
  24. #define TEMPORARY "TEMPORARY "
  25. #define REPARSEPOINT "REPARSEPOINT "
  26. #define SPARSEFILE "SPARSEFILE "
  27. #define COMPRESSED "COMPRESSED "
  28. #define OFFLINE "OFFLINE "
  29. #define ENCRYPTED "ENCRYPTED "
  30. /////////////////////////////////////////////////////////////////////////////
  31. // tracing stuff
  32. #ifdef THIS_FILE
  33. #undef THIS_FILE
  34. #endif
  35. static char __szTraceSourceFile[] = __FILE__;
  36. #define THIS_FILE __szTraceSourceFile
  37. #define TRACE_ID DCID_FILEUPLOAD
  38. CPCH_FileUpload MyPCH_FileUploadSet (PROVIDER_NAME_PCH_FILEUPLOAD, PCH_NAMESPACE) ;
  39. // Property names
  40. //===============
  41. const static WCHAR* pTimeStamp = L"TimeStamp" ;
  42. const static WCHAR* pChange = L"Change" ;
  43. const static WCHAR* pData = L"Data" ;
  44. const static WCHAR* pDateAccessed = L"DateAccessed" ;
  45. const static WCHAR* pDateCreated = L"DateCreated" ;
  46. const static WCHAR* pDateModified = L"DateModified" ;
  47. const static WCHAR* pFileAttributes = L"FileAttributes" ;
  48. const static WCHAR* pPath = L"Path" ;
  49. const static WCHAR* pSize = L"Size" ;
  50. /*****************************************************************************
  51. *
  52. * FUNCTION : CPCH_FileUpload::ExecQuery
  53. *
  54. * DESCRIPTION : You are passed a method context to use in the creation of
  55. * instances that satisfy the query, and a CFrameworkQuery
  56. * which describes the query. Create and populate all
  57. * instances which satisfy the query. WinMgmt will post -
  58. * filter the query for you, so you may return more instances
  59. * or more properties than are requested and WinMgmt
  60. * will filter out any that do not apply.
  61. *
  62. *
  63. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  64. * A query object describing the query to satisfy.
  65. * A long that contains the flags described in
  66. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  67. * flags are handled by (and filtered out by) WinMgmt:
  68. * WBEM_FLAG_FORWARD_ONLY
  69. * WBEM_FLAG_BIDIRECTIONAL
  70. * WBEM_FLAG_ENSURE_LOCATABLE
  71. *
  72. * RETURNS : WBEM_E_PROVIDER_NOT_CAPABLE if not supported for this class
  73. * WBEM_E_FAILED if the query failed
  74. * WBEM_S_NO_ERROR if query was successful
  75. *
  76. * COMMENTS : TO DO: Most providers will not need to implement this method. If you don't, WinMgmt
  77. * will call your enumerate function to get all the instances and perform the
  78. * filtering for you. Unless you expect SIGNIFICANT savings from implementing
  79. * queries, you should remove this method.
  80. *
  81. *****************************************************************************/
  82. HRESULT CPCH_FileUpload::ExecQuery (MethodContext *pMethodContext, CFrameworkQuery& Query, long lFlags)
  83. {
  84. TraceFunctEnter("CPCH_FileUpLoad::ExecQuery");
  85. HRESULT hRes;
  86. HANDLE hFile;
  87. CHStringArray chstrFiles;
  88. TCHAR tchFileName[MAX_PATH];
  89. TCHAR tchRootDir[MAX_PATH];
  90. TCHAR tchWindowsDir[MAX_PATH];
  91. WIN32_FIND_DATA FindFileData;
  92. // CInstance *pPCHFileUploadInstance;
  93. SYSTEMTIME stUTCTime;
  94. BOOL fTimeStamp;
  95. BOOL fChange;
  96. BOOL fData;
  97. BOOL fDateAccessed;
  98. BOOL fDateCreated;
  99. BOOL fDateModified;
  100. BOOL fFileAttributes;
  101. BOOL fSize;
  102. BOOL fCommit;
  103. BOOL fFileRead = FALSE;
  104. BOOL fFileFound = FALSE;
  105. BOOL fNoData = TRUE;
  106. CComVariant varAttributes;
  107. CComVariant varSize;
  108. CComVariant varRequestedFileName;
  109. CComVariant varSnapshot = "SnapShot";
  110. CComVariant varData;
  111. ULARGE_INTEGER ulnFileSize;
  112. WBEMINT64 wbemulnFileSize;
  113. char *pbBuffer;
  114. WCHAR *pwcBuffer;
  115. DWORD dwDesiredAccess = GENERIC_READ;
  116. DWORD dwNumBytesRead;
  117. DWORD dwAttributes;
  118. BSTR bstrData;
  119. CComBSTR bstrFileName;
  120. CComBSTR bstrFileNameWithPath;
  121. CComBSTR bstrKey = L"Path";
  122. int nBufferSize;
  123. int nFilesRequested = 0;
  124. int nIndex;
  125. int nFileSize;
  126. int nRetChars;
  127. TCHAR tchAttributes[MAX_PATH];
  128. //
  129. std::tstring szEnv;
  130. // End Declarations
  131. GetSystemTime(&stUTCTime);
  132. hRes = WBEM_S_NO_ERROR;
  133. hRes = Query.GetValuesForProp(bstrKey, chstrFiles);
  134. if(FAILED(hRes))
  135. {
  136. goto END;
  137. }
  138. else
  139. {
  140. fTimeStamp = Query.IsPropertyRequired(pTimeStamp);
  141. fChange = Query.IsPropertyRequired(pChange);
  142. fData = Query.IsPropertyRequired(pData);
  143. fDateAccessed = Query.IsPropertyRequired(pDateAccessed);
  144. fDateCreated = Query.IsPropertyRequired(pDateCreated);
  145. fDateModified = Query.IsPropertyRequired(pDateModified);
  146. fFileAttributes = Query.IsPropertyRequired(pFileAttributes);
  147. fSize = Query.IsPropertyRequired(pSize);
  148. nFilesRequested = chstrFiles.GetSize();
  149. for (nIndex = 0; nIndex < nFilesRequested; nIndex++)
  150. {
  151. USES_CONVERSION;
  152. varRequestedFileName = chstrFiles[nIndex];
  153. bstrFileName = chstrFiles[nIndex];
  154. szEnv = W2T(chstrFiles[nIndex]);
  155. hRes = MPC::SubstituteEnvVariables(szEnv);
  156. if(SUCCEEDED(hRes))
  157. {
  158. // Found the file
  159. _tcscpy(tchFileName, szEnv.c_str());
  160. hFile = FindFirstFile(tchFileName, &FindFileData);
  161. if(hFile != INVALID_HANDLE_VALUE)
  162. {
  163. // Close the File Handle
  164. FindClose(hFile);
  165. // Create the Fileupload Instance
  166. // Create an instance of PCH_Startup
  167. CInstancePtr pPCHFileUploadInstance(CreateNewInstance(pMethodContext), false);
  168. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  169. // PATH //
  170. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  171. hRes = pPCHFileUploadInstance->SetVariant(pPath, varRequestedFileName);
  172. if(SUCCEEDED(hRes))
  173. {
  174. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  175. // SIZE //
  176. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. ulnFileSize.LowPart = FindFileData.nFileSizeLow;
  178. ulnFileSize.HighPart = FindFileData.nFileSizeHigh;
  179. if(ulnFileSize.HighPart > 0)
  180. {
  181. // File Size too large don't populate the Data field.
  182. fNoData = TRUE;
  183. }
  184. else if(ulnFileSize.LowPart > MAX_FILE_SIZE)
  185. {
  186. // File Size Exceeds the set limit
  187. fNoData = TRUE;
  188. }
  189. else
  190. {
  191. fNoData = FALSE;
  192. nFileSize = ulnFileSize.LowPart;
  193. }
  194. if(fSize)
  195. {
  196. hRes = pPCHFileUploadInstance->SetWBEMINT64(pSize,ulnFileSize.QuadPart);
  197. if (FAILED(hRes))
  198. {
  199. // Could not Set the Time Stamp
  200. // Continue anyway
  201. ErrorTrace(TRACE_ID, "SetVariant on Size Field failed.");
  202. }
  203. }
  204. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  205. // DATA //
  206. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. if(fData)
  208. {
  209. if(!fNoData)
  210. {
  211. hFile = CreateFile(tchFileName, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL);
  212. if(hFile != INVALID_HANDLE_VALUE)
  213. {
  214. // Allocate the memory for the buffer
  215. pbBuffer = new char[nFileSize];
  216. if (pbBuffer != NULL)
  217. {
  218. try
  219. {
  220. fFileRead = ReadFile(hFile, pbBuffer, nFileSize, &dwNumBytesRead, NULL);
  221. if(fFileRead)
  222. {
  223. pwcBuffer = new WCHAR[nFileSize];
  224. if (pwcBuffer != NULL)
  225. {
  226. try
  227. {
  228. nRetChars = MultiByteToWideChar(CP_ACP, 0, (const char *)pbBuffer, nFileSize, pwcBuffer, nFileSize);
  229. if(nRetChars != 0)
  230. {
  231. // MultiByteToWideChar succeeds
  232. // Copy the byte buffer into BSTR
  233. bstrData = SysAllocStringLen(pwcBuffer, nFileSize);
  234. varData = bstrData;
  235. SysFreeString(bstrData);
  236. hRes = pPCHFileUploadInstance->SetVariant(pData,varData);
  237. if(FAILED(hRes))
  238. {
  239. // Could not Set the Time Stamp
  240. // Continue anyway
  241. ErrorTrace(TRACE_ID, "SetVariant on Data Field failed.");
  242. }
  243. }
  244. }
  245. catch(...)
  246. {
  247. delete [] pwcBuffer;
  248. throw;
  249. }
  250. delete [] pwcBuffer;
  251. }
  252. else
  253. {
  254. // Cannot allocate pwcBuffer
  255. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  256. }
  257. }
  258. }
  259. catch(...)
  260. {
  261. CloseHandle(hFile);
  262. delete [] pbBuffer;
  263. throw;
  264. }
  265. }
  266. else
  267. {
  268. // Cannot allocate pwcBuffer
  269. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  270. }
  271. CloseHandle(hFile);
  272. }
  273. }
  274. }
  275. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  276. // TIMESTAMP //
  277. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  278. if(fTimeStamp)
  279. {
  280. hRes = pPCHFileUploadInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime));
  281. if (FAILED(hRes))
  282. {
  283. // Could not Set the Time Stamp
  284. // Continue anyway
  285. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  286. }
  287. }
  288. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  289. // CHANGE //
  290. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  291. if(fChange)
  292. {
  293. hRes = pPCHFileUploadInstance->SetVariant(pChange, varSnapshot);
  294. if (FAILED(hRes))
  295. {
  296. //Could not Set the CHANGE property
  297. // Continue anyway
  298. ErrorTrace(TRACE_ID, "Set Variant on SnapShot Field failed.");
  299. }
  300. }
  301. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  302. // DATEACCESSED //
  303. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  304. // ftLastAccessTime gives the last access time for the file.
  305. if(fDateAccessed)
  306. {
  307. hRes = pPCHFileUploadInstance->SetDateTime(pDateAccessed, WBEMTime(FindFileData.ftLastAccessTime));
  308. if (FAILED(hRes))
  309. {
  310. // Could not Set the Date Accessed
  311. // Continue anyway
  312. ErrorTrace(TRACE_ID, "SetDateTime on DATEACCESSED Field failed.");
  313. }
  314. }
  315. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  316. // DATECREATED //
  317. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  318. if(fDateCreated)
  319. {
  320. hRes = pPCHFileUploadInstance->SetDateTime(pDateCreated, WBEMTime(FindFileData.ftCreationTime));
  321. if (FAILED(hRes))
  322. {
  323. // Could not Set the Date Created
  324. // Continue anyway
  325. ErrorTrace(TRACE_ID, "SetDateTime on DATECREATED Field failed.");
  326. }
  327. }
  328. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  329. // DATEMODIFIED //
  330. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  331. if(fDateModified)
  332. {
  333. hRes = pPCHFileUploadInstance->SetDateTime(pDateModified, WBEMTime(FindFileData.ftLastWriteTime));
  334. if (FAILED(hRes))
  335. {
  336. // Could not Set the Date Modified
  337. // Continue anyway
  338. ErrorTrace(TRACE_ID, "SetDateTime on DateModified Field failed.");
  339. }
  340. }
  341. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  342. // FILEATTRIBUTES //
  343. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  344. if(fFileAttributes)
  345. {
  346. dwAttributes = FindFileData.dwFileAttributes;
  347. tchAttributes[0] = 0;
  348. // Get the attributes as a string
  349. if(dwAttributes & FILE_ATTRIBUTE_READONLY)
  350. {
  351. _tcscat(tchAttributes, READONLY);
  352. }
  353. if(dwAttributes & FILE_ATTRIBUTE_HIDDEN)
  354. {
  355. _tcscat(tchAttributes, HIDDEN);
  356. }
  357. if(dwAttributes & FILE_ATTRIBUTE_SYSTEM)
  358. {
  359. _tcscat(tchAttributes, SYSTEM);
  360. }
  361. if(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)
  362. {
  363. _tcscat(tchAttributes, DIRECTORY);
  364. }
  365. if(dwAttributes & FILE_ATTRIBUTE_ARCHIVE)
  366. {
  367. _tcscat(tchAttributes, ARCHIVE);
  368. }
  369. if(dwAttributes & FILE_ATTRIBUTE_NORMAL)
  370. {
  371. _tcscat(tchAttributes, NORMAL);
  372. }
  373. if(dwAttributes & FILE_ATTRIBUTE_TEMPORARY)
  374. {
  375. _tcscat(tchAttributes, TEMPORARY);
  376. }
  377. if(dwAttributes & FILE_ATTRIBUTE_COMPRESSED)
  378. {
  379. _tcscat(tchAttributes, COMPRESSED);
  380. }
  381. if(dwAttributes & FILE_ATTRIBUTE_ENCRYPTED)
  382. {
  383. _tcscat(tchAttributes, ENCRYPTED);
  384. }
  385. if(dwAttributes & FILE_ATTRIBUTE_OFFLINE)
  386. {
  387. _tcscat(tchAttributes, OFFLINE);
  388. }
  389. if(dwAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  390. {
  391. _tcscat(tchAttributes, REPARSEPOINT);
  392. }
  393. if(dwAttributes & FILE_ATTRIBUTE_SPARSE_FILE)
  394. {
  395. _tcscat(tchAttributes, SPARSEFILE);
  396. }
  397. varAttributes = tchAttributes;
  398. // hRes = varAttributes.ChangeType(VT_BSTR, NULL);
  399. // if(SUCCEEDED(hRes))
  400. // {
  401. hRes = pPCHFileUploadInstance->SetVariant(pFileAttributes, varAttributes);
  402. if (FAILED(hRes))
  403. {
  404. // Could not Set the File Attributes
  405. // Continue anyway
  406. ErrorTrace(TRACE_ID, "SetVariant on FileAttributes Field failed.");
  407. // }
  408. }
  409. }
  410. hRes = pPCHFileUploadInstance->Commit();
  411. if(FAILED(hRes))
  412. {
  413. // Could not Commit the instance
  414. ErrorTrace(TRACE_ID, "Commit on PCHFileUploadInstance Failed");
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. END:TraceFunctLeave();
  422. return (hRes);
  423. }