• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4308

14 Jun 2025 02:06PM UTC coverage: 62.454% (-0.3%) from 62.777%
#4308

push

travis-ci

web-flow
fix: taosdump windows pthread_mutex_unlock crash(3.0) (#31357)

* fix: windows pthread_mutex_unlock crash

* enh: sync from main fix taosdump crash windows

* fix: restore .github action branch to main

153985 of 315105 branches covered (48.87%)

Branch coverage included in aggregate %.

238120 of 312727 relevant lines covered (76.14%)

6462519.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.03
/source/common/src/tname.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "tname.h"
18
#include "tcommon.h"
19

20
#define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T)
21

22
void toName(int32_t acctId, const char* pDbName, const char* pTableName, SName* pName) {
4,209,772✔
23
  if (pName == NULL){
4,209,772!
24
    return;
×
25
  }
26
  pName->type = TSDB_TABLE_NAME_T;
4,209,772✔
27
  pName->acctId = acctId;
4,209,772✔
28
  snprintf(pName->dbname, sizeof(pName->dbname), "%s", pDbName);
4,209,772✔
29
  snprintf(pName->tname, sizeof(pName->tname), "%s", pTableName);
4,209,772✔
30
}
31

32
int32_t tNameExtractFullName(const SName* name, char* dst) {
14,000,190✔
33
  // invalid full name format, abort
34
  if (!tNameIsValid(name)) {
14,000,190✔
35
    return TSDB_CODE_INVALID_PARA;
10✔
36
  }
37

38
  int32_t len = tsnprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
14,000,529✔
39

40
  size_t tnameLen = strlen(name->tname);
14,000,807✔
41
  if (tnameLen > 0) {
14,000,807✔
42
    dst[len] = TS_PATH_DELIMITER[0];
13,993,032✔
43

44
    memcpy(dst + len + 1, name->tname, tnameLen);
13,993,032✔
45
    dst[len + tnameLen + 1] = 0;
13,993,032✔
46
  }
47

48
  return 0;
14,000,807✔
49
}
50

51
int32_t tNameLen(const SName* name) {
×
52
  char    tmp[12] = {0};
×
53
  int32_t len = sprintf(tmp, "%d", name->acctId);
×
54
  int32_t len1 = (int32_t)strlen(name->dbname);
×
55
  int32_t len2 = (int32_t)strlen(name->tname);
×
56

57
  if (name->type == TSDB_DB_NAME_T) {
×
58
    return len + len1 + TSDB_NAME_DELIMITER_LEN;
×
59
  } else {
60
    return len + len1 + len2 + TSDB_NAME_DELIMITER_LEN * 2;
×
61
  }
62
}
63

64
bool tNameIsValid(const SName* name) {
14,000,197✔
65
  if (!VALID_NAME_TYPE(name->type)) {
14,000,197✔
66
    return false;
1✔
67
  }
68

69
  if (name->type == TSDB_DB_NAME_T) {
14,000,196✔
70
    return strlen(name->dbname) > 0;
8,803✔
71
  } else {
72
    return strlen(name->dbname) > 0 && strlen(name->tname) > 0;
13,991,393!
73
  }
74
}
75

76
SName* tNameDup(const SName* name) {
×
77
  SName* p = taosMemoryMalloc(sizeof(SName));
×
78
  if (p) TAOS_MEMCPY(p, name, sizeof(SName));
×
79
  return p;
×
80
}
81

82
int32_t tNameGetDbName(const SName* name, char* dst) {
42,507✔
83
  tstrncpy(dst, name->dbname, tListLen(name->dbname));
42,507✔
84
  return 0;
42,507✔
85
}
86

87
const char* tNameGetDbNameP(const SName* name) { return &name->dbname[0]; }
×
88

89
int32_t tNameGetFullDbName(const SName* name, char* dst) {
13,696,895✔
90
  if (name == NULL || dst == NULL) {
13,696,895!
91
    return TSDB_CODE_INVALID_PARA;
×
92
  }
93
  (void)snprintf(dst, TSDB_DB_FNAME_LEN, "%d.%s", name->acctId, name->dbname);
13,697,170✔
94
  return 0;
13,697,170✔
95
}
96

97
bool tNameIsEmpty(const SName* name) { return name->type == 0 || name->acctId == 0; }
×
98

99
const char* tNameGetTableName(const SName* name) {
535,153✔
100
  if (!(name != NULL && name->type == TSDB_TABLE_NAME_T)) {
535,153!
101
    terrno = TSDB_CODE_INVALID_PARA;
×
102
    return NULL;
×
103
  }
104
  return &name->tname[0];
535,185✔
105
}
106

107
int32_t tNameGetFullTableName(const SName* name, char* dst) {
×
108
  if (name == NULL || dst == NULL) {
×
109
    return TSDB_CODE_INVALID_PARA;
×
110
  }
111
  (void)snprintf(dst, TSDB_TABLE_FNAME_LEN, "%s.%s", name->dbname, name->tname);
×
112
  return 0;
×
113
}
114

115
void tNameAssign(SName* dst, const SName* src) { memcpy(dst, src, sizeof(SName)); }
52,426✔
116

117
int32_t tNameSetDbName(SName* dst, int32_t acct, const char* dbName, size_t nameLen) {
2,196,023✔
118
  // too long account id or too long db name
119
  if (nameLen <= 0 || nameLen >= tListLen(dst->dbname)) {
2,196,023!
120
    return TSDB_CODE_INVALID_PARA;
×
121
  }
122

123
  dst->type = TSDB_DB_NAME_T;
2,196,155✔
124
  dst->acctId = acct;
2,196,155✔
125
  tstrncpy(dst->dbname, dbName, nameLen + 1);
2,196,155✔
126
  return 0;
2,196,155✔
127
}
128

129
int32_t tNameAddTbName(SName* dst, const char* tbName, size_t nameLen) {
4,934✔
130
  // too long account id or too long db name
131
  if (nameLen >= tListLen(dst->tname) || nameLen <= 0) {
4,934!
132
    return TSDB_CODE_INVALID_PARA;
×
133
  }
134

135
  dst->type = TSDB_TABLE_NAME_T;
4,934✔
136
  tstrncpy(dst->tname, tbName, nameLen + 1);
4,934✔
137
  return 0;
4,934✔
138
}
139

140
int32_t tNameSetAcctId(SName* dst, int32_t acctId) {
×
141
  dst->acctId = acctId;
×
142
  return 0;
×
143
}
144

145
bool tNameDBNameEqual(SName* left, SName* right) {
157✔
146
  if (NULL == left) {
157!
147
    if (NULL == right) {
×
148
      return true;
×
149
    }
150

151
    return false;
×
152
  }
153

154
  if (NULL == right) {
157!
155
    return false;
×
156
  }
157

158
  if (left->acctId != right->acctId) {
157!
159
    return false;
×
160
  }
161

162
  return (0 == strcmp(left->dbname, right->dbname));
157✔
163
}
164

165
bool tNameTbNameEqual(SName* left, SName* right) {
157✔
166
  bool equal = tNameDBNameEqual(left, right);
157✔
167
  if (equal) {
157✔
168
    return (0 == strcmp(left->tname, right->tname));
133✔
169
  }
170

171
  return equal;
24✔
172
}
173

174
int32_t tNameFromString(SName* dst, const char* str, uint32_t type) {
10,267,753✔
175
  if (strlen(str) == 0) {
10,267,753✔
176
    return TSDB_CODE_INVALID_PARA;
1✔
177
  }
178

179
  char* p = NULL;
10,267,752✔
180
  if ((type & T_NAME_ACCT) == T_NAME_ACCT) {
10,267,752✔
181
    p = strstr(str, TS_PATH_DELIMITER);
8,188,165✔
182
    if (p == NULL) {
8,188,165!
183
      return TSDB_CODE_INVALID_PARA;
×
184
    }
185

186
    dst->acctId = taosStr2Int32(str, NULL, 10);
8,188,165✔
187
  }
188

189
  if ((type & T_NAME_DB) == T_NAME_DB) {
10,267,702✔
190
    dst->type = TSDB_DB_NAME_T;
8,187,104✔
191
    char* start = (char*)((p == NULL) ? str : (p + 1));
8,187,104!
192

193
    int32_t len = 0;
8,187,104✔
194
    p = strstr(start, TS_PATH_DELIMITER);
8,187,104✔
195
    if (p == NULL) {
8,187,104✔
196
      len = (int32_t)strlen(start);
2,140,928✔
197
    } else {
198
      len = (int32_t)(p - start);
6,046,176✔
199
    }
200

201
    // too long account id or too long db name
202
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
8,187,104!
203
      return TSDB_CODE_INVALID_PARA;
×
204
    }
205

206
    memcpy(dst->dbname, start, len);
8,187,113✔
207
    dst->dbname[len] = 0;
8,187,113✔
208
  }
209

210
  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
10,267,711✔
211
    dst->type = TSDB_TABLE_NAME_T;
8,129,590✔
212
    char* start = (char*)((p == NULL) ? str : (p + 1));
8,129,590✔
213

214
    // too long account id or too long db name
215
    int32_t len = (int32_t)strlen(start);
8,129,590✔
216
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
8,129,590!
217
      return TSDB_CODE_INVALID_PARA;
×
218
    }
