Skip to content
This repository
tree: 3a72b07a33
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 890 lines (726 sloc) 23.074 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 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
// -*-c++-*-

#include "pub3hilev.h"
#include "pub3prot.h"
#include "crypt.h"
#include "pub3hilev.h"
#include "pub3out.h"

static str
trim_null_bytes (str s)
{
  const char *c = s;
  if (s.len () != strlen (c)) s = c;
  return s;
}

namespace pub3 {

  typedef abstract_publisher_t ap_t;
  typedef remote_publisher_t rp_t;
  typedef caching_remote_publisher_t crp_t;
  typedef local_publisher_t lp_t;

  //=======================================================================

  static void
  status2evec (const xpub_status_t &res, vec<str> *out)
  {
    if (res.status == XPUB_STATUS_RPC_ERR) {
      strbuf b;
      b << "RPC error:" << clnt_stat (*(res.rpc_err));
      out->push_back (b);
    } else if (res.status == XPUB_STATUS_NOENT ||
res.status == XPUB_STATUS_EPARSE ||
res.status == XPUB_STATUS_EIO) {
      for (size_t i = 0; i < res.errors->size (); i++) {
out->push_back ((*res.errors)[i]);
      }
    } else {
      out->push_back (*res.error);
    }
  }

  //=======================================================================

  bool
  localizer_t::localize_many (const str &infn, vec<str> *out) const
  { return false; }

  //-----------------------------------------------------------------------

  str localizer_t::localize (const str &infn) const { return NULL; }

  //-----------------------------------------------------------------------

  void
  localizer_t::compound_localize (const str &infn, vec<str> *out) const
  {
    str res;
    
    if (!localize_many(infn, out))
      res = localize(infn);
    if (res)
      out->push_back(res);
  }

  //=======================================================================

  abstract_publisher_t::abstract_publisher_t (opts_t o,
ptr<const localizer_t> l) :
    _opts (o),
    _pub3_err_obj_key (ok_pub3_err_obj_key),
    _localizer (l) {}

  //-----------------------------------------------------------------------

  ptr<expr_dict_t> ok_iface_t::get_universals ()
  { return singleton_t::get ()->universals (); }

  //-----------------------------------------------------------------------

  obj_t ok_iface_t::get_universals_obj ()
  { return pub3::obj_dict_t (get_universals ()); }

  //-----------------------------------------------------------------------

  obj_dict_t ok_iface_t::pub3_config_obj ()
  { return pub3::obj_dict_t (get_universals ()); }

  //-----------------------------------------------------------------------

  void
  ap_t::init_for_run (eval_t *p, opts_t opts, ptr<expr_dict_t> d)
  {
    if (opts < 0) opts = _opts;
    
    p->set_opts (opts);
    p->set_pub_iface (mkref (this));

    if (d) {
      p->env ()->bind_globals (d);
    }

    // In "placeholders" mode, there's a little red placeholder published
    // and then the error object is published into the page, for the
    // designer to style as desired.
    if ((opts & P_OUTPUT_ERR_OBJ) && _pub3_err_obj_key) {
      p->add_err_obj (_pub3_err_obj_key);
    }
  }

  //-----------------------------------------------------------------------

  void
  ap_t::uninit_for_run (eval_t *p)
  {
    p->set_pub_iface (NULL);
  }

  //-----------------------------------------------------------------------
  
  tamed void
  ap_t::run (zbuf *b, str fn, evb_t ev, ptr<expr_dict_t> d, opts_t opts,
status_t *sp, ptr<file_t> *fp)
  {
    tvars {
      holdvar ptr<bindtab_t> unis (singleton_t::get ()->universals ());
      ptr<eval_t> pub (New refcounted<eval_t> (unis, b, opts));
      status_t status;
      ptr<file_t> file;
    }

    init_for_run (pub, opts, d);
    
    twait { publish (pub, fn, mkevent (status, file)); }

    if (sp) *sp = status;
    if (fp) *fp = file;

    uninit_for_run (pub);

    ev->trigger (status.status == XPUB_STATUS_OK);
  }

  //-----------------------------------------------------------------------

