You need to sign in or sign up before continuing.
smaUtil.c 7.9 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "sma.h"

C
Cary Xu 已提交
18 19
// smaFileUtil ================

C
Cary Xu 已提交
20 21 22 23 24 25 26 27 28 29 30 31
#define TD_FILE_STATE_OK  0
#define TD_FILE_STATE_BAD 1

#define TD_FILE_INIT_MAGIC 0xFFFFFFFF

static int32_t tdEncodeTFInfo(void **buf, STFInfo *pInfo);
static void   *tdDecodeTFInfo(void *buf, STFInfo *pInfo);

static int32_t tdEncodeTFInfo(void **buf, STFInfo *pInfo) {
  int32_t tlen = 0;

  tlen += taosEncodeFixedU32(buf, pInfo->magic);
C
Cary Xu 已提交
32
  tlen += taosEncodeFixedU32(buf, pInfo->ftype);
C
Cary Xu 已提交
33
  tlen += taosEncodeFixedU32(buf, pInfo->fver);
C
Cary Xu 已提交
34
  tlen += taosEncodeFixedI64(buf, pInfo->fsize);
C
Cary Xu 已提交
35 36 37 38 39 40

  return tlen;
}

static void *tdDecodeTFInfo(void *buf, STFInfo *pInfo) {
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
C
Cary Xu 已提交
41
  buf = taosDecodeFixedU32(buf, &(pInfo->ftype));
C
Cary Xu 已提交
42
  buf = taosDecodeFixedU32(buf, &(pInfo->fver));
C
Cary Xu 已提交
43
  buf = taosDecodeFixedI64(buf, &(pInfo->fsize));
C
Cary Xu 已提交
44 45 46 47
  return buf;
}

int64_t tdWriteTFile(STFile *pTFile, void *buf, int64_t nbyte) {
C
Cary Xu 已提交
48
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
49 50 51 52 53 54 55 56 57 58 59

  int64_t nwrite = taosWriteFile(pTFile->pFile, buf, nbyte);
  if (nwrite < nbyte) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nwrite;
}

int64_t tdSeekTFile(STFile *pTFile, int64_t offset, int whence) {
C
Cary Xu 已提交
60
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
61

C
Cary Xu 已提交
62
  int64_t loffset = taosLSeekFile(TD_TFILE_PFILE(pTFile), offset, whence);
C
Cary Xu 已提交
63 64 65 66 67 68 69 70
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}

71
int64_t tdGetTFileSize(STFile *pTFile, int64_t *size) {
C
Cary Xu 已提交
72
  ASSERT(TD_TFILE_OPENED(pTFile));
73 74 75
  return taosFStatFile(pTFile->pFile, size, NULL);
}

