blob: 291f0d96ed4f58b34cbed73f715a7e8777bcfdca [file] [log] [blame]
José Fonsecaa3285532011-11-27 12:32:00 +00001/**************************************************************************
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é Fonsecab1b5a382012-04-20 22:49:20 +010028#include <getopt.h>
29
José Fonsecaa3285532011-11-27 12:32:00 +000030#include <iostream>
31
32#include "cli.hpp"
33
34#include "trace_file.hpp"
Jose Fonsecace2ed372015-11-07 23:01:11 +000035#include "trace_ostream.hpp"
José Fonsecaa3285532011-11-27 12:32:00 +000036
37
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000038static const char *synopsis = "Repack a trace file with different compression.";
José Fonsecaa3285532011-11-27 12:32:00 +000039
40static void
41usage(void)
42{
43 std::cout
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000044 << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n"
José Fonsecaa3285532011-11-27 12:32:00 +000045 << 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 Fonsecae76ff4b2015-11-07 23:47:40 +000049 << "\n"
50 << " -z,--zlib Use ZLib compression instead\n"
José Fonsecaa3285532011-11-27 12:32:00 +000051 << "\n";
52}
53
José Fonsecab1b5a382012-04-20 22:49:20 +010054const static char *
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000055shortOptions = "hz";
José Fonsecab1b5a382012-04-20 22:49:20 +010056
57const static struct option
58longOptions[] = {
59 {"help", no_argument, 0, 'h'},
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000060 {"zlib", no_argument, 0, 'z'},
José Fonsecab1b5a382012-04-20 22:49:20 +010061 {0, 0, 0, 0}
62};
63
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000064enum Format {
65 FORMAT_SNAPPY = 0,
66 FORMAT_ZLIB,
67};
68
José Fonsecaa3285532011-11-27 12:32:00 +000069static int
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000070repack(const char *inFileName, const char *outFileName, Format format)
José Fonsecaa3285532011-11-27 12:32:00 +000071{
72 trace::File *inFile = trace::File::createForRead(inFileName);
73 if (!inFile) {
74 return 1;
75 }
76
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000077 trace::OutStream *outFile;
78 if (format == FORMAT_SNAPPY) {
79 outFile = trace::createSnappyStream(outFileName);
80 } else {
81 outFile = trace::createZLibStream(outFileName);
82 }
José Fonsecaa3285532011-11-27 12:32:00 +000083 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
103static int
104command(int argc, char *argv[])
105{
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000106 Format format = FORMAT_SNAPPY;
José Fonsecab1b5a382012-04-20 22:49:20 +0100107 int opt;
108 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
109 switch (opt) {
110 case 'h':
José Fonsecaa3285532011-11-27 12:32:00 +0000111 usage();
112 return 0;
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000113 case 'z':
114 format = FORMAT_ZLIB;
115 break;
José Fonsecab1b5a382012-04-20 22:49:20 +0100116 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700117 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonsecaa3285532011-11-27 12:32:00 +0000118 usage();
119 return 1;
120 }
121 }
122
José Fonsecab1b5a382012-04-20 22:49:20 +0100123 if (argc != optind + 2) {
José Fonsecaa3285532011-11-27 12:32:00 +0000124 std::cerr << "error: insufficient number of arguments\n";
125 usage();
126 return 1;
127 }
128
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000129 return repack(argv[optind], argv[optind + 1], format);
José Fonsecaa3285532011-11-27 12:32:00 +0000130}
131
132const Command repack_command = {
133 "repack",
134 synopsis,
135 usage,
136 command
137};