FileHelper.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #include "FileHelper.h"
  24. #include <sys/stat.h>
  25. CString GetCurrentDirectory()
  26. {
  27. char szPath[MAX_PATH];
  28. if(getcwd(szPath, sizeof(szPath) - 1) == nullptr)
  29. szPath[0] = 0;
  30. return szPath;
  31. }
  32. CString GetModuleFileName(pid_t pid)
  33. {
  34. if(pid == 0)
  35. pid = SELF_PROCESS_ID;
  36. char szLink[MAX_PATH];
  37. char szPath[MAX_PATH];
  38. sprintf(szLink, "/proc/%d/exe", pid);
  39. SSIZE_T rs = readlink(szLink, szPath, sizeof(szPath) - 1);
  40. if(rs < 0) rs = 0;
  41. szPath[rs] = 0;
  42. return szPath;
  43. }
  44. BOOL SetCurrentPathToModulePath(pid_t pid)
  45. {
  46. CString strPath = GetModuleFileName(pid);
  47. if(strPath.IsEmpty())
  48. return FALSE;
  49. CString::size_type pos = strPath.rfind('/');
  50. if(pos == CString::npos)
  51. return FALSE;
  52. return IS_NO_ERROR(chdir(strPath.substr(0, pos + 1)));
  53. }
  54. BOOL CFile::Open(LPCTSTR lpszFilePath, int iFlag, mode_t iMode)
  55. {
  56. CHECK_ERROR(!IsValid(), ERROR_INVALID_STATE);
  57. m_fd = open(lpszFilePath, iFlag, iMode);
  58. return IS_VALID_FD(m_fd);
  59. }
  60. BOOL CFile::Close()
  61. {
  62. CHECK_ERROR(IsValid(), ERROR_INVALID_STATE);
  63. if(IS_NO_ERROR(close(m_fd)))
  64. {
  65. m_fd = INVALID_FD;
  66. return TRUE;
  67. }
  68. return FALSE;
  69. }
  70. BOOL CFile::Stat(struct stat& st)
  71. {
  72. CHECK_ERROR_INVOKE(fstat(m_fd, &st));
  73. return TRUE;
  74. }
  75. BOOL CFile::GetSize(SIZE_T& dwSize)
  76. {
  77. struct stat st;
  78. CHECK_IS_OK(Stat(st));
  79. dwSize = st.st_size;
  80. return TRUE;
  81. }
  82. BOOL CFile::IsDirectory()
  83. {
  84. struct stat st;
  85. CHECK_IS_OK(Stat(st));
  86. return S_ISDIR(st.st_mode);
  87. }
  88. BOOL CFile::IsFile()
  89. {
  90. struct stat st;
  91. CHECK_IS_OK(Stat(st));
  92. return S_ISREG(st.st_mode);
  93. }
  94. BOOL CFile::IsExist(LPCTSTR lpszFilePath)
  95. {
  96. return IS_NO_ERROR(access(lpszFilePath, F_OK));
  97. }
  98. BOOL CFile::IsDirectory(LPCTSTR lpszFilePath)
  99. {
  100. struct stat st;
  101. CHECK_ERROR_INVOKE(stat(lpszFilePath, &st));
  102. return S_ISDIR(st.st_mode);
  103. }
  104. BOOL CFile::IsFile(LPCTSTR lpszFilePath)
  105. {
  106. struct stat st;
  107. CHECK_ERROR_INVOKE(stat(lpszFilePath, &st));
  108. return S_ISREG(st.st_mode);
  109. }
  110. BOOL CFile::IsLink(LPCTSTR lpszFilePath)
  111. {
  112. struct stat st;
  113. CHECK_ERROR_INVOKE(lstat(lpszFilePath, &st));
  114. return S_ISLNK(st.st_mode);
  115. }
  116. BOOL CFileMapping::Map(LPCTSTR lpszFilePath, SIZE_T dwSize, SIZE_T dwOffset, int iProtected, int iFlag)
  117. {
  118. CHECK_ERROR(!IsValid(), ERROR_INVALID_STATE);
  119. FD fd = INVALID_FD;
  120. if(lpszFilePath != nullptr)
  121. {
  122. int iFileFlag = O_RDONLY;
  123. if(iProtected & PROT_WRITE)
  124. {
  125. if(iProtected & PROT_READ)
  126. iFileFlag = O_RDWR;
  127. else
  128. iFileFlag = O_WRONLY;
  129. }
  130. fd = open(lpszFilePath, iFileFlag);
  131. CHECK_ERROR_FD(fd);
  132. }
  133. BOOL isOK = Map(fd, dwSize, dwOffset, iProtected, iFlag);
  134. if(IS_VALID_FD(fd)) EXECUTE_RESTORE_ERROR(close(fd));
  135. return isOK;
  136. }
  137. BOOL CFileMapping::Map(FD fd, SIZE_T dwSize, SIZE_T dwOffset, int iProtected, int iFlag)
  138. {
  139. CHECK_ERROR(!IsValid(), ERROR_INVALID_STATE);
  140. if(IS_INVALID_FD(fd))
  141. {
  142. CHECK_EINVAL((iFlag & MAP_ANONYMOUS) && (dwSize > 0));
  143. }
  144. else
  145. {
  146. CHECK_EINVAL((iFlag & MAP_ANONYMOUS) == 0);
  147. struct stat st;
  148. CHECK_ERROR_INVOKE(fstat(fd, &st));
  149. CHECK_ERROR(S_ISREG(st.st_mode), ERROR_BAD_FILE_TYPE);
  150. if(dwSize == 0)
  151. dwSize = st.st_size;
  152. }
  153. m_pv = (PBYTE)mmap(nullptr, dwSize, iProtected, iFlag, fd, dwOffset);
  154. if(IsValid())
  155. {
  156. m_dwSize = dwSize;
  157. return TRUE;
  158. }
  159. return FALSE;
  160. }
  161. BOOL CFileMapping::Unmap()
  162. {
  163. CHECK_ERROR(IsValid(), ERROR_INVALID_STATE);
  164. if(IS_NO_ERROR(munmap(m_pv, m_dwSize)))
  165. {
  166. m_pv = INVALID_MAP_ADDR;
  167. m_dwSize = 0;
  168. return TRUE;
  169. }
  170. return FALSE;
  171. }
  172. BOOL CFileMapping::MSync(int iFlag, SIZE_T dwSize)
  173. {
  174. CHECK_ERROR(IsValid(), ERROR_INVALID_STATE);
  175. if(dwSize == 0) dwSize = m_dwSize;
  176. return IS_NO_ERROR(msync(m_pv, dwSize, iFlag));
  177. }