rred.cc 15 KB

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