Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 773 lines (633 sloc) 15.755 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/*
* Blob cache
* Copyright (C) 2010 Andreas Öman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>

#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>

#include "showtime.h"
#include "blobcache.h"
#include "misc/pool.h"
#include "misc/callout.h"
#include "misc/sha.h"
#include "misc/md5.h"
#include "arch/arch.h"
#include "arch/threads.h"
#include "arch/atomic.h"
#include "settings.h"
#include "notifications.h"

#define BC2_MAGIC 0x62630203

typedef struct blobcache_item {
  struct blobcache_item *bi_link;
  uint64_t bi_key_hash;
  uint64_t bi_content_hash;
  uint32_t bi_lastaccess;
  uint32_t bi_expiry;
  uint32_t bi_modtime;
  uint32_t bi_size;
  char *bi_etag;
} blobcache_item_t;

typedef struct blobcache_diskitem {
  uint64_t di_key_hash;
  uint64_t di_content_hash;
  uint32_t di_lastaccess;
  uint32_t di_expiry;
  uint32_t di_modtime;
  uint32_t di_size;
  uint8_t di_etaglen;
  uint8_t di_etag[0];
} __attribute__((packed)) blobcache_diskitem_t;

#define ITEM_HASH_SIZE 256
#define ITEM_HASH_MASK (ITEM_HASH_SIZE - 1)

static blobcache_item_t *hashvector[ITEM_HASH_SIZE];

static pool_t *item_pool;
static hts_mutex_t cache_lock;
static int zombie;




static callout_t blobcache_callout;

static void blobcache_do_prune(struct callout *c, void *opaque);

#define BLOB_CACHE_MINSIZE (10 * 1000 * 1000)
#define BLOB_CACHE_MAXSIZE (500 * 1000 * 1000)

static uint64_t current_cache_size;


/**
*
*/
static uint64_t
blobcache_compute_maxsize(void)
{
  uint64_t avail = arch_cache_avail_bytes() + current_cache_size;
  avail = MAX(BLOB_CACHE_MINSIZE, MIN(avail / 10, BLOB_CACHE_MAXSIZE));
  return avail;
}


/**
*
*/
static uint64_t
digest_key(const char *key, const char *stash)
{
  union {
    uint8_t d[20];
    uint64_t u64;
  } u;
  sha1_decl(shactx);
  sha1_init(shactx);
  sha1_update(shactx, (const uint8_t *)key, strlen(key));
  sha1_update(shactx, (const uint8_t *)stash, strlen(stash));
  sha1_final(shactx, u.d);
  return u.u64;
}


/**
*
*/
static uint64_t
digest_content(const void *data, size_t len)
{
  union {
    uint8_t d[16];
    uint64_t u64;
  } u;
  md5_decl(ctx);
  md5_init(ctx);
  md5_update(ctx, data, len);
  md5_final(ctx, u.d);
  return u.u64;
}


/**
*
*/
static void
make_filename(char *buf, size_t len, uint64_t hash, int for_write)
{
  uint8_t dir = hash;
  if(for_write) {
    snprintf(buf, len, "%s/bc2/%02x", gconf.cache_path, dir);
    mkdir(buf, 0777);
  }
  snprintf(buf, len, "%s/bc2/%02x/%016"PRIx64, gconf.cache_path, dir, hash);
}


