~mcf/samurai

ref: edeec43d638c826d9e446917f97e95151988e0e0 samurai/log.c -rw-r--r-- 3.2 KiB
edeec43dMichael Forney log: Use fgets instead of getline 2 years ago
                                                                                
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
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph.h"
#include "log.h"
#include "util.h"

static FILE *logfile;
static const char *logname = ".ninja_log";
static const char *logtmpname = ".ninja_log.tmp";
static const char *logfmt = "# ninja log v%d\n";
static const int logver = 5;

static char *
nextfield(char **end)
{
	char *s = *end;

	if (!*s) {
		warn("corrupt build log: missing field");
		return NULL;
	}
	*end += strcspn(*end, "\t\n");
	if (**end)
		*(*end)++ = '\0';

	return s;
}

void
loginit(const char *builddir)
{
	int ver;
	char *logpath = (char *)logname, *logtmppath = (char *)logtmpname, *p, *s;
	size_t nline, nentry, i;
	struct edge *e;
	struct node *n;
	int64_t mtime;
	struct buffer buf = {0};

	nline = 0;
	nentry = 0;

	if (logfile)
		fclose(logfile);
	if (builddir)
		xasprintf(&logpath, "%s/%s", builddir, logname);
	logfile = fopen(logpath, "r+");
	if (!logfile) {
		if (errno != ENOENT)
			fatal("open %s:", logpath);
		goto rewrite;
	}
	setvbuf(logfile, NULL, _IOLBF, 0);
	if (fscanf(logfile, logfmt, &ver) < 1)
		goto rewrite;
	if (ver != logver)
		goto rewrite;

	for (;;) {
		if (buf.cap - buf.len < BUFSIZ) {
			buf.cap = buf.cap ? buf.cap * 2 : BUFSIZ;
			buf.data = xreallocarray(buf.data, buf.cap, 1);
		}
		buf.data[buf.cap - 2] = '\0';
		if (!fgets(buf.data + buf.len, buf.cap - buf.len, logfile))
			break;
		if (buf.data[buf.cap - 2] && buf.data[buf.cap - 2] != '\n') {
			buf.len = buf.cap - 1;
			continue;
		}
		++nline;
		p = buf.data;
		buf.len = 0;
		if (!nextfield(&p))  /* start time */
			continue;
		if (!nextfield(&p))  /* end time */
			continue;
		s = nextfield(&p);  /* mtime (used for restat) */
		if (!s)
			continue;
		mtime = strtoll(s, &s, 10);
		if (*s) {
			warn("corrupt build log: invalid mtime");
			continue;
		}
		s = nextfield(&p);  /* output path */
		if (!s)
			continue;
		n = nodeget(s, 0);
		if (!n || !n->gen)
			continue;
		if (n->logmtime == MTIME_MISSING)
			++nentry;
		n->logmtime = mtime;
		s = nextfield(&p);  /* command hash */
		if (!s)
			continue;
		n->hash = strtoull(s, &s, 16);
		if (*s) {
			warn("corrupt build log: invalid hash for '%s'", n->path->s);
			continue;
		}
	}
	free(buf.data);
	if (ferror(logfile)) {
		warn("build log read:");
		goto rewrite;
	}
	if (nline <= 100 || nline <= 3 * nentry) {
		if (builddir)
			free(logpath);
		return;
	}

rewrite:
	if (logfile)
		fclose(logfile);
	if (builddir)
		xasprintf(&logtmppath, "%s/%s", builddir, logtmpname);
	logfile = fopen(logtmppath, "w");
	if (!logfile)
		fatal("open %s:", logtmppath);
	setvbuf(logfile, NULL, _IOLBF, 0);
	fprintf(logfile, logfmt, logver);
	if (nentry > 0) {
		for (e = alledges; e; e = e->allnext) {
			for (i = 0; i < e->nout; ++i) {
				n = e->out[i];
				if (!n->hash)
					continue;
				logrecord(n);
			}
		}
	}
	fflush(logfile);
	if (ferror(logfile))
		fatal("build log write failed");
	if (rename(logtmppath, logpath) < 0)
		fatal("build log rename:");
	if (builddir) {
		free(logpath);
		free(logtmppath);
	}
}

void
logclose(void)
{
	fflush(logfile);
	if (ferror(logfile))
		fatal("build log write failed");
	fclose(logfile);
}

void
logrecord(struct node *n)
{
	fprintf(logfile, "0\t0\t%" PRId64 "\t%s\t%" PRIx64 "\n", n->logmtime, n->path->s, n->hash);
}