CommonUtil.cpp 5.5 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
J
jinhai 已提交
5
//
6 7 8 9 10
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "utils/CommonUtil.h"
G
groot 已提交
13
#include "utils/Log.h"
G
groot 已提交
14 15

#include <dirent.h>
16
#include <fiu/fiu-local.h>
S
starlord 已提交
17 18 19
#include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>
C
Cai Yudong 已提交
20
#include <boost/filesystem.hpp>
S
starlord 已提交
21
#include <iostream>
22
#include <vector>
G
groot 已提交
23

J
jinhai 已提交
24
namespace milvus {
G
groot 已提交
25 26 27

namespace fs = boost::filesystem;

S
starlord 已提交
28
bool
S
starlord 已提交
29 30
CommonUtil::IsDirectoryExist(const std::string& path) {
    DIR* dp = nullptr;
G
groot 已提交
31 32 33 34 35 36 37 38
    if ((dp = opendir(path.c_str())) == nullptr) {
        return false;
    }

    closedir(dp);
    return true;
}

S
starlord 已提交
39
Status
S
starlord 已提交
40
CommonUtil::CreateDirectory(const std::string& path) {
S
starlord 已提交
41
    if (path.empty()) {
S
starlord 已提交
42
        return Status::OK();
S
starlord 已提交
43 44
    }

S
starlord 已提交
45 46 47
    struct stat directory_stat;
    int status = stat(path.c_str(), &directory_stat);
    if (status == 0) {
S
starlord 已提交
48
        return Status::OK();  // already exist
G
groot 已提交
49 50 51 52
    }

    fs::path fs_path(path);
    fs::path parent_path = fs_path.parent_path();
S
starlord 已提交
53
    Status err_status = CreateDirectory(parent_path.string());
S
shengjh 已提交
54
    fiu_do_on("CommonUtil.CreateDirectory.create_parent_fail", err_status = Status(SERVER_INVALID_ARGUMENT, ""));
S
starlord 已提交
55
    if (!err_status.ok()) {
S
starlord 已提交
56
        return err_status;
G
groot 已提交
57 58
    }

S
starlord 已提交
59 60
    status = stat(path.c_str(), &directory_stat);
    if (status == 0) {
S
starlord 已提交
61
        return Status::OK();  // already exist
G
groot 已提交
62 63
    }

S
starlord 已提交
64
    int makeOK = mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IROTH);
S
shengjh 已提交
65
    fiu_do_on("CommonUtil.CreateDirectory.create_dir_fail", makeOK = 1);
G
groot 已提交
66
    if (makeOK != 0) {
S
starlord 已提交
67
        return Status(SERVER_UNEXPECTED_ERROR, "failed to create directory: " + path);
G
groot 已提交
68 69
    }

S
starlord 已提交
70
    return Status::OK();
G
groot 已提交
71 72
}

G
groot 已提交
73
namespace {
S
starlord 已提交
74
void
S
starlord 已提交
75 76
RemoveDirectory(const std::string& path) {
    DIR* dir = nullptr;
S
starlord 已提交
77 78 79 80 81
    const int32_t buf_size = 256;
    char file_name[buf_size];

    std::string folder_name = path + "/%s";
    if ((dir = opendir(path.c_str())) != nullptr) {
C
Cai Yudong 已提交
82
        struct dirent* dmsg;
S
starlord 已提交
83
        while ((dmsg = readdir(dir)) != nullptr) {
S
starlord 已提交
84
            if (strcmp(dmsg->d_name, ".") != 0 && strcmp(dmsg->d_name, "..") != 0) {
S
starlord 已提交
85 86
                snprintf(file_name, buf_size, folder_name.c_str(), dmsg->d_name);
                std::string tmp = file_name;
C
chen qingxiang 已提交
87
                if (tmp.find('.') == std::string::npos) {
S
starlord 已提交
88
                    RemoveDirectory(file_name);
G
groot 已提交
89
                }
S
starlord 已提交
90
                remove(file_name);
G
groot 已提交
91 92
            }
        }
S
starlord 已提交
93
    }
G
groot 已提交
94

S
starlord 已提交
95 96
    if (dir != nullptr) {
        closedir(dir);
G
groot 已提交
97
    }
S
starlord 已提交
98
    remove(path.c_str());
G
groot 已提交
99
}
S
starlord 已提交
100
}  // namespace
G
groot 已提交
101