/**
*
*/
static void
save_index(void)
{
  char filename[PATH_MAX];
  uint8_t *out, *base;
  int i;
  blobcache_item_t *p;
  blobcache_diskitem_t *di;
  size_t siz;
  snprintf(filename, sizeof(filename), "%s/bc2/index.dat", gconf.cache_path);
  
  int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  if(fd == -1)
    return;
  
  int items = 0;
  siz = 8 + 20;

  for(i = 0; i < ITEM_HASH_SIZE; i++) {
    for(p = hashvector[i]; p != NULL; p = p->bi_link) {
      siz += sizeof(blobcache_diskitem_t);
      siz += p->bi_etag ? strlen(p->bi_etag) : 0;
      items++;
    }
  }

  base = out = mymalloc(siz);
  if(out == NULL) {
    close(fd);
    return;
  }
  *(uint32_t *)out = BC2_MAGIC;
  out += 4;
  *(uint32_t *)out = items;
  out += 4;
  for(i = 0; i < ITEM_HASH_SIZE; i++) {
    for(p = hashvector[i]; p != NULL; p = p->bi_link) {
      const int etaglen = p->bi_etag ? strlen(p->bi_etag) : 0;
      di = (blobcache_diskitem_t *)out;
      di->di_key_hash = p->bi_key_hash;
      di->di_content_hash = p->bi_content_hash;
      di->di_lastaccess = p->bi_lastaccess;
      di->di_expiry = p->bi_expiry;
      di->di_modtime = p->bi_modtime;
      di->di_size = p->bi_size;
      di->di_etaglen = etaglen;
      
      out += sizeof(blobcache_diskitem_t);
      if(etaglen) {
memcpy(out, p->bi_etag, etaglen);
out += etaglen;
      }

    }
  }

  sha1_decl(shactx);
  sha1_init(shactx);
  sha1_update(shactx, base, siz - 20);
  sha1_final(shactx, out);

  if(write(fd, base, siz) != siz)
    TRACE(TRACE_INFO, "blobcache", "Unable to store index file %s -- %s",
filename, strerror(errno));
  free(base);
  close(fd);
}



/**
*
*/
static void
load_index(void)
{
  char filename[PATH_MAX];
  uint8_t *in, *base;
  int i;
  blobcache_item_t *p;
  blobcache_diskitem_t *di;
  struct stat st;
  uint8_t digest[20];

  snprintf(filename, sizeof(filename), "%s/bc2/index.dat", gconf.cache_path);
  
  int fd = open(filename, O_RDONLY, 0);
  if(fd == -1)
    return;

  if(fstat(fd, &st) || st.st_size < 28) {
    close(fd);
    return;
  }

  base = in = mymalloc(st.st_size);
  if(in == NULL) {
    close(fd);
    return;
  }

  size_t r = read(fd, in, st.st_size);
  close(fd);
  if(r != st.st_size) {
    free(in);
    return;
  }


  if(*(uint32_t *)in != BC2_MAGIC) {
    free(in);
    return;
  }

  sha1_decl(shactx);
  sha1_init(shactx);
  sha1_update(shactx, in, st.st_size - 20);
  sha1_final(shactx, digest);

  if(memcmp(digest, in + st.st_size - 20, 20)) {
    free(in);
    TRACE(TRACE_INFO, "blobcache", "Index file corrupt, throwing away cache");
    return;
  }

  in += 4;

  int items = *(uint32_t *)in;

  in += 4;

  for(i = 0; i < items; i++) {
    di = (blobcache_diskitem_t *)in;
    p = pool_get(item_pool);

    p->bi_key_hash = di->di_key_hash;
    p->bi_content_hash = di->di_content_hash;
    p->bi_lastaccess = di->di_lastaccess;
    p->bi_expiry = di->di_expiry;
    p->bi_modtime = di->di_modtime;
    p->bi_size = di->di_size;
    int etaglen = di->di_etaglen;

    in += sizeof(blobcache_diskitem_t);
    if(etaglen) {
      p->bi_etag = malloc(etaglen+1);
      memcpy(p->bi_etag, in, etaglen);
      p->bi_etag[etaglen] = 0;
      in += etaglen;
    }
    p->bi_link = hashvector[p->bi_key_hash & ITEM_HASH_MASK];
    hashvector[p->bi_key_hash & ITEM_HASH_MASK] = p;
    current_cache_size += p->bi_size;
  }
  free(base);
}



