You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

661 lines
14 KiB

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