  tamed void
  ap_t::run_cfg (str fn, evb_t ev, ptr<expr_dict_t> d_in, opts_t o,
status_t *sp)
  {
    tvars {
      holdvar ptr<bindtab_t> unis (singleton_t::get ()->universals ());
      ptr<expr_dict_t> d (d_in ? d_in : expr_dict_t::alloc ());
      opts_t opts ((o < 0 ? _self->opts () : o) | P_CONFIG);
      ptr<eval_t> pub (New refcounted<eval_t> (unis, d, opts));
      status_t status;
      ptr<file_t> file;
    }
    pub->set_pub_iface (mkref (this));

    twait { publish (pub, fn, mkevent (status, file)); }

    if (sp) *sp = status;
    
    pub->set_pub_iface (NULL);

    // Finally, if requested, copy the universal pub information
    // back into the universals slot...
    if (!d_in || (opts & P_COPY_CONF)) {
      *(singleton_t::get ()->universals ()) += *d;
    }
    
    ev->trigger (status.status == XPUB_STATUS_OK);
  }
  
  //-----------------------------------------------------------------------
  
  tamed void
  ap_t::publish_prepare (eval_t *pub, str fn, str *rfn, str *errp,
getfile_ev_t ev)
  {
    tvars {
      status_t status (XPUB_STATUS_NOENT);
      ptr<file_t> file;
      str err;
      vec<str> files_to_check;
      size_t i;
      ptr<const localizer_t> localizer;
    }

    // generate a list of internationalized files. if that list is
    // empty, we'll skip the following loop and we'll just
    // fall through with status.status == XPUB_STATUS_NOENT
    localizer = get_localizer (pub);
    list_files_to_check (pub->cwd (), fn, &files_to_check, localizer);

    for (i = 0; i < files_to_check.size (); i++) {

      twait {
getfile (files_to_check[i], mkevent (status, file), pub->opts ());
      }
      if (status.status != XPUB_STATUS_NOENT) {
break;
      }
    }

    switch (status.status) {
    case XPUB_STATUS_OK:
      assert (file);
      assert (i < files_to_check.size ());
      *rfn = files_to_check[i];
      break;
    case XPUB_STATUS_RPC_ERR:
      err = strbuf () << fn << ": RPC error: " << *status.rpc_err;
      break;
    case XPUB_STATUS_NOENT:
      err = strbuf () << fn << ": file not found";
      break;
    case XPUB_STATUS_EIO:
    case XPUB_STATUS_EPARSE:
      pub->output_errs (*status.errors, P_ERR_ERROR);
      break;
    default:
      err = *status.error;
      break;
    }
    *errp = err;

    ev->trigger (status, file);
  }

  //-----------------------------------------------------------------------

  tamed void
  ap_t::publish (eval_t *pub, str fn, getfile_ev_t ev)
  {
    tvars {
      status_t status (XPUB_STATUS_NOENT);
      ptr<file_t> file;
      str err;
      str d, f, cwd_prev, rfn;
    }

    twait { publish_prepare (pub, fn, &rfn, &err, mkevent (status, file)); }
    if (status.status == XPUB_STATUS_OK) {
      assert (file);
      file->metadata ()->set_input_filename (fn);
      basename_dirname (rfn, &d, &f);

      cwd_prev = pub->set_cwd (d);

      twait { pub->publish_file (file, mkevent (status)); }
      if (status.status != XPUB_STATUS_OK) {
err = strbuf () << "error in publishing file: " << fn;
      }

      // Reset the working directory to whatever it was previously.
      pub->set_cwd (cwd_prev);
    }
    if (err) { pub->output_err (err, P_ERR_ERROR); }
    ev->trigger (status, file);
  }
  
  //-----------------------------------------------------------------------
  
  static void use_file (str f, vec<str> *out) { if (f) out->push_back (f); }
  
  //-----------------------------------------------------------------------
  
  void
  ap_t::list_files_to_check (str cwd, str nm, vec<str> *out,
ptr<const localizer_t> l)
  {
    str d,f;
    bool abs = basename_dirname (nm, &d, &f);
    if (!f) return;
    bool use_defs = !(l && l->strict ()) ;
    
    if (abs) {
      if (l) l->compound_localize (nm, out);
      if (use_defs) use_file (nm, out);
    } else {
      
      str n1, n2;
      strbuf b;
      if (is_remote ()) b << "/";
      b << nm;
      n1 = b;
      
      if (cwd) {
n2 = apply_container_dir (cwd, nm);
      }

      if (l) {
if (n1) l->compound_localize (n1, out);
if (n2) l->compound_localize (n2, out);
      }
 
      if (use_defs) {
if (n1) use_file (n1, out);
if (n2) use_file (n2, out);
      }
    }

    if (l) { use_file (l->get_default_fnf (), out); }
  }

