osDir.c 4.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
Shengliang Guan 已提交
20
#include "osString.h"
S
TD-1574  
Shengliang Guan 已提交
21

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

// todo

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

S
Shengliang Guan 已提交
34 35 36 37 38 39
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wordexp.h>

wafwerar's avatar
wafwerar 已提交
40 41 42 43
typedef struct dirent dirent;
typedef struct DIR TdDir;
typedef struct dirent TdDirent;

H
Hongze Cheng 已提交
44
void taosRemoveDir(const char *dirname) {
S
Shengliang Guan 已提交
45
  DIR *dir = opendir(dirname);
S
Shengliang Guan 已提交
46 47 48 49 50
  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 已提交
51

S
Shengliang Guan 已提交
52
    char filename[1024];
S
Shengliang Guan 已提交
53
    snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name);
S
Shengliang Guan 已提交
54 55 56
    if (de->d_type & DT_DIR) {
      taosRemoveDir(filename);
    } else {
57
      (void)taosRemoveFile(filename);
S
Shengliang Guan 已提交
58
      //printf("file:%s is removed\n", filename);
S
Shengliang Guan 已提交
59 60 61 62
    }
  }

  closedir(dir);
S
Shengliang Guan 已提交
63
  rmdir(dirname);
S
Shengliang Guan 已提交
64

S
Shengliang Guan 已提交
65
  //printf("dir:%s is removed\n", dirname);
S
Shengliang Guan 已提交
66 67
}

68
bool taosDirExist(char *dirname) { return taosCheckExistFile(dirname); }
69

70
int32_t taosMkDir(const char *dirname) {
S
Shengliang Guan 已提交
71 72
  int32_t code = mkdir(dirname, 0755);
  if (code < 0 && errno == EEXIST) {
73
    return 0;
S
Shengliang Guan 已提交
74 75
  }

76
  return code;
S
Shengliang Guan 已提交
77 78
}

S
config  
Shengliang Guan 已提交
79
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
S
Shengliang Guan 已提交
80
  DIR *dir = opendir(dirname);
S
TD-1263  
Shengliang Guan 已提交
81 82
  if (dir == NULL) return;

S
TD-4088  
Shengliang Guan 已提交
83
  int64_t        sec = taosGetTimestampSec();
S
TD-1263  
Shengliang Guan 已提交
84 85 86 87 88 89
  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 已提交
90
    snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name);
S
TD-1263  
Shengliang Guan 已提交
91 92 93
    if (de->d_type & DT_DIR) {
      continue;
    } else {
S
Shengliang Guan 已提交
94
      int32_t len = (int32_t)strlen(filename);
S
TD-1574  
Shengliang Guan 已提交
95 96 97 98
      if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) {
        len -= 3;
      }

S
TD-1263  
Shengliang Guan 已提交
99
      int64_t fileSec = 0;
S
Shengliang Guan 已提交
100
      for (int32_t i = len - 1; i >= 0; i--) {
S
TD-1263  
Shengliang Guan 已提交
101
        if (filename[i] == '.') {
S
TD-1263  
Shengliang Guan 已提交
102
          fileSec = atoll(filename + i + 1);
S
TD-1263  
Shengliang Guan 已提交
103 104 105 106
          break;
        }
      }

S
TD-1263  
Shengliang Guan 已提交
107
      if (fileSec <= 100) continue;
dengyihao's avatar
dengyihao 已提交
108
      int32_t days = (int32_t)(TABS(sec - fileSec) / 86400 + 1);
S
TD-1263  
Shengliang Guan 已提交
109
      if (days > keepDays) {
110
        (void)taosRemoveFile(filename);
S
Shengliang Guan 已提交
111
        //printf("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
112
      } else {
S
Shengliang Guan 已提交
113
        //printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
114 115 116 117 118
      }
    }
  }

  closedir(dir);
S
Shengliang Guan 已提交
119
  rmdir(dirname);
S
TD-1263  
Shengliang Guan 已提交
120
}
S
TD-1574  
Shengliang Guan 已提交
121

S
config  
Shengliang Guan 已提交
122
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
S
Shengliang Guan 已提交
123 124
  wordexp_t full_path;
  if (0 != wordexp(dirname, &full_path, 0)) {
S
Shengliang Guan 已提交
125
    //printf("failed to expand path:%s since %s", dirname, strerror(errno));
S
Shengliang Guan 已提交
126
    wordfree(&full_path);
127
    return -1;
S
TD-1574  
Shengliang Guan 已提交
128 129
  }

S
Shengliang Guan 已提交
130 131
  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 已提交
132 133
  }

S
Shengliang Guan 已提交
134
  wordfree(&full_path);
S
TD-1574  
Shengliang Guan 已提交
135

136
  return 0;
S
Shengliang Guan 已提交
137
}
S
TD-1574  
Shengliang Guan 已提交
138

139
int32_t taosRealPath(char *dirname, int32_t maxlen) {
S
Shengliang Guan 已提交
140 141 142
  char tmp[PATH_MAX] = {0};
  if (realpath(dirname, tmp) != NULL) {
    strncpy(dirname, tmp, maxlen);
S
TD-1574  
Shengliang Guan 已提交
143 144
  }

145
  return 0;
S
TD-1574  
Shengliang Guan 已提交
146
}
S
Shengliang Guan 已提交
147

148 149 150 151 152 153 154 155 156
bool taosIsDir(const char *dirname) {
  DIR *dir = opendir(dirname);
  if (dir != NULL) {
    closedir(dir);
    return true;
  }
  return false;
}

wafwerar's avatar
wafwerar 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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
char* taosDirName(char *name) {
  return dirname(name);
}

char* taosDirEntryBaseName(char *name) {
  return basename(name);
}

TdDirPtr taosOpenDir(const char *dirname) {
  if (dirname == NULL) {
    return NULL;
  }
  return (TdDirPtr)opendir(dirname);
}

TdDirEntryPtr taosReadDir(TdDirPtr pDir) {
  if (pDir == NULL) {
    return NULL;
  }
  return (TdDirEntryPtr)readdir((DIR*)pDir);
}

bool taosDirEntryIsDir(TdDirEntryPtr pDirEntry) {
  if (pDirEntry == NULL) {
    return false;
  }
  return (((dirent*)pDirEntry)->d_type & DT_DIR) != 0;
}

char* taosGetDirEntryName(TdDirEntryPtr pDirEntry) {
  if (pDirEntry == NULL) {
    return NULL;
  }
  return ((dirent*)pDirEntry)->d_name;
}

int32_t taosCloseDir(TdDirPtr pDir) {
  if (pDir == NULL) {
    return -1;
  }
  return closedir((DIR*)pDir);
}

L
Liu Jicong 已提交
200
#endif