use utimes instead of utimensat/futimens
[ntk/apt.git] / methods / rred.cc
1 // Copyright (c) 2014 Anthony Towns
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7
8 #include <config.h>
9
10 #include <apt-pkg/fileutl.h>
11 #include <apt-pkg/mmap.h>
12 #include <apt-pkg/error.h>
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/strutl.h>
15 #include <apt-pkg/hashes.h>
16 #include <apt-pkg/configuration.h>
17
18 #include <string>
19 #include <list>
20 #include <vector>
21 #include <iterator>
22
23 #include <fcntl.h>
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30
31 #include <apti18n.h>
32
33 #define BLOCK_SIZE (512*1024)
34
35 class MemBlock {
36 char *start;
37 size_t size;
38 char *free;
39 struct MemBlock *next;
40
41 MemBlock(size_t size) : size(size), next(NULL)
42 {
43 free = start = new char[size];
44 }
45
46 size_t avail(void) { return size - (free - start); }
47
48 public:
49
50 MemBlock(void) {
51 free = start = new char[BLOCK_SIZE];
52 size = BLOCK_SIZE;
53 next = NULL;
54 }
55
56 ~MemBlock() {
57 delete [] start;
58 delete next;
59 }
60
61 void clear(void) {
62 free = start;
63 if (next)
64 next->clear();
65 }
66
67 char *add_easy(char *src, size_t len, char *last)
68 {
69 if (last) {
70 for (MemBlock *k = this; k; k = k->next) {
71 if (k->free == last) {
72 if (len <= k->avail()) {
73 char *n = k->add(src, len);
74 assert(last == n);
75 if (last == n)
76 return NULL;
77 return n;
78 } else {
79 break;
80 }
81 } else if (last >= start && last < free) {
82 break;
83 }
84 }
85 }
86 return add(src, len);
87 }
88
89 char *add(char *src, size_t len) {
90 if (len > avail()) {
91 if (!next) {
92 if (len > BLOCK_SIZE) {
93 next = new MemBlock(len);
94 } else {
95 next = new MemBlock;
96 }
97 }
98 return next->add(src, len);
99 }
100 char *dst = free;
101 free += len;
102 memcpy(dst, src, len);
103 return dst;
104 }
105 };
106
107 struct Change {
108 /* Ordering:
109 *
110 * 1. write out <offset> lines unchanged
111 * 2. skip <del_cnt> lines from source
112 * 3. write out <add_cnt> lines (<add>/<add_len>)
113 */
114 size_t offset;
115 size_t del_cnt;
116 size_t add_cnt; /* lines */
117 size_t add_len; /* bytes */
118 char *add;
119
120 Change(int off)
121 {
122 offset = off;
123 del_cnt = add_cnt = add_len = 0;
124 add = NULL;
125 }
126
127 /* actually, don't write <lines> lines from <add> */
128 void skip_lines(size_t lines)
129 {
130 while (lines > 0) {
131 char *s = (char*) memchr(add, '\n', add_len);
132 assert(s != NULL);
133 s++;
134 add_len -= (s - add);
135 add_cnt--;
136 lines--;
137 if (add_len == 0) {
138 add = NULL;
139 assert(add_cnt == 0);
140 assert(lines == 0);
141 } else {
142 add = s;
143 assert(add_cnt > 0);
144 }
145 }
146 }
147 };
148
149 class FileChanges {
150 std::list<struct Change> changes;
151 std::list<struct Change>::iterator where;
152 size_t pos; // line number is as far left of iterator as possible
153
154 bool pos_is_okay(void)
155 {
156 #ifdef POSDEBUG
157 size_t cpos = 0;
158 std::list<struct Change>::iterator x;
159 for (x = changes.begin(); x != where; ++x) {
160 assert(x != changes.end());
161 cpos += x->offset + x->add_cnt;
162 }
163 return cpos == pos;
164 #else
165 return true;
166 #endif
167 }
168
169 public:
170 FileChanges() {
171 where = changes.end();
172 pos = 0;
173 }
174
175 std::list<struct Change>::iterator begin(void) { return changes.begin(); }
176 std::list<struct Change>::iterator end(void) { return changes.end(); }
177
178 std::list<struct Change>::reverse_iterator rbegin(void) { return changes.rbegin(); }
179 std::list<struct Change>::reverse_iterator rend(void) { return changes.rend(); }
180
181 void add_change(Change c) {
182 assert(pos_is_okay());
183 go_to_change_for(c.offset);
184 assert(pos + where->offset == c.offset);
185 if (c.del_cnt > 0)
186 delete_lines(c.del_cnt);
187 assert(pos + where->offset == c.offset);
188 if (c.add_len > 0) {
189 assert(pos_is_okay());
190 if (where->add_len > 0)
191 new_change();
192 assert(where->add_len == 0 && where->add_cnt == 0);
193
194 where->add_len = c.add_len;
195 where->add_cnt = c.add_cnt;
196 where->add = c.add;
197 }
198 assert(pos_is_okay());
199 merge();
200 assert(pos_is_okay());
201 }
202
203 private:
204 void merge(void)
205 {
206 while (where->offset == 0 && where != changes.begin()) {
207 left();
208 }
209 std::list<struct Change>::iterator next = where;
210 ++next;
211
212 while (next != changes.end() && next->offset == 0) {
213 where->del_cnt += next->del_cnt;
214 next->del_cnt = 0;
215 if (next->add == NULL) {
216 next = changes.erase(next);
217 } else if (where->add == NULL) {
218 where->add = next->add;
219 where->add_len = next->add_len;
220 where->add_cnt = next->add_cnt;
221 next = changes.erase(next);
222 } else {
223 ++next;
224 }
225 }
226 }
227
228 void go_to_change_for(size_t line)
229 {
230 while(where != changes.end()) {
231 if (line < pos) {
232 left();
233 continue;
234 }
235 if (pos + where->offset + where->add_cnt <= line) {
236 right();
237 continue;
238 }
239 // line is somewhere in this slot
240 if (line < pos + where->offset) {
241 break;
242 } else if (line == pos + where->offset) {
243 return;
244 } else {
245 split(line - pos);
246 right();
247 return;
248 }
249 }
250 /* it goes before this patch */
251 insert(line-pos);
252 }
253
254 void new_change(void) { insert(where->offset); }
255
256 void insert(size_t offset)
257 {
258 assert(pos_is_okay());
259 assert(where == changes.end() || offset <= where->offset);
260 if (where != changes.end())
261 where->offset -= offset;
262 changes.insert(where, Change(offset));
263 --where;
264 assert(pos_is_okay());
265 }
266
267 void split(size_t offset)
268 {
269 assert(pos_is_okay());
270
271 assert(where->offset < offset);
272 assert(offset < where->offset + where->add_cnt);
273
274 size_t keep_lines = offset - where->offset;
275
276 Change before(*where);
277
278 where->del_cnt = 0;
279 where->offset = 0;
280 where->skip_lines(keep_lines);
281
282 before.add_cnt = keep_lines;
283 before.add_len -= where->add_len;
284
285 changes.insert(where, before);
286 --where;
287 assert(pos_is_okay());
288 }
289
290 void delete_lines(size_t cnt)
291 {
292 std::list<struct Change>::iterator x = where;
293 assert(pos_is_okay());
294 while (cnt > 0)
295 {
296 size_t del;
297 del = x->add_cnt;
298 if (del > cnt)
299 del = cnt;
300 x->skip_lines(del);
301 cnt -= del;
302
303 ++x;
304 if (x == changes.end()) {
305 del = cnt;
306 } else {
307 del = x->offset;
308 if (del > cnt)
309 del = cnt;
310 x->offset -= del;
311 }
312 where->del_cnt += del;
313 cnt -= del;
314 }
315 assert(pos_is_okay());
316 }
317
318 void left(void) {
319 assert(pos_is_okay());
320 --where;
321 pos -= where->offset + where->add_cnt;
322 assert(pos_is_okay());
323 }
324
325 void right(void) {
326 assert(pos_is_okay());
327 pos += where->offset + where->add_cnt;
328 ++where;
329 assert(pos_is_okay());
330 }
331 };
332
333 class Patch {
334 FileChanges filechanges;
335 MemBlock add_text;
336
337 static bool retry_fwrite(char *b, size_t l, FILE *f, Hashes *hash)
338 {
339 size_t r = 1;
340 while (r > 0 && l > 0)
341 {
342 r = fwrite(b, 1, l, f);
343 if (hash)
344 hash->Add((unsigned char*)b, r);
345 l -= r;
346 b += r;
347 }
348 return l == 0;
349 }
350
351 static void dump_rest(FILE *o, FILE *i, Hashes *hash)
352 {
353 char buffer[BLOCK_SIZE];
354 size_t l;
355 while (0 < (l = fread(buffer, 1, sizeof(buffer), i))) {
356 if (!retry_fwrite(buffer, l, o, hash))
357 break;
358 }
359 }
360
361 static void dump_lines(FILE *o, FILE *i, size_t n, Hashes *hash)
362 {
363 char buffer[BLOCK_SIZE];
364 while (n > 0) {
365 if (fgets(buffer, sizeof(buffer), i) == 0)
366 buffer[0] = '\0';
367 size_t const l = strlen(buffer);
368 if (l == 0 || buffer[l-1] == '\n')
369 n--;
370 retry_fwrite(buffer, l, o, hash);
371 }
372 }
373
374 static void skip_lines(FILE *i, int n)
375 {
376 char buffer[BLOCK_SIZE];
377 while (n > 0) {
378 if (fgets(buffer, sizeof(buffer), i) == 0)
379 buffer[0] = '\0';
380 size_t const l = strlen(buffer);
381 if (l == 0 || buffer[l-1] == '\n')
382 n--;
383 }
384 }
385
386 static void dump_mem(FILE *o, char *p, size_t s, Hashes *hash) {
387 retry_fwrite(p, s, o, hash);
388 }
389
390 public:
391
392 void read_diff(FileFd &f)
393 {
394 char buffer[BLOCK_SIZE];
395 bool cmdwanted = true;
396
397 Change ch(0);
398 while(f.ReadLine(buffer, sizeof(buffer)))
399 {
400 if (cmdwanted) {
401 char *m, *c;
402 size_t s, e;
403 s = strtol(buffer, &m, 10);
404 if (m == buffer) {
405 s = e = ch.offset + ch.add_cnt;
406 c = buffer;
407 } else if (*m == ',') {
408 m++;
409 e = strtol(m, &c, 10);
410 } else {
411 e = s;
412 c = m;
413 }
414 switch(*c) {
415 case 'a':
416 cmdwanted = false;
417 ch.add = NULL;
418 ch.add_cnt = 0;
419 ch.add_len = 0;
420 ch.offset = s;
421 ch.del_cnt = 0;
422 break;
423 case 'c':
424 cmdwanted = false;
425 ch.add = NULL;
426 ch.add_cnt = 0;
427 ch.add_len = 0;
428 ch.offset = s - 1;
429 ch.del_cnt = e - s + 1;
430 break;
431 case 'd':
432 ch.offset = s - 1;
433 ch.del_cnt = e - s + 1;
434 ch.add = NULL;
435 ch.add_cnt = 0;
436 ch.add_len = 0;
437 filechanges.add_change(ch);
438 break;
439 }
440 } else { /* !cmdwanted */
441 if (buffer[0] == '.' && buffer[1] == '\n') {
442 cmdwanted = true;
443 filechanges.add_change(ch);
444 } else {
445 char *last = NULL;
446 char *add;
447 size_t l;
448 if (ch.add)
449 last = ch.add + ch.add_len;
450 l = strlen(buffer);
451 add = add_text.add_easy(buffer, l, last);
452 if (!add) {
453 ch.add_len += l;
454 ch.add_cnt++;
455 } else {
456 if (ch.add) {
457 filechanges.add_change(ch);
458 ch.del_cnt = 0;
459 }
460 ch.offset += ch.add_cnt;
461 ch.add = add;
462 ch.add_len = l;
463 ch.add_cnt = 1;
464 }
465 }
466 }
467 }
468 }
469
470 void write_diff(FILE *f)
471 {
472 size_t line = 0;
473 std::list<struct Change>::reverse_iterator ch;
474 for (ch = filechanges.rbegin(); ch != filechanges.rend(); ++ch) {
475 line += ch->offset + ch->del_cnt;
476 }
477
478 for (ch = filechanges.rbegin(); ch != filechanges.rend(); ++ch) {
479 std::list<struct Change>::reverse_iterator mg_i, mg_e = ch;
480 while (ch->del_cnt == 0 && ch->offset == 0)
481 ++ch;
482 line -= ch->del_cnt;
483 if (ch->add_cnt > 0) {
484 if (ch->del_cnt == 0) {
485 fprintf(f, "%lua\n", line);
486 } else if (ch->del_cnt == 1) {
487 fprintf(f, "%luc\n", line+1);
488 } else {
489 fprintf(f, "%lu,%luc\n", line+1, line+ch->del_cnt);
490 }
491
492 mg_i = ch;
493 do {
494 dump_mem(f, mg_i->add, mg_i->add_len, NULL);
495 } while (mg_i-- != mg_e);
496
497 fprintf(f, ".\n");
498 } else if (ch->del_cnt == 1) {
499 fprintf(f, "%lud\n", line+1);
500 } else if (ch->del_cnt > 1) {
501 fprintf(f, "%lu,%lud\n", line+1, line+ch->del_cnt);
502 }
503 line -= ch->offset;
504 }
505 }
506
507 void apply_against_file(FILE *out, FILE *in, Hashes *hash = NULL)
508 {
509 std::list<struct Change>::iterator ch;
510 for (ch = filechanges.begin(); ch != filechanges.end(); ++ch) {
511 dump_lines(out, in, ch->offset, hash);
512 skip_lines(in, ch->del_cnt);
513 dump_mem(out, ch->add, ch->add_len, hash);
514 }
515 dump_rest(out, in, hash);
516 }
517 };
518
519 class RredMethod : public pkgAcqMethod {
520 private:
521 bool Debug;
522
523 protected:
524 virtual bool Fetch(FetchItem *Itm) {
525 Debug = _config->FindB("Debug::pkgAcquire::RRed", false);
526 URI Get = Itm->Uri;
527 std::string Path = Get.Host + Get.Path; // rred:/path - no host
528
529 FetchResult Res;
530 Res.Filename = Itm->DestFile;
531 if (Itm->Uri.empty())
532 {
533 Path = Itm->DestFile;
534 Itm->DestFile.append(".result");
535 } else
536 URIStart(Res);
537
538 std::vector<std::string> patchpaths;
539 Patch patch;
540
541 if (FileExists(Path + ".ed") == true)
542 patchpaths.push_back(Path + ".ed");
543 else
544 {
545 _error->PushToStack();
546 std::vector<std::string> patches = GetListOfFilesInDir(flNotFile(Path), "gz", true, false);
547 _error->RevertToStack();
548
549 std::string const baseName = Path + ".ed.";
550 for (std::vector<std::string>::const_iterator p = patches.begin();
551 p != patches.end(); ++p)
552 if (p->compare(0, baseName.length(), baseName) == 0)
553 patchpaths.push_back(*p);
554 }
555
556 std::string patch_name;
557 for (std::vector<std::string>::iterator I = patchpaths.begin();
558 I != patchpaths.end();
559 ++I)
560 {
561 patch_name = *I;
562 if (Debug == true)
563 std::clog << "Patching " << Path << " with " << patch_name
564 << std::endl;
565
566 FileFd p;
567 // all patches are compressed, even if the name doesn't reflect it
568 if (p.Open(patch_name, FileFd::ReadOnly, FileFd::Gzip) == false) {
569 std::cerr << "Could not open patch file " << patch_name << std::endl;
570 _error->DumpErrors(std::cerr);
571 abort();
572 }
573 patch.read_diff(p);
574 p.Close();
575 }
576
577 if (Debug == true)
578 std::clog << "Applying patches against " << Path
579 << " and writing results to " << Itm->DestFile
580 << std::endl;
581
582 FILE *inp = fopen(Path.c_str(), "r");
583 FILE *out = fopen(Itm->DestFile.c_str(), "w");
584
585 Hashes hash;
586
587 patch.apply_against_file(out, inp, &hash);
588
589 fclose(out);
590 fclose(inp);
591
592 if (Debug == true) {
593 std::clog << "rred: finished file patching of " << Path << "." << std::endl;
594 }
595
596 struct stat bufbase, bufpatch;
597 if (stat(Path.c_str(), &bufbase) != 0 ||
598 stat(patch_name.c_str(), &bufpatch) != 0)
599 return _error->Errno("stat", _("Failed to stat"));
600
601 struct timeval times[2];
602 times[0].tv_sec = bufbase.st_atime;
603 times[1].tv_sec = bufpatch.st_mtime;
604 times[0].tv_usec = times[1].tv_usec = 0;
605 if (utimes(Itm->DestFile.c_str(), times) != 0)
606 return _error->Errno("utimes",_("Failed to set modification time"));
607
608 if (stat(Itm->DestFile.c_str(), &bufbase) != 0)
609 return _error->Errno("stat", _("Failed to stat"));
610
611 Res.LastModified = bufbase.st_mtime;
612 Res.Size = bufbase.st_size;
613 Res.TakeHashes(hash);
614 URIDone(Res);
615
616 return true;
617 }
618
619 public:
620 RredMethod() : pkgAcqMethod("2.0",SingleInstance | SendConfig), Debug(false) {}
621 };
622
623 int main(int argc, char **argv)
624 {
625 int i;
626 bool just_diff = true;
627 Patch patch;
628
629 if (argc <= 1) {
630 RredMethod Mth;
631 return Mth.Run();
632 }
633
634 if (argc > 1 && strcmp(argv[1], "-f") == 0) {
635 just_diff = false;
636 i = 2;
637 } else {
638 i = 1;
639 }
640
641 for (; i < argc; i++) {
642 FileFd p;
643 if (p.Open(argv[i], FileFd::ReadOnly) == false) {
644 _error->DumpErrors(std::cerr);
645 exit(1);
646 }
647 patch.read_diff(p);
648 }
649
650 if (just_diff) {
651 patch.write_diff(stdout);
652 } else {
653 FILE *out, *inp;
654 out = stdout;
655 inp = stdin;
656
657 patch.apply_against_file(out, inp);
658 }
659 return 0;
660 }