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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

59.38
/source/os/src/osDir.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
#define ALLOW_FORBID_FUNC
18

19
#include "os.h"
20

21
#ifdef WINDOWS
22

23
#include <windows.h>
24

25
typedef struct TdDirEntry {
26
  WIN32_FIND_DATA findFileData;
27
} TdDirEntry;
28

29
typedef struct TdDir {
30
  TdDirEntry dirEntry;
31
  HANDLE     hFind;
32
} TdDir;
33

34
enum {
35
  WRDE_NOSPACE = 1, /* Ran out of memory.  */
36
  WRDE_BADCHAR,     /* A metachar appears in the wrong place.  */
37
  WRDE_BADVAL,      /* Undefined var reference with WRDE_UNDEF.  */
38
  WRDE_CMDSUB,      /* Command substitution with WRDE_NOCMD.  */
39
  WRDE_SYNTAX       /* Shell syntax error.  */
40
};
41

42
int wordexp(char *words, wordexp_t *pwordexp, int flags) {
43
  pwordexp->we_offs = 0;
44
  pwordexp->we_wordc = 1;
45
  pwordexp->we_wordv[0] = pwordexp->wordPos;
46

47
  (void)memset(pwordexp->wordPos, 0, 1025);
48
  if (_fullpath(pwordexp->wordPos, words, 1024) == NULL) {
49
    pwordexp->we_wordv[0] = words;
50
    printf("failed to parse relative path:%s to abs path\n", words);
51
    return -1;
52
  }
53

54
  // printf("parse relative path:%s to abs path:%s\n", words, pwordexp->wordPos);
55
  return 0;
56
}
57

58
void wordfree(wordexp_t *pwordexp) {}
59

60
#elif defined(DARWIN)
61

62
#include <dirent.h>
63
#include <fcntl.h>
64
#include <sys/stat.h>
65
#include <unistd.h>
66
#include <wordexp.h>
67

68
typedef struct dirent dirent;
69
typedef struct dirent TdDirEntry;
70

71
typedef struct TdDir {
72
  TdDirEntry    dirEntry;
73
  TdDirEntry    dirEntry1;
74
  TdDirEntryPtr dirEntryPtr;
75
  DIR          *pDir;
76
} TdDir;
77

78
#else
79

80
#include <dirent.h>
81
#include <fcntl.h>
82
#include <sys/stat.h>
83
#include <unistd.h>
84
#include <wordexp.h>
85

86
typedef struct dirent dirent;
87
typedef struct DIR    TdDir;
88
typedef struct dirent TdDirEntry;
89

90
#endif
91

92
#define TDDIRMAXLEN 1024
93

94
void taosRemoveDir(const char *dirname) {
56,000✔
95
  TdDirPtr pDir = taosOpenDir(dirname);
56,000✔
96
  if (pDir == NULL) return;
56,004✔
97

98
  TdDirEntryPtr de = NULL;
54,433✔
99
  while ((de = taosReadDir(pDir)) != NULL) {
323,945✔
100
    if (strcmp(taosGetDirEntryName(de), ".") == 0 || strcmp(taosGetDirEntryName(de), "..") == 0) continue;
269,515✔
101

102
    char filename[1024] = {0};
160,658✔
103
    (void)snprintf(filename, sizeof(filename), "%s%s%s", dirname, TD_DIRSEP, taosGetDirEntryName(de));
160,658✔
104
    if (taosDirEntryIsDir(de)) {
160,652✔
105
      taosRemoveDir(filename);
41,410✔
106
    } else {
107
      TAOS_UNUSED(taosRemoveFile(filename));
119,255✔
108
      // printf("file:%s is removed\n", filename);
109
    }
110
  }
111

112
  TAOS_UNUSED(taosCloseDir(&pDir));
54,425✔
113
  TAOS_UNUSED(rmdir(dirname));
54,433✔
114

115
  // printf("dir:%s is removed\n", dirname);
116
  return;
54,431✔
117
}
118

119
bool taosDirExist(const char *dirname) {
340,737✔
120
  if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return false;
340,737!
121
  return taosCheckExistFile(dirname);
340,769✔
122
}
123

