From 9098237f5bb16b204a5ea999b702e5eb170f68ac Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Jan 2009 15:25:46 +0000 Subject: Corrected some larger read/write issues in corner cases that I hit suprisingly often within nids. There's still a problem somewhere, but I'll find it. Also, even after having the file class canRead and canWrite functions work properly, and using them before trying to write to a nids to update info, we never ever write anything, so something is still wrong there. For now, all utilities that open a nids stream read-only will crash when it closes. Pretty minor really. --- src/tests/nidstool.cpp | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tests/rh.cpp | 52 +++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/tests/nidstool.cpp create mode 100644 src/tests/rh.cpp (limited to 'src/tests') diff --git a/src/tests/nidstool.cpp b/src/tests/nidstool.cpp new file mode 100644 index 0000000..546534e --- /dev/null +++ b/src/tests/nidstool.cpp @@ -0,0 +1,124 @@ +#include "bu/file.h" +#include "bu/nids.h" +#include "bu/nidsstream.h" +#include "bu/paramproc.h" + +#include + +class Param : public Bu::ParamProc +{ +public: + Param( int argc, char *argv[] ) + { + addHelpBanner("nidstool - Do stuff with nids files.\n\n"); + addParam("info", 'i', mkproc(Param::procInfo), + "Print some info about the file."); + addParam("dump", 'd', mkproc(Param::procDump), + "Dump a stream to a file"); + addParam("help", 'h', mkproc(Bu::ParamProc::help), "This help."); + process( argc, argv ); + } + + virtual ~Param() + { + } + + int procInfo( int argc, char *argv[] ) + { + if( argc < 1 ) + { + printf("You must provide a file name.\n"); + exit( 1 ); + } + + Bu::File fIn( argv[0], Bu::File::Read ); + Bu::Nids n( fIn ); + n.initialize(); + + printf("Block size: %db\n", n.getBlockSize() ); + printf("Block count: %d\n", n.getNumBlocks() ); + printf("Blocks used: %d (%d%%)\n", n.getNumUsedBlocks(), + n.getNumUsedBlocks()*100/n.getNumBlocks() ); + printf("Block start: %db\n", n.getBlockStart() ); + printf("Block overhead: %db\n", n.getBlockOverhead() ); + printf("Block storage: %db (%d%%)\n", + n.getBlockSize()-n.getBlockOverhead(), + (n.getBlockSize()-n.getBlockOverhead())*100/n.getBlockSize() ); + + if( argc >= 2 ) + { + typedef struct Block + { + uint32_t uFirstBlock; + uint32_t uNextBlock; + uint32_t uPrevBlock; + uint32_t uBytesUsed; + uint32_t uReserved; + } Block; + + uint32_t uStream = strtoul( argv[1], NULL, 0 ); + uint32_t uBlock = uStream; + + Block b; + + for(;;) + { + fIn.setPos( n.getBlockStart()+n.getBlockSize()*uBlock ); + fIn.read( &b, sizeof(Block) ); + printf("Stream %u: block %u, next %u, prev %u, %ub used.\n", + uStream, uBlock, b.uNextBlock, b.uPrevBlock, b.uBytesUsed + ); + if( b.uNextBlock == 0xFFFFFFFFUL ) + break; + uBlock = b.uNextBlock; + } + printf("Stream End.\n"); + + return 2; + } + + return 1; + } + + int procDump( int argc, char *argv[] ) + { + if( argc < 3 ) + { + printf("You must provide a nids file, a stream id, and an output " + "file.\n"); + exit( 1 ); + } + + Bu::File fIn( argv[0], Bu::File::Read ); + Bu::Nids n( fIn ); + n.initialize(); + + int iStream = strtol( argv[1], NULL, 0 ); + Bu::NidsStream sIn = n.openStream( iStream ); + + Bu::File fOut( argv[2], Bu::File::Write|Bu::File::Create ); + int iTotal = 0; + char buf[100]; + for(;;) + { + int iRead = sIn.read( buf, 100 ); + iTotal += fOut.write( buf, iRead ); + if( iRead < 100 ) + break; + } + + printf("Wrote %db from stream %d in %s to %s.\n", + iTotal, iStream, argv[0], argv[2] ); + return 3; + } + +}; + + +int main( int argc, char *argv[] ) +{ + Param p( argc, argv ); + + return 0; +} + diff --git a/src/tests/rh.cpp b/src/tests/rh.cpp new file mode 100644 index 0000000..70abcb7 --- /dev/null +++ b/src/tests/rh.cpp @@ -0,0 +1,52 @@ +#include "bu/file.h" +#include "bu/hash.h" +#include "bu/archive.h" +#include "bu/fstring.h" +#include "bu/nids.h" +#include "bu/nidsstream.h" + +int main( int argc, char *argv[] ) +{ + if( argv[1][0] == 'r' ) + { + typedef Bu::Hash Hsh; + + Bu::File fIn( argv[2], Bu::File::Read ); + Bu::Archive ar( fIn, Bu::Archive::load ); + + Hsh h; + ar >> h; + + printf("Read %d.\n", h.getSize() ); + for( Hsh::iterator i = h.begin(); i != h.end(); i++ ) + { + printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); + } + + printf("%d vs. %d\n", h.getSize(), h.getKeys().getSize() ); + } + else if( argv[1][0] == 'n' ) + { + typedef Bu::Hash Hsh; + + Bu::File fIn( argv[2], Bu::File::Read ); + Bu::Nids n( fIn ); + n.initialize(); + Bu::NidsStream sIn = n.openStream( 0 ); + Bu::Archive ar( sIn, Bu::Archive::load ); + + Hsh h; + ar >> h; + + printf("Read %d.\n", h.getSize() ); + for( Hsh::iterator i = h.begin(); i != h.end(); i++ ) + { + printf(" - \"%s\" = %d\n", i.getKey().getStr(), i.getValue() ); + } + + printf("%d vs. %d\n", h.getSize(), h.getKeys().getSize() ); + } + + return 0; +} + -- cgit v1.2.3