}\r
else if((DirEntryIndexToOffset(ulDeleteIdx) + DIRENT_SIZE) == pPInode->pInodeBuf->ullSize)\r
{\r
- uint32_t ulTruncIdx = ulDeleteIdx;\r
+ /* Start searching one behind the index to be deleted.\r
+ */\r
+ uint32_t ulTruncIdx = ulDeleteIdx - 1U;\r
bool fDone = false;\r
\r
/* We are deleting the last dirent in the directory, so search\r
backwards to find the last populated dirent, allowing us to truncate\r
the directory to that point.\r
*/\r
- while((ret == 0) && (ulTruncIdx > 0U) && !fDone)\r
+ while((ret == 0) && (ulTruncIdx != UINT32_MAX) && !fDone)\r
{\r
ret = RedInodeDataSeekAndRead(pPInode, ulTruncIdx / DIRENTS_PER_BLOCK);\r
\r
if(ret == 0)\r
{\r
const DIRENT *pDirents = CAST_CONST_DIRENT_PTR(pPInode->pbData);\r
- uint32_t ulBlockIdx = (ulTruncIdx - 1U) % DIRENTS_PER_BLOCK;\r
+ uint32_t ulBlockIdx = ulTruncIdx % DIRENTS_PER_BLOCK;\r
\r
do\r
{\r
}\r
}\r
\r
+ /* Currently ulTruncIdx represents the last valid dirent index, or\r
+ UINT32_MAX if the directory is now empty. Increment it so that it\r
+ represents the first invalid entry, which will be truncated.\r
+ */\r
+ ulTruncIdx++;\r
+\r
/* Truncate the directory, deleting the requested entry and any empty\r
dirents at the end of the directory.\r
*/\r
#if (REDCONF_API_POSIX_READDIR == 1) || (REDCONF_CHECKER == 1)\r
/** @brief Read the next entry from a directory, given a starting index.\r
\r
- @param pInode A pointer to the cached inode structure of the directory to\r
+ @param pPInode A pointer to the cached inode structure of the directory to\r
read from.\r
@param pulIdx On entry, the directory index to start reading from. On\r
successful return, populated with the directory index to use\r