dmFile.c 4.2 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
Shengliang 已提交
17
#include "dmUtil.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20
#define MAXLEN 1024

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

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

S
shm  
Shengliang Guan 已提交
36
  len = taosReadFile(pFile, content, MAXLEN);
S
shm  
Shengliang Guan 已提交
37 38
  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
  }

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

  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 已提交
51
    goto _OVER;
S
shm  
Shengliang Guan 已提交
52
  }
S
shm  
Shengliang Guan 已提交
53
  *pDeployed = deployed->valueint != 0;
S
shm  
Shengliang Guan 已提交
54

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

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

  terrno = code;
  return code;
}

S
Shengliang 已提交
66
int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
S
shm  
Shengliang Guan 已提交
67 68 69 70 71 72 73
  int32_t   code = -1;
  int32_t   len = 0;
  char      content[MAXLEN + 1] = {0};
  char      file[PATH_MAX] = {0};
  char      realfile[PATH_MAX] = {0};
  TdFilePtr pFile = NULL;

S
Shengliang 已提交
74 75
  snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
  snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name);
S
shm  
Shengliang Guan 已提交
76

77
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
78
  if (pFile == NULL) {
S
shm  
Shengliang Guan 已提交
79
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
80
    dError("failed to write %s since %s", file, terrstr());
S
shm  
Shengliang Guan 已提交
81
    goto _OVER;
S
shm  
Shengliang Guan 已提交
82 83
  }

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

S
shm  
Shengliang Guan 已提交
88 89 90 91 92
  if (taosWriteFile(pFile, content, len) != len) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to write file:%s since %s", file, terrstr());
    goto _OVER;
  }
S
shm  
Shengliang Guan 已提交
93

S
shm  
Shengliang Guan 已提交
94 95 96 97 98
  if (taosFsyncFile(pFile) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to fsync file:%s since %s", file, terrstr());
    goto _OVER;
  }
S
shm  
Shengliang Guan 已提交
99

S
shm  
Shengliang Guan 已提交
100
  taosCloseFile(&pFile);
S
shm  
Shengliang Guan 已提交
101 102

  if (taosRenameFile(file, realfile) != 0) {
S
shm  
Shengliang Guan 已提交
103
    terrno = TAOS_SYSTEM_ERROR(errno);
S
shm  
Shengliang Guan 已提交
104 105 106 107
    dError("failed to rename %s since %s", file, terrstr());
    return -1;
  }

S
shm  
Shengliang Guan 已提交
108
  dInfo("successed to write %s, deployed:%d", realfile, deployed);
S
shm  
Shengliang Guan 已提交
109 110 111 112 113 114 115 116 117 118
  code = 0;

_OVER:
  if (pFile != NULL) {
    taosCloseFile(&pFile);
  }

  return code;
}

S
Shengliang Guan 已提交
119
TdFilePtr dmCheckRunning(const char *dataDir) {
S
shm  
Shengliang Guan 已提交
120 121 122
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP);

123
  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
S
shm  
Shengliang Guan 已提交
124 125
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
126
    dError("failed to open file:%s since %s", filepath, terrstr());
S
shm  
Shengliang Guan 已提交
127 128 129
    return NULL;
  }

130 131 132 133 134 135
  int32_t retryTimes = 0;
  int32_t ret = 0;
  do {
    ret = taosLockFile(pFile);
    if (ret == 0) break;
    terrno = TAOS_SYSTEM_ERROR(errno);
136
    taosMsleep(1000);
137 138
    retryTimes++;
    dError("failed to lock file:%s since %s, retryTimes:%d", filepath, terrstr(), retryTimes);
139
  } while (retryTimes < 12);
140 141

  if (ret < 0) {
S
shm  
Shengliang Guan 已提交
142 143 144 145 146
    terrno = TAOS_SYSTEM_ERROR(errno);
    taosCloseFile(&pFile);
    return NULL;
  }

147
  terrno = 0;
S
Shengliang Guan 已提交
148
  dDebug("lock file:%s to prevent repeated starts", filepath);
S
shm  
Shengliang Guan 已提交
149 150
  return pFile;
}