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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
crate::ix!();
pub struct FlatFileSeq {
pub dir: PathBuf,
pub prefix: *const u8,
pub chunk_size: usize,
}
impl FlatFileSeq {
pub fn new(
dir: PathBuf,
prefix: *const u8,
chunk_size: usize) -> Result<Self,StdException> {
let mut x = Self {
dir: dir,
prefix: prefix,
chunk_size: chunk_size,
};
if x.chunk_size == 0 {
return Err(invalid_argument("chunk_size must be positive"));
}
Ok(x)
}
pub fn file_name(&self, pos: &FlatFilePos) -> PathBuf {
let mut buf: PathBuf = PathBuf::new();
buf.push(self.dir.clone());
buf.push(format!{"{:p}{:05}.dat", self.prefix, pos.n_file});
buf
}
pub fn open(&mut self,
pos: &FlatFilePos,
read_only: Option<bool>) -> *mut libc::FILE {
let read_only: bool = read_only.unwrap_or(false);
if pos.is_null() {
return std::ptr::null_mut();
}
let path: PathBuf = self.file_name(pos);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let mode = match read_only {
true => "rb",
false => "rb+"
}.as_ptr() as *const i8;
let path_str = path.as_os_str().to_str().unwrap().as_ptr() as *const i8;
let mut file: *mut libc::FILE = {
unsafe {
libc::fopen(path_str,mode)
}
};
if file == null_mut() && !read_only {
unsafe {
file = libc::fopen(path_str,"wb+".as_ptr() as *const i8);
}
}
if file == null_mut() {
log_printf!(
"Unable to open file {}\n",
path_to_string(path)
);
return std::ptr::null_mut();
}
if pos.n_pos != 0
&& unsafe {
libc::fseek(
file,
pos.n_pos.try_into().unwrap(),
libc::SEEK_SET)
} != 0
{
log_printf!(
"Unable to seek to position %u of %s\n",
pos.n_pos,
path_to_string(path)
);
unsafe {
libc::fclose(file);
}
return std::ptr::null_mut();
}
file
}
pub fn allocate(&mut self,
pos: &FlatFilePos,
add_size: usize,
out_of_space: &mut bool) -> usize {
*out_of_space = false;
let n_old_chunks: u32 = {
let n_pos = pos.n_pos;
let chunk_size: u32 = self.chunk_size.try_into().unwrap();
(n_pos + chunk_size - 1) / chunk_size
};
let n_new_chunks: u32 = {
let n_pos = pos.n_pos;
let chunk_size: u32 = self.chunk_size.try_into().unwrap();
let add_size: u32 = add_size.try_into().unwrap();
(n_pos + add_size + chunk_size - 1) / chunk_size
};
if n_new_chunks > n_old_chunks {
let old_size: usize = pos.n_pos.try_into().unwrap();
let new_size: usize = usize::try_from(n_new_chunks).unwrap() * self.chunk_size;
let inc_size: usize = new_size - old_size;
if check_disk_space(
&self.dir,
Some(inc_size.try_into().unwrap())
) {
let file: *mut libc::FILE = self.open(pos, None);
if file != null_mut() {
log_print!(
bc_log::validation,
"Pre-allocating up to position 0x{:x} in {}{:05}.dat\n",
new_size,
prefix,
pos.n_file
);
allocate_file_range(
file,
pos.n_pos,
inc_size.try_into().unwrap()
);
unsafe {
libc::fclose(file);
}
return inc_size;
}
} else {
*out_of_space = true;
}
}
0
}
pub fn flush(&mut self,
pos: &FlatFilePos,
finalize: Option<bool>) -> Result<bool,String> {
let finalize: bool = finalize.unwrap_or(false);
let file: *mut libc::FILE = self.open(&FlatFilePos::new(pos.n_file,0), None);
if file == null_mut() {
let msg = format!{
"flush: failed to open file {}",
pos.n_file
};
return Err(msg);
}
if finalize && !truncate_file(file,pos.n_pos) {
unsafe {
libc::fclose(file);
}
let msg = format!{
"flush: failed to truncate file {}",
pos.n_file
};
return Err(msg);
}
if !file_commit(file) {
unsafe {
libc::fclose(file);
}
let msg = format!{
"flush: failed to commit file {}",
pos.n_file
};
return Err(msg);
}
directory_commit(&self.dir);
unsafe {
libc::fclose(file);
}
Ok(true)
}
}