Lines Matching refs:log
289 struct log { struct
348 local int log_lock(struct log *log) in log_lock() argument
353 strcpy(log->end, ".lock"); in log_lock()
354 while ((fd = open(log->path, O_CREAT | O_EXCL, 0644)) < 0) { in log_lock()
357 if (stat(log->path, &st) == 0 && time(NULL) - st.st_mtime > PATIENCE) { in log_lock()
358 unlink(log->path); in log_lock()
364 if (stat(log->path, &st) == 0) in log_lock()
365 log->lock = st.st_mtime; in log_lock()
372 local void log_touch(struct log *log) in log_touch() argument
376 strcpy(log->end, ".lock"); in log_touch()
377 utimes(log->path, NULL); in log_touch()
378 if (stat(log->path, &st) == 0) in log_touch()
379 log->lock = st.st_mtime; in log_touch()
384 local int log_check(struct log *log) in log_check() argument
388 strcpy(log->end, ".lock"); in log_check()
389 if (stat(log->path, &st) || st.st_mtime != log->lock) in log_check()
391 log_touch(log); in log_check()
396 local void log_unlock(struct log *log) in log_unlock() argument
398 if (log_check(log)) in log_unlock()
400 strcpy(log->end, ".lock"); in log_unlock()
401 unlink(log->path); in log_unlock()
402 log->lock = 0; in log_unlock()
410 local int log_head(struct log *log) in log_head() argument
415 if (lseek(log->fd, 0, SEEK_SET) < 0 || in log_head()
416 read(log->fd, buf, HEAD + EXTRA) != HEAD + EXTRA || in log_head()
420 log->first = PULL8(buf + HEAD); in log_head()
421 log->last = PULL8(buf + HEAD + 8); in log_head()
422 log->ccrc = PULL4(buf + HEAD + 16); in log_head()
423 log->clen = PULL4(buf + HEAD + 20); in log_head()
424 log->tcrc = PULL4(buf + HEAD + 24); in log_head()
425 log->tlen = PULL4(buf + HEAD + 28); in log_head()
426 log->stored = PULL2(buf + HEAD + 32); in log_head()
427 log->back = 3 + (buf[HEAD + 34] & 7); in log_head()
437 local int log_mark(struct log *log, int op) in log_mark() argument
442 PUT8(ext, log->first); in log_mark()
443 PUT8(ext + 8, log->last); in log_mark()
444 PUT4(ext + 16, log->ccrc); in log_mark()
445 PUT4(ext + 20, log->clen); in log_mark()
446 PUT4(ext + 24, log->tcrc); in log_mark()
447 PUT4(ext + 28, log->tlen); in log_mark()
448 PUT2(ext + 32, log->stored); in log_mark()
449 ext[34] = log->back - 3 + (op << 3); in log_mark()
450 fsync(log->fd); in log_mark()
451 ret = lseek(log->fd, HEAD, SEEK_SET) < 0 || in log_mark()
452 write(log->fd, ext, EXTRA) != EXTRA ? -1 : 0; in log_mark()
453 fsync(log->fd); in log_mark()
462 local int log_last(struct log *log, int last) in log_last() argument
468 back = log->last == log->first ? log->back : 8; in log_last()
476 if (back != 8 && (lseek(log->fd, log->last - len, SEEK_SET) < 0 || in log_last()
477 read(log->fd, buf, 1) != 1)) in log_last()
489 PUT2(buf + 2, log->stored); in log_last()
490 PUT2(buf + 4, log->stored ^ 0xffff); in log_last()
491 return lseek(log->fd, log->last - len, SEEK_SET) < 0 || in log_last()
492 write(log->fd, buf + 2 - len, len + 4) != len + 4 || in log_last()
493 lseek(log->fd, log->stored, SEEK_CUR) < 0 ? -1 : 0; in log_last()
501 local int log_append(struct log *log, unsigned char *data, size_t len) in log_append() argument
510 if (log_last(log, 1)) in log_append()
517 put = (MAX_STORE << 10) - log->stored; in log_append()
521 if (write(log->fd, data, put) != put) in log_append()
524 log->tcrc = crc32(log->tcrc, data, put); in log_append()
525 log->tlen += put; in log_append()
526 log->stored += put; in log_append()
534 if (log_last(log, 0)) in log_append()
538 log->last += 4 + log->stored + 1; in log_append()
539 log->stored = 0; in log_append()
543 if (log_last(log, 1)) in log_append()
550 PUT4(buf, log->tcrc); in log_append()
551 PUT4(buf + 4, log->tlen); in log_append()
552 if (write(log->fd, buf, 8) != 8 || in log_append()
553 (end = lseek(log->fd, 0, SEEK_CUR)) < 0 || ftruncate(log->fd, end)) in log_append()
557 if (log_mark(log, NO_OP)) in log_append()
559 strcpy(log->end, ".add"); in log_append()
560 unlink(log->path); /* ignore error, since may not exist */ in log_append()
571 local int log_replace(struct log *log) in log_replace() argument
577 strcpy(log->end, ".add"); in log_replace()
578 unlink(log->path); /* ignore error, since may not exist */ in log_replace()
582 strcpy(log->end, ".dict"); in log_replace()
583 dest = malloc(strlen(log->path) + 1); in log_replace()
586 strcpy(dest, log->path); in log_replace()
587 strcpy(log->end, ".temp"); in log_replace()
588 ret = rename(log->path, dest); in log_replace()
595 return log_mark(log, NO_OP); in log_replace()
608 local int log_compress(struct log *log, unsigned char *data, size_t len) in log_compress() argument
628 strcpy(log->end, ".dict"); in log_compress()
629 fd = open(log->path, O_RDONLY, 0); in log_compress()
640 log_touch(log); in log_compress()
644 if (lseek(log->fd, log->first - (log->back > 8 ? 2 : 1), in log_compress()
646 read(log->fd, buf, 1) != 1 || lseek(log->fd, -1, SEEK_CUR) < 0) { in log_compress()
650 deflatePrime(&strm, (8 - log->back) & 7, *buf); in log_compress()
663 if (got && write(log->fd, buf, got) != got) { in log_compress()
667 log_touch(log); in log_compress()
677 if ((log->first = lseek(log->fd, -1, SEEK_CUR)) < 0 || in log_compress()
678 read(log->fd, buf, 1) != 1) in log_compress()
680 log->first++; in log_compress()
682 log->back = 1; in log_compress()
683 while ((*buf & ((uint)1 << (8 - log->back++))) == 0) in log_compress()
687 log->back = 10; in log_compress()
690 log->ccrc = log->tcrc; in log_compress()
691 log->clen = log->tlen; in log_compress()
695 log->tcrc = log->ccrc; in log_compress()
696 log->tlen = log->clen; in log_compress()
700 log->last = log->first; in log_compress()
701 log->stored = 0; in log_compress()
702 PUT4(buf, log->tcrc); in log_compress()
703 PUT4(buf + 4, log->tlen); in log_compress()
704 if (log_last(log, 1) || write(log->fd, buf, 8) != 8 || in log_compress()
705 (end = lseek(log->fd, 0, SEEK_CUR)) < 0 || ftruncate(log->fd, end)) in log_compress()
710 if (log_mark(log, REPLACE_OP)) in log_compress()
714 return log_replace(log); in log_compress()
718 local void log_log(struct log *log, int op, char *record) in log_log() argument
724 strcpy(log->end, ".repairs"); in log_log()
725 rec = fopen(log->path, "a"); in log_log()
738 local int log_recover(struct log *log, int op) in log_recover() argument
746 log_log(log, op, "start"); in log_recover()
750 strcpy(log->end, ".add"); in log_recover()
751 if (stat(log->path, &st) == 0 && st.st_size) { in log_recover()
755 log_log(log, op, "allocation failure"); in log_recover()
758 if ((fd = open(log->path, O_RDONLY, 0)) < 0) { in log_recover()
759 log_log(log, op, ".add file read failure"); in log_recover()
765 log_log(log, op, ".add file read failure"); in log_recover()
768 log_log(log, op, "loaded .add file"); in log_recover()
771 log_log(log, op, "missing .add file!"); in log_recover()
777 ret = log_append(log, data, len); in log_recover()
780 ret = log_compress(log, data, len); in log_recover()
783 ret = log_replace(log); in log_recover()
787 log_log(log, op, ret ? "failure" : "complete"); in log_recover()
796 local void log_close(struct log *log) in log_close() argument
798 if (log->fd >= 0) in log_close()
799 close(log->fd); in log_close()
800 log->fd = -1; in log_close()
801 log_unlock(log); in log_close()
813 local int log_open(struct log *log) in log_open() argument
819 if (log->fd >= 0) in log_open()
820 close(log->fd); in log_open()
821 log->fd = -1; in log_open()
824 if (log_lock(log) < 0) in log_open()
828 strcpy(log->end, ".gz"); in log_open()
829 log->fd = open(log->path, O_RDWR | O_CREAT, 0644); in log_open()
830 if (log->fd < 0) { in log_open()
831 log_close(log); in log_open()
836 if (lseek(log->fd, 0, SEEK_END) == 0) { in log_open()
837 if (write(log->fd, log_gzhead, HEAD) != HEAD || in log_open()
838 write(log->fd, log_gzext, EXTRA) != EXTRA || in log_open()
839 write(log->fd, log_gzbody, BODY) != BODY) { in log_open()
840 log_close(log); in log_open()
843 strcpy(log->end, ".dict"); in log_open()
844 unlink(log->path); in log_open()
848 if ((op = log_head(log)) < 0) { in log_open()
849 log_close(log); in log_open()
854 if (op != NO_OP && log_recover(log, op)) { in log_open()
855 log_close(log); in log_open()
860 log_touch(log); in log_open()
868 struct log *log; in gzlog_open() local
875 log = malloc(sizeof(struct log)); in gzlog_open()
876 if (log == NULL) in gzlog_open()
878 strcpy(log->id, LOGID); in gzlog_open()
879 log->fd = -1; in gzlog_open()
883 log->path = malloc(n + 9); /* allow for ".repairs" */ in gzlog_open()
884 if (log->path == NULL) { in gzlog_open()
885 free(log); in gzlog_open()
888 strcpy(log->path, path); in gzlog_open()
889 log->end = log->path + n; in gzlog_open()
893 if (log_open(log)) { in gzlog_open()
894 free(log->path); in gzlog_open()
895 free(log); in gzlog_open()
900 return log; in gzlog_open()
914 struct log *log = logd; in gzlog_compress() local
917 if (log == NULL || strcmp(log->id, LOGID)) in gzlog_compress()
923 if (log_check(log) && log_open(log)) in gzlog_compress()
927 len = ((size_t)(log->last - log->first) & ~(((size_t)1 << 10) - 1)) + in gzlog_compress()
928 log->stored; in gzlog_compress()
935 if (lseek(log->fd, log->first - 1, SEEK_SET) < 0) in gzlog_compress()
939 if (read(log->fd, buf, 5) != 5) in gzlog_compress()
943 read(log->fd, (char *)data + next, block) != block) in gzlog_compress()
947 if (lseek(log->fd, 0, SEEK_CUR) != log->last + 4 + log->stored) in gzlog_compress()
949 log_touch(log); in gzlog_compress()
952 strcpy(log->end, ".add"); in gzlog_compress()
953 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644); in gzlog_compress()
959 log_touch(log); in gzlog_compress()
962 strcpy(log->end, ".temp"); in gzlog_compress()
963 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644); in gzlog_compress()
970 log_touch(log); in gzlog_compress()
973 log->last = log->first; in gzlog_compress()
974 log->stored = 0; in gzlog_compress()
975 if (log_mark(log, COMPRESS_OP)) in gzlog_compress()
980 ret = log_compress(log, data, len); in gzlog_compress()
998 struct log *log = logd; in gzlog_write() local
1001 if (log == NULL || strcmp(log->id, LOGID)) in gzlog_write()
1009 if (log_check(log) && log_open(log)) in gzlog_write()
1013 strcpy(log->end, ".add"); in gzlog_write()
1014 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644); in gzlog_write()
1020 log_touch(log); in gzlog_write()
1023 if (log_mark(log, APPEND_OP)) in gzlog_write()
1028 if (log_append(log, data, len)) in gzlog_write()
1032 if (((log->last - log->first) >> 10) + (log->stored >> 10) < TRIGGER) in gzlog_write()
1036 return gzlog_compress(log); in gzlog_write()
1044 struct log *log = logd; in gzlog_close() local
1047 if (log == NULL || strcmp(log->id, LOGID)) in gzlog_close()
1051 log_close(log); in gzlog_close()
1054 if (log->path != NULL) in gzlog_close()
1055 free(log->path); in gzlog_close()
1056 strcpy(log->id, "bad"); in gzlog_close()
1057 free(log); in gzlog_close()