124
int32_t taosMkDir(const char *dirname) {
252,939✔
125
  if (taosDirExist(dirname)) return 0;
252,939✔
126
#ifdef WINDOWS
127
  int32_t code = _mkdir(dirname, 0755);
128
#elif defined(DARWIN)
129
  int32_t code = mkdir(dirname, 0777);
130
#else
131
  int32_t code = mkdir(dirname, 0755);
75,726✔
132
#endif
133
  if (-1 == code) {
75,726✔
134
    if (errno == EEXIST) {
34✔
135
      return 0;
3✔
136
    } else {
137
      terrno = TAOS_SYSTEM_ERROR(errno);
31✔
138
      code = terrno;
31✔
139
    }
140
  }
141

142
  return code;
75,733✔
143
}
144

145
int32_t taosMulMkDir(const char *dirname) {
23,424✔
146
  if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) return -1;
23,424!
147
  char    temp[TDDIRMAXLEN];
148
  char   *pos = temp;
23,425✔
149
  int32_t code = 0;
23,425✔
150
#ifdef WINDOWS
151
  code = taosRealPath(dirname, temp, sizeof(temp));
152
  if(code != 0) {
153
    return code;
154
  }
155
  if (temp[1] == ':') pos += 3;
156
#else
157
  tstrncpy(temp, dirname, sizeof(temp));
23,425✔
158
#endif
159

160
  if (taosDirExist(temp)) return code;
23,425✔
161

162
  if (strncmp(temp, TD_DIRSEP, 1) == 0) {
9,687✔
163
    pos += 1;
9,685✔
164
  } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) {
2!
165
    pos += 2;
×
166
  }
167

168
  for (; *pos != '\0'; pos++) {
873,568✔
169
    if (*pos == TD_DIRSEP[0]) {
863,869✔
170
      *pos = '\0';
94,470✔
171
#ifdef WINDOWS
172
      code = _mkdir(temp, 0755);
173
#elif defined(DARWIN)
174
      code = mkdir(temp, 0777);
175
#else
176
      code = mkdir(temp, 0755);
94,470✔
177
#endif
178
      if (code < 0 && errno != EEXIST) {
94,482!
179
        terrno = TAOS_SYSTEM_ERROR(errno);
×
180
        return code;
×
181
      }
182
      *pos = TD_DIRSEP[0];
94,482✔
183
    }
184
  }
185

186
  if (*(pos - 1) != TD_DIRSEP[0]) {
9,699✔
187
#ifdef WINDOWS
188
    code = _mkdir(temp, 0755);
189
#elif defined(DARWIN)
190
    code = mkdir(dirname, 0777);
191
#else
192
    code = mkdir(temp, 0755);
9,686✔
193
#endif
194
    if (code < 0 && errno != EEXIST) {
9,684!
195
       terrno = TAOS_SYSTEM_ERROR(errno);
×
196
      return code;
×
197
    }
198
  }
199

200
  if (code < 0 && errno == EEXIST) {
9,697!
201
    return 0;
×
202
  }
203

204
  return code;
9,697✔
205
}
206

207
int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) {
50,124✔
208
  if (dirname == NULL || strlen(dirname) >= TDDIRMAXLEN) {
50,124!
UNCOV
209
    terrno = TSDB_CODE_INVALID_PARA;
×
210
    return terrno;
×
211
  }
212
  char    temp[TDDIRMAXLEN];
213
  char   *pos = temp;
50,126✔
214
  int32_t code = 0;
50,126✔
215
#ifdef WINDOWS
216
  code = taosRealPath(dirname, temp, sizeof(temp));
217
  if(code != 0) {
218
    return code;
219
  }
220
  if (temp[1] == ':') pos += 3;
221
#else
222
  tstrncpy(temp, dirname, sizeof(temp));
50,126✔
223
#endif
224

225
  if (taosDirExist(temp)) {
50,126✔
226
    if (checkAccess && taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) {
30,444!
227
      return 0;
13,313✔
228
    }
229

230
    code = chmod(temp, mode);
17,131✔
231
    if (-1 == code) {
17,105!
UNCOV
232
      struct stat statbuf = {0};
×
UNCOV
233
      code = stat(temp, &statbuf);
×
234
      if (code != 0 || (statbuf.st_mode & mode) != mode) {
×
235
        terrno = TAOS_SYSTEM_ERROR(errno);
×
236
        return terrno;
×
237
      }
238
    }
239
  }
240

241
  if (strncmp(temp, TD_DIRSEP, 1) == 0) {
36,802!
242
    pos += 1;
36,802✔
243
  } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) {
×
244
    pos += 2;
×
245
  }
246

