osDir.c 10.2 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 61

#else
S
Shengliang Guan 已提交
62

S
Shengliang Guan 已提交
63 64 65 66 67 68
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>

wafwerar's avatar
wafwerar 已提交
69
typedef struct dirent dirent;
dengyihao's avatar
dengyihao 已提交
70
typedef struct DIR    TdDir;
wafwerar's avatar
wafwerar 已提交
71 72 73
typedef struct dirent TdDirEntry;

#endif
wafwerar's avatar
wafwerar 已提交
74

H
Hongze Cheng 已提交
75
void taosRemoveDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
76 77
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir == NULL) return;
S
Shengliang Guan 已提交
78

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

S
Shengliang Guan 已提交
83 84
    char filename[1024] = {0};
    snprintf(filename, sizeof(filename), "%s%s%s", dirname, TD_DIRSEP, taosGetDirEntryName(de));
wafwerar's avatar
wafwerar 已提交
85
    if (taosDirEntryIsDir(de)) {
S
Shengliang Guan 已提交
86 87
      taosRemoveDir(filename);
    } else {
88
      (void)taosRemoveFile(filename);
dengyihao's avatar
dengyihao 已提交
89
      // printf("file:%s is removed\n", filename);
S
Shengliang Guan 已提交
90 91 92
    }
  }

wafwerar's avatar
wafwerar 已提交
93
  taosCloseDir(&pDir);
S
Shengliang Guan 已提交
94
  rmdir(dirname);
S
Shengliang Guan 已提交
95

dengyihao's avatar
dengyihao 已提交
96
  // printf("dir:%s is removed\n", dirname);
wafwerar's avatar
wafwerar 已提交
97
  return;
S
Shengliang Guan 已提交
98 99
}

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

102
int32_t taosMkDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
103 104 105 106
  if (taosDirExist(dirname)) return 0;
#ifdef WINDOWS
  int32_t code = _mkdir(dirname, 0755);
#else
wafwerar's avatar
wafwerar 已提交
107
  int32_t code = mkdir(dirname, 0755);
wafwerar's avatar
wafwerar 已提交
108
#endif
wafwerar's avatar
wafwerar 已提交
109 110 111 112 113 114 115 116
  if (code < 0 && errno == EEXIST) {
    return 0;
  }

  return code;
}

int32_t taosMulMkDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
117
  if (dirname == NULL) return -1;
L
Liu Jicong 已提交
118 119
  char    temp[1024];
  char   *pos = temp;
wafwerar's avatar
wafwerar 已提交
120
  int32_t code = 0;
wafwerar's avatar
wafwerar 已提交
121 122
#ifdef WINDOWS
  taosRealPath(dirname, temp, sizeof(temp));
wafwerar's avatar
wafwerar 已提交
123
  if (temp[1] == ':') pos += 3;
wafwerar's avatar
wafwerar 已提交
124 125 126
#else
  strcpy(temp, dirname);
#endif
wafwerar's avatar
wafwerar 已提交
127

wafwerar's avatar
wafwerar 已提交
128 129 130
  if (taosDirExist(temp)) return code;

  if (strncmp(temp, TD_DIRSEP, 1) == 0) {
wafwerar's avatar
wafwerar 已提交
131
    pos += 1;
wafwerar's avatar
wafwerar 已提交
132
  } else if (strncmp(temp, "." TD_DIRSEP, 2) == 0) {
wafwerar's avatar
wafwerar 已提交
133 134
    pos += 2;
  }
dengyihao's avatar
dengyihao 已提交
135 136

  for (; *pos != '\0'; pos++) {
wafwerar's avatar
wafwerar 已提交
137
    if (*pos == TD_DIRSEP[0]) {
wafwerar's avatar
wafwerar 已提交
138
      *pos = '\0';
L
Liu Jicong 已提交
139
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
140
      code = _mkdir(temp, 0755);
L
Liu Jicong 已提交
141
#else
wafwerar's avatar
wafwerar 已提交
142
      code = mkdir(temp, 0755);
L
Liu Jicong 已提交
143
#endif
wafwerar's avatar
wafwerar 已提交
144
      if (code < 0 && errno != EEXIST) {
wafwerar's avatar
wafwerar 已提交
145
        terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
146 147
        return code;
      }
wafwerar's avatar
wafwerar 已提交
148
      *pos = TD_DIRSEP[0];
wafwerar's avatar
wafwerar 已提交
149 150
    }
  }
dengyihao's avatar
dengyihao 已提交
151

wafwerar's avatar
wafwerar 已提交
152
  if (*(pos - 1) != TD_DIRSEP[0]) {
L
Liu Jicong 已提交
153
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
154
    code = _mkdir(temp, 0755);
L
Liu Jicong 已提交
155
#else
wafwerar's avatar
wafwerar 已提交
156
    code = mkdir(temp, 0755);
L
Liu Jicong 已提交
157
#endif
wafwerar's avatar
wafwerar 已提交
158
    if (code < 0 && errno != EEXIST) {
wafwerar's avatar
wafwerar 已提交
159
      terrno = TAOS_SYSTEM_ERROR(errno);
wafwerar's avatar
wafwerar 已提交
160 161 162 163 164
      return code;
    }
  }

  // int32_t code = mkdir(dirname, 0755);
