ccache_entry.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "platform.h"
  2. #include <sys/stat.h>
  3. #include <errno.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <zlib.h>
  10. #include "ccache_internal.h"
  11. #include "crypto.h"
  12. #include "multitape.h"
  13. #include "multitape_internal.h"
  14. #include "patricia.h"
  15. #include "sysendian.h"
  16. #include "tsnetwork.h"
  17. #include "warnp.h"
  18. #include "ccache.h"
  19. /* A cookie for higher layers to access a cache entry. */
  20. struct ccache_entry {
  21. struct ccache_internal * cci; /* Cache data structure. */
  22. struct ccache_record * ccr; /* Actual cache entry. */
  23. struct ccache_record ** ccrp; /* Pointer to pointer in tree. */
  24. int hittrailer; /* Non-zero if the multitape layer */
  25. /* has told us about a trailer. */
  26. uint8_t * trailer; /* Uncompressed trailer. */
  27. ino_t ino_new; /* New inode number. */
  28. off_t size_new; /* New file size. */
  29. time_t mtime_new; /* New modification time. */
  30. };
  31. static int callback_addchunk(void *, struct chunkheader *);
  32. static int callback_addtrailer(void *, const uint8_t *, size_t);
  33. static int callback_faketrailer(void *, const uint8_t *, size_t);
  34. /* Callback to add a chunk header to a cache entry. */
  35. static int
  36. callback_addchunk(void * cookie, struct chunkheader * ch)
  37. {
  38. struct ccache_entry * cce = cookie;
  39. struct ccache_record * ccr = cce->ccr;
  40. struct chunkheader * p;
  41. size_t nchalloc_new;
  42. /*
  43. * Has the multitape layer written a "trailer" already for this file?
  44. * If so, return without doing anything. This can occur if an archive
  45. * checkpoint occurs in the middle of an archive entry's data.
  46. */
  47. if (cce->hittrailer)
  48. goto done;
  49. /* Do we need to expand the allocated space? */
  50. if (ccr->nch >= ccr->nchalloc) {
  51. /* Double the allocated memory. */
  52. if (ccr->nchalloc)
  53. nchalloc_new = ccr->nchalloc * 2;
  54. else {
  55. /* No data or mmapped data. */
  56. nchalloc_new = ccr->nch + 1;
  57. }
  58. /* Make sure we don't overflow. */
  59. if (nchalloc_new >
  60. SIZE_MAX / sizeof(struct chunkheader)) {
  61. errno = ENOMEM;
  62. goto err0;
  63. }
  64. /* Attempt to reallocate. */
  65. if (ccr->nchalloc) {
  66. if ((p = realloc(ccr->chp, nchalloc_new *
  67. sizeof(struct chunkheader))) == NULL)
  68. goto err0;
  69. } else {
  70. if ((p = malloc(nchalloc_new *
  71. sizeof(struct chunkheader))) == NULL)
  72. goto err0;
  73. memcpy(p, ccr->chp, ccr->nch *
  74. sizeof(struct chunkheader));
  75. }
  76. /* Successfully reallocated. */
  77. ccr->chp = p;
  78. ccr->nchalloc = nchalloc_new;
  79. }
  80. /* We now have space; add the new record. */
  81. memcpy(ccr->chp + ccr->nch, ch, sizeof(struct chunkheader));
  82. ccr->nch += 1;
  83. /* Adjust memory usage accounting. */
  84. cce->cci->chunksusage += sizeof(struct chunkheader);
  85. done:
  86. /* Success! */
  87. return (0);
  88. err0:
  89. /* Failure! */
  90. return (-1);
  91. }
  92. /* Callback to add a file trailer. */
  93. static int
  94. callback_addtrailer(void * cookie, const uint8_t * buf, size_t buflen)
  95. {
  96. struct ccache_entry * cce = cookie;
  97. struct ccache_record * ccr = cce->ccr;
  98. uint8_t * zbuf;
  99. uLongf zlen;
  100. int rc;
  101. /*
  102. * Has the multitape layer written a "trailer" already for this file?
  103. * If so, return without doing anything. This can occur if an archive
  104. * checkpoint occurs in the middle of an archive entry's data.
  105. */
  106. if (cce->hittrailer)
  107. goto done;
  108. /* We have now been informed about a trailer. */
  109. cce->hittrailer = 1;
  110. /* Allocate space for the trailer. */
  111. zlen = buflen + (buflen >> 9) + 13;
  112. if ((zbuf = malloc(zlen)) == NULL)
  113. goto err0;
  114. /* Compress trailer. */
  115. if ((rc = compress2(zbuf, &zlen, buf, buflen, 9)) != Z_OK) {
  116. switch (rc) {
  117. case Z_MEM_ERROR:
  118. errno = ENOMEM;
  119. warnp("Error compressing data");
  120. break;
  121. case Z_BUF_ERROR:
  122. warn0("Programmer error: "
  123. "Buffer too small to hold zlib-compressed data");
  124. break;
  125. default:
  126. warn0("Programmer error: "
  127. "Unexpected error code from compress2: %d", rc);
  128. break;
  129. }
  130. goto err1;
  131. }
  132. /* Reallocate to correct length. */
  133. if ((ccr->ztrailer = realloc(zbuf, zlen)) == NULL)
  134. goto err1;
  135. ccr->tlen = buflen;
  136. ccr->tzlen = zlen;
  137. ccr->flags = ccr->flags | CCR_ZTRAILER_MALLOC;
  138. /* Adjust memory usage accounting. */
  139. cce->cci->trailerusage += zlen;
  140. done:
  141. /* Success! */
  142. return (0);
  143. err1:
  144. free(zbuf);
  145. err0:
  146. /* Failure! */
  147. return (-1);
  148. }
  149. /*
  150. * Callback to record that the multitape code has logged a trailer but not
  151. * bother caching the trailer.
  152. */
  153. static int
  154. callback_faketrailer(void * cookie, const uint8_t * buf, size_t buflen)
  155. {
  156. struct ccache_entry * cce = cookie;
  157. (void)buf; /* UNUSED */
  158. (void)buflen; /* UNUSED */
  159. /* We have now been informed about a trailer. */
  160. cce->hittrailer = 1;
  161. /* Success! */
  162. return (0);
  163. }
  164. /**
  165. * ccache_entry_lookup(cache, path, sb, cookie, fullentry):
  166. * An archive entry is being written for the file ${path} with lstat data
  167. * ${sb}, to the multitape with write cookie ${cookie}. Look up the file in
  168. * the chunkification cache ${cache}, and set ${fullentry} to a non-zero
  169. * value iff the cache can provide at least sb->st_size bytes of the archive
  170. * entry. Return a cookie which can be passed to either ccache_entry_write
  171. * or ccache_entry_start depending upon whether ${fullentry} is zero or not.
  172. */
  173. CCACHE_ENTRY *
  174. ccache_entry_lookup(CCACHE * cache, const char * path, const struct stat * sb,
  175. TAPE_W * cookie, int * fullentry)
  176. {
  177. struct ccache_internal * C = cache;
  178. struct ccache_entry * cce;
  179. int fresh;
  180. size_t cnum = 0; /* No chunks known to be available yet. */
  181. off_t skiplen = 0; /* No data known to be providable yet. */
  182. ssize_t lenwrit;
  183. uLongf tbuflen;
  184. int rc;
  185. /* Allocate memory. */
  186. if ((cce = malloc(sizeof(struct ccache_entry))) == NULL)
  187. goto err0;
  188. /* Record the cache with which this entry is affiliated. */
  189. cce->cci = cache;
  190. /*
  191. * The multitape layer hasn't written any "trailer" for this file
  192. * yet. It doesn't matter if we have a trailer in our cache -- if
  193. * we're in a position where we're getting callbacks from the
  194. * multitape code (i.e., if we can't supply the entire file using
  195. * cached data) we'll have thrown away the trailer we have.
  196. */
  197. cce->hittrailer = 0;
  198. /* Record the new inode number, size, and modification time. */
  199. cce->ino_new = sb->st_ino;
  200. cce->size_new = sb->st_size;
  201. cce->mtime_new = sb->st_mtime;
  202. /* Look up cache entry. */
  203. if ((cce->ccrp = (struct ccache_record **)patricia_lookup(C->tree,
  204. (const uint8_t *)path, strlen(path))) == NULL) {
  205. /* No cache entry for this path. Create an empty record. */
  206. if ((cce->ccr = malloc(sizeof(struct ccache_record))) == NULL)
  207. goto err1;
  208. memset(cce->ccr, 0, sizeof(struct ccache_record));
  209. /* No decompressed trailer. */
  210. cce->trailer = NULL;
  211. /* That's all, folks! */
  212. goto done;
  213. }
  214. /* Entry is in the tree. */
  215. cce->ccr = *cce->ccrp;
  216. /* Is the cache entry fresh? */
  217. if ((cce->ino_new == cce->ccr->ino) &&
  218. (cce->size_new == cce->ccr->size) &&
  219. (cce->mtime_new == cce->ccr->mtime))
  220. fresh = 1;
  221. else
  222. fresh = 0;
  223. /* If the cache entry is fresh, check if the chunks are available. */
  224. if (fresh) {
  225. /* Check if chunks are still available. */
  226. for (; cnum < cce->ccr->nch; cnum++) {
  227. lenwrit = writetape_ischunkpresent(cookie,
  228. cce->ccr->chp + cnum);
  229. /* Error? */
  230. if (lenwrit < 0)
  231. goto err1;
  232. /* Not present? */
  233. if (lenwrit == 0)
  234. break;
  235. /* We can supply this data. */
  236. skiplen += lenwrit;
  237. }
  238. }
  239. /*
  240. * If the cache entry is fresh; all of the chunks are available; we
  241. * have a trailer; and the trailer is long enough that having it will
  242. * allow us to provide the entire archive entry out of the cache;
  243. * then try to decompress the trailer.
  244. */
  245. if (fresh &&
  246. (cnum == cce->ccr->nch) &&
  247. (cce->ccr->tlen > 0) &&
  248. (skiplen + (off_t)(cce->ccr->tlen) >= sb->st_size)) {
  249. /* Allocate space for trailer. */
  250. tbuflen = cce->ccr->tlen;
  251. if ((cce->trailer = malloc(tbuflen)) == NULL)
  252. goto err1;
  253. /* Decompress trailer. */
  254. rc = uncompress(cce->trailer, &tbuflen,
  255. cce->ccr->ztrailer, cce->ccr->tzlen);
  256. /* Print warnings. */
  257. if (rc != Z_OK) {
  258. switch (rc) {
  259. case Z_MEM_ERROR:
  260. errno = ENOMEM;
  261. warnp("Error decompressing cache");
  262. break;
  263. case Z_BUF_ERROR:
  264. case Z_DATA_ERROR:
  265. warn0("Warning: cached trailer is corrupt");
  266. break;
  267. default:
  268. warn0("Programmer error: "
  269. "Unexpected error code from "
  270. "uncompress: %d", rc);
  271. break;
  272. }
  273. } else if (tbuflen != cce->ccr->tlen) {
  274. warn0("Cached trailer is corrupt");
  275. rc = Z_DATA_ERROR;
  276. }
  277. /* If the trailer didn't decompress properly, clean it up. */
  278. if (rc != Z_OK) {
  279. free(cce->trailer);
  280. cce->trailer = NULL;
  281. }
  282. /* We can supply the trailer data from the cache. */
  283. skiplen += cce->ccr->tlen;
  284. } else {
  285. cce->trailer = NULL;
  286. }
  287. /*
  288. * If there is a compressed trailer but no decompressed trailer, we
  289. * must have decided that the compressed trailer was useless; delete
  290. * it.
  291. */
  292. if ((cce->trailer == NULL) && (cce->ccr->tlen > 0)) {
  293. /* Free the compressed trailer if appropriate. */
  294. if (cce->ccr->flags & CCR_ZTRAILER_MALLOC) {
  295. cce->cci->trailerusage -= cce->ccr->tzlen;
  296. free(cce->ccr->ztrailer);
  297. }
  298. /* We have no compressed trailer. */
  299. cce->ccr->ztrailer = NULL;
  300. cce->ccr->tlen = cce->ccr->tzlen = 0;
  301. }
  302. done:
  303. /* Can we supply the entire file worth of data? */
  304. if (skiplen >= sb->st_size)
  305. *fullentry = 1;
  306. else
  307. *fullentry = 0;
  308. /* Success! */
  309. return (cce);
  310. err1:
  311. free(cce);
  312. err0:
  313. /* Failure! */
  314. return (NULL);
  315. }
  316. /**
  317. * ccache_entry_write(cce, cookie):
  318. * Write the cached archive entry ${cce} to the multitape with write cookie
  319. * ${cookie}. Note that this may only be called if ${cce} was returned by
  320. * a ccache_entry_lookup which set ${fullentry} to a non-zero value. Return
  321. * the length written.
  322. */
  323. off_t
  324. ccache_entry_write(CCACHE_ENTRY * cce, TAPE_W * cookie)
  325. {
  326. off_t skiplen = 0;
  327. size_t cnum;
  328. ssize_t lenwrit;
  329. /* Make sure the cache entry isn't stale. */
  330. if ((cce->ino_new != cce->ccr->ino) ||
  331. (cce->size_new != cce->ccr->size) ||
  332. (cce->mtime_new != cce->ccr->mtime)) {
  333. warn0("Programmer error: "
  334. "ccache_entry_write called with stale cache entry");
  335. goto err0;
  336. }
  337. /* Write chunks. */
  338. for (cnum = 0; cnum < cce->ccr->nch; cnum++) {
  339. lenwrit = writetape_writechunk(cookie, cce->ccr->chp + cnum);
  340. /* Error? */
  341. if (lenwrit < 0)
  342. goto err0;
  343. /* We should always be able to write chunks at this point. */
  344. if (lenwrit == 0) {
  345. warn0("Programmer error: "
  346. "writetape_writechunk unexpectedly returned 0");
  347. goto err0;
  348. }
  349. skiplen += lenwrit;
  350. }
  351. /* If we have a trailer, write it. */
  352. if (cce->trailer != NULL) {
  353. lenwrit = writetape_write(cookie, cce->trailer,
  354. cce->ccr->tlen);
  355. /* Error? */
  356. if (lenwrit < 0)
  357. goto err0;
  358. skiplen += lenwrit;
  359. }
  360. /* Success! */
  361. return (skiplen);
  362. err0:
  363. /* Failure! */
  364. return (-1);
  365. }
  366. /**
  367. * ccache_entry_writefile(cce, cookie, notrailer, fd):
  368. * Write data from the file descriptor ${fd} to the multitape with write
  369. * cookie ${cookie}, using the cache entry ${cce} as a hint about how data
  370. * is chunkified; and set up callbacks from the multitape layer so that the
  371. * cache entry will be updated with any further chunks and (if ${notrailer}
  372. * is zero) any trailer. Return the length written.
  373. */
  374. off_t
  375. ccache_entry_writefile(CCACHE_ENTRY * cce, TAPE_W * cookie,
  376. int notrailer, int fd)
  377. {
  378. off_t skiplen = 0;
  379. uint8_t * chunkbuf;
  380. size_t chunklen, cpos;
  381. size_t cnum;
  382. ssize_t lenwrit, lenread;
  383. uint8_t hbuf[32];
  384. /*
  385. * Make sure there is no trailer in this cache entry -- a trailer
  386. * should only exist if we can supply the entire file, in which case
  387. * ccache_entry_write should be called instead.
  388. */
  389. if (cce->ccr->tlen > 0) {
  390. warn0("Programmer error: "
  391. "ccache_entry_writefile called but trailer exists");
  392. goto err0;
  393. }
  394. /* If we have some chunks, allocate a buffer for verification. */
  395. if (cce->ccr->nch) {
  396. if ((chunkbuf = malloc(MAXCHUNK)) == NULL)
  397. goto err0;
  398. } else {
  399. chunkbuf = NULL;
  400. }
  401. /* Read chunk-sized blocks and write them if unchanged. */
  402. for (cnum = 0; cnum < cce->ccr->nch; cnum++) {
  403. /* Handle network activity if necessary. */
  404. if (network_select(0))
  405. goto err1;
  406. /* Decode a chunk. */
  407. chunklen = le32dec((cce->ccr->chp + cnum)->len);
  408. /* Sanity check. */
  409. if (chunklen > MAXCHUNK) {
  410. warn0("Cache entry is corrupt");
  411. break;
  412. }
  413. /*
  414. * We can't go beyond the length which libarchive thinks the
  415. * file is, even if the file has grown since when we called
  416. * lstat on it and the cache is corrupt.
  417. */
  418. if ((skiplen + (off_t)chunklen) > cce->size_new)
  419. break;
  420. /* Read until we've got the whole chunk. */
  421. for (cpos = 0; cpos < chunklen; cpos += (size_t)lenread) {
  422. lenread = read(fd, chunkbuf + cpos, chunklen - cpos);
  423. if (lenread < 0) {
  424. warnp("reading file");
  425. goto err1;
  426. } else if (lenread == 0) {
  427. /*
  428. * There's nothing wrong with the file being
  429. * shorter than it used to be.
  430. */
  431. break;
  432. }
  433. }
  434. /* If we hit EOF, we can't use this chunk. */
  435. if (cpos < chunklen)
  436. break;
  437. /* Compute the hash of the data we've read. */
  438. if (crypto_hash_data(CRYPTO_KEY_HMAC_CHUNK,
  439. chunkbuf, chunklen, hbuf))
  440. goto err1;
  441. /* Is it different? */
  442. if (memcmp(hbuf, (cce->ccr->chp + cnum)->hash, 32))
  443. break;
  444. /* Ok, pass the chunk header to the multitape code. */
  445. lenwrit = writetape_writechunk(cookie, cce->ccr->chp + cnum);
  446. /* Error? */
  447. if (lenwrit < 0)
  448. goto err1;
  449. /*
  450. * Chunk not present? This can happen in here, since
  451. * we don't verify that all the chunks are available
  452. * during ccache_entry_start if the file has changed.
  453. */
  454. if (lenwrit == 0)
  455. break;
  456. /* We've written the chunk; the caller can skip it. */
  457. skiplen += lenwrit;
  458. }
  459. /* Free chunk buffer. */
  460. free(chunkbuf);
  461. /* Record the number of chunks we wrote. */
  462. cce->ccr->nch = cnum;
  463. /* Update the inode number, file size, and modification time. */
  464. cce->ccr->ino = cce->ino_new;
  465. cce->ccr->size = cce->size_new;
  466. cce->ccr->mtime = cce->mtime_new;
  467. /* Ask the multitape layer to inform us about later chunks. */
  468. writetape_setcallback(cookie, callback_addchunk,
  469. ((cce->cci->trailerusage > cce->cci->chunksusage * 2) ||
  470. (notrailer != 0)) ? callback_faketrailer : callback_addtrailer,
  471. cce);
  472. /* Success! */
  473. return (skiplen);
  474. err1:
  475. free(chunkbuf);
  476. err0:
  477. /* Failure! */
  478. return (-1);
  479. }
  480. /**
  481. * ccache_entry_end(cache, cce, cookie, path, snaptime):
  482. * The archive entry is ending; clean up callbacks, insert the cache entry
  483. * into the cache if it isn't already present, and free memory.
  484. */
  485. int
  486. ccache_entry_end(CCACHE * cache, CCACHE_ENTRY * cce, TAPE_W * cookie,
  487. const char * path, time_t snaptime)
  488. {
  489. size_t slen;
  490. /* Don't want any more callbacks. */
  491. writetape_setcallback(cookie, NULL, NULL, NULL);
  492. /*
  493. * If the cache entry is stale and ccache_entry_writefile was
  494. * never called, the cached chunks we have are probably not useful
  495. * (the file was probably truncated to 0 bytes); so remove them.
  496. */
  497. if ((cce->ino_new != cce->ccr->ino) ||
  498. (cce->size_new != cce->ccr->size) ||
  499. (cce->mtime_new != cce->ccr->mtime))
  500. cce->ccr->nch = 0;
  501. /*
  502. * If the modification time is equal to or after the snapshot time,
  503. * adjust the modification time to ensure that we will consider this
  504. * file to be "modified" the next time we see it.
  505. */
  506. if (cce->ccr->mtime >= snaptime)
  507. cce->ccr->mtime = snaptime - 1;
  508. /* This cache entry is in use and should not be expired yet. */
  509. cce->ccr->age = 0;
  510. /*
  511. * If the entry is worth keeping, make sure it's in the cache;
  512. * otherwise, free it if it's not already in the cache.
  513. */
  514. if ((cce->ccr->nch != 0) || (cce->ccr->tlen != 0)) {
  515. if (cce->ccrp == NULL) {
  516. slen = strlen(path);
  517. if (patricia_insert(cache->tree,
  518. (const uint8_t *)path, slen, cce->ccr))
  519. goto err1;
  520. }
  521. } else {
  522. if (cce->ccrp == NULL) {
  523. if (cce->ccr->nchalloc)
  524. free(cce->ccr->chp);
  525. free(cce->ccr);
  526. }
  527. }
  528. /* Free the cache entry cookie. */
  529. free(cce->trailer);
  530. free(cce);
  531. /* Success! */
  532. return (0);
  533. err1:
  534. if (cce->ccr->flags & CCR_ZTRAILER_MALLOC)
  535. free(cce->ccr->ztrailer);
  536. if (cce->ccr->nchalloc)
  537. free(cce->ccr->chp);
  538. free(cce->ccr);
  539. free(cce->trailer);
  540. free(cce);
  541. /* Failure! */
  542. return (-1);
  543. }
  544. /**
  545. * ccache_entry_free(cce, cookie):
  546. * Free the cache entry and cancel callbacks from the multitape layer.
  547. */
  548. void
  549. ccache_entry_free(CCACHE_ENTRY * cce, TAPE_W * cookie)
  550. {
  551. if (cce == NULL)
  552. return;
  553. /* Don't want any more callbacks. */
  554. writetape_setcallback(cookie, NULL, NULL, NULL);
  555. /* If the record isn't in the tree, free it. */
  556. if (cce->ccrp == NULL) {
  557. if (cce->ccr->flags & CCR_ZTRAILER_MALLOC)
  558. free(cce->ccr->ztrailer);
  559. if (cce->ccr->nchalloc)
  560. free(cce->ccr->chp);
  561. free(cce->ccr);
  562. }
  563. /* Free the cache entry cookie. */
  564. free(cce->trailer);
  565. free(cce);
  566. }