md5chunks.cc

Go to the documentation of this file.
00001 
00002 #include <sys/errno.h>
00003 
00004 #include "../debug/Log.h"
00005 #include "../io/FileUtils.h"
00006 #include "../io/MmapFile.h"
00007 #include "../util/MD5.h"
00008 
00009 using namespace oasys;
00010 
00011 int
00012 main(int argc, const char** argv)
00013 {
00014     const char* LOG = "/md5chunks";
00015     
00016     if (argc != 3) {
00017         fprintf(stderr, "usage: %s <filename> <size>\n", argv[0]);
00018         exit(1);
00019     }
00020 
00021     const char* filename = argv[1];
00022     size_t chunk_size = (size_t)atoi(argv[2]);
00023 
00024     Log::init();
00025 
00026     int size = FileUtils::size(filename);
00027     if (size < 0) {
00028         log_err_p(LOG, "error getting file size %s: %s",
00029                   filename, strerror(errno));
00030         exit(1);
00031     }
00032 
00033     MmapFile mm("/md5chunks/mmap");
00034     const u_char* bp = (const u_char*)mm.map(filename, PROT_READ, 0);
00035     if (bp == NULL) {
00036         log_err_p(LOG, "error mmap'ing file: %s", strerror(errno));
00037         exit(1);
00038     }
00039     
00040     size_t todo = size;
00041     int i = 0;
00042     do {
00043         size_t chunk = std::min(chunk_size, todo);
00044         MD5 md5;
00045         md5.update(bp, chunk);
00046         md5.finalize();
00047         printf("%s\t%d\t%zu\t%s\n", filename, i, chunk, md5.digest_ascii().c_str());
00048         fflush(stdout);
00049         i++;
00050         todo -= chunk;
00051         bp   += chunk;
00052     } while (todo != 0);
00053 }

Generated on Sat Sep 8 08:43:30 2007 for DTN Reference Implementation by  doxygen 1.5.3