dndFile.c 3.1 KB
Newer Older
S
shm  
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
shm  
Shengliang Guan 已提交
17
#include "dndInt.h"
S
shm  
Shengliang Guan 已提交
18

S
shm  
Shengliang Guan 已提交
19
int32_t dndReadFile(SMgmtWrapper *pWrapper, bool *pDeployed) {
S
shm  
Shengliang Guan 已提交
20
  int32_t   code = TSDB_CODE_NODE_PARSE_FILE_ERROR;
S
shm  
Shengliang Guan 已提交
21 22 23 24 25 26 27
  int32_t   len = 0;
  int32_t   maxLen = 1024;
  char     *content = calloc(1, maxLen + 1);
  cJSON    *root = NULL;
  char      file[PATH_MAX];
  TdFilePtr pFile = NULL;

S
shm  
Shengliang Guan 已提交
28
  snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
29 30 31 32
  pFile = taosOpenFile(file, TD_FILE_READ);
  if (pFile == NULL) {
    dDebug("file %s not exist", file);
    code = 0;
S
shm  
Shengliang Guan 已提交
33
    goto _OVER;
S
shm  
Shengliang Guan 已提交
34 35 36 37 38
  }

  len = (int32_t)taosReadFile(pFile, content, maxLen);
  if (len <= 0) {
    dError("failed to read %s since content is null", file);
S
shm  
Shengliang Guan 已提交
39
    goto _OVER;
S
shm  
Shengliang Guan 已提交
40 41 42 43 44 45
  }

  content[len] = 0;
  root = cJSON_Parse(content);
  if (root == NULL) {
    dError("failed to read %s since invalid json format", file);
S
shm  
Shengliang Guan 已提交
46
    goto _OVER;
S
shm  
Shengliang Guan 已提交
47 48 49 50 51
  }

  cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
  if (!deployed || deployed->type != cJSON_Number) {
    dError("failed to read %s since deployed not found", file);
S
shm  
Shengliang Guan 已提交
52
    goto _OVER;
S
shm  
Shengliang Guan 已提交
53
  }
S
shm  
Shengliang Guan 已提交
54
  *pDeployed = deployed->valueint != 0;
S
shm  
Shengliang Guan 已提交
55 56

  code = 0;
S
shm  
Shengliang Guan 已提交
57
  dDebug("succcessed to read file %s, deployed:%d", file, *pDeployed);
S
shm  
Shengliang Guan 已提交
58

S
shm  
Shengliang Guan 已提交
59
_OVER:
S
shm  
Shengliang Guan 已提交
60 61 62 63 64 65 66 67
  if (content != NULL) free(content);
  if (root != NULL) cJSON_Delete(root);
  if (pFile != NULL) taosCloseFile(&pFile);

  terrno = code;
  return code;
}

S
shm  
Shengliang Guan 已提交
68
int32_t dndWriteFile(SMgmtWrapper *pWrapper, bool deployed) {
S
shm  
Shengliang Guan 已提交
69
  char file[PATH_MAX];
S
shm  
Shengliang Guan 已提交
70
  snprintf(file, sizeof(file), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
71 72 73

  TdFilePtr pFile = taosOpenFile(file, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
74
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
75 76 77 78 79 80 81 82 83
    dError("failed to write %s since %s", file, terrstr());
    return -1;
  }

  int32_t len = 0;
  int32_t maxLen = 1024;
  char   *content = calloc(1, maxLen + 1);

  len += snprintf(content + len, maxLen - len, "{\n");
S
shm  
Shengliang Guan 已提交
84
  len += snprintf(content + len, maxLen - len, "  \"deployed\": %d\n", deployed);
S
shm  
Shengliang Guan 已提交
85 86 87 88 89 90 91 92
  len += snprintf(content + len, maxLen - len, "}\n");

  taosWriteFile(pFile, content, len);
  taosFsyncFile(pFile);
  taosCloseFile(&pFile);
  free(content);

  char realfile[PATH_MAX];
S
shm  
Shengliang Guan 已提交
93
  snprintf(realfile, sizeof(realfile), "%s%s%s.json", pWrapper->path, TD_DIRSEP, pWrapper->name);
S
shm  
Shengliang Guan 已提交
94 95

  if (taosRenameFile(file, realfile) != 0) {
S
shm  
Shengliang Guan 已提交
96
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
97 98 99 100
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
101
  dInfo("successed to write %s, deployed:%d", realfile, deployed);
S
shm  
Shengliang Guan 已提交
102 103
  return 0;
}