2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This is the implementation file for File class.
24 #include <unique_ptr.h>
26 #include <FBaseResult.h>
27 #include <FBaseSysLog.h>
30 #include <FBase_NativeError.h>
31 #include <FApp_AppInfo.h>
32 #include "FIo_FileImpl.h"
35 using namespace Tizen::Base;
37 namespace Tizen { namespace Io
51 File::Construct(const String& filePath, const String& openMode, bool createParentDirsToo)
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 '/'.");
59 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
60 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
62 result r = pFileImpl->Construct(filePath, openMode, createParentDirsToo, null);
69 SysPropagate(NID_IO, r);
73 __pFileImpl = pFileImpl.release();
79 File::Construct(const String& filePath, const String& openMode)
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 '/'.");
87 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
88 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
90 result r = pFileImpl->Construct(filePath, openMode, false, null);
93 if (r == E_SYSTEM && Tizen::App::_AppInfo::IsOspCompat() == true)
97 SysPropagate(NID_IO, r);
101 __pFileImpl = pFileImpl.release();
107 File::Construct(const String& filePath, const char* pOpenMode)
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 '/'.");
115 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
116 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
118 result r = pFileImpl->Construct(filePath, pOpenMode, null);
119 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
121 __pFileImpl = pFileImpl.release();
127 File::Construct(const String& filePath, const char* pOpenMode, const ByteBuffer& key)
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 '/'.");
135 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
136 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
138 result r = pFileImpl->Construct(filePath, pOpenMode, &key);
139 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
141 __pFileImpl = pFileImpl.release();
147 File::Read(ByteBuffer& buffer)
149 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
150 return __pFileImpl->Read(buffer);
154 File::Read(void* buffer, int length)
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.");
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.");
168 File::Read(String& buffer)
170 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
171 return __pFileImpl->Read(buffer);
175 File::Write(const ByteBuffer& buffer)
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);
183 File::Write(const void* buffer, int length)
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.");
191 return __pFileImpl->Write(buffer, length);
195 File::Write(const String& buffer)
197 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
198 return __pFileImpl->Write(buffer);
204 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
205 return __pFileImpl->Flush();
209 File::Tell(void) const
211 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
212 return __pFileImpl->Tell();
216 File::Seek(FileSeekPosition position, long offset)
218 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
219 return __pFileImpl->Seek(position, offset);
223 File::Truncate(int length)
225 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
226 return __pFileImpl->Truncate(length);
230 File::GetName(void) const
232 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
233 return __pFileImpl->GetName();
237 File::Remove(const String& filePath)
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.");
242 return _FileImpl::Remove(filePath);
246 File::Move(const String& oldFilePath, const String& newFilePath)
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.");
253 return _FileImpl::Move(oldFilePath, newFilePath);
257 File::Copy(const String& srcFilePath, const String& destFilePath, bool failIfExist)
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.");
264 return _FileImpl::Copy(srcFilePath, destFilePath, failIfExist);
268 File::GetAttributes(const String& filePath, FileAttributes& attribute)
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.");
273 return _FileImpl::GetAttributes(filePath, attribute);
277 File::GetFileName(const String& filePath)
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.");
283 return _FileImpl::GetFileName(filePath);
287 File::GetFileExtension(const String& filePath)
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.");
293 return _FileImpl::GetFileExtension(filePath);
297 File::IsFileExist(const String& filePath)
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.");
302 return _FileImpl::IsFileExist(filePath);
306 File::ConvertToSecureFile(const String& plainFilePath, const String& secureFilePath, const ByteBuffer& key)
308 return _FileImpl::ConvertToSecureFile(plainFilePath, secureFilePath, key);
312 File::LockN(FileLockType lockType)
314 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
315 return __pFileImpl->LockN(lockType);
319 File::LockN(FileLockType lockType, int offset, int length)
321 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
322 return __pFileImpl->LockN(lockType, offset, length);
326 File::TryToLockN(FileLockType lockType)
328 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
329 return __pFileImpl->TryToLockN(lockType);
333 File::TryToLockN(FileLockType lockType, int offset, int length)
335 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
336 return __pFileImpl->TryToLockN(lockType, offset, length);