Skip to content

Commit

Permalink
Bug 1170646 - Handle short read in (old) Cache code still used by C-C…
Browse files Browse the repository at this point in the history
… TB. r=michal
  • Loading branch information
zephyrus00jp committed Jul 7, 2015
1 parent dbe075d commit 55c4436
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions netwerk/cache/nsDiskCacheBlockFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@

using namespace mozilla;

/* to cope with short read.
* xxx not sure if we want to repeat PR_Read() if no octet is ever read
* and is errno == EINTR
*/
static
PRInt32
busy_beaver_PR_Read(PRFileDesc *fd, void * start, PRInt32 len)
{
int n;
PRInt32 remaining = len;

while (remaining > 0) {
n = PR_Read(fd, start, remaining);
if (n < 0) {
if( (len - remaining) == 0 ) // no octet is ever read
return -1;
break;
} else {
remaining -= n;
char *cp = (char *) start;
cp += n;
start = cp;
}
}
return len - remaining;
}

/******************************************************************************
* nsDiskCacheBlockFile -
*****************************************************************************/
Expand Down Expand Up @@ -74,7 +101,7 @@ nsDiskCacheBlockFile::Open(nsIFile * blockFile,

} else {
// read the bit map
const int32_t bytesRead = PR_Read(mFD, mBitMap, bitMapBytes);
const int32_t bytesRead = busy_beaver_PR_Read(mFD, mBitMap, bitMapBytes);
if ((bytesRead < 0) || ((uint32_t)bytesRead < bitMapBytes)) {
*corruptInfo = nsDiskCache::kBlockFileBitMapReadError;
rv = NS_ERROR_UNEXPECTED;
Expand Down Expand Up @@ -253,11 +280,15 @@ nsDiskCacheBlockFile::ReadBlocks( void * buffer,
if ((bytesToRead <= 0) || ((uint32_t)bytesToRead > mBlockSize * numBlocks)) {
bytesToRead = mBlockSize * numBlocks;
}
*bytesRead = PR_Read(mFD, buffer, bytesToRead);

/* This has to tolerate short read, i.e., we need to repeat! */
*bytesRead = busy_beaver_PR_Read(mFD, buffer, bytesToRead);

CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Read [this=%p] "
"returned %d / %d bytes", this, *bytesRead, bytesToRead));

if(*bytesRead == -1)
return NS_ERROR_UNEXPECTED;

return NS_OK;
}

Expand Down

0 comments on commit 55c4436

Please sign in to comment.