b3aabb3bce94a521d087c12758e8dbba135f52bd
[platform/framework/native/appfw.git] / src / io / FIoDirectory.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                FIoDirectory.cpp
20  * @brief               This is the implementation file for Directory class.
21  */
22
23 #include <limits.h>
24 #include <unique_ptr.h>
25
26 #include <FIoDirectory.h>
27 #include <FBaseSysLog.h>
28
29 #include <FBase_NativeError.h>
30 #include <FIo_DirectoryImpl.h>
31
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Io
35 {
36
37 Directory::Directory(void)
38         : __pDirectoryImpl(null)
39 {
40 }
41
42 Directory::~Directory(void)
43 {
44         delete __pDirectoryImpl;
45 }
46
47 result
48 Directory::Construct(const String& dirPath)
49 {
50         result r = E_SUCCESS;
51
52         SysTryReturnResult(NID_IO, dirPath.GetLength() > 0 && dirPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
53                            ("Given directory name length is not correct!"));
54
55         SysAssertf(__pDirectoryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
56
57         std::unique_ptr<_DirectoryImpl> pDirectoryImpl(new (std::nothrow) _DirectoryImpl);
58         SysTryReturnResult(NID_IO, pDirectoryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
59
60         r = pDirectoryImpl->Construct(dirPath);
61         SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
62
63         __pDirectoryImpl = pDirectoryImpl.release();
64
65         return E_SUCCESS;
66 }
67
68 DirEnumerator*
69 Directory::ReadN(void)
70 {
71         SysAssertf(__pDirectoryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
72         return __pDirectoryImpl->ReadN();
73 }
74
75 result
76 Directory::Create(const String& dirPath, bool createParentDirsToo)
77 {
78         SysTryReturnResult(NID_IO, dirPath.GetLength() > 0 && dirPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
79                            ("Given directory name length is not correct!"));
80         return _DirectoryImpl::Create(dirPath, createParentDirsToo);
81 }
82
83 result
84 Directory::Remove(const String& dirPath, bool recursive)
85 {
86         SysTryReturnResult(NID_IO, dirPath.GetLength() > 0 && dirPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
87                            ("Given file name length is not correct!"));
88         return _DirectoryImpl::Remove(dirPath, recursive);
89 }
90
91 result
92 Directory::Rename(const String& orgDirPath, const String& newDirPath)
93 {
94         SysTryReturnResult(NID_IO, orgDirPath.GetLength() > 0 && orgDirPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
95                            ("Given file name length is not correct!"));
96         SysTryReturnResult(NID_IO, newDirPath.GetLength() > 0 && newDirPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
97                            ("Given file name length is not correct!"));
98         return _DirectoryImpl::Rename(orgDirPath, newDirPath);
99 }
100
101 }} // Tizen::Io
102