-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheve4ceph-mdti.c
210 lines (173 loc) · 5.25 KB
/
eve4ceph-mdti.c
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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 1023
void main(int argc, char **argv) {
FILE *fdiff;
FILE *fimg;
if (argc == 2 && strcmp(argv[1], "version") == 0) {
//&& argv[1] == 'version'
// printf(argv[1]);
printf("0.0.1\n");
exit(0);
}
if (argc < 3) {
printf(" ______ __ _ _ "
"________\n");
printf(" / ____/___ / /____ _________ _____(_)_______ | | / / "
"____/\n");
printf(" / __/ / __ \\/ __/ _ \\/ ___/ __ \\/ ___/ / ___/ _ \\ | | / / "
"__/\n");
printf(
" / /___/ / / / /_/ __/ / / /_/ / / / (__ ) __/ | |/ / /___\n");
printf("/_____/_/ /_/\\__/\\___/_/ / .___/_/ /_/____/\\___/ "
"|___/_____/\n");
printf(" /_/\n\n");
printf("EnterpriseVE Merge Diff file To Image for Ceph (Made in "
"Italy)\n\n");
printf("Usage:\n");
printf(" eve4ceph-mdti version\n");
printf(" eve4ceph-mdti <FULL_IMAGE_FILE> <DIFF_FILE>\n\n");
printf("Report bugs to <[email protected]>.\n");
exit(0);
}
// open image
char *img = argv[1];
fimg = fopen(img, "r+");
if (fimg == NULL) {
printf("Error opening image file %s\n", img);
exit(1);
}
// opem diff file
char *diff = argv[2];
fdiff = fopen(diff, "r");
if (fdiff == NULL) {
printf("Error opening diff file %s\n", diff);
exit(1);
}
// check header diff file
char header[255];
if (fread(header, 1, 12, fdiff) != 12) {
printf("Error reading diff header\n");
exit(1);
}
if (strncmp(header, "rbd diff v1", 11)) {
printf("Unrecognised diff format\n");
exit(1);
}
int debug = 0;
// reading metadata
guint8 tag;
int reading_metadata = 1;
printf("Reading metadata\n");
while (reading_metadata) {
if (fread(&tag, 1, 1, fdiff) != 1) {
printf("Error reading metadata\n");
exit(1);
}
guint32 name_len;
// int rlen;
char name[MAX_LEN + 1];
guint64 image_size;
switch (tag) {
case 'f': // from snap
case 't': // to snaps
fread(&name_len, 1, sizeof(guint32), fdiff);
int rlen = name_len > MAX_LEN ? MAX_LEN : name_len;
fread(name, 1, rlen, fdiff);
name[rlen] = 0;
printf("%s snap: %s\n", (tag == 'f' ? "From" : "To"), name);
if (name_len > MAX_LEN) {
fseek(fdiff, name_len - MAX_LEN, SEEK_CUR);
}
break;
case 's': // size
fread(&image_size, 1, sizeof(guint64), fdiff);
printf("Image size: %lu (%luGB)\n", image_size,
image_size / (1024 * 1024 * 1024));
break;
default:
reading_metadata = 0;
printf("End of metadata\n");
fseek(fdiff, -1, SEEK_CUR);
break;
}
}
char *rbuf = (char *)malloc(4096);
char *zbuf = (char *)malloc(4096);
memset((void *)zbuf, 0, 4096);
int total_length = 0;
// reading data
int reading_data = 1;
while (reading_data) {
if (fread(&tag, 1, 1, fdiff) != 1) {
printf("Error reading data\n");
exit(1);
}
guint64 offset;
guint64 length;
switch (tag) {
case 'w': // Update data
case 'z': // Zero data
fread(&offset, 1, sizeof(guint64), fdiff); // offset
fread(&length, 1, sizeof(guint64), fdiff); // length
total_length += length;
if (debug) {
printf("Data at offset %lu of length %lu\n", offset, length);
}
int written = 0;
while (written < length) {
int l = (length - written > 4096) ? 4096 : length - written;
if (tag == 'w') {
if (fread(rbuf, 1, l, fdiff) != l) {
printf("error reading diff file\n");
exit(0);
}
}
if (fseek(fimg, offset + written, SEEK_SET)) {
printf("error seeking in image file\n");
exit(0);
}
if (debug) {
printf("writing %d bytes to image\n", l);
}
int n = 0;
if (tag == 'w') {
n = fwrite(rbuf, 1, l, fimg);
} else if (tag == 'z') {
n = fwrite(zbuf, 1, l, fimg);
}
written += n;
if (n == 0) {
printf("Error writing to image\n");
exit(0);
}
}
break;
case 'e': // End data
reading_data = 0;
printf("End of data\n");
printf("Writing %d bytes to image\n", total_length);
break;
default:
reading_data = 0;
printf("Unknown data encountered %d\n", tag);
break;
}
}
fclose(fimg);
fclose(fdiff);
}