sync with master
[platform/framework/native/appfw.git] / src / io / FIoFile.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIoFile.cpp
20  * @brief       This is the implementation file for File class.
21  */
22
23 #include <new>
24 #include <limits.h>
25 #include <unique_ptr.h>
26
27 #include <FBaseResult.h>
28 #include <FBaseSysLog.h>
29 #include <FIoFile.h>
30
31 #include <FBase_NativeError.h>
32 #include <FApp_AppInfo.h>
33 #include "FIo_FileImpl.h"
34
35 using namespace std;
36 using namespace Tizen::Base;
37
38 namespace Tizen { namespace Io
39 {
40
41 File::File(void)
42         : __pFileImpl(null)
43 {
44 }
45
46 File::~File(void)
47 {
48         delete __pFileImpl;
49 }
50
51 result
52 File::Construct(const String& filePath, const String& openMode, bool createParentDirsToo)
53 {
54         SysAssertf(__pFileImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
55         SysTryReturnResult(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
56                            "The specified filePath length is zero or exceeds system limitations.");
57         SysTryReturnResult(NID_IO, filePath.EndsWith(L"/") == false, E_INVALID_ARG,
58                            "The specified filePath is not correct. It ends with '/'.");
59
60         unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
61         SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
62
63         result r = pFileImpl->Construct(filePath, openMode, createParentDirsToo, null);
64         if (IsFailed(r))
65         {
66                 if (r == E_SYSTEM)
67                 {
68                         r = E_IO;
69                 }
70                 SysPropagate(NID_IO, r);
71                 return r;
72         }
73
74         __pFileImpl = pFileImpl.release();
75
76         return E_SUCCESS;
77 }
78
79 result
80 File::Construct(const String& filePath, const String& openMode)
81 {
82         SysAssertf(__pFileImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
83         SysTryReturnResult(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
84                            "The specified filePath length is zero or exceeds system limitations.");
85         SysTryReturnResult(NID_IO, filePath.EndsWith(L"/") == false, E_INVALID_ARG,
86                            "The specified filePath is not correct. It ends with '/'.");
87
88         unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
89         SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
90
91         result r = pFileImpl->Construct(filePath, openMode, false, null);
92         if (IsFailed(r))
93         {
94                 if (r == E_SYSTEM && Tizen::App::_AppInfo::IsOspCompat() == true)
95                 {
96                         r = E_IO;
97                 }
98                 SysPropagate(NID_IO, r);
99                 return r;
100         }
101
102         __pFileImpl = pFileImpl.release();
103
104         return E_SUCCESS;
105 }
106
107 result
108 File::Construct(const String& filePath, const char* pOpenMode)
109 {
110         SysAssertf(__pFileImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
111         SysTryReturnResult(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
112                            "The specified filePath length is zero or exceeds system limitations.");
113         SysTryReturnResult(NID_IO, filePath.EndsWith(L"/") == false, E_INVALID_ARG,
114                            "The specified filePath is not correct. It ends with '/'.");
115
116         unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
117         SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
118
119         result r = pFileImpl->Construct(filePath, pOpenMode, null);
120         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
121
122         __pFileImpl = pFileImpl.release();
123
124         return E_SUCCESS;
125 }
126
127 result
128 File::Construct(const String& filePath, const char* pOpenMode, const ByteBuffer& key)
129 {
130         SysAssertf(__pFileImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
131         SysTryReturnResult(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
132                            "The specified filePath length is zero or exceeds system limitations.");
133         SysTryReturnResult(NID_IO, filePath.EndsWith(L"/") == false, E_INVALID_ARG,
134                            "The specified filePath is not correct. It ends with '/'.");
135
136         unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
137         SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
138
139         result r = pFileImpl->Construct(filePath, pOpenMode, &key);
140         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
141
142         __pFileImpl = pFileImpl.release();
143
144         return E_SUCCESS;
145 }
146
147 result
148 File::Read(ByteBuffer& buffer)
149 {
150         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
151         return __pFileImpl->Read(buffer);
152 }
153
154 int
155 File::Read(void* buffer, int length)
156 {
157         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
158         SysTryReturnResult(NID_IO, buffer, E_INVALID_ARG, "The specified buffer is null.");
159         SysTryReturnResult(NID_IO, length > 0, E_INVALID_ARG, "The specified length is zero or exceeds system limitations.");
160
161         int readBytes =  __pFileImpl->Read(buffer, length);
162         SysTryReturn(NID_IO, GetLastResult() != E_END_OF_FILE, 0, E_END_OF_FILE,
163                         "[E_END_OF_FILE] The end of file is reached.");
164
165     return readBytes;
166 }
167
168 result
169 File::Read(String& buffer)
170 {
171         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
172         return __pFileImpl->Read(buffer);
173 }
174
175 result
176 File::Write(const ByteBuffer& buffer)
177 {
178         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
179         SysTryReturnResult(NID_IO, buffer.GetPointer() != null && buffer.GetLimit() > 0, E_INVALID_ARG, "The specified buffer is invalid.");
180         return __pFileImpl->Write(buffer);
181 }
182
183 result
184 File::Write(const void* buffer, int length)
185 {
186         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
187         SysTryReturnResult(NID_IO, length > 0, E_INVALID_ARG,
188                            "The specified length is zero or exceeds system limitations.");
189         SysTryReturnResult(NID_IO, buffer, E_INVALID_ARG,
190                            "The specified buffer is null.");
191
192         return __pFileImpl->Write(buffer, length);
193 }
194
195 result
196 File::Write(const String& buffer)
197 {
198         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
199         return __pFileImpl->Write(buffer);
200 }
201
202 result
203 File::Flush(void)
204 {
205         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
206         return __pFileImpl->Flush();
207 }
208
209 int
210 File::Tell(void) const
211 {
212         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
213         return __pFileImpl->Tell();
214 }
215
216 result
217 File::Seek(FileSeekPosition position, long offset)
218 {
219         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
220         return __pFileImpl->Seek(position, offset);
221 }
222
223 result
224 File::Truncate(int length)
225 {
226         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
227         return __pFileImpl->Truncate(length);
228 }
229
230 String
231 File::GetName(void) const
232 {
233         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
234         return __pFileImpl->GetName();
235 }
236
237 result
238 File::Remove(const String& filePath)
239 {
240         SysTryReturnResult(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
241                            "The specified filePath length is zero or exceeds system limitations.");
242
243         return _FileImpl::Remove(filePath);
244 }
245
246 result
247 File::Move(const String& oldFilePath, const String& newFilePath)
248 {
249         SysTryReturnResult(NID_IO, oldFilePath.GetLength() > 0 && oldFilePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
250                            "The specified oldFilePath length is zero or exceeds system limitations.");
251         SysTryReturnResult(NID_IO, newFilePath.GetLength() > 0 && newFilePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
252                            "Given newFilePath length is zero or exceeds system limitations.");
253
254         return _FileImpl::Move(oldFilePath, newFilePath);
255 }
256
257 result
258 File::Copy(const String& srcFilePath, const String& destFilePath, bool failIfExist)
259 {
260         SysTryReturnResult(NID_IO, srcFilePath.GetLength() > 0 && srcFilePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
261                            "The specified rcFilePath length is zero or exceeds system limitations.");
262         SysTryReturnResult(NID_IO, destFilePath.GetLength() > 0 && destFilePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
263                            "The specified destFilePath length is zero or exceeds system limitations.");
264
265         return _FileImpl::Copy(srcFilePath, destFilePath, failIfExist);
266 }
267
268 result
269 File::GetAttributes(const String& filePath, FileAttributes& attribute)
270 {
271         SysTryReturnResult(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, E_INVALID_ARG,
272                         "The length of the specified filePath is zero or exceeds system limitations.");
273
274         return _FileImpl::GetAttributes(filePath, attribute);
275 }
276
277 String
278 File::GetFileName(const String& filePath)
279 {
280         String name;
281         SysTryReturn(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, name, E_INVALID_ARG,
282                         "[E_INVALID_ARG] The length of the specified filePath is zero or exceeds system limitations.");
283
284         return _FileImpl::GetFileName(filePath);
285 }
286
287 String
288 File::GetFileExtension(const String& filePath)
289 {
290         String extName;
291         SysTryReturn(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, extName, E_INVALID_ARG,
292                         "[E_INVALID_ARG] The length of the specified filePath is zero or exceeds system limitations.");
293
294         return _FileImpl::GetFileExtension(filePath);
295 }
296
297 bool
298 File::IsFileExist(const String& filePath)
299 {
300         SysTryReturn(NID_IO, filePath.GetLength() > 0 && filePath.GetLength() <= PATH_MAX, false, E_INVALID_ARG,
301                            "[E_INVALID_ARG] Given filePath length is zero or exceeds system limitations.");
302
303         return _FileImpl::IsFileExist(filePath);
304 }
305
306 result
307 File::ConvertToSecureFile(const String& plainFilePath, const String& secureFilePath, const ByteBuffer& key)
308 {
309         return _FileImpl::ConvertToSecureFile(plainFilePath, secureFilePath, key);
310 }
311
312 FileLock*
313 File::LockN(FileLockType lockType)
314 {
315         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
316         return __pFileImpl->LockN(lockType);
317 }
318
319 FileLock*
320 File::LockN(FileLockType lockType, int offset, int length)
321 {
322         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
323         return __pFileImpl->LockN(lockType, offset, length);
324 }
325
326 FileLock*
327 File::TryToLockN(FileLockType lockType)
328 {
329         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
330         return __pFileImpl->TryToLockN(lockType);
331 }
332
333 FileLock*
334 File::TryToLockN(FileLockType lockType, int offset, int length)
335 {
336         SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
337         return __pFileImpl->TryToLockN(lockType, offset, length);
338 }
339
340 }} // Tizen::Io
341