José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2011 Jose Fonseca |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | **************************************************************************/ |
| 25 | |
| 26 | |
| 27 | #include <string.h> |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 28 | #include <getopt.h> |
| 29 | |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 30 | #include <iostream> |
| 31 | |
| 32 | #include "cli.hpp" |
| 33 | |
| 34 | #include "trace_file.hpp" |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame] | 35 | #include "trace_ostream.hpp" |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 36 | |
| 37 | |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 38 | static const char *synopsis = "Repack a trace file with different compression."; |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 39 | |
| 40 | static void |
| 41 | usage(void) |
| 42 | { |
| 43 | std::cout |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 44 | << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n" |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 45 | << synopsis << "\n" |
| 46 | << "\n" |
| 47 | << "Snappy compression allows for faster replay and smaller memory footprint,\n" |
| 48 | << "at the expense of a slightly smaller compression ratio than zlib\n" |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 49 | << "\n" |
| 50 | << " -z,--zlib Use ZLib compression instead\n" |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 51 | << "\n"; |
| 52 | } |
| 53 | |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 54 | const static char * |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 55 | shortOptions = "hz"; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 56 | |
| 57 | const static struct option |
| 58 | longOptions[] = { |
| 59 | {"help", no_argument, 0, 'h'}, |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 60 | {"zlib", no_argument, 0, 'z'}, |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 61 | {0, 0, 0, 0} |
| 62 | }; |
| 63 | |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 64 | enum Format { |
| 65 | FORMAT_SNAPPY = 0, |
| 66 | FORMAT_ZLIB, |
| 67 | }; |
| 68 | |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 69 | static int |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 70 | repack(const char *inFileName, const char *outFileName, Format format) |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 71 | { |
| 72 | trace::File *inFile = trace::File::createForRead(inFileName); |
| 73 | if (!inFile) { |
| 74 | return 1; |
| 75 | } |
| 76 | |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 77 | trace::OutStream *outFile; |
| 78 | if (format == FORMAT_SNAPPY) { |
| 79 | outFile = trace::createSnappyStream(outFileName); |
| 80 | } else { |
| 81 | outFile = trace::createZLibStream(outFileName); |
| 82 | } |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 83 | if (!outFile) { |
| 84 | delete inFile; |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | size_t size = 8192; |
| 89 | char *buf = new char[size]; |
| 90 | size_t read; |
| 91 | |
| 92 | while ((read = inFile->read(buf, size)) != 0) { |
| 93 | outFile->write(buf, read); |
| 94 | } |
| 95 | |
| 96 | delete [] buf; |
| 97 | delete outFile; |
| 98 | delete inFile; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int |
| 104 | command(int argc, char *argv[]) |
| 105 | { |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 106 | Format format = FORMAT_SNAPPY; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 107 | int opt; |
| 108 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 109 | switch (opt) { |
| 110 | case 'h': |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 111 | usage(); |
| 112 | return 0; |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 113 | case 'z': |
| 114 | format = FORMAT_ZLIB; |
| 115 | break; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 116 | default: |
José Fonseca | 4ce88b8 | 2013-10-11 17:24:47 -0700 | [diff] [blame] | 117 | std::cerr << "error: unexpected option `" << (char)opt << "`\n"; |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 118 | usage(); |
| 119 | return 1; |
| 120 | } |
| 121 | } |
| 122 | |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 123 | if (argc != optind + 2) { |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 124 | std::cerr << "error: insufficient number of arguments\n"; |
| 125 | usage(); |
| 126 | return 1; |
| 127 | } |
| 128 | |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 129 | return repack(argv[optind], argv[optind + 1], format); |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | const Command repack_command = { |
| 133 | "repack", |
| 134 | synopsis, |
| 135 | usage, |
| 136 | command |
| 137 | }; |