osDir.c 5.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
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) {
wafwerar's avatar
wafwerar 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  if (dirname == NULL) return -1;
  char *temp = strdup(dirname);
  char *pos = temp;
  int32_t code = 0;

  if (strncmp(temp, "/", 1) == 0) {
    pos += 1;
  } else if (strncmp(temp, "./", 2) == 0) {
    pos += 2;
  }
  
  for ( ; *pos != '\0'; pos++) {
    if (*pos == '/') {
      *pos = '\0';
      code = mkdir(temp, 0755);
      if (code < 0 && errno != EEXIST) {
        free(temp);
        return code;
      }
      *pos = '/';
    }
  }
  
  if (*(pos - 1) != '/') {
    code = mkdir(temp, 0755);
    if (code < 0 && errno != EEXIST) {
      free(temp);
      return code;
    }
  }
  free(temp);

  // int32_t code = mkdir(dirname, 0755);
S
Shengliang Guan 已提交
104
  if (code < 0 && errno == EEXIST) {
105
    return 0;
S
Shengliang Guan 已提交
106 107
  }

108
  return code;
S
Shengliang Guan 已提交
109 110
}

S
config  
Shengliang Guan 已提交
111
void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
S
Shengliang Guan 已提交
112
  DIR *dir = opendir(dirname);
S
TD-1263  
Shengliang Guan 已提交
113 114
  if (dir == NULL) return;

S
TD-4088  
Shengliang Guan 已提交
115
  int64_t        sec = taosGetTimestampSec();
S
TD-1263  
Shengliang Guan 已提交
116 117 118 119 120 121
  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 已提交
122
    snprintf(filename, sizeof(filename), "%s/%s", dirname, de->d_name);
S
TD-1263  
Shengliang Guan 已提交
123 124 125
    if (de->d_type & DT_DIR) {
      continue;
    } else {
S
Shengliang Guan 已提交
126
      int32_t len = (int32_t)strlen(filename);
S
TD-1574  
Shengliang Guan 已提交
127 128 129 130
      if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) {
        len -= 3;
      }

S
TD-1263  
Shengliang Guan 已提交
131
      int64_t fileSec = 0;
S
Shengliang Guan 已提交
132
      for (int32_t i = len - 1; i >= 0; i--) {
S
TD-1263  
Shengliang Guan 已提交
133
        if (filename[i] == '.') {
S
TD-1263  
Shengliang Guan 已提交
134
          fileSec = atoll(filename + i + 1);
S
TD-1263  
Shengliang Guan 已提交
135 136 137 138
          break;
        }
      }

S
TD-1263  
Shengliang Guan 已提交
139
      if (fileSec <= 100) continue;
dengyihao's avatar
dengyihao 已提交
140
      int32_t days = (int32_t)(TABS(sec - fileSec) / 86400 + 1);
S
TD-1263  
Shengliang Guan 已提交
141
      if (days > keepDays) {
142
        (void)taosRemoveFile(filename);
S
Shengliang Guan 已提交
143
        //printf("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
144
      } else {
S
Shengliang Guan 已提交
145
        //printf("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
S
TD-1263  
Shengliang Guan 已提交
146 147 148 149 150
      }
    }
  }

  closedir(dir);
S
Shengliang Guan 已提交
151
  rmdir(dirname);
S
TD-1263  
Shengliang Guan 已提交
152
}
S
TD-1574  
Shengliang Guan 已提交
153

S
config  
Shengliang Guan 已提交
154
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {
S
Shengliang Guan 已提交
155 156
  wordexp_t full_path;
  if (0 != wordexp(dirname, &full_path, 0)) {
S
Shengliang Guan 已提交
157
    //printf("failed to expand path:%s since %s", dirname, strerror(errno));
S
Shengliang Guan 已提交
158
    wordfree(&full_path);
159
    return -1;
S
TD-1574  
Shengliang Guan 已提交
160 161
  }

S
Shengliang Guan 已提交
162 163
  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 已提交
164 165
  }

S
Shengliang Guan 已提交
166
  wordfree(&full_path);
S
TD-1574  
Shengliang Guan 已提交
167

168
  return 0;
S
Shengliang Guan 已提交
169
}
S
TD-1574  
Shengliang Guan 已提交
170

171
int32_t taosRealPath(char *dirname, int32_t maxlen) {
S
Shengliang Guan 已提交
172 173 174
  char tmp[PATH_MAX] = {0};
  if (realpath(dirname, tmp) != NULL) {
    strncpy(dirname, tmp, maxlen);
S
TD-1574  
Shengliang Guan 已提交
175 176
  }

177
  return 0;
S
TD-1574  
Shengliang Guan 已提交
178
}
S
Shengliang Guan 已提交
179

180 181 182 183 184 185 186 187 188
bool taosIsDir(const char *dirname) {
  DIR *dir = opendir(dirname);
  if (dir != NULL) {
    closedir(dir);
    return true;
  }
  return false;
}

wafwerar's avatar
wafwerar 已提交
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
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 已提交
232
#endif