MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Filename.cpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 
20 #include "Filename.hpp"
21 #include "ErrorHandlingMacros.hpp"
22 #include "RefConvert.hpp"
23 #include "DebuggerNames.hpp"
24 #include "Ndbfs.hpp"
25 
26 #include <signaldata/FsOpenReq.hpp>
27 
28 static const char* fileExtension[] = {
29  ".Data",
30  ".FragLog",
31  ".LocLog",
32  ".FragList",
33  ".TableList",
34  ".SchemaLog",
35  ".sysfile",
36  ".log",
37  ".ctl"
38 };
39 
40 static const Uint32 noOfExtensions = sizeof(fileExtension)/sizeof(char*);
41 
42 Filename::Filename()
43 {
44 }
45 
46 Filename::~Filename(){
47 }
48 
49 void
50 Filename::set(Ndbfs* fs,
51  BlockReference blockReference,
52  const Uint32 filenumber[4], bool dir,
54 {
55  char buf[PATH_MAX];
56 
57  const Uint32 type = FsOpenReq::getSuffix(filenumber);
58  const Uint32 version = FsOpenReq::getVersion(filenumber);
59 
60  size_t sz;
61  if (version == 2)
62  {
63  sz = BaseString::snprintf(theName, sizeof(theName), "%s",
64  fs->get_base_path(FsOpenReq::BP_BACKUP).c_str());
65  m_base_name = theName + fs->get_base_path(FsOpenReq::BP_BACKUP).length();
66  }
67  else
68  {
69  sz = BaseString::snprintf(theName, sizeof(theName), "%s",
70  fs->get_base_path(FsOpenReq::BP_FS).c_str());
71  m_base_name = theName + fs->get_base_path(FsOpenReq::BP_FS).length();
72  }
73 
74  switch(version){
75  case 1 :{
76  const Uint32 diskNo = FsOpenReq::v1_getDisk(filenumber);
77  const Uint32 table = FsOpenReq::v1_getTable(filenumber);
78  const Uint32 frag = FsOpenReq::v1_getFragment(filenumber);
79  const Uint32 S_val = FsOpenReq::v1_getS(filenumber);
80  const Uint32 P_val = FsOpenReq::v1_getP(filenumber);
81 
82  if (diskNo < 0xff){
83  BaseString::snprintf(buf, sizeof(buf), "D%d%s", diskNo, DIR_SEPARATOR);
84  strcat(theName, buf);
85  }
86 
87  {
88  const char* blockName = getBlockName( refToMain(blockReference) );
89  if (blockName == NULL){
90  ERROR_SET(ecError, NDBD_EXIT_AFS_PARAMETER,"","No Block Name");
91  return;
92  }
93  BaseString::snprintf(buf, sizeof(buf), "%s%s", blockName, DIR_SEPARATOR);
94  strcat(theName, buf);
95  }
96 
97  if (table < 0xffffffff){
98  BaseString::snprintf(buf, sizeof(buf), "T%d%s", table, DIR_SEPARATOR);
99  strcat(theName, buf);
100  }
101 
102  if (frag < 0xffffffff){
103  BaseString::snprintf(buf, sizeof(buf), "F%d%s", frag, DIR_SEPARATOR);
104  strcat(theName, buf);
105  }
106 
107 
108  if (S_val < 0xffffffff){
109  BaseString::snprintf(buf, sizeof(buf), "S%d", S_val);
110  strcat(theName, buf);
111  }
112 
113  if (P_val < 0xff){
114  BaseString::snprintf(buf, sizeof(buf), "P%d", P_val);
115  strcat(theName, buf);
116  }
117 
118  }
119  break;
120  case 2:{
121  const Uint32 seq = FsOpenReq::v2_getSequence(filenumber);
122  const Uint32 nodeId = FsOpenReq::v2_getNodeId(filenumber);
123  const Uint32 count = FsOpenReq::v2_getCount(filenumber);
124 
125  BaseString::snprintf(buf, sizeof(buf), "BACKUP%sBACKUP-%u%s",
126  DIR_SEPARATOR, seq, DIR_SEPARATOR);
127  strcat(theName, buf);
128  if(count == 0xffffffff) {
129  BaseString::snprintf(buf, sizeof(buf), "BACKUP-%u.%d",
130  seq, nodeId); strcat(theName, buf);
131  } else {
132  BaseString::snprintf(buf, sizeof(buf), "BACKUP-%u-%d.%d",
133  seq, count, nodeId); strcat(theName, buf);
134  }
135  break;
136  }
137  break;
138  case 3:{
139  const Uint32 diskNo = FsOpenReq::v1_getDisk(filenumber);
140 
141  if(diskNo == 0xFF){
142  ERROR_SET(ecError, NDBD_EXIT_AFS_PARAMETER,"","Invalid disk specification");
143  }
144 
145  BaseString::snprintf(buf, sizeof(buf), "D%d%s", diskNo, DIR_SEPARATOR);
146  strcat(theName, buf);
147  }
148  break;
149  case 4:
150  {
151  char buf[PATH_MAX];
152  copy((Uint32*)&buf[0], ptr);
153  if(buf[0] == DIR_SEPARATOR[0])
154  {
155  strncpy(theName, buf, PATH_MAX);
156  m_base_name = theName;
157  }
158  else
159  {
160 #ifdef NDB_WIN32
161  char* b= buf;
162  while((b= strchr(b, '/')) && b)
163  {
164  *b= '\\';
165  }
166 #endif
167  Uint32 bp = FsOpenReq::v4_getBasePath(filenumber);
168  BaseString::snprintf(theName, sizeof(theName), "%s%s",
169  fs->get_base_path(bp).c_str(), buf);
170  m_base_name = theName + fs->get_base_path(bp).length();
171  }
172  return; // No extension
173  }
174  case 5:
175  {
176  Uint32 tableId = FsOpenReq::v5_getTableId(filenumber);
177  Uint32 lcpNo = FsOpenReq::v5_getLcpNo(filenumber);
178  Uint32 fragId = FsOpenReq::v5_getFragmentId(filenumber);
179  BaseString::snprintf(buf, sizeof(buf), "LCP%s%d%sT%dF%d", DIR_SEPARATOR, lcpNo, DIR_SEPARATOR, tableId, fragId);
180  strcat(theName, buf);
181  break;
182  }
183  case 6:
184  {
185  Uint32 bp = FsOpenReq::v5_getLcpNo(filenumber);
186  sz = BaseString::snprintf(theName, sizeof(theName), "%s",
187  fs->get_base_path(bp).c_str());
188  break;
189  }
190  default:
191  ERROR_SET(ecError, NDBD_EXIT_AFS_PARAMETER,"","Wrong version");
192  }
193  if (type >= noOfExtensions){
194  ERROR_SET(ecError, NDBD_EXIT_AFS_PARAMETER,"","File Type doesn't exist");
195  return;
196  }
197  strcat(theName, fileExtension[type]);
198 
199  if(dir == true){
200  for(int l = strlen(theName) - 1; l >= 0; l--){
201  if(theName[l] == DIR_SEPARATOR[0]){
202  theName[l] = 0;
203  break;
204  }
205  }
206  }
207 }