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
Shengliang Guan 已提交
19 20
#define MAXLEN 1024

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

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

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

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

  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 已提交
54
    goto _OVER;
S
shm  
Shengliang Guan 已提交
55
  }
S
shm  
Shengliang Guan 已提交
56
  *pDeployed = deployed->valueint != 0;
S
shm  
Shengliang Guan 已提交
57 58

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

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

  terrno = code;
  return code;
}

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

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

S
Shengliang Guan 已提交
80 81 82
  int32_t       len = 0;
  const int32_t maxLen = MAXLEN;
  char          content[MAXLEN + 1] = {0};
S
shm  
Shengliang Guan 已提交
83 84

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

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

S
Shengliang Guan 已提交
92
  char realfile[PATH_MAX] = {0};
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;
}