1 #include "first.h" 2 3 #include "log.h" 4 #include "stat_cache.h" 5 #include "fdevent.h" 6 #include "etag.h" 7 #include "splaytree.h" 8 9 #include <sys/types.h> 10 #include <sys/stat.h> 11 12 #include <stdlib.h> 13 #include <string.h> 14 #include <errno.h> 15 #include <unistd.h> 16 #include <fcntl.h> 17 18 #ifdef HAVE_ATTR_ATTRIBUTES_H 19 # include <attr/attributes.h> 20 #endif 21 22 #ifdef HAVE_SYS_EXTATTR_H 23 # include <sys/extattr.h> 24 #endif 25 26 #ifdef HAVE_FAM_H 27 # include <fam.h> 28 #endif 29 30 #ifndef HAVE_LSTAT 31 # define lstat stat 32 #endif 33 34 #if 0 35 /* enables debug code for testing if all nodes in the stat-cache as accessable */ 36 #define DEBUG_STAT_CACHE 37 #endif 38 39 /* 40 * stat-cache 41 * 42 * we cache the stat() calls in our own storage 43 * the directories are cached in FAM 44 * 45 * if we get a change-event from FAM, we increment the version in the FAM->dir mapping 46 * 47 * if the stat()-cache is queried we check if the version id for the directory is the 48 * same and return immediatly. 49 * 50 * 51 * What we need: 52 * 53 * - for each stat-cache entry we need a fast indirect lookup on the directory name 54 * - for each FAMRequest we have to find the version in the directory cache (index as userdata) 55 * 56 * stat <<-> directory <-> FAMRequest 57 * 58 * if file is deleted, directory is dirty, file is rechecked ... 59 * if directory is deleted, directory mapping is removed 60 * 61 * */ 62 63 #ifdef HAVE_FAM_H 64 typedef struct { 65 FAMRequest *req; 66 67 buffer *name; 68 69 int version; 70 } fam_dir_entry; 71 #endif 72 73 /* the directory name is too long to always compare on it 74 * - we need a hash 75 * - the hash-key is used as sorting criteria for a tree 76 * - a splay-tree is used as we can use the caching effect of it 77 */ 78 79 /* we want to cleanup the stat-cache every few seconds, let's say 10 80 * 81 * - remove entries which are outdated since 30s 82 * - remove entries which are fresh but havn't been used since 60s 83 * - if we don't have a stat-cache entry for a directory, release it from the monitor 84 */ 85 86 #ifdef DEBUG_STAT_CACHE 87 typedef struct { 88 int *ptr; 89 90 size_t used; 91 size_t size; 92 } fake_keys; 93 94 static fake_keys ctrl; 95 #endif 96 97 typedef struct stat_cache { 98 splay_tree *files; /* the nodes of the tree are stat_cache_entry's */ 99 100 buffer *dir_name; /* for building the dirname from the filename */ 101 #ifdef HAVE_FAM_H 102 splay_tree *dirs; /* the nodes of the tree are fam_dir_entry */ 103 104 FAMConnection fam; 105 int fam_fcce_ndx; 106 #endif 107 buffer *hash_key; /* temp-store for the hash-key */ 108 } stat_cache; 109 110 #ifdef HAVE_FAM_H 111 static handler_t stat_cache_handle_fdevent(server *srv, void *_fce, int revent); 112 #endif 113 114 stat_cache *stat_cache_init(server *srv) { 115 stat_cache *sc = NULL; 116 UNUSED(srv); 117 118 sc = calloc(1, sizeof(*sc)); 119 force_assert(NULL != sc); 120 121 sc->dir_name = buffer_init(); 122 sc->hash_key = buffer_init(); 123 124 #ifdef HAVE_FAM_H 125 sc->fam_fcce_ndx = -1; 126 #endif 127 128 #ifdef DEBUG_STAT_CACHE 129 ctrl.size = 0; 130 #endif 131 132 #ifdef HAVE_FAM_H 133 /* setup FAM */ 134 if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM) { 135 if (0 != FAMOpen2(&sc->fam, "lighttpd")) { 136 log_error_write(srv, __FILE__, __LINE__, "s", 137 "could not open a fam connection, dieing."); 138 free(sc); 139 return NULL; 140 } 141 #ifdef HAVE_FAMNOEXISTS 142 FAMNoExists(&sc->fam); 143 #endif 144 145 fdevent_setfd_cloexec(FAMCONNECTION_GETFD(&sc->fam)); 146 fdevent_register(srv->ev, FAMCONNECTION_GETFD(&sc->fam), stat_cache_handle_fdevent, NULL); 147 fdevent_event_set(srv->ev, &(sc->fam_fcce_ndx), FAMCONNECTION_GETFD(&sc->fam), FDEVENT_IN); 148 } 149 #endif 150 151 return sc; 152 } 153 154 static stat_cache_entry * stat_cache_entry_init(void) { 155 stat_cache_entry *sce = NULL; 156 157 sce = calloc(1, sizeof(*sce)); 158 force_assert(NULL != sce); 159 160 sce->name = buffer_init(); 161 sce->etag = buffer_init(); 162 sce->content_type = buffer_init(); 163 164 return sce; 165 } 166 167 static void stat_cache_entry_free(void *data) { 168 stat_cache_entry *sce = data; 169 if (!sce) return; 170 171 buffer_free(sce->etag); 172 buffer_free(sce->name); 173 buffer_free(sce->content_type); 174 175 free(sce); 176 } 177 178 #ifdef HAVE_FAM_H 179 static fam_dir_entry * fam_dir_entry_init(void) { 180 fam_dir_entry *fam_dir = NULL; 181 182 fam_dir = calloc(1, sizeof(*fam_dir)); 183 force_assert(NULL != fam_dir); 184 185 fam_dir->name = buffer_init(); 186 187 return fam_dir; 188 } 189 190 static void fam_dir_entry_free(FAMConnection *fc, void *data) { 191 fam_dir_entry *fam_dir = data; 192 193 if (!fam_dir) return; 194 195 FAMCancelMonitor(fc, fam_dir->req); 196 197 buffer_free(fam_dir->name); 198 free(fam_dir->req); 199 200 free(fam_dir); 201 } 202 #endif 203 204 void stat_cache_free(stat_cache *sc) { 205 while (sc->files) { 206 int osize; 207 splay_tree *node = sc->files; 208 209 osize = sc->files->size; 210 211 stat_cache_entry_free(node->data); 212 sc->files = splaytree_delete(sc->files, node->key); 213 214 force_assert(osize - 1 == splaytree_size(sc->files)); 215 } 216 217 buffer_free(sc->dir_name); 218 buffer_free(sc->hash_key); 219 220 #ifdef HAVE_FAM_H 221 while (sc->dirs) { 222 int osize; 223 splay_tree *node = sc->dirs; 224 225 osize = sc->dirs->size; 226 227 fam_dir_entry_free(&sc->fam, node->data); 228 sc->dirs = splaytree_delete(sc->dirs, node->key); 229 230 if (osize == 1) { 231 force_assert(NULL == sc->dirs); 232 } else { 233 force_assert(osize == (sc->dirs->size + 1)); 234 } 235 } 236 237 if (-1 != sc->fam_fcce_ndx) { 238 /* fd events already gone */ 239 sc->fam_fcce_ndx = -1; 240 241 FAMClose(&sc->fam); 242 } 243 #endif 244 free(sc); 245 } 246 247 #if defined(HAVE_XATTR) 248 static int stat_cache_attr_get(buffer *buf, char *name, char *xattrname) { 249 int attrlen; 250 int ret; 251 252 buffer_string_prepare_copy(buf, 1023); 253 attrlen = buf->size - 1; 254 if(0 == (ret = attr_get(name, xattrname, buf->ptr, &attrlen, 0))) { 255 buffer_commit(buf, attrlen); 256 } 257 return ret; 258 } 259 #elif defined(HAVE_EXTATTR) 260 static int stat_cache_attr_get(buffer *buf, char *name, char *xattrname) { 261 ssize_t attrlen; 262 263 buffer_string_prepare_copy(buf, 1023); 264 265 if (-1 != (attrlen = extattr_get_file(name, EXTATTR_NAMESPACE_USER, xattrname, buf->ptr, buf->size - 1))) { 266 buf->used = attrlen + 1; 267 buf->ptr[attrlen] = '\0'; 268 return 0; 269 } 270 return -1; 271 } 272 #endif 273 274 const buffer * stat_cache_mimetype_by_ext(const connection *con, const char *name, size_t nlen) 275 { 276 const char *end = name + nlen; /*(end of string)*/ 277 const size_t used = con->conf.mimetypes->used; 278 if (used < 16) { 279 for (size_t i = 0; i < used; ++i) { 280 /* suffix match */ 281 const data_string *ds = (data_string *)con->conf.mimetypes->data[i]; 282 const size_t klen = buffer_string_length(ds->key); 283 if (klen <= nlen && 0 == strncasecmp(end-klen, ds->key->ptr, klen)) 284 return ds->value; 285 } 286 } 287 else { 288 const char *s; 289 const data_string *ds; 290 if (nlen) { 291 for (s = end-1; s != name && *s != '/'; --s) ; /*(like memrchr())*/ 292 if (*s == '/') ++s; 293 } 294 else { 295 s = name; 296 } 297 /* search for basename, then longest .ext2.ext1, then .ext1, then "" */ 298 ds = (data_string *)array_get_element_klen(con->conf.mimetypes, s, end - s); 299 if (NULL != ds) return ds->value; 300 while (++s < end) { 301 while (*s != '.' && ++s != end) ; 302 if (s == end) break; 303 /* search ".ext" then "ext" */ 304 ds = (data_string *)array_get_element_klen(con->conf.mimetypes, s, end - s); 305 if (NULL != ds) return ds->value; 306 /* repeat search without leading '.' to handle situation where 307 * admin configured mimetype.assign keys without leading '.' */ 308 if (++s < end) { 309 if (*s == '.') { --s; continue; } 310 ds = (data_string *)array_get_element_klen(con->conf.mimetypes, s, end - s); 311 if (NULL != ds) return ds->value; 312 } 313 } 314 /* search for ""; catchall */ 315 ds = (data_string *)array_get_element(con->conf.mimetypes, ""); 316 if (NULL != ds) return ds->value; 317 } 318 319 return NULL; 320 } 321 322 /* the famous DJB hash function for strings */ 323 static uint32_t hashme(buffer *str) { 324 uint32_t hash = 5381; 325 const char *s; 326 for (s = str->ptr; *s; s++) { 327 hash = ((hash << 5) + hash) + *s; 328 } 329 330 hash &= ~(((uint32_t)1) << 31); /* strip the highest bit */ 331 332 return hash; 333 } 334 335 #ifdef HAVE_FAM_H 336 static handler_t stat_cache_handle_fdevent(server *srv, void *_fce, int revent) { 337 size_t i; 338 stat_cache *sc = srv->stat_cache; 339 size_t events; 340 341 UNUSED(_fce); 342 /* */ 343 344 if (revent & FDEVENT_IN) { 345 events = FAMPending(&sc->fam); 346 347 for (i = 0; i < events; i++) { 348 FAMEvent fe; 349 fam_dir_entry *fam_dir; 350 splay_tree *node; 351 int ndx, j; 352 353 FAMNextEvent(&sc->fam, &fe); 354 355 /* handle event */ 356 357 switch(fe.code) { 358 case FAMChanged: 359 case FAMDeleted: 360 case FAMMoved: 361 /* if the filename is a directory remove the entry */ 362 363 fam_dir = fe.userdata; 364 fam_dir->version++; 365 366 /* file/dir is still here */ 367 if (fe.code == FAMChanged) break; 368 369 /* we have 2 versions, follow and no-follow-symlink */ 370 371 for (j = 0; j < 2; j++) { 372 buffer_copy_string(sc->hash_key, fe.filename); 373 buffer_append_int(sc->hash_key, j); 374 375 ndx = hashme(sc->hash_key); 376 377 sc->dirs = splaytree_splay(sc->dirs, ndx); 378 node = sc->dirs; 379 380 if (node && (node->key == ndx)) { 381 int osize = splaytree_size(sc->dirs); 382 383 fam_dir_entry_free(&sc->fam, node->data); 384 sc->dirs = splaytree_delete(sc->dirs, ndx); 385 386 force_assert(osize - 1 == splaytree_size(sc->dirs)); 387 } 388 } 389 break; 390 default: 391 break; 392 } 393 } 394 } 395 396 if (revent & FDEVENT_HUP) { 397 /* fam closed the connection */ 398 fdevent_event_del(srv->ev, &(sc->fam_fcce_ndx), FAMCONNECTION_GETFD(&sc->fam)); 399 fdevent_unregister(srv->ev, FAMCONNECTION_GETFD(&sc->fam)); 400 401 FAMClose(&sc->fam); 402 } 403 404 return HANDLER_GO_ON; 405 } 406 407 static int buffer_copy_dirname(buffer *dst, buffer *file) { 408 size_t i; 409 410 if (buffer_string_is_empty(file)) return -1; 411 412 for (i = buffer_string_length(file); i > 0; i--) { 413 if (file->ptr[i] == '/') { 414 buffer_copy_string_len(dst, file->ptr, i); 415 return 0; 416 } 417 } 418 419 return -1; 420 } 421 #endif 422 423 #ifdef HAVE_LSTAT 424 static int stat_cache_lstat(server *srv, buffer *dname, struct stat *lst) { 425 if (lstat(dname->ptr, lst) == 0) { 426 return S_ISLNK(lst->st_mode) ? 0 : 1; 427 } 428 else { 429 log_error_write(srv, __FILE__, __LINE__, "sbs", 430 "lstat failed for:", 431 dname, strerror(errno)); 432 }; 433 return -1; 434 } 435 #endif 436 437 /*** 438 * 439 * 440 * 441 * returns: 442 * - HANDLER_FINISHED on cache-miss (don't forget to reopen the file) 443 * - HANDLER_ERROR on stat() failed -> see errno for problem 444 */ 445 446 handler_t stat_cache_get_entry(server *srv, connection *con, buffer *name, stat_cache_entry **ret_sce) { 447 #ifdef HAVE_FAM_H 448 fam_dir_entry *fam_dir = NULL; 449 int dir_ndx = -1; 450 #endif 451 stat_cache_entry *sce = NULL; 452 stat_cache *sc; 453 struct stat st; 454 int fd; 455 struct stat lst; 456 #ifdef DEBUG_STAT_CACHE 457 size_t i; 458 #endif 459 460 int file_ndx; 461 462 *ret_sce = NULL; 463 464 /* 465 * check if the directory for this file has changed 466 */ 467 468 sc = srv->stat_cache; 469 470 buffer_copy_buffer(sc->hash_key, name); 471 buffer_append_int(sc->hash_key, con->conf.follow_symlink); 472 473 file_ndx = hashme(sc->hash_key); 474 sc->files = splaytree_splay(sc->files, file_ndx); 475 476 #ifdef DEBUG_STAT_CACHE 477 for (i = 0; i < ctrl.used; i++) { 478 if (ctrl.ptr[i] == file_ndx) break; 479 } 480 #endif 481 482 if (sc->files && (sc->files->key == file_ndx)) { 483 #ifdef DEBUG_STAT_CACHE 484 /* it was in the cache */ 485 force_assert(i < ctrl.used); 486 #endif 487 488 /* we have seen this file already and 489 * don't stat() it again in the same second */ 490 491 sce = sc->files->data; 492 493 /* check if the name is the same, we might have a collision */ 494 495 if (buffer_is_equal(name, sce->name)) { 496 if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_SIMPLE) { 497 if (sce->stat_ts == srv->cur_ts && con->conf.follow_symlink) { 498 *ret_sce = sce; 499 return HANDLER_GO_ON; 500 } 501 } 502 } else { 503 /* collision, forget about the entry */ 504 sce = NULL; 505 } 506 } else { 507 #ifdef DEBUG_STAT_CACHE 508 if (i != ctrl.used) { 509 log_error_write(srv, __FILE__, __LINE__, "xSB", 510 file_ndx, "was already inserted but not found in cache, ", name); 511 } 512 force_assert(i == ctrl.used); 513 #endif 514 } 515 516 #ifdef HAVE_FAM_H 517 /* dir-check */ 518 if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM) { 519 if (0 != buffer_copy_dirname(sc->dir_name, name)) { 520 log_error_write(srv, __FILE__, __LINE__, "sb", 521 "no '/' found in filename:", name); 522 return HANDLER_ERROR; 523 } 524 525 buffer_copy_buffer(sc->hash_key, sc->dir_name); 526 buffer_append_int(sc->hash_key, con->conf.follow_symlink); 527 528 dir_ndx = hashme(sc->hash_key); 529 530 sc->dirs = splaytree_splay(sc->dirs, dir_ndx); 531 532 if ((NULL != sc->dirs) && (sc->dirs->key == dir_ndx)) { 533 fam_dir = sc->dirs->data; 534 535 /* check whether we got a collision */ 536 if (buffer_is_equal(sc->dir_name, fam_dir->name)) { 537 /* test whether a found file cache entry is still ok */ 538 if ((NULL != sce) && (fam_dir->version == sce->dir_version)) { 539 /* the stat()-cache entry is still ok */ 540 541 *ret_sce = sce; 542 return HANDLER_GO_ON; 543 } 544 } else { 545 /* hash collision, forget about the entry */ 546 fam_dir = NULL; 547 } 548 } 549 } 550 #endif 551 552 /* 553 * *lol* 554 * - open() + fstat() on a named-pipe results in a (intended) hang. 555 * - stat() if regular file + open() to see if we can read from it is better 556 * 557 * */ 558 if (-1 == stat(name->ptr, &st)) { 559 return HANDLER_ERROR; 560 } 561 562 563 if (S_ISREG(st.st_mode)) { 564 /* fix broken stat/open for symlinks to reg files with appended slash on freebsd,osx */ 565 if (name->ptr[buffer_string_length(name) - 1] == '/') { 566 errno = ENOTDIR; 567 return HANDLER_ERROR; 568 } 569 570 /* try to open the file to check if we can read it */ 571 if (-1 == (fd = open(name->ptr, O_RDONLY))) { 572 return HANDLER_ERROR; 573 } 574 close(fd); 575 } 576 577 if (NULL == sce) { 578 579 sce = stat_cache_entry_init(); 580 buffer_copy_buffer(sce->name, name); 581 582 /* already splayed file_ndx */ 583 if ((NULL != sc->files) && (sc->files->key == file_ndx)) { 584 /* hash collision: replace old entry */ 585 stat_cache_entry_free(sc->files->data); 586 sc->files->data = sce; 587 } else { 588 int osize = splaytree_size(sc->files); 589 590 sc->files = splaytree_insert(sc->files, file_ndx, sce); 591 force_assert(osize + 1 == splaytree_size(sc->files)); 592 593 #ifdef DEBUG_STAT_CACHE 594 if (ctrl.size == 0) { 595 ctrl.size = 16; 596 ctrl.used = 0; 597 ctrl.ptr = malloc(ctrl.size * sizeof(*ctrl.ptr)); 598 force_assert(NULL != ctrl.ptr); 599 } else if (ctrl.size == ctrl.used) { 600 ctrl.size += 16; 601 ctrl.ptr = realloc(ctrl.ptr, ctrl.size * sizeof(*ctrl.ptr)); 602 force_assert(NULL != ctrl.ptr); 603 } 604 605 ctrl.ptr[ctrl.used++] = file_ndx; 606 #endif 607 } 608 force_assert(sc->files); 609 force_assert(sc->files->data == sce); 610 } 611 612 sce->st = st; 613 sce->stat_ts = srv->cur_ts; 614 615 /* catch the obvious symlinks 616 * 617 * this is not a secure check as we still have a race-condition between 618 * the stat() and the open. We can only solve this by 619 * 1. open() the file 620 * 2. fstat() the fd 621 * 622 * and keeping the file open for the rest of the time. But this can 623 * only be done at network level. 624 * 625 * per default it is not a symlink 626 * */ 627 #ifdef HAVE_LSTAT 628 sce->is_symlink = 0; 629 630 /* we want to only check for symlinks if we should block symlinks. 631 */ 632 if (!con->conf.follow_symlink) { 633 if (stat_cache_lstat(srv, name, &lst) == 0) { 634 #ifdef DEBUG_STAT_CACHE 635 log_error_write(srv, __FILE__, __LINE__, "sb", 636 "found symlink", name); 637 #endif 638 sce->is_symlink = 1; 639 } 640 641 /* 642 * we assume "/" can not be symlink, so 643 * skip the symlink stuff if our path is / 644 **/ 645 else if (buffer_string_length(name) > 1) { 646 buffer *dname; 647 char *s_cur; 648 649 dname = buffer_init(); 650 buffer_copy_buffer(dname, name); 651 652 while ((s_cur = strrchr(dname->ptr, '/'))) { 653 buffer_string_set_length(dname, s_cur - dname->ptr); 654 if (dname->ptr == s_cur) { 655 #ifdef DEBUG_STAT_CACHE 656 log_error_write(srv, __FILE__, __LINE__, "s", "reached /"); 657 #endif 658 break; 659 } 660 #ifdef DEBUG_STAT_CACHE 661 log_error_write(srv, __FILE__, __LINE__, "sbs", 662 "checking if", dname, "is a symlink"); 663 #endif 664 if (stat_cache_lstat(srv, dname, &lst) == 0) { 665 sce->is_symlink = 1; 666 #ifdef DEBUG_STAT_CACHE 667 log_error_write(srv, __FILE__, __LINE__, "sb", 668 "found symlink", dname); 669 #endif 670 break; 671 }; 672 }; 673 buffer_free(dname); 674 }; 675 }; 676 #endif 677 678 if (S_ISREG(st.st_mode)) { 679 /* determine mimetype */ 680 buffer_reset(sce->content_type); 681 #if defined(HAVE_XATTR) || defined(HAVE_EXTATTR) 682 if (con->conf.use_xattr) { 683 stat_cache_attr_get(sce->content_type, name->ptr, srv->srvconf.xattr_name->ptr); 684 } 685 #endif 686 /* xattr did not set a content-type. ask the config */ 687 if (buffer_string_is_empty(sce->content_type)) { 688 const buffer *type = stat_cache_mimetype_by_ext(con, CONST_BUF_LEN(name)); 689 if (NULL != type) { 690 buffer_copy_buffer(sce->content_type, type); 691 } 692 } 693 etag_create(sce->etag, &(sce->st), con->etag_flags); 694 } else if (S_ISDIR(st.st_mode)) { 695 etag_create(sce->etag, &(sce->st), con->etag_flags); 696 } 697 698 #ifdef HAVE_FAM_H 699 if (srv->srvconf.stat_cache_engine == STAT_CACHE_ENGINE_FAM) { 700 /* is this directory already registered ? */ 701 if (NULL == fam_dir) { 702 fam_dir = fam_dir_entry_init(); 703 704 buffer_copy_buffer(fam_dir->name, sc->dir_name); 705 706 fam_dir->version = 1; 707 708 fam_dir->req = calloc(1, sizeof(FAMRequest)); 709 force_assert(NULL != fam_dir); 710 711 if (0 != FAMMonitorDirectory(&sc->fam, fam_dir->name->ptr, 712 fam_dir->req, fam_dir)) { 713 714 log_error_write(srv, __FILE__, __LINE__, "sbsbs", 715 "monitoring dir failed:", 716 fam_dir->name, 717 "file:", name, 718 FamErrlist[FAMErrno]); 719 720 fam_dir_entry_free(&sc->fam, fam_dir); 721 fam_dir = NULL; 722 } else { 723 int osize = splaytree_size(sc->dirs); 724 725 /* already splayed dir_ndx */ 726 if ((NULL != sc->dirs) && (sc->dirs->key == dir_ndx)) { 727 /* hash collision: replace old entry */ 728 fam_dir_entry_free(&sc->fam, sc->dirs->data); 729 sc->dirs->data = fam_dir; 730 } else { 731 sc->dirs = splaytree_insert(sc->dirs, dir_ndx, fam_dir); 732 force_assert(osize == (splaytree_size(sc->dirs) - 1)); 733 } 734 735 force_assert(sc->dirs); 736 force_assert(sc->dirs->data == fam_dir); 737 } 738 } 739 740 /* bind the fam_fc to the stat() cache entry */ 741 742 if (fam_dir) { 743 sce->dir_version = fam_dir->version; 744 } 745 } 746 #endif 747 748 *ret_sce = sce; 749 750 return HANDLER_GO_ON; 751 } 752 753 int stat_cache_open_rdonly_fstat (server *srv, connection *con, buffer *name, struct stat *st) { 754 /*(Note: O_NOFOLLOW affects only the final path segment, the target file, 755 * not any intermediate symlinks along the path)*/ 756 #ifndef O_BINARY 757 #define O_BINARY 0 758 #endif 759 #ifndef O_LARGEFILE 760 #define O_LARGEFILE 0 761 #endif 762 #ifndef O_NOCTTY 763 #define O_NOCTTY 0 764 #endif 765 #ifndef O_NONBLOCK 766 #define O_NONBLOCK 0 767 #endif 768 #ifndef O_NOFOLLOW 769 #define O_NOFOLLOW 0 770 #endif 771 const int oflags = O_BINARY | O_LARGEFILE | O_NOCTTY | O_NONBLOCK 772 | (con->conf.follow_symlink ? 0 : O_NOFOLLOW); 773 const int fd = open(name->ptr, O_RDONLY | oflags); 774 if (fd >= 0) { 775 if (0 == fstat(fd, st)) { 776 return fd; 777 } else { 778 close(fd); 779 } 780 } 781 UNUSED(srv); /*(might log_error_write(srv, ...) in the future)*/ 782 return -1; 783 } 784 785 /** 786 * remove stat() from cache which havn't been stat()ed for 787 * more than 10 seconds 788 * 789 * 790 * walk though the stat-cache, collect the ids which are too old 791 * and remove them in a second loop 792 */ 793 794 static int stat_cache_tag_old_entries(server *srv, splay_tree *t, int *keys, size_t *ndx) { 795 stat_cache_entry *sce; 796 797 if (!t) return 0; 798 799 stat_cache_tag_old_entries(srv, t->left, keys, ndx); 800 stat_cache_tag_old_entries(srv, t->right, keys, ndx); 801 802 sce = t->data; 803 804 if (srv->cur_ts - sce->stat_ts > 2) { 805 keys[(*ndx)++] = t->key; 806 } 807 808 return 0; 809 } 810 811 int stat_cache_trigger_cleanup(server *srv) { 812 stat_cache *sc; 813 size_t max_ndx = 0, i; 814 int *keys; 815 816 sc = srv->stat_cache; 817 818 if (!sc->files) return 0; 819 820 keys = calloc(1, sizeof(int) * sc->files->size); 821 force_assert(NULL != keys); 822 823 stat_cache_tag_old_entries(srv, sc->files, keys, &max_ndx); 824 825 for (i = 0; i < max_ndx; i++) { 826 int ndx = keys[i]; 827 splay_tree *node; 828 829 sc->files = splaytree_splay(sc->files, ndx); 830 831 node = sc->files; 832 833 if (node && (node->key == ndx)) { 834 #ifdef DEBUG_STAT_CACHE 835 size_t j; 836 int osize = splaytree_size(sc->files); 837 stat_cache_entry *sce = node->data; 838 #endif 839 stat_cache_entry_free(node->data); 840 sc->files = splaytree_delete(sc->files, ndx); 841 842 #ifdef DEBUG_STAT_CACHE 843 for (j = 0; j < ctrl.used; j++) { 844 if (ctrl.ptr[j] == ndx) { 845 ctrl.ptr[j] = ctrl.ptr[--ctrl.used]; 846 break; 847 } 848 } 849 850 force_assert(osize - 1 == splaytree_size(sc->files)); 851 #endif 852 } 853 } 854 855 free(keys); 856 857 return 0; 858 } 859