forked from omniti-labs/jlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlog.c
1576 lines (1409 loc) · 42.9 KB
/
jlog.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2005-2008, Message Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name Message Systems, Inc. nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************
Journaled logging... append only.
(1) find current file, or allocate a file, extendible and mark
it current.
(2) Write records to it, records include their size, so
a simple inspection can detect and incomplete trailing
record.
(3) Write append until the file reaches a certain size.
(4) Allocate a file, extensible.
(5) RESYNC INDEX on 'finished' file (see reading:3) and postpend
an offset '0' to the index.
(2) goto (1)
Reading journals...
(1) find oldest checkpoint of all subscribers, remove all older files.
(2) (file, last_read) = find_checkpoint for this subscriber
(3) RESYNC INDEX:
open record index for file, seek to the end - off_t.
this is the offset of the last noticed record in this file.
open file, seek to this point, roll forward writing the index file
_do not_ write an offset for the last record unless it is found
complete.
(4) read entries from last_read+1 -> index of record index
*/
#include <stdio.h>
#include "jlog_config.h"
#include "jlog_private.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_DIRENT_H
#include <dirent.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_TIME_H
#include <time.h>
#endif
#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#define BUFFERED_INDICES 1024
static jlog_file *__jlog_open_writer(jlog_ctx *ctx);
static int __jlog_close_writer(jlog_ctx *ctx);
static jlog_file *__jlog_open_reader(jlog_ctx *ctx, u_int32_t log);
static int __jlog_close_reader(jlog_ctx *ctx);
static int __jlog_close_checkpoint(jlog_ctx *ctx);
static jlog_file *__jlog_open_indexer(jlog_ctx *ctx, u_int32_t log);
static int __jlog_close_indexer(jlog_ctx *ctx);
static int __jlog_resync_index(jlog_ctx *ctx, u_int32_t log, jlog_id *last, int *c);
static jlog_file *__jlog_open_named_checkpoint(jlog_ctx *ctx, const char *cpname, int flags);
static int __jlog_mmap_reader(jlog_ctx *ctx, u_int32_t log);
static int __jlog_munmap_reader(jlog_ctx *ctx);
int jlog_snprint_logid(char *b, int n, const jlog_id *id) {
return snprintf(b, n, "%08x:%08x", id->log, id->marker);
}
int jlog_repair_datafile(jlog_ctx *ctx, u_int32_t log)
{
jlog_message_header hdr;
char *this, *next, *afternext = NULL, *mmap_end;
int i, invalid_count = 0;
struct {
off_t start, end;
} *invalid = NULL;
off_t orig_len, src, dst, len;
#define TAG_INVALID(s, e) do { \
if (invalid_count) \
invalid = realloc(invalid, (invalid_count + 1) * sizeof(*invalid)); \
else \
invalid = malloc(sizeof(*invalid)); \
invalid[invalid_count].start = s - (char *)ctx->mmap_base; \
invalid[invalid_count].end = e - (char *)ctx->mmap_base; \
invalid_count++; \
} while (0)
ctx->last_error = JLOG_ERR_SUCCESS;
/* we want the reader's open logic because this runs in the read path
* the underlying fds are always RDWR anyway */
__jlog_open_reader(ctx, log);
if (!ctx->data) {
ctx->last_error = JLOG_ERR_FILE_OPEN;
ctx->last_errno = errno;
return -1;
}
if (!jlog_file_lock(ctx->data)) {
ctx->last_error = JLOG_ERR_LOCK;
ctx->last_errno = errno;
return -1;
}
if (__jlog_mmap_reader(ctx, log) != 0)
SYS_FAIL(JLOG_ERR_FILE_READ);
orig_len = ctx->mmap_len;
mmap_end = (char*)ctx->mmap_base + ctx->mmap_len;
/* these values will cause us to fall right into the error clause and
* start searching for a valid header from offset 0 */
this = (char*)ctx->mmap_base - sizeof(hdr);
hdr.reserved = 0;
hdr.mlen = 0;
while (this + sizeof(hdr) <= mmap_end) {
next = this + sizeof(hdr) + hdr.mlen;
if (next <= (char *)ctx->mmap_base) goto error;
if (next == mmap_end) {
this = next;
break;
}
if (next + sizeof(hdr) > mmap_end) goto error;
memcpy(&hdr, next, sizeof(hdr));
if (hdr.reserved != 0) goto error;
this = next;
continue;
error:
for (next = this + sizeof(hdr); next + sizeof(hdr) <= mmap_end; next++) {
if (!next[0] && !next[1] && !next[2] && !next[3]) {
memcpy(&hdr, next, sizeof(hdr));
afternext = next + sizeof(hdr) + hdr.mlen;
if (afternext <= (char *)ctx->mmap_base) continue;
if (afternext == mmap_end) break;
if (afternext + sizeof(hdr) > mmap_end) continue;
memcpy(&hdr, afternext, sizeof(hdr));
if (hdr.reserved == 0) break;
}
}
/* correct for while loop entry condition */
if (this < (char *)ctx->mmap_base) this = ctx->mmap_base;
if (next + sizeof(hdr) > mmap_end) break;
if (next > this) TAG_INVALID(this, next);
this = afternext;
}
if (this != mmap_end) TAG_INVALID(this, mmap_end);
#undef TAG_INVALID
#define MOVE_SEGMENT do { \
char cpbuff[4096]; \
off_t chunk; \
while(len > 0) { \
chunk = len; \
if (chunk > sizeof(cpbuff)) chunk = sizeof(cpbuff); \
if (!jlog_file_pread(ctx->data, &cpbuff, chunk, src)) \
SYS_FAIL(JLOG_ERR_FILE_READ); \
if (!jlog_file_pwrite(ctx->data, &cpbuff, chunk, dst)) \
SYS_FAIL(JLOG_ERR_FILE_WRITE); \
src += chunk; \
dst += chunk; \
len -= chunk; \
} \
} while (0)
if (invalid_count > 0) {
__jlog_munmap_reader(ctx);
dst = invalid[0].start;
for (i = 0; i < invalid_count - 1; ) {
src = invalid[i].end;
len = invalid[++i].start - src;
MOVE_SEGMENT;
}
src = invalid[invalid_count - 1].end;
len = orig_len - src;
if (len > 0) MOVE_SEGMENT;
if (!jlog_file_truncate(ctx->data, dst))
SYS_FAIL(JLOG_ERR_FILE_WRITE);
}
#undef MOVE_SEGMENT
finish:
jlog_file_unlock(ctx->data);
if (invalid) free(invalid);
if (ctx->last_error != JLOG_ERR_SUCCESS) return -1;
return invalid_count;
}
int jlog_inspect_datafile(jlog_ctx *ctx, u_int32_t log)
{
jlog_message_header hdr;
char *this, *next, *mmap_end;
int i;
time_t timet;
struct tm tm;
char tbuff[128];
ctx->last_error = JLOG_ERR_SUCCESS;
__jlog_open_reader(ctx, log);
if (!ctx->data)
SYS_FAIL(JLOG_ERR_FILE_OPEN);
if (__jlog_mmap_reader(ctx, log) != 0)
SYS_FAIL(JLOG_ERR_FILE_READ);
mmap_end = (char*)ctx->mmap_base + ctx->mmap_len;
this = ctx->mmap_base;
i = 0;
while (this + sizeof(hdr) <= mmap_end) {
memcpy(&hdr, this, sizeof(hdr));
i++;
if (hdr.reserved != 0) {
fprintf(stderr, "Message %d at [%ld] has invalid reserved value %u\n",
i, (long int)(this - (char *)ctx->mmap_base), hdr.reserved);
return 1;
}
fprintf(stderr, "Message %d at [%ld] of (%lu+%u)", i,
(long int)(this - (char *)ctx->mmap_base),
(long unsigned int)sizeof(hdr), hdr.mlen);
next = this + sizeof(hdr) + hdr.mlen;
if (next <= (char *)ctx->mmap_base) {
fprintf(stderr, " WRAPPED TO NEGATIVE OFFSET!\n");
return 1;
}
if (next > mmap_end) {
fprintf(stderr, " OFF THE END!\n");
return 1;
}
timet = hdr.tv_sec;
localtime_r(&timet, &tm);
strftime(tbuff, sizeof(tbuff), "%c", &tm);
fprintf(stderr, "\n\ttime: %s\n\tmlen: %u\n", tbuff, hdr.mlen);
this = next;
}
if (this < mmap_end) {
fprintf(stderr, "%ld bytes of junk at the end\n",
(long int)(mmap_end - this));
return 1;
}
return 0;
finish:
return -1;
}
int jlog_idx_details(jlog_ctx *ctx, u_int32_t log,
u_int32_t *marker, int *closed)
{
off_t index_len;
u_int64_t index;
__jlog_open_indexer(ctx, log);
if (!ctx->index)
SYS_FAIL(JLOG_ERR_IDX_OPEN);
if ((index_len = jlog_file_size(ctx->index)) == -1)
SYS_FAIL(JLOG_ERR_IDX_SEEK);
if (index_len % sizeof(u_int64_t))
SYS_FAIL(JLOG_ERR_IDX_CORRUPT);
if (index_len > sizeof(u_int64_t)) {
if (!jlog_file_pread(ctx->index, &index, sizeof(u_int64_t),
index_len - sizeof(u_int64_t)))
{
SYS_FAIL(JLOG_ERR_IDX_READ);
}
if (index) {
*marker = index_len / sizeof(u_int64_t);
*closed = 0;
} else {
*marker = (index_len / sizeof(u_int64_t)) - 1;
*closed = 1;
}
} else {
*marker = index_len / sizeof(u_int64_t);
*closed = 0;
}
return 0;
finish:
return -1;
}
static int __jlog_unlink_datafile(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
int len;
if(ctx->current_log == log) {
__jlog_close_reader(ctx);
__jlog_close_indexer(ctx);
}
STRSETDATAFILE(ctx, file, log);
#ifdef DEBUG
fprintf(stderr, "unlinking %s\n", file);
#endif
unlink(file);
len = strlen(file);
if((len + sizeof(INDEX_EXT)) > sizeof(file)) return -1;
memcpy(file + len, INDEX_EXT, sizeof(INDEX_EXT));
#ifdef DEBUG
fprintf(stderr, "unlinking %s\n", file);
#endif
unlink(file);
return 0;
}
static int __jlog_open_metastore(jlog_ctx *ctx)
{
char file[MAXPATHLEN];
int len;
#ifdef DEBUG
fprintf(stderr, "__jlog_open_metastore\n");
#endif
len = strlen(ctx->path);
if((len + 1 /* IFS_CH */ + 9 /* "metastore" */ + 1) > MAXPATHLEN) {
#ifdef ENAMETOOLONG
ctx->last_errno = ENAMETOOLONG;
#endif
ctx->last_error = JLOG_ERR_CREATE_META;
return -1;
}
memcpy(file, ctx->path, len);
file[len++] = IFS_CH;
memcpy(&file[len], "metastore", 10); /* "metastore" + '\0' */
ctx->metastore = jlog_file_open(file, O_CREAT, ctx->file_mode);
if (!ctx->metastore) {
ctx->last_errno = errno;
ctx->last_error = JLOG_ERR_CREATE_META;
return -1;
}
return 0;
}
/* exported */
int __jlog_pending_readers(jlog_ctx *ctx, u_int32_t log) {
int readers;
DIR *dir;
struct dirent *ent;
char file[MAXPATHLEN];
int len;
jlog_id id;
readers = 0;
dir = opendir(ctx->path);
if (!dir) return -1;
len = strlen(ctx->path);
if(len + 2 > sizeof(file)) return -1;
memcpy(file, ctx->path, len);
file[len++] = IFS_CH;
file[len] = '\0';
while ((ent = readdir(dir))) {
if (ent->d_name[0] == 'c' && ent->d_name[1] == 'p' && ent->d_name[2] == '.') {
jlog_file *cp;
int dlen;
dlen = strlen(ent->d_name);
if((len + dlen + 1) > sizeof(file)) continue;
memcpy(file + len, ent->d_name, dlen + 1); /* include \0 */
#ifdef DEBUG
fprintf(stderr, "Checking if %s needs %s...\n", ent->d_name, ctx->path);
#endif
if ((cp = jlog_file_open(file, 0, ctx->file_mode))) {
if (jlog_file_lock(cp)) {
jlog_file_pread(cp, &id, sizeof(id), 0);
#ifdef DEBUG
fprintf(stderr, "\t%u <= %u (pending reader)\n", id.log, log);
#endif
if (id.log <= log) {
readers++;
}
jlog_file_unlock(cp);
}
jlog_file_close(cp);
}
}
}
closedir(dir);
return readers;
}
struct _jlog_subs {
char **subs;
int used;
int allocd;
};
int jlog_ctx_list_subscribers_dispose(jlog_ctx *ctx, char **subs) {
char *s;
int i = 0;
if(subs) {
while((s = subs[i++]) != NULL) free(s);
free(subs);
}
return 0;
}
int jlog_ctx_list_subscribers(jlog_ctx *ctx, char ***subs) {
struct _jlog_subs js = { NULL, 0, 0 };
DIR *dir;
struct dirent *ent;
unsigned char file[MAXPATHLEN];
char *p;
int len;
js.subs = calloc(16, sizeof(char *));
js.allocd = 16;
dir = opendir(ctx->path);
if (!dir) return -1;
while ((ent = readdir(dir))) {
if (ent->d_name[0] == 'c' && ent->d_name[1] == 'p' && ent->d_name[2] == '.') {
for (len = 0, p = ent->d_name + 3; *p;) {
unsigned char c;
int i;
for (c = 0, i = 0; i < 16; i++) {
if (__jlog_hexchars[i] == *p) {
c = i << 4;
break;
}
}
p++;
for (i = 0; i < 16; i++) {
if (__jlog_hexchars[i] == *p) {
c |= i;
break;
}
}
p++;
file[len++] = c;
}
file[len] = '\0';
js.subs[js.used++] = strdup((char *)file);
if(js.used == js.allocd) {
js.allocd *= 2;
js.subs = realloc(js.subs, js.allocd*sizeof(char *));
}
js.subs[js.used] = NULL;
}
}
closedir(dir);
*subs = js.subs;
return js.used;
}
static int __jlog_save_metastore(jlog_ctx *ctx, int ilocked)
{
struct _jlog_meta_info info;
#ifdef DEBUG
fprintf(stderr, "__jlog_save_metastore\n");
#endif
if (!ilocked && !jlog_file_lock(ctx->metastore)) {
return -1;
}
info.storage_log = ctx->storage.log;
info.unit_limit = ctx->unit_limit;
info.safety = ctx->safety;
if (!jlog_file_pwrite(ctx->metastore, &info, sizeof(info), 0)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
return -1;
}
if (ctx->safety == JLOG_SAFE) {
jlog_file_sync(ctx->metastore);
}
if (!ilocked) jlog_file_unlock(ctx->metastore);
return 0;
}
static int __jlog_restore_metastore(jlog_ctx *ctx, int ilocked)
{
struct _jlog_meta_info info;
#ifdef DEBUG
fprintf(stderr, "__jlog_restore_metastore\n");
#endif
if (!ilocked && !jlog_file_lock(ctx->metastore)) {
return -1;
}
if (!jlog_file_pread(ctx->metastore, &info, sizeof(info), 0)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
return -1;
}
if (!ilocked) jlog_file_unlock(ctx->metastore);
ctx->storage.log = info.storage_log;
ctx->unit_limit = info.unit_limit;
ctx->safety = info.safety;
return 0;
}
int jlog_get_checkpoint(jlog_ctx *ctx, const char *s, jlog_id *id) {
jlog_file *f;
int rv = -1;
if(ctx->subscriber_name && !strcmp(ctx->subscriber_name, s)) {
if(!ctx->checkpoint) {
ctx->checkpoint = __jlog_open_named_checkpoint(ctx, s, 0);
}
f = ctx->checkpoint;
} else
f = __jlog_open_named_checkpoint(ctx, s, 0);
if (f) {
if (jlog_file_lock(f)) {
if (jlog_file_pread(f, id, sizeof(*id), 0)) rv = 0;
jlog_file_unlock(f);
}
}
if (f && f != ctx->checkpoint) jlog_file_close(f);
return rv;
}
static int __jlog_set_checkpoint(jlog_ctx *ctx, const char *s, const jlog_id *id)
{
jlog_file *f;
int rv = -1;
jlog_id old_id;
u_int32_t log;
if(ctx->subscriber_name && !strcmp(ctx->subscriber_name, s)) {
if(!ctx->checkpoint) {
ctx->checkpoint = __jlog_open_named_checkpoint(ctx, s, 0);
}
f = ctx->checkpoint;
} else
f = __jlog_open_named_checkpoint(ctx, s, 0);
if(!f) return -1;
if (!jlog_file_lock(f))
goto failset;
if (jlog_file_size(f) == 0) {
/* we're setting it for the first time, no segments were pending on it */
old_id.log = id->log;
} else {
if (!jlog_file_pread(f, &old_id, sizeof(old_id), 0))
goto failset;
}
if (!jlog_file_pwrite(f, id, sizeof(*id), 0))
goto failset;
if (ctx->safety == JLOG_SAFE) {
jlog_file_sync(f);
}
jlog_file_unlock(f);
rv = 0;
for (log = old_id.log; log < id->log; log++) {
if (__jlog_pending_readers(ctx, log) == 0) {
__jlog_unlink_datafile(ctx, log);
}
}
failset:
if (f && f != ctx->checkpoint) jlog_file_close(f);
return rv;
}
static int __jlog_close_metastore(jlog_ctx *ctx) {
if (ctx->metastore) {
jlog_file_close(ctx->metastore);
ctx->metastore = NULL;
}
return 0;
}
/* path is assumed to be MAXPATHLEN */
static char *compute_checkpoint_filename(jlog_ctx *ctx, const char *subscriber, char *name)
{
const char *sub;
int len;
/* build checkpoint filename */
len = strlen(ctx->path);
memcpy(name, ctx->path, len);
name[len++] = IFS_CH;
name[len++] = 'c';
name[len++] = 'p';
name[len++] = '.';
for (sub = subscriber; *sub; ) {
name[len++] = __jlog_hexchars[((*sub & 0xf0) >> 4)];
name[len++] = __jlog_hexchars[(*sub & 0x0f)];
sub++;
}
name[len] = '\0';
#ifdef DEBUG
fprintf(stderr, "checkpoint %s filename is %s\n", subscriber, name);
#endif
return name;
}
static jlog_file *__jlog_open_named_checkpoint(jlog_ctx *ctx, const char *cpname, int flags)
{
char name[MAXPATHLEN];
compute_checkpoint_filename(ctx, cpname, name);
return jlog_file_open(name, flags, ctx->file_mode);
}
static jlog_file *__jlog_open_reader(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
if(ctx->current_log != log) {
__jlog_close_reader(ctx);
__jlog_close_indexer(ctx);
}
if(ctx->data) {
return ctx->data;
}
STRSETDATAFILE(ctx, file, log);
#ifdef DEBUG
fprintf(stderr, "opening log file[ro]: '%s'\n", file);
#endif
ctx->data = jlog_file_open(file, 0, ctx->file_mode);
ctx->current_log = log;
return ctx->data;
}
static int __jlog_munmap_reader(jlog_ctx *ctx) {
if(ctx->mmap_base) {
munmap(ctx->mmap_base, ctx->mmap_len);
ctx->mmap_base = NULL;
ctx->mmap_len = 0;
}
return 0;
}
static int __jlog_mmap_reader(jlog_ctx *ctx, u_int32_t log) {
if(ctx->current_log == log && ctx->mmap_base) return 0;
__jlog_open_reader(ctx, log);
if(!ctx->data)
return -1;
if (!jlog_file_map_read(ctx->data, &ctx->mmap_base, &ctx->mmap_len)) {
ctx->mmap_base = NULL;
ctx->last_error = JLOG_ERR_FILE_READ;
ctx->last_errno = errno;
return -1;
}
return 0;
}
static jlog_file *__jlog_open_writer(jlog_ctx *ctx) {
char file[MAXPATHLEN];
if(ctx->data) {
/* Still open */
return ctx->data;
}
if(!jlog_file_lock(ctx->metastore))
SYS_FAIL(JLOG_ERR_LOCK);
if(__jlog_restore_metastore(ctx, 1))
SYS_FAIL(JLOG_ERR_META_OPEN);
STRSETDATAFILE(ctx, file, ctx->storage.log);
#ifdef DEBUG
fprintf(stderr, "opening log file[rw]: '%s'\n", file);
#endif
ctx->data = jlog_file_open(file, O_CREAT, ctx->file_mode);
finish:
jlog_file_unlock(ctx->metastore);
return ctx->data;
}
static int __jlog_close_writer(jlog_ctx *ctx) {
if (ctx->data) {
jlog_file_close(ctx->data);
ctx->data = NULL;
}
return 0;
}
static int __jlog_close_reader(jlog_ctx *ctx) {
__jlog_munmap_reader(ctx);
if (ctx->data) {
jlog_file_close(ctx->data);
ctx->data = NULL;
}
return 0;
}
static int __jlog_close_checkpoint(jlog_ctx *ctx) {
if (ctx->checkpoint) {
jlog_file_close(ctx->checkpoint);
ctx->checkpoint = NULL;
}
return 0;
}
static jlog_file *__jlog_open_indexer(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
int len;
if(ctx->current_log != log) {
__jlog_close_reader(ctx);
__jlog_close_indexer(ctx);
}
if(ctx->index) {
return ctx->index;
}
STRSETDATAFILE(ctx, file, log);
len = strlen(file);
if((len + sizeof(INDEX_EXT)) > sizeof(file)) return NULL;
memcpy(file + len, INDEX_EXT, sizeof(INDEX_EXT));
#ifdef DEBUG
fprintf(stderr, "opening index file: '%s'\n", idx);
#endif
ctx->index = jlog_file_open(file, O_CREAT, ctx->file_mode);
ctx->current_log = log;
return ctx->index;
}
static int __jlog_close_indexer(jlog_ctx *ctx) {
if (ctx->index) {
jlog_file_close(ctx->index);
ctx->index = NULL;
}
return 0;
}
static int
___jlog_resync_index(jlog_ctx *ctx, u_int32_t log, jlog_id *last,
int *closed) {
jlog_message_header logmhdr;
int i, second_try = 0;
off_t index_off, data_off, data_len;
u_int64_t index;
u_int64_t indices[BUFFERED_INDICES];
ctx->last_error = JLOG_ERR_SUCCESS;
if(closed) *closed = 0;
__jlog_open_reader(ctx, log);
if (!ctx->data) {
ctx->last_error = JLOG_ERR_FILE_OPEN;
ctx->last_errno = errno;
return -1;
}
#define RESTART do { \
if (second_try == 0) { \
jlog_file_truncate(ctx->index, 0); \
jlog_file_unlock(ctx->index); \
second_try = 1; \
ctx->last_error = JLOG_ERR_SUCCESS; \
goto restart; \
} \
SYS_FAIL(JLOG_ERR_IDX_CORRUPT); \
} while (0)
restart:
__jlog_open_indexer(ctx, log);
if (!ctx->index) {
ctx->last_error = JLOG_ERR_IDX_OPEN;
ctx->last_errno = errno;
return -1;
}
if (!jlog_file_lock(ctx->index)) {
ctx->last_error = JLOG_ERR_LOCK;
ctx->last_errno = errno;
return -1;
}
data_off = 0;
if ((data_len = jlog_file_size(ctx->data)) == -1)
SYS_FAIL(JLOG_ERR_FILE_SEEK);
if ((index_off = jlog_file_size(ctx->index)) == -1)
SYS_FAIL(JLOG_ERR_IDX_SEEK);
if (index_off % sizeof(u_int64_t)) {
#ifdef DEBUG
fprintf(stderr, "corrupt index [%llu]\n", index_off);
#endif
RESTART;
}
if (index_off > sizeof(u_int64_t)) {
if (!jlog_file_pread(ctx->index, &index, sizeof(index),
index_off - sizeof(u_int64_t)))
{
SYS_FAIL(JLOG_ERR_IDX_READ);
}
if (index == 0) {
/* This log file has been "closed" */
#ifdef DEBUG
fprintf(stderr, "index closed\n");
#endif
if(last) {
last->log = log;
last->marker = (index_off / sizeof(u_int64_t)) - 1;
}
if(closed) *closed = 1;
goto finish;
} else {
if (index > data_len) {
#ifdef DEBUG
fprintf(stderr, "index told me to seek somehwere I can't\n");
#endif
RESTART;
}
data_off = index;
}
}
if (index_off > 0) {
/* We are adding onto a partial index so we must advance a record */
if (!jlog_file_pread(ctx->data, &logmhdr, sizeof(logmhdr), data_off))
SYS_FAIL(JLOG_ERR_FILE_READ);
if ((data_off += sizeof(logmhdr) + logmhdr.mlen) > data_len)
RESTART;
}
i = 0;
while (data_off + sizeof(logmhdr) <= data_len) {
off_t next_off = data_off;
if (!jlog_file_pread(ctx->data, &logmhdr, sizeof(logmhdr), data_off))
SYS_FAIL(JLOG_ERR_FILE_READ);
if (logmhdr.reserved != 0)
SYS_FAIL(JLOG_ERR_FILE_CORRUPT);
if ((next_off += sizeof(logmhdr) + logmhdr.mlen) > data_len)
break;
/* Write our new index offset */
indices[i++] = data_off;
if(i >= BUFFERED_INDICES) {
#ifdef DEBUG
fprintf(stderr, "writing %i offsets\n", i);
#endif
if (!jlog_file_pwrite(ctx->index, indices, i * sizeof(u_int64_t), index_off))
RESTART;
index_off += i * sizeof(u_int64_t);
i = 0;
}
data_off = next_off;
}
if(i > 0) {
#ifdef DEBUG
fprintf(stderr, "writing %i offsets\n", i);
#endif
if (!jlog_file_pwrite(ctx->index, indices, i * sizeof(u_int64_t), index_off))
RESTART;
index_off += i * sizeof(u_int64_t);
}
if(last) {
last->log = log;
last->marker = index_off / sizeof(u_int64_t);
}
if(log < ctx->storage.log) {
if (data_off != data_len) {
#ifdef DEBUG
fprintf(stderr, "closing index, but %llu != %llu\n", data_off, data_len);
#endif
SYS_FAIL(JLOG_ERR_FILE_CORRUPT);
}
/* Special case: if we are closing, we next write a '0'
* we can't write the closing marker if the data segment had no records
* in it, since it will be confused with an index to offset 0 by the
* next reader; this only happens when segments are repaired */
if (index_off) {
index = 0;
if (!jlog_file_pwrite(ctx->index, &index, sizeof(u_int64_t), index_off))
RESTART;
}
if(closed) *closed = 1;
}
#undef RESTART
finish:
jlog_file_unlock(ctx->index);
#ifdef DEBUG
fprintf(stderr, "index is %s\n", closed?(*closed?"closed":"open"):"unknown");
#endif
if(ctx->last_error == JLOG_ERR_SUCCESS) return 0;
return -1;
}
static int __jlog_resync_index(jlog_ctx *ctx, u_int32_t log, jlog_id *last, int *closed) {
int attempts, rv = -1;
for(attempts=0; attempts<4; attempts++) {
rv = ___jlog_resync_index(ctx, log, last, closed);
if(ctx->last_error == JLOG_ERR_SUCCESS) break;
if(ctx->last_error == JLOG_ERR_FILE_OPEN ||
ctx->last_error == JLOG_ERR_IDX_OPEN) break;
/* We can't fix the file if someone may write to it again */
if(log >= ctx->storage.log) break;
jlog_file_lock(ctx->index);
/* it doesn't really matter what jlog_repair_datafile returns
* we'll keep retrying anyway */
jlog_repair_datafile(ctx, log);
jlog_file_truncate(ctx->index, 0);
jlog_file_unlock(ctx->index);
}
return rv;
}
jlog_ctx *jlog_new(const char *path) {
jlog_ctx *ctx;
ctx = calloc(1, sizeof(*ctx));
ctx->unit_limit = DEFAULT_UNIT_LIMIT;
ctx->file_mode = DEFAULT_FILE_MODE;
ctx->safety = DEFAULT_SAFETY;
ctx->context_mode = JLOG_NEW;
ctx->path = strdup(path);
return ctx;
}
void jlog_set_error_func(jlog_ctx *ctx, jlog_error_func Func, void *ptr) {
ctx->error_func = Func;
ctx->error_ctx = ptr;
}
size_t jlog_raw_size(jlog_ctx *ctx) {
DIR *d;
struct dirent *de;
size_t totalsize = 0;
int ferr, len;
char filename[MAXPATHLEN];
d = opendir(ctx->path);
if(!d) return 0;
len = strlen(ctx->path);
memcpy(filename, ctx->path, len);
filename[len++] = IFS_CH;
while((de = readdir(d)) != NULL) {
struct stat sb;
int dlen;
dlen = strlen(de->d_name);
if((len + dlen + 1) > sizeof(filename)) continue;
memcpy(filename+len, de->d_name, dlen + 1); /* include \0 */
while((ferr = stat(filename, &sb)) == -1 && errno == EINTR);
if(ferr == 0 && S_ISREG(sb.st_mode)) totalsize += sb.st_size;
}
closedir(d);
return totalsize;
}