Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:18

0001 /*
0002 
0003 
0004   Program to calculate adler32 checksum for every file given
0005   on the command line. Uses zlib's adler32 routine.
0006 
0007   For the CMS Experiment http://cms.cern.ch
0008 
0009   Author: Stephen J. Gowdy <gowdy@cern.ch>
0010   Created: 6th Dec 2008
0011 
0012 */
0013 
0014 #define _LARGEFILE64_SOURCE
0015 #include <cstdio>
0016 #include <cstdlib>
0017 #include <fcntl.h>
0018 #include <fmt/format.h>
0019 #include <iostream>
0020 #include <libgen.h>
0021 #include <sys/stat.h>
0022 #include <unistd.h>
0023 #include <zlib.h>
0024 #ifdef __APPLE__
0025 typedef off_t off64_t;
0026 #define O_LARGEFILE 0
0027 #endif
0028 
0029 constexpr int EDMFILEUTILADLERBUFSIZE = 10 * 1024 * 1024;  // 10MB buffer
0030 
0031 int main(int argc, char* argv[]) {
0032   if (argc == 1) {
0033     std::cout << basename(argv[0]) << ": no files specified.\n";
0034     exit(1);
0035   }
0036 
0037   std::unique_ptr<unsigned char[]> buffer{new unsigned char[EDMFILEUTILADLERBUFSIZE]};
0038   int fileNum = 0;
0039   for (fileNum = 1; fileNum < argc; fileNum++) {
0040     uLong adlerCksum = adler32(0, nullptr, 0);
0041     off64_t fileSize = 0;
0042 
0043     int myFD = open(argv[fileNum], O_RDONLY | O_LARGEFILE);
0044     if (myFD == -1) {
0045       std::cout << basename(argv[0]) << ": failed to open file " << argv[fileNum] << ".\n";
0046       continue;
0047     }
0048 
0049     lseek(myFD, 0, SEEK_SET);
0050 
0051     int readSize = 0;
0052     while ((readSize = read(myFD, buffer.get(), EDMFILEUTILADLERBUFSIZE)) > 0) {
0053       adlerCksum = adler32(adlerCksum, buffer.get(), readSize);
0054       fileSize += readSize;
0055     }
0056 
0057     std::cout << fmt::format("{:x} {} {}\n", adlerCksum, fileSize, argv[fileNum]);
0058   }
0059 
0060   return (0);
0061 }
0062 
0063 /* Compile-line: gcc adler32.c -o adler32 -lz */