1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/global.h>
21
#include <sys/types.h>
29
#include <drizzled/message/replication.pb.h>
30
#include "drizzled/gettext.h"
31
#include "drizzled/message/command_transform.h"
32
#include "drizzled/korr.h"
35
using namespace drizzled;
37
static void printCommand(const message::Command &command)
39
cout << "/* Timestamp: " << command.timestamp() << " */"<< endl;
41
message::TransactionContext trx= command.transaction_context();
43
cout << "/* SERVER ID: " << trx.server_id() << " TRX ID: " << trx.transaction_id();
45
if (command.has_session_id())
46
cout << " SESSION ID: " << command.session_id();
52
message::transformCommand2Sql(command, &sql, message::DRIZZLE);
55
* Replace \n with spaces so that SQL statements
56
* are always on a single line
58
const std::string newline= "\n";
59
while (sql.find(newline) != std::string::npos)
60
sql.replace(sql.find(newline), 1, " ");
62
cout << sql << ';' << endl;
65
int main(int argc, char* argv[])
67
GOOGLE_PROTOBUF_VERIFY_VERSION;
72
fprintf(stderr, _("Usage: %s COMMAND_LOG\n"), argv[0]);
76
message::Command command;
78
file= open(argv[1], O_RDONLY);
81
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
85
char *temp_buffer= NULL;
86
uint64_t previous_length= 0;
87
ssize_t read_bytes= 0;
91
/* We use korr.h macros when writing and must do the same when reading... */
92
unsigned char coded_length[8];
93
unsigned char coded_checksum[4];
95
/* Read in the length of the command */
96
while ((read_bytes= read(file, coded_length, sizeof(uint64_t))) != 0)
100
fprintf(stderr, _("Failed to read initial length header\n"));
103
length= uint8korr(coded_length);
105
if (length > SIZE_MAX)
107
fprintf(stderr, _("Attempted to read record bigger than SIZE_MAX\n"));
114
* First time around...just malloc the length. This block gets rid
115
* of a GCC warning about uninitialized temp_buffer.
117
temp_buffer= (char *) malloc((size_t) length);
119
/* No need to allocate if we have a buffer big enough... */
120
else if (length > previous_length)
122
temp_buffer= (char *) realloc(buffer, (size_t) length);
125
if (temp_buffer == NULL)
127
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"), length);
133
/* Read the Command */
134
read_bytes= read(file, buffer, (size_t) length);
135
if ((read_bytes != (ssize_t) length))
137
fprintf(stderr, _("Could not read entire transaction. Read %" PRIu64 " bytes instead of %" PRIu64 " bytes.\n"), (uint64_t) read_bytes, (uint64_t) length);
141
if (! command.ParseFromArray(buffer, (int) length))
143
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), command.InitializationErrorString().c_str());
145
fprintf(stderr, _("BUFFER: %s\n"), buffer);
149
/* Read the checksum */
150
read_bytes= read(file, coded_checksum, sizeof(uint32_t));
151
if ((read_bytes != (ssize_t) sizeof(uint32_t)))
153
fprintf(stderr, _("Could not read entire checksum. Read %" PRIu64 " bytes instead of 4 bytes.\n"), (uint64_t) read_bytes);
156
checksum= uint4korr(coded_checksum);
160
/* @TODO checksumming.. */
163
/* Print the command */
164
printCommand(command);
166
/* Reset our length check */
167
previous_length= length;
168
memset(coded_length, 0, sizeof(coded_length));