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