Filename.cpp 5.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2003 MySQL 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 */

17 18
#include <ndb_global.h>

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <NdbOut.hpp>

#include "Filename.hpp"
#include "ErrorHandlingMacros.hpp"
#include "Error.hpp"
#include "RefConvert.hpp"
#include "DebuggerNames.hpp"

#include <signaldata/FsOpenReq.hpp>

static const char* fileExtension[] = {
  ".Data",
  ".FragLog",
  ".LocLog",
  ".FragList",
  ".TableList",
  ".SchemaLog",
  ".sysfile",
  ".log",
  ".ctl"
};

static const Uint32 noOfExtensions = sizeof(fileExtension)/sizeof(char*);

Filename::Filename() :
  theLevelDepth(0)
{
}

void
49 50 51 52 53
Filename::init(Uint32 nodeid,
	       const char * pFileSystemPath,
	       const char * pBackupDirPath){
  DBUG_ENTER("Filename::init");

54 55 56 57 58
  if (pFileSystemPath == NULL) {
    ERROR_SET(fatal, AFS_ERROR_NOPATH, ""," Filename::init()");
    return;
  }

59 60 61 62 63 64
  snprintf(theFileSystemDirectory, sizeof(theFileSystemDirectory),
	   "%sndb_%u_fs%s", pFileSystemPath, nodeid, DIR_SEPARATOR);
  strncpy(theBackupDirectory, pBackupDirPath, sizeof(theBackupDirectory));

  DBUG_PRINT("info", ("theFileSystemDirectory=%s", theFileSystemDirectory));
  DBUG_PRINT("info", ("theBackupDirectory=%s", theBackupDirectory));
65

66
#ifdef NDB_WIN32
67
  CreateDirectory(theFileSystemDirectory, 0);
68
#else
69
  mkdir(theFileSystemDirectory, S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IRGRP);
70
#endif
71 72 73
  theBaseDirectory= 0;

  DBUG_VOID_RETURN;
74
}
75 76 77 78 79 80 81 82 83 84

Filename::~Filename(){
}

void 
Filename::set(BlockReference blockReference, 
	      const Uint32 filenumber[4], bool dir) 
{
  char buf[PATH_MAX];
  theLevelDepth = 0;
85

86 87
  const Uint32 type = FsOpenReq::getSuffix(filenumber);
  const Uint32 version = FsOpenReq::getVersion(filenumber);
88 89 90 91 92 93 94

  if (version == 2)
    theBaseDirectory= theBackupDirectory;
  else
    theBaseDirectory= theFileSystemDirectory;
  strncpy(theName, theBaseDirectory, PATH_MAX);
      
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  switch(version){
  case 1 :{
    const Uint32 diskNo = FsOpenReq::v1_getDisk(filenumber);
    const Uint32 table  = FsOpenReq::v1_getTable(filenumber);
    const Uint32 frag   = FsOpenReq::v1_getFragment(filenumber);
    const Uint32 S_val  = FsOpenReq::v1_getS(filenumber);
    const Uint32 P_val  = FsOpenReq::v1_getP(filenumber);

    if (diskNo < 0xff){	  
      snprintf(buf, sizeof(buf), "D%d%s", diskNo, DIR_SEPARATOR);
      strcat(theName, buf);
      theLevelDepth++;
    }
    
    {
      const char* blockName = getBlockName( refToBlock(blockReference) );
      if (blockName == NULL){
	ERROR_SET(ecError, AFS_ERROR_PARAMETER,"","No Block Name");
	return;
      }
      snprintf(buf, sizeof(buf), "%s%s", blockName, DIR_SEPARATOR);
      strcat(theName, buf);
      theLevelDepth++;
    }
    
    if (table < 0xffffffff){
      snprintf(buf, sizeof(buf), "T%d%s", table, DIR_SEPARATOR);
      strcat(theName, buf);
      theLevelDepth++;
    }
    
    if (frag < 0xffffffff){
      snprintf(buf, sizeof(buf), "F%d%s", frag, DIR_SEPARATOR);
      strcat(theName, buf);
      theLevelDepth++;
    }
    
    
    if (S_val < 0xffffffff){
      snprintf(buf, sizeof(buf), "S%d", S_val);
      strcat(theName, buf);
    }

    if (P_val < 0xff){
      snprintf(buf, sizeof(buf), "P%d", P_val);
      strcat(theName, buf);
    }
    
  }
  break;
  case 2:{
    const Uint32 seq = FsOpenReq::v2_getSequence(filenumber);
    const Uint32 nodeId = FsOpenReq::v2_getNodeId(filenumber);
    const Uint32 count = FsOpenReq::v2_getCount(filenumber);
    
    snprintf(buf, sizeof(buf), "BACKUP%sBACKUP-%d%s",
	     DIR_SEPARATOR, seq, DIR_SEPARATOR); 
    strcat(theName, buf);
    if(count == 0xffffffff) {
      snprintf(buf, sizeof(buf), "BACKUP-%d.%d",
	       seq, nodeId); strcat(theName, buf);
    } else {
      snprintf(buf, sizeof(buf), "BACKUP-%d-%d.%d",
	       seq, count, nodeId); strcat(theName, buf);
    }
    theLevelDepth = 2;
    break;
  }
  break;
  case 3:{
    const Uint32 diskNo = FsOpenReq::v1_getDisk(filenumber);

    if(diskNo == 0xFF){
      ERROR_SET(ecError, AFS_ERROR_PARAMETER,"","Invalid disk specification");
    }

    snprintf(buf, sizeof(buf), "D%d%s", diskNo, DIR_SEPARATOR);
    strcat(theName, buf);
    theLevelDepth++;
  }
  break;
  default:
    ERROR_SET(ecError, AFS_ERROR_PARAMETER,"","Wrong version");
  }
  if (type >= noOfExtensions){
    ERROR_SET(ecError, AFS_ERROR_PARAMETER,"","File Type doesn't exist");
    return;
  }
  strcat(theName, fileExtension[type]);
  
  if(dir == true){
186
    for(int l = strlen(theName) - 1; l >= 0; l--){
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
      if(theName[l] == DIR_SEPARATOR[0]){
	theName[l] = 0;
	break;
      }
    }
  }
}

/**
 * Find out directory name on level
 * Ex: 
 * theName = "/tmp/fs/T0/NDBFS/D0/P0/S27.data"
 * level = 1 
 * would return "/tmp/fs/T0/NDBFS/
 */
const char* Filename::directory(int level)
{
  const char* p;
  
  p = theName;
  p += strlen(theBaseDirectory);
  
  for (int i = 0; i <= level; i++){
    p = strstr(p, DIR_SEPARATOR);
    p++;
  } 
  
  strncpy(theDirectory, theName, p - theName - 1);
  theDirectory[p-theName-1] = 0;
  return theDirectory;
}