S
Shengliang Guan 已提交
165
  if (code < 0 && errno == EEXIST) {
166
    return 0;
S
Shengliang Guan 已提交
167 168
  }

169
  return code;
S
Shengliang Guan 已提交
170 171
}

wafwerar's avatar
wafwerar 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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 222 223 224 225 226 227 228 229 230 231
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);
#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);
#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 已提交
232
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
wafwerar's avatar
wafwerar 已提交
233 234
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir == NULL) return;
S
TD-1263  
Shengliang Guan 已提交
235

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

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

    char filename[1024];
wafwerar's avatar
wafwerar 已提交
243 244
    snprintf(filename, sizeof(filename), "%s/%s", dirname, taosGetDirEntryName(de));
    if (taosDirEntryIsDir(de)) {
S
TD-1263  
Shengliang Guan 已提交
245 246
      continue;
    } else {
S
Shengliang Guan 已提交
247
      int32_t len = (int32_t)strlen(filename);
S
TD-1574  
Shengliang Guan 已提交
248 249 250 251
      if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) {
        len -= 3;
      }

S
TD-1263  
Shengliang Guan 已提交
252
      int64_t fileSec = 0;
S
Shengliang Guan 已提交
253
      for (int32_t i = len - 1; i >= 0; i--) {
S
TD-1263  
Shengliang Guan 已提交
254
        if (filename[i] == '.') {
S
TD-1263  
Shengliang Guan 已提交
255
          fileSec = atoll(filename + i + 1);
S
TD-1263  
Shengliang Guan 已提交
256 257 258 259
          break;
        }
      }

S
TD-1263  
Shengliang Guan 已提交
260
      if (fileSec <= 100) continue;
dengyihao's avatar
dengyihao 已提交
261
      int32_t days = (int32_t)(TABS(sec - fileSec) / 86400 + 1);
S
TD-1263  
Shengliang Guan 已提交
262
      if (days > keepDays) {
263
        (void)taosRemoveFile(filename);
dengyihao's avatar
dengyihao 已提交
264
        // printf("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
265
      } else {
dengyihao's avatar
dengyihao 已提交
266
        // printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
267 268 269 270
      }
    }
  }

wafwerar's avatar
wafwerar 已提交
271
  taosCloseDir(&pDir);
S
Shengliang Guan 已提交
272
  rmdir(dirname);
S
TD-1263  
Shengliang Guan 已提交
273
}
S
TD-1574  
Shengliang Guan 已提交
274

S
config  
Shengliang Guan 已提交
275
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
S
Shengliang Guan 已提交
276
  wordexp_t full_path;
wafwerar's avatar
wafwerar 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  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));
292
    return -1;
S
TD-1574  
Shengliang Guan 已提交
293 294
  }

S
Shengliang Guan 已提交
295 296
  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 已提交
297 298
  }

S
Shengliang Guan 已提交
299
  wordfree(&full_path);
S
TD-1574  
Shengliang Guan 已提交
300

301
  return 0;
S
Shengliang Guan 已提交
302
}
S
TD-1574  
Shengliang Guan 已提交
303

