From 00bb8c39b97638c872ebccc6aee7f3c5fb57d7d6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Jan 2009 16:54:55 +0000 Subject: Corrected the Bu::File::canRead() and Bu::File::canWrite() functions, they work now. It helps to read the system docs. Anyway, nids is all fixed up, it seems to work great now, and I guess I got all the corner cases we'll hit for a while, fishtrax really did a number on them :) I also cleaned up all the debugging output, now you can see your program run instead of libbu++ internals. There could still be a good amount of improvement made in nids, it really shouldn't re-write whole blocks every time you write to a stream, but that will be an easy change down the line that won't effect any of the existing code. --- src/cachestorenids.h | 7 ------- src/file.cpp | 10 ++++++++-- src/nids.cpp | 12 ++++++------ src/nidsstream.cpp | 14 +++++++++++--- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/cachestorenids.h b/src/cachestorenids.h index bd0fcc7..9288008 100644 --- a/src/cachestorenids.h +++ b/src/cachestorenids.h @@ -48,12 +48,6 @@ namespace Bu NidsStream ns = nStore.openStream( 0 ); Bu::Archive ar( ns, Bu::Archive::save ); ar << hId; - - Bu::FString sName; - sName.format("hash-%d.%02d", time( NULL ), iCnt++ ); - Bu::File sTmp( sName, Bu::File::Write|Bu::File::Create ); - Bu::Archive artmp( sTmp, Bu::Archive::save ); - artmp << hId; } virtual obtype *load( const keytype &key ) @@ -80,7 +74,6 @@ namespace Bu keytype key = __cacheGetKey( pSrc ); int iStream = nStore.createStream(); hId.insert( key, iStream ); - printf("Creating stream: %d\n", iStream ); NidsStream ns = nStore.openStream( iStream ); Bu::Archive ar( ns, Bu::Archive::save ); obtype *pOb = new obtype(); diff --git a/src/file.cpp b/src/file.cpp index 20ff5c9..aa0ea32 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -113,7 +113,10 @@ bool Bu::File::canRead() #ifdef WIN32 return true; #else - return (fcntl( fd, F_GETFL, 0 )&O_RDONLY) == O_RDONLY; + int iMode = fcntl( fd, F_GETFL, 0 )&O_ACCMODE; + if( iMode == O_RDONLY || iMode == O_RDWR ) + return true; + return false; #endif } @@ -122,7 +125,10 @@ bool Bu::File::canWrite() #ifdef WIN32 return true; #else - return (fcntl( fd, F_GETFL, 0 )&O_WRONLY) == O_WRONLY; + int iMode = fcntl( fd, F_GETFL, 0 )&O_ACCMODE; + if( iMode == O_WRONLY || iMode == O_RDWR ) + return true; + return false; #endif } diff --git a/src/nids.cpp b/src/nids.cpp index 00d5f2b..23c11b0 100644 --- a/src/nids.cpp +++ b/src/nids.cpp @@ -114,8 +114,8 @@ void Bu::Nids::initialize( int iBlockSize, int iPreAllocate ) void Bu::Nids::updateHeader() { -// if( !sStore.canWrite() ) -// return; + if( !sStore.canWrite() ) + return; sStore.setPos( 10 ); // Skip the magic number, version, bpi, block size sStore.write( &iBlocks, 4 ); sStore.write( &iUsed, 4 ); @@ -222,8 +222,8 @@ void Bu::Nids::setBlock( uint32_t uIndex, Bu::Nids::Block *pBlock ) void Bu::Nids::updateStreamSize( uint32_t uIndex, uint32_t uSize ) { -// if( !sStore.canWrite() ) -// return; + if( !sStore.canWrite() ) + return; sStore.setPos( iBlockStart + (iBlockSize*uIndex)+4*3 ); sStore.write( &uSize, 4 ); } @@ -240,8 +240,8 @@ uint32_t Bu::Nids::getNextBlock( uint32_t uIndex, sStore.setPos( iBlockStart + (iBlockSize*uIndex)+1*4 ); sStore.write( &uNew, 4 ); getBlock( uNew, pBlock ); - printf("Allocated new block (%u) for stream %u.\n", - uNew, pBlock->uFirstBlock ); + //printf("Allocated new block (%u) for stream %u.\n", + // uNew, pBlock->uFirstBlock ); } else { diff --git a/src/nidsstream.cpp b/src/nidsstream.cpp index fff73f6..31e1293 100644 --- a/src/nidsstream.cpp +++ b/src/nidsstream.cpp @@ -55,7 +55,7 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes ) if( iRead > pCurBlock->uBytesUsed-(uPos%uBlockSize) ) iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); memcpy( pBuf, pCurBlock->pData+(uPos%uBlockSize), iRead ); - //printf("buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)", + //printf("read buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u)", // iRead, uPos, uPos+iRead-1, uSize, 0, iRead-1, nBytes, uCurBlock ); uPos += iRead; //printf(" -- %u\n", uPos%uBlockSize ); @@ -80,7 +80,7 @@ size_t Bu::NidsStream::read( void *pBuf, size_t nBytes ) iRead = pCurBlock->uBytesUsed-(uPos%uBlockSize); memcpy( ((char *)pBuf)+nTotal, pCurBlock->pData+(uPos%uBlockSize), iRead ); - //printf("buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n", + //printf(" read buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u)\n", // iRead, uPos, uPos+iRead-1, uSize, // nTotal, nTotal+nBytes-1, nBytes, uCurBlock ); uPos += iRead; @@ -111,6 +111,10 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes ) { //printf("wa: %u:%u:%u:%u -> ", uPos, uPos%uBlockSize, uSize, pCurBlock->uBytesUsed ); memcpy( pCurBlock->pData+(uPos%uBlockSize), pBuf, nBytes ); + //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (a:%u:%u)\n", + // nBytes, 0, nBytes-1, nBytes, + // uPos, uPos+nBytes-1, uSize, uCurBlock, + // pCurBlock->uBytesUsed ); if( (uPos%uBlockSize)+nBytes > pCurBlock->uBytesUsed ) pCurBlock->uBytesUsed = (uPos%uBlockSize)+nBytes; rNids.setBlock( uCurBlock, pCurBlock ); @@ -132,6 +136,9 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes ) uNow = nBytes; memcpy( pCurBlock->pData+(uPos%uBlockSize), &((char *)pBuf)[nTotal], uNow ); + //printf("write buffill: %ub, %u-%u/%u -> %d-%d/%d (b:%u:%u)\n", + // uNow, nTotal, nTotal+uNow-1, nBytes, + // uPos, uPos+uNow-1, uSize, uCurBlock, pCurBlock->uBytesUsed ); if( (uPos%uBlockSize)+uNow > pCurBlock->uBytesUsed ) pCurBlock->uBytesUsed = (uPos%uBlockSize)+uNow; rNids.setBlock( uCurBlock, pCurBlock ); @@ -142,7 +149,8 @@ size_t Bu::NidsStream::write( const void *pBuf, size_t nBytes ) nBytes -= uNow; //printf("wb: block %u = %ub (%ub total)\n", // uCurBlock, pCurBlock->uBytesUsed, uSize ); - if( pCurBlock->uBytesUsed == uBlockSize ) + //if( pCurBlock->uBytesUsed == uBlockSize ) + if( uPos%uBlockSize == 0 ) uCurBlock = rNids.getNextBlock( uCurBlock, pCurBlock ); if( nBytes == 0 ) return nTotal; -- cgit v1.2.3