printSchemaFile.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:3k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. #include <ndb_global.h>
  14. #include <NdbMain.h>
  15. #include <NdbOut.hpp>
  16. #include <SchemaFile.hpp>
  17. void 
  18. usage(const char * prg){
  19.   ndbout << "Usage " << prg 
  20.  << " P0.SchemaLog" << endl;  
  21. }
  22. void
  23. fill(const char * buf, int mod){
  24.   int len = strlen(buf)+1;
  25.   ndbout << buf << " ";
  26.   while((len % mod) != 0){
  27.     ndbout << " ";
  28.     len++;
  29.   }
  30. }
  31. void 
  32. print(const char * filename, const SchemaFile * file){
  33.   ndbout << "----- Schemafile: " << filename << " -----" << endl;
  34.   ndbout_c("Magic: %.*s ByteOrder: %.8x NdbVersion: %d FileSize: %d",
  35.    sizeof(file->Magic), file->Magic, 
  36.    file->ByteOrder, 
  37.    file->NdbVersion,
  38.    file->FileSize);
  39.   for(Uint32 i = 0; i<file->NoOfTableEntries; i++){
  40.     SchemaFile::TableEntry te = file->TableEntries[i];
  41.     if(te.m_tableState != SchemaFile::INIT){
  42.       ndbout << "Table " << i << ": State = " << te.m_tableState 
  43.      << " version = " << te.m_tableVersion
  44.              << " type = " << te.m_tableType
  45.      << " noOfPages = " << te.m_noOfPages
  46.      << " gcp: " << te.m_gcp << endl;
  47.     }
  48.   }
  49. }
  50. NDB_COMMAND(printSchemafile, 
  51.     "printSchemafile", "printSchemafile", "Prints a schemafile", 16384){ 
  52.   if(argc < 2){
  53.     usage(argv[0]);
  54.     return 0;
  55.   }
  56.   const char * filename = argv[1];
  57.   struct stat sbuf;
  58.   const int res = stat(filename, &sbuf);
  59.   if(res != 0){
  60.     ndbout << "Could not find file: "" << filename << """ << endl;
  61.     return 0;
  62.   }
  63.   const Uint32 bytes = sbuf.st_size;
  64.   
  65.   Uint32 * buf = new Uint32[bytes/4+1];
  66.   
  67.   FILE * f = fopen(filename, "rb");
  68.   if(f == 0){
  69.     ndbout << "Failed to open file" << endl;
  70.     delete [] buf;
  71.     return 0;
  72.   }
  73.   Uint32 sz = fread(buf, 1, bytes, f);
  74.   fclose(f);
  75.   if(sz != bytes){
  76.     ndbout << "Failure while reading file" << endl;
  77.     delete [] buf;
  78.     return 0;
  79.   }
  80.   
  81.   print(filename, (SchemaFile *)&buf[0]);
  82.   Uint32 chk = 0, i;
  83.   for (i = 0; i < bytes/4; i++)
  84.     chk ^= buf[i];
  85.   if (chk != 0)
  86.     ndbout << "Invalid checksum!" << endl;
  87.   delete [] buf;
  88.   return 0;
  89. }