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.

341 lines
8.9 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. ULONG CFileILB::Seek (ULONGLONG ulPos)
  178. {
  179. ULONG ulRet = 0;
  180. if (ulPos > MAX_ULONG)
  181. {
  182. fseek(_f, MAX_ULONG, SEEK_SET);
  183. while (ulPos > MAX_ULONG)
  184. {
  185. ulPos -= MAX_ULONG;
  186. if (ulPos > MAX_ULONG)
  187. ulRet = fseek(_f, MAX_ULONG, SEEK_CUR);
  188. else
  189. ulRet = fseek(_f, (long) ulPos, SEEK_CUR);
  190. }
  191. }
  192. else ulRet = fseek(_f, (long) ulPos, SEEK_SET);
  193. return ulRet;
  194. }
  195. STDMETHODIMP CFileILB::ReadAt(ULARGE_INTEGER ulPosition,
  196. VOID HUGEP *pb,
  197. ULONG cb,
  198. ULONG *pcbRead)
  199. {
  200. Seek (ulPosition.QuadPart);
  201. *pcbRead = fread(pb, 1, cb, _f);
  202. return NOERROR;
  203. }
  204. STDMETHODIMP CFileILB::WriteAt(ULARGE_INTEGER ulPosition,
  205. VOID const HUGEP *pb,
  206. ULONG cb,
  207. ULONG FAR *pcbWritten)
  208. {
  209. Seek (ulPosition.QuadPart);
  210. *pcbWritten = fwrite(pb, 1, cb, _f);
  211. return NOERROR;
  212. }
  213. STDMETHODIMP CFileILB::Flush(void)
  214. {
  215. fflush(_f);
  216. return NOERROR;
  217. }
  218. STDMETHODIMP CFileILB::SetSize(ULARGE_INTEGER ulNewSize)
  219. {
  220. LONGLONG cbNewSize = ulNewSize.QuadPart;
  221. LONGLONG cbCurrentSize = _filelengthi64 (_fileno(_f));
  222. if(-1 == cbCurrentSize)
  223. return STG_E_SEEKERROR;
  224. if(cbCurrentSize < cbNewSize)
  225. { // Increase the Size
  226. Seek (cbNewSize-1);
  227. if(1 != fwrite("", 1, 1, _f))
  228. return STG_E_WRITEFAULT;
  229. }
  230. else if(cbCurrentSize > cbNewSize)
  231. { // Decrease the Size
  232. // OS specific: file truncation.
  233. }
  234. return NOERROR;
  235. }
  236. STDMETHODIMP CFileILB::LockRegion(ULARGE_INTEGER libOffset,
  237. ULARGE_INTEGER cb,
  238. DWORD dwLockType)
  239. {
  240. UNREFERENCED_PARM(dwLockType);
  241. UNREFERENCED_PARM(cb);
  242. UNREFERENCED_PARM(libOffset);
  243. olAssert(FALSE && aMsg("function not implemented!"));
  244. return ResultFromScode(STG_E_INVALIDFUNCTION);
  245. }
  246. STDMETHODIMP CFileILB::UnlockRegion(ULARGE_INTEGER libOffset,
  247. ULARGE_INTEGER cb,
  248. DWORD dwLockType)
  249. {
  250. UNREFERENCED_PARM(dwLockType);
  251. UNREFERENCED_PARM(cb);
  252. UNREFERENCED_PARM(libOffset);
  253. olAssert(FALSE && aMsg("function not implemented!"));
  254. return ResultFromScode(STG_E_INVALIDFUNCTION);
  255. }
  256. STDMETHODIMP CFileILB::Stat(STATSTG FAR *pstatstg, DWORD grfStatFlag)
  257. {
  258. memset(pstatstg, 0, sizeof(STATSTG));
  259. if ((grfStatFlag & STATFLAG_NONAME) == 0)
  260. {
  261. char pchTemp[_MAX_PATH+1];
  262. _fullpath(pchTemp, _pszName, _MAX_PATH+1);
  263. pstatstg->pwcsName = new TCHAR[strlen(pchTemp)+1];
  264. STOT(pchTemp, pstatstg->pwcsName, strlen(pchTemp)+1);
  265. }
  266. pstatstg->type = STGTY_LOCKBYTES;
  267. fseek(_f, 0, SEEK_END);
  268. pstatstg->cbSize.QuadPart = ftell(_f);
  269. // just return a default, the function that calls this should fill in
  270. // the structure.
  271. pstatstg->grfMode = STGM_READWRITE | STGM_DIRECT | STGM_SHARE_EXCLUSIVE;
  272. struct _stat buf;
  273. int result = _stat(_pszName, &buf);
  274. if (!result) // fill in zeros
  275. {
  276. pstatstg->atime.dwLowDateTime = pstatstg->atime.dwHighDateTime = 0;
  277. pstatstg->mtime.dwLowDateTime = pstatstg->mtime.dwHighDateTime = 0;
  278. pstatstg->ctime.dwLowDateTime = pstatstg->ctime.dwHighDateTime = 0;
  279. }
  280. else
  281. {
  282. TimeTToFileTime(&buf.st_atime, &pstatstg->atime);
  283. TimeTToFileTime(&buf.st_mtime, &pstatstg->mtime);
  284. TimeTToFileTime(&buf.st_ctime, &pstatstg->ctime);
  285. }
  286. return NOERROR;
  287. }
  288. EXTERN_C STDAPI_(BOOL) IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
  289. {
  290. return (memcmp(&rguid1, &rguid2, sizeof(GUID)) == 0);
  291. }