log_event.cc
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:17k
- /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
- #ifndef MYSQL_CLIENT
- #ifdef __GNUC__
- #pragma implementation // gcc: Class implementation
- #endif
- #include "mysql_priv.h"
- #endif /* MYSQL_CLIENT */
- static void pretty_print_char(FILE* file, int c)
- {
- fputc(''', file);
- switch(c) {
- case 'n': fprintf(file, "\n"); break;
- case 'r': fprintf(file, "\r"); break;
- case '\': fprintf(file, "\\"); break;
- case 'b': fprintf(file, "\b"); break;
- case ''': fprintf(file, "\'"); break;
- case 0 : fprintf(file, "\0"); break;
- default:
- fputc(c, file);
- break;
- }
- fputc(''', file);
- }
- int Query_log_event::write(IO_CACHE* file)
- {
- return query ? Log_event::write(file) : -1;
- }
- int Log_event::write(IO_CACHE* file)
- {
- return (write_header(file) || write_data(file)) ? -1 : 0;
- }
- int Log_event::write_header(IO_CACHE* file)
- {
- // make sure to change this when the header gets bigger
- char buf[LOG_EVENT_HEADER_LEN];
- char* pos = buf;
- int4store(pos, when); // timestamp
- pos += 4;
- *pos++ = get_type_code(); // event type code
- int4store(pos, server_id);
- pos += 4;
- long tmp=get_data_size() + LOG_EVENT_HEADER_LEN;
- int4store(pos, tmp);
- pos += 4;
- return (my_b_write(file, (byte*) buf, (uint) (pos - buf)));
- }
- #ifndef MYSQL_CLIENT
- int Log_event::read_log_event(IO_CACHE* file, String* packet,
- pthread_mutex_t* log_lock)
- {
- ulong data_len;
- char buf[LOG_EVENT_HEADER_LEN];
- if (log_lock)
- pthread_mutex_lock(log_lock);
- if (my_b_read(file, (byte*) buf, sizeof(buf)))
- {
- if (log_lock) pthread_mutex_unlock(log_lock);
- // if the read hits eof, we must report it as eof
- // so the caller will know it can go into cond_wait to be woken up
- // on the next update to the log
- if(!file->error) return LOG_READ_EOF;
- return file->error > 0 ? LOG_READ_TRUNC: LOG_READ_IO;
- }
- data_len = uint4korr(buf + EVENT_LEN_OFFSET);
- if (data_len < LOG_EVENT_HEADER_LEN || data_len > max_allowed_packet)
- {
- if (log_lock) pthread_mutex_unlock(log_lock);
- return (data_len < LOG_EVENT_HEADER_LEN) ? LOG_READ_BOGUS :
- LOG_READ_TOO_LARGE;
- }
- packet->append(buf, sizeof(buf));
- data_len -= LOG_EVENT_HEADER_LEN;
- if (data_len)
- {
- if (packet->append(file, data_len))
- {
- if(log_lock)
- pthread_mutex_unlock(log_lock);
- // here we should never hit eof in a non-error condtion
- // eof means we are reading the event partially, which should
- // never happen
- return file->error >= 0 ? LOG_READ_TRUNC: LOG_READ_IO;
- }
- }
- if (log_lock) pthread_mutex_unlock(log_lock);
- return 0;
- }
- #endif // MYSQL_CLIENT
- // allocates memory - the caller is responsible for clean-up
- Log_event* Log_event::read_log_event(IO_CACHE* file, pthread_mutex_t* log_lock)
- {
- time_t timestamp;
- uint32 server_id;
-
- char buf[LOG_EVENT_HEADER_LEN-4];
- if(log_lock) pthread_mutex_lock(log_lock);
- if (my_b_read(file, (byte *) buf, sizeof(buf)))
- {
- if (log_lock) pthread_mutex_unlock(log_lock);
- return NULL;
- }
- timestamp = uint4korr(buf);
- server_id = uint4korr(buf + 5);
-
- switch(buf[EVENT_TYPE_OFFSET])
- {
- case QUERY_EVENT:
- {
- Query_log_event* q = new Query_log_event(file, timestamp, server_id);
- if(log_lock) pthread_mutex_unlock(log_lock);
- if (!q->query)
- {
- delete q;
- q=NULL;
- }
- return q;
- }
-
- case LOAD_EVENT:
- {
- Load_log_event* l = new Load_log_event(file, timestamp, server_id);
- if(log_lock) pthread_mutex_unlock(log_lock);
- if (!l->table_name)
- {
- delete l;
- l=NULL;
- }
- return l;
- }
- case ROTATE_EVENT:
- {
- Rotate_log_event* r = new Rotate_log_event(file, timestamp, server_id);
- if(log_lock) pthread_mutex_unlock(log_lock);
-
- if (!r->new_log_ident)
- {
- delete r;
- r=NULL;
- }
- return r;
- }
- case INTVAR_EVENT:
- {
- Intvar_log_event* e = new Intvar_log_event(file, timestamp, server_id);
- if(log_lock) pthread_mutex_unlock(log_lock);
-
- if (e->type == INVALID_INT_EVENT)
- {
- delete e;
- e=NULL;
- }
- return e;
- }
-
- case START_EVENT:
- {
- Start_log_event* e = new Start_log_event(file, timestamp, server_id);
- if(log_lock) pthread_mutex_unlock(log_lock);
- return e;
- }
- case STOP_EVENT:
- {
- Stop_log_event* e = new Stop_log_event(file, timestamp, server_id);
- if(log_lock) pthread_mutex_unlock(log_lock);
- return e;
- }
- default:
- break;
- }
- // default
- if (log_lock) pthread_mutex_unlock(log_lock);
- return NULL;
- }
- Log_event* Log_event::read_log_event(const char* buf, int event_len)
- {
- if(event_len < EVENT_LEN_OFFSET ||
- (uint)event_len != uint4korr(buf+EVENT_LEN_OFFSET))
- return NULL; // general sanity check - will fail on a partial read
-
- switch(buf[EVENT_TYPE_OFFSET])
- {
- case QUERY_EVENT:
- {
- Query_log_event* q = new Query_log_event(buf, event_len);
- if (!q->query)
- {
- delete q;
- return NULL;
- }
- return q;
- }
- case LOAD_EVENT:
- {
- Load_log_event* l = new Load_log_event(buf, event_len);
- if (!l->table_name)
- {
- delete l;
- return NULL;
- }
- return l;
- }
- case ROTATE_EVENT:
- {
- Rotate_log_event* r = new Rotate_log_event(buf, event_len);
- if (!r->new_log_ident)
- {
- delete r;
- return NULL;
- }
- return r;
- }
- case START_EVENT: return new Start_log_event(buf);
- case STOP_EVENT: return new Stop_log_event(buf);
- case INTVAR_EVENT: return new Intvar_log_event(buf);
- default:
- break;
- }
- return NULL; // default value
- }
- void Log_event::print_header(FILE* file)
- {
- fputc('#', file);
- print_timestamp(file);
- fprintf(file, " server id %d ", server_id);
- }
- void Log_event::print_timestamp(FILE* file, time_t* ts)
- {
- struct tm tm_tmp;
- if (!ts)
- {
- ts = &when;
- }
- localtime_r(ts,&tm_tmp);
- fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
- tm_tmp.tm_year % 100,
- tm_tmp.tm_mon+1,
- tm_tmp.tm_mday,
- tm_tmp.tm_hour,
- tm_tmp.tm_min,
- tm_tmp.tm_sec);
- }
- void Start_log_event::print(FILE* file, bool short_form)
- {
- if (short_form)
- return;
- print_header(file);
- fprintf(file, "tStart: binlog v %d, server v %s created ", binlog_version,
- server_version);
- print_timestamp(file, (time_t*)&created);
- fputc('n', file);
- fflush(file);
- }
- void Stop_log_event::print(FILE* file, bool short_form)
- {
- if (short_form)
- return;
- print_header(file);
- fprintf(file, "tStopn");
- fflush(file);
- }
- void Rotate_log_event::print(FILE* file, bool short_form)
- {
- if (short_form)
- return;
- print_header(file);
- fprintf(file, "tRotate to ");
- if (new_log_ident)
- my_fwrite(file, (byte*) new_log_ident, (uint)ident_len,
- MYF(MY_NABP | MY_WME));
- fprintf(file, "n");
- fflush(file);
- }
- Rotate_log_event::Rotate_log_event(IO_CACHE* file, time_t when_arg,
- uint32 server_id):
- Log_event(when_arg, 0, 0, server_id),new_log_ident(NULL),alloced(0)
- {
- char *tmp_ident;
- char buf[4];
- if (my_b_read(file, (byte*) buf, sizeof(buf)))
- return;
- ulong event_len;
- event_len = uint4korr(buf);
- if (event_len < ROTATE_EVENT_OVERHEAD)
- return;
- ident_len = (uchar)(event_len - ROTATE_EVENT_OVERHEAD);
- if (!(tmp_ident = (char*) my_malloc((uint)ident_len, MYF(MY_WME))))
- return;
- if (my_b_read( file, (byte*) tmp_ident, (uint) ident_len))
- {
- my_free((gptr) tmp_ident, MYF(0));
- return;
- }
- new_log_ident = tmp_ident;
- alloced = 1;
- }
- Start_log_event::Start_log_event(const char* buf) :Log_event(buf)
- {
- buf += EVENT_LEN_OFFSET + 4; // skip even length
- binlog_version = uint2korr(buf);
- memcpy(server_version, buf + 2, sizeof(server_version));
- created = uint4korr(buf + 2 + sizeof(server_version));
- }
- int Start_log_event::write_data(IO_CACHE* file)
- {
- char buff[sizeof(server_version)+2+4];
- int2store(buff,binlog_version);
- memcpy(buff+2,server_version,sizeof(server_version));
- int4store(buff+2+sizeof(server_version),created);
- return (my_b_write(file, (byte*) buff, sizeof(buff)) ? -1 : 0);
- }
- Rotate_log_event::Rotate_log_event(const char* buf, int event_len):
- Log_event(buf),new_log_ident(NULL),alloced(0)
- {
- // the caller will ensure that event_len is what we have at
- // EVENT_LEN_OFFSET
- if(event_len < ROTATE_EVENT_OVERHEAD)
- return;
- ident_len = (uchar)(event_len - ROTATE_EVENT_OVERHEAD);
- if (!(new_log_ident = (char*) my_memdup((byte*) buf + LOG_EVENT_HEADER_LEN,
- (uint) ident_len, MYF(MY_WME))))
- return;
- alloced = 1;
- }
- int Rotate_log_event::write_data(IO_CACHE* file)
- {
- return my_b_write(file, (byte*) new_log_ident, (uint) ident_len) ? -1 :0;
- }
- Query_log_event::Query_log_event(IO_CACHE* file, time_t when_arg,
- uint32 server_id):
- Log_event(when_arg,0,0,server_id),data_buf(0),query(NULL),db(NULL)
- {
- char buf[QUERY_HEADER_LEN + 4];
- ulong data_len;
- if (my_b_read(file, (byte*) buf, sizeof(buf)))
- return; // query == NULL will tell the
- // caller there was a problem
- data_len = uint4korr(buf);
- if (data_len < QUERY_EVENT_OVERHEAD)
- return; // tear-drop attack protection :)
- data_len -= QUERY_EVENT_OVERHEAD;
- exec_time = uint4korr(buf + 8);
- db_len = (uint)buf[12];
- error_code = uint2korr(buf + 13);
-
- /* Allocate one byte extra for end