osDir.c 11.3 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

L
Liu Jicong 已提交
16
#define _DEFAULT_SOURCE
wafwerar's avatar
wafwerar 已提交
17
#define ALLOW_FORBID_FUNC
L
Liu Jicong 已提交
18

S
Shengliang Guan 已提交
19
#include "os.h"
S
TD-1574  
Shengliang Guan 已提交
20

wafwerar's avatar
wafwerar 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
#ifdef WINDOWS

#include <windows.h>

typedef struct TdDirEntry {
  WIN32_FIND_DATA findFileData;
} TdDirEntry;

typedef struct TdDir {
  TdDirEntry dirEntry;
  HANDLE     hFind;
} TdDir;

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

wafwerar's avatar
wafwerar 已提交
43 44 45 46 47 48 49 50
int wordexp(char *words, wordexp_t *pwordexp, int flags) {
  pwordexp->we_offs = 0;
  pwordexp->we_wordc = 1;
  pwordexp->we_wordv[0] = pwordexp->wordPos;

  memset(pwordexp->wordPos, 0, 1025);
  if (_fullpath(pwordexp->wordPos, words, 1024) == NULL) {
    pwordexp->we_wordv[0] = words;
wafwerar's avatar
wafwerar 已提交
51
    printf("failed to parse relative path:%s to abs path\n", words);
wafwerar's avatar
wafwerar 已提交
52 53 54
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
55
  // printf("parse relative path:%s to abs path:%s\n", words, pwordexp->wordPos);
wafwerar's avatar
wafwerar 已提交
56 57
  return 0;
}
S
Shengliang Guan 已提交
58

wafwerar's avatar
wafwerar 已提交
59
void wordfree(wordexp_t *pwordexp) {}
S
Shengliang Guan 已提交
60

wafwerar's avatar
wafwerar 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#elif defined(DARWIN)

#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>

typedef struct dirent dirent;
typedef struct dirent TdDirEntry;

typedef struct TdDir {
  TdDirEntry    dirEntry;
  TdDirEntry    dirEntry1;
  TdDirEntryPtr dirEntryPtr;
  DIR          *pDir;
} TdDir;

S
Shengliang Guan 已提交
79
#else
S
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81 82 83 84 85 86
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>

wafwerar's avatar
wafwerar 已提交
87
typedef struct dirent dirent;
dengyihao's avatar
dengyihao 已提交
88
typedef struct DIR    TdDir;
wafwerar's avatar
wafwerar 已提交
89 90 91
typedef struct dirent TdDirEntry;

#endif
wafwerar's avatar
wafwerar 已提交
92

H
Hongze Cheng 已提交
93
void taosRemoveDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
94 95
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir == NULL) return;
S
Shengliang Guan 已提交
96

wafwerar's avatar
wafwerar 已提交
97 98 99
  TdDirEntryPtr de = NULL;
  while ((de = taosReadDir(pDir)) != NULL) {
    if (strcmp(taosGetDirEntryName(de), ".") == 0 || strcmp(taosGetDirEntryName(de), "..") == 0) continue;
S
TD-4088  
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101 102
    char filename[1024] = {0};
    snprintf(filename, sizeof(filename), "%s%s%s", dirname, TD_DIRSEP, taosGetDirEntryName(de));
wafwerar's avatar
wafwerar 已提交
103
    if (taosDirEntryIsDir(de)) {
S
Shengliang Guan 已提交
104 105
      taosRemoveDir(filename);
    } else {
106
      (void)taosRemoveFile(filename);
dengyihao's avatar
dengyihao 已提交
107
      // printf("file:%s is removed\n", filename);
S
Shengliang Guan 已提交
108 109 110
    }
  }

wafwerar's avatar
wafwerar 已提交
111
  taosCloseDir(&pDir);
S
Shengliang Guan 已提交
112
  rmdir(dirname);
S
Shengliang Guan 已提交
113

dengyihao's avatar
dengyihao 已提交
114
  // printf("dir:%s is removed\n", dirname);
wafwerar's avatar
wafwerar 已提交
115
  return;
S
Shengliang Guan 已提交
116 117
}

wafwerar's avatar
wafwerar 已提交
118
bool taosDirExist(const char *dirname) { return taosCheckExistFile(dirname); }
119

120
int32_t taosMkDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
121 122 123
  if (taosDirExist(dirname)) return 0;
#ifdef WINDOWS
  int32_t code = _mkdir(dirname, 0755);
