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.
18 * @file FIo_DirEnumeratorImpl.cpp
19 * @brief This is the implementation file for _DirEnumeratorImpl class.
28 #include <unique_ptr.h>
30 #include <FBaseDateTime.h>
31 #include <FIoDirEntry.h>
32 #include <FIoDirEnumerator.h>
33 #include <FBaseSysLog.h>
35 #include <FBase_NativeError.h>
36 #include <FBase_StringConverter.h>
37 #include <FIo_DirEnumeratorImpl.h>
38 #include <FIo_DirEntryImpl.h>
41 using namespace Tizen::Base;
43 namespace Tizen { namespace Io
46 static const int _BASE_YEAR = 1900;
48 _DirEnumeratorImpl::_DirEnumeratorImpl(const String& dirPath)
49 : __validAccess(false)
50 , __pFileFindInfo(null)
51 , __pCurDirEntry(null)
57 _DirEnumeratorImpl::_DirEnumeratorImpl(const _DirEnumeratorImpl& dirEnumeratorImpl)
58 : __validAccess(false)
59 , __pFileFindInfo(null)
60 , __pCurDirEntry(null)
62 SysLogException(NID_IO, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] This copy constructor is not implemented.");
63 SetLastResult(E_UNSUPPORTED_OPERATION);
67 _DirEnumeratorImpl::operator =(const _DirEnumeratorImpl& dirEnumeratorImpl)
69 if (this != &dirEnumeratorImpl)
71 SysLogException(NID_IO, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] This assign operator is not implemented.");
72 SetLastResult(E_UNSUPPORTED_OPERATION);
77 _DirEnumeratorImpl::~_DirEnumeratorImpl(void)
81 closedir(static_cast <DIR*>(__pFileFindInfo));
82 __pFileFindInfo = null;
85 delete __pCurDirEntry;
89 _DirEnumeratorImpl::GetCurrentDirEntry(void) const
93 unique_ptr<DirEntry> pDirEntry(_DirEntryImpl::CreateDirEntryInstanceN());
94 DirEntry dirEntryObject = *pDirEntry;
96 SysTryReturn(NID_IO, __pCurDirEntry != null, dirEntryObject, E_INVALID_STATE,
97 "[E_INVALID_STATE] Instance has not been created yet.");
99 _DirEntryImpl::GetInstance(&dirEntryObject)->Set(__pCurDirEntry->GetDateTime(),
100 __pCurDirEntry->GetFileSize(), __pCurDirEntry->GetName(), __pCurDirEntry->IsDirectory(),
101 __pCurDirEntry->IsReadOnly(), __pCurDirEntry->IsHidden());
104 return dirEntryObject;
108 _DirEnumeratorImpl::GetCurrent(void) const
110 SetLastResult(E_SUCCESS);
111 return __pCurDirEntry;
115 _DirEnumeratorImpl::MoveNext(void)
117 SysTryReturnResult(NID_IO, __validAccess == true, E_ILLEGAL_ACCESS,
118 "Given path cannot be accessed!");
119 SysTryReturnResult(NID_IO, __dirPath.GetLength() > 0 && __dirPath.GetLength() <= PATH_MAX,
120 E_INVALID_ARG, "Invalid argument was passed. Given pattern length is not correct!");
121 result r = E_SUCCESS;
123 unique_ptr<char[]> pDirPath(_StringConverter::CopyToCharArrayN(String(__dirPath)));
124 SysTryReturn(NID_IO, pDirPath != null, GetLastResult(), GetLastResult(),
125 "[%s] Invalid argument was passed. Given pattern length is not correct!", GetErrorMessage(GetLastResult()));
127 if (__pFileFindInfo == null)
129 __pFileFindInfo = opendir(pDirPath.get());
130 if (__pFileFindInfo == null)
132 r = _NativeError::ConvertNativeErrorToResult(errno, true);
133 SysLog(NID_IO, "[%s] Failed to open directory (%s), [%s].", GetErrorMessage(r),
134 GetErrorMessage((result) E_FILE_NOT_FOUND), pDirPath.get());
140 struct dirent* pDirEnt = readdir((DIR*) __pFileFindInfo);
141 SysTryReturnResult(NID_IO, errno == 0, __ConvertNativeErrorToResult(errno),
142 "Failed to the next directory entry. parent: %s, errno: %d (%s)", pDirPath.get(), errno, strerror(errno));
143 SysTryReturnResult(NID_IO, pDirEnt != null, E_END_OF_FILE, "End of directory entries");
145 size_t len = strlen(pDirPath.get()) + strlen(pDirEnt->d_name) + 2;
146 unique_ptr<char[]> pEntryPath(new (std::nothrow) char[len]);
147 SysTryReturnResult(NID_IO, pEntryPath != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
149 memset(pEntryPath.get(), 0, len);
150 strcpy(pEntryPath.get(), pDirPath.get());
151 if (__dirPath.EndsWith(L"/") == false)
153 strncat(pEntryPath.get(), "/", 1);
155 strncat(pEntryPath.get(), pDirEnt->d_name, strlen(pDirEnt->d_name));
157 struct stat64 statbuf;
158 int ret = stat64(pEntryPath.get(), &statbuf);
159 SysSecureTryReturnResult(NID_IO, ret == 0, __ConvertNativeErrorToResult(errno),
160 "Failed to get attributes for directory entry (%s), errno: %d (%s)", pEntryPath.get(), errno, strerror(errno));
163 struct tm* pTm = localtime_r(&statbuf.st_mtime, &resultTm);
164 SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to get local time (%s).", strerror(errno));
167 r = dateTime.SetValue(_BASE_YEAR + pTm->tm_year, 1 + pTm->tm_mon, pTm->tm_mday, pTm->tm_hour, pTm->tm_min, pTm->tm_sec);
168 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
170 if (__pCurDirEntry == null)
172 __pCurDirEntry = _DirEntryImpl::CreateDirEntryInstanceN();
173 SysTryReturnResult(NID_IO, __pCurDirEntry != null, E_OUT_OF_MEMORY,
174 "DirEntry allocation failed.");
177 // check for a hidden file/directory including . and ..
179 if (pDirEnt->d_name[0] == '.')
184 _DirEntryImpl::GetInstance(__pCurDirEntry)->Set(dateTime, statbuf.st_size, String(pDirEnt->d_name),
185 S_ISDIR(statbuf.st_mode), (statbuf.st_mode & S_IWUSR) ? false : true, hidden);
191 _DirEnumeratorImpl::Reset(void)
193 result r = E_SUCCESS;
197 delete __pCurDirEntry;
198 __pCurDirEntry = null;
201 // close and reopen the dir
204 closedir(static_cast <DIR*>(__pFileFindInfo));
207 unique_ptr<char[]> pDirPathName(_StringConverter::CopyToCharArrayN(__dirPath));
208 SysTryReturn(NID_IO, pDirPathName != null, GetLastResult(), GetLastResult(),
209 "[%s] Failed to close file.", GetErrorMessage(GetLastResult()));
211 __pFileFindInfo = opendir(pDirPathName.get());
212 if (__pFileFindInfo == null)
214 r = __ConvertNativeErrorToResult(errno);
215 SysLog(NID_IO, "[%s] Failed to open directory (%s).", GetErrorMessage(r), pDirPathName.get());
222 _DirEnumeratorImpl::GetInstance(DirEnumerator& dirEnumerator)
224 return dirEnumerator.__pDirEnumeratorImpl;
227 const _DirEnumeratorImpl*
228 _DirEnumeratorImpl::GetInstance(const DirEnumerator& dirEnumerator)
230 return dirEnumerator.__pDirEnumeratorImpl;
234 _DirEnumeratorImpl::CreateDirEnumeratorInstanceN(const String& dirPath)
236 unique_ptr<DirEnumerator> pDirEnumerator(new (std::nothrow) DirEnumerator(dirPath));
237 SysTryReturn(NID_IO, pDirEnumerator != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
238 return pDirEnumerator.release();