247
  for (; *pos != '\0'; pos++) {
2,264,618✔
248
    if (*pos == TD_DIRSEP[0]) {
2,227,708✔
249
      *pos = '\0';
307,373✔
250
#ifdef WINDOWS
251
      code = _mkdir(temp, mode);
252
#elif defined(DARWIN)
253
      code = mkdir(temp, 0777);
254
#else
255
      code = mkdir(temp, mode);
307,373✔
256
#endif
257
      if (code < 0 && errno != EEXIST) {
307,481!
258
        terrno = TAOS_SYSTEM_ERROR(errno);
×
259
        return terrno;
×
260
      }
261
      *pos = TD_DIRSEP[0];
307,481✔
262
    }
263
  }
264

265
  if (*(pos - 1) != TD_DIRSEP[0]) {
36,910✔
266
#ifdef WINDOWS
267
    code = _mkdir(temp, mode);
268
#elif defined(DARWIN)
269
    code = mkdir(dirname, 0777);
270
#else
271
    code = mkdir(temp, mode);
36,818✔
272
#endif
273
    if (code < 0 && errno != EEXIST) {
36,815!
274
      terrno = TAOS_SYSTEM_ERROR(errno);
×
275
      return terrno;
×
276
    }
277
  }
278

279
  if (code < 0 && errno == EEXIST) {
36,907!
280
    if (checkAccess && taosCheckAccessFile(temp, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) {
17,130!
281
      return 0;
×
282
    }
283
  }
284

285
  code = chmod(temp, mode);
36,907✔
286
  if (-1 == code) {
36,808!
287
    terrno = TAOS_SYSTEM_ERROR(errno);
×
288
    return terrno;
×
289
  }
290
  return code;
36,812✔
291
}
292

293
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
×
294
  TdDirPtr pDir = taosOpenDir(dirname);
×
295
  if (pDir == NULL) return;
×
296

297
  int64_t       sec = taosGetTimestampSec();
×
298
  TdDirEntryPtr de = NULL;
×
299

300
  while ((de = taosReadDir(pDir)) != NULL) {
×
301
    if (strcmp(taosGetDirEntryName(de), ".") == 0 || strcmp(taosGetDirEntryName(de), "..") == 0) continue;
×
302

303
    char filename[1024];
304
    (void)snprintf(filename, sizeof(filename), "%s/%s", dirname, taosGetDirEntryName(de));
×
305
    if (taosDirEntryIsDir(de)) {
×
306
      continue;
×
307
    } else {
308
      int32_t len = (int32_t)strlen(filename);
×
309
      if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) {
×
310
        len -= 3;
×
311
      }else{
312
        continue;
×
313
      }
314

315
      int64_t fileSec = 0;
×
316
      for (int32_t i = len - 1; i >= 0; i--) {
×
317
        if (filename[i] == '.') {
×
318
          fileSec = atoll(filename + i + 1);
×
319
          break;
×
320
        }
321
      }
322

323
      if (fileSec <= 100) continue;
×
324
      int32_t days = (int32_t)(TABS(sec - fileSec) / 86400 + 1);
×
325
      if (days > keepDays) {
×
326
        TAOS_UNUSED(taosRemoveFile(filename));
×
327
         uInfo("file:%s is removed, days:%d keepDays:%d, sed:%"PRId64, filename, days, keepDays, fileSec);
×
328
      } else {
329
        // printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
330
      }
331
    }
332
  }
333

334
  TAOS_UNUSED(taosCloseDir(&pDir));
×
335
  TAOS_UNUSED(rmdir(dirname));
×
336
}
337

338
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
118,214✔
339
  OS_PARAM_CHECK(dirname);
118,214!
340
  OS_PARAM_CHECK(outname);
118,214!
341
  wordexp_t full_path;
342
  int32_t code = wordexp(dirname, &full_path, 0);
118,214✔
343
  switch (code) {
118,214!
344
    case 0:
118,214✔
345
      break;
118,214✔
346
    case WRDE_NOSPACE:
×
347
      wordfree(&full_path);
×
348
      // FALL THROUGH
349
    default:
×
350
      return code;
×
351
  }
352

353
  if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) {
118,214!
354
    tstrncpy(outname, full_path.we_wordv[0], maxlen);
118,214✔
355
  }
356

357
  wordfree(&full_path);
118,214✔
358

359
  return 0;
118,214✔
360
}
361