wafwerar's avatar
wafwerar 已提交
124 125
#elif defined(DARWIN)
  int32_t code = mkdir(dirname, 0777);
wafwerar's avatar
wafwerar 已提交
126
#else
wafwerar's avatar
wafwerar 已提交
127
  int32_t code = mkdir(dirname, 0755);
wafwerar's avatar
wafwerar 已提交
128
#endif
wafwerar's avatar
wafwerar 已提交
129 130 131 132 133 134 135 136
  if (code < 0 && errno == EEXIST) {
    return 0;
  }

  return code;
}

int32_t taosMulMkDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
137
  if (dirname == NULL) return -1;
L
Liu Jicong 已提交
138 139
  char    temp[1024];
  char   *pos = temp;
wafwerar's avatar
wafwerar 已提交
140
  int32_t code = 0;
wafwerar's avatar
wafwerar 已提交
141 142
#ifdef WINDOWS
  taosRealPath(dirname, temp, sizeof(temp));
wafwerar's avatar
wafwerar 已提交
143
  if (temp[1] == ':') pos += 3;
wafwerar's avatar
wafwerar 已提交
144 145 146
#else
  strcpy(temp, dirname);
#endif
wafwerar's avatar
wafwerar 已提交
147

wafwerar's avatar
wafwerar 已提交
148 149 150
  if (taosDirExist(temp)) return code;

  if (strncmp(temp, TD_DIRSEP, 1) == 0) {
wafwerar's avatar
wafwerar 已提交
151
    pos += 1;
wafwerar's avatar
wafwerar 已提交
152
  } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) {
wafwerar's avatar
wafwerar 已提交
153 154
    pos += 2;
  }
dengyihao's avatar
dengyihao 已提交
155 156

  for (; *pos != '\0'; pos++) {
wafwerar's avatar
wafwerar 已提交
157
    if (*pos == TD_DIRSEP[0]) {
wafwerar's avatar
wafwerar 已提交
158
      *pos = '\0';
L
Liu Jicong 已提交
159
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
160
      code = _mkdir(temp, 0755);
wafwerar's avatar
wafwerar 已提交
161 162
#elif defined(DARWIN)
      code = mkdir(dirname, 0777);
L
Liu Jicong 已提交
163
#else
wafwerar's avatar
wafwerar 已提交
164
      code = mkdir(temp, 0755);
L
Liu Jicong 已提交
165
#endif
wafwerar's avatar
wafwerar 已提交
166
      if (code < 0 && errno != EEXIST) {
wafwerar's avatar
wafwerar 已提交
167
        terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
168 169
        return code;
      }
wafwerar's avatar
wafwerar 已提交
170
      *pos = TD_DIRSEP[0];
wafwerar's avatar
wafwerar 已提交
171 172
    }
  }
dengyihao's avatar
dengyihao 已提交
173

wafwerar's avatar
wafwerar 已提交
174
  if (*(pos - 1) != TD_DIRSEP[0]) {
L
Liu Jicong 已提交
175
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
176
    code = _mkdir(temp, 0755);
wafwerar's avatar
wafwerar 已提交
177 178
#elif defined(DARWIN)
      code = mkdir(dirname, 0777);
L
Liu Jicong 已提交
179
#else
wafwerar's avatar
wafwerar 已提交
180
    code = mkdir(temp, 0755);
L
Liu Jicong 已提交
181
#endif
wafwerar's avatar
wafwerar 已提交
182
    if (code < 0 && errno != EEXIST) {
wafwerar's avatar
wafwerar 已提交
183
      terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
184 185 186 187
      return code;
    }
  }

S
Shengliang Guan 已提交
188
  if (code < 0 && errno == EEXIST) {
189
    return 0;
S
Shengliang Guan 已提交
190 191
  }

192
  return code;
S
Shengliang Guan 已提交
193 194
}

wafwerar's avatar
wafwerar 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
int32_t taosMulModeMkDir(const char *dirname, int mode) {
  if (dirname == NULL) return -1;
  char    temp[1024];
  char   *pos = temp;
  int32_t code = 0;
#ifdef WINDOWS
  taosRealPath(dirname, temp, sizeof(temp));
  if (temp[1] == ':') pos += 3;
#else
  strcpy(temp, dirname);
#endif

  if (taosDirExist(temp)) {
    chmod(temp, mode);
    return code;
  }

  if (strncmp(temp, TD_DIRSEP, 1) == 0) {
    pos += 1;
  } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) {
    pos += 2;
  }

  for (; *pos != '\0'; pos++) {
    if (*pos == TD_DIRSEP[0]) {
      *pos = '\0';
#ifdef WINDOWS
      code = _mkdir(temp, mode);
wafwerar's avatar
wafwerar 已提交
223 224
#elif defined(DARWIN)
      code = mkdir(dirname, 0777);
wafwerar's avatar
wafwerar 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238
#else
      code = mkdir(temp, mode);
#endif
      if (code < 0 && errno != EEXIST) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return code;
      }
      *pos = TD_DIRSEP[0];
    }
  }

  if (*(pos - 1) != TD_DIRSEP[0]) {
#ifdef WINDOWS
    code = _mkdir(temp, mode);
wafwerar's avatar
wafwerar 已提交
239 240
#elif defined(DARWIN)
      code = mkdir(dirname, 0777);
wafwerar's avatar
wafwerar 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
#else
    code = mkdir(temp, mode);
#endif
    if (code < 0 && errno != EEXIST) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return code;
    }
  }

  if (code < 0 && errno == EEXIST) {
    chmod(temp, mode);
    return 0;
  }

  chmod(temp, mode);
  return code;
}

S
config  
Shengliang Guan 已提交
259
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
wafwerar's avatar
wafwerar 已提交
260 261
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir == NULL) return;
S
TD-1263  
Shengliang Guan 已提交
262

dengyihao's avatar
dengyihao 已提交
263
  int64_t       sec = taosGetTimestampSec();
wafwerar's avatar
wafwerar 已提交
264
  TdDirEntryPtr de = NULL;
S
TD-1263  
Shengliang Guan 已提交
265

wafwerar's avatar
wafwerar 已提交
266 267
  while ((de = taosReadDir(pDir)) != NULL) {
    if (strcmp(taosGetDirEntryName(de), ".") == 0 || strcmp(taosGetDirEntryName(de), "..") == 0) continue;
S
TD-1263  
Shengliang Guan 已提交
268 269

    char filename[1024];
wafwerar's avatar
wafwerar 已提交
270 271
    snprintf(filename, sizeof(filename), "%s/%s", dirname, taosGetDirEntryName(de));
    if (taosDirEntryIsDir(de)) {
S
TD-1263  
Shengliang Guan 已提交
272 273
      continue;
    } else {
S
Shengliang Guan 已提交
274
      int32_t len = (int32_t)strlen(filename);
S
TD-1574  
Shengliang Guan 已提交
275 276 277 278
      if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) {
        len -= 3;
      }

S
TD-1263  
Shengliang Guan 已提交
279
      int64_t fileSec = 0;
S
Shengliang Guan 已提交
280
      for (int32_t i = len - 1; i >= 0; i--) {
S
TD-1263  
Shengliang Guan 已提交
281
        if (filename[i] == '.') {
S
TD-1263  
Shengliang Guan 已提交
282
          fileSec = atoll(filename + i + 1);
S
TD-1263  
Shengliang Guan 已提交
283 284 285 286
          break;
        }
      }

S
TD-1263  
Shengliang Guan 已提交
287
      if (fileSec <= 100) continue;
dengyihao's avatar
dengyihao 已提交
288
      int32_t days = (int32_t)(TABS(sec - fileSec) / 86400 + 1);
S
TD-1263  
Shengliang Guan 已提交
289
      if (days > keepDays) {
290
        (void)taosRemoveFile(filename);
dengyihao's avatar
dengyihao 已提交
291
        // printf("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
292
      } else {
dengyihao's avatar
dengyihao 已提交
293
        // printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
294 295 296 297
      }
    }
  }

wafwerar's avatar
wafwerar 已提交
298
  taosCloseDir(&pDir);
S
Shengliang Guan 已提交
299
  rmdir(dirname);
S
TD-1263  
Shengliang Guan 已提交
300
}
S
TD-1574  
Shengliang Guan 已提交
301

S
config  
Shengliang Guan 已提交
302
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
S
Shengliang Guan 已提交
303
  wordexp_t full_path;
wafwerar's avatar
wafwerar 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  switch (wordexp (dirname, &full_path, 0)) {
  case 0:
    break;
  case WRDE_NOSPACE:
    wordfree (&full_path);
    // printf("failed to expand path:%s since Out of memory\n", dirname);
    return -1;
  case WRDE_BADCHAR:
    // printf("failed to expand path:%s since illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }\n", dirname);
    return -1;
  case WRDE_SYNTAX:
    // printf("failed to expand path:%s since Shell syntax error, such as unbalanced parentheses or unmatched quotes\n", dirname);
    return -1;
  default:
    // printf("failed to expand path:%s since %s\n", dirname, strerror(errno));
319
    return -1;
S
TD-1574  
Shengliang Guan 已提交
320 321
  }

S
Shengliang Guan 已提交
322 323
  if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) {
    strncpy(outname, full_path.we_wordv[0], maxlen);
S
TD-1574  
Shengliang Guan 已提交
324 325
  }

S
Shengliang Guan 已提交
326
  wordfree(&full_path);
S
TD-1574  
Shengliang Guan 已提交
327

328
  return 0;
S
Shengliang Guan 已提交
329
}
S
TD-1574  
Shengliang Guan 已提交
330

wafwerar's avatar
wafwerar 已提交
331
int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen) {
S
Shengliang Guan 已提交
332
  char tmp[PATH_MAX] = {0};
wafwerar's avatar
wafwerar 已提交
333
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
334
  if (_fullpath(tmp, dirname, maxlen) != NULL) {
wafwerar's avatar
wafwerar 已提交
335
#else
S
Shengliang Guan 已提交
336
  if (realpath(dirname, tmp) != NULL) {
wafwerar's avatar
wafwerar 已提交
337 338 339 340 341 342 343
#endif
    if (realPath == NULL) {
      strncpy(dirname, tmp, maxlen);
    } else {
      strncpy(realPath, tmp, maxlen);
    }
    return 0;
S
TD-1574  
Shengliang Guan 已提交
344 345
  }

wafwerar's avatar
wafwerar 已提交
346
  return -1;
S
TD-1574  
Shengliang Guan 已提交
347
}
S
Shengliang Guan 已提交
348

349
bool taosIsDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
350 351 352
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir != NULL) {
    taosCloseDir(&pDir);
353 354 355 356 357
    return true;
  }
  return false;
}

dengyihao's avatar
dengyihao 已提交
358
char *taosDirName(char *name) {
wafwerar's avatar
wafwerar 已提交
359
#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
360
  char Drive1[MAX_PATH], Dir1[MAX_PATH];
wafwerar's avatar
wafwerar 已提交
361 362 363
  _splitpath(name, Drive1, Dir1, NULL, NULL);
  size_t dirNameLen = strlen(Drive1) + strlen(Dir1);
  if (dirNameLen > 0) {
wafwerar's avatar
wafwerar 已提交
364 365 366 367 368 369 370
    if (name[dirNameLen - 1] == '/' || name[dirNameLen - 1] == '\\') {
      name[dirNameLen - 1] = 0;
    } else {
      name[dirNameLen] = 0;
    }
  } else {
    name[0] = 0;
wafwerar's avatar
wafwerar 已提交
371 372
  }
  return name;
wafwerar's avatar
wafwerar 已提交
373
#else
wafwerar's avatar
wafwerar 已提交
374 375 376 377 378 379
  char *end = strrchr(name, '/');
  if (end != NULL) {
    *end = '\0';
  } else {
    name[0] = 0;
  }
L
Liu Jicong 已提交
380
  return name;
wafwerar's avatar
wafwerar 已提交
381
#endif
wafwerar's avatar
wafwerar 已提交
382 383
}

dengyihao's avatar
dengyihao 已提交
384
char *taosDirEntryBaseName(char *name) {
wafwerar's avatar
wafwerar 已提交
385 386 387 388 389
#ifdef WINDOWS
  char Filename1[MAX_PATH], Ext1[MAX_PATH];
  _splitpath(name, NULL, NULL, Filename1, Ext1);
  return name + (strlen(name) - strlen(Filename1) - strlen(Ext1));
#else
wafwerar's avatar
wafwerar 已提交
390 391 392
  char *pPoint = strchr(name, '.');
  if (pPoint != NULL) pPoint = 0;
  return name;
wafwerar's avatar
wafwerar 已提交
393
#endif
wafwerar's avatar
wafwerar 已提交
394 395 396 397 398 399
}

TdDirPtr taosOpenDir(const char *dirname) {
  if (dirname == NULL) {
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
400 401

#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
402 403
  char   szFind[MAX_PATH];  //这是要找的
  HANDLE hFind;
wafwerar's avatar
wafwerar 已提交
404 405 406 407 408 409 410 411 412 413 414 415

  TdDirPtr pDir = taosMemoryMalloc(sizeof(TdDir));

  strcpy(szFind, dirname);
  strcat(szFind, "\\*.*");  //利用通配符找这个目录下的所以文件,包括目录

  pDir->hFind = FindFirstFile(szFind, &(pDir->dirEntry.findFileData));
  if (INVALID_HANDLE_VALUE == pDir->hFind) {
    taosMemoryFree(pDir);
    return NULL;
  }
  return pDir;
wafwerar's avatar
wafwerar 已提交
416 417 418 419 420 421 422
#elif defined(DARWIN)
  DIR *pDir = opendir(dirname);
  if (pDir == NULL) return NULL;
  TdDirPtr dirPtr = (TdDirPtr)taosMemoryMalloc(sizeof(TdDir));
  dirPtr->dirEntryPtr = (TdDirEntryPtr)&(dirPtr->dirEntry1);
  dirPtr->pDir = pDir;
  return dirPtr;
wafwerar's avatar
wafwerar 已提交
423
#else
wafwerar's avatar
wafwerar 已提交
424
  return (TdDirPtr)opendir(dirname);
wafwerar's avatar
wafwerar 已提交
425
#endif
wafwerar's avatar
wafwerar 已提交
426 427 428 429 430 431
}

TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
  if (pDir == NULL) {
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
432 433 434 435
#ifdef WINDOWS
  if (!FindNextFile(pDir->hFind, &(pDir->dirEntry.findFileData))) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
436
  return (TdDirEntryPtr) & (pDir->dirEntry.findFileData);
wafwerar's avatar
wafwerar 已提交
437 438 439 440 441 442
#elif defined(DARWIN)
  if (readdir_r(pDir->pDir, (dirent*)&(pDir->dirEntry), (dirent**)&(pDir->dirEntryPtr)) == 0) {
    return pDir->dirEntryPtr;
  } else {
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
443
#else
dengyihao's avatar
dengyihao 已提交
444
  return (TdDirEntryPtr)readdir((DIR *)pDir);
wafwerar's avatar
wafwerar 已提交
445
#endif
wafwerar's avatar
wafwerar 已提交
446 447 448 449 450 451
}

bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) {
  if (pDirEntry == NULL) {
    return false;
  }
wafwerar's avatar
wafwerar 已提交
452 453 454
#ifdef WINDOWS
  return (pDirEntry->findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
dengyihao's avatar
dengyihao 已提交
455
  return (((dirent *)pDirEntry)->d_type & DT_DIR) != 0;
wafwerar's avatar
wafwerar 已提交
456
#endif
wafwerar's avatar
wafwerar 已提交
457 458
}

dengyihao's avatar
dengyihao 已提交
459
char *taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
L
Liu Jicong 已提交
460 461 462
  /*if (pDirEntry == NULL) {*/
  /*return NULL;*/
  /*}*/
wafwerar's avatar
wafwerar 已提交
463 464 465
#ifdef WINDOWS
  return pDirEntry->findFileData.cFileName;
#else
dengyihao's avatar
dengyihao 已提交
466
  return ((dirent *)pDirEntry)->d_name;
wafwerar's avatar
wafwerar 已提交
467
#endif
wafwerar's avatar
wafwerar 已提交
468 469
}

wafwerar's avatar
wafwerar 已提交
470 471
int32_t taosCloseDir(TdDirPtr *ppDir) {
  if (ppDir == NULL || *ppDir == NULL) {
wafwerar's avatar
wafwerar 已提交
472 473
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
474 475 476 477 478
#ifdef WINDOWS
  FindClose((*ppDir)->hFind);
  taosMemoryFree(*ppDir);
  *ppDir = NULL;
  return 0;
wafwerar's avatar
wafwerar 已提交
479 480 481 482 483
#elif defined(DARWIN)
  closedir((*ppDir)->pDir);
  taosMemoryFree(*ppDir);
  *ppDir = NULL;
  return 0;
wafwerar's avatar
wafwerar 已提交
484
#else
dengyihao's avatar
dengyihao 已提交
485
  closedir((DIR *)*ppDir);
wafwerar's avatar
wafwerar 已提交
486 487
  *ppDir = NULL;
  return 0;
L
Liu Jicong 已提交
488
#endif
wafwerar's avatar
wafwerar 已提交
489
}