forked from dyorgio/apfs-clone-checker
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clone_checker.c
244 lines (214 loc) · 6.89 KB
/
clone_checker.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
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
/*
Originally created by Dyorgio Nascimento on 2020-12-10.
Modified for possum by anacrolix starting 2023-01-05, to determine block cloning in values file snapshots.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <string.h>
// declare methods
void printUsage(char* executable);
int compare_blocks(int block_size, char *filenameA, char *filenameB, int fdA, int fdB);
int compare_boundary_blocks(char *filenameA, char *filenameB, int fdA, int fdB);
void check_disk_fs(char *filename, bool is_forced_mode);
struct stat check_file(char *filename, bool is_forced_mode);
// entrypoint
int main(int args_count, char **args) {
bool is_forced_mode = false;
bool is_quick_mode = false;
int opt;
while ( (opt = getopt(args_count, args, "fqv?h")) != -1) {
switch ( opt ) {
case 'f': is_forced_mode = true; break;
case 'q': is_quick_mode = true; break;
case 'v': fprintf(stderr, "APFS Clone Checker - Version: 1.0.0.0\n"); exit(EXIT_SUCCESS); break;
case '?':
case 'h': printUsage(args[0]);
default:
printUsage(args[0]);
}
}
if ( args_count - optind < 2 ) {
printUsage(args[0]);
}
char* filenameA = args[optind];
char* filenameB = args[optind + 1];
check_disk_fs(filenameA, is_forced_mode);
check_disk_fs(filenameB, is_forced_mode);
struct stat statA = check_file(filenameA, is_forced_mode);
struct stat statB = check_file(filenameB, is_forced_mode);
if (statA.st_dev != statB.st_dev || statA.st_size != statB.st_size || statA.st_size < 1
|| statA.st_ino == statB.st_ino) {
// clones only are supported on same device and have same size em blocks count, a file cannot be a clone of itself
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
}
int fdA = open(filenameA, O_RDONLY);
if (fdA < 0 ) {
fprintf(stderr,"%s: Cannot open. %s\n", filenameA, strerror(errno));
if ( is_forced_mode ) {
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
int fdB = open(filenameB, O_RDONLY);
if ( fdB < 0 ) {
fprintf(stderr,"%s: Cannot open. %s\n", filenameB, strerror(errno));
close(fdA);
if ( is_forced_mode ) {
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
int result;
if ( is_quick_mode ) {
result = compare_boundary_blocks(filenameA, filenameB, fdA, fdB);
} else {
result = compare_blocks(statA.st_blksize, filenameA, filenameB, fdA, fdB);
}
close(fdA);
close(fdB);
if ( result != -1 ) {
fprintf(stdout,"%i\n", result);
exit(EXIT_SUCCESS);
} else {
if ( is_forced_mode ) {
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
}
void printUsage(char* executable){
fprintf(stderr, "Usage: %s [-fqv] fileA fileB\n", executable);
exit(EXIT_FAILURE);
}
int compare_blocks(int block_size, char *filenameA, char *filenameB, int fdA, int fdB) {
long sts = 0;
struct log2phys physA;
struct log2phys physB;
for ( off_t offset = 0; sts >= 0; offset += block_size ) {
physA.l2p_devoffset = offset;
// get current blocks physical location
sts = fcntl(fdA, F_LOG2PHYS_EXT, &physA);
if ( sts < 0 && errno == ERANGE ) {
physB.l2p_devoffset = offset;
sts = fcntl(fdB, F_LOG2PHYS_EXT, &physB);
if ( sts < 0 && errno == ERANGE ) {
// both files seeked to the end with same offsets
return true;
} else if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameB, errno, strerror(errno));
return -1;
}
break;
} else if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameA, errno, strerror(errno));
return -1;
}
physB.l2p_devoffset = offset;
sts = fcntl(fdB, F_LOG2PHYS_EXT, &physB);
if ( sts < 0 && errno == ERANGE ) {
// insanity check, size of files already verified before
break;
} else if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameB, errno, strerror(errno));
return -1;
}
fprintf(stderr, "%lld %lld\n", physA.l2p_devoffset, physB.l2p_devoffset);
if ( physA.l2p_devoffset != physB.l2p_devoffset ) {
// found a diff block
break;
}
}
// not a clone (check loop breaked)
return false;
}
int compare_boundary_blocks(char *filenameA, char *filenameB, int fdA, int fdB) {
long sts = 0;
struct log2phys physA;
struct log2phys physB;
// get initial blocks physical location
sts = fcntl(fdA, F_LOG2PHYS, &physA);
if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameA, errno, strerror(errno));
return -1;
}
sts = fcntl(fdB, F_LOG2PHYS, &physB);
if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameB, errno, strerror(errno));
return -1;
}
if ( physA.l2p_devoffset == physB.l2p_devoffset ) {
// Move to end of files
sts = lseek(fdA, -1, SEEK_END);
if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot seek. %ld %s\n", filenameA, sts, strerror(errno));
return -1;
}
sts = lseek(fdB, -1, SEEK_END);
if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot seek. %ld %s\n", filenameB, sts, strerror(errno));
return -1;
}
// get last blocks physical location
sts = fcntl(fdA, F_LOG2PHYS, &physA);
if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameA, errno, strerror(errno));
return -1;
}
sts = fcntl(fdB, F_LOG2PHYS, &physB);
if ( sts < 0 ) {
fprintf(stderr,"%s: Cannot convert logical to physical offset. %i %s\n", filenameB, errno, strerror(errno));
return -1;
}
return physA.l2p_devoffset == physB.l2p_devoffset;
}
return false;
}
void check_disk_fs(char *filename, bool is_forced_mode) {
struct statfs fs;
if( statfs(filename, &fs) == 0 ) {
if( strcmp(fs.f_fstypename, "apfs") != 0) {
fprintf(stderr, "%s: Only APFS is supported: %s\n", filename, fs.f_fstypename);
if ( is_forced_mode ) {
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
}
}
struct stat check_file(char *filename, bool is_forced_mode) {
struct stat st;
if ( stat(filename, &st) < 0 ) {
fprintf(stderr, "%s: No such file\n", filename);
if ( is_forced_mode ) {
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
if ( (st.st_mode & S_IFMT) != S_IFREG ) {
fprintf(stderr, "%s: Not a regular file\n", filename);
if ( is_forced_mode ) {
fprintf(stdout,"0\n");
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
}
return st;
}