/
egtbscanner.cpp
222 lines (217 loc) · 6.65 KB
/
egtbscanner.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
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <dirent.h>
#include "egtbscanner.h"
#include "egmaintypes.h"
#include "egpglobals.h"
#include "egtbfile.h"
using namespace std;
// < 150 symbols
void get_clock_str(tm *clock, char *str) {
str[0] = '\0';
char time_path[20];
sprintf(time_path, "%d", clock->tm_year);
strcat(str, time_path);
sprintf(time_path, "%d", clock->tm_yday);
strcat(str, time_path);
sprintf(time_path, "%d", clock->tm_hour);
strcat(str, time_path);
sprintf(time_path, "%d", clock->tm_min);
strcat(str, time_path);
sprintf(time_path, "%d", clock->tm_sec);
strcat(str, time_path);
}
//create TB_ini.txt in current directory whis information about exist files and write this in not_exist_tables
//return 0 if error and lenght max pieces if success
void find_not_files() {
char str[MAX_PATH], cur_path[MAX_PATH];
reset_not_exist_tables(false);
// cur_path = current directory (is it true?)
strcpy(cur_path, "TB_error.txt");
if (!access(cur_path, 0)) unlink(cur_path);
// cur_path = current directory
strcpy(cur_path, "TB_ini.txt");
FILE *TB_file = fopen(cur_path, "w");
list<char *>::iterator it_paths = table_paths.begin();
int table_type;
while (it_paths != table_paths.end()) {
strcpy(str, *it_paths);
str[strlen(str) - 1] = '\0';
char clock_str[150];
struct stat attrib;
int stat_res;
if (stat_res = stat(str, &attrib)) {
sprintf(clock_str, "NOT_EXIST");
} else {
tm *clock = gmtime((time_t *)&(attrib.st_mtime));
get_clock_str(clock, clock_str);
}
strcat(str, "\\*.* /");
strcat(str, clock_str);
int len = strlen(str);
str[len] = '/';
str[len+1] = '\0';
fprintf(TB_file, "%s\n", str);
if (stat_res) {
it_paths++;
continue;
}
dirent *drnt;
DIR *dir = opendir(*it_paths);
int extension_length;
while ((drnt = readdir(dir)) != NULL) {
if (!strcmp(drnt->d_name, ".") || !strcmp(drnt->d_name, "..") || drnt->d_type == DT_DIR)
continue;
strcpy(str, drnt->d_name);
char *ex = strchr(str, '.');
if (ex) ex++;
if (ex && compare_extension(ex, &table_type, &extension_length) && table_type <= MAX_TYPE) {
// it's used only in the rehashing of global_cache. But the rehashing is disabled.
//calculate_min_block_size(*it_paths, f.cFileName, table_type);
ex = &ex[extension_length];
while (*ex == '.' || (*ex <= '9' && *ex >= '0')) // it may be not first volume (for example *.1)
ex = &ex[1];
if (*ex == '\0') { // if it's first volume
int size_bg = strcspn(&str[1], "Kk") + 1, size_en = strcspn(str, ".");
int pieces_cnt = size_en;
if (strcspn(str, "234567") != strlen(str))
pieces_cnt--;
if (max_pieces_count[table_type] < pieces_cnt)
max_pieces_count[table_type] = pieces_cnt;
if (size_bg == size_en - size_bg) { // may be full color symmetry
char tmp_str1[10], tmp_str2[10];
strncpy(tmp_str1, str, size_bg);
tmp_str1[size_bg] = '\0';
strncpy(tmp_str2, &str[size_bg], size_bg);
tmp_str2[size_bg] = '\0';
if (!strcmp(tmp_str1, tmp_str2)) { // full color symmetry
new_set_cur_table_not_exist(str, table_type);
int str_color = strlen(str) - 1;
while (str[str_color] != 'w' && str[str_color] != 'b')
str_color--;
if (str[str_color] == 'w')
str[str_color] = 'b';
else
str[str_color] = 'w';
new_set_cur_table_not_exist(str, table_type);
} else
new_set_cur_table_not_exist(str, table_type);
} else {
new_set_cur_table_not_exist(str, table_type);
}
}
}
}
closedir(dir);
it_paths++;
}
strncpy(str, "//", 2);
for (table_type = MIN_TYPE; table_type <= MAX_TYPE; table_type++)
str[table_type+2] = '0' + max_pieces_count[table_type];
str[table_type+2] = '\0';
fprintf(TB_file, "%s\n", str);
for (table_type = MIN_TYPE; table_type <= MAX_TYPE; table_type++) {
for (unsigned int i = 0; i < NOT_EXIST_TABLES_SIZE; ++i) {
if (fwrite(¬_exist_tables[table_type][i], sizeof(char), 1, TB_file) != 1) {
fclose(TB_file);
return;
}
}
}
for (table_type = MIN_TYPE; table_type <= MAX_TYPE; table_type++)
fwrite(&min_block_size[table_type], sizeof(unsigned long), 1, TB_file);
fclose(TB_file);
known_not_exist = true;
}
//read TB_ini.txt in current directory with the information about existing files
//return 0 if error and lenght max pieces if success
void explicit_get_max_pieces_count() {
char path_file[MAX_PATH];
// path_file = current directory
strcpy(path_file, "TB_error.txt");
if (!access(path_file, 0)) {
find_not_files();
return;
}
// path_file = current directory
strcpy(path_file, "TB_ini.txt");
FILE *TB_file = fopen(path_file, "rb");
if (TB_file == NULL) {
find_not_files();
return;
}
reset_not_exist_tables(false);
char str[MAX_PATH];
bool fl = true;
list<char *>::iterator it_paths = table_paths.begin();
while (fl) {
if (fgets(str, MAX_PATH, TB_file)) {
if (strncmp(str, "//", 2) != 0) {
int separ = strcspn(str, " ");
char *time_path_from_TB = &str[separ + 2];
str[separ - 3] = '\0';
if (it_paths == table_paths.end() || strcmp(str, *it_paths)) {
fclose(TB_file);
find_not_files();
return;
}
str[separ - 4] = '\0';
it_paths++;
separ = strcspn(time_path_from_TB, "/");
time_path_from_TB[separ] = '\0';
char time_path[150];
struct tm* clock;
struct stat attrib;
if (stat(str, &attrib)) {
sprintf(time_path, "NOT_EXIST");
} else {
clock = gmtime((time_t *)&(attrib.st_mtime));
get_clock_str(clock, time_path);
}
if (strcmp(time_path, time_path_from_TB)) {
fclose(TB_file);
find_not_files();
return;
}
} else {
for (int table_type = MIN_TYPE; table_type <= MAX_TYPE; table_type++)
max_pieces_count[table_type] = str[2+table_type] - '0';
fl = false;
}
} else {
fclose(TB_file);
find_not_files();
return;
}
}
if (it_paths != table_paths.end()) {
fclose(TB_file);
find_not_files();
return;
}
fl = false;
for (int table_type = MIN_TYPE; table_type <= MAX_TYPE && !fl; table_type++) {
unsigned int index = 0;
while (index < NOT_EXIST_TABLES_SIZE && !fl) {
if (fread(¬_exist_tables[table_type][index], sizeof(char), 1, TB_file) != 1)
fl = true;
++index;
}
}
for (int table_type = MIN_TYPE; table_type <= MAX_TYPE && !fl; table_type++) {
fl = (fread(&min_block_size[table_type], sizeof(unsigned long), 1, TB_file) != 1);
}
fclose(TB_file);
if (fl)
find_not_files();
else
known_not_exist = true;
}
void scan_tables() {
explicit_get_max_pieces_count();
if (!known_not_exist)
reset_not_exist_tables(true);
// Rehashing seems to be not effective (because of hidden cache takes more memory than cache of block)
//global_cache.rehash();
}