  //-----------------------------------------------------------------------

  ptr<const localizer_t>
  ap_t::get_localizer (eval_t *p)
  {
    ptr<const localizer_t> ret;
    if (!(p->opts () & P_NOLOCALE)) {
      ret = _localizer;
    }
    return ret;
  }

  //-----------------------------------------------------------------------

  tamed void
  ap_t::syntax_check (str f, vec<str> *err, evi_t ev)
  {
    tvars {
      xpub_status_t res;
      ptr<file_t> dummy;
      int rc (0);
    }
    twait { getfile (f, mkevent (res, dummy), 0); }
    if (res.status != XPUB_STATUS_OK) {
      status2evec (res, err);
      rc = -1;
    } else {
      rc = 0;
    }
    ev->trigger (rc);
  }
  
  //=======================================================================
  
  remote_publisher_t::remote_publisher_t (ptr<axprt_stream> x, opts_t o)
    : ap_t (o),
      _x (x),
      _cli (aclnt::alloc (x, pub_prog_3)),
      _srv (asrv::alloc (x, pub_prog_3, wrap (this, &rp_t::dispatch))),
      _maxsz (min<size_t> (ok_pub3_max_datasz, ok_axprt_ps >> 1)) {}

  //-----------------------------------------------------------------------
  
  tamed void
  rp_t::connect (evb_t ev)
  {
    tvars {
      clnt_stat err;
      bool ret (true);
    }
    twait { rpc::pub_prog_3::pub3_null (_cli, mkevent (err)); }
    if (err) {
      warn << "NULL call to pubd failed: " << err << "\n";
      ret = false;
    }
    ev->trigger (ret);
  }

  //-----------------------------------------------------------------------
  
  void rp_t::dispatch (svccb *sbp)
  {
    if (!sbp) {
      lost_connection ();
      warn << "connection to pubd unexpectedly died!\n";
      exit (0);
    } else {
      switch (sbp->proc ()) {
      case PUB3_PUSH_DELTAS:
handle_new_deltas (sbp);
break;
      default:
sbp->reject (PROC_UNAVAIL);
break;
      }
    }
  }

  //-----------------------------------------------------------------------
  
  tamed void
  rp_t::getchunk (const xpub3_hash_t &key, opts_t opts, size_t offset,
size_t sz, char *buf, evb_t ev)
  {
    tvars {
      clnt_stat err;
      xpub3_getchunk_arg_t arg;
      xpub3_getchunk_res_t res;
      bool ok (false);
      strbuf b;
    }
    memcpy (arg.hash.base (), key.base (), PUBHASHSIZE);
    arg.offset = offset;
    arg.size = sz;
    arg.opts = opts;

    twait { rpc::pub_prog_3::pub3_getchunk (_cli, arg, &res, mkevent (err) ); }

    if (err) {
      b << "RPC error in PUB3_GETCHUNK: " << err << "\n";
      OKDBG3(PUB3_CHUNKS, ERROR, b);

    } else if (res.status != XPUB_STATUS_OK) {
      b << "PUB error in PUB3_GETCHUNK: " << res.status;
      if (res.status == XPUB_STATUS_ERR)
b << ": " << *res.error;
      OKDBG3(PUB3_CHUNKS, ERROR, b);

    } else if (res.chunk->data.size () != sz) {
      OKDBG3(PUB3_CHUNKS, ERROR,
"Error in PUB3_GETCHUNK: datasize mismatch");
      
    } else {
      ok = true;
      memcpy (buf + offset, res.chunk->data.base (), res.chunk->data.size ());

      if (OKDBG2(PUB3_CHUNKS)) {
strbuf b ("get_chunk(");
rpc_print (b, arg);
char buf[PUBHASHSIZE];
sha1_hash (buf, res.chunk->data.base (), res.chunk->data.size ());
str fp = armor32 (buf, PUBHASHSIZE);
b.fmt (") -> %s", fp.cstr ());
okdbg_warn (CHATTER, b);
      }
    }
    ev->trigger (ok);
  }

  //-----------------------------------------------------------------------
  
  void rp_t::handle_new_deltas (svccb *sbp)
  {
    rpc::pub_prog_3::pub3_push_deltas_srv_t<svccb> srv (sbp);
    srv.reply (false);
  }

  //-----------------------------------------------------------------------
  
