-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvfile_handler.c
More file actions
242 lines (211 loc) · 8.48 KB
/
Copy pathvfile_handler.c
File metadata and controls
242 lines (211 loc) · 8.48 KB
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
#include "vfile_handler.h"
#include "fs_helper.h"
#include <stdint.h>
#include "fcntl.h"
#include "stdint.h"
#include "stdlib.h"
#include "unistd.h"
#include "assert.h"
#include "errno.h"
#include <sys/stat.h>
#include <time.h>
int store_blocks(size_t write_size, off_t offset, int fd_disk, int fd_file, int fd_vt, int fd_vf)
{
struct stat st;
if (fstat(fd_file, &st) == -1)
return -errno;
off_t file_size = st.st_size;
uint64_t affected_interval_left = offset / BLOCK_SIZE;
uint64_t affected_interval_right = (file_size < (offset + write_size))
? ((file_size + BLOCK_SIZE - 1) / BLOCK_SIZE)
: (((offset + write_size) + BLOCK_SIZE - 1) / BLOCK_SIZE);
uint64_t version;
off_t disk_pos;
uint64_t vf_offset;
if (affected_interval_right - affected_interval_left != 0)
{
if (pread(fd_vt, &version, sizeof(version), 0) == -1)
return -errno;
disk_pos = lseek(fd_disk, 0, SEEK_END);
if (disk_pos == -1)
return -errno;
if (pread(fd_vt, &vf_offset, sizeof(vf_offset), version * sizeof(uint64_t)) == -1)
return -errno;
vf_offset += 2 * sizeof(uint64_t); // skip version number(ts) and version size
char buffer[BLOCK_SIZE];
for (uint64_t i = affected_interval_left; i < affected_interval_right; i++)
{
uint64_t value;
if (pread(fd_vf, &value, sizeof(value), vf_offset + i * sizeof(uint64_t)) == -1)
return -errno;
if (IS_VERSION(value))
{
if (pread(fd_file, buffer, BLOCK_SIZE, i * BLOCK_SIZE) == -1)
return -errno;
if (write(fd_disk, buffer, BLOCK_SIZE) != BLOCK_SIZE)
return -errno;
uint64_t disk_block_id = disk_pos / BLOCK_SIZE;
disk_pos += BLOCK_SIZE;
if (pwrite(fd_vf, &disk_block_id, sizeof(disk_block_id), vf_offset + (i * sizeof(uint64_t))) == -1)
return -errno;
}
}
}
return 0;
}
// traverse previous versions untill pointer to disk found
uint64_t check_version(int fd_disk, int fd_file, int fd_vt, int fd_vf, uint64_t version, uint64_t version_max, uint64_t block_number)
{
if (version > version_max)
return -1;
uint64_t vf_off = 0;
if (pread(fd_vt, &vf_off, sizeof(vf_off), version * sizeof(uint64_t)) == -1)
return errno;
uint64_t value = 0;
if (pread(fd_vf, &value, sizeof(value), vf_off + 2 * sizeof(uint64_t) + block_number * sizeof(uint64_t)) == -1)
return errno;
if (!IS_VERSION(value))
return value;
uint64_t res = check_version(fd_disk, fd_file, fd_vt, fd_vf, GET_VALUE(value), version_max, block_number);
uint64_t latest_version_ref = MAKE_VERSION(version_max + 1);
if (pwrite(fd_vf, &latest_version_ref, sizeof(latest_version_ref), vf_off + 2 * sizeof(uint64_t) + block_number * sizeof(uint64_t)) == -1)
return errno;
// if (pwrite(fd_vf, &res, sizeof(res), vf_off + 2 * sizeof(uint64_t) + block_number * sizeof(uint64_t)) == -1)
// return errno;
// if (pwrite(myfh->fd_vf, &res, sizeof(value), vf_off + 2 * sizeof(uint64_t) + block_number * sizeof(uint64_t)) == -1)
// return errno;
return res;
}
int read_version(size_t read_size, off_t offset, char *buf, uint64_t version, int fd_disk, int fd_file, int fd_vt, int fd_vf, uint64_t *bytes_read)
{
uint64_t vf_offset = 0;
if (pread(fd_vt, &vf_offset, sizeof(vf_offset), version * sizeof(uint64_t)) == -1)
return -errno;
uint64_t version_max = 0;
if (pread(fd_vt, &version_max, sizeof(uint64_t), 0) == -1)
return -errno;
uint64_t file_size = 0;
if (pread(fd_vf, &file_size, sizeof(file_size), vf_offset + sizeof(uint64_t)) == -1)
return -errno;
uint64_t affected_interval_left = offset / BLOCK_SIZE;
uint64_t affected_interval_right = (file_size < (offset + read_size))
? ((file_size + BLOCK_SIZE - 1) / BLOCK_SIZE)
: (((offset + read_size) + BLOCK_SIZE - 1) / BLOCK_SIZE);
vf_offset += 2 * sizeof(uint64_t);
*bytes_read = 0;
for (uint64_t i = affected_interval_left; i < affected_interval_right; i++)
{
uint64_t to_read = BLOCK_SIZE;
int is_one = 0;
int is_first = 0;
int is_last = 0;
if ((affected_interval_right - affected_interval_left) == 1)
{
to_read = file_size;
is_one = 1;
}
else if (i == affected_interval_left)
{
to_read = BLOCK_SIZE - (offset % BLOCK_SIZE);
is_first = 1;
}
else if (i == affected_interval_right - 1)
{
to_read = (offset + file_size) % BLOCK_SIZE;
if (to_read == 0)
to_read = BLOCK_SIZE;
is_last = 1;
}
uint64_t value;
if (pread(fd_vf, &value, sizeof(value), vf_offset + (i * sizeof(uint64_t))) == -1)
return -errno;
if (!IS_VERSION(value))
{
if (is_one)
{
if (pread(fd_disk, buf + *bytes_read, to_read, (value * BLOCK_SIZE) + (offset % BLOCK_SIZE)) == -1)
return -errno;
*bytes_read += to_read;
}
else if (is_first)
{
if (pread(fd_disk, buf + *bytes_read, to_read, (value * BLOCK_SIZE) + (offset % BLOCK_SIZE)) == -1)
return -errno;
*bytes_read += to_read;
}
else if (is_last)
{
if (pread(fd_disk, buf + *bytes_read, to_read, value * BLOCK_SIZE) == -1)
return -errno;
*bytes_read += to_read;
}
else
{
if (pread(fd_disk, buf + *bytes_read, BLOCK_SIZE, value * BLOCK_SIZE) == -1)
return -errno;
*bytes_read += BLOCK_SIZE;
}
}
else
{
uint64_t relevant_block = check_version(fd_disk, fd_file, fd_vt, fd_vf, GET_VALUE(value), version_max, i);
// if (relevant_block == -1)
// {
// if (is_one)
// {
// if (pread(fd_file, buf + *bytes_read, to_read, (i * BLOCK_SIZE) + (offset % BLOCK_SIZE)) == -1)
// return -errno;
// *bytes_read += BLOCK_SIZE;
// }
// else if (is_first)
// {
// if (pread(fd_file, buf + *bytes_read, to_read, (i * BLOCK_SIZE) + (offset % BLOCK_SIZE)) == -1)
// return -errno;
// *bytes_read += BLOCK_SIZE;
// }
// else if (is_last)
// {
// if (pread(fd_file, buf + *bytes_read, to_read, i * BLOCK_SIZE) == -1)
// return -errno;
// *bytes_read += BLOCK_SIZE;
// }
// else
// {
// if (pread(fd_file, buf + *bytes_read, BLOCK_SIZE, i * BLOCK_SIZE) == -1)
// return -errno;
// *bytes_read += BLOCK_SIZE;
// }
// }
// else
// {
if (is_one)
{
if (pread(fd_disk, buf + *bytes_read, to_read, (relevant_block * BLOCK_SIZE) + (offset % BLOCK_SIZE)) == -1)
return -errno;
*bytes_read += BLOCK_SIZE;
}
else if (is_first)
{
if (pread(fd_file, buf + *bytes_read, to_read, (relevant_block * BLOCK_SIZE) + (offset % BLOCK_SIZE)) == -1)
return -errno;
*bytes_read += BLOCK_SIZE;
}
else if (is_last)
{
if (pread(fd_file, buf + *bytes_read, to_read, relevant_block * BLOCK_SIZE) == -1)
return -errno;
*bytes_read += BLOCK_SIZE;
}
else
{
if (pread(fd_disk, buf + *bytes_read, BLOCK_SIZE, relevant_block * BLOCK_SIZE) == -1)
return -errno;
*bytes_read += BLOCK_SIZE;
}
//}
// if(pwrite(myfh->fd_vf, &relevant_block, sizeof(relevant_block), i) == -1)
// return errno;
}
}
return *bytes_read;
}