-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolkit.cpp
More file actions
261 lines (233 loc) · 5.75 KB
/
toolkit.cpp
File metadata and controls
261 lines (233 loc) · 5.75 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* ************************************************************************ *
* Written by Alex de Kruijff 21 April 2009 *
* ************************************************************************ *
* This source was written with a tabstop every four characters *
* In vi type :set ts=4 *
* ************************************************************************ */
#include "configure.h"
#include "toolkit.h"
#include <fcntl.h>
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#include <new>
static char decimal_point = localeconv()->decimal_point[0];
static struct Buffer
{
size_t size;
char *buf1, *buf2;
Buffer();
~Buffer() throw();
} buffer;
Buffer::Buffer()
{
#ifdef HAVE_MMAP
size = (size_t)sysconf(_SC_PAGESIZE);
#else
size = 2^15;
#endif
buf1 = new char[size << 1]; // throw bad_alloc
buf2 = buf1 + size;
}
Buffer::~Buffer() throw()
{
delete[] buf1;
}
int fcmp(int fd1, int fd2, const struct stat &s1, const struct stat &s2) throw()
{
if (fd1 <= 0) return FILE_OPEN1_ERROR;
if (fd2 <= 0) return FILE_OPEN2_ERROR;
if (s1.st_size != s2.st_size) // diffent sizes means different
return FILE_DIFFERENT;
else if (s1.st_size == 0) // identical files if both file sizes are 0
return FILE_IDENTICAL;
int len = buffer.size;
off_t offset = 0;
int previous;
do
{
#ifdef HAVE_MMAP
void *m1 = mmap (0, buffer.size, PROT_READ, MAP_SHARED, fd1, offset);
void *m2 = mmap (0, buffer.size, PROT_READ, MAP_SHARED, fd2, offset);
if (m1 != MAP_FAILED && m2 != MAP_FAILED)
if(memcmp(m1, m2, buffer.size))
{
munmap(m1, buffer.size);
munmap(m2, buffer.size);
return FILE_DIFFERENT;
}
else
{
munmap(m1, buffer.size);
munmap(m2, buffer.size);
}
else
{
if (m1 == MAP_FAILED)
munmap(m1, buffer.size);
else
munmap(m2, buffer.size);
#endif // HAVE_MMAP
// Read bytes from first files and check if we could.
if ((previous = pread(fd1, buffer.buf1, buffer.size, offset)) < 0)
return FILE_READ1_ERROR;
// Read bytes from the second file and check if we read
// the same amount.
if (previous != pread(fd2, buffer.buf2, buffer.size, offset))
return FILE_READ2_ERROR;
if (memcmp(buffer.buf1, buffer.buf2, len) != 0)
return FILE_DIFFERENT;
#ifdef HAVE_MMAP
}
#endif // HAVE_MMAP
offset += len;
} while(offset < s1.st_size);
return FILE_IDENTICAL;
}
int fcmp(const char *f1, const char *f2,
const struct stat &s1, const struct stat &s2) throw()
{
if (s1.st_size != s2.st_size) // diffent sizes means different
return FILE_DIFFERENT;
else if (s1.st_size == 0) // identical files if both file sizes are 0
return FILE_IDENTICAL;
// open files f1 and f2
int fd1 = open(f1, O_RDONLY | O_SHLOCK | O_DIRECT);
if (fd1 < 0)
return FILE_OPEN1_ERROR;
int fd2 = open(f2, O_RDONLY | O_SHLOCK | O_DIRECT);
if (fd2 < 0)
{
close(fd1);
return FILE_OPEN2_ERROR;
}
// check file input
int status = fcmp(fd1, fd2, s1, s2);
// Files are identical.
close(fd1);
close(fd2);
return status ? status : FILE_IDENTICAL;
}
char *fgetline(char *&str, size_t &size, FILE *file, int eol, char *ptr)
throw (std::bad_alloc)
{
if (&size == 0)
return NULL;
if (ptr == NULL)
ptr = str;
int input = fgetc(file);
if (input == EOF)
return NULL;
size_t n = 0;
char *end = str + size - 1;
#ifdef DEBUG
if (ptr - str >= size)
{
fprintf(stderr, "%s:%d ptr to far\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif // DEBUG
while(input != eol && input != EOF)
{
#ifdef DEBUG
if (ptr - str >= size)
{
fprintf(stderr, "%s:%d ptr to far\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif // DEBUG
*ptr = input;
if (++ptr == end)
{
char *tmp = new char[size << 1]; // throws bad_alloc
memcpy(tmp, str, size);
delete[] str;
ptr = (str = tmp) + size - 1;
end = ptr + size;
size <<= 1;
}
input = fgetc(file);
}
*ptr = 0;
#ifdef DEBUG
if (ptr - str >= size)
{
fprintf(stderr, "%s:%d ptr to far\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
if (strlen(str) >= size)
{
fprintf(stderr, "%s:%d string to large length %u max_size %u\n",
__FILE__, __LINE__, strlen(str), size);
exit(EXIT_FAILURE);
}
#endif // DEBUG
return str;
}
int digits(unsigned long number) throw()
{
int counter = 0;
while(number)
{
++counter;
number /= 10;
}
return counter;
}
void fprintsize(FILE *out, unsigned long size) throw()
{
int i = 0;
while(size > 2048)
{
size >>= 10;
++i;
}
switch(i)
{
case 0: fprintf(out, "%lu Bytes", size); break;
case 1: fprintf(out, "%lu KB", size); break;
case 2: fprintf(out, "%lu MB", size); break;
case 3: fprintf(out, "%lu GB", size); break;
case 4: fprintf(out, "%lu TB", size); break;
default: fprintf(out, "%lu ?B", size);
}
}
static timeval tmpTime;
void fprinttime(FILE *out, struct timeval &after, struct timeval &before,
int humanReadble) throw()
{
tmpTime.tv_sec = after.tv_sec - before.tv_sec;
tmpTime.tv_usec = after.tv_usec - before.tv_usec;
if (tmpTime.tv_usec < 0)
tmpTime.tv_sec--, tmpTime.tv_usec += 1000000;
tmpTime.tv_usec /= 10000;
if (humanReadble)
{
unsigned int days, hours, mins;
days = tmpTime.tv_sec / 86400;
tmpTime.tv_sec %= 86400;
hours = tmpTime.tv_sec / 3600;
tmpTime.tv_sec %= 3600;
mins = tmpTime.tv_sec / 60;
tmpTime.tv_sec %= 60;
fprintf(out, "\t");
if (days)
fprintf(out, "%dd", days);
if (hours)
fprintf(out, "%dh", hours);
if (mins)
fprintf(out, "%dm", mins);
fprintf(out, "%u%c%02lds", tmpTime.tv_sec,
decimal_point, tmpTime.tv_usec);
}
else
fprintf(out, "%9jd%c%02ld", (intmax_t)tmpTime.tv_sec,
decimal_point, tmpTime.tv_usec);
}