MFC64bitFix.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. #include "stdafx.h"
  15. #ifndef MPEXT
  16. #include "MFC64bitFix.h"
  17. #endif
  18. __int64 GetLength64(CFile &file)
  19. {
  20. DWORD low;
  21. DWORD high;
  22. low=GetFileSize((void *)file.m_hFile, &high);
  23. _int64 size=((_int64)high<<32)+low;
  24. return size;
  25. }
  26. BOOL GetLength64(CString filename, _int64 &size)
  27. {
  28. WIN32_FIND_DATA findFileData;
  29. HANDLE hFind = FindFirstFile(filename, &findFileData);
  30. if (hFind == INVALID_HANDLE_VALUE)
  31. return FALSE;
  32. VERIFY(FindClose(hFind));
  33. size=((_int64)findFileData.nFileSizeHigh<<32)+findFileData.nFileSizeLow;
  34. return TRUE;
  35. }
  36. BOOL AFXAPI AfxFullPath(LPTSTR lpszPathOut, LPCTSTR lpszFileIn);
  37. BOOL PASCAL GetStatus64(LPCTSTR lpszFileName, CFileStatus64& rStatus)
  38. {
  39. // attempt to fully qualify path first
  40. if (!AfxFullPath(rStatus.m_szFullName, lpszFileName))
  41. {
  42. rStatus.m_szFullName[0] = _MPT('\0');
  43. return FALSE;
  44. }
  45. WIN32_FIND_DATA findFileData;
  46. HANDLE hFind = FindFirstFile((LPTSTR)lpszFileName, &findFileData);
  47. if (hFind == INVALID_HANDLE_VALUE)
  48. return FALSE;
  49. VERIFY(FindClose(hFind));
  50. // strip attribute of NORMAL bit, our API doesn't have a "normal" bit.
  51. rStatus.m_attribute = (BYTE)
  52. (findFileData.dwFileAttributes & ~FILE_ATTRIBUTE_NORMAL);
  53. rStatus.m_size = ((_int64)findFileData.nFileSizeHigh<<32)+findFileData.nFileSizeLow;
  54. // convert times as appropriate
  55. TRY
  56. {
  57. rStatus.m_ctime = CTime(findFileData.ftCreationTime);
  58. rStatus.m_has_ctime = true;
  59. }
  60. CATCH_ALL(e)
  61. {
  62. rStatus.m_has_ctime = false;
  63. }
  64. END_CATCH_ALL;
  65. TRY
  66. {
  67. rStatus.m_atime = CTime(findFileData.ftLastAccessTime);
  68. rStatus.m_has_atime = true;
  69. }
  70. CATCH_ALL(e)
  71. {
  72. rStatus.m_has_atime = false;
  73. }
  74. END_CATCH_ALL;
  75. TRY
  76. {
  77. rStatus.m_mtime = CTime(findFileData.ftLastWriteTime);
  78. rStatus.m_has_mtime = true;
  79. }
  80. CATCH_ALL(e)
  81. {
  82. rStatus.m_has_mtime = false;
  83. }
  84. END_CATCH_ALL;
  85. if (!rStatus.m_has_ctime || rStatus.m_ctime.GetTime() == 0)
  86. {
  87. if (rStatus.m_has_mtime)
  88. {
  89. rStatus.m_ctime = rStatus.m_mtime;
  90. rStatus.m_has_ctime = true;
  91. }
  92. else
  93. rStatus.m_has_ctime = false;
  94. }
  95. if (!rStatus.m_has_atime || rStatus.m_atime.GetTime() == 0)
  96. {
  97. if (rStatus.m_has_mtime)
  98. {
  99. rStatus.m_atime = rStatus.m_mtime;
  100. rStatus.m_has_atime = true;
  101. }
  102. else
  103. rStatus.m_has_atime = false;
  104. }
  105. if (!rStatus.m_has_mtime || rStatus.m_mtime.GetTime() == 0)
  106. {
  107. if (rStatus.m_has_ctime)
  108. {
  109. rStatus.m_mtime = rStatus.m_ctime;
  110. rStatus.m_has_mtime = true;
  111. }
  112. else
  113. rStatus.m_has_mtime = false;
  114. }
  115. return TRUE;
  116. }
  117. _int64 GetPosition64(CFile &file)
  118. {
  119. LONG low=0;
  120. LONG high=0;
  121. low=SetFilePointer((HANDLE)file.m_hFile, low, &high, FILE_CURRENT);
  122. if (low==0xFFFFFFFF && GetLastError!=NO_ERROR)
  123. CFileException::ThrowOsError((LONG)::GetLastError());
  124. return ((_int64)high<<32)+low;
  125. }