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

#include "os.h"
S
Shengliang Guan 已提交
17
#include "osString.h"
S
TD-1574  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20 21 22 23 24 25 26 27 28 29
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
/*
 * windows implementation
 */

// todo

#else
/*
 * linux implementation
 */
S
Shengliang Guan 已提交
30

S
Shengliang Guan 已提交
31 32 33 34 35 36
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>

H
Hongze Cheng 已提交
37
void taosRemoveDir(const char *dirname) {
S
Shengliang Guan 已提交
38
  DIR *dir = opendir(dirname);
S
Shengliang Guan 已提交
39 40 41 42 43
  if (dir == NULL) return;

  struct dirent *de = NULL;
  while ((de = readdir(dir)) != NULL) {
    if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
S
TD-4088  
Shengliang Guan 已提交
44

S
Shengliang Guan 已提交
45
    char filename[1024];
S
Shengliang Guan 已提交
46
    snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name);
S
Shengliang Guan 已提交
47 48 49 50
    if (de->d_type & DT_DIR) {
      taosRemoveDir(filename);
    } else {
      (void)remove(filename);
H
more  
Hongze Cheng 已提交
51
      printf("file:%s is removed\n", filename);
S
Shengliang Guan 已提交
52 53 54 55
    }
  }

  closedir(dir);
S
Shengliang Guan 已提交
56
  rmdir(dirname);
S
Shengliang Guan 已提交
57

L
Liu Jicong 已提交
58
  printf("dir:%s is removed\n", dirname);
S
Shengliang Guan 已提交
59 60
}

S
Shengliang Guan 已提交
61
bool taosDirExist(char *dirname) { return access(dirname, F_OK) == 0; }
62

63
bool taosMkDir(char *dirname) {
S
Shengliang Guan 已提交
64 65 66 67 68 69
  int32_t code = mkdir(dirname, 0755);
  if (code < 0 && errno == EEXIST) {
    return true;
  }

  return code == 0;
S
Shengliang Guan 已提交
70 71
}

S
Shengliang Guan 已提交
72 73
void taosRemoveOldFiles(char *dirname, int32_t keepDays) {
  DIR *dir = opendir(dirname);
S
TD-1263  
Shengliang Guan 已提交
74 75
  if (dir == NULL) return;

S
TD-4088  
Shengliang Guan 已提交
76
  int64_t        sec = taosGetTimestampSec();
S
TD-1263  
Shengliang Guan 已提交
77 78 79 80 81 82
  struct dirent *de = NULL;

  while ((de = readdir(dir)) != NULL) {
    if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;

    char filename[1024];
S
Shengliang Guan 已提交
83
    snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name);
S
TD-1263  
Shengliang Guan 已提交
84 85 86
    if (de->d_type & DT_DIR) {
      continue;
    } else {
S
Shengliang Guan 已提交
87
      int32_t len = (int32_t)strlen(filename);
S
TD-1574  
Shengliang Guan 已提交
88 89 90 91
      if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) {
        len -= 3;
      }

S
TD-1263  
Shengliang Guan 已提交
92
      int64_t fileSec = 0;
S
Shengliang Guan 已提交
93
      for (int32_t i = len - 1; i >= 0; i--) {
S
TD-1263  
Shengliang Guan 已提交
94
        if (filename[i] == '.') {
S
TD-1263  
Shengliang Guan 已提交
95
          fileSec = atoll(filename + i + 1);
S
TD-1263  
Shengliang Guan 已提交
96 97 98 99
          break;
        }
      }

S
TD-1263  
Shengliang Guan 已提交
100
      if (fileSec <= 100) continue;
S
Shengliang Guan 已提交
101
      int32_t days = (int32_t)(ABS(sec - fileSec) / 86400 + 1);
S
TD-1263  
Shengliang Guan 已提交
102 103
      if (days > keepDays) {
        (void)remove(filename);
S
Shengliang Guan 已提交
104
        printf("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
105
      } else {
S
Shengliang Guan 已提交
106
        printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
107 108 109 110 111
      }
    }
  }

  closedir(dir);
S
Shengliang Guan 已提交
112
  rmdir(dirname);
S
TD-1263  
Shengliang Guan 已提交
113
}
S
TD-1574  
Shengliang Guan 已提交
114

S
Shengliang Guan 已提交
115 116 117 118 119 120
bool taosExpandDir(char *dirname, char *outname, int32_t maxlen) {
  wordexp_t full_path;
  if (0 != wordexp(dirname, &full_path, 0)) {
    printf("failed to expand path:%s since %s", dirname, strerror(errno));
    wordfree(&full_path);
    return false;
S
TD-1574  
Shengliang Guan 已提交
121 122
  }

S
Shengliang Guan 已提交
123 124
  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 已提交
125 126
  }

S
Shengliang Guan 已提交
127
  wordfree(&full_path);
S
TD-1574  
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130
  return true;
}
S
TD-1574  
Shengliang Guan 已提交
131

S
Shengliang Guan 已提交
132 133 134 135
bool taosRealPath(char *dirname, int32_t maxlen) {
  char tmp[PATH_MAX] = {0};
  if (realpath(dirname, tmp) != NULL) {
    strncpy(dirname, tmp, maxlen);
S
TD-1574  
Shengliang Guan 已提交
136 137
  }

S
Shengliang Guan 已提交
138
  return true;
S
TD-1574  
Shengliang Guan 已提交
139
}
S
Shengliang Guan 已提交
140

L
Liu Jicong 已提交
141
#endif