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
/*
 * 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 17
#define _DEFAULT_SOURCE

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

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

// todo

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

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

H
Hongze Cheng 已提交
39
void taosRemoveDir(const char *dirname) {
S
Shengliang Guan 已提交
40
  DIR *dir = opendir(dirname);
S
Shengliang Guan 已提交
41 42 43 44 45
  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 已提交
46

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

  closedir(dir);
S
Shengliang Guan 已提交
58
  rmdir(dirname);
S
Shengliang Guan 已提交
59

S
Shengliang Guan 已提交
60
  //printf("dir:%s is removed\n", dirname);
S
Shengliang Guan 已提交
61 62
}

63
int32_t taosDirExist(char *dirname) { return access(dirname, F_OK); }
64

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

71
  return code;
S
Shengliang Guan 已提交
72 73
}

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
129
  wordfree(&full_path);
S
TD-1574  
Shengliang Guan 已提交
130

131
  return 0;
S
Shengliang Guan 已提交
132
}
S
TD-1574  
Shengliang Guan 已提交
133

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

140
  return 0;
S
TD-1574  
Shengliang Guan 已提交
141
}
S
Shengliang Guan 已提交
142

L
Liu Jicong 已提交
143
#endif