S
starlord 已提交
102
Status
S
starlord 已提交
103
CommonUtil::DeleteDirectory(const std::string& path) {
S
starlord 已提交
104
    if (path.empty()) {
S
starlord 已提交
105
        return Status::OK();
S
starlord 已提交
106 107
    }

S
starlord 已提交
108 109
    struct stat directory_stat;
    int statOK = stat(path.c_str(), &directory_stat);
S
starlord 已提交
110 111 112
    if (statOK != 0) {
        return Status::OK();
    }
G
groot 已提交
113 114

    RemoveDirectory(path);
S
starlord 已提交
115
    return Status::OK();
G
groot 已提交
116 117
}

S
starlord 已提交
118
bool
S
starlord 已提交
119
CommonUtil::IsFileExist(const std::string& path) {
G
groot 已提交
120 121 122
    return (access(path.c_str(), F_OK) == 0);
}

S
starlord 已提交
123
uint64_t
S
starlord 已提交
124
CommonUtil::GetFileSize(const std::string& path) {
S
starlord 已提交
125 126
    struct stat file_info;
    if (stat(path.c_str(), &file_info) < 0) {
S
starlord 已提交
127 128
        return 0;
    }
S
starlord 已提交
129 130

    return static_cast<uint64_t>(file_info.st_size);
S
starlord 已提交
131 132
}

S
starlord 已提交
133 134
std::string
CommonUtil::GetFileName(std::string filename) {
S
starlord 已提交
135 136 137 138
    int pos = filename.find_last_of('/');
    return filename.substr(pos + 1);
}

S
starlord 已提交
139 140
std::string
CommonUtil::GetExePath() {
C
Cai Yudong 已提交
141
    const int64_t buf_len = 1024;
G
groot 已提交
142
    char buf[buf_len];
C
Cai Yudong 已提交
143
    int64_t cnt = readlink("/proc/self/exe", buf, buf_len);
S
shengjh 已提交
144
    fiu_do_on("CommonUtil.GetExePath.readlink_fail", cnt = -1);
S
starlord 已提交
145
    if (cnt < 0 || cnt >= buf_len) {
G
groot 已提交
146 147 148
        return "";
    }

G
groot 已提交
149 150 151
    buf[cnt] = '\0';

    std::string exe_path = buf;
S
shengjh 已提交
152
    fiu_do_on("CommonUtil.GetExePath.exe_path_error", exe_path = "/");
A
ABNER-1 已提交
153
    if (exe_path.rfind('/') != exe_path.length() - 1) {
G
groot 已提交
154
        std::string sub_str = exe_path.substr(0, exe_path.rfind('/'));
G
groot 已提交
155 156
        return sub_str + "/";
    }
G
groot 已提交
157
    return exe_path;
G
groot 已提交
158
}
G
groot 已提交
159

S
starlord 已提交
160
bool
S
starlord 已提交
161 162
CommonUtil::TimeStrToTime(const std::string& time_str, time_t& time_integer, tm& time_struct,
                          const std::string& format) {
G
groot 已提交
163 164 165
    time_integer = 0;
    memset(&time_struct, 0, sizeof(tm));

S
starlord 已提交
166 167
    int ret = sscanf(time_str.c_str(), format.c_str(), &(time_struct.tm_year), &(time_struct.tm_mon),
                     &(time_struct.tm_mday), &(time_struct.tm_hour), &(time_struct.tm_min), &(time_struct.tm_sec));
S
starlord 已提交
168
    if (ret <= 0) {
G
groot 已提交
169 170 171 172 173 174 175 176 177 178
        return false;
    }

    time_struct.tm_year -= 1900;
    time_struct.tm_mon--;
    time_integer = mktime(&time_struct);

    return true;
}

S
starlord 已提交
179
void
S
starlord 已提交
180
CommonUtil::ConvertTime(time_t time_integer, tm& time_struct) {
S
starlord 已提交
181
    localtime_r(&time_integer, &time_struct);
G
groot 已提交
182 183
}

S
starlord 已提交
184
void
S
starlord 已提交
185
CommonUtil::ConvertTime(tm time_struct, time_t& time_integer) {
G
groot 已提交
186
    time_integer = mktime(&time_struct);
G
groot 已提交
187 188
}

F
feisiyicl 已提交
189
#ifdef ENABLE_CPU_PROFILING
C
Cai Yudong 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
std::string
CommonUtil::GetCurrentTimeStr() {
    time_t tt;
    time(&tt);
    tt = tt + 8 * 60;
    tm t;
    gmtime_r(&tt, &t);

    std::string str = std::to_string(t.tm_year + 1900) + "_" + std::to_string(t.tm_mon + 1) + "_" +
                      std::to_string(t.tm_mday) + "_" + std::to_string(t.tm_hour) + "_" + std::to_string(t.tm_min) +
                      "_" + std::to_string(t.tm_sec);
    return str;
}
#endif

S
starlord 已提交
205
}  // namespace milvus