| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
| 8 #include "fxcrt_posix.h" | 8 #include "fxcrt_posix.h" |
| 9 | 9 |
| 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || _FXM_PLATFORM_ == _FXM_PLATFORM_AP
PLE_ || _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ | 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \ |
| 11 IFXCRT_FileAccess* FXCRT_FileAccess_Create() | 11 _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \ |
| 12 { | 12 _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_ |
| 13 return new CFXCRT_FileAccess_Posix; | 13 IFXCRT_FileAccess* FXCRT_FileAccess_Create() { |
| 14 return new CFXCRT_FileAccess_Posix; |
| 14 } | 15 } |
| 15 void FXCRT_Posix_GetFileMode(FX_DWORD dwModes, int32_t &nFlags, int32_t &nMasks) | 16 void FXCRT_Posix_GetFileMode(FX_DWORD dwModes, |
| 16 { | 17 int32_t& nFlags, |
| 17 nFlags = O_BINARY | O_LARGEFILE; | 18 int32_t& nMasks) { |
| 18 if (dwModes & FX_FILEMODE_ReadOnly) { | 19 nFlags = O_BINARY | O_LARGEFILE; |
| 19 nFlags |= O_RDONLY; | 20 if (dwModes & FX_FILEMODE_ReadOnly) { |
| 20 nMasks = 0; | 21 nFlags |= O_RDONLY; |
| 21 } else { | 22 nMasks = 0; |
| 22 nFlags |= O_RDWR | O_CREAT; | 23 } else { |
| 23 if (dwModes & FX_FILEMODE_Truncate) { | 24 nFlags |= O_RDWR | O_CREAT; |
| 24 nFlags |= O_TRUNC; | 25 if (dwModes & FX_FILEMODE_Truncate) { |
| 25 } | 26 nFlags |= O_TRUNC; |
| 26 nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; | |
| 27 } | 27 } |
| 28 nMasks = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 29 } |
| 28 } | 30 } |
| 29 CFXCRT_FileAccess_Posix::CFXCRT_FileAccess_Posix() | 31 CFXCRT_FileAccess_Posix::CFXCRT_FileAccess_Posix() : m_nFD(-1) {} |
| 30 : m_nFD(-1) | 32 CFXCRT_FileAccess_Posix::~CFXCRT_FileAccess_Posix() { |
| 31 { | 33 Close(); |
| 32 } | 34 } |
| 33 CFXCRT_FileAccess_Posix::~CFXCRT_FileAccess_Posix() | 35 FX_BOOL CFXCRT_FileAccess_Posix::Open(const CFX_ByteStringC& fileName, |
| 34 { | 36 FX_DWORD dwMode) { |
| 35 Close(); | 37 if (m_nFD > -1) { |
| 38 return FALSE; |
| 39 } |
| 40 int32_t nFlags, nMasks; |
| 41 FXCRT_Posix_GetFileMode(dwMode, nFlags, nMasks); |
| 42 m_nFD = open(fileName.GetCStr(), nFlags, nMasks); |
| 43 return m_nFD > -1; |
| 36 } | 44 } |
| 37 FX_BOOL CFXCRT_FileAccess_Posix::Open(const CFX_ByteStringC& fileName, FX_DWORD
dwMode) | 45 FX_BOOL CFXCRT_FileAccess_Posix::Open(const CFX_WideStringC& fileName, |
| 38 { | 46 FX_DWORD dwMode) { |
| 39 if (m_nFD > -1) { | 47 return Open(FX_UTF8Encode(fileName), dwMode); |
| 40 return FALSE; | 48 } |
| 49 void CFXCRT_FileAccess_Posix::Close() { |
| 50 if (m_nFD < 0) { |
| 51 return; |
| 52 } |
| 53 close(m_nFD); |
| 54 m_nFD = -1; |
| 55 } |
| 56 void CFXCRT_FileAccess_Posix::Release() { |
| 57 delete this; |
| 58 } |
| 59 FX_FILESIZE CFXCRT_FileAccess_Posix::GetSize() const { |
| 60 if (m_nFD < 0) { |
| 61 return 0; |
| 62 } |
| 63 struct stat s; |
| 64 FXSYS_memset(&s, 0, sizeof(s)); |
| 65 fstat(m_nFD, &s); |
| 66 return s.st_size; |
| 67 } |
| 68 FX_FILESIZE CFXCRT_FileAccess_Posix::GetPosition() const { |
| 69 if (m_nFD < 0) { |
| 70 return (FX_FILESIZE)-1; |
| 71 } |
| 72 return lseek(m_nFD, 0, SEEK_CUR); |
| 73 } |
| 74 FX_FILESIZE CFXCRT_FileAccess_Posix::SetPosition(FX_FILESIZE pos) { |
| 75 if (m_nFD < 0) { |
| 76 return (FX_FILESIZE)-1; |
| 77 } |
| 78 return lseek(m_nFD, pos, SEEK_SET); |
| 79 } |
| 80 size_t CFXCRT_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer) { |
| 81 if (m_nFD < 0) { |
| 82 return 0; |
| 83 } |
| 84 return read(m_nFD, pBuffer, szBuffer); |
| 85 } |
| 86 size_t CFXCRT_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer) { |
| 87 if (m_nFD < 0) { |
| 88 return 0; |
| 89 } |
| 90 return write(m_nFD, pBuffer, szBuffer); |
| 91 } |
| 92 size_t CFXCRT_FileAccess_Posix::ReadPos(void* pBuffer, |
| 93 size_t szBuffer, |
| 94 FX_FILESIZE pos) { |
| 95 if (m_nFD < 0) { |
| 96 return 0; |
| 97 } |
| 98 if (pos >= GetSize()) { |
| 99 return 0; |
| 100 } |
| 101 if (SetPosition(pos) == (FX_FILESIZE)-1) { |
| 102 return 0; |
| 103 } |
| 104 return Read(pBuffer, szBuffer); |
| 105 } |
| 106 size_t CFXCRT_FileAccess_Posix::WritePos(const void* pBuffer, |
| 107 size_t szBuffer, |
| 108 FX_FILESIZE pos) { |
| 109 if (m_nFD < 0) { |
| 110 return 0; |
| 111 } |
| 112 if (SetPosition(pos) == (FX_FILESIZE)-1) { |
| 113 return 0; |
| 114 } |
| 115 return Write(pBuffer, szBuffer); |
| 116 } |
| 117 FX_BOOL CFXCRT_FileAccess_Posix::Flush() { |
| 118 if (m_nFD < 0) { |
| 119 return FALSE; |
| 120 } |
| 121 return fsync(m_nFD) > -1; |
| 122 } |
| 123 FX_BOOL CFXCRT_FileAccess_Posix::Truncate(FX_FILESIZE szFile) { |
| 124 if (m_nFD < 0) { |
| 125 return FALSE; |
| 126 } |
| 127 return !ftruncate(m_nFD, szFile); |
| 128 } |
| 129 FX_BOOL FX_File_Exist(const CFX_ByteStringC& fileName) { |
| 130 return access(fileName.GetCStr(), F_OK) > -1; |
| 131 } |
| 132 FX_BOOL FX_File_Exist(const CFX_WideStringC& fileName) { |
| 133 return FX_File_Exist(FX_UTF8Encode(fileName)); |
| 134 } |
| 135 FX_BOOL FX_File_Delete(const CFX_ByteStringC& fileName) { |
| 136 return remove(fileName.GetCStr()) > -1; |
| 137 } |
| 138 FX_BOOL FX_File_Delete(const CFX_WideStringC& fileName) { |
| 139 return FX_File_Delete(FX_UTF8Encode(fileName)); |
| 140 } |
| 141 FX_BOOL FX_File_Copy(const CFX_ByteStringC& fileNameSrc, |
| 142 const CFX_ByteStringC& fileNameDst) { |
| 143 CFXCRT_FileAccess_Posix src, dst; |
| 144 if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) { |
| 145 return FALSE; |
| 146 } |
| 147 FX_FILESIZE size = src.GetSize(); |
| 148 if (!size) { |
| 149 return FALSE; |
| 150 } |
| 151 if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) { |
| 152 return FALSE; |
| 153 } |
| 154 size_t num = 0; |
| 155 uint8_t* pBuffer = FX_Alloc(uint8_t, 32768); |
| 156 num = src.Read(pBuffer, 32768); |
| 157 while (num) { |
| 158 if (dst.Write(pBuffer, num) != num) { |
| 159 break; |
| 41 } | 160 } |
| 42 int32_t nFlags, nMasks; | 161 num = src.Read(pBuffer, 32768); |
| 43 FXCRT_Posix_GetFileMode(dwMode, nFlags, nMasks); | 162 } |
| 44 m_nFD = open(fileName.GetCStr(), nFlags, nMasks); | 163 FX_Free(pBuffer); |
| 45 return m_nFD > -1; | 164 return TRUE; |
| 46 } | 165 } |
| 47 FX_BOOL CFXCRT_FileAccess_Posix::Open(const CFX_WideStringC& fileName, FX_DWORD
dwMode) | 166 FX_BOOL FX_File_Copy(const CFX_WideStringC& fileNameSrc, |
| 48 { | 167 const CFX_WideStringC& fileNameDst) { |
| 49 return Open(FX_UTF8Encode(fileName), dwMode); | 168 return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); |
| 50 } | 169 } |
| 51 void CFXCRT_FileAccess_Posix::Close() | 170 FX_BOOL FX_File_Move(const CFX_ByteStringC& fileNameSrc, |
| 52 { | 171 const CFX_ByteStringC& fileNameDst) { |
| 53 if (m_nFD < 0) { | 172 return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); |
| 54 return; | |
| 55 } | |
| 56 close(m_nFD); | |
| 57 m_nFD = -1; | |
| 58 } | 173 } |
| 59 void CFXCRT_FileAccess_Posix::Release() | 174 FX_BOOL FX_File_Move(const CFX_WideStringC& fileNameSrc, |
| 60 { | 175 const CFX_WideStringC& fileNameDst) { |
| 61 delete this; | 176 return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); |
| 62 } | |
| 63 FX_FILESIZE CFXCRT_FileAccess_Posix::GetSize() const | |
| 64 { | |
| 65 if (m_nFD < 0) { | |
| 66 return 0; | |
| 67 } | |
| 68 struct stat s; | |
| 69 FXSYS_memset(&s, 0, sizeof(s)); | |
| 70 fstat(m_nFD, &s); | |
| 71 return s.st_size; | |
| 72 } | |
| 73 FX_FILESIZE CFXCRT_FileAccess_Posix::GetPosition() const | |
| 74 { | |
| 75 if (m_nFD < 0) { | |
| 76 return (FX_FILESIZE) - 1; | |
| 77 } | |
| 78 return lseek(m_nFD, 0, SEEK_CUR); | |
| 79 } | |
| 80 FX_FILESIZE CFXCRT_FileAccess_Posix::SetPosition(FX_FILESIZE pos) | |
| 81 { | |
| 82 if (m_nFD < 0) { | |
| 83 return (FX_FILESIZE) - 1; | |
| 84 } | |
| 85 return lseek(m_nFD, pos, SEEK_SET); | |
| 86 } | |
| 87 size_t CFXCRT_FileAccess_Posix::Read(void* pBuffer, size_t szBuffer) | |
| 88 { | |
| 89 if (m_nFD < 0) { | |
| 90 return 0; | |
| 91 } | |
| 92 return read(m_nFD, pBuffer, szBuffer); | |
| 93 } | |
| 94 size_t CFXCRT_FileAccess_Posix::Write(const void* pBuffer, size_t szBuffer) | |
| 95 { | |
| 96 if (m_nFD < 0) { | |
| 97 return 0; | |
| 98 } | |
| 99 return write(m_nFD, pBuffer, szBuffer); | |
| 100 } | |
| 101 size_t CFXCRT_FileAccess_Posix::ReadPos(void* pBuffer, size_t szBuffer, FX_FILES
IZE pos) | |
| 102 { | |
| 103 if (m_nFD < 0) { | |
| 104 return 0; | |
| 105 } | |
| 106 if (pos >= GetSize()) { | |
| 107 return 0; | |
| 108 } | |
| 109 if (SetPosition(pos) == (FX_FILESIZE) - 1) { | |
| 110 return 0; | |
| 111 } | |
| 112 return Read(pBuffer, szBuffer); | |
| 113 } | |
| 114 size_t CFXCRT_FileAccess_Posix::WritePos(const void* pBuffer, size_t szBuffer, F
X_FILESIZE pos) | |
| 115 { | |
| 116 if (m_nFD < 0) { | |
| 117 return 0; | |
| 118 } | |
| 119 if (SetPosition(pos) == (FX_FILESIZE) - 1) { | |
| 120 return 0; | |
| 121 } | |
| 122 return Write(pBuffer, szBuffer); | |
| 123 } | |
| 124 FX_BOOL CFXCRT_FileAccess_Posix::Flush() | |
| 125 { | |
| 126 if (m_nFD < 0) { | |
| 127 return FALSE; | |
| 128 } | |
| 129 return fsync(m_nFD) > -1; | |
| 130 } | |
| 131 FX_BOOL CFXCRT_FileAccess_Posix::Truncate(FX_FILESIZE szFile) | |
| 132 { | |
| 133 if (m_nFD < 0) { | |
| 134 return FALSE; | |
| 135 } | |
| 136 return !ftruncate(m_nFD, szFile); | |
| 137 } | |
| 138 FX_BOOL FX_File_Exist(const CFX_ByteStringC& fileName) | |
| 139 { | |
| 140 return access(fileName.GetCStr(), F_OK) > -1; | |
| 141 } | |
| 142 FX_BOOL FX_File_Exist(const CFX_WideStringC& fileName) | |
| 143 { | |
| 144 return FX_File_Exist(FX_UTF8Encode(fileName)); | |
| 145 } | |
| 146 FX_BOOL FX_File_Delete(const CFX_ByteStringC& fileName) | |
| 147 { | |
| 148 return remove(fileName.GetCStr()) > -1; | |
| 149 } | |
| 150 FX_BOOL FX_File_Delete(const CFX_WideStringC& fileName) | |
| 151 { | |
| 152 return FX_File_Delete(FX_UTF8Encode(fileName)); | |
| 153 } | |
| 154 FX_BOOL FX_File_Copy(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC&
fileNameDst) | |
| 155 { | |
| 156 CFXCRT_FileAccess_Posix src, dst; | |
| 157 if (!src.Open(fileNameSrc, FX_FILEMODE_ReadOnly)) { | |
| 158 return FALSE; | |
| 159 } | |
| 160 FX_FILESIZE size = src.GetSize(); | |
| 161 if (!size) { | |
| 162 return FALSE; | |
| 163 } | |
| 164 if (!dst.Open(fileNameDst, FX_FILEMODE_Truncate)) { | |
| 165 return FALSE; | |
| 166 } | |
| 167 size_t num = 0; | |
| 168 uint8_t* pBuffer = FX_Alloc(uint8_t, 32768); | |
| 169 num = src.Read(pBuffer, 32768); | |
| 170 while (num) { | |
| 171 if (dst.Write(pBuffer, num) != num) { | |
| 172 break; | |
| 173 } | |
| 174 num = src.Read(pBuffer, 32768); | |
| 175 } | |
| 176 FX_Free(pBuffer); | |
| 177 return TRUE; | |
| 178 } | |
| 179 FX_BOOL FX_File_Copy(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC&
fileNameDst) | |
| 180 { | |
| 181 return FX_File_Copy(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); | |
| 182 } | |
| 183 FX_BOOL FX_File_Move(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC&
fileNameDst) | |
| 184 { | |
| 185 return rename(fileNameSrc.GetCStr(), fileNameDst.GetCStr()); | |
| 186 } | |
| 187 FX_BOOL FX_File_Move(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC&
fileNameDst) | |
| 188 { | |
| 189 return FX_File_Move(FX_UTF8Encode(fileNameSrc), FX_UTF8Encode(fileNameDst)); | |
| 190 } | 177 } |
| 191 #endif | 178 #endif |
| OLD | NEW |