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.

321 lines
8.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  5. //
  6. // File: refilb.cxx
  7. //
  8. // Contents: Reference ILockBytes class
  9. //
  10. // Classes: CFileILB
  11. //
  12. // Notes: This Class always call single byte I/O routines
  13. // because most systems only have support for single byte
  14. // I/O. This makes the code more portable.
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "msfhead.cxx"
  18. #include <errno.h>
  19. #include "h/refilb.hxx"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/stat.h>
  23. #include "time.hxx"
  24. #include "h/tchar.h"
  25. #ifdef _WIN32
  26. #include <io.h> // to get definition of wunlink
  27. #else
  28. #include <unistd.h>
  29. // get the correct stat structure
  30. #endif
  31. #include <stdlib.h>
  32. #include "h/ole.hxx"
  33. DECLARE_INFOLEVEL(ol, DEB_ERROR)
  34. static int filenum = 0;
  35. char * GetTempFileName(void)
  36. {
  37. char *psz = new char[_MAX_PATH +1];
  38. strcpy(psz, "dft");
  39. _itoa(filenum, psz + 3, 10);
  40. filenum++;
  41. return psz;
  42. }
  43. CFileILB::CFileILB(const TCHAR *pszName,
  44. DWORD grfMode,
  45. BOOL fOpenFile/* =TRUE */)
  46. {
  47. _pszName = NULL;
  48. _fDelete = FALSE;
  49. if (pszName == NULL)
  50. {
  51. _pszName = GetTempFileName();
  52. _unlink(_pszName); // make sure file is over written
  53. _fDelete |= ILB_DELETEONERR; // don't want to keep scratch files
  54. }
  55. else
  56. {
  57. _pszName = new char[_MAX_PATH + 1];
  58. TTOS(pszName, _pszName, _tcslen(pszName)+1);
  59. }
  60. if (grfMode & STGM_DELETEONRELEASE)
  61. _fDelete |= ILB_DELETEONRELEASE;
  62. _f = NULL;
  63. if (fOpenFile)
  64. {
  65. // disregard if file is already there
  66. Create(STGM_CREATE|STGM_READWRITE);
  67. // got to open the file with this option
  68. olAssert(_f && "CFileILB could not open the file!");
  69. }
  70. _ulRef = 1;
  71. }
  72. static const char pcszReadOnly[] ="rb";
  73. static const char pcszReadWrite[] = "r+b";
  74. static const char pcszWrite[] = "w+b";
  75. SCODE CFileILB::Create(DWORD grfMode)
  76. {
  77. char const *pszMode = pcszReadOnly; // default
  78. if (grfMode & STGM_READWRITE)
  79. pszMode = pcszReadWrite;
  80. else
  81. olDebugOut((DEB_ITRACE, "CFileILB::Create called with Read Only!!\n"));
  82. _f = fopen(_pszName, pszMode);
  83. if (_f) // open succeeded
  84. {
  85. if ((grfMode & (STGM_CREATE|STGM_CONVERT) ) == STGM_FAILIFTHERE)
  86. return STG_E_FILEALREADYEXISTS;
  87. }
  88. else if (errno==EACCES && (grfMode & STGM_CONVERT))
  89. {
  90. olDebugOut((DEB_ERROR,"Access Denied in CFileILB::Create\n"));
  91. return STG_E_ACCESSDENIED;
  92. }
  93. else
  94. {
  95. // the file does not exists, create the file
  96. _f = fopen(_pszName, pcszWrite);
  97. if (_f==NULL)
  98. {
  99. // we could not create the file for some reason
  100. // return the appropriate error code
  101. if (errno== EACCES)
  102. return STG_E_ACCESSDENIED;
  103. else
  104. {
  105. return STG_E_INVALIDNAME; // assume it is an invalid name
  106. }
  107. }
  108. else
  109. {
  110. // the newly create file should be deleted on error
  111. _fDelete |= ILB_DELETEONERR;
  112. }
  113. }
  114. return S_OK;
  115. }
  116. SCODE CFileILB::Open(DWORD grfMode)
  117. {
  118. char const *pszMode = pcszReadOnly; // default
  119. // this means an null named file
  120. olAssert( (_fDelete & ILB_DELETEONERR)==0 );
  121. // has been opened
  122. if (grfMode & STGM_READWRITE)
  123. pszMode = pcszReadWrite;
  124. else
  125. olDebugOut((DEB_ITRACE, "CFileILB::Open called with Read Only!!\n"));
  126. _f = fopen(_pszName, pszMode);
  127. if (_f == NULL)
  128. {
  129. if (errno==EACCES) return STG_E_ACCESSDENIED;
  130. else if (errno==ENOENT) return STG_E_FILENOTFOUND;
  131. else return STG_E_INVALIDNAME; // we assume that the name is invalid
  132. }
  133. return S_OK;
  134. }
  135. CFileILB::~CFileILB()
  136. {
  137. if (_f)
  138. fclose(_f);
  139. if (_fDelete & ILB_DELETEONRELEASE)
  140. {
  141. // nothing we can do if the file cannot be deleted somehow
  142. // since the ref impl. is not multi-thread safe
  143. _unlink(_pszName);
  144. }
  145. delete _pszName;
  146. }
  147. STDMETHODIMP CFileILB::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  148. {
  149. *ppvObj = NULL;
  150. UNREFERENCED_PARM(riid);
  151. olAssert(FALSE && aMsg("function not implemented!"));
  152. return ResultFromScode(STG_E_INVALIDFUNCTION);
  153. }
  154. STDMETHODIMP_(ULONG) CFileILB::AddRef(void)
  155. {
  156. AtomicInc(&_ulRef);
  157. return(_ulRef);
  158. }
  159. STDMETHODIMP_(ULONG) CFileILB::Release(void)
  160. {
  161. AtomicDec(&_ulRef);
  162. olDebugOut( (DEB_ITRACE, "CFileILB::Release => %lx\n", _ulRef) );
  163. if (_ulRef > 0)
  164. return(_ulRef);
  165. delete this;
  166. return(0);
  167. }
  168. ULONG CFileILB::ReleaseOnError(void)
  169. {
  170. // this function should be not used otherwise
  171. olAssert(_ulRef == 1);
  172. // Delete the file if it is a file we just created
  173. if (_fDelete & ILB_DELETEONERR)
  174. _fDelete |= ILB_DELETEONRELEASE;
  175. return( Release() );
  176. }
  177. STDMETHODIMP CFileILB::ReadAt(ULARGE_INTEGER ulPosition,
  178. VOID HUGEP *pb,
  179. ULONG cb,
  180. ULONG *pcbRead)
  181. {
  182. fseek(_f, ULIGetLow(ulPosition), SEEK_SET);
  183. *pcbRead = fread(pb, 1, cb, _f);
  184. return NOERROR;
  185. }
  186. STDMETHODIMP CFileILB::WriteAt(ULARGE_INTEGER ulPosition,
  187. VOID const HUGEP *pb,
  188. ULONG cb,
  189. ULONG FAR *pcbWritten)
  190. {
  191. fseek(_f, ULIGetLow(ulPosition), SEEK_SET);
  192. *pcbWritten = fwrite(pb, 1, cb, _f);
  193. return NOERROR;
  194. }
  195. STDMETHODIMP CFileILB::Flush(void)
  196. {
  197. fflush(_f);
  198. return NOERROR;
  199. }
  200. STDMETHODIMP CFileILB::SetSize(ULARGE_INTEGER ulNewSize)
  201. {
  202. LONG cbNewSize = ULIGetLow(ulNewSize);
  203. LONG cbCurrentSize = ftell(_f);
  204. if(-1 == cbCurrentSize)
  205. return STG_E_SEEKERROR;
  206. if(cbCurrentSize < cbNewSize)
  207. { // Increase the Size
  208. fseek(_f, cbNewSize-1, SEEK_SET);
  209. if(1 != fwrite("", 1, 1, _f))
  210. return STG_E_WRITEFAULT;
  211. }
  212. else if(cbCurrentSize > cbNewSize)
  213. { // Decrease the Size
  214. // OS specific: file truncation.
  215. }
  216. return NOERROR;
  217. }
  218. STDMETHODIMP CFileILB::LockRegion(ULARGE_INTEGER libOffset,
  219. ULARGE_INTEGER cb,
  220. DWORD dwLockType)
  221. {
  222. UNREFERENCED_PARM(dwLockType);
  223. UNREFERENCED_PARM(cb);
  224. UNREFERENCED_PARM(libOffset);
  225. olAssert(FALSE && aMsg("function not implemented!"));
  226. return ResultFromScode(STG_E_INVALIDFUNCTION);
  227. }
  228. STDMETHODIMP CFileILB::UnlockRegion(ULARGE_INTEGER libOffset,
  229. ULARGE_INTEGER cb,
  230. DWORD dwLockType)
  231. {
  232. UNREFERENCED_PARM(dwLockType);
  233. UNREFERENCED_PARM(cb);
  234. UNREFERENCED_PARM(libOffset);
  235. olAssert(FALSE && aMsg("function not implemented!"));
  236. return ResultFromScode(STG_E_INVALIDFUNCTION);
  237. }
  238. STDMETHODIMP CFileILB::Stat(STATSTG FAR *pstatstg, DWORD grfStatFlag)
  239. {
  240. memset(pstatstg, 0, sizeof(STATSTG));
  241. if ((grfStatFlag & STATFLAG_NONAME) == 0)
  242. {
  243. char pchTemp[_MAX_PATH+1];
  244. _fullpath(pchTemp, _pszName, _MAX_PATH+1);
  245. pstatstg->pwcsName = new TCHAR[strlen(pchTemp)+1];
  246. STOT(pchTemp, pstatstg->pwcsName, strlen(pchTemp)+1);
  247. }
  248. pstatstg->type = STGTY_LOCKBYTES;
  249. ULISetHigh(pstatstg->cbSize, 0);
  250. fseek(_f, 0, SEEK_END);
  251. ULISetLow(pstatstg->cbSize, ftell(_f));
  252. // just return a default, the function that calls this should fill in
  253. // the structure.
  254. pstatstg->grfMode = STGM_READWRITE | STGM_DIRECT | STGM_SHARE_EXCLUSIVE;
  255. struct _stat buf;
  256. int result = _stat(_pszName, &buf);
  257. if (!result) // fill in zeros
  258. {
  259. pstatstg->atime.dwLowDateTime = pstatstg->atime.dwHighDateTime = 0;
  260. pstatstg->mtime.dwLowDateTime = pstatstg->mtime.dwHighDateTime = 0;
  261. pstatstg->ctime.dwLowDateTime = pstatstg->ctime.dwHighDateTime = 0;
  262. }
  263. else
  264. {
  265. TimeTToFileTime(&buf.st_atime, &pstatstg->atime);
  266. TimeTToFileTime(&buf.st_mtime, &pstatstg->mtime);
  267. TimeTToFileTime(&buf.st_ctime, &pstatstg->ctime);
  268. }
  269. return NOERROR;
  270. }
  271. EXTERN_C STDAPI_(BOOL) IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
  272. {
  273. return (memcmp(&rguid1, &rguid2, sizeof(GUID)) == 0);
  274. }