, __pCurDirEntry(null)
{
__validAccess = true;
- __absoluteDirPath = dirPath;
+ __dirPath = dirPath;
}
_DirEnumeratorImpl::_DirEnumeratorImpl(const _DirEnumeratorImpl& dirEnumeratorImpl)
{
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[]> pDirPath(_StringConverter::CopyToCharArrayN(String(__absoluteDirPath)));
+ 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()));
errno = 0;
struct dirent* pDirEnt = readdir((DIR*) __pFileFindInfo);
SysTryReturnResult(NID_IO, errno == 0, __ConvertNativeErrorToResult(errno),
- "Failed to the next directory entry. path: %s, errno: %d (%s)", pDirPath.get(), errno, strerror(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");
- char entryPath[PATH_MAX] = { 0, };
- strcpy(entryPath, pDirPath.get());
- strncat(entryPath, "/", 1);
- strncat(entryPath, pDirEnt->d_name, strlen(pDirEnt->d_name));
+ 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)
+ {
+ strncat(pEntryPath.get(), "/", 1);
+ }
+ strncat(pEntryPath.get(), pDirEnt->d_name, strlen(pDirEnt->d_name));
struct stat64 statbuf;
- int ret = stat64(entryPath, &statbuf);
- SysSecureTryReturnResult(NID_IO, ret == 0, r = __ConvertNativeErrorToResult(errno),
- "[%s] Failed to get attributes for directory entry (%s), errno: %d (%s)",
- entryPath, errno, strerror(errno));
+ 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));
- struct tm* pTm = localtime(&statbuf.st_mtime);
- SysTryReturnResult(NID_IO, pTm != null, E_SYSTEM, "Failed to call localtime() (%s).", strerror(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));
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);
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()));