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"
18
#include "tjson.h"
S
shm  
Shengliang Guan 已提交
19

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

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

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

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

  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

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

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

  terrno = code;
  return code;
}

67 68 69 70 71
static int32_t dmEncodeFile(SJson *pJson, bool deployed) {
  if (tjsonAddDoubleToObject(pJson, "deployed", deployed) < 0) return -1;
  return 0;
}

S
Shengliang 已提交
72
int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
S
shm  
Shengliang Guan 已提交
73
  int32_t   code = -1;
74 75 76
  char     *buffer = NULL;
  SJson    *pJson = NULL;
  TdFilePtr pFile = NULL;
S
shm  
Shengliang Guan 已提交
77 78
  char      file[PATH_MAX] = {0};
  char      realfile[PATH_MAX] = {0};
S
Shengliang 已提交
79 80
  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 已提交
81

82 83 84 85 86 87 88
  terrno = TSDB_CODE_OUT_OF_MEMORY;
  pJson = tjsonCreateObject();
  if (pJson == NULL) goto _OVER;
  if (dmEncodeFile(pJson, deployed) != 0) goto _OVER;
  buffer = tjsonToString(pJson);
  if (buffer == NULL) goto _OVER;
  terrno = 0;
S
shm  
Shengliang Guan 已提交
89

90 91
  pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
  if (pFile == NULL) goto _OVER;
S
shm  
Shengliang Guan 已提交
92

93 94 95
  int32_t len = strlen(buffer);
  if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
  if (taosFsyncFile(pFile) < 0) goto _OVER;
S
shm  
Shengliang Guan 已提交
96

S
shm  
Shengliang Guan 已提交
97
  taosCloseFile(&pFile);
98
  if (taosRenameFile(file, realfile) != 0) goto _OVER;
S
shm  
Shengliang Guan 已提交
99

S
shm  
Shengliang Guan 已提交
100
  code = 0;
101
  dInfo("succeed to write file:%s, deloyed:%d", realfile, deployed);
S
shm  
Shengliang Guan 已提交
102 103

_OVER:
104 105 106
  if (pJson != NULL) tjsonDelete(pJson);
  if (buffer != NULL) taosMemoryFree(buffer);
  if (pFile != NULL) taosCloseFile(&pFile);
S
shm  
Shengliang Guan 已提交
107

108 109 110 111
  if (code != 0) {
    if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
    dError("failed to write file:%s since %s, deloyed:%d", realfile, terrstr(), deployed);
  }
S
shm  
Shengliang Guan 已提交
112 113 114
  return code;
}

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

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

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

  if (ret < 0) {
S
shm  
Shengliang Guan 已提交
138 139 140 141 142
    terrno = TAOS_SYSTEM_ERROR(errno);
    taosCloseFile(&pFile);
    return NULL;
  }

143
  terrno = 0;
S
Shengliang Guan 已提交
144
  dDebug("lock file:%s to prevent repeated starts", filepath);
S
shm  
Shengliang Guan 已提交
145 146
  return pFile;
}