forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
380 lines (323 loc) · 7.3 KB
/
file.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "dat.h"
static void warnpos(File*, char*, ...);
static int readrec(File*, job, int*);
static int readfull(File*, void*, int, int*, char*);
void
fileincref(File *f)
{
if (!f) return;
f->refs++;
}
void
filedecref(File *f)
{
if (!f) return;
f->refs--;
if (f->refs < 1) {
walgc(f->w);
}
}
void
fileaddjob(File *f, job j)
{
job h;
h = &f->jlist;
if (!h->fprev) h->fprev = h;
j->file = f;
j->fprev = h->fprev;
j->fnext = h;
h->fprev->fnext = j;
h->fprev = j;
fileincref(f);
}
void
filermjob(File *f, job j)
{
if (!f) return;
if (f != j->file) return;
j->fnext->fprev = j->fprev;
j->fprev->fnext = j->fnext;
j->fnext = 0;
j->fprev = 0;
j->file = NULL;
f->w->alive -= j->walused;
j->walused = 0;
filedecref(f);
}
// Fileread reads jobs from f->path into list.
// It returns 0 on success, or 1 if any errors occurred.
int
fileread(File *f, job list)
{
int err = 0, v;
if (!readfull(f, &v, sizeof(v), &err, "version")) {
return err;
}
if (v != Walver) {
warnx("%s: wrong version: want %d, got %d", f->path, Walver, v);
return 1;
}
fileincref(f);
while (readrec(f, list, &err));
filedecref(f);
return err;
}
// Readrec reads a record from f->fd into linked list l.
// If an error occurs, it sets *err to 1.
// Readrec returns the number of records read, either 1 or 0.
static int
readrec(File *f, job l, int *err)
{
int r, sz = 0;
int namelen;
Jobrec jr;
job j;
tube t;
char tubename[MAX_TUBE_NAME_LEN];
r = read(f->fd, &namelen, sizeof(int));
if (r == -1) {
twarn("read");
warnpos(f, "error");
*err = 1;
return 0;
}
if (r != sizeof(int)) {
return 0;
}
sz += r;
if (namelen >= MAX_TUBE_NAME_LEN) {
warnpos(f, "namelen %d exceeds maximum of %d", namelen, MAX_TUBE_NAME_LEN - 1);
*err = 1;
return 0;
}
if (namelen) {
r = readfull(f, tubename, namelen, err, "tube name");
if (!r) {
return 0;
}
sz += r;
}
tubename[namelen] = '\0';
r = readfull(f, &jr, sizeof(Jobrec), err, "job struct");
if (!r) {
return 0;
}
sz += r;
// are we reading trailing zeroes?
if (!jr.id) return 0;
j = job_find(jr.id);
if (!(j || namelen)) {
// We read a short record without having seen a
// full record for this job, so the full record
// was in an eariler file that has been deleted.
// Therefore the job itself has either been
// deleted or migrated; either way, this record
// should be ignored.
return 1;
}
switch (jr.state) {
case Reserved:
jr.state = Ready;
case Ready:
case Buried:
case Delayed:
if (!j) {
t = tube_find_or_make(tubename);
j = make_job_with_id(jr.pri, jr.delay, jr.ttr, jr.body_size,
t, jr.id);
j->next = j->prev = j;
j->r.created_at = jr.created_at;
}
j->r = jr;
job_insert(l, j);
// full record; read the job body
if (namelen) {
if (jr.body_size != j->r.body_size) {
warnpos(f, "job %llu size changed", j->r.id);
warnpos(f, "was %zu, now %zu", j->r.body_size, jr.body_size);
goto Error;
}
r = readfull(f, j->body, j->r.body_size, err, "job body");
if (!r) {
goto Error;
}
sz += r;
// since this is a full record, we can move
// the file pointer and decref the old
// file, if any
filermjob(j->file, j);
fileaddjob(f, j);
}
j->walused += sz;
f->w->alive += sz;
return 1;
case Invalid:
if (j) {
job_remove(j);
filermjob(j->file, j);
job_free(j);
}
return 1;
}
Error:
*err = 1;
if (j) {
job_remove(j);
filermjob(j->file, j);
job_free(j);
}
return 0;
}
static int
readfull(File *f, void *c, int n, int *err, char *desc)
{
int r;
r = read(f->fd, c, n);
if (r == -1) {
twarn("read");
warnpos(f, "error reading %s", desc);
*err = 1;
return 0;
}
if (r != n) {
warnpos(f, "unexpected EOF reading %s", desc);
*err = 1;
return 0;
}
return r;
}
static void
warnpos(File *f, char *fmt, ...)
{
int off;
va_list ap;
off = lseek(f->fd, 0, SEEK_CUR);
fprintf(stderr, "%s:%u: ", f->path, off);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
// Opens f for writing, writes a header, and initializes
// f->free and f->resv.
// Sets f->iswopen if successful.
void
filewopen(File *f)
{
int fd, r;
int n;
int ver = Walver;
fd = open(f->path, O_WRONLY|O_CREAT, 0400);
if (fd < 0) {
twarn("open %s", f->path);
return;
}
r = falloc(fd, f->w->filesz);
if (r) {
close(fd);
errno = r;
twarn("Cannot allocate space for file %s", f->path);
return;
}
n = write(fd, &ver, sizeof(int));
if (n < sizeof(int)) {
twarn("write %s", f->path);
close(fd);
return;
}
f->fd = fd;
f->iswopen = 1;
fileincref(f);
f->free = f->w->filesz - n;
f->resv = 0;
}
static int
filewrite(File *f, job j, void *buf, int len)
{
int r;
r = write(f->fd, buf, len);
if (r != len) {
twarn("write");
return 0;
}
f->w->resv -= r;
f->resv -= r;
j->walresv -= r;
j->walused += r;
f->w->alive += r;
return 1;
}
int
filewrjobshort(File *f, job j)
{
int r, nl;
nl = 0; // name len 0 indicates short record
r = filewrite(f, j, &nl, sizeof nl) &&
filewrite(f, j, &j->r, sizeof j->r);
if (!r) return 0;
if (j->r.state == Invalid) {
filermjob(j->file, j);
}
return r;
}
int
filewrjobfull(File *f, job j)
{
int nl;
fileaddjob(f, j);
nl = strlen(j->tube->name);
return
filewrite(f, j, &nl, sizeof nl) &&
filewrite(f, j, j->tube->name, nl) &&
filewrite(f, j, &j->r, sizeof j->r) &&
filewrite(f, j, j->body, j->r.body_size);
}
void
filewclose(File *f)
{
int r;
if (!f) return;
if (!f->iswopen) return;
if (f->free) {
// Some compilers give a warning if the return value of ftruncate is
// ignored. So we pretend to use it.
r = ftruncate(f->fd, f->w->filesz - f->free);
if (r == -1); // do nothing
}
close(f->fd);
f->iswopen = 0;
filedecref(f);
}
int
fileinit(File *f, Wal *w, int n)
{
f->w = w;
f->seq = n;
f->path = fmtalloc("%s/binlog.%d", w->dir, n);
return !!f->path;
}
// Adds f to the linked list in w,
// updating w->tail and w->head as necessary.
Wal*
fileadd(File *f, Wal *w)
{
if (w->tail) {
w->tail->next = f;
}
w->tail = f;
if (!w->head) {
w->head = f;
}
w->nfile++;
return w;
}