blob: 3e908f543c76811087724584c4cf0863391f3843 [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
38static const char *synopsis = "Repack a trace file with Snappy compression.";
39
40static void
41usage(void)
42{
43 std::cout
44 << "usage: apitrace repack <in-trace-file> <out-trace-file>\n"
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"
49 << "\n";
50}
51
José Fonsecab1b5a382012-04-20 22:49:20 +010052const static char *
53shortOptions = "h";
54
55const static struct option
56longOptions[] = {
57 {"help", no_argument, 0, 'h'},
58 {0, 0, 0, 0}
59};
60
José Fonsecaa3285532011-11-27 12:32:00 +000061static int
62repack(const char *inFileName, const char *outFileName)
63{
64 trace::File *inFile = trace::File::createForRead(inFileName);
65 if (!inFile) {
66 return 1;
67 }
68
Jose Fonsecace2ed372015-11-07 23:01:11 +000069 trace::OutStream *outFile = trace::createSnappyStream(outFileName);
José Fonsecaa3285532011-11-27 12:32:00 +000070 if (!outFile) {
71 delete inFile;
72 return 1;
73 }
74
75 size_t size = 8192;
76 char *buf = new char[size];
77 size_t read;
78
79 while ((read = inFile->read(buf, size)) != 0) {
80 outFile->write(buf, read);
81 }
82
83 delete [] buf;
84 delete outFile;
85 delete inFile;
86
87 return 0;
88}
89
90static int
91command(int argc, char *argv[])
92{
José Fonsecab1b5a382012-04-20 22:49:20 +010093 int opt;
94 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
95 switch (opt) {
96 case 'h':
José Fonsecaa3285532011-11-27 12:32:00 +000097 usage();
98 return 0;
José Fonsecab1b5a382012-04-20 22:49:20 +010099 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700100 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonsecaa3285532011-11-27 12:32:00 +0000101 usage();
102 return 1;
103 }
104 }
105
José Fonsecab1b5a382012-04-20 22:49:20 +0100106 if (argc != optind + 2) {
José Fonsecaa3285532011-11-27 12:32:00 +0000107 std::cerr << "error: insufficient number of arguments\n";
108 usage();
109 return 1;
110 }
111
José Fonsecab1b5a382012-04-20 22:49:20 +0100112 return repack(argv[optind], argv[optind + 1]);
José Fonsecaa3285532011-11-27 12:32:00 +0000113}
114
115const Command repack_command = {
116 "repack",
117 synopsis,
118 usage,
119 command
120};