362
int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen) {
40,137✔
363
  OS_PARAM_CHECK(dirname);
40,137!
364
  OS_PARAM_CHECK(realPath);
40,137✔
365
  char tmp[PATH_MAX] = {0};
2,576✔
366
#ifdef WINDOWS
367
  if (_fullpath(tmp, dirname, maxlen) != NULL) {
368
#else
369
  if (realpath(dirname, tmp) != NULL) {
2,576!
370
#endif
371
    if (strlen(tmp) < maxlen) {
2,575!
372
      if (realPath == NULL) {
2,575!
373
        tstrncpy(dirname, tmp, maxlen);
×
374
      } else {
375
        tstrncpy(realPath, tmp, maxlen);
2,575✔
376
      }
377
      return 0;
2,575✔
378
    }
379
  }
380

381
  terrno = TAOS_SYSTEM_ERROR(errno);
×
382

383
  return terrno;
×
384
}
385

386
bool taosIsDir(const char *dirname) {
42,602✔
387
  TdDirPtr pDir = taosOpenDir(dirname);
42,602✔
388
  if (pDir != NULL) {
42,608✔
389
    TAOS_SKIP_ERROR(taosCloseDir(&pDir));
27,425✔
390
    return true;
27,425✔
391
  }
392
  return false;
15,183✔
393
}
394

395
char *taosDirName(char *name) {
2,404✔
396
  if(name == NULL) {
2,404!
397
    terrno = TSDB_CODE_INVALID_PARA;
×
398
    return NULL;
×
399
  }
400
#ifdef WINDOWS
401
  char Drive1[MAX_PATH], Dir1[MAX_PATH];
402
  _splitpath(name, Drive1, Dir1, NULL, NULL);
403
  size_t dirNameLen = strlen(Drive1) + strlen(Dir1);
404
  if (dirNameLen > 0) {
405
    if (name[dirNameLen - 1] == '/' || name[dirNameLen - 1] == '\\') {
406
      name[dirNameLen - 1] = 0;
407
    } else {
408
      name[dirNameLen] = 0;
409
    }
410
  } else {
411
    name[0] = 0;
412
  }
413
  return name;
414
#else
415
  char *end = strrchr(name, '/');
2,404✔
416
  if (end != NULL) {
2,404!
417
    *end = '\0';
2,404✔
418
  } else {
419
    name[0] = 0;
×
420
  }
421
  return name;
2,404✔
422
#endif
423
}
424

425
char *taosDirEntryBaseName(char *name) {
36,352,439✔
426
  if(name == NULL) {
36,352,439!
427
    terrno = TSDB_CODE_INVALID_PARA;
×
428
    return NULL;
×
429
  }
430
#ifdef WINDOWS
431
  char Filename1[MAX_PATH], Ext1[MAX_PATH];
432
  _splitpath(name, NULL, NULL, Filename1, Ext1);
433
  return name + (strlen(name) - strlen(Filename1) - strlen(Ext1));
434
#else
435
  if ((name[0] == '/' && name[1] == '\0')) return name;
36,352,439!
436
  char *pPoint = strrchr(name, '/');
36,352,439✔
437
  if (pPoint != NULL) {
36,352,439!
438
    if (*(pPoint + 1) == '\0') {
×
439
      *pPoint = '\0';
×
440
      return taosDirEntryBaseName(name);
×
441
    }
442
    return pPoint + 1;
×
443
  }
444
  return name;
36,352,439✔
445
#endif
446
}
447

448
TdDirPtr taosOpenDir(const char *dirname) {
538,978✔
449
  if (dirname == NULL) {
538,978!
450
    terrno = TSDB_CODE_INVALID_PARA;
×
451
    return NULL;
×
452
  }
453

454
#ifdef WINDOWS
455
  char   szFind[MAX_PATH];  //这是要找的
456
  HANDLE hFind;
457

458
  TdDirPtr pDir = taosMemoryMalloc(sizeof(TdDir));
459
  if(pDir == NULL) {
460
    return NULL;
461
  }
462

463
  snprintf(szFind, sizeof(szFind), "%s%s", dirname, "\\*.*");  //利用通配符找这个目录下的所以文件,包括目录
464

465
  pDir->hFind = FindFirstFile(szFind, &(pDir->dirEntry.findFileData));
466
  if (INVALID_HANDLE_VALUE == pDir->hFind) {
467
    taosMemoryFree(pDir);
468
    DWORD errorCode = GetLastError();
469
    terrno = TAOS_SYSTEM_WINAPI_ERROR(errorCode);
470
    return NULL;
471
  }
472
  return pDir;
473
#elif defined(DARWIN)
474
  DIR *pDir = opendir(dirname);
475
  if (pDir == NULL) return NULL;
476
  TdDirPtr dirPtr = (TdDirPtr)taosMemoryMalloc(sizeof(TdDir));
477
  if (dirPtr == NULL) {
478
    (void)closedir(pDir);
479
    terrno = TAOS_SYSTEM_ERROR(errno);
480
    return NULL;
481
  }
482
  dirPtr->dirEntryPtr = (TdDirEntryPtr) & (dirPtr->dirEntry1);
483
  dirPtr->pDir = pDir;
484
  return dirPtr;
485
#else
486
  TdDirPtr ptr = (TdDirPtr)opendir(dirname);
538,978✔
487
  if (NULL == ptr) {
539,052✔
488
    terrno = TAOS_SYSTEM_ERROR(errno);
16,686✔
489
  }
490
  return ptr;
539,118✔
491
#endif
492
}
493

494
TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
37,278,772✔
495
  if (pDir == NULL) {
37,278,772!
496
    terrno = TSDB_CODE_INVALID_PARA;  
×
497
    return NULL;
×
498
  }
499
#ifdef WINDOWS
500
  if (!FindNextFile(pDir->hFind, &(pDir->dirEntry.findFileData))) {
501
    return NULL;
502
  }
503
  return (TdDirEntryPtr) & (pDir->dirEntry.findFileData);
504
#elif defined(DARWIN)
505
  if (readdir_r(pDir->pDir, (dirent *)&(pDir->dirEntry), (dirent **)&(pDir->dirEntryPtr)) == 0) {
506
    return pDir->dirEntryPtr;
507
  } else {
508
    return NULL;
509
  }
510
#else
511
  errno = 0;
37,278,772✔
512
  terrno = 0;
37,278,772✔
513
  TdDirEntryPtr p = (TdDirEntryPtr)readdir((DIR *)pDir);
37,162,372✔
514
  if (NULL == p && errno) {
37,203,389!
515
    terrno = TAOS_SYSTEM_ERROR(errno);
×
516
  }
517
  return p;
37,203,389✔
518
#endif
519
}
520

