tversion.c 2.3 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "tversion.h"
S
Shengliang Guan 已提交
18 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include "taoserror.h"

int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
  if (vstr == NULL) {
    terrno = TSDB_CODE_INVALID_VERSION_STRING;
    return -1;
  }

  int32_t vnum[4] = {0};
  int32_t len = strlen(vstr);
  char    tmp[16] = {0};

  for (int32_t spos = 0, tpos = 0, vpos = 0; spos < len && vpos < 4; ++spos) {
    if (vstr[spos] != '.') {
      tmp[spos - tpos] = vstr[spos];
    } else {
      vnum[vpos] = atoi(tmp);
      memset(tmp, 0, sizeof(tmp));
      vpos++;
      tpos = spos + 1;
    }
  }

  if (vnum[0] <= 0) {
    terrno = TSDB_CODE_INVALID_VERSION_STRING;
    return -1;
  }

  *vint = vnum[0] * 1000000 + vnum[1] * 10000 + vnum[2] * 100 + vnum[3];
  return 0;
}

int32_t taosVersionIntToStr(int32_t vint, char *vstr, int32_t len) {
  int32_t s1 = (vint % 100000000) / 1000000;
  int32_t s2 = (vint % 1000000) / 10000;
  int32_t s3 = (vint % 10000) / 100;
  int32_t s4 = vint % 100;
  if (s1 <= 0) {
    terrno = TSDB_CODE_INVALID_VERSION_NUMBER;
    return -1;
  }

  snprintf(vstr, len, "%02d.%02d.%02d.%02d", s1, s2, s3, s4);
  return 0;
}

int32_t taosCheckVersionCompatible(int32_t clientVer, int32_t serverVer, int32_t comparedSegments) {
  switch (comparedSegments) {
    case 4:
      break;
    case 3:
      clientVer %= 100;
      serverVer %= 100;
      break;
    case 2:
      clientVer %= 10000;
      serverVer %= 10000;
      break;
    case 1:
      clientVer %= 1000000;
      serverVer %= 1000000;
      break;
    default:
      terrno = TSDB_CODE_INVALID_VERSION_NUMBER;
      return -1;
  }

  if (clientVer == serverVer) {
    return 0;
  } else {
    terrno = TSDB_CODE_VERSION_NOT_COMPATIBLE;
    return -1;
  }
}