wafwerar's avatar
wafwerar 已提交
304
int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen) {
S
Shengliang Guan 已提交
305
  char tmp[PATH_MAX] = {0};
wafwerar's avatar
wafwerar 已提交
306
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
307
  if (_fullpath(tmp, dirname, maxlen) != NULL) {
wafwerar's avatar
wafwerar 已提交
308
#else
S
Shengliang Guan 已提交
309
  if (realpath(dirname, tmp) != NULL) {
wafwerar's avatar
wafwerar 已提交
310 311 312 313 314 315 316
#endif
    if (realPath == NULL) {
      strncpy(dirname, tmp, maxlen);
    } else {
      strncpy(realPath, tmp, maxlen);
    }
    return 0;
S
TD-1574  
Shengliang Guan 已提交
317 318
  }

wafwerar's avatar
wafwerar 已提交
319
  return -1;
S
TD-1574  
Shengliang Guan 已提交
320
}
S
Shengliang Guan 已提交
321

322
bool taosIsDir(const char *dirname) {
wafwerar's avatar
wafwerar 已提交
323 324 325
  TdDirPtr pDir = taosOpenDir(dirname);
  if (pDir != NULL) {
    taosCloseDir(&pDir);
326 327 328 329 330
    return true;
  }
  return false;
}

dengyihao's avatar
dengyihao 已提交
331
char *taosDirName(char *name) {
wafwerar's avatar
wafwerar 已提交
332
#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
333
  char Drive1[MAX_PATH], Dir1[MAX_PATH];
wafwerar's avatar
wafwerar 已提交
334 335 336
  _splitpath(name, Drive1, Dir1, NULL, NULL);
  size_t dirNameLen = strlen(Drive1) + strlen(Dir1);
  if (dirNameLen > 0) {
wafwerar's avatar
wafwerar 已提交
337 338 339 340 341 342 343
    if (name[dirNameLen - 1] == '/' || name[dirNameLen - 1] == '\\') {
      name[dirNameLen - 1] = 0;
    } else {
      name[dirNameLen] = 0;
    }
  } else {
    name[0] = 0;
wafwerar's avatar
wafwerar 已提交
344 345
  }
  return name;
wafwerar's avatar
wafwerar 已提交
346 347 348 349 350 351 352
#elif defined(_TD_DARWIN_64)
  char *end = strrchr(name, '/');
  if (end != NULL) {
    *end = '\0';
  } else {
    name[0] = 0;
  }
L
Liu Jicong 已提交
353
  return name;
wafwerar's avatar
wafwerar 已提交
354
#else
wafwerar's avatar
wafwerar 已提交
355
  return dirname(name);
wafwerar's avatar
wafwerar 已提交
356
#endif
wafwerar's avatar
wafwerar 已提交
357 358
}

dengyihao's avatar
dengyihao 已提交
359
char *taosDirEntryBaseName(char *name) {
wafwerar's avatar
wafwerar 已提交
360 361 362 363 364
#ifdef WINDOWS
  char Filename1[MAX_PATH], Ext1[MAX_PATH];
  _splitpath(name, NULL, NULL, Filename1, Ext1);
  return name + (strlen(name) - strlen(Filename1) - strlen(Ext1));
#else
dengyihao's avatar
dengyihao 已提交
365
  return (char *)basename(name);
wafwerar's avatar
wafwerar 已提交
366
#endif
wafwerar's avatar
wafwerar 已提交
367 368 369 370 371 372
}

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

#ifdef WINDOWS
dengyihao's avatar
dengyihao 已提交
375 376
  char   szFind[MAX_PATH];  //这是要找的
  HANDLE hFind;
wafwerar's avatar
wafwerar 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389

  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;
#else
wafwerar's avatar
wafwerar 已提交
390
  return (TdDirPtr)opendir(dirname);
wafwerar's avatar
wafwerar 已提交
391
#endif
wafwerar's avatar
wafwerar 已提交
392 393 394 395 396 397
}

TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
  if (pDir == NULL) {
    return NULL;
  }
wafwerar's avatar
wafwerar 已提交
398 399 400 401
#ifdef WINDOWS
  if (!FindNextFile(pDir->hFind, &(pDir->dirEntry.findFileData))) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
402
  return (TdDirEntryPtr) & (pDir->dirEntry.findFileData);
wafwerar's avatar
wafwerar 已提交
403
#else
dengyihao's avatar
dengyihao 已提交
404
  return (TdDirEntryPtr)readdir((DIR *)pDir);
wafwerar's avatar
wafwerar 已提交
405
#endif
wafwerar's avatar
wafwerar 已提交
406 407 408 409 410 411
}

bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) {
  if (pDirEntry == NULL) {
    return false;
  }
wafwerar's avatar
wafwerar 已提交
412 413 414
#ifdef WINDOWS
  return (pDirEntry->findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
dengyihao's avatar
dengyihao 已提交
415
  return (((dirent *)pDirEntry)->d_type & DT_DIR) != 0;
wafwerar's avatar
wafwerar 已提交
416
#endif
wafwerar's avatar
wafwerar 已提交
417 418
}

dengyihao's avatar
dengyihao 已提交
419
char *taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
L
Liu Jicong 已提交
420 421 422
  /*if (pDirEntry == NULL) {*/
  /*return NULL;*/
  /*}*/
wafwerar's avatar
wafwerar 已提交
423 424 425
#ifdef WINDOWS
  return pDirEntry->findFileData.cFileName;
#else
dengyihao's avatar
dengyihao 已提交
426
  return ((dirent *)pDirEntry)->d_name;
wafwerar's avatar
wafwerar 已提交
427
#endif
wafwerar's avatar
wafwerar 已提交
428 429
}

wafwerar's avatar
wafwerar 已提交
430 431
int32_t taosCloseDir(TdDirPtr *ppDir) {
  if (ppDir == NULL || *ppDir == NULL) {
wafwerar's avatar
wafwerar 已提交
432 433
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
434 435 436 437 438 439
#ifdef WINDOWS
  FindClose((*ppDir)->hFind);
  taosMemoryFree(*ppDir);
  *ppDir = NULL;
  return 0;
#else
dengyihao's avatar
dengyihao 已提交
440
  closedir((DIR *)*ppDir);
wafwerar's avatar
wafwerar 已提交
441 442
  *ppDir = NULL;
  return 0;
L
Liu Jicong 已提交
443
#endif
wafwerar's avatar
wafwerar 已提交
444
}