forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DfsCommon.cpp
257 lines (209 loc) · 5.25 KB
/
DfsCommon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* @Filename DfsCommon.cpp
* @Author huanggaofeng
* @Date 2017-01-31
* @brief utils functions
*/
#include "DfsCommon.h"
#include "DfsBase.h"
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include "DfsMd5.h"
using namespace dev::rpc::fs;
namespace dev
{
namespace rpc
{
namespace fs
{
/// check if the specific directory exists
bool ChkDirExists(const string &pm_sDir)
{
struct stat stDir;
if (stat(pm_sDir.c_str(), &stDir) != 0)
{
return false;
}
return S_ISDIR(stDir.st_mode);
}
/// check if the specific directory has write permission
bool ChkDirWritePerm(const string &pm_sDir)
{
return (0 == access(pm_sDir.data(), W_OK));
}
/// check if the specific file exists
bool ChkFileExists(const string &pm_sFile)
{
struct stat stFile;
if (stat(pm_sFile.c_str(), &stFile) != 0)
{
return false;
}
return S_ISREG(stFile.st_mode);
}
/// get the file size
long GetFileSize(const string &pm_sFile)
{
struct stat stFile;
if (stat(pm_sFile.c_str(), &stFile) != 0)
{
return -1;
}
return stFile.st_size;
}
/// simple copy file function
int FileCpy(const string &pm_sSrc, const string &pm_sDest)
{
FILE* fp = NULL;
FILE* fp_dst = NULL;
fp = fopen(pm_sSrc.c_str(), "rb");
if (fp == NULL)
{
return -1;
}
fp_dst = fopen(pm_sDest.c_str(), "wb");
char szBuffer[4096];
size_t bytes = 0;
size_t bytes_write = 0;
size_t bytes_read = 0;
while((bytes=fread(szBuffer, 1, 4096, fp)) > 0) {
bytes_read += bytes;
int ret = fwrite(szBuffer, 1, bytes, fp_dst);
if (ret)
{
bytes_write += ret;
}
}
fclose(fp);
fclose(fp_dst);
if (bytes_write != bytes_read)
{
return -1;
}
return 0;
}
/// simply move file function
int FileMv(const string &pm_sSrc, const string &pm_sDest)
{
if (rename(pm_sSrc.c_str(), pm_sDest.c_str()) != 0)
{
return -1;
}
return 0;
}
/// remove a file
int FileRm(const string &pm_sFileName)
{
if (unlink(pm_sFileName.c_str()) != 0)
{
return -1;
}
return 0;
}
// create a specific directory
bool createFileDir(const string &dirPath)
{
if (dirPath.size() <= 0)
return false;
if (0 != mkdir(dirPath.c_str(), 0755))
return false;
return true;
}
//get the date string with the pattern: YYYYMMDD_HHMMSS
string currentTimestamp()
{
struct timeval objTime;
gettimeofday(&objTime, NULL);
char szTime[256];
memset(szTime, 0, 256);
snprintf(szTime, 256, "%ld", objTime.tv_sec);
return szTime;
}
void createDfsFileNames(const string& path, const string &fileID, const string &fileName, string &filename, string &filename_bak)
{
filename = path;
filename += "/";
filename += fileID;
filename += "_";
filename += fileName;
filename_bak = filename;
filename_bak += "_";
filename_bak += currentTimestamp();
//strFileBak += ".bak";
}
void listAllFiles(const std::string &dirPath, vector<string> &files, bool fullPath)
{
if (!ChkDirExists(dirPath))
{
return;
}
files.clear();
DIR *d;
struct dirent *dir;
d = opendir(dirPath.data());
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
{
continue;
}
if (!fullPath)
{
files.push_back(dir->d_name);
}
else
{
string strFile = dirPath;
strFile += "/";
strFile += dir->d_name;
files.push_back(strFile);
}
}
closedir(d);
}
return;
}
void SplitString(const string &pm_sExpression, char pm_cDelimiter, vector<string> &pm_vecArray)
{
size_t iPosBegin = 0;
size_t iPosEnd;
while ((iPosEnd = pm_sExpression.find(pm_cDelimiter, iPosBegin)) != string::npos)
{
pm_vecArray.push_back(pm_sExpression.substr(iPosBegin, iPosEnd - iPosBegin));
iPosBegin = iPosEnd + 1;
}
pm_vecArray.push_back(pm_sExpression.substr(iPosBegin));
}
// create the directory path by container
void createDirectoryByContainer(const string& base, const string &container, string &directory)
{
directory.clear();
if (container.empty())
{
dfs_warn << "the container is null \n";
return;
}
MD5_CTX ctx;
unsigned char szResult[16] = {0};
memset(szResult, 0, 16);
ms_MD5_Init(&ctx);
ms_MD5_Update(&ctx, container.data(), container.size());
ms_MD5_Final(szResult, &ctx);
unsigned char lowPart = szResult[0];
unsigned char hignPart = szResult[8];
char szDirAppend[256] = {0};
memset(szDirAppend, 0, 256);
snprintf(szDirAppend, 256, "%02X/%02X/%s", lowPart, hignPart, container.data());
directory = base;
directory += "/";
directory += szDirAppend;
}
}
}
}