521
bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) {
223,706✔
522
  if (pDirEntry == NULL) {
223,706!
523
    return false;
×
524
  }
525
#ifdef WINDOWS
526
  return (pDirEntry->findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
527
#else
528
  return (((dirent *)pDirEntry)->d_type & DT_DIR) != 0;
223,706✔
529
#endif
530
}
531

532
char *taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
37,141,506✔
533
  if (pDirEntry == NULL) {
37,141,506!
534
    return NULL;
×
535
  }
536
#ifdef WINDOWS
537
  return pDirEntry->findFileData.cFileName;
538
#else
539
  return ((dirent *)pDirEntry)->d_name;
37,141,506✔
540
#endif
541
}
542

543
int32_t taosCloseDir(TdDirPtr *ppDir) {
522,294✔
544
  int32_t code =  0;
522,294✔
545
  if (ppDir == NULL || *ppDir == NULL) {
522,294!
546
    terrno = TSDB_CODE_INVALID_PARA;
×
547
    return terrno;
×
548
  }
549
#ifdef WINDOWS
550
  if(!FindClose((*ppDir)->hFind)) {
551
    terrno = TAOS_SYSTEM_WINAPI_ERROR(GetLastError());
552
    return terrno;
553
  }
554
  taosMemoryFree(*ppDir);
555
  *ppDir = NULL;
556
  return 0;
557
#elif defined(DARWIN)
558
  code = closedir((*ppDir)->pDir);
559
  if (-1 == code) {
560
    terrno = TAOS_SYSTEM_ERROR(errno);
561
    return terrno;
562
  }
563
  taosMemoryFree(*ppDir);
564
  *ppDir = NULL;
565
  return 0;
566
#else
567
  code = closedir((DIR *)*ppDir);
522,382✔
568
  *ppDir = NULL;
522,504✔
569
  if (-1 == code) {
522,504!
570
    terrno = TAOS_SYSTEM_ERROR(errno);
×
571
    return terrno;
×
572
  }
573
  return 0;
522,504✔
574
#endif
575
}
576

577
void taosGetCwd(char *buf, int32_t len) {
2,390✔
578
#if !defined(WINDOWS)
579
  char *unused __attribute__((unused));
580
  unused = getcwd(buf, len - 1);
2,390✔
581
#else
582
  tstrncpy(buf, "not implemented on windows", len);
583
#endif
584
}
2,390✔
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