/**
*
*/
int
blobcache_put(const char *key, const char *stash,
const void *data, size_t size, int maxage,
const char *etag, time_t mtime)
{
  uint64_t dk = digest_key(key, stash);
  uint64_t dc = digest_content(data, size);
  uint32_t now = time(NULL);
  char filename[PATH_MAX];
  blobcache_item_t *p;

  if(etag != NULL && strlen(etag) > 255)
    etag = NULL;

  hts_mutex_lock(&cache_lock);
  if(zombie) {
    hts_mutex_unlock(&cache_lock);
    return 0;
  }

  for(p = hashvector[dk & ITEM_HASH_MASK]; p != NULL; p = p->bi_link)
    if(p->bi_key_hash == dk)
      break;

  if(p != NULL && p->bi_content_hash == dc && p->bi_size == size) {
    p->bi_modtime = mtime;
    p->bi_expiry = now + maxage;
    p->bi_lastaccess = now;
    mystrset(&p->bi_etag, etag);
    hts_mutex_unlock(&cache_lock);
    return 1;
  }

  make_filename(filename, sizeof(filename), dk, 1);
  int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  if(fd == -1) {
    hts_mutex_unlock(&cache_lock);
    return 0;
  }

  if(write(fd, data, size) != size) {
    unlink(filename);
    hts_mutex_unlock(&cache_lock);
    return 0;
  }
  close(fd);

  if(p == NULL) {
    p = pool_get(item_pool);
    p->bi_key_hash = dk;
    p->bi_size = 0;
    p->bi_link = hashvector[dk & ITEM_HASH_MASK];
    hashvector[dk & ITEM_HASH_MASK] = p;
  }

  int64_t expiry = (int64_t)maxage + now;

  p->bi_modtime = mtime;
  mystrset(&p->bi_etag, etag);
  p->bi_expiry = MIN(INT32_MAX, expiry);
  p->bi_lastaccess = now;
  p->bi_content_hash = dc;
  current_cache_size -= p->bi_size;
  p->bi_size = size;
  current_cache_size += p->bi_size;

  if(blobcache_compute_maxsize() < current_cache_size &&
     !callout_isarmed(&blobcache_callout))
    callout_arm(&blobcache_callout, blobcache_do_prune, NULL, 5);

  hts_mutex_unlock(&cache_lock);
  return 0;
}


/**
*
*/
void *
blobcache_get(const char *key, const char *stash, size_t *sizep, int pad,
int *ignore_expiry, char **etagp, time_t *mtimep)
{
  uint64_t dk = digest_key(key, stash);
  blobcache_item_t *p, **q;
  char filename[PATH_MAX];
  struct stat st;
  uint32_t now;

  hts_mutex_lock(&cache_lock);

  if(zombie) {
    p = NULL;
  } else {
    for(q = &hashvector[dk & ITEM_HASH_MASK]; (p = *q); q = &p->bi_link)
      if(p->bi_key_hash == dk)
break;
  }

  if(p == NULL) {
    hts_mutex_unlock(&cache_lock);
    return NULL;
  }
  
  now = time(NULL);

  int expired = now > p->bi_expiry;

  if(expired && ignore_expiry == NULL) {
    hts_mutex_unlock(&cache_lock);
    goto bad;
  }

  make_filename(filename, sizeof(filename), p->bi_key_hash, 0);
  int fd = open(filename, O_RDONLY, 0);
  if(fd == -1) {
  bad:
    *q = p->bi_link;
    pool_put(item_pool, p);
    hts_mutex_unlock(&cache_lock);
    return NULL;
  }
  
  if(fstat(fd, &st))
    goto bad;

  if(st.st_size != p->bi_size) {
    unlink(filename);
    goto bad;
  }

  if(mtimep)
    *mtimep = p->bi_modtime;

  if(etagp != NULL)
    *etagp = p->bi_etag ? strdup(p->bi_etag) : NULL;

  p->bi_lastaccess = now;

  hts_mutex_unlock(&cache_lock);

  if(ignore_expiry != NULL)
    *ignore_expiry = expired;

  uint8_t *r = mymalloc(st.st_size + pad);
  if(r == NULL) {
    close(fd);
    return NULL;
  }

  if(read(fd, r, st.st_size) != st.st_size) {
    free(r);
    close(fd);
    return NULL;
  }
  memset(r + st.st_size, 0, pad);
  close(fd);

  *sizep = st.st_size;
  return r;
}





/**
*
*/
int
blobcache_get_meta(const char *key, const char *stash,
char **etagp, time_t *mtimep)
{
  uint64_t dk = digest_key(key, stash);
  blobcache_item_t *p;
  int r;
  hts_mutex_lock(&cache_lock);
  if(zombie) {
    p = NULL;
  } else {
    for(p = hashvector[dk & ITEM_HASH_MASK]; p != NULL; p = p->bi_link)
      if(p->bi_key_hash == dk)
break;
  }

  if(p != NULL) {
    r = 0;

    if(mtimep != NULL)
      *mtimep = p->bi_modtime;

    if(etagp != NULL)
      *etagp = p->bi_etag ? strdup(p->bi_etag) : NULL;

  } else {
    r = -1;
  }

  hts_mutex_unlock(&cache_lock);
  return r;
}


/**
* Assume we're locked
*/
static blobcache_item_t *
lookup_item(uint64_t dk)
{
  blobcache_item_t *p;
  for(p = hashvector[dk & ITEM_HASH_MASK]; p != NULL; p = p->bi_link)
    if(p->bi_key_hash == dk)
      return p;
  return NULL;
}

/**
*
*/
static void
prune_stale(void)
{
  DIR *d1, *d2;
  struct dirent *de1, *de2;
  char path[PATH_MAX];
  char path2[PATH_MAX];
  char path3[PATH_MAX];
  uint64_t k;

  snprintf(path, sizeof(path), "%s/bc2", gconf.cache_path);

  if((d1 = opendir(path)) == NULL)
    return;

  while((de1 = readdir(d1)) != NULL) {
    if(de1->d_name[0] != '.') {
      snprintf(path2, sizeof(path2), "%s/bc2/%s",
gconf.cache_path, de1->d_name);

      if((d2 = opendir(path2)) != NULL) {
while((de2 = readdir(d2)) != NULL) {
          if(de2->d_name[0] != '.') {

snprintf(path3, sizeof(path3), "%s/bc2/%s/%s",
gconf.cache_path, de1->d_name,
de2->d_name);

if(sscanf(de2->d_name, "%016"PRIx64, &k) != 1 ||
lookup_item(k) == NULL) {
TRACE(TRACE_DEBUG, "Blobcache", "Removed stale file %s", path3);
unlink(path3);
}
}
}
closedir(d2);
      }
      rmdir(path2);
    }
  }
  closedir(d1);
}




/**
*
*/
static void
prune_item(blobcache_item_t *p)
{
  char filename[PATH_MAX];
  make_filename(filename, sizeof(filename), p->bi_key_hash, 0);
  unlink(filename);
  pool_put(item_pool, p);
}


/**
*
*/
static int
accesstimecmp(const void *A, const void *B)
{
  const blobcache_item_t *a = *(const blobcache_item_t **)A;
  const blobcache_item_t *b = *(const blobcache_item_t **)B;
  return a->bi_lastaccess - b->bi_lastaccess;
}


/**
*
*/
static void
prune_to_size(void)
{
  int i, tot, j = 0;
  blobcache_item_t *p, **sv;

  hts_mutex_lock(&cache_lock);
  if(zombie)
    goto out;

  uint64_t maxsize = blobcache_compute_maxsize();
  tot = pool_num(item_pool);

  sv = malloc(sizeof(blobcache_item_t *) * tot);
  current_cache_size = 0;
  for(i = 0; i < ITEM_HASH_SIZE; i++) {
    for(p = hashvector[i]; p != NULL; p = p->bi_link) {
      sv[j++] = p;
      current_cache_size += p->bi_size;
    }
    hashvector[i] = NULL;
  }

  assert(j == tot);

  qsort(sv, j, sizeof(blobcache_item_t *), accesstimecmp);
  for(i = 0; i < j; i++) {
    p = sv[i];
    if(current_cache_size < maxsize)
      break;
    current_cache_size -= p->bi_size;
    prune_item(p);
  }

  for(; i < j; i++) {
    p = sv[i];
    p->bi_link = hashvector[p->bi_key_hash & ITEM_HASH_MASK];
    hashvector[p->bi_key_hash & ITEM_HASH_MASK] = p;
  }

  free(sv);
  save_index();
 out:
  hts_mutex_unlock(&cache_lock);
}



/**
*
*/
static void
blobcache_prune_old(void)
{
  DIR *d1, *d2;
  struct dirent *de1, *de2;
  char path[PATH_MAX];
  char path2[PATH_MAX];
  char path3[PATH_MAX];

  snprintf(path, sizeof(path), "%s/blobcache", gconf.cache_path);

  if((d1 = opendir(path)) != NULL) {

    while((de1 = readdir(d1)) != NULL) {
      if(de1->d_name[0] != '.') {
snprintf(path2, sizeof(path2), "%s/blobcache/%s",
gconf.cache_path, de1->d_name);

if((d2 = opendir(path2)) != NULL) {
while((de2 = readdir(d2)) != NULL) {
if(de2->d_name[0] != '.') {

snprintf(path3, sizeof(path3), "%s/blobcache/%s/%s",
gconf.cache_path, de1->d_name,
de2->d_name);
unlink(path3);
}
}
closedir(d2);
}
rmdir(path2);
      }
    }
    closedir(d1);
    rmdir(path);
  }

  snprintf(path, sizeof(path), "%s/cachedb/cache.db", gconf.cache_path);
  unlink(path);
  snprintf(path, sizeof(path), "%s/cachedb/cache.db-shm", gconf.cache_path);
  unlink(path);
  snprintf(path, sizeof(path), "%s/cachedb/cache.db-wal", gconf.cache_path);
  unlink(path);
}

static void
cache_clear(void *opaque, prop_event_t event, ...)
{
  int i;
  blobcache_item_t *p, *n;

  hts_mutex_lock(&cache_lock);

  for(i = 0; i < ITEM_HASH_SIZE; i++) {
    for(p = hashvector[i]; p != NULL; p = n) {
      n = p->bi_link;
      prune_item(p);
    }
    hashvector[i] = NULL;
  }
  current_cache_size = 0;
  save_index();
  hts_mutex_unlock(&cache_lock);
  notify_add(NULL, NOTIFY_INFO, NULL, 3, _("Cache cleared"));
}

/**
*
*/
void
blobcache_init(void)
{
  char buf[256];

  blobcache_prune_old();
  snprintf(buf, sizeof(buf), "%s/bc2", gconf.cache_path);
  if(mkdir(buf, 0777) && errno != EEXIST)
    TRACE(TRACE_ERROR, "blobcache", "Unable to create cache dir %s -- %s",
buf, strerror(errno));

  hts_mutex_init(&cache_lock);
  item_pool = pool_create("blobcacheitems", sizeof(blobcache_item_t), 0);

  load_index();
  prune_stale();
  prune_to_size();
  TRACE(TRACE_INFO, "blobcache",
"Initialized: %d items consuming %"PRId64" bytes on disk in %s",
pool_num(item_pool), current_cache_size, buf);

  settings_create_action(settings_general, _p("Clear cached files"),
cache_clear, NULL, 0, NULL);
}


void
blobcache_fini(void)
{
  hts_mutex_lock(&cache_lock);
  zombie = 1;
  save_index();
  hts_mutex_unlock(&cache_lock);
}

/**
*
*/
static void
blobcache_do_prune(struct callout *c, void *opaque)
{
  prune_to_size();
}
Something went wrong with that request. Please try again.