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) {
755 log_log(log, op, "allocation failure");
758 if ((fd = open(log->path, O_RDONLY, 0)) < 0) {
759 log_log(log, op, ".add file read failure");
765 log_log(log, op, ".add file read failure");
768 log_log(log, op, "loaded .add file");
771 log_log(log, op, "missing .add file!");
777 ret = log_append(log, data, len);
780 ret = log_compress(log, data, len);
783 ret = log_replace(log);
786 /* log status */
787 log_log(log, op, ret ? "failure" : "complete");
796 local void log_close(struct log *log)
798 if (log->fd >= 0)
799 close(log->fd);
800 log->fd = -1;
801 log_unlock(log);
807 extra, and body content of an empty foo.gz log file. If there is an error
809 writing the foo.gz file, or if the foo.gz file is not a proper log file for
813 local int log_open(struct log *log)
819 if (log->fd >= 0)
820 close(log->fd);
821 log->fd = -1;
824 if (log_lock(log) < 0)
827 /* open the log file, foo.gz */
828 strcpy(log->end, ".gz");
829 log->fd = open(log->path, O_RDWR | O_CREAT, 0644);
830 if (log->fd < 0) {
831 log_close(log);
835 /* if new, initialize foo.gz with an empty log, delete old dictionary */
836 if (lseek(log->fd, 0, SEEK_END) == 0) {
837 if (write(log->fd, log_gzhead, HEAD) != HEAD ||
838 write(log->fd, log_gzext, EXTRA) != EXTRA ||
839 write(log->fd, log_gzbody, BODY) != BODY) {
840 log_close(log);
843 strcpy(log->end, ".dict");
844 unlink(log->path);
847 /* verify log file and load extra field information */
848 if ((op = log_head(log)) < 0) {
849 log_close(log);
854 if (op != NO_OP && log_recover(log, op)) {
855 log_close(log);
860 log_touch(log);
868 struct log *log;
874 /* allocate and initialize log structure */
875 log = malloc(sizeof(struct log));
876 if (log == NULL)
878 strcpy(log->id, LOGID);
879 log->fd = -1;
883 log->path = malloc(n + 9); /* allow for ".repairs" */
884 if (log->path == NULL) {
885 free(log);
888 strcpy(log->path, path);
889 log->end = log->path + n;
891 /* gain exclusive access and verify log file -- may perform a
893 if (log_open(log)) {
894 free(log->path);
895 free(log);
899 /* return pointer to log structure */
900 return log;
907 -3: invalid log pointer argument */
914 struct log *log = logd;
917 if (log == NULL || strcmp(log->id, LOGID))
923 if (log_check(log) && log_open(log))
927 len = ((size_t)(log->last - log->first) & ~(((size_t)1 << 10) - 1)) +
928 log->stored;
935 if (lseek(log->fd, log->first - 1, SEEK_SET) < 0)
939 if (read(log->fd, buf, 5) != 5)
943 read(log->fd, (char *)data + next, block) != block)
947 if (lseek(log->fd, 0, SEEK_CUR) != log->last + 4 + log->stored)
949 log_touch(log);
952 strcpy(log->end, ".add");
953 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
959 log_touch(log);
962 strcpy(log->end, ".temp");
963 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
970 log_touch(log);
973 log->last = log->first;
974 log->stored = 0;
975 if (log_mark(log, COMPRESS_OP))
980 ret = log_compress(log, data, len);
994 -3: invalid log pointer argument */
998 struct log *log = logd;
1001 if (log == NULL || strcmp(log->id, LOGID))
1009 if (log_check(log) && log_open(log))
1013 strcpy(log->end, ".add");
1014 fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1020 log_touch(log);
1022 /* mark log file with append in progress */
1023 if (log_mark(log, APPEND_OP))
1028 if (log_append(log, data, len))
1032 if (((log->last - log->first) >> 10) + (log->stored >> 10) < TRIGGER)
1036 return gzlog_compress(log);
1041 -3: invalid log pointer argument */
1044 struct log *log = logd;
1047 if (log == NULL || strcmp(log->id, LOGID))
1050 /* close the log file and release the lock */
1051 log_close(log);
1054 if (log->path != NULL)
1055 free(log->path);
1056 strcpy(log->id, "bad");
1057 free(log);