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.

359 lines
8.4 KiB

  1. /**************************************************************************
  2. ** FILENAME:
  3. **
  4. * INTEL CORPORATION PROPRIETARY INFORMATION
  5. * Copyright Intel Corporation
  6. *
  7. * This software is supplied under the terms of a license agreement or
  8. * non-disclosure agreement with Intel Corporation and may not be copied
  9. * or disclosed in accordance with the terms of that agreement.
  10. *
  11. *
  12. ** PURPOSE:
  13. **
  14. ** $Header:
  15. **
  16. **
  17. ** $Log:
  18. *********************************************************************************/
  19. #include "mbftpch.h"
  20. #include "fileio.hpp"
  21. #define STRSAFE_NO_DEPRECATE 1
  22. #include <strsafe.h>
  23. extern int MyLoadString(UINT, LPTSTR);
  24. extern int MyLoadString(UINT, LPTSTR, LPTSTR);
  25. extern int MyLoadString(UINT, LPTSTR, LPTSTR, LPTSTR);
  26. CMBFTFile::CMBFTFile()
  27. {
  28. m_FileHandle = INVALID_HANDLE_VALUE;
  29. m_LastIOError = 0;
  30. m_szFileName[0] = 0;
  31. }
  32. CMBFTFile::~CMBFTFile()
  33. {
  34. /* close file if still open */
  35. Close ();
  36. }
  37. BOOL CMBFTFile::Open(LPCSTR lpszFileName,unsigned iOpenMode)
  38. {
  39. lstrcpyn(m_szFileName,lpszFileName,sizeof(m_szFileName));
  40. DWORD fdwAccess = ((0!=(iOpenMode&FDW_Read))*GENERIC_READ)
  41. | ((0!=(iOpenMode&FDW_Write))*GENERIC_WRITE);
  42. DWORD fdwShareMode = ((0==(iOpenMode&FDW_RDeny))*FILE_SHARE_READ)
  43. | ((0==(iOpenMode&FDW_WDeny))*FILE_SHARE_WRITE);
  44. DWORD fdwCreate = (iOpenMode&FDW_Create)?CREATE_ALWAYS:OPEN_EXISTING;
  45. m_LastIOError = 0;
  46. m_FileHandle = CreateFile(
  47. lpszFileName,
  48. fdwAccess,
  49. fdwShareMode,
  50. NULL,
  51. fdwCreate,
  52. FILE_ATTRIBUTE_NORMAL,
  53. NULL );
  54. if( INVALID_HANDLE_VALUE == m_FileHandle )
  55. {
  56. m_LastIOError = GetLastError();
  57. }
  58. return(m_LastIOError == 0);
  59. }
  60. BOOL CMBFTFile::Close(BOOL status)
  61. {
  62. m_LastIOError = 0;
  63. /* nothing to do if file already closed */
  64. if( m_FileHandle == INVALID_HANDLE_VALUE )
  65. return ( m_LastIOError == 0 );
  66. /* close the file */
  67. if( !CloseHandle( m_FileHandle ) )
  68. {
  69. m_LastIOError = GetLastError();
  70. }
  71. m_FileHandle = INVALID_HANDLE_VALUE;
  72. /* just delete file if status==FALSE */
  73. if( status == FALSE )
  74. {
  75. ::DeleteFile(m_szFileName);
  76. }
  77. return( m_LastIOError == 0 );
  78. }
  79. BOOL CMBFTFile::Create(LPCSTR lpszDirName, LPCSTR lpszFileName)
  80. {
  81. DWORD dwTick;
  82. BOOL bCreateFile = TRUE;
  83. /* protect against path info embedded in received file name */
  84. lpszFileName = GetFileNameFromPath(lpszFileName);
  85. /* copy original file name */
  86. if(FAILED(StringCchPrintfA(m_szFileName, CCHMAX(m_szFileName), "%s\\%s", lpszDirName, lpszFileName)))
  87. {
  88. m_LastIOError = ERROR_BUFFER_OVERFLOW;
  89. return FALSE;
  90. }
  91. /* generate temp file name if file exists */
  92. if (FFileExists(m_szFileName))
  93. {
  94. // REVIEW
  95. //Small hack here -- if file already exists, check to see if we have write access.
  96. //If not, we say report an access denied error...
  97. if (FFileExists(m_szFileName))
  98. {
  99. TCHAR szNewFileName[_MAX_PATH];
  100. INT_PTR nCount = 1;
  101. while (1)
  102. {
  103. MyLoadString(IDS_COPY_N_OF, szNewFileName, (LPTSTR)nCount, (LPTSTR)lpszFileName);
  104. if(FAILED(StringCchPrintfA(m_szFileName, CCHMAX(m_szFileName), "%s\\%s", lpszDirName, szNewFileName)))
  105. {
  106. m_LastIOError = ERROR_BUFFER_OVERFLOW;
  107. return FALSE;
  108. }
  109. if (!FFileExists(m_szFileName))
  110. {
  111. break;
  112. }
  113. nCount++;
  114. }
  115. }
  116. else
  117. {
  118. bCreateFile = FALSE;
  119. }
  120. }
  121. //This flag is reset only if file already exists and is read only...
  122. if(bCreateFile)
  123. {
  124. /* finally, create the file */
  125. m_LastIOError = 0;
  126. m_FileHandle = CreateFile(
  127. m_szFileName,
  128. GENERIC_READ|GENERIC_WRITE,
  129. FILE_SHARE_READ,
  130. NULL,
  131. CREATE_ALWAYS,
  132. FILE_ATTRIBUTE_NORMAL,
  133. NULL );
  134. if( INVALID_HANDLE_VALUE == m_FileHandle )
  135. {
  136. m_LastIOError = GetLastError();
  137. }
  138. }
  139. else
  140. {
  141. m_szFileName[0] = TEXT('\0'); // Clear file name
  142. }
  143. return(bCreateFile && (m_LastIOError == 0));
  144. }
  145. LONG CMBFTFile::Seek( LONG lOffset, int iFromWhere )
  146. {
  147. DWORD MoveMethod[] = { FILE_BEGIN, FILE_CURRENT, FILE_END };
  148. LONG Position = (LONG)SetFilePointer( m_FileHandle, lOffset, NULL, MoveMethod[min((unsigned)iFromWhere,2)] );
  149. m_LastIOError = 0;
  150. if( Position == -1L )
  151. {
  152. m_LastIOError = GetLastError();
  153. }
  154. return( Position );
  155. }
  156. ULONG CMBFTFile::Read(LPSTR lpszBuffer, ULONG iNumBytes)
  157. {
  158. ULONG iBytesRead = 0;
  159. if(iNumBytes)
  160. {
  161. m_LastIOError = 0;
  162. if( !ReadFile( m_FileHandle, lpszBuffer, iNumBytes, (LPDWORD)&iBytesRead, NULL ) )
  163. {
  164. m_LastIOError = GetLastError();
  165. }
  166. if(m_LastIOError != 0)
  167. {
  168. iBytesRead = (ULONG)-1;
  169. }
  170. }
  171. return(iBytesRead);
  172. }
  173. BOOL CMBFTFile::Write(LPCSTR lpszBuffer, ULONG iNumBytes)
  174. {
  175. ULONG iBytesWritten = 0;
  176. if(iNumBytes)
  177. {
  178. if( !WriteFile( m_FileHandle, lpszBuffer, iNumBytes, (LPDWORD)&iBytesWritten, NULL ) )
  179. {
  180. m_LastIOError = GetLastError();
  181. }
  182. if(!m_LastIOError)
  183. {
  184. if(iBytesWritten != iNumBytes)
  185. {
  186. m_LastIOError = (ULONG)-1; //Kludge for insufficient disk space...
  187. }
  188. }
  189. }
  190. return(iBytesWritten == iNumBytes);
  191. }
  192. LONG CMBFTFile::GetFileSize(void)
  193. {
  194. return( (LONG)::GetFileSize( m_FileHandle, NULL ) );
  195. }
  196. BOOL CMBFTFile::DeleteFile(void)
  197. {
  198. BOOL bReturn = FALSE;
  199. /* delete if has name */
  200. if(lstrlen(m_szFileName))
  201. {
  202. /* close file before deleting */
  203. CloseHandle( m_FileHandle );
  204. bReturn = ::DeleteFile( m_szFileName );
  205. if( !bReturn )
  206. {
  207. m_LastIOError = GetLastError();
  208. }
  209. }
  210. return(bReturn);
  211. }
  212. time_t CMBFTFile::GetFileDateTime(void)
  213. {
  214. WORD Date = 0;
  215. WORD Time = 0;
  216. BY_HANDLE_FILE_INFORMATION bhfi;
  217. if( !GetFileInformationByHandle( m_FileHandle, &bhfi ) )
  218. {
  219. return( 0 );
  220. }
  221. FileTimeToDosDateTime( &bhfi.ftLastWriteTime, &Date, &Time );
  222. return(MAKELONG(Time,Date));
  223. }
  224. BOOL CMBFTFile::SetFileDateTime(time_t FileDateTime)
  225. {
  226. BOOL bReturn = FALSE;
  227. FILETIME ft;
  228. DosDateTimeToFileTime( HIWORD(FileDateTime), LOWORD(FileDateTime), &ft );
  229. bReturn = SetFileTime( m_FileHandle, NULL, NULL, &ft );
  230. return(bReturn);
  231. }
  232. int CMBFTFile::GetLastErrorCode(void)
  233. {
  234. struct XLAT
  235. {
  236. unsigned Win32ErrorCode;
  237. int MBFTErrorCode;
  238. };
  239. static XLAT MBFTXlatTable[] =
  240. {
  241. {0,iMBFT_OK},
  242. {ERROR_FILE_NOT_FOUND,iMBFT_FILE_NOT_FOUND},
  243. {ERROR_PATH_NOT_FOUND,iMBFT_INVALID_PATH},
  244. {ERROR_TOO_MANY_OPEN_FILES,iMBFT_TOO_MANY_OPEN_FILES},
  245. {ERROR_ACCESS_DENIED,iMBFT_FILE_ACCESS_DENIED},
  246. {ERROR_SHARING_VIOLATION,iMBFT_FILE_SHARING_VIOLATION},
  247. {ERROR_HANDLE_DISK_FULL,iMBFT_INSUFFICIENT_DISK_SPACE}
  248. };
  249. int Index;
  250. int MBFTErrorCode = iMBFT_FILE_IO_ERROR;
  251. for(Index = 0;Index < (sizeof(MBFTXlatTable) / sizeof(MBFTXlatTable[0]));Index++)
  252. {
  253. if(MBFTXlatTable[Index].Win32ErrorCode == m_LastIOError)
  254. {
  255. MBFTErrorCode = MBFTXlatTable[Index].MBFTErrorCode;
  256. break;
  257. }
  258. }
  259. return(MBFTErrorCode);
  260. }
  261. LPCSTR CMBFTFile::GetTempDirectory(void)
  262. {
  263. LPSTR lpszPointer = NULL;
  264. TCHAR szTempPath[MAX_PATH] = TEXT("");
  265. DWORD cchTempPath = 0;
  266. // get the temp path from the system
  267. cchTempPath = ::GetTempPath(sizeof(szTempPath), szTempPath);
  268. if (cchTempPath == 0 || cchTempPath >= sizeof(szTempPath))
  269. {
  270. return NULL;
  271. }
  272. if( GetTempFileName( szTempPath, "Junk", 0, m_szTempDirectory ) ) /*Localization OK*/
  273. {
  274. ::DeleteFile( m_szTempDirectory );
  275. lpszPointer = SzFindLastCh(m_szTempDirectory,'\\');
  276. if(lpszPointer)
  277. {
  278. *lpszPointer = '\0';
  279. }
  280. }
  281. if(!lpszPointer)
  282. {
  283. lstrcpy(m_szTempDirectory,"");
  284. }
  285. return((LPCSTR)m_szTempDirectory);
  286. }
  287. BOOL CMBFTFile::GetIsEOF()
  288. {
  289. BOOL fReturn = FALSE;
  290. if( INVALID_HANDLE_VALUE != m_FileHandle )
  291. {
  292. DWORD dwCurrentPosition = SetFilePointer( m_FileHandle, 0, NULL, FILE_CURRENT );
  293. DWORD dwEndPosition = SetFilePointer( m_FileHandle, 0, NULL, FILE_END );
  294. fReturn = dwCurrentPosition >= dwEndPosition;
  295. SetFilePointer( m_FileHandle, dwCurrentPosition, NULL, FILE_BEGIN );
  296. }
  297. return( fReturn );
  298. }