Add conditional statement for removing platform log
[platform/framework/native/appfw.git] / src / io / FIo_DirEnumeratorImpl.cpp
index 032bbc6..d3ce322 100644 (file)
@@ -51,7 +51,7 @@ _DirEnumeratorImpl::_DirEnumeratorImpl(const String& dirPath)
        , __pCurDirEntry(null)
 {
        __validAccess = true;
-       __absoluteDirPath = dirPath;
+       __dirPath = dirPath;
 }
 
 _DirEnumeratorImpl::_DirEnumeratorImpl(const _DirEnumeratorImpl& dirEnumeratorImpl)
@@ -114,71 +114,56 @@ _DirEnumeratorImpl::GetCurrent(void) const
 result
 _DirEnumeratorImpl::MoveNext(void)
 {
-       result r = E_SUCCESS;
-       struct dirent dirEnt;
-       struct dirent* pDirEntResult = null;
-       struct stat64 statbuf;
-       struct tm* pTm = null;
-       DateTime dateTime;
-       int ret = 0;
-       String dirEntryPath;
-       bool hidden = false;
-       char* pDirEntryName = null;
-
        SysTryReturnResult(NID_IO, __validAccess == true, E_ILLEGAL_ACCESS,
                        "Given path cannot be accessed!");
-       SysTryReturnResult(NID_IO, __absoluteDirPath.GetLength() > 0 && __absoluteDirPath.GetLength() <= PATH_MAX,
+       SysTryReturnResult(NID_IO, __dirPath.GetLength() > 0 && __dirPath.GetLength() <= PATH_MAX,
                        E_INVALID_ARG, "Invalid argument was passed. Given pattern length is not correct!");
+       result r = E_SUCCESS;
 
-       unique_ptr<char[]> pAbsDirPath(_StringConverter::CopyToCharArrayN(String(__absoluteDirPath)));
-       SysTryReturn(NID_IO, (pAbsDirPath != null), GetLastResult(), GetLastResult(),
+       unique_ptr<char[]> pDirPath(_StringConverter::CopyToCharArrayN(String(__dirPath)));
+       SysTryReturn(NID_IO, pDirPath != null, GetLastResult(), GetLastResult(),
                        "[%s] Invalid argument was passed. Given pattern length is not correct!", GetErrorMessage(GetLastResult()));
 
        if (__pFileFindInfo == null)
        {
-               __pFileFindInfo = opendir(pAbsDirPath.get());
+               __pFileFindInfo = opendir(pDirPath.get());
                if (__pFileFindInfo == null)
                {
                        r = _NativeError::ConvertNativeErrorToResult(errno, true);
                        SysLog(NID_IO, "[%s] Failed to open directory (%s), [%s].", GetErrorMessage(r),
-                                               GetErrorMessage((result) E_FILE_NOT_FOUND), pAbsDirPath.get());
+                                               GetErrorMessage((result) E_FILE_NOT_FOUND), pDirPath.get());
                        return r;
                }
        }
 
-//SKIP_DIR:
-       // move to next entry in the directory
        errno = 0;
-       ret = readdir_r((DIR*) __pFileFindInfo, &dirEnt, &pDirEntResult);
-       SysTryReturn(NID_IO, (ret == 0), __ConvertNativeErrorToResult(errno),
-                       __ConvertNativeErrorToResult(errno), "[%s] Failed to read next dir entry.", __ConvertNativeErrorToMessage(errno));
-
-       if (pDirEntResult == null)
+       struct dirent* pDirEnt = readdir((DIR*) __pFileFindInfo);
+       SysTryReturnResult(NID_IO, errno == 0, __ConvertNativeErrorToResult(errno),
+                       "Failed to the next directory entry. parent: %s, errno: %d (%s)", pDirPath.get(), errno, strerror(errno));
+       SysTryReturnResult(NID_IO, pDirEnt != null, E_END_OF_FILE, "End of directory entries");
+
+       size_t len = strlen(pDirPath.get()) + strlen(pDirEnt->d_name) + 2;
+       unique_ptr<char[]> pEntryPath(new (std::nothrow) char[len]);
+       SysTryReturnResult(NID_IO, pEntryPath != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
+
+       memset(pEntryPath.get(), 0, len);
+       strcpy(pEntryPath.get(), pDirPath.get());
+       if (__dirPath.EndsWith(L"/") == false)
        {
-               return E_END_OF_FILE; // Complete reading the directory
+               strncat(pEntryPath.get(), "/", 1);
        }
+       strncat(pEntryPath.get(), pDirEnt->d_name, strlen(pDirEnt->d_name));
 
-       //if (dirEnt.d_name[0] == '.')
-       //{
-       //      goto SKIP_DIR;
-       //}
-
-       // prepare entry path
-       dirEntryPath.Append(__absoluteDirPath);
-       dirEntryPath.Append(L"/");
-       dirEntryPath.Append(dirEnt.d_name);
-       unique_ptr<char[]> pAbsDirEntryPath(_StringConverter::CopyToCharArrayN(dirEntryPath));
-       SysTryReturn(NID_IO, (pAbsDirEntryPath != null), GetLastResult(), GetLastResult(),
-                       "[%s] Invalid argument was passed. Given pattern length is not correct!", GetErrorMessage(GetLastResult()));
+       struct stat64 statbuf;
+       int ret = stat64(pEntryPath.get(), &statbuf);
+       SysSecureTryReturnResult(NID_IO, ret == 0, __ConvertNativeErrorToResult(errno),
+                       "Failed to get attributes for directory entry (%s), errno: %d (%s)", pEntryPath.get(), errno, strerror(errno));
 
-       // get its details
-       ret = stat64(pAbsDirEntryPath.get(), &statbuf);
-       SysTryReturn(NID_IO, (ret == 0), r = __ConvertNativeErrorToResult(errno), __ConvertNativeErrorToResult(errno),
-                       "[%s] Failed to get attributes for dir entry (%s) whose absolute dirpath is (%s)with errno [%d].",
-                       __ConvertNativeErrorToMessage(errno), dirEnt.d_name, pAbsDirPath.get(), errno);
+       struct tm resultTm;
+       struct tm* pTm = localtime_r(&statbuf.st_mtime, &resultTm);
+       SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to get local time (%s).", strerror(errno));
 
-       pTm = localtime(&statbuf.st_mtime);
-       SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to call localtime() (%s).", strerror(errno));
+       DateTime dateTime;
        r = dateTime.SetValue(_BASE_YEAR + pTm->tm_year, 1 + pTm->tm_mon, pTm->tm_mday, pTm->tm_hour, pTm->tm_min, pTm->tm_sec);
        SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
 
@@ -189,15 +174,14 @@ _DirEnumeratorImpl::MoveNext(void)
                                   "DirEntry allocation failed.");
        }
 
-       pDirEntryName = &dirEnt.d_name[0];
-
        // check for a hidden file/directory including . and ..
-       if (dirEnt.d_name[0] == '.')
+       bool hidden = false;
+       if (pDirEnt->d_name[0] == '.')
        {
                hidden = true;
        }
 
-       _DirEntryImpl::GetInstance(__pCurDirEntry)->Set(dateTime, statbuf.st_size, String(dirEnt.d_name),
+       _DirEntryImpl::GetInstance(__pCurDirEntry)->Set(dateTime, statbuf.st_size, String(pDirEnt->d_name),
                        S_ISDIR(statbuf.st_mode), (statbuf.st_mode & S_IWUSR) ? false : true, hidden);
 
        return E_SUCCESS;
@@ -220,7 +204,7 @@ _DirEnumeratorImpl::Reset(void)
                closedir(static_cast <DIR*>(__pFileFindInfo));
        }
 
-       unique_ptr<char[]> pDirPathName(_StringConverter::CopyToCharArrayN(__absoluteDirPath));
+       unique_ptr<char[]> pDirPathName(_StringConverter::CopyToCharArrayN(__dirPath));
        SysTryReturn(NID_IO, pDirPathName != null, GetLastResult(), GetLastResult(),
                        "[%s] Failed to close file.", GetErrorMessage(GetLastResult()));