redrust 0.1.1

redrust is a port of the popular Redis database system written in Rust programming language. This port aims to provide all the features of Redis while taking advantage of the Rust language's safety, speed, and modern language features.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
extern crate c2rust_bitfields;
extern crate libc;
extern crate core;
extern "C" {
    pub type _IO_wide_data;
    pub type _IO_codecvt;
    pub type _IO_marker;
    static mut stdin: *mut FILE;
    fn fclose(__stream: *mut FILE) -> libc::c_int;
    fn fopen(_: *const libc::c_char, _: *const libc::c_char) -> *mut FILE;
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
    fn snprintf(
        _: *mut libc::c_char,
        _: libc::c_ulong,
        _: *const libc::c_char,
        _: ...
    ) -> libc::c_int;
    fn fgets(
        __s: *mut libc::c_char,
        __n: libc::c_int,
        __stream: *mut FILE,
    ) -> *mut libc::c_char;
    fn fread(
        _: *mut libc::c_void,
        _: libc::c_ulong,
        _: libc::c_ulong,
        _: *mut FILE,
    ) -> libc::c_ulong;
    fn fseek(
        __stream: *mut FILE,
        __off: libc::c_long,
        __whence: libc::c_int,
    ) -> libc::c_int;
    fn ftello(__stream: *mut FILE) -> __off64_t;
    fn feof(__stream: *mut FILE) -> libc::c_int;
    fn fileno(__stream: *mut FILE) -> libc::c_int;
    fn sdsfree(s: sds);
    fn __errno_location() -> *mut libc::c_int;
    fn strtol(
        _: *const libc::c_char,
        _: *mut *mut libc::c_char,
        _: libc::c_int,
    ) -> libc::c_long;
    fn exit(_: libc::c_int) -> !;
    fn memcpy(
        _: *mut libc::c_void,
        _: *const libc::c_void,
        _: libc::c_ulong,
    ) -> *mut libc::c_void;
    fn memcmp(
        _: *const libc::c_void,
        _: *const libc::c_void,
        _: libc::c_ulong,
    ) -> libc::c_int;
    fn strcmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
    fn strncmp(
        _: *const libc::c_char,
        _: *const libc::c_char,
        _: libc::c_ulong,
    ) -> libc::c_int;
    fn strlen(_: *const libc::c_char) -> libc::c_ulong;
    fn strerror(_: libc::c_int) -> *mut libc::c_char;
    fn strcasecmp(__s1: *const libc::c_char, __s2: *const libc::c_char) -> libc::c_int;
    fn strncasecmp(
        __s1: *const libc::c_char,
        __s2: *const libc::c_char,
        __n: size_t,
    ) -> libc::c_int;
    fn ftruncate(__fd: libc::c_int, __length: __off64_t) -> libc::c_int;
    fn listNext(iter: *mut listIter) -> *mut listNode;
    fn listRewind(list: *mut list, li: *mut listIter);
    fn zmalloc(size: size_t) -> *mut libc::c_void;
    fn zfree(ptr: *mut libc::c_void);
    fn makePath(path: *mut libc::c_char, filename: *mut libc::c_char) -> sds;
    fn aofManifestFree(am: *mut aofManifest);
    fn redis_check_rdb_main(
        argc: libc::c_int,
        argv: *mut *mut libc::c_char,
        fp: *mut FILE,
    ) -> libc::c_int;
    fn __fxstat(
        __ver: libc::c_int,
        __fildes: libc::c_int,
        __stat_buf: *mut stat,
    ) -> libc::c_int;
    fn dirname(__path: *mut libc::c_char) -> *mut libc::c_char;
    fn aofLoadManifestFromFile(am_filepath: sds) -> *mut aofManifest;
}
pub type __dev_t = libc::c_ulong;
pub type __uid_t = libc::c_uint;
pub type __gid_t = libc::c_uint;
pub type __ino64_t = libc::c_ulong;
pub type __mode_t = libc::c_uint;
pub type __nlink_t = libc::c_uint;
pub type __off_t = libc::c_long;
pub type __off64_t = libc::c_long;
pub type __time_t = libc::c_long;
pub type __blksize_t = libc::c_int;
pub type __blkcnt64_t = libc::c_long;
pub type __syscall_slong_t = libc::c_long;
pub type size_t = libc::c_ulong;
pub type off_t = __off64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct timespec {
    pub tv_sec: __time_t,
    pub tv_nsec: __syscall_slong_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct stat {
    pub st_dev: __dev_t,
    pub st_ino: __ino64_t,
    pub st_mode: __mode_t,
    pub st_nlink: __nlink_t,
    pub st_uid: __uid_t,
    pub st_gid: __gid_t,
    pub st_rdev: __dev_t,
    pub __pad1: __dev_t,
    pub st_size: __off64_t,
    pub st_blksize: __blksize_t,
    pub __pad2: libc::c_int,
    pub st_blocks: __blkcnt64_t,
    pub st_atim: timespec,
    pub st_mtim: timespec,
    pub st_ctim: timespec,
    pub __glibc_reserved: [libc::c_int; 2],
}
pub type time_t = __time_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
    pub _flags: libc::c_int,
    pub _IO_read_ptr: *mut libc::c_char,
    pub _IO_read_end: *mut libc::c_char,
    pub _IO_read_base: *mut libc::c_char,
    pub _IO_write_base: *mut libc::c_char,
    pub _IO_write_ptr: *mut libc::c_char,
    pub _IO_write_end: *mut libc::c_char,
    pub _IO_buf_base: *mut libc::c_char,
    pub _IO_buf_end: *mut libc::c_char,
    pub _IO_save_base: *mut libc::c_char,
    pub _IO_backup_base: *mut libc::c_char,
    pub _IO_save_end: *mut libc::c_char,
    pub _markers: *mut _IO_marker,
    pub _chain: *mut _IO_FILE,
    pub _fileno: libc::c_int,
    pub _flags2: libc::c_int,
    pub _old_offset: __off_t,
    pub _cur_column: libc::c_ushort,
    pub _vtable_offset: libc::c_schar,
    pub _shortbuf: [libc::c_char; 1],
    pub _lock: *mut libc::c_void,
    pub _offset: __off64_t,
    pub _codecvt: *mut _IO_codecvt,
    pub _wide_data: *mut _IO_wide_data,
    pub _freeres_list: *mut _IO_FILE,
    pub _freeres_buf: *mut libc::c_void,
    pub __pad5: size_t,
    pub _mode: libc::c_int,
    pub _unused2: [libc::c_char; 20],
}
pub type _IO_lock_t = ();
pub type FILE = _IO_FILE;
pub type sds = *mut libc::c_char;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct listNode {
    pub prev: *mut listNode,
    pub next: *mut listNode,
    pub value: *mut libc::c_void,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct listIter {
    pub next: *mut listNode,
    pub direction: libc::c_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct list {
    pub head: *mut listNode,
    pub tail: *mut listNode,
    pub dup: Option::<unsafe extern "C" fn(*mut libc::c_void) -> *mut libc::c_void>,
    pub free: Option::<unsafe extern "C" fn(*mut libc::c_void) -> ()>,
    pub match_0: Option::<
        unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> libc::c_int,
    >,
    pub len: libc::c_ulong,
}
pub type aof_file_type = libc::c_uint;
pub const AOF_FILE_TYPE_INCR: aof_file_type = 105;
pub const AOF_FILE_TYPE_HIST: aof_file_type = 104;
pub const AOF_FILE_TYPE_BASE: aof_file_type = 98;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct aofInfo {
    pub file_name: sds,
    pub file_seq: libc::c_longlong,
    pub file_type: aof_file_type,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct aofManifest {
    pub base_aof_info: *mut aofInfo,
    pub incr_aof_list: *mut list,
    pub history_aof_list: *mut list,
    pub curr_base_file_seq: libc::c_longlong,
    pub curr_incr_file_seq: libc::c_longlong,
    pub dirty: libc::c_int,
}
pub const AOF_RDB_PREAMBLE: input_file_type = 1;
pub const AOF_RESP: input_file_type = 0;
pub const AOF_MULTI_PART: input_file_type = 2;
pub type input_file_type = libc::c_uint;
#[inline]
unsafe extern "C" fn fstat(
    mut __fd: libc::c_int,
    mut __statbuf: *mut stat,
) -> libc::c_int {
    return __fxstat(0 as libc::c_int, __fd, __statbuf);
}
static mut error: [libc::c_char; 1044] = [0; 1044];
static mut epos: off_t = 0;
static mut line: libc::c_longlong = 1 as libc::c_int as libc::c_longlong;
static mut to_timestamp: time_t = 0 as libc::c_int as time_t;
#[no_mangle]
pub unsafe extern "C" fn consumeNewline(mut buf: *mut libc::c_char) -> libc::c_int {
    if strncmp(
        buf,
        b"\r\n\0" as *const u8 as *const libc::c_char,
        2 as libc::c_int as libc::c_ulong,
    ) != 0 as libc::c_int
    {
        let mut __buf: [libc::c_char; 1024] = [0; 1024];
        snprintf(
            __buf.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
            b"Expected \\r\\n, got: %02x%02x\0" as *const u8 as *const libc::c_char,
            *buf.offset(0 as libc::c_int as isize) as libc::c_int,
            *buf.offset(1 as libc::c_int as isize) as libc::c_int,
        );
        snprintf(
            error.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
            b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
            epos as libc::c_longlong,
            __buf.as_mut_ptr(),
        );
        return 0 as libc::c_int;
    }
    line += 1 as libc::c_int as libc::c_longlong;
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn readLong(
    mut fp: *mut FILE,
    mut prefix: libc::c_char,
    mut target: *mut libc::c_long,
) -> libc::c_int {
    let mut buf: [libc::c_char; 128] = [0; 128];
    let mut eptr: *mut libc::c_char = 0 as *mut libc::c_char;
    epos = ftello(fp);
    if (fgets(
        buf.as_mut_ptr(),
        core::mem::size_of::<[libc::c_char; 128]>() as libc::c_ulong as libc::c_int,
        fp,
    ))
        .is_null()
    {
        return 0 as libc::c_int;
    }
    if buf[0 as libc::c_int as usize] as libc::c_int != prefix as libc::c_int {
        let mut __buf: [libc::c_char; 1024] = [0; 1024];
        snprintf(
            __buf.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
            b"Expected prefix '%c', got: '%c'\0" as *const u8 as *const libc::c_char,
            prefix as libc::c_int,
            buf[0 as libc::c_int as usize] as libc::c_int,
        );
        snprintf(
            error.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
            b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
            epos as libc::c_longlong,
            __buf.as_mut_ptr(),
        );
        return 0 as libc::c_int;
    }
    *target = strtol(
        buf.as_mut_ptr().offset(1 as libc::c_int as isize),
        &mut eptr,
        10 as libc::c_int,
    );
    return consumeNewline(eptr);
}
#[no_mangle]
pub unsafe extern "C" fn readBytes(
    mut fp: *mut FILE,
    mut target: *mut libc::c_char,
    mut length: libc::c_long,
) -> libc::c_int {
    let mut real: libc::c_long = 0;
    epos = ftello(fp);
    real = fread(
        target as *mut libc::c_void,
        1 as libc::c_int as libc::c_ulong,
        length as libc::c_ulong,
        fp,
    ) as libc::c_long;
    if real != length {
        let mut __buf: [libc::c_char; 1024] = [0; 1024];
        snprintf(
            __buf.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
            b"Expected to read %ld bytes, got %ld bytes\0" as *const u8
                as *const libc::c_char,
            length,
            real,
        );
        snprintf(
            error.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
            b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
            epos as libc::c_longlong,
            __buf.as_mut_ptr(),
        );
        return 0 as libc::c_int;
    }
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn readString(
    mut fp: *mut FILE,
    mut target: *mut *mut libc::c_char,
) -> libc::c_int {
    let mut len: libc::c_long = 0;
    *target = 0 as *mut libc::c_char;
    if readLong(fp, '$' as i32 as libc::c_char, &mut len) == 0 {
        return 0 as libc::c_int;
    }
    if len < 0 as libc::c_int as libc::c_long
        || len > 9223372036854775807 as libc::c_long - 2 as libc::c_int as libc::c_long
    {
        let mut __buf: [libc::c_char; 1024] = [0; 1024];
        snprintf(
            __buf.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
            b"Expected to read string of %ld bytes, which is not in the suitable range\0"
                as *const u8 as *const libc::c_char,
            len,
        );
        snprintf(
            error.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
            b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
            epos as libc::c_longlong,
            __buf.as_mut_ptr(),
        );
        return 0 as libc::c_int;
    }
    len += 2 as libc::c_int as libc::c_long;
    *target = zmalloc(len as size_t) as *mut libc::c_char;
    if readBytes(fp, *target, len) == 0 {
        zfree(*target as *mut libc::c_void);
        *target = 0 as *mut libc::c_char;
        return 0 as libc::c_int;
    }
    if consumeNewline(
        (*target).offset(len as isize).offset(-(2 as libc::c_int as isize)),
    ) == 0
    {
        zfree(*target as *mut libc::c_void);
        *target = 0 as *mut libc::c_char;
        return 0 as libc::c_int;
    }
    *(*target)
        .offset(
            (len - 2 as libc::c_int as libc::c_long) as isize,
        ) = '\0' as i32 as libc::c_char;
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn readArgc(
    mut fp: *mut FILE,
    mut target: *mut libc::c_long,
) -> libc::c_int {
    return readLong(fp, '*' as i32 as libc::c_char, target);
}
#[no_mangle]
pub unsafe extern "C" fn processRESP(
    mut fp: *mut FILE,
    mut filename: *mut libc::c_char,
    mut out_multi: *mut libc::c_int,
) -> libc::c_int {
    let mut argc: libc::c_long = 0;
    let mut str: *mut libc::c_char = 0 as *mut libc::c_char;
    if readArgc(fp, &mut argc) == 0 {
        return 0 as libc::c_int;
    }
    let mut i: libc::c_int = 0 as libc::c_int;
    while (i as libc::c_long) < argc {
        if readString(fp, &mut str) == 0 {
            return 0 as libc::c_int;
        }
        if i == 0 as libc::c_int {
            if strcasecmp(str, b"multi\0" as *const u8 as *const libc::c_char)
                == 0 as libc::c_int
            {
                let fresh0 = *out_multi;
                *out_multi = *out_multi + 1;
                if fresh0 != 0 {
                    let mut __buf: [libc::c_char; 1024] = [0; 1024];
                    snprintf(
                        __buf.as_mut_ptr(),
                        core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
                        b"Unexpected MULTI in AOF %s\0" as *const u8
                            as *const libc::c_char,
                        filename,
                    );
                    snprintf(
                        error.as_mut_ptr(),
                        core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
                        b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
                        epos as libc::c_longlong,
                        __buf.as_mut_ptr(),
                    );
                    zfree(str as *mut libc::c_void);
                    return 0 as libc::c_int;
                }
            } else if strcasecmp(str, b"exec\0" as *const u8 as *const libc::c_char)
                == 0 as libc::c_int
            {
                *out_multi -= 1;
                if *out_multi != 0 {
                    let mut __buf_0: [libc::c_char; 1024] = [0; 1024];
                    snprintf(
                        __buf_0.as_mut_ptr(),
                        core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
                        b"Unexpected EXEC in AOF %s\0" as *const u8
                            as *const libc::c_char,
                        filename,
                    );
                    snprintf(
                        error.as_mut_ptr(),
                        core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
                        b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
                        epos as libc::c_longlong,
                        __buf_0.as_mut_ptr(),
                    );
                    zfree(str as *mut libc::c_void);
                    return 0 as libc::c_int;
                }
            }
        }
        zfree(str as *mut libc::c_void);
        i += 1;
    }
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn processAnnotations(
    mut fp: *mut FILE,
    mut filename: *mut libc::c_char,
    mut last_file: libc::c_int,
) -> libc::c_int {
    let mut buf: [libc::c_char; 1024] = [0; 1024];
    epos = ftello(fp);
    if (fgets(
        buf.as_mut_ptr(),
        core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong as libc::c_int,
        fp,
    ))
        .is_null()
    {
        printf(
            b"Failed to read annotations from AOF %s, aborting...\n\0" as *const u8
                as *const libc::c_char,
            filename,
        );
        exit(1 as libc::c_int);
    }
    if to_timestamp != 0
        && strncmp(
            buf.as_mut_ptr(),
            b"#TS:\0" as *const u8 as *const libc::c_char,
            4 as libc::c_int as libc::c_ulong,
        ) == 0 as libc::c_int
    {
        let mut endptr: *mut libc::c_char = 0 as *mut libc::c_char;
        *__errno_location() = 0 as libc::c_int;
        let mut ts: time_t = strtol(
            buf.as_mut_ptr().offset(4 as libc::c_int as isize),
            &mut endptr,
            10 as libc::c_int,
        );
        if *__errno_location() != 0 as libc::c_int
            || *endptr as libc::c_int != '\r' as i32
        {
            printf(
                b"Invalid timestamp annotation\n\0" as *const u8 as *const libc::c_char,
            );
            exit(1 as libc::c_int);
        }
        if ts <= to_timestamp {
            return 1 as libc::c_int;
        }
        if epos == 0 as libc::c_int as libc::c_long {
            printf(
                b"AOF %s has nothing before timestamp %ld, aborting...\n\0" as *const u8
                    as *const libc::c_char,
                filename,
                to_timestamp,
            );
            exit(1 as libc::c_int);
        }
        if last_file == 0 {
            printf(
                b"Failed to truncate AOF %s to timestamp %ld to offset %ld because it is not the last file.\n\0"
                    as *const u8 as *const libc::c_char,
                filename,
                to_timestamp,
                epos,
            );
            printf(
                b"If you insist, please delete all files after this file according to the manifest file and delete the corresponding records in manifest file manually. Then re-run redis-check-aof.\n\0"
                    as *const u8 as *const libc::c_char,
            );
            exit(1 as libc::c_int);
        }
        if ftruncate(fileno(fp), epos) == -(1 as libc::c_int) {
            printf(
                b"Failed to truncate AOF %s to timestamp %ld\n\0" as *const u8
                    as *const libc::c_char,
                filename,
                to_timestamp,
            );
            exit(1 as libc::c_int);
        } else {
            return 0 as libc::c_int
        }
    }
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn checkSingleAof(
    mut aof_filename: *mut libc::c_char,
    mut aof_filepath: *mut libc::c_char,
    mut last_file: libc::c_int,
    mut fix: libc::c_int,
    mut preamble: libc::c_int,
) -> libc::c_int {
    let mut pos: off_t = 0 as libc::c_int as off_t;
    let mut diff: off_t = 0;
    let mut multi: libc::c_int = 0 as libc::c_int;
    let mut buf: [libc::c_char; 2] = [0; 2];
    let mut fp: *mut FILE = fopen(
        aof_filepath,
        b"r+\0" as *const u8 as *const libc::c_char,
    );
    if fp.is_null() {
        printf(
            b"Cannot open file %s: %s, aborting...\n\0" as *const u8
                as *const libc::c_char,
            aof_filepath,
            strerror(*__errno_location()),
        );
        exit(1 as libc::c_int);
    }
    let mut sb: stat = stat {
        st_dev: 0,
        st_ino: 0,
        st_mode: 0,
        st_nlink: 0,
        st_uid: 0,
        st_gid: 0,
        st_rdev: 0,
        __pad1: 0,
        st_size: 0,
        st_blksize: 0,
        __pad2: 0,
        st_blocks: 0,
        st_atim: timespec { tv_sec: 0, tv_nsec: 0 },
        st_mtim: timespec { tv_sec: 0, tv_nsec: 0 },
        st_ctim: timespec { tv_sec: 0, tv_nsec: 0 },
        __glibc_reserved: [0; 2],
    };
    if fstat(fileno(fp), &mut sb) == -(1 as libc::c_int) {
        printf(
            b"Cannot stat file: %s, aborting...\n\0" as *const u8 as *const libc::c_char,
            aof_filename,
        );
        exit(1 as libc::c_int);
    }
    let mut size: off_t = sb.st_size;
    if size == 0 as libc::c_int as libc::c_long {
        return 1 as libc::c_int;
    }
    if preamble != 0 {
        let mut argv: [*mut libc::c_char; 2] = [0 as *mut libc::c_char, aof_filename];
        if redis_check_rdb_main(2 as libc::c_int, argv.as_mut_ptr(), fp)
            == -(1 as libc::c_int)
        {
            printf(
                b"RDB preamble of AOF file is not sane, aborting.\n\0" as *const u8
                    as *const libc::c_char,
            );
            exit(1 as libc::c_int);
        } else {
            printf(
                b"RDB preamble is OK, proceeding with AOF tail...\n\0" as *const u8
                    as *const libc::c_char,
            );
        }
    }
    loop {
        if multi == 0 {
            pos = ftello(fp);
        }
        if (fgets(
            buf.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 2]>() as libc::c_ulong as libc::c_int,
            fp,
        ))
            .is_null()
        {
            if feof(fp) != 0 {
                break;
            }
            printf(
                b"Failed to read from AOF %s, aborting...\n\0" as *const u8
                    as *const libc::c_char,
                aof_filename,
            );
            exit(1 as libc::c_int);
        } else {
            if fseek(fp, -(1 as libc::c_int) as libc::c_long, 1 as libc::c_int)
                == -(1 as libc::c_int)
            {
                printf(
                    b"Failed to fseek in AOF %s: %s\0" as *const u8
                        as *const libc::c_char,
                    aof_filename,
                    strerror(*__errno_location()),
                );
                exit(1 as libc::c_int);
            }
            if buf[0 as libc::c_int as usize] as libc::c_int == '#' as i32 {
                if processAnnotations(fp, aof_filepath, last_file) == 0 {
                    fclose(fp);
                    return 3 as libc::c_int;
                }
            } else if buf[0 as libc::c_int as usize] as libc::c_int == '*' as i32 {
                if processRESP(fp, aof_filepath, &mut multi) == 0 {
                    break;
                }
            } else {
                printf(
                    b"AOF %s format error\n\0" as *const u8 as *const libc::c_char,
                    aof_filename,
                );
                break;
            }
        }
    }
    if feof(fp) != 0 && multi != 0
        && strlen(error.as_mut_ptr()) == 0 as libc::c_int as libc::c_ulong
    {
        let mut __buf: [libc::c_char; 1024] = [0; 1024];
        snprintf(
            __buf.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1024]>() as libc::c_ulong,
            b"Reached EOF before reading EXEC for MULTI\0" as *const u8
                as *const libc::c_char,
        );
        snprintf(
            error.as_mut_ptr(),
            core::mem::size_of::<[libc::c_char; 1044]>() as libc::c_ulong,
            b"0x%16llx: %s\0" as *const u8 as *const libc::c_char,
            epos as libc::c_longlong,
            __buf.as_mut_ptr(),
        );
    }
    if strlen(error.as_mut_ptr()) > 0 as libc::c_int as libc::c_ulong {
        printf(b"%s\n\0" as *const u8 as *const libc::c_char, error.as_mut_ptr());
    }
    diff = size - pos;
    if diff == 0 as libc::c_int as libc::c_long && to_timestamp != 0 {
        printf(
            b"Truncate nothing in AOF %s to timestamp %ld\n\0" as *const u8
                as *const libc::c_char,
            aof_filename,
            to_timestamp,
        );
        fclose(fp);
        return 0 as libc::c_int;
    }
    printf(
        b"AOF analyzed: filename=%s, size=%lld, ok_up_to=%lld, ok_up_to_line=%lld, diff=%lld\n\0"
            as *const u8 as *const libc::c_char,
        aof_filename,
        size as libc::c_longlong,
        pos as libc::c_longlong,
        line,
        diff as libc::c_longlong,
    );
    if diff > 0 as libc::c_int as libc::c_long {
        if fix != 0 {
            if last_file == 0 {
                printf(
                    b"Failed to truncate AOF %s because it is not the last file\n\0"
                        as *const u8 as *const libc::c_char,
                    aof_filename,
                );
                exit(1 as libc::c_int);
            }
            let mut buf_0: [libc::c_char; 2] = [0; 2];
            printf(
                b"This will shrink the AOF %s from %lld bytes, with %lld bytes, to %lld bytes\n\0"
                    as *const u8 as *const libc::c_char,
                aof_filename,
                size as libc::c_longlong,
                diff as libc::c_longlong,
                pos as libc::c_longlong,
            );
            printf(b"Continue? [y/N]: \0" as *const u8 as *const libc::c_char);
            if (fgets(
                buf_0.as_mut_ptr(),
                core::mem::size_of::<[libc::c_char; 2]>() as libc::c_ulong
                    as libc::c_int,
                stdin,
            ))
                .is_null()
                || strncasecmp(
                    buf_0.as_mut_ptr(),
                    b"y\0" as *const u8 as *const libc::c_char,
                    1 as libc::c_int as size_t,
                ) != 0 as libc::c_int
            {
                printf(b"Aborting...\n\0" as *const u8 as *const libc::c_char);
                exit(1 as libc::c_int);
            }
            if ftruncate(fileno(fp), pos) == -(1 as libc::c_int) {
                printf(
                    b"Failed to truncate AOF %s\n\0" as *const u8 as *const libc::c_char,
                    aof_filename,
                );
                exit(1 as libc::c_int);
            } else {
                fclose(fp);
                return 2 as libc::c_int;
            }
        } else {
            printf(
                b"AOF %s is not valid. Use the --fix option to try fixing it.\n\0"
                    as *const u8 as *const libc::c_char,
                aof_filename,
            );
            exit(1 as libc::c_int);
        }
    }
    fclose(fp);
    return 0 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn fileIsRDB(mut filepath: *mut libc::c_char) -> libc::c_int {
    let mut fp: *mut FILE = fopen(filepath, b"r\0" as *const u8 as *const libc::c_char);
    if fp.is_null() {
        printf(
            b"Cannot open file %s: %s\n\0" as *const u8 as *const libc::c_char,
            filepath,
            strerror(*__errno_location()),
        );
        exit(1 as libc::c_int);
    }
    let mut sb: stat = stat {
        st_dev: 0,
        st_ino: 0,
        st_mode: 0,
        st_nlink: 0,
        st_uid: 0,
        st_gid: 0,
        st_rdev: 0,
        __pad1: 0,
        st_size: 0,
        st_blksize: 0,
        __pad2: 0,
        st_blocks: 0,
        st_atim: timespec { tv_sec: 0, tv_nsec: 0 },
        st_mtim: timespec { tv_sec: 0, tv_nsec: 0 },
        st_ctim: timespec { tv_sec: 0, tv_nsec: 0 },
        __glibc_reserved: [0; 2],
    };
    if fstat(fileno(fp), &mut sb) == -(1 as libc::c_int) {
        printf(
            b"Cannot stat file: %s\n\0" as *const u8 as *const libc::c_char,
            filepath,
        );
        exit(1 as libc::c_int);
    }
    let mut size: off_t = sb.st_size;
    if size == 0 as libc::c_int as libc::c_long {
        fclose(fp);
        return 0 as libc::c_int;
    }
    if size >= 8 as libc::c_int as libc::c_long {
        let mut sig: [libc::c_char; 5] = [0; 5];
        let mut rdb_file: libc::c_int = (fread(
            sig.as_mut_ptr() as *mut libc::c_void,
            core::mem::size_of::<[libc::c_char; 5]>() as libc::c_ulong,
            1 as libc::c_int as libc::c_ulong,
            fp,
        ) == 1 as libc::c_int as libc::c_ulong
            && memcmp(
                sig.as_mut_ptr() as *const libc::c_void,
                b"REDIS\0" as *const u8 as *const libc::c_char as *const libc::c_void,
                core::mem::size_of::<[libc::c_char; 5]>() as libc::c_ulong,
            ) == 0 as libc::c_int) as libc::c_int;
        if rdb_file != 0 {
            fclose(fp);
            return 1 as libc::c_int;
        }
    }
    fclose(fp);
    return 0 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn fileIsManifest(mut filepath: *mut libc::c_char) -> libc::c_int {
    let mut is_manifest: libc::c_int = 0 as libc::c_int;
    let mut fp: *mut FILE = fopen(filepath, b"r\0" as *const u8 as *const libc::c_char);
    if fp.is_null() {
        printf(
            b"Cannot open file %s: %s\n\0" as *const u8 as *const libc::c_char,
            filepath,
            strerror(*__errno_location()),
        );
        exit(1 as libc::c_int);
    }
    let mut sb: stat = stat {
        st_dev: 0,
        st_ino: 0,
        st_mode: 0,
        st_nlink: 0,
        st_uid: 0,
        st_gid: 0,
        st_rdev: 0,
        __pad1: 0,
        st_size: 0,
        st_blksize: 0,
        __pad2: 0,
        st_blocks: 0,
        st_atim: timespec { tv_sec: 0, tv_nsec: 0 },
        st_mtim: timespec { tv_sec: 0, tv_nsec: 0 },
        st_ctim: timespec { tv_sec: 0, tv_nsec: 0 },
        __glibc_reserved: [0; 2],
    };
    if fstat(fileno(fp), &mut sb) == -(1 as libc::c_int) {
        printf(
            b"Cannot stat file: %s\n\0" as *const u8 as *const libc::c_char,
            filepath,
        );
        exit(1 as libc::c_int);
    }
    let mut size: off_t = sb.st_size;
    if size == 0 as libc::c_int as libc::c_long {
        fclose(fp);
        return 0 as libc::c_int;
    }
    let mut buf: [libc::c_char; 1025] = [0; 1025];
    loop {
        if (fgets(buf.as_mut_ptr(), 1024 as libc::c_int + 1 as libc::c_int, fp))
            .is_null()
        {
            if feof(fp) != 0 {
                break;
            }
            printf(
                b"Cannot read file: %s\n\0" as *const u8 as *const libc::c_char,
                filepath,
            );
            exit(1 as libc::c_int);
        } else {
            if buf[0 as libc::c_int as usize] as libc::c_int == '#' as i32 {
                continue;
            }
            if memcmp(
                buf.as_mut_ptr() as *const libc::c_void,
                b"file\0" as *const u8 as *const libc::c_char as *const libc::c_void,
                strlen(b"file\0" as *const u8 as *const libc::c_char),
            ) == 0
            {
                is_manifest = 1 as libc::c_int;
            }
        }
    }
    fclose(fp);
    return is_manifest;
}
#[no_mangle]
pub unsafe extern "C" fn getInputFileType(
    mut filepath: *mut libc::c_char,
) -> input_file_type {
    if fileIsManifest(filepath) != 0 {
        return AOF_MULTI_PART
    } else if fileIsRDB(filepath) != 0 {
        return AOF_RDB_PREAMBLE
    } else {
        return AOF_RESP
    };
}
#[no_mangle]
pub unsafe extern "C" fn checkMultiPartAof(
    mut dirpath: *mut libc::c_char,
    mut manifest_filepath: *mut libc::c_char,
    mut fix: libc::c_int,
) {
    let mut total_num: libc::c_int = 0 as libc::c_int;
    let mut aof_num: libc::c_int = 0 as libc::c_int;
    let mut last_file: libc::c_int = 0;
    let mut ret: libc::c_int = 0;
    printf(b"Start checking Multi Part AOF\n\0" as *const u8 as *const libc::c_char);
    let mut am: *mut aofManifest = aofLoadManifestFromFile(manifest_filepath);
    if !((*am).base_aof_info).is_null() {
        total_num += 1;
    }
    if !((*am).incr_aof_list).is_null() {
        total_num = (total_num as libc::c_ulong).wrapping_add((*(*am).incr_aof_list).len)
            as libc::c_int as libc::c_int;
    }
    if !((*am).base_aof_info).is_null() {
        let mut aof_filename: sds = (*(*am).base_aof_info).file_name;
        let mut aof_filepath: sds = makePath(dirpath, aof_filename);
        aof_num += 1;
        last_file = (aof_num == total_num) as libc::c_int;
        let mut aof_preable: libc::c_int = fileIsRDB(aof_filepath);
        printf(
            b"Start to check BASE AOF (%s format).\n\0" as *const u8
                as *const libc::c_char,
            if aof_preable != 0 {
                b"RDB\0" as *const u8 as *const libc::c_char
            } else {
                b"RESP\0" as *const u8 as *const libc::c_char
            },
        );
        ret = checkSingleAof(aof_filename, aof_filepath, last_file, fix, aof_preable);
        if ret == 0 as libc::c_int {
            printf(
                b"BASE AOF %s is valid\n\0" as *const u8 as *const libc::c_char,
                aof_filename,
            );
        } else if ret == 1 as libc::c_int {
            printf(
                b"BASE AOF %s is empty\n\0" as *const u8 as *const libc::c_char,
                aof_filename,
            );
        } else if ret == 3 as libc::c_int {
            printf(
                b"Successfully truncated AOF %s to timestamp %ld\n\0" as *const u8
                    as *const libc::c_char,
                aof_filename,
                to_timestamp,
            );
        } else if ret == 2 as libc::c_int {
            printf(
                b"Successfully truncated AOF %s\n\0" as *const u8 as *const libc::c_char,
                aof_filename,
            );
        }
        sdsfree(aof_filepath);
    }
    if (*(*am).incr_aof_list).len != 0 {
        let mut ln: *mut listNode = 0 as *mut listNode;
        let mut li: listIter = listIter {
            next: 0 as *mut listNode,
            direction: 0,
        };
        printf(b"Start to check INCR files.\n\0" as *const u8 as *const libc::c_char);
        listRewind((*am).incr_aof_list, &mut li);
        loop {
            ln = listNext(&mut li);
            if ln.is_null() {
                break;
            }
            let mut ai: *mut aofInfo = (*ln).value as *mut aofInfo;
            let mut aof_filename_0: sds = (*ai).file_name;
            let mut aof_filepath_0: sds = makePath(dirpath, aof_filename_0);
            aof_num += 1;
            last_file = (aof_num == total_num) as libc::c_int;
            ret = checkSingleAof(
                aof_filename_0,
                aof_filepath_0,
                last_file,
                fix,
                0 as libc::c_int,
            );
            if ret == 0 as libc::c_int {
                printf(
                    b"INCR AOF %s is valid\n\0" as *const u8 as *const libc::c_char,
                    aof_filename_0,
                );
            } else if ret == 1 as libc::c_int {
                printf(
                    b"INCR AOF %s is empty\n\0" as *const u8 as *const libc::c_char,
                    aof_filename_0,
                );
            } else if ret == 3 as libc::c_int {
                printf(
                    b"Successfully truncated AOF %s to timestamp %ld\n\0" as *const u8
                        as *const libc::c_char,
                    aof_filename_0,
                    to_timestamp,
                );
            } else if ret == 2 as libc::c_int {
                printf(
                    b"Successfully truncated AOF %s\n\0" as *const u8
                        as *const libc::c_char,
                    aof_filename_0,
                );
            }
            sdsfree(aof_filepath_0);
        }
    }
    aofManifestFree(am);
    printf(
        b"All AOF files and manifest are valid\n\0" as *const u8 as *const libc::c_char,
    );
}
#[no_mangle]
pub unsafe extern "C" fn checkOldStyleAof(
    mut filepath: *mut libc::c_char,
    mut fix: libc::c_int,
    mut preamble: libc::c_int,
) {
    printf(b"Start checking Old-Style AOF\n\0" as *const u8 as *const libc::c_char);
    let mut ret: libc::c_int = checkSingleAof(
        filepath,
        filepath,
        1 as libc::c_int,
        fix,
        preamble,
    );
    if ret == 0 as libc::c_int {
        printf(b"AOF %s is valid\n\0" as *const u8 as *const libc::c_char, filepath);
    } else if ret == 1 as libc::c_int {
        printf(b"AOF %s is empty\n\0" as *const u8 as *const libc::c_char, filepath);
    } else if ret == 3 as libc::c_int {
        printf(
            b"Successfully truncated AOF %s to timestamp %ld\n\0" as *const u8
                as *const libc::c_char,
            filepath,
            to_timestamp,
        );
    } else if ret == 2 as libc::c_int {
        printf(
            b"Successfully truncated AOF %s\n\0" as *const u8 as *const libc::c_char,
            filepath,
        );
    }
}
#[no_mangle]
pub unsafe extern "C" fn redis_check_aof_main(
    mut argc: libc::c_int,
    mut argv: *mut *mut libc::c_char,
) -> libc::c_int {
    let mut type_0: input_file_type = AOF_RESP;
    let mut current_block: u64;
    let mut filepath: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut temp_filepath: [libc::c_char; 4097] = [0; 4097];
    let mut dirpath: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut fix: libc::c_int = 0 as libc::c_int;
    if !(argc < 2 as libc::c_int) {
        if argc == 2 as libc::c_int {
            filepath = *argv.offset(1 as libc::c_int as isize);
            current_block = 13797916685926291137;
        } else if argc == 3 as libc::c_int {
            if strcmp(
                *argv.offset(1 as libc::c_int as isize),
                b"--fix\0" as *const u8 as *const libc::c_char,
            ) == 0
            {
                filepath = *argv.offset(2 as libc::c_int as isize);
                fix = 1 as libc::c_int;
                current_block = 13797916685926291137;
            } else {
                current_block = 12445938659794055887;
            }
        } else if argc == 4 as libc::c_int {
            if strcmp(
                *argv.offset(1 as libc::c_int as isize),
                b"--truncate-to-timestamp\0" as *const u8 as *const libc::c_char,
            ) == 0
            {
                let mut endptr: *mut libc::c_char = 0 as *mut libc::c_char;
                *__errno_location() = 0 as libc::c_int;
                to_timestamp = strtol(
                    *argv.offset(2 as libc::c_int as isize),
                    &mut endptr,
                    10 as libc::c_int,
                );
                if *__errno_location() != 0 as libc::c_int
                    || *endptr as libc::c_int != '\0' as i32
                {
                    printf(
                        b"Invalid timestamp, aborting...\n\0" as *const u8
                            as *const libc::c_char,
                    );
                    exit(1 as libc::c_int);
                }
                filepath = *argv.offset(3 as libc::c_int as isize);
                current_block = 13797916685926291137;
            } else {
                current_block = 12445938659794055887;
            }
        } else {
            current_block = 12445938659794055887;
        }
        match current_block {
            12445938659794055887 => {}
            _ => {
                memcpy(
                    temp_filepath.as_mut_ptr() as *mut libc::c_void,
                    filepath as *const libc::c_void,
                    (strlen(filepath)).wrapping_add(1 as libc::c_int as libc::c_ulong),
                );
                dirpath = dirname(temp_filepath.as_mut_ptr());
                type_0 = getInputFileType(filepath);
                match type_0 as libc::c_uint {
                    2 => {
                        checkMultiPartAof(dirpath, filepath, fix);
                    }
                    0 => {
                        checkOldStyleAof(filepath, fix, 0 as libc::c_int);
                    }
                    1 => {
                        checkOldStyleAof(filepath, fix, 1 as libc::c_int);
                    }
                    _ => {}
                }
                exit(0 as libc::c_int);
            }
        }
    }
    printf(
        b"Usage: %s [--fix|--truncate-to-timestamp $timestamp] <file.manifest|file.aof>\n\0"
            as *const u8 as *const libc::c_char,
        *argv.offset(0 as libc::c_int as isize),
    );
    exit(1 as libc::c_int);
}