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