]> git.saurik.com Git - apt.git/blob - methods/rred.cc
fix argument check for the rred method
[apt.git] / methods / rred.cc
1 // Includes /*{{{*/
2 #include <apt-pkg/fileutl.h>
3 #include <apt-pkg/mmap.h>
4 #include <apt-pkg/error.h>
5 #include <apt-pkg/acquire-method.h>
6 #include <apt-pkg/strutl.h>
7 #include <apt-pkg/hashes.h>
8
9 #include <sys/stat.h>
10 #include <sys/uio.h>
11 #include <unistd.h>
12 #include <utime.h>
13 #include <stdio.h>
14 #include <errno.h>
15 #include <apti18n.h>
16 /*}}}*/
17 /** \brief RredMethod - ed-style incremential patch method {{{
18 *
19 * This method implements a patch functionality similar to "patch --ed" that is
20 * used by the "tiffany" incremental packages download stuff. It differs from
21 * "ed" insofar that it is way more restricted (and therefore secure).
22 * The currently supported ed commands are "<em>c</em>hange", "<em>a</em>dd" and
23 * "<em>d</em>elete" (diff doesn't output any other).
24 * Additionally the records must be reverse sorted by line number and
25 * may not overlap (diff *seems* to produce this kind of output).
26 * */
27 class RredMethod : public pkgAcqMethod {
28 bool Debug;
29 // the size of this doesn't really matter (except for performance)
30 const static int BUF_SIZE = 1024;
31 // the supported ed commands
32 enum Mode {MODE_CHANGED='c', MODE_DELETED='d', MODE_ADDED='a'};
33 // return values
34 enum State {ED_OK, ED_ORDERING, ED_PARSER, ED_FAILURE, MMAP_FAILED};
35
36 State applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file,
37 unsigned long &line, char *buffer, Hashes *hash) const;
38 void ignoreLineInFile(FILE *fin, char *buffer) const;
39 void copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines,
40 Hashes *hash, char *buffer) const;
41
42 State patchFile(FileFd &Patch, FileFd &From, FileFd &out_file, Hashes *hash) const;
43 State patchMMap(FileFd &Patch, FileFd &From, FileFd &out_file, Hashes *hash) const;
44
45 protected:
46 // the methods main method
47 virtual bool Fetch(FetchItem *Itm);
48
49 public:
50 RredMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
51 };
52 /*}}}*/
53 /** \brief applyFile - in reverse order with a tail recursion {{{
54 *
55 * As it is expected that the commands are in reversed order in the patch file
56 * we check in the first half if the command is valid, but doesn't execute it
57 * and move a step deeper. After reaching the end of the file we apply the
58 * patches in the correct order: last found command first.
59 *
60 * \param ed_cmds patch file to apply
61 * \param in_file base file we want to patch
62 * \param out_file file to write the patched result to
63 * \param line of command operation
64 * \param buffer internal used read/write buffer
65 * \param hash the created file for correctness
66 * \return the success State of the ed command executor
67 */
68 RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file,
69 unsigned long &line, char *buffer, Hashes *hash) const {
70 // get the current command and parse it
71 if (fgets(buffer, BUF_SIZE, ed_cmds) == NULL) {
72 if (Debug == true)
73 std::clog << "rred: encounter end of file - we can start patching now." << std::endl;
74 line = 0;
75 return ED_OK;
76 }
77
78 // parse in the effected linenumbers
79 char* idx;
80 errno=0;
81 unsigned long const startline = strtol(buffer, &idx, 10);
82 if (errno == ERANGE || errno == EINVAL) {
83 _error->Errno("rred", "startline is an invalid number");
84 return ED_PARSER;
85 }
86 if (startline > line) {
87 _error->Error("rred: The start line (%lu) of the next command is higher than the last line (%lu). This is not allowed.", startline, line);
88 return ED_ORDERING;
89 }
90 unsigned long stopline;
91 if (*idx == ',') {
92 idx++;
93 errno=0;
94 stopline = strtol(idx, &idx, 10);
95 if (errno == ERANGE || errno == EINVAL) {
96 _error->Errno("rred", "stopline is an invalid number");
97 return ED_PARSER;
98 }
99 }
100 else {
101 stopline = startline;
102 }
103 line = startline;
104
105 // which command to execute on this line(s)?
106 switch (*idx) {
107 case MODE_CHANGED:
108 if (Debug == true)
109 std::clog << "Change from line " << startline << " to " << stopline << std::endl;
110 break;
111 case MODE_ADDED:
112 if (Debug == true)
113 std::clog << "Insert after line " << startline << std::endl;
114 break;
115 case MODE_DELETED:
116 if (Debug == true)
117 std::clog << "Delete from line " << startline << " to " << stopline << std::endl;
118 break;
119 default:
120 _error->Error("rred: Unknown ed command '%c'. Abort.", *idx);
121 return ED_PARSER;
122 }
123 unsigned char mode = *idx;
124
125 // save the current position
126 unsigned const long pos = ftell(ed_cmds);
127
128 // if this is add or change then go to the next full stop
129 unsigned int data_length = 0;
130 if (mode == MODE_CHANGED || mode == MODE_ADDED) {
131 do {
132 ignoreLineInFile(ed_cmds, buffer);
133 data_length++;
134 }
135 while (strncmp(buffer, ".", 1) != 0);
136 data_length--; // the dot should not be copied
137 }
138
139 // do the recursive call - the last command is the one we need to execute at first
140 const State child = applyFile(ed_cmds, in_file, out_file, line, buffer, hash);
141 if (child != ED_OK) {
142 return child;
143 }
144
145 // change and delete are working on "line" - add is done after "line"
146 if (mode != MODE_ADDED)
147 line++;
148
149 // first wind to the current position and copy over all unchanged lines
150 if (line < startline) {
151 copyLinesFromFileToFile(in_file, out_file, (startline - line), hash, buffer);
152 line = startline;
153 }
154
155 if (mode != MODE_ADDED)
156 line--;
157
158 // include data from ed script
159 if (mode == MODE_CHANGED || mode == MODE_ADDED) {
160 fseek(ed_cmds, pos, SEEK_SET);
161 copyLinesFromFileToFile(ed_cmds, out_file, data_length, hash, buffer);
162 }
163
164 // ignore the corresponding number of lines from input
165 if (mode == MODE_CHANGED || mode == MODE_DELETED) {
166 while (line < stopline) {
167 ignoreLineInFile(in_file, buffer);
168 line++;
169 }
170 }
171 return ED_OK;
172 }
173 /*}}}*/
174 void RredMethod::copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines,/*{{{*/
175 Hashes *hash, char *buffer) const {
176 while (0 < lines--) {
177 do {
178 fgets(buffer, BUF_SIZE, fin);
179 size_t const written = fwrite(buffer, 1, strlen(buffer), fout);
180 hash->Add((unsigned char*)buffer, written);
181 } while (strlen(buffer) == (BUF_SIZE - 1) &&
182 buffer[BUF_SIZE - 2] != '\n');
183 }
184 }
185 /*}}}*/
186 void RredMethod::ignoreLineInFile(FILE *fin, char *buffer) const { /*{{{*/
187 fgets(buffer, BUF_SIZE, fin);
188 while (strlen(buffer) == (BUF_SIZE - 1) &&
189 buffer[BUF_SIZE - 2] != '\n') {
190 fgets(buffer, BUF_SIZE, fin);
191 buffer[0] = ' ';
192 }
193 }
194 /*}}}*/
195 RredMethod::State RredMethod::patchFile(FileFd &Patch, FileFd &From, /*{{{*/
196 FileFd &out_file, Hashes *hash) const {
197 char buffer[BUF_SIZE];
198 FILE* fFrom = fdopen(From.Fd(), "r");
199 FILE* fPatch = fdopen(Patch.Fd(), "r");
200 FILE* fTo = fdopen(out_file.Fd(), "w");
201
202 /* we do a tail recursion to read the commands in the right order */
203 unsigned long line = -1; // assign highest possible value
204 State const result = applyFile(fPatch, fFrom, fTo, line, buffer, hash);
205
206 /* read the rest from infile */
207 if (result == ED_OK) {
208 while (fgets(buffer, BUF_SIZE, fFrom) != NULL) {
209 size_t const written = fwrite(buffer, 1, strlen(buffer), fTo);
210 hash->Add((unsigned char*)buffer, written);
211 }
212 fflush(fTo);
213 }
214 return result;
215 }
216 /*}}}*/
217 struct EdCommand { /*{{{*/
218 size_t data_start;
219 size_t data_end;
220 size_t data_lines;
221 size_t first_line;
222 size_t last_line;
223 char type;
224 };
225 #define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */
226 /*}}}*/
227 RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/
228 FileFd &out_file, Hashes *hash) const {
229 #ifdef _POSIX_MAPPED_FILES
230 MMap ed_cmds(Patch, MMap::ReadOnly);
231 MMap in_file(From, MMap::ReadOnly);
232 FILE* fTo = fdopen(out_file.Fd(), "w");
233
234 if (ed_cmds.Size() == 0 || in_file.Size() == 0)
235 return MMAP_FAILED;
236
237 EdCommand* commands = 0;
238 size_t command_count = 0;
239 size_t command_alloc = 0;
240
241 const char* begin = (char*) ed_cmds.Data();
242 const char* end = begin;
243 const char* ed_end = (char*) ed_cmds.Data() + ed_cmds.Size();
244
245 const char* input = (char*) in_file.Data();
246 const char* input_end = (char*) in_file.Data() + in_file.Size();
247
248 size_t i;
249
250 /* 1. Parse entire script. It is executed in reverse order, so we cather it
251 * in the `commands' buffer first
252 */
253
254 for(;;) {
255 EdCommand cmd;
256 cmd.data_start = 0;
257 cmd.data_end = 0;
258
259 while(begin != ed_end && *begin == '\n')
260 ++begin;
261 while(end != ed_end && *end != '\n')
262 ++end;
263 if(end == ed_end && begin == end)
264 break;
265
266 /* Determine command range */
267 const char* tmp = begin;
268
269 for(;;) {
270 /* atoll is safe despite lacking NUL-termination; we know there's an
271 * alphabetic character at end[-1]
272 */
273 if(tmp == end) {
274 cmd.first_line = atol(begin);
275 cmd.last_line = cmd.first_line;
276 break;
277 }
278 if(*tmp == ',') {
279 cmd.first_line = atol(begin);
280 cmd.last_line = atol(tmp + 1);
281 break;
282 }
283 ++tmp;
284 }
285
286 // which command to execute on this line(s)?
287 switch (end[-1]) {
288 case MODE_CHANGED:
289 if (Debug == true)
290 std::clog << "Change from line " << cmd.first_line << " to " << cmd.last_line << std::endl;
291 break;
292 case MODE_ADDED:
293 if (Debug == true)
294 std::clog << "Insert after line " << cmd.first_line << std::endl;
295 break;
296 case MODE_DELETED:
297 if (Debug == true)
298 std::clog << "Delete from line " << cmd.first_line << " to " << cmd.last_line << std::endl;
299 break;
300 default:
301 _error->Error("rred: Unknown ed command '%c'. Abort.", end[-1]);
302 free(commands);
303 return ED_PARSER;
304 }
305 cmd.type = end[-1];
306
307 /* Determine the size of the inserted text, so we don't have to scan this
308 * text again later.
309 */
310 begin = end + 1;
311 end = begin;
312 cmd.data_lines = 0;
313
314 if(cmd.type == MODE_ADDED || cmd.type == MODE_CHANGED) {
315 cmd.data_start = begin - (char*) ed_cmds.Data();
316 while(end != ed_end) {
317 if(*end == '\n') {
318 if(end[-1] == '.' && end[-2] == '\n')
319 break;
320 ++cmd.data_lines;
321 }
322 ++end;
323 }
324 cmd.data_end = end - (char*) ed_cmds.Data() - 1;
325 begin = end + 1;
326 end = begin;
327 }
328 if(command_count == command_alloc) {
329 command_alloc = (command_alloc + 64) * 3 / 2;
330 commands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand));
331 }
332 commands[command_count++] = cmd;
333 }
334
335 struct iovec* iov = new struct iovec[IOV_COUNT];
336 size_t iov_size = 0;
337
338 size_t amount, remaining;
339 size_t line = 1;
340 EdCommand* cmd;
341
342 /* 2. Execute script. We gather writes in a `struct iov' array, and flush
343 * using writev to minimize the number of system calls. Data is read
344 * directly from the memory mappings of the input file and the script.
345 */
346
347 for(i = command_count; i-- > 0; ) {
348 cmd = &commands[i];
349 if(cmd->type == MODE_ADDED)
350 amount = cmd->first_line + 1;
351 else
352 amount = cmd->first_line;
353
354 if(line < amount) {
355 begin = input;
356 while(line != amount) {
357 input = (const char*) memchr(input, '\n', input_end - input);
358 if(!input)
359 break;
360 ++line;
361 ++input;
362 }
363
364 iov[iov_size].iov_base = (void*) begin;
365 iov[iov_size].iov_len = input - begin;
366 hash->Add((const unsigned char*) begin, input - begin);
367
368 if(++iov_size == IOV_COUNT) {
369 writev(out_file.Fd(), iov, IOV_COUNT);
370 iov_size = 0;
371 }
372 }
373
374 if(cmd->type == MODE_DELETED || cmd->type == MODE_CHANGED) {
375 remaining = (cmd->last_line - cmd->first_line) + 1;
376 line += remaining;
377 while(remaining) {
378 input = (const char*) memchr(input, '\n', input_end - input);
379 if(!input)
380 break;
381 --remaining;
382 ++input;
383 }
384 }
385
386 if(cmd->type == MODE_CHANGED || cmd->type == MODE_ADDED) {
387 if(cmd->data_end != cmd->data_start) {
388 iov[iov_size].iov_base = (void*) ((char*)ed_cmds.Data() + cmd->data_start);
389 iov[iov_size].iov_len = cmd->data_end - cmd->data_start;
390 hash->Add((const unsigned char*) ((char*)ed_cmds.Data() + cmd->data_start),
391 iov[iov_size].iov_len);
392
393 if(++iov_size == IOV_COUNT) {
394 writev(out_file.Fd(), iov, IOV_COUNT);
395 iov_size = 0;
396 }
397 }
398 }
399 }
400
401 if(input != input_end) {
402 iov[iov_size].iov_base = (void*) input;
403 iov[iov_size].iov_len = input_end - input;
404 hash->Add((const unsigned char*) input, input_end - input);
405 ++iov_size;
406 }
407
408 if(iov_size) {
409 writev(out_file.Fd(), iov, iov_size);
410 iov_size = 0;
411 }
412
413 for(i = 0; i < iov_size; i += IOV_COUNT) {
414 if(iov_size - i < IOV_COUNT)
415 writev(out_file.Fd(), iov + i, iov_size - i);
416 else
417 writev(out_file.Fd(), iov + i, IOV_COUNT);
418 }
419
420 delete [] iov;
421 free(commands);
422
423 fflush(fTo);
424
425 return ED_OK;
426 #else
427 return MMAP_FAILED;
428 #endif
429 }
430 /*}}}*/
431 bool RredMethod::Fetch(FetchItem *Itm) /*{{{*/
432 {
433 Debug = _config->FindB("Debug::pkgAcquire::RRed", false);
434 URI Get = Itm->Uri;
435 string Path = Get.Host + Get.Path; // To account for relative paths
436
437 FetchResult Res;
438 Res.Filename = Itm->DestFile;
439 if (Itm->Uri.empty() == true) {
440 Path = Itm->DestFile;
441 Itm->DestFile.append(".result");
442 } else
443 URIStart(Res);
444
445 if (Debug == true)
446 std::clog << "Patching " << Path << " with " << Path
447 << ".ed and putting result into " << Itm->DestFile << std::endl;
448 // Open the source and destination files (the d'tor of FileFd will do
449 // the cleanup/closing of the fds)
450 FileFd From(Path,FileFd::ReadOnly);
451 FileFd Patch(Path+".ed",FileFd::ReadOnly);
452 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
453 To.EraseOnFailure();
454 if (_error->PendingError() == true)
455 return false;
456
457 Hashes Hash;
458 // now do the actual patching
459 State const result = patchMMap(Patch, From, To, &Hash);
460 if (result == MMAP_FAILED) {
461 // retry with patchFile
462 lseek(Patch.Fd(), 0, SEEK_SET);
463 lseek(From.Fd(), 0, SEEK_SET);
464 To.Open(Itm->DestFile,FileFd::WriteEmpty);
465 if (_error->PendingError() == true)
466 return false;
467 if (patchFile(Patch, From, To, &Hash) != ED_OK) {
468 return _error->Errno("rred", _("Could not patch file %s"), Path.append(" (1)").c_str());
469 }
470 } else if (result != ED_OK) {
471 return _error->Errno("rred", _("Could not patch file %s"), Path.append(" (2)").c_str());
472 }
473
474 // write out the result
475 From.Close();
476 Patch.Close();
477 To.Close();
478
479 // Transfer the modification times
480 struct stat Buf;
481 if (stat(Path.c_str(),&Buf) != 0)
482 return _error->Errno("stat",_("Failed to stat"));
483
484 struct utimbuf TimeBuf;
485 TimeBuf.actime = Buf.st_atime;
486 TimeBuf.modtime = Buf.st_mtime;
487 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
488 return _error->Errno("utime",_("Failed to set modification time"));
489
490 if (stat(Itm->DestFile.c_str(),&Buf) != 0)
491 return _error->Errno("stat",_("Failed to stat"));
492
493 // return done
494 if (Itm->Uri.empty() == true) {
495 Res.LastModified = Buf.st_mtime;
496 Res.Size = Buf.st_size;
497 Res.TakeHashes(Hash);
498 URIDone(Res);
499 }
500
501 return true;
502 }
503 /*}}}*/
504 /** \brief Wrapper class for testing rred */ /*{{{*/
505 class TestRredMethod : public RredMethod {
506 public:
507 /** \brief Run rred in debug test mode
508 *
509 * This method can be used to run the rred method outside
510 * of the "normal" acquire environment for easier testing.
511 *
512 * \param base basename of all files involved in this rred test
513 */
514 bool Run(char const *base) {
515 _config->CndSet("Debug::pkgAcquire::RRed", "true");
516 FetchItem *test = new FetchItem;
517 test->DestFile = base;
518 return Fetch(test);
519 }
520 };
521 /*}}}*/
522 /** \brief Starter for the rred method (or its test method) {{{
523 *
524 * Used without parameters is the normal behavior for methods for
525 * the APT acquire system. While this works great for the acquire system
526 * it is very hard to test the method and therefore the method also
527 * accepts one parameter which will switch it directly to debug test mode:
528 * The test mode expects that if "Testfile" is given as parameter
529 * the file "Testfile" should be ed-style patched with "Testfile.ed"
530 * and will write the result to "Testfile.result".
531 */
532 int main(int argc, char *argv[]) {
533 if (argc <= 1) {
534 RredMethod Mth;
535 return Mth.Run();
536 } else {
537 TestRredMethod Mth;
538 bool result = Mth.Run(argv[1]);
539 _error->DumpErrors();
540 return result;
541 }
542 }
543 /*}}}*/