C
Cary Xu 已提交
76
int64_t tdReadTFile(STFile *pTFile, void *buf, int64_t nbyte) {
C
Cary Xu 已提交
77
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

  int64_t nread = taosReadFile(pTFile->pFile, buf, nbyte);
  if (nread < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nread;
}

int32_t tdUpdateTFileHeader(STFile *pTFile) {
  char buf[TD_FILE_HEAD_SIZE] = "\0";

  if (tdSeekTFile(pTFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  void *ptr = buf;
  tdEncodeTFInfo(&ptr, &(pTFile->info));

  taosCalcChecksumAppend(0, (uint8_t *)buf, TD_FILE_HEAD_SIZE);
  if (tdWriteTFile(pTFile, buf, TD_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  return 0;
}

int32_t tdLoadTFileHeader(STFile *pTFile, STFInfo *pInfo) {
  char     buf[TD_FILE_HEAD_SIZE] = "\0";
  uint32_t _version;

C
Cary Xu 已提交
110
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  if (tdSeekTFile(pTFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  if (tdReadTFile(pTFile, buf, TD_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  if (!taosCheckChecksumWhole((uint8_t *)buf, TD_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_FILE_CORRUPTED;
    return -1;
  }

  void *pBuf = buf;
  pBuf = tdDecodeTFInfo(pBuf, pInfo);
  return 0;
}

void tdUpdateTFileMagic(STFile *pTFile, void *pCksm) {
  pTFile->info.magic = taosCalcChecksum(pTFile->info.magic, (uint8_t *)(pCksm), sizeof(TSCKSUM));
}

int64_t tdAppendTFile(STFile *pTFile, void *buf, int64_t nbyte, int64_t *offset) {
C
Cary Xu 已提交
135
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
136 137 138 139 140 141 142

  int64_t toffset;

  if ((toffset = tdSeekTFile(pTFile, 0, SEEK_END)) < 0) {
    return -1;
  }

C
Cary Xu 已提交
143
#if 0
144 145
  smaDebug("append to file %s, offset:%" PRIi64 " nbyte:%" PRIi64 " fsize:%" PRIi64, TD_TFILE_FULL_NAME(pTFile),
           toffset, nbyte, toffset + nbyte);
C
Cary Xu 已提交
146 147
#endif

C
Cary Xu 已提交
148
  ASSERT(pTFile->info.fsize == toffset);
C
Cary Xu 已提交
149 150 151 152 153 154 155 156 157

  if (offset) {
    *offset = toffset;
  }

  if (tdWriteTFile(pTFile, buf, nbyte) < 0) {
    return -1;
  }

C
Cary Xu 已提交
158
  pTFile->info.fsize += nbyte;
C
Cary Xu 已提交
159 160 161 162 163

  return nbyte;
}

int32_t tdOpenTFile(STFile *pTFile, int flags) {
C
Cary Xu 已提交
164
  ASSERT(!TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
165

C
Cary Xu 已提交
166
  pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), flags);
C
Cary Xu 已提交
167 168 169 170 171 172 173 174 175
  if (pTFile->pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

void tdCloseTFile(STFile *pTFile) {
C
Cary Xu 已提交
176
  if (TD_TFILE_OPENED(pTFile)) {
C
Cary Xu 已提交
177
    taosCloseFile(&pTFile->pFile);
C
Cary Xu 已提交
178
    TD_TFILE_SET_CLOSED(pTFile);
C
Cary Xu 已提交
179 180 181
  }
}

C
Cary Xu 已提交
182 183
void tdDestroyTFile(STFile *pTFile) { taosMemoryFreeClear(TD_TFILE_FULL_NAME(pTFile)); }

C
Cary Xu 已提交
184 185
void tdGetVndFileName(int32_t vgId, const char *pdname, const char *dname, const char *fname, int64_t version,
                      char *outputName) {
C
Cary Xu 已提交
186
  if (version < 0) {
C
Cary Xu 已提交
187 188 189 190 191 192 193
    if (pdname) {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s%sv%d%s", pdname, TD_DIRSEP, TD_DIRSEP, vgId,
               TD_DIRSEP, dname, TD_DIRSEP, vgId, fname);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s%sv%d%s", TD_DIRSEP, vgId, TD_DIRSEP, dname, TD_DIRSEP,
               vgId, fname);
    }
C
Cary Xu 已提交
194
  } else {
C
Cary Xu 已提交
195 196 197 198 199 200 201
    if (pdname) {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s%sv%d%s%" PRIi64, pdname, TD_DIRSEP, TD_DIRSEP,
               vgId, TD_DIRSEP, dname, TD_DIRSEP, vgId, fname, version);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s%sv%d%s%" PRIi64, TD_DIRSEP, vgId, TD_DIRSEP, dname,
               TD_DIRSEP, vgId, fname, version);
    }
C
Cary Xu 已提交
202
  }
C
Cary Xu 已提交
203 204
}

C
Cary Xu 已提交
205
void tdGetVndDirName(int32_t vgId, const char *pdname, const char *dname, bool endWithSep, char *outputName) {
C
Cary Xu 已提交
206
  if (pdname) {
C
Cary Xu 已提交
207 208 209 210 211 212 213
    if (endWithSep) {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s%s", pdname, TD_DIRSEP, TD_DIRSEP, vgId, TD_DIRSEP,
               dname, TD_DIRSEP);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s", pdname, TD_DIRSEP, TD_DIRSEP, vgId, TD_DIRSEP,
               dname);
    }
C
Cary Xu 已提交
214
  } else {
C
Cary Xu 已提交
215 216 217 218 219
    if (endWithSep) {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s%s", TD_DIRSEP, vgId, TD_DIRSEP, dname, TD_DIRSEP);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s", TD_DIRSEP, vgId, TD_DIRSEP, dname);
    }
C
Cary Xu 已提交
220
  }
C
Cary Xu 已提交
221
}
C
Cary Xu 已提交
222

C
Cary Xu 已提交
223
int32_t tdInitTFile(STFile *pTFile, const char *dname, const char *fname) {
C
Cary Xu 已提交
224 225
  TD_TFILE_SET_STATE(pTFile, TD_FILE_STATE_OK);
  TD_TFILE_SET_CLOSED(pTFile);
C
Cary Xu 已提交
226 227 228 229

  memset(&(pTFile->info), 0, sizeof(pTFile->info));
  pTFile->info.magic = TD_FILE_INIT_MAGIC;

C
Cary Xu 已提交
230 231 232 233 234 235
  char tmpName[TSDB_FILENAME_LEN * 2 + 32] = {0};
  snprintf(tmpName, TSDB_FILENAME_LEN * 2 + 32, "%s%s%s", dname, TD_DIRSEP, fname);
  int32_t tmpNameLen = strlen(tmpName) + 1;
  pTFile->fname = taosMemoryMalloc(tmpNameLen);
  if (!pTFile->fname) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
C
Cary Xu 已提交
236 237
    return -1;
  }
C
Cary Xu 已提交
238
  tstrncpy(pTFile->fname, tmpName, tmpNameLen);
C
Cary Xu 已提交
239 240 241 242

  return 0;
}

C
Cary Xu 已提交
243
int32_t tdCreateTFile(STFile *pTFile, bool updateHeader, int8_t fType) {
C
Cary Xu 已提交
244
  ASSERT(pTFile->info.fsize == 0 && pTFile->info.magic == TD_FILE_INIT_MAGIC);
C
Cary Xu 已提交
245
  pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
C
Cary Xu 已提交
246 247 248
  if (pTFile->pFile == NULL) {
    if (errno == ENOENT) {
      // Try to create directory recursively
C
Cary Xu 已提交
249 250 251 252 253 254 255 256 257
      char *s = strdup(TD_TFILE_FULL_NAME(pTFile));
      if (taosMulMkDir(taosDirName(s)) != 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        taosMemoryFree(s);
        return -1;
      }
      taosMemoryFree(s);
      pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
      if (pTFile->pFile == NULL) {
C
Cary Xu 已提交
258 259 260 261
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    }
C
Cary Xu 已提交
262
  }
C
Cary Xu 已提交
263

C
Cary Xu 已提交
264 265 266
  if (!updateHeader) {
    return 0;
  }
C
Cary Xu 已提交
267

C
Cary Xu 已提交
268 269
  pTFile->info.fsize += TD_FILE_HEAD_SIZE;
  pTFile->info.fver = 0;
C
Cary Xu 已提交
270

C
Cary Xu 已提交
271 272 273 274
  if (tdUpdateTFileHeader(pTFile) < 0) {
    tdCloseTFile(pTFile);
    tdRemoveTFile(pTFile);
    return -1;
C
Cary Xu 已提交
275 276 277 278 279
  }

  return 0;
}

C
Cary Xu 已提交
280 281 282 283 284 285 286
int32_t tdRemoveTFile(STFile *pTFile) {
  if (taosRemoveFile(TD_TFILE_FULL_NAME(pTFile)) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  };
  return 0;
}
C
Cary Xu 已提交
287 288 289

// smaXXXUtil ================
// ...