Lines Matching refs:log

30    Files (log name "foo"):
31 foo.gz -- gzip file with the complete log
36 foo.repairs -- log file for log file recovery operations (not compressed)
83 gain exclusive access to the log files, first a foo.lock file must be
287 /* internal structure for log information */
289 struct log {
292 char *path; /* allocated path, e.g. "/var/log/foo" or "foo" */
348 local int log_lock(struct log *log)
353 strcpy(log->end, ".lock");
354 while ((fd = open(log->path, O_CREAT | O_EXCL, 0644)) < 0) {
357 if (stat(log->path, &st) == 0 && time(NULL) - st.st_mtime > PATIENCE) {
358 unlink(log->path);
364 if (stat(log->path, &st) == 0)
365 log->lock = st.st_mtime;
372 local void log_touch(struct log *log)
376 strcpy(log->end, ".lock");
377 utimes(log->path, NULL);
378 if (stat(log->path, &st) == 0)
379 log->lock = st.st_mtime;
382 /* Check the log file modify time against what is expected. Return true if
384 local int log_check(struct log *log)
388 strcpy(log->end, ".lock");
389 if (stat(log->path, &st) || st.st_mtime != log->lock)
391 log_touch(log);
396 local void log_unlock(struct log *log)
398 if (log_check(log))
400 strcpy(log->end, ".lock");
401 unlink(log->path);
402 log->lock = 0;
406 the log structure. Return op on success or -1 if the gzip header was not as
409 file descriptor log->fd. */
410 local int log_head(struct log *log)
415 if (lseek(log->fd, 0, SEEK_SET) < 0 ||
416 read(log->fd, buf, HEAD + EXTRA) != HEAD + EXTRA ||
420 log->first = PULL8(buf + HEAD);
421 log->last = PULL8(buf + HEAD + 8);
422 log->ccrc = PULL4(buf + HEAD + 16);
423 log->clen = PULL4(buf + HEAD + 20);
424 log->tcrc = PULL4(buf + HEAD + 24);
425 log->tlen = PULL4(buf + HEAD + 28);
426 log->stored = PULL2(buf + HEAD + 32);
427 log->back = 3 + (buf[HEAD + 34] & 7);
435 assure that the log is recoverable in the event of an interruption at any
437 local int log_mark(struct log *log, int op)
442 PUT8(ext, log->first);
443 PUT8(ext + 8, log->last);
444 PUT4(ext + 16, log->ccrc);
445 PUT4(ext + 20, log->clen);
446 PUT4(ext + 24, log->tcrc);
447 PUT4(ext + 28, log->tlen);
448 PUT2(ext + 32, log->stored);
449 ext[34] = log->back - 3 + (op << 3);
450 fsync(log->fd);
451 ret = lseek(log->fd, HEAD, SEEK_SET) < 0 ||
452 write(log->fd, ext, EXTRA) != EXTRA ? -1 : 0;
453 fsync(log->fd);
462 local int log_last(struct log *log, int last)
468 back = log->last == log->first ? log->back : 8;
469 len = back > 8 ? 2 : 1; /* bytes back from log->last */
476 if (back != 8 && (lseek(log->fd, log->last - len, SEEK_SET) < 0 ||
477 read(log->fd, buf, 1) != 1))
489 PUT2(buf + 2, log->stored);
490 PUT2(buf + 4, log->stored ^ 0xffff);
491 return lseek(log->fd, log->last - len, SEEK_SET) < 0 ||
492 write(log->fd, buf + 2 - len, len + 4) != len + 4 ||
493 lseek(log->fd, log->stored, SEEK_CUR) < 0 ? -1 : 0;
496 /* Append len bytes from data to the locked and open log file. len may be zero
501 local int log_append(struct log *log, unsigned char *data, size_t len)
510 if (log_last(log, 1))
517 put = (MAX_STORE << 10) - log->stored;
521 if (write(log->fd, data, put) != put)
524 log->tcrc = crc32(log->tcrc, data, put);
525 log->tlen += put;
526 log->stored += put;
534 if (log_last(log, 0))
538 log->last += 4 + log->stored + 1;
539 log->stored = 0;
543 if (log_last(log, 1))
550 PUT4(buf, log->tcrc);
551 PUT4(buf + 4, log->tlen);
552 if (write(log->fd, buf, 8) != 8 ||
553 (end = lseek(log->fd, 0, SEEK_CUR)) < 0 || ftruncate(log->fd, end))
556 /* write the extra field, marking the log file as done, delete .add file */
557 if (log_mark(log, NO_OP))
559 strcpy(log->end, ".add");
560 unlink(log->path); /* ignore error, since may not exist */
571 local int log_replace(struct log *log)
577 strcpy(log->end, ".add");
578 unlink(log->path); /* ignore error, since may not exist */
582 strcpy(log->end, ".dict");
583 dest = malloc(strlen(log->path) + 1);
586 strcpy(dest, log->path);
587 strcpy(log->end, ".temp");
588 ret = rename(log->path, dest);
595 return log_mark(log, NO_OP);
606 reading or writing the log.gz file failed, or -2 if there was a memory
608 local int log_compress(struct log *log, unsigned char *data, size_t len)
628 strcpy(log->end, ".dict");
629 fd = open(log->path, O_RDONLY, 0);
640 log_touch(log);
644 if (lseek(log->fd, log->first - (log->back > 8 ? 2 : 1),
646 read(log->fd, buf, 1) != 1 || lseek(log->fd, -1, SEEK_CUR) < 0) {
650 deflatePrime(&strm, (8 - log->back) & 7, *buf);
663 if (got && write(log->fd, buf, got) != got) {
667 log_touch(log);
677 if ((log->first = lseek(log->fd, -1, SEEK_CUR)) < 0 ||
678 read(log->fd, buf, 1) != 1)
680 log->first++;
682 log->back = 1;
683 while ((*buf & ((uint)1 << (8 - log->back++))) == 0)
687 log->back = 10;
690 log->ccrc = log->tcrc;
691 log->clen = log->tlen;
695 log->tcrc = log->ccrc;
696 log->tlen = log->clen;
700 log->last = log->first;
701 log->stored = 0;
702 PUT4(buf, log->tcrc);
703 PUT4(buf + 4, log->tlen);
704 if (log_last(log, 1) || write(log->fd, buf, 8) != 8 ||
705 (end = lseek(log->fd, 0, SEEK_CUR)) < 0 || ftruncate(log->fd, end))
710 if (log_mark(log, REPLACE_OP))
714 return log_replace(log);
717 /* log a repair record to the .repairs file */
718 local void log_log(struct log *log, int op, char *record)
724 strcpy(log->end, ".repairs");
725 rec = fopen(log->path, "a");
738 local int log_recover(struct log *log, int op)
745 /* log recovery */
746 log_log(log, op, "start");
750 strcpy(log->end, ".add");
751 if (stat(log->path, &st) == 0 && st.st_size) {
754 log_log(log, op, "allocation failure");
757 if ((fd = open(log->path, O_RDONLY, 0)) < 0) {
758 log_log(log, op, ".add file read failure");
764 log_log(log, op, ".add file read failure");
767 log_log(log, op, "loaded .add file");
770 log_log(log, op, "missing .add file!");
776 ret = log_append(log, data, len);
779 ret = log_compress(log, data, len);
782 ret = log_replace(log);
785 /* log status */
786 log_log(log, op, ret ? "failure" : "complete");
795 local void log_close(struct log *log)
797 if (log->fd >= 0)
798 close(log->fd);
799 log->fd = -1;
800 log_unlock(log);
806 extra, and body content of an empty foo.gz log file. If there is an error
808 writing the foo.gz file, or if the foo.gz file is not a proper log file for
812 local int log_open(struct log *log)
818 if (log->fd >= 0)
819 close(log->fd);
820 log->fd = -1;
823 if (log_lock(log) < 0)
826 /* open the log file, foo.gz */
827 strcpy(log->end, ".gz");
828 log->fd = open(log->path, O_RDWR | O_CREAT, 0644);
829 if (log->fd < 0) {
830 log_close(log);
834 /* if new, initialize foo.gz with an empty log, delete old dictionary */
835 if (lseek(log->fd, 0, SEEK_END) == 0) {
836 if (write(log->fd, log_gzhead, HEAD) != HEAD ||
837 write(log->fd, log_gzext, EXTRA) != EXTRA ||
838 write(log->fd, log_gzbody, BODY) != BODY) {
839 log_close(log);
842 strcpy(log->end, ".dict");
843 unlink(log->path);
846 /* verify log file and load extra field information */
847 if ((op = log_head(log)) < 0) {
848 log_close(log);
853 if (op != NO_OP && log_recover(log, op)) {
854 log_close(log);
859 log_touch(log);
867 struct log *log;
873 /* allocate and initialize log structure */
874 log = malloc(sizeof(struct log));
875 if (log == NULL)
877 strcpy(log->id, LOGID);
878 log->fd = -1;
882 log->path = malloc(n + 9); /* allow for ".repairs" */
883 if (log->path == NULL) {
884 free(log);
887 strcpy(log->path, path);
888 log->end = log->path + n;
890 /* gain exclusive access and verify log file -- may perform a
892 if (log_open(log)) {
893 free(log->path);
894 free(log);
898 /* return pointer to log structure */
899 return log;
906 -3: invalid log pointer argument */
913 struct log *log = logd;
916 if (log == NULL || strcmp(log->id, LOGID) || len < 0)
922 if (log_check(log) && log_open(log))
926 len = ((size_t)(log->last - log->first) & ~(((size_t)1 << 10) - 1)) +
927 log->stored;
934 if (lseek(log->fd, log->first - 1, SEEK_SET) < 0)
938 if (read(log->fd, buf, 5) != 5)
942 read(log->fd, (char *)data + next, block) != block)
946 if (lseek(log->fd, 0, SEEK_CUR) != log->last + 4 + log->stored)
948 log_touch(log);
951 strcpy(log->end, ".add");
952 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
958 log_touch(log);
961 strcpy(log->end, ".temp");
962 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
969 log_touch(log);
972 log->last = log->first;
973 log->stored = 0;
974 if (log_mark(log, COMPRESS_OP))
979 ret = log_compress(log, data, len);
993 -3: invalid log pointer argument */
997 struct log *log = logd;
1000 if (log == NULL || strcmp(log->id, LOGID) || len < 0)
1008 if (log_check(log) && log_open(log))
1012 strcpy(log->end, ".add");
1013 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1019 log_touch(log);
1021 /* mark log file with append in progress */
1022 if (log_mark(log, APPEND_OP))
1027 if (log_append(log, data, len))
1031 if (((log->last - log->first) >> 10) + (log->stored >> 10) < TRIGGER)
1035 return gzlog_compress(log);
1040 -3: invalid log pointer argument */
1043 struct log *log = logd;
1046 if (log == NULL || strcmp(log->id, LOGID))
1049 /* close the log file and release the lock */
1050 log_close(log);
1053 if (log->path != NULL)
1054 free(log->path);
1055 strcpy(log->id, "bad");
1056 free(log);