2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief This is the implementation file for File class.
25 #include <unique_ptr.h>
27 #include <FBaseResult.h>
28 #include <FBaseSysLog.h>
31 #include <FBase_NativeError.h>
32 #include <FApp_AppInfo.h>
33 #include "FIo_FileImpl.h"
36 using namespace Tizen::Base;
38 namespace Tizen { namespace Io
52 File::Construct(const String& filePath, const String& openMode, bool createParentDirsToo)
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 '/'.");
60 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
61 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
63 result r = pFileImpl->Construct(filePath, openMode, createParentDirsToo, null);
70 SysPropagate(NID_IO, r);
74 __pFileImpl = pFileImpl.release();
80 File::Construct(const String& filePath, const String& openMode)
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 '/'.");
88 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
89 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
91 result r = pFileImpl->Construct(filePath, openMode, false, null);
94 if (r == E_SYSTEM && Tizen::App::_AppInfo::IsOspCompat() == true)
98 SysPropagate(NID_IO, r);
102 __pFileImpl = pFileImpl.release();
108 File::Construct(const String& filePath, const char* pOpenMode)
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 '/'.");
116 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
117 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
119 result r = pFileImpl->Construct(filePath, pOpenMode, null);
120 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
122 __pFileImpl = pFileImpl.release();
128 File::Construct(const String& filePath, const char* pOpenMode, const ByteBuffer& key)
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 '/'.");
136 unique_ptr<_FileImpl> pFileImpl(new (std::nothrow) _FileImpl);
137 SysTryReturnResult(NID_IO, pFileImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
139 result r = pFileImpl->Construct(filePath, pOpenMode, &key);
140 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
142 __pFileImpl = pFileImpl.release();
148 File::Read(ByteBuffer& buffer)
150 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
151 return __pFileImpl->Read(buffer);
155 File::Read(void* buffer, int length)
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.");
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.");
169 File::Read(String& buffer)
171 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
172 return __pFileImpl->Read(buffer);
176 File::Write(const ByteBuffer& buffer)
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);
184 File::Write(const void* buffer, int length)
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.");
192 return __pFileImpl->Write(buffer, length);
196 File::Write(const String& buffer)
198 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
199 return __pFileImpl->Write(buffer);
205 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
206 return __pFileImpl->Flush();
210 File::Tell(void) const
212 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
213 return __pFileImpl->Tell();
217 File::Seek(FileSeekPosition position, long offset)
219 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
220 return __pFileImpl->Seek(position, offset);
224 File::Truncate(int length)
226 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
227 return __pFileImpl->Truncate(length);
231 File::GetName(void) const
233 SysAssertf(__pFileImpl != null, "Not yet constructed. Construct() should be called before use.\n");
234 return __pFileImpl->GetName();
238 File::Remove(const String& filePath)
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.");
243 return _FileImpl::Remove(filePath);
247 File::Move(const String& oldFilePath, const String& newFilePath)
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.");
254 return _FileImpl::Move(oldFilePath, newFilePath);
258 File::Copy(const String& srcFilePath, const String& destFilePath, bool failIfExist)
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.");
265 return _FileImpl::Copy(srcFilePath, destFilePath, failIfExist);
269 File::GetAttributes(const String& filePath, FileAttributes& attribute)
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.");
274 return _FileImpl::GetAttributes(filePath, attribute);
278 File::GetFileName(const String& filePath)
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.");
284 return _FileImpl::GetFileName(filePath);
288 File::GetFileExtension(const String& filePath)
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.");
294 return _FileImpl::GetFileExtension(filePath);
298 File::IsFileExist(const String& filePath)
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.");
303 return _FileImpl::IsFileExist(filePath);
307 File::ConvertToSecureFile(const String& plainFilePath, const String& secureFilePath, const ByteBuffer& key)
309 return _FileImpl::ConvertToSecureFile(plainFilePath, secureFilePath, key);