  tamed void
  rp_t::getfile_chunked (const xpub3_chunkshdr_t &hdr, opts_t opts,
xpub3_file_t *file, status_ev_t ev)
  {
    tvars {
      status_t status (XPUB_STATUS_OK);
      size_t dsz (hdr.datasize);
      mstr buf (dsz);
      rendezvous_t<ptr<bool> > rv (__FILE__, __LINE__);
      bool ok (true);
      str m;
      size_t cs;
      size_t i (0);
      size_t nsent (0), nrecv (0);
      size_t ws (ok_pub3_chunk_window_size);
      ptr<bool> nok;
    }

    memset (buf, 0xcd, dsz);

    for ( ; (i < dsz || nrecv < nsent) && ok;) {

      if (i < dsz && nsent - nrecv < ws) {
nsent ++;
cs = min<size_t> (_maxsz, dsz - i);
nok = New refcounted<bool> (true);

getchunk (hdr.dathash, opts, i, cs, buf.cstr (),
mkevent (rv, nok, *nok));
i += _maxsz;
      } else {
twait (rv, nok);
nrecv++;
if (!*nok) ok = false;
      }
    }

    buf.setlen (dsz);

    if (!ok) {
      rv.cancel ();
      m = "Not all chunks retrieved";
    } else {
      char h[PUBHASHSIZE];
      sha1_hash (h, buf.cstr (), buf.len ());
      if (memcmp (h, hdr.xdrhash.base (), PUBHASHSIZE) != 0) {
m = "Hash mismatch";
      } else if (!str2xdr (*file, buf)) {
m = "Demarshall of chunks failed";
      }
      if (OKDBG2(PUB3_CHUNKS)) {
str s1 = armor32 (h, PUBHASHSIZE);
str s2 = armor32 (hdr.xdrhash.base (), PUBHASHSIZE);
if (m) {
okdbg_warn (ERROR, "hash mismatch (%s v %s)\n",
s1.cstr (), s2.cstr ());
} else {
okdbg_warn (CHATTER, "hash match on %s\n", s1.cstr ());
}
      }
    }

    if (m) {
      status.set_status (XPUB_STATUS_CORRUPTION);
      *status.error = m;
    }

    ev->trigger (status);
  }

  //-----------------------------------------------------------------------
  
  tamed void
  rp_t::getfile_body (str nm, const xpub3_getfile_res_t *res,
getfile_ev_t ev, opts_t opts)
  {
    tvars {
      ptr<file_t> out;
      status_t status (XPUB_STATUS_OK);
      const xpub3_file_t *filep (NULL);
      xpub3_file_t file;
    }
    if (res->file->mode == XPUB_XFER_WHOLE) {
      filep = res->file->whole;
    } else if (res->file->mode == XPUB_XFER_CHUNKED) {
      twait {
getfile_chunked (*res->file->chunked, opts, &file, mkevent (status));
      }
      if (status.status == XPUB_STATUS_OK) {
filep = &file;
      }
    } else {
      status.set_status (XPUB_STATUS_ERR);
      *status.error = "Unknown transfer mode";
    }

    if (filep) { out = file_t::alloc (*filep, opts); }

    ev->trigger (status, out);
  }

  //-----------------------------------------------------------------------
  
  bool
  rp_t::prepare_getfile (const cache_key_t &k, xpub3_getfile_arg_t *arg,
ptr<file_t> *f, status_t *status)
  {
    // We've had some attacks wherein attackers ask for files of the
    // form "/fooo\0\0\0\0". This causes an XDR failure since filenames
    // are XDR strings and not opaques. The quick solution is just to
    // trim off any trailing null bytes and stop the string at the first
    // one, upholding the required assertion.
    arg->filename = trim_null_bytes (k.fn ());

    arg->options = k.opts();
    arg->fresh.set_mode (XPUB3_FRESH_NONE);
    arg->maxsz = _maxsz;
    return false;
  }

  //-----------------------------------------------------------------------

  void rp_t::getfile (str fn, getfile_ev_t ev, opts_t o)
  { getfile_T (fn, ev, o); }
  
  //-----------------------------------------------------------------------