219

220
    memcpy(dst->tname, start, len);
8,129,644✔
221
    dst->tname[len] = 0;
8,129,644✔
222
  }
223

224
  return 0;
10,267,765✔
225
}
226

227
static int compareKv(const void* p1, const void* p2) {
29,432✔
228
  SSmlKv* kv1 = (SSmlKv*)p1;
29,432✔
229
  SSmlKv* kv2 = (SSmlKv*)p2;
29,432✔
230
  int32_t kvLen1 = kv1->keyLen;
29,432✔
231
  int32_t kvLen2 = kv2->keyLen;
29,432✔
232
  int32_t res = taosStrncasecmp(kv1->key, kv2->key, TMIN(kvLen1, kvLen2));
29,432✔
233
  if (res != 0) {
29,432✔
234
    return res;
29,208✔
235
  } else {
236
    return kvLen1 - kvLen2;
224✔
237
  }
238
}
239

240
/*
241
 * use stable name and tags to generate child table name
242
 */
243
int32_t buildChildTableName(RandTableName* rName) {
4,243✔
244
  taosArraySort(rName->tags, compareKv);
4,243✔
245

246
  T_MD5_CTX context;
247
  tMD5Init(&context);
4,243✔
248

249
  // add stable name
250
  tMD5Update(&context, (uint8_t*)rName->stbFullName, rName->stbFullNameLen);
4,244✔
251

252
  // add tags
253
  for (int j = 0; j < taosArrayGetSize(rName->tags); ++j) {
24,187✔
254
    SSmlKv* tagKv = taosArrayGet(rName->tags, j);
19,939✔
255
    if (tagKv == NULL) {
19,937!
256
      return TSDB_CODE_SML_INVALID_DATA;
×
257
    }
258

259
    tMD5Update(&context, (uint8_t*)",", 1);
19,937✔
260
    tMD5Update(&context, (uint8_t*)tagKv->key, tagKv->keyLen);
19,937✔
261
    tMD5Update(&context, (uint8_t*)"=", 1);
19,938✔
262

263
    if (IS_VAR_DATA_TYPE(tagKv->type)) {
19,937!
264
      tMD5Update(&context, (uint8_t*)tagKv->value, tagKv->length);
13,919✔
265
    } else {
266
      tMD5Update(&context, (uint8_t*)(&(tagKv->value)), tagKv->length);
6,018✔
267
    }
268
  }
269

270
  tMD5Final(&context);
4,244✔
271

272
  rName->ctbShortName[0] = 't';
4,245✔
273
  rName->ctbShortName[1] = '_';
4,245✔
274
  taosByteArrayToHexStr(context.digest, 16, rName->ctbShortName + 2);
4,245✔
275
  rName->ctbShortName[34] = 0;
4,245✔
276

277
  return TSDB_CODE_SUCCESS;
4,245✔
278
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc