osDir.c 11.6 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;

H
Hongze Cheng 已提交
34 35 36 37 38 39 40
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 已提交
41

wafwerar's avatar
wafwerar 已提交
42 43 44 45 46 47 48 49
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 已提交
50
    printf("failed to parse relative path:%s to abs path\n", words);
wafwerar's avatar
wafwerar 已提交
51 52 53
    return -1;
  }

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

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

wafwerar's avatar
wafwerar 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#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 已提交
78
#else
S
Shengliang Guan 已提交
79

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

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

#endif
wafwerar's avatar
wafwerar 已提交
91

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

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

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

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

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

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

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

  return code;
}

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

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

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

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

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

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

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

wafwerar's avatar
wafwerar 已提交
194 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
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 已提交
222 223
#elif defined(DARWIN)
      code = mkdir(dirname, 0777);
wafwerar's avatar
wafwerar 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237
#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 已提交
238
#elif defined(DARWIN)
H
Hongze Cheng 已提交
239
    code = mkdir(dirname, 0777);
wafwerar's avatar
wafwerar 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
#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 已提交
258
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
wafwerar's avatar
wafwerar 已提交
259 260
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir == NULL) return;
S
TD-1263  
Shengliang Guan 已提交
261

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

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

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

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

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

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

S
config  
Shengliang Guan 已提交
301
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
S
Shengliang Guan 已提交
302
  wordexp_t full_path;
H
Hongze Cheng 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  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));
      return -1;
S
TD-1574  
Shengliang Guan 已提交
321 322
  }

S
Shengliang Guan 已提交
323 324
  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 已提交
325 326
  }

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

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

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

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

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

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

dengyihao's avatar
dengyihao 已提交
385
char *taosDirEntryBaseName(char *name) {
wafwerar's avatar
wafwerar 已提交
386 387 388 389 390
#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 已提交
391 392 393 394 395 396 397 398 399
  if (name == NULL || (name[0] == '/' && name[1] == '\0')) return name;
  char *pPoint = strrchr(name, '/');
  if (pPoint != NULL) {
    if (*(pPoint + 1) == '\0') {
        *pPoint = '\0';
        return taosDirEntryBaseName(name);
    }
    return pPoint + 1;
  }
wafwerar's avatar
wafwerar 已提交
400
  return name;
wafwerar's avatar
wafwerar 已提交
401
#endif
wafwerar's avatar
wafwerar 已提交
402 403 404 405 406 407
}

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

#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
410 411
  char   szFind[MAX_PATH];  //这是要找的
  HANDLE hFind;
wafwerar's avatar
wafwerar 已提交
412 413 414 415 416 417 418 419 420 421 422 423

  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 已提交
424 425 426 427
#elif defined(DARWIN)
  DIR *pDir = opendir(dirname);
  if (pDir == NULL) return NULL;
  TdDirPtr dirPtr = (TdDirPtr)taosMemoryMalloc(sizeof(TdDir));
H
Hongze Cheng 已提交
428
  dirPtr->dirEntryPtr = (TdDirEntryPtr) & (dirPtr->dirEntry1);
wafwerar's avatar
wafwerar 已提交
429 430
  dirPtr->pDir = pDir;
  return dirPtr;
wafwerar's avatar
wafwerar 已提交
431
#else
wafwerar's avatar
wafwerar 已提交
432
  return (TdDirPtr)opendir(dirname);
wafwerar's avatar
wafwerar 已提交
433
#endif
wafwerar's avatar
wafwerar 已提交
434 435 436 437 438 439
}

TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
  if (pDir == NULL) {
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
440 441 442 443
#ifdef WINDOWS
  if (!FindNextFile(pDir->hFind, &(pDir->dirEntry.findFileData))) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
444
  return (TdDirEntryPtr) & (pDir->dirEntry.findFileData);
wafwerar's avatar
wafwerar 已提交
445
#elif defined(DARWIN)
H
Hongze Cheng 已提交
446
  if (readdir_r(pDir->pDir, (dirent *)&(pDir->dirEntry), (dirent **)&(pDir->dirEntryPtr)) == 0) {
wafwerar's avatar
wafwerar 已提交
447 448 449 450
    return pDir->dirEntryPtr;
  } else {
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
451
#else
dengyihao's avatar
dengyihao 已提交
452
  return (TdDirEntryPtr)readdir((DIR *)pDir);
wafwerar's avatar
wafwerar 已提交
453
#endif
wafwerar's avatar
wafwerar 已提交
454 455 456 457 458 459
}

bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) {
  if (pDirEntry == NULL) {
    return false;
  }
wafwerar's avatar
wafwerar 已提交
460 461 462
#ifdef WINDOWS
  return (pDirEntry->findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
dengyihao's avatar
dengyihao 已提交
463
  return (((dirent *)pDirEntry)->d_type & DT_DIR) != 0;
wafwerar's avatar
wafwerar 已提交
464
#endif
wafwerar's avatar
wafwerar 已提交
465 466
}

dengyihao's avatar
dengyihao 已提交
467
char *taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
L
Liu Jicong 已提交
468 469 470
  /*if (pDirEntry == NULL) {*/
  /*return NULL;*/
  /*}*/
wafwerar's avatar
wafwerar 已提交
471 472 473
#ifdef WINDOWS
  return pDirEntry->findFileData.cFileName;
#else
dengyihao's avatar
dengyihao 已提交
474
  return ((dirent *)pDirEntry)->d_name;
wafwerar's avatar
wafwerar 已提交
475
#endif
wafwerar's avatar
wafwerar 已提交
476 477
}

wafwerar's avatar
wafwerar 已提交
478 479
int32_t taosCloseDir(TdDirPtr *ppDir) {
  if (ppDir == NULL || *ppDir == NULL) {
wafwerar's avatar
wafwerar 已提交
480 481
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
482 483 484 485 486
#ifdef WINDOWS
  FindClose((*ppDir)->hFind);
  taosMemoryFree(*ppDir);
  *ppDir = NULL;
  return 0;
wafwerar's avatar
wafwerar 已提交
487 488 489 490 491
#elif defined(DARWIN)
  closedir((*ppDir)->pDir);
  taosMemoryFree(*ppDir);
  *ppDir = NULL;
  return 0;
wafwerar's avatar
wafwerar 已提交
492
#else
dengyihao's avatar
dengyihao 已提交
493
  closedir((DIR *)*ppDir);
wafwerar's avatar
wafwerar 已提交
494 495
  *ppDir = NULL;
  return 0;
L
Liu Jicong 已提交
496
#endif
wafwerar's avatar
wafwerar 已提交
497
}