  tamed void
  rp_t::getfile_T (str nm, getfile_ev_t ev, opts_t opts)
  {
    tvars {
      clnt_stat err;
      ptr<file_t> file;
      xpub3_getfile_arg_t arg;
      xpub3_getfile_res_t res;
      status_t status (XPUB_STATUS_OK);
      cache_key_t k (nm, opts);
    }
    if (!prepare_getfile (k, &arg, &file, &status)) {
      twait { rpc::pub_prog_3::pub3_getfile(_cli, arg, &res, mkevent (err)); }
      if (err) {
warn << nm << ": error in RPC call to PUB2_GETFILE: "
<< err << "\n";
status.set_status(XPUB_STATUS_RPC_ERR);
*status.rpc_err = err;
      } else {

switch (res.code) {

case XPUB_STATUS_OK:
twait { getfile_body (nm, &res, mkevent (status, file), opts); }
if (status.status == XPUB_STATUS_OK) {
cache_getfile (k, file);
}
break;

case XPUB_STATUS_NOCHANGE:
file = file_nochange (k);
assert (file);
break;

case XPUB_STATUS_NOENT:
{
cache_noent (nm);
strbuf b;
b << nm << ": file not found";

// it's OK to fail here, since we can be checking for many
// files in the path.
// warn << b << "\n";

status.set_status (XPUB_STATUS_NOENT);
status.errors->push_back (b);
}
break;

default:
status = *res.error_status;
break;
}
      }
    }
    ev->trigger (status, file);
  }

  //=======================================================================

  local_publisher_t::local_publisher_t (ptr<pub_parser_t> p,
opts_t opts,
ptr<file_lookup_t> l,
ptr<jailer_t> j)
    : abstract_publisher_t (opts),
      _parser (p ? p : pub_parser_t::alloc ()),
      _lookup (l ? l : file_lookup_t::alloc ()),
      _jailer (j ? j : jailer_t::alloc ()) {}
  
  //-----------------------------------------------------------------------

  void
  lp_t::getfile (str fn, getfile_ev_t ev, opts_t o)
  {
    xpub3_file_freshcheck_t frsh (XPUB3_FRESH_NONE);
    getfile (fn, ev, frsh, o);
  }

  //-----------------------------------------------------------------------

  void
  lp_t::getfile (str jfn, getfile_ev_t ev,
const xpub3_file_freshcheck_t &fresh, opts_t o)
  {
    str rfn = _jailer->jail2real (jfn);

    ptr<fhash_t> hsh;
    ptr<file_t> f;
    time_t ctime;
    xpub_status_t res (XPUB_STATUS_OK);
    opts_t opt_mask (P_NOPARSE);
    
    struct ::stat sb;
    
    // Store the jailed file names in the cache!
    if (!_lookup->lookup (jfn, &hsh, &ctime)) {

      // This might need to go over the network
      hsh = file2hash (rfn, &sb);
      
      // pass 'timenow' (not sb.st_ctime) as the ctime of the file, since
      // this file is the most up-to-date as of right now.
      time_t now = okwstime ();
      ctime = now;
      _lookup->cache_lookup (jfn, rfn, hsh, now, sb.st_size);
    }
    
    if (hsh) {
      
      // It could be that the asked for a new file only if we have one
      // fresher than the given arguments.
      if ((fresh.mode == XPUB3_FRESH_CTIME && ctime < time_t (*fresh.ctime)) ||
(fresh.mode == XPUB3_FRESH_HASH && *hsh == *fresh.hash)) {

res.set_status (XPUB_STATUS_NOCHANGE);

      } else {

parse_status_t s;
str em;
parse_ret_t pr;

ptr<metadata_t> meta = New refcounted<metadata_t> (jfn, rfn, hsh);
if (!_lookup->getfile (hsh, o, &f, &s, &em)) {

// this might need to go over the network, too,
// if we're bypassing NFS
if (_parser->parse (meta, &pr, o & opt_mask)) {
f = pr.file ();
meta->set_ctime (ctime);
_lookup->cache_getfile (hsh, o, f, PARSE_OK, NULL);
}
}

if (!pr.ok ()) {
assert (!f);
pr.to_xdr (&res);
}
      }
    } else {
      res.set_status (XPUB_STATUS_NOENT);
    }
    ev->trigger (res, f);
  }

  //=======================================================================
  
