forked from Infinidat/slimfastq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filer.cpp
303 lines (262 loc) · 10 KB
/
filer.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/***********************************************************************************************************************/
/* This program was written by Josef Ezra <[email protected]> */
/* Copyright (c) 2013, Infinidat */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, are permitted provided that */
/* the following conditions are met: */
/* */
/* Redistributions of source code must retain the above copyright notice, this list of conditions and the following */
/* disclaimer. */
/* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following */
/* disclaimer in the documentation and/or other materials provided with the distribution. */
/* Neither the name of the Infinidat nor the names of its contributors may be used to endorse or promote products */
/* derived from this software without specific prior written permission. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR */
/* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */
/* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE */
/* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/***********************************************************************************************************************/
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "filer.hpp"
static void croak(const char* fmt, long long num) __attribute__ ((noreturn, cold));
static void croak(const char* fmt, long long num) {
fprintf(stderr, fmt, num, errno ? strerror(errno) : "");
fprintf(stderr, "\n");
exit(1);
}
struct OneFile {
struct file_attr_t {
UINT64 name;
UINT64 size ; // in bytes
UINT32 first; // page index
UINT32 node ; // page index
} PACKED;
enum {
max_root_files = FILER_PAGE / sizeof(file_attr_t),
max_node_files = FILER_PAGE / 4,
};
file_attr_t files[max_root_files+1]; // pad to fit one page
UINT64 next_findex; // offset in files table
UINT64 num_pages; // free alloc index
// UINT32 files_index; - currently limitted to single root dir (341 files, including zero).
FILE* m_out;
FILE* m_in ;
UINT32 get_findex() {
assert(m_out);
rarely_if(next_findex >= max_root_files)
croak("Internal error: Too many open files: %d %s", next_findex);
return next_findex ++;
}
UINT32 get_findex(UINT64 name) {
assert(m_in);
for(UINT32 i = 1; i < next_findex; i++)
if (files[i].name == name)
return i;
return 0;
}
UINT32 allocate() {
return num_pages ++;
}
void read_page(UINT32 offset, UCHAR* page) {
// Note: -D_FILE_OFFSET_BITS=64 is required
UINT64 offs = offset;
offs *= FILER_PAGE;
fseek(m_in, offs , SEEK_SET);
UINT32 cnt = fread(page, 1, FILER_PAGE, m_in);
if (cnt != FILER_PAGE)
croak("Failed reading page index %d: %s", offset);
}
void write_page(UINT32 offset, UCHAR* page) {
UINT64 offs = offset;
offs *= FILER_PAGE;
fseek(m_out, offs, SEEK_SET);
UINT32 cnt = fwrite(page, 1, FILER_PAGE, m_out);
if (cnt != FILER_PAGE)
croak("Failed writing page index %d: %s", offset);
}
OneFile() {
m_out = m_in = NULL;
}
void init_read(FILE* in) {
next_findex = 1;
m_in = in;
m_out = NULL;
read_page(1, (UCHAR*)files);
next_findex = files[0].first;
files[0].first = 0;
}
void do_confess() const {
// i: 'name' : size : first : node
fprintf(stderr, " i: name : NODES count : 1st : 2nd\n");
for (UINT32 i = 0;
i < next_findex;
i++) {
char name[10];
strncpy(name, (char*)&files[i].name, 8);
fprintf(stderr, "%2d: %-10s: %-15lld: %d:\t: %d\n",
i, (i? name : "<info>"), files[i].size, files[i].first, files[i].node);
}
}
void init_write(FILE* out) {
next_findex = 1;
m_in = NULL;
m_out = out;
files[0].name = 0;
files[0].first = 0;
num_pages = 2;
}
void finit_write() {
if (m_out) {
files[0].first = next_findex;
write_page(1, (UCHAR*)files);
fclose(m_out);
m_out = NULL;
}
}
UINT64 finit_size() {
return num_pages * FILER_PAGE;
}
~OneFile() { finit_write() ; } // call explicitly from config?
} onef ;
// Static
void FilerSave::init(FILE* out) { onef.init_write(out); }
void FilerSave::finit() { onef.finit_write() ; }
void FilerLoad::init(FILE* in) { onef.init_read(in) ; }
void FilerLoad::confess() { onef.do_confess() ; }
UINT64 FilerSave::finit_size() { return onef.finit_size(); }
// Base
static UINT64 name2u(const char* name) {
UINT64 uname ;
assert(strlen(name) <=8 );
strncpy((char*)&uname, name, 8);
return uname;
}
FilerBase::FilerBase() {
m_node_i = 0;
m_node_p = 0;
m_valid = true;
m_cur = m_count = 0;
m_page_count = 0;
}
size_t FilerBase::tell() const {
return
( m_page_count ) ?
((m_page_count-1) * FILER_PAGE) +
( m_cur ) :
( m_cur ) ;
}
size_t FilerSave::tell() const {
return FilerBase::tell();
}
size_t FilerLoad::tell() const {
return FilerBase::tell();
}
// Save
bool FilerSave::is_valid() const { return m_valid; }
FilerSave::FilerSave(const char* name) {
UINT32 fi = m_onef_i = onef.get_findex();
onef.files[fi].name = name2u(name);
onef.files[fi].size = 0;
onef.files[fi].node = 0;
onef.files[fi].first = onef.allocate();
}
FilerSave::FilerSave(int forty_two) {
assert(forty_two == 42); // Verify this constructor wasn't called by mistake
BZERO(onef.files[0]);
m_onef_i = 0;
}
FilerSave::~FilerSave() {
save_page(true);
m_valid = false;
if (m_node_p)
save_node(0);
}
void FilerSave::save_node(UINT32 next_node) {
assert(m_node_i <= maxi_nodes );
assert(m_node_p);
m_node[m_node_i ] = next_node;
onef.write_page(m_node_p, (UCHAR*) m_node);
m_node_p = next_node;
m_node_i = 0;
}
void FilerSave::save_page(bool finit) {
if (not m_valid or
not m_cur)
return ;
assert(m_cur <= FILER_PAGE);
rarely_if (not m_node_p) {
assert(m_node_i == 0);
onef.write_page(onef.files[m_onef_i].first, m_buff);
if (not finit) // (m_cur == FILER_PAGE) // Don't allocate at EOF (harmless in the rare case of exact one page file)
onef.files[m_onef_i].node = m_node_p = onef.allocate();
}
else {
onef.write_page(m_node[ m_node_i ++ ], m_buff);
rarely_if (m_node_i == maxi_nodes and not finit)
save_node(onef.allocate());
}
if (not finit)
m_node[ m_node_i ] = onef.allocate();
onef.files[m_onef_i].size += m_cur;
m_cur = 0;
m_page_count++;
}
// Load
FilerLoad::FilerLoad(const char* name, bool* valid_ptr) {
m_onef_i = onef.get_findex(name2u(name));
m_valid_ptr = valid_ptr;
if (not m_onef_i) {
*valid_ptr = m_valid = false;
return;
}
* valid_ptr = m_valid = true ;
load_page();
}
FilerLoad::FilerLoad(int forty_two, bool* valid_ptr) {
assert(forty_two == 42); // Verify this constructor wasn't called by mistake
m_onef_i = 0;
m_valid_ptr = valid_ptr;
* valid_ptr = m_valid = true ;
load_page();
}
FilerLoad::~FilerLoad() {
if ( m_valid_ptr )
*m_valid_ptr = false;
}
bool FilerLoad::is_valid() const { return m_valid ; }
void FilerLoad::load_page() {
rarely_if(not m_valid)
return;
rarely_if (tell() >= onef.files[m_onef_i].size ) {
*m_valid_ptr = m_valid = false;
return; // EOF
}
rarely_if (m_node_p == 0) {
assert(m_node_i == 0);
onef.read_page(onef.files[m_onef_i].first, m_buff);
m_node_p = onef.files[m_onef_i].node;
if (m_node_p)
onef.read_page(m_node_p, (UCHAR*) m_node);
m_node_i = 0;
}
else {
rarely_if (m_node_i == maxi_nodes) { // load node page
m_node_p = m_node[ maxi_nodes ]; // keep it for debugging
onef.read_page(m_node_p, (UCHAR*) m_node);
m_node_i = 0;
}
onef.read_page(m_node[m_node_i ++], m_buff);
}
m_cur = 0;
UINT64 size = onef.files[m_onef_i].size;
m_count = size/FILER_PAGE == m_page_count ? size % FILER_PAGE : FILER_PAGE ;
m_page_count++;
}