  bool
  crp_t::prepare_getfile (const cache_key_t &k, xpub3_getfile_arg_t *arg,
ptr<file_t> *f, status_t *status)
  {
    cached_getfile_t *c = _getfile_cache[k];
    str s = k.fn ();
    if (c) {
      *f = c->file ();
      return true;
    } else if (_noent_cache[s]) {
      status->set_status (XPUB_STATUS_NOENT);
      strbuf b;
      b << s << ": file not found (failure cached)";
      status->errors->push_back (b);
      *f = NULL;
      return true;
    } else {
      if (OKDBG2(PUB3_CACHE)) {
str tmp = k.fn ();
if (!tmp) tmp = "<NONE>";
okdbg_warn (CHATTER, "cache MISS: %s:%x\n", tmp.cstr (), k.opts ());
      }
      return rp_t::prepare_getfile (k, arg, f, status);
    }
  }

  //----------------------------------------------------------------------

  tamed void
  crp_t::connect (evb_t ev)
  {
    tvars {
      clnt_stat stat;
      bool ret (true);
    }

    twait {
      rpc::pub_prog_3::pub3_get_pushes (_cli, &_connected, mkevent (stat));
    }

    if (stat) {
      warn << "Intial RPC call to pubd failed: " << stat << "\n";
      ret = false;
    } else if (!_connected) {
      warn << "pubd denied request for file status pushes!\n";
    }
    ev->trigger (ret);
  }

  //-----------------------------------------------------------------------

  bool
  crp_t::invalidate_cache (str n)
  {
    bool ret = false;
    ptr<bhash<opts_t> > *h = _opts_map[n];
    if (h) {

      ret = true;
      assert (*h);
      bhash_const_iterator_t<opts_t> it (**h);
      const opts_t *o;
      while ((o = it.next ())) { rm_file (n, *o); }
      _opts_map.remove (n);

      if (OKDBG2(PUB3_CACHE)) {
strbuf b;
b << "Removing file '" << n << "' from GETFILE cache";
okdbg_warn (CHATTER, b);
      }
    }
    return ret;

  }


  //-----------------------------------------------------------------------
  
  void
  crp_t::handle_new_deltas (const xpub3_delta_set_t &s)
  {
    if (_delta_id >= 0 && s.serial != _delta_id + 1) {
      okdbg_warn (ERROR, "Missed a delta update! Clearing cache!");
      _opts_map.clear ();
      _getfile_cache.clear ();
    } else {
      _delta_id = s.serial;
      for (size_t i = 0; i < s.files.size (); i++) {
str n = s.files[i];
invalidate_cache (n);
      }
    }
  }

  //-----------------------------------------------------------------------

  void
  crp_t::cache_noent (str s)
  {
    _noent_cache.insert (s);
    invalidate_cache (s);
  }
  
  //-----------------------------------------------------------------------
  
  bool
  crp_t::is_cached (str n, opts_t o, const fhash_t &h) const
  {
    cache_key_t k (n, o);
    const cached_getfile_t *c = _getfile_cache[k];
    return c && h == c->file ()->metadata ()->hash ();
  }

  //-----------------------------------------------------------------------
  
  void
  crp_t::handle_new_deltas (svccb *sbp)
  {
    rpc::pub_prog_3::pub3_push_deltas_srv_t<svccb> srv (sbp);
    const xpub3_delta_set_t *s = srv.getarg ();
    handle_new_deltas (*s);
    srv.reply (true);
  }

  //-----------------------------------------------------------------------

  void crp_t::lost_connection ()
  {
    _connected = false;
    clear_cache ();
  }

  //-----------------------------------------------------------------------
  
  void
  crp_t::cache_getfile (const cache_key_t &k, ptr<file_t> f)
  {
    //
    // Make a mapping of filenames -> options, so that we know which
    // cache entries are associated with a given filename (in case it's
    // later removed from the cache.
    //
    ptr<bhash<opts_t> > *hp, h;
    if (!( hp = _opts_map[k.fn ()] )) {
      h = New refcounted<bhash<opts_t> > ();
      _opts_map.insert (k.fn (), h);
      hp = &h;
    }
    (*hp)->insert (k.opts ());
    
    OKDBG4(PUB3_CACHE, CHATTER,
"cache INSERT: %s:%x\n", k.fn ().cstr (), k.opts ());
    
    _noent_cache.remove (k.fn ());
    _getfile_cache.insert (cached_getfile_t (k, f));
  }
  

  //-----------------------------------------------------------------------
  
  void crp_t::rm_file (str nm, opts_t opt)
  {
    cache_key_t k (nm, opt);
    _getfile_cache.remove (k);
  }

  //=======================================================================

};
Something went wrong with that request. Please try again.