aboutsummaryrefslogblamecommitdiffstats
path: root/lib/kernel/test/disk_log_SUITE.erl
blob: ad987fe7a710fa458150070c58b3013a328ea7ee (plain) (tree)
1
2
3
4
5
6
7
8
9
10
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
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924

                   
  
                                                        
  




                                                                      
  



                                                                         
  













                                             
                                                    




                                                

                                                                    
 
                         

                                          
                                                      


                                                                   
                                                     
 
                        
 
                                          
 
                                    
 
                                                 
 
                                                                    




                        
                                                        


                   
                                                                      
 
                                                       
 
                                                    






                      
                         
 
                                                     




                                                             
                                                        









                                                                 
                                                   














































                                                                         
                                         
 
         







                                                          


                                                          

























                                                              
 





                         
                                     
           

                                    
           
 






















                                                  
                                  



                                     













                                                                          






















































































                                                                       








































































































                                                                                       
                                          






























































































                                                                              


















































































































































                                                                               












                                                                          


























































































                                                                       



























































































































































































































































































































































































































                                                                               


















































































































































































                                                                             































































































































































































































                                                                               



















                                                                         
                                                                              

























































































































































































































                                                                               
                    
                



                                                                           


                                   
                                                



























                                                                           






















































































































































































































































                                                                               


























































































































































































































































































































































                                                                          


                                       






































































                                                                           















































































































































































                                                                               
                                                                      









                                                                          
                                                                      

































































































































































































































































































































































































































































































                                                                               
                                                                             







                                                              
                     




































                                                                          











































































































































































































                                                                          




























































































































































































































































































































































































































































































































































































































































































                                                                                            





































































































































































































































                                                                             


                                       

















































































































































































































































































































































































































































































































































































                                                                              
                                             














                                                             
                                             























































































































































































































































                                                                              
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(disk_log_SUITE).

%-define(debug, true).

-ifdef(debug).
-define(format(S, A), io:format(S, A)).
-define(line, put(line, ?LINE), ).
-define(privdir(_), "./disk_log_SUITE_priv").
-define(datadir(_), "./disk_log_SUITE_data").
-define(config(X,Y), foo).
-define(t,test_server).
-else.
-include_lib("test_server/include/test_server.hrl").
-define(format(S, A), ok).
-define(privdir(Conf), ?config(priv_dir, Conf)).
-define(datadir(Conf), ?config(data_dir, Conf)).
-endif.

-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2, 

	 halt_int_inf/1, 
	 halt_int_sz_1/1, halt_int_sz_2/1,

	 halt_int_ro/1, halt_ext_ro/1, wrap_int_ro/1, 
	 wrap_ext_ro/1, halt_trunc/1, halt_misc/1, halt_ro_alog/1, 
	 halt_ro_balog/1, halt_ro_crash/1,

	 wrap_int_1/1, wrap_int_2/1, inc_wrap_file/1,

	 halt_ext_inf/1,

	 halt_ext_sz_1/1, halt_ext_sz_2/1,

	 wrap_ext_1/1, wrap_ext_2/1,

	 head_func/1, plain_head/1, one_header/1,

	 wrap_notif/1, full_notif/1, trunc_notif/1, blocked_notif/1,

	 new_idx_vsn/1, 

	 reopen/1, 

	 block_blocked/1, block_queue/1, block_queue2/1,

	 unblock/1,

	 open_overwrite/1, open_size/1, open_truncate/1, open_error/1,

	 close_race/1, close_block/1, close_deadlock/1,

	 error_repair/1, error_log/1, error_index/1,

	 chunk/1, 

	 truncate/1,

	 many_users/1,

	 info_current/1, 

	 change_size_before/1, change_size_during/1, 
	 change_size_after/1, default_size/1, change_size2/1,
	 change_size_truncate/1,

	 change_attribute/1,

	 dist_open/1, dist_error_open/1, dist_notify/1, 
	 dist_terminate/1, dist_accessible/1, dist_deadlock/1,
         dist_open2/1, other_groups/1,

         evil/1,

         otp_6278/1]).

-export([head_fun/1, hf/0, lserv/1, 
	 measure/0, init_m/1, xx/0, head_exit/0, slow_header/1]).

-export([init_per_testcase/2, end_per_testcase/2]).

-export([try_unblock/1]).

-export([client/4]).

-define(default_timeout, ?t:minutes(1)).

%% error_logger
-export([init/1,
	 handle_event/2, handle_call/2, handle_info/2,
	 terminate/2]).

-include_lib("kernel/include/file.hrl").
-include_lib("kernel/src/disk_log.hrl").

%% TODO (old):
%%   - global logs
%%   - badarg
%%   - force file:write fail (how?)
%%   - kill logging proc while he is logging
%%   - kill logging node while he is logging
%%   - test chunk_step

%% These are all tests, the list to be returned by all().
-define(ALL_TESTS,
	[halt_int, wrap_int, halt_ext, wrap_ext, read_mode, head,
	 notif, new_idx_vsn, reopen, block, unblock, open, close,
	 error, chunk, truncate, many_users, info, change_size,
	 change_attribute, distribution, evil, otp_6278]).

%% The following two lists should be mutually exclusive. To skip a case
%% on VxWorks altogether, use the kernel.spec.vxworks file instead.
%% PLEASE don't skip out of laziness, the goal is to make every
%% testcase runnable on VxWorks.

%% These test cases should be skipped if the VxWorks card is 
%% configured without NFS cache.
-define(SKIP_NO_CACHE,[distribution]).
%% These tests should be skipped if the VxWorks card is configured *with*
%% nfs cache.
-define(SKIP_LARGE_CACHE,[inc_wrap_file, halt_ext, wrap_ext, read_mode, 
			  head, wrap_notif, open_size, error_log, 
                          error_index, chunk, 
			  change_size_before, change_size_during, 
			  change_size_after, default_size]).


suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    [{group, halt_int}, {group, wrap_int},
     {group, halt_ext}, {group, wrap_ext},
     {group, read_mode}, {group, head}, {group, notif},
     new_idx_vsn, reopen, {group, block}, unblock,
     {group, open}, {group, close}, {group, error}, chunk,
     truncate, many_users, {group, info},
     {group, change_size}, change_attribute,
     {group, distribution}, evil, otp_6278].

groups() -> 
    [{halt_int, [], [halt_int_inf, {group, halt_int_sz}]},
     {halt_int_sz, [], [halt_int_sz_1, halt_int_sz_2]},
     {read_mode, [],
      [halt_int_ro, halt_ext_ro, wrap_int_ro, wrap_ext_ro,
       halt_trunc, halt_misc, halt_ro_alog, halt_ro_balog,
       halt_ro_crash]},
     {wrap_int, [], [wrap_int_1, wrap_int_2, inc_wrap_file]},
     {halt_ext, [], [halt_ext_inf, {group, halt_ext_sz}]},
     {halt_ext_sz, [], [halt_ext_sz_1, halt_ext_sz_2]},
     {wrap_ext, [], [wrap_ext_1, wrap_ext_2]},
     {head, [], [head_func, plain_head, one_header]},
     {notif, [],
      [wrap_notif, full_notif, trunc_notif, blocked_notif]},
     {block, [], [block_blocked, block_queue, block_queue2]},
     {open, [],
      [open_overwrite, open_size, open_truncate, open_error]},
     {close, [], [close_race, close_block, close_deadlock]},
     {error, [], [error_repair, error_log, error_index]},
     {info, [], [info_current]},
     {change_size, [],
      [change_size_before, change_size_during,
       change_size_after, default_size, change_size2,
       change_size_truncate]},
     {distribution, [],
      [dist_open, dist_error_open, dist_notify,
       dist_terminate, dist_accessible, dist_deadlock,
       dist_open2, other_groups]}].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.



init_per_testcase(Case, Config) ->
    case should_skip(Case,Config) of
	true ->
	    CS = check_nfs(Config),
	    {skipped, lists:flatten
	     (io_lib:format
	      ("The test does not work "
	       "with current NFS cache size (~w),"
	       " to get this test to run, "
	       "~s the NFS cache size~n",  
	       [CS, case CS of
			0 ->
			    "enlarge";
			_ ->
			    "zero"
		    end]))};
	_ ->
	    Dog=?t:timetrap(?t:minutes(2)),
	    [{watchdog, Dog}|Config]
    end.

end_per_testcase(_Case, Config) ->
    Dog=?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    ok.


halt_int_inf(suite) -> [];
halt_int_inf(doc) -> ["Test simple halt disk log, size infinity"];
halt_int_inf(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    ?line ok = disk_log:start(),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal},
				   {file, File}]),
    ?line simple_log(a),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).


halt_int_sz_1(suite) -> [];
halt_int_sz_1(doc) -> ["Test simple halt disk log, size defined"];
halt_int_sz_1(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,18000},
				   {format,internal},
				   {file, File}]),
    ?line simple_log(a),
    ?line ok = disk_log:truncate(a),
    ?line [] = get_all_terms(a),
    T1 = mk_bytes(10000),
    T2 = mk_bytes(5000),
    ?line ok = disk_log:log(a, T1),
    ?line case get_all_terms(a) of
	      [T1] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, [T1]})
	  end,
    ?line ok = disk_log:log(a, T2),
    ?line {error, {full, a}} = disk_log:log(a, T1),
    ?line ok = disk_log:alog(a, T1),
    ?line case get_all_terms(a) of
	      [T1, T2] ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, [T1, T2]})
	  end,
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

halt_int_sz_2(suite) -> [];
halt_int_sz_2(doc) -> ["Test simple halt disk log, size ~8192"];
halt_int_sz_2(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File1 = filename:join(Dir, "a.LOG"),
    File2 = filename:join(Dir, "b.LOG"),
    File3 = filename:join(Dir, "c.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,8191},
				   {format,internal},
				   {file, File1}]),
    ?line {ok, b} = disk_log:open([{name,b}, {type,halt}, {size,8192},
				   {format,internal},
				   {file, File2}]),
    ?line {ok, c} = disk_log:open([{name,c}, {type,halt}, {size,8193},
				   {format,internal},
				   {file, File3}]),
    T1 = mk_bytes(8191-16), % 16 is size of header + magics for 1 item
    T2 = mk_bytes(8192-16),
    T3 = mk_bytes(8193-16),
    ?line ok = disk_log:log(a, T1),
    ?line ok = disk_log:log(b, T2),
    ?line ok = disk_log:log(c, T3),
    ?line case get_all_terms(a) of
	      [T1] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, [T1]})
	  end,
    ?line case get_all_terms(b) of
	      [T2] ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, [T2]})
	  end,
    ?line case get_all_terms(c) of
	      [T3] ->
		  ok;
	      E3 ->
		  test_server_fail({bad_terms, E3, [T3]})
	  end,
    ?line ok = disk_log:truncate(a),
    ?line ok = disk_log:truncate(b),
    ?line {error, {full, a}} = disk_log:log(a, T2),
    ?line {error, {full, b}} = disk_log:log(b, T3),
    ?line [] = get_all_terms(a),
    ?line [] = get_all_terms(b),
    ?line ok = disk_log:close(a),
    ?line ok = disk_log:close(b),
    ?line ok = disk_log:close(c),
    ?line ok = file:delete(File1),
    ?line ok = file:delete(File2),
    ?line ok = file:delete(File3),
    ok.


halt_int_ro(suite) -> [];
halt_int_ro(doc) -> ["Test simple halt disk log, read only, internal"];
halt_int_ro(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),

    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),

    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File},
				   {mode,read_only}]),
    T1 = "not allowed to write",
    ?line {error, {read_only_mode, a}} = disk_log:log(a, T1),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

halt_ext_ro(suite) -> [];
halt_ext_ro(doc) -> ["Test simple halt disk log, read only, external"];
halt_ext_ro(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,external}, {file, File}]),
    xsimple_log(File, a),
    ?line ok = disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,external}, {file, File},
				   {mode,read_only}]),
    T1 = "not allowed to write",
    ?line {error, {read_only_mode, a}}  = disk_log:blog(a, T1),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

wrap_int_ro(suite) -> [];
wrap_int_ro(doc) -> ["Test simple wrap disk log, read only, internal"];
wrap_int_ro(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,internal}, {file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,internal}, {file, File}, {mode,read_only}]),
    T1 = "not allowed to write",
    ?line {error, {read_only_mode, a}} = disk_log:log(a, T1),
    ?line ok = disk_log:close(a),
    ?line del(File, 4).

wrap_ext_ro(suite) -> [];
wrap_ext_ro(doc) -> ["Test simple wrap disk log, read only, external"];
wrap_ext_ro(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,external}, {file, File}]),
    x2simple_log(File ++ ".1", a),
    ?line ok = disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,external}, {file, File},
				   {mode,read_only}]),
    T1 = "not allowed to write",
    ?line {error, {read_only_mode, a}}  = disk_log:blog(a, T1),
    ?line {error, {read_only_mode, a}}  = disk_log:inc_wrap_file(a),
    ?line ok = disk_log:close(a),
    del(File, 4).

halt_trunc(suite) -> [];
halt_trunc(doc) -> ["Test truncation of halt disk log"];
halt_trunc(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),
    ?line {error,{badarg,repair_read_only}} =
	disk_log:open([{name,a}, {type,halt}, {size,infinity},
		       {repair, truncate}, {format,internal},
		       {file, File}, {mode,read_only}]),
    ?line ok = file:delete(File).

halt_misc(suite) -> [];
halt_misc(doc) -> ["Test truncation of halt disk log"];
halt_misc(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File},
				   {mode,read_only}]),
    T1 = "not allowed to write",
    ?line {error, {read_only_mode, a}} = disk_log:log(a, T1),
    ?line {error, {read_only_mode, a}} = disk_log:sync(a),
    ?line {error, {read_only_mode, a}} = disk_log:reopen(a, "b.LOG"),
    ?line {error, {read_only_mode, a}} = 
        disk_log:change_header(a, {head,header}),
    ?line {error, {read_only_mode, a}} = 
        disk_log:change_size(a, infinity),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

halt_ro_alog(suite) -> [];
halt_ro_alog(doc) -> ["Test truncation of halt disk log, read only"];
halt_ro_alog(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {notify,true}, {format,internal},
				   {file, File}, {mode,read_only}]),
    T1 = "not allowed to write",
    ?line ok = disk_log:alog(a, T1),
    ?line ok = halt_ro_alog_wait_notify(a, T1),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

halt_ro_alog_wait_notify(Log, T) ->
    Term = term_to_binary(T),
    receive
	{disk_log, _, Log,{read_only, Term}} ->
	    ok;
	Other ->
	    Other
    after 5000 ->
	    failed
    end.

halt_ro_balog(suite) -> [];
halt_ro_balog(doc) -> ["Test truncation of halt disk log, read only"];
halt_ro_balog(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,internal}, {file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {notify,true}, {format,external},
				   {file, File}, {mode,read_only}]),
    T1 = "not allowed to write",
    ?line ok = disk_log:balog(a, T1),
    ?line ok = halt_ro_balog_wait_notify(a, T1),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

halt_ro_balog_wait_notify(Log, T) ->
    Term = list_to_binary(T),
    receive
	{disk_log, _, Log,{read_only, Term}} ->
	    ok;
	Other ->
	    Other
    after 5000 ->
	    failed
    end.

halt_ro_crash(suite) -> [];
halt_ro_crash(doc) -> ["Test truncation of halt disk log, read only, repair"];
halt_ro_crash(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),

    ?line file:delete(File),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
			   {format,internal},{file, File}]),
    simple_log(a),
    ?line ok = disk_log:close(a),
    crash(File, 10),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {notify,true}, {format,internal},
				   {file, File}, {mode,read_only}]),

    ?line Error1 = {error, {read_only_mode, a}} = disk_log:truncate(a),
    ?line "The disk log" ++ _ = format_error(Error1),

    %% crash/1 sets the length of the first item to something big (2.5 kb).
    %% In R6B, binary_to_term accepts garbage at the end of the binary,
    %% which means that the first item is recognized!
    %% This is how it was before R6B:
    %% ?line {C1,T1,15} = disk_log:chunk(a,start),
    %% ?line {C2,T2} = disk_log:chunk(a,C1),
    {C1,_OneItem,7478} = disk_log:chunk(a,start),
    {C2, [], 7} = disk_log:chunk(a,C1),
    ?line eof = disk_log:chunk(a,C2),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).





wrap_int_1(suite) -> [];
wrap_int_1(doc) -> ["Test wrap disk log, internal"];
wrap_int_1(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,internal},
				   {file, File}]),
    ?line [_] = 
        lists:filter(fun(P) -> disk_log:pid2name(P) =/= undefined end, 
                     erlang:processes()),
    simple_log(a),
    ?line ok = disk_log:close(a),
    del(File, 4),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,internal},
				   {file, File}]),
    ?line [] = get_all_terms(a),
    T1 = mk_bytes(10000), % file 2
    T2 = mk_bytes(5000),  % file 3
    T3 = mk_bytes(4000),  % file 4
    T4 = mk_bytes(2000),  % file 4
    T5 = mk_bytes(5000),  % file 1
    T6 = mk_bytes(5000),  % file 2
    ?line ok = disk_log:log(a, T1),
    ?line ok = disk_log:log(a, T2),
    ?line ok = disk_log:log(a, T3),
    ?line ok = disk_log:log_terms(a, [T4, T5, T6]),
    ?line case get_all_terms(a) of
	      [T2,T3,T4,T5,T6] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, [T2,T3,T4,T5,T6]})
	  end,
    ?line ok = disk_log:close(a),
    del(File, 4).

wrap_int_2(suite) -> [];
wrap_int_2(doc) -> ["Test wrap disk log, internal"];
wrap_int_2(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File1 = filename:join(Dir, "a.LOG"),
    File2 = filename:join(Dir, "b.LOG"),
    File3 = filename:join(Dir, "c.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8191,3}},
				   {format,internal},
				   {file, File1}]),
    ?line {ok, b} = disk_log:open([{name,b}, {type,wrap}, {size,{8192,3}},
				   {format,internal},
				   {file, File2}]),
    ?line {ok, c} = disk_log:open([{name,c}, {type,wrap}, {size,{8193,3}},
				   {format,internal},
				   {file, File3}]),
    T1 = mk_bytes(8191-16), % 16 is size of header + magics for 1 item
    T2 = mk_bytes(8192-16),
    T3 = mk_bytes(8193-16),
    ?line ok = disk_log:log(a, T1),
    ?line ok = disk_log:log(b, T2),
    ?line ok = disk_log:log(c, T3),
    ?line case get_all_terms(a) of
	      [T1] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, [T1]})
	  end,
    ?line case get_all_terms(b) of
	      [T2] ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, [T2]})
	  end,
    ?line case get_all_terms(c) of
	      [T3] ->
		  ok;
	      E3 ->
		  test_server_fail({bad_terms, E3, [T3]})
	  end,
    ?line ok = disk_log:close(a),
    ?line ok = disk_log:close(b),
    ?line ok = disk_log:close(c),
    del(File1, 3),
    del(File2, 3),
    del(File3, 3).

inc_wrap_file(suite) -> [];
inc_wrap_file(doc) -> ["Test disk log, force a change to next file"];
inc_wrap_file(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File1 = filename:join(Dir, "a.LOG"),
    File2 = filename:join(Dir, "b.LOG"),
    File3 = filename:join(Dir, "c.LOG"),

    %% Test that halt logs gets an error message
    ?line {ok, a} = disk_log:open([{name, a}, {type, halt}, 
				   {format, internal},
				   {file, File1}]),
    ?line ok = disk_log:log(a, "message one"),
    ?line {error, {halt_log, a}} = disk_log:inc_wrap_file(a),

    %% test an internally formatted wrap log file
    ?line {ok, b} = disk_log:open([{name, b}, {type, wrap}, {size, {100,3}},
				   {format, internal}, {head, 'thisisahead'},
				   {file, File2}]),
    ?line ok = disk_log:log(b, "message one"),
    ?line ok = disk_log:inc_wrap_file(b),
    ?line ok = disk_log:log(b, "message two"),
    ?line ok = disk_log:inc_wrap_file(b),
    ?line ok = disk_log:log(b, "message three"),
    ?line ok = disk_log:inc_wrap_file(b),
    ?line ok = disk_log:log(b, "message four"),
    ?line T1 = get_all_terms(b),
    ?line ['thisisahead', "message two", 
	   'thisisahead', "message three", 
	   'thisisahead', "message four"] = T1,

    %% test an externally formatted wrap log file
    ?line {ok, c} = disk_log:open([{name, c}, {type, wrap}, {size, {100,3}},
				   {format,external}, {head,"this is a head "},
				   {file, File3}]),
    ?line ok = disk_log:blog(c, "message one"),
    ?line ok = disk_log:inc_wrap_file(c),
    ?line ok = disk_log:blog(c, "message two"),
    ?line ok = disk_log:inc_wrap_file(c),
    ?line ok = disk_log:blog(c, "message three"),
    ?line ok = disk_log:inc_wrap_file(c),
    ?line ok = disk_log:blog(c, "message four"),
    ?line ok = disk_log:sync(c),
    ?line {ok, Fd31} = file:open(File3 ++ ".1", [read]),
    ?line {ok,"this is a head message four"} = file:read(Fd31, 200),
    ?line {ok, Fd32} = file:open(File3 ++ ".2", [read]),
    ?line {ok,"this is a head message two"} = file:read(Fd32, 200),
    ?line {ok, Fd33} = file:open(File3 ++ ".3", [read]),
    ?line {ok,"this is a head message three"} = file:read(Fd33, 200),
    ?line ok = file:close(Fd31),
    ?line ok = file:close(Fd32),
    ?line ok = file:close(Fd33),

    ?line ok = disk_log:close(a),
    ?line ok = disk_log:close(b),
    ?line ok = disk_log:close(c),
    ?line ok = file:delete(File1),
    del(File2, 3),
    del(File3, 3).




halt_ext_inf(suite) -> [];
halt_ext_inf(doc) -> ["Test halt disk log, external, infinity"];
halt_ext_inf(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
				   {format,external},
				   {file, File}]),
    ?line xsimple_log(File, a),
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).


halt_ext_sz_1(suite) -> [];
halt_ext_sz_1(doc) -> ["Test halt disk log, external, size defined"];
halt_ext_sz_1(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,18000},
				   {format,external},
				   {file, File}]),
    xsimple_log(File, a),
    ?line ok = disk_log:truncate(a),
    ?line [] = get_list(File, a),
    {B1, T1} = x_mk_bytes(10000),
    {B2, T2} = x_mk_bytes(5000),
    {B3, T3} = x_mk_bytes(1000),
    ?line ok = disk_log:blog(a, B1),
    ?line case get_list(File, a) of
	      T1 ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, T1})
	  end,
    ?line ok = disk_log:blog(a, B2),
    ?line {error, {full, a}} = disk_log:blog_terms(a, [B3,B3,B1]),
    ?line ok = disk_log:balog(a, B1),
    ?line Tmp = T1 ++ T2 ++ T3 ++ T3,
    ?line case get_list(File, a) of
	      Tmp ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, Tmp})
	  end,
    ?line ok = disk_log:close(a),
    ?line ok = file:delete(File).

halt_ext_sz_2(suite) -> [];
halt_ext_sz_2(doc) -> ["Test halt disk log, external, size defined"];
halt_ext_sz_2(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File1 = filename:join(Dir, "a.LOG"),
    File2 = filename:join(Dir, "b.LOG"),
    File3 = filename:join(Dir, "c.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,8191},
				   {format,external},
				   {file, File1}]),
    ?line {ok, b} = disk_log:open([{name,b}, {type,halt}, {size,8192},
				   {format,external},
				   {file, File2}]),
    ?line {ok, c} = disk_log:open([{name,c}, {type,halt}, {size,8193},
				   {format,external},
				   {file, File3}]),
    {B1, T1} = x_mk_bytes(8191),
    {B2, T2} = x_mk_bytes(8192),
    {B3, T3} = x_mk_bytes(8193),
    ?line ok = disk_log:blog(a, B1),
    ?line ok = disk_log:blog(b, B2),
    ?line ok = disk_log:blog(c, B3),
    ?line case get_list(File1, a) of
	      T1 ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, T1})
	  end,
    ?line case get_list(File2, b) of
	      T2 ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, T2})
	  end,
    ?line case get_list(File3, c) of
	      T3 ->
		  ok;
	      E3 ->
		  test_server_fail({bad_terms, E3, T3})
	  end,
    ?line ok = disk_log:truncate(a),
    ?line ok = disk_log:truncate(b),
    ?line {error, {full, a}} = disk_log:blog(a, B2),
    ?line Error1 = {error, {full, b}} = disk_log:blog(b, B3),
    ?line "The halt log" ++ _ = format_error(Error1),
    ?line true = info(b, full, false),
    ?line [] = get_list(File1, a),
    ?line [] = get_list(File2, b),
    ?line ok = disk_log:close(a),
    ?line ok = disk_log:close(b),
    ?line ok = disk_log:close(c),
    ?line ok = file:delete(File1),
    ?line ok = file:delete(File2),
    ?line ok = file:delete(File3),
    ok.


wrap_ext_1(suite) -> [];
wrap_ext_1(doc) -> ["Test wrap disk log, external, size defined"];
wrap_ext_1(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,external},
				   {file, File}]),
    x2simple_log(File ++ ".1", a),
    ?line ok = disk_log:close(a),
%    del(File, 4),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8000, 4}},
				   {format,external},
				   {file, File}]),
    {B1, _T1} = x_mk_bytes(10000), % file 2
    {B2, T2} = x_mk_bytes(5000),  % file 3
    {B3, T3} = x_mk_bytes(4000),  % file 4
    {B4, T4} = x_mk_bytes(2000),  % file 4
    {B5, T5} = x_mk_bytes(5000),  % file 1
    {B6, T6} = x_mk_bytes(5000),  % file 2
    ?line ok = disk_log:blog(a, B1),
    ?line ok = disk_log:blog(a, B2),
    ?line ok = disk_log:blog(a, B3),
    ?line ok = disk_log:blog_terms(a, [B4, B5, B6]),
    ?line case get_list(File ++ ".3", a) of
	      T2 ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, T2})
	  end,
    ?line T34 = T3 ++ T4,
    ?line case get_list(File ++ ".4", a) of
	      T34 ->
		  ok;
	      E34 ->
		  test_server_fail({bad_terms, E34, T34})
	  end,
    ?line case get_list(File ++ ".1", a) of
	      T5 ->
		  ok;
	      E5 ->
		  test_server_fail({bad_terms, E5, T5})
	  end,
    ?line case get_list(File ++ ".2", a) of
	      T6 ->
		  ok;
	      E6 ->
		  test_server_fail({bad_terms, E6, T6})
	  end,
    ?line ok = disk_log:close(a),
    del(File, 4).

wrap_ext_2(suite) -> [];
wrap_ext_2(doc) -> ["Test wrap disk log, external, size defined"];
wrap_ext_2(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File1 = filename:join(Dir, "a.LOG"),
    File2 = filename:join(Dir, "b.LOG"),
    File3 = filename:join(Dir, "c.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{8191,3}},
				   {format,external},
				   {file, File1}]),
    ?line {ok, b} = disk_log:open([{name,b}, {type,wrap}, {size,{8192,3}},
				   {format,external},
				   {file, File2}]),
    ?line {ok, c} = disk_log:open([{name,c}, {type,wrap}, {size,{8193,3}},
				   {format,external},
				   {file, File3}]),
    {B1, T1} = x_mk_bytes(8191),
    {B2, T2} = x_mk_bytes(8192),
    {B3, T3} = x_mk_bytes(8193),
    ?line ok = disk_log:blog(a, B1),
    ?line ok = disk_log:blog(b, B2),
    ?line ok = disk_log:blog(c, B3),
    ?line case get_list(File1 ++ ".1", a) of
	      T1 ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, T1})
	  end,
    ?line case get_list(File2 ++ ".1", b) of
	      T2 ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, T2})
	  end,
    ?line case get_list(File3 ++ ".1", c) of
	      T3 ->
		  ok;
	      E3 ->
		  test_server_fail({bad_terms, E3, T3})
	  end,
    ?line ok = disk_log:close(a),
    ?line ok = disk_log:close(b),
    ?line ok = disk_log:close(c),
    ?line del(File1, 3),
    ?line del(File2, 3),
    ?line del(File3, 3),
    ok.

simple_log(Log) ->
    T1 = "hej",
    T2 = hopp,
    T3 = {tjena, 12},
    T4 = mk_bytes(10000),
    ?line ok = disk_log:log(Log, T1),
    ?line ok = disk_log:log_terms(Log, [T2, T3]),
    ?line case get_all_terms(Log) of
	      [T1, T2, T3] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, [T1, T2, T3]})
	  end,
    ?line ok = disk_log:log(a, T4),
    ?line case get_all_terms(Log) of
	      [T1, T2, T3, T4] ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, [T1, T2, T3, T4]})
	  end.

xsimple_log(File, Log) ->
    T1 = "hej",
    T2 = list_to_binary("hopp"),
    T3 = list_to_binary(["sena", list_to_binary("sejer")]),
    T4 = list_to_binary(By = mk_bytes(10000)),
    ?line ok = disk_log:blog(Log, T1),
    ?line ok = disk_log:blog_terms(Log, [T2, T3]),
    ?line X = "hejhoppsenasejer",
    ?line X2 = get_list(File, Log),
    ?line case X2 of
	      X -> ok;
	      Z1 -> test_server_fail({bad_terms, Z1, X2})
	  end,
    ?line ok = disk_log:blog(Log, T4),
    ?line Tmp = get_list(File, Log),
    ?line case X ++ By of
	      Tmp -> ok;
	      Z2 -> test_server_fail({bad_terms, Z2, X ++ By})
	  end.

x2simple_log(File, Log) ->
    T1 = "hej",
    T2 = list_to_binary("hopp"),
    T3 = list_to_binary(["sena", list_to_binary("sejer")]),
    T4 = list_to_binary(By = mk_bytes(1000)),
    ?line ok = disk_log:blog(Log, T1),
    ?line ok = disk_log:blog_terms(Log, [T2, T3]),
    ?line X = "hejhoppsenasejer",
    ?line X2 = get_list(File, Log),
    ?line case X2 of
	      X -> ok;
	      Z1 -> test_server_fail({bad_terms, Z1, X2})
	  end,
    ?line ok = disk_log:blog(Log, T4),
    ?line Tmp = get_list(File, Log),
    ?line case X ++ By of
	      Tmp -> ok;
	      Z2 -> test_server_fail({bad_terms, Z2, X ++ By})
	  end.

x_mk_bytes(N) ->
    X = lists:duplicate(N, $a),
    {list_to_binary(X), X}.

mk_bytes(N) when N > 4 ->
    X = lists:duplicate(N-4, $a),
    case byte_size(term_to_binary(X)) of
        N -> X;
        Z -> test_server_fail({bad_terms, Z, N})
    end.

get_list(File, Log) ->
    ?t:format(0, "File ~p~n",[File]),
    ok = disk_log:sync(Log),
    {ok, B} = file:read_file(File),
    binary_to_list(B).


get_all_terms(Log, File, Type) ->
    {ok, _Log} = disk_log:open([{name,Log}, {type,Type}, {size,infinity},
			       {format,internal}, {file, File}, 
				{mode, read_only}]),
    Ts = get_all_terms(Log),
    ok = disk_log:close(Log),
    Ts.

get_all_terms(Log) ->
    get_all_terms1(Log, start, []).

get_all_terms1(Log, Cont, Res) ->
    case disk_log:chunk(Log, Cont) of
	{error, _R} ->
	    test_server_fail({bad_chunk, Log, Cont});
	{Cont2, Terms} ->
	    get_all_terms1(Log, Cont2, Res ++ Terms);
	eof ->
	    Res
    end.

get_all_terms_and_bad(Log, File, Type) ->
    {ok, _Log} = disk_log:open([{name,Log}, {type,Type}, {size,infinity},
			       {format,internal}, {file, File}, 
				{mode, read_only}]),
    Ts = get_all_terms_and_bad(Log),
    ok = disk_log:close(Log),
    Ts.

get_all_terms_and_bad(Log) ->
    ?line read_only = info(Log, mode, foo),
    get_all_terms_and_bad1(Log, start, [], 0).

%% 
get_all_terms_and_bad1(Log, Cont, Res, Bad0) ->
    case disk_log:chunk(Log, Cont) of
	{Cont2, Terms} ->
	    get_all_terms_and_bad1(Log, Cont2, Res ++ Terms, Bad0);
	{Cont2, Terms, Bad} ->
	    get_all_terms_and_bad1(Log, Cont2, Res ++ Terms, Bad0+Bad);
	eof ->
	    {Res, Bad0}
    end.

get_all_binary_terms_and_bad(Log, File, Type) ->
    {ok, _Log} = disk_log:open([{name,Log}, {type,Type}, {size,infinity},
			       {format,internal}, {file, File}, 
				{mode, read_only}]),
    Ts = get_all_binary_terms_and_bad(Log),
    ok = disk_log:close(Log),
    Ts.

get_all_binary_terms_and_bad(Log) ->
    read_only = info(Log, mode, foo),
    get_all_binary_terms_and_bad1(Log, start, [], 0).

%% 
get_all_binary_terms_and_bad1(Log, Cont, Res, Bad0) ->
    case disk_log:bchunk(Log, Cont) of
	{Cont2, BinTerms} ->
	    get_all_binary_terms_and_bad1(Log, Cont2, Res ++ BinTerms, Bad0);
	{Cont2, BinTerms, Bad} ->
	    get_all_binary_terms_and_bad1(Log, Cont2, Res ++ BinTerms, 
                                          Bad0+Bad);
	eof ->
	    {Res, Bad0}
    end.

del(File, 0) ->
    file:delete(File ++ ".siz"),
    file:delete(File ++ ".idx");
del(File, N) ->
    file:delete(File ++ "." ++ integer_to_list(N)),
    del(File, N-1).

test_server_fail(R) ->
    exit({?MODULE, get(line), R}).

xx() ->
    File = "a.LOG",
    {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
			     {format,internal}, {file, File}]),
    W = xwr(a, 400),
    disk_log:close(a),
%    file:delete(File),
    W.

%% old: 6150
%% new: 5910
xwr(Log, BytesItem) ->
    NoW = 1000,
    Item1 = mk_bytes(BytesItem),
    Item2 = mk_bytes(BytesItem),
    Item3 = mk_bytes(BytesItem),
    Item4 = mk_bytes(BytesItem),
    Item5 = mk_bytes(BytesItem),
    Item6 = mk_bytes(BytesItem),
    Item7 = mk_bytes(BytesItem),
    Item8 = mk_bytes(BytesItem),
    Item9 = mk_bytes(BytesItem),
    Item0 = mk_bytes(BytesItem),
    Term = [Item1,Item2,Item3,Item4,Item5,Item6,Item7,Item8,Item9,Item0],
    {W, _} = timer:tc(?MODULE, wr, [Log, Term, NoW]),
    W/NoW.

measure() ->
    proc_lib:start_link(?MODULE, init_m, [self()]).

init_m(Par) ->
    process_flag(trap_exit, true),
    Res = m(),
    proc_lib:init_ack(Par, Res).

m() ->
    {W10, R10, Rep10, C10} = m_halt_int(10),
    {W11, R11, Rep11, C11} = m_halt_int(100),
    {W12, R12, Rep12, C12} = m_halt_int(400),
    {W13, R13, Rep13, C13} = m_halt_int(1000),
    {W14, R14, Rep14, C14} = m_halt_int(10000),
    {W2, R2, Rep2, C2} = m_wrap_int(400),
    {W3, R3, Rep3, C3} = m_many_halt_int(10, 400),
    {W4, R4, Rep4, C4} = m_many_halt_int(20, 400),
    {W5, R5, Rep5, C5} = m_many_halt_int(10, 1000),
    {W6, R6, Rep6, C6} = m_many_halt_int(10, 10),
    {W7, R7, Rep7, C7} = m_many_halt_int(20, 10),

    io:format("Type of log            mysec/write  mysec/read"
	      "  mysec/repair byte  cpu/write\n"),
    io:format("===========            ===========  =========="
	      "  =================  =========\n"),
    one_line("halt,int.inf. (10)", W10, R10, Rep10, C10),
    one_line("halt,int.inf. (100)", W11, R11, Rep11, C11),
    one_line("halt,int.inf. (400)", W12, R12, Rep12, C12),
    one_line("halt,int.inf. (1000)", W13, R13, Rep13, C13),
    one_line("halt,int.inf. (10000)", W14, R14, Rep14, C14),
    one_line("wrap,int.  4. (400)", W2, R2, Rep2, C2),
    one_line("halt,int.inf. (10,10)", W6, R6, Rep6, C6),
    one_line("halt,int.inf. (20,10)", W7, R7, Rep7, C7),
    one_line("halt,int.inf. (10,400)", W3, R3, Rep3, C3),
    one_line("halt,int.inf. (20,400)", W4, R4, Rep4, C4),
    one_line("halt,int.inf. (10,1000)", W5, R5, Rep5, C5),
    io:format("\n"),
    io:format("\tWrap log time depends on how often the log wraps, as this\n"),
    io:format("\tinvolves opening of new files, which costs alot."),
    io:format("\n").

one_line(Txt, W, R, Rep, C) ->
    io:format("~.22s  ~.10w  ~.10w  ~.17w  ~.9w\n", [Txt, W, R, Rep, C]).

m_halt_int(BytesItem) ->
    File = "a.LOG",
    {ok, a} = disk_log:open([{name,a}, {type,halt}, {size,infinity},
			     {format,internal}, {file, File}]),
    {T,W} = wr(a, BytesItem),
    R = r(a),
    [{_,P}] = ets:lookup(?DISK_LOG_NAME_TABLE, a),
    exit(P, kill),
    receive after 100 -> ok end,
    crash(File, 10),
    Sz = file_size(File),
    Start = start_times(),
    {repaired, a, {recovered, Rec}, {badbytes, Bad}} = 
	disk_log:open([{name,a}, {type,halt}, {size,infinity},
		       {format,internal}, {file, File}]),
    {_,Rep} = end_times(Start),
    io:format("m_halt_int: Rep = ~p, Rec = ~p, Bad = ~p~n", [Rep, Rec, Bad]),
    disk_log:close(a),
    file:delete(File),
    {W,R,1000*Rep/Sz,T}.

m_wrap_int(BytesItem) ->
    File = "a.LOG",
    {ok, a} = disk_log:open([{name,a}, {type,wrap}, {size,{405*1000, 4}},
			     {format,internal}, {file, File}]),
    {T,W} = wr(a, BytesItem),
    R = r(a),
    [{_,P}] = ets:lookup(?DISK_LOG_NAME_TABLE, a),
    exit(P, kill),
    receive after 100 -> ok end,
    del(File, 4),
    {W,R,'n/a',T}.

m_many_halt_int(NoClients, BytesItem) ->
    Name = 'log.LOG',
    File = "log.LOG",
    {ok, _} = disk_log:open([{name,Name}, {type,halt}, 
			     {size,infinity},
			     {format,internal}, {file,File}]),
    NoW = round(lists:max([lists:min([5000000/BytesItem/NoClients,
				      50000/NoClients]),
			   1000])),
    {T,W} = many_wr(NoClients, Name, NoW, BytesItem),
    ok = disk_log:close(Name),
    file:delete(File),
    {1000*W/NoW/NoClients,'n/a','n/a',1000*T/NoW/NoClients}.

many_wr(NoClients, Log, NoW, BytesItem) ->
    Item = mk_bytes(BytesItem),
    Fun = fun(Name, _Pid, _I) -> disk_log:log(Name, Item) end,
    Start = start_times(),
    Pids = spawn_clients(NoClients, client, [self(), Log, NoW, Fun]),
    check_clients(Pids),
    end_times(Start).

wr(Log, BytesItem) ->
    NoW = round(lists:max([lists:min([5000000/BytesItem,50000]),1000])),
    Item = mk_bytes(BytesItem),
    Start = start_times(),
    wr(Log, Item, NoW),
    {T,W} = end_times(Start),
    {1000*T/NoW, 1000*W/NoW}.

wr(Log, _Item, 0) ->
    disk_log:sync(Log),
    ok;
wr(Log, Item, N) ->
    ok = disk_log:log(Log, Item),
    wr(Log, Item, N-1).

r(_) ->
    nyi.

start_times() ->
    {T1, _} = statistics(runtime),
    {W1, _} = statistics(wall_clock),
    {T1, W1}.

end_times({T1,W1}) ->
    {T2, _} = statistics(runtime),
    {W2, _} = statistics(wall_clock),
    {T2-T1, W2-W1}.


head_func(suite) -> [];
head_func(doc) -> ["Test head parameter"];
head_func(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ets:new(xxx, [named_table, set, public]),
    ets:insert(xxx, {wrapc, 0}),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap},
				   {size, {100,4}},
				   {head_func, {?MODULE, hf, []}}]),
    ?line B = mk_bytes(60),
    ?line disk_log:log(a, B),
    ?line disk_log:alog(a, B),
    ?line disk_log:alog(a, B),
    ?line disk_log:log(a, B),
    H = [1,2,3],
    ?line [{wrapc, 4}] = ets:lookup(xxx, wrapc),
    ets:delete(xxx),
    ?line case get_all_terms(a) of
	      [H,B,H,B,H,B,H,B] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, 
				    [H,B,H,B,H,B,H,B]})
	  end,
    ?line 8  = no_written_items(a),
    disk_log:close(a),
    del(File, 4),

    % invalid header function
    ?line {error, {invalid_header, {_, {term}}}} = 
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, external},
		       {head_func, {?MODULE, head_fun, [{term}]}}]),
    file:delete(File),

    ?line {error, {invalid_header, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, external},
		       {head_func, {?MODULE, head_fun, [{ok,{term}}]}}]),
    file:delete(File),

    ?line {ok,n} =
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, external},
		       {head_func, {?MODULE, head_fun, [{ok,<<"head">>}]}}]),
    ?line ok = disk_log:close(n),
    ?line {ok,<<"head">>} = file:read_file(File),
    file:delete(File),

    ?line {ok,n} =
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, external},
		       {head_func, {?MODULE, head_fun, [{ok,"head"}]}}]),
    ?line ok = disk_log:close(n),
    ?line {ok,<<"head">>} = file:read_file(File),
    file:delete(File),

    ?line Error1 = {error, {badarg, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {head_func, {tjo,hej,san}},{size, {100, 4}}]),
    ?line "The argument " ++ _ = format_error(Error1),
    
    ?line Error2 = {error, {invalid_header, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {head_func, {tjo,hej,[san]}}]),
    ?line "The disk log header" ++ _ = format_error(Error2),
    file:delete(File).


head_fun(H) ->
    H.

hf() ->
    ets:update_counter(xxx, wrapc, 1),
    {ok, [1,2,3]}.

plain_head(suite) -> [];
plain_head(doc) -> ["Test head parameter"];
plain_head(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    H = [1,2,3],
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap},
				   {size, {100,4}}, {head, H}]),
    %% This one is not "counted".
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap},
				   {size, {100,4}}, {head, H}]),
    ?line B = mk_bytes(60),
    ?line disk_log:log(a, B),
    ?line disk_log:alog(a, B),
    ?line disk_log:alog(a, B),
    ?line disk_log:log(a, B),
    ?line case get_all_terms(a) of
	      [H,B,H,B,H,B,H,B] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, 
				    [H,B,H,B,H,B,H,B]})
	  end,
    ?line 8 = no_written_items(a),
    ?line ok = disk_log:close(a),
    ?line {error, no_such_log} = disk_log:close(a),
    del(File, 4).



one_header(suite) -> [];
one_header(doc) -> ["Test that a header is just printed once in a log file"];
one_header(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    H = [1,2,3],
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap},
				   {size, {100,4}}, {head, H}]),
    ?line B = mk_bytes(60),
    ?line ok = disk_log:log(a, B),
    ?line ok = disk_log:alog(a, B),
    ?line ok = disk_log:alog(a, B),
    ?line ok = disk_log:log(a, B),
    ?line case get_all_terms(a) of
	      [H,B,H,B,H,B,H,B] ->
		  ok;
	      E1 ->
		  test_server_fail({bad_terms, E1, 
				    [H,B,H,B,H,B,H,B]})
	  end,
    ?line 8  = no_written_items(a),
    ?line ok = disk_log:close(a),
    del(File, 4),

    Fileb = filename:join(Dir, "b.LOG"),
    ?line {ok, b} = disk_log:open([{name,b}, {file, Fileb}, {head, H}]),
    ?line ok = disk_log:close(b),
    ?line {ok, b} = disk_log:open([{name,b}, {file, Fileb}, {head, H}]),
    ?line ok = disk_log:log(b, "first log"),
    ?line ok = disk_log:alog(b, "second log"),
    ?line ok = disk_log:close(b),
    ?line {ok, b} = disk_log:open([{name,b}, {file, Fileb}, {head, H}]),
    ?line ok = disk_log:alog(b, "3rd log"),
    ?line ok = disk_log:log(b, "4th log"),
    ?line case get_all_terms(b) of
	      [H, "first log", "second log", "3rd log", "4th log"] ->
		  ok;
	      E2 ->
		  test_server_fail({bad_terms, E2, 
				    [H, "first log", "second log", 
				     "3rd log", "4th log"]})
	  end,
    ?line 2  = no_written_items(b),
    ?line ok = disk_log:close(b),
    ?line ok = file:delete(Fileb),

    Filec = filename:join(Dir, "c.LOG"),
    H2 = "this is a header ",
    ?line {ok, c} = disk_log:open([{name,c}, {format, external}, 
				   {file, Filec}, {head, H2}]),
    ?line ok = disk_log:close(c),
    ?line {ok, c} = disk_log:open([{name,c}, {format, external}, 
				   {file, Filec}, {head, H2}]),
    ?line ok = disk_log:blog(c, "first log"),
    ?line ok = disk_log:balog(c, "second log"),
    ?line ok = disk_log:close(c),
    ?line {ok, c} = disk_log:open([{name,c}, {format, external}, 
				   {file, Filec}, {head, H2}]),
    ?line ok = disk_log:balog(c, "3rd log"),
    ?line ok = disk_log:blog(c, "4th log"),
    ?line ok = disk_log:sync(c),
    ?line {ok, Fdc} = file:open(Filec, [read]),
    ?line {ok,"this is a header first logsecond log3rd log4th log"} = 
	file:read(Fdc, 200),
    ?line ok = file:close(Fdc),
    ?line 2  = no_written_items(c),
    ?line disk_log:close(c),
    ?line ok = file:delete(Filec),
    ok.



wrap_notif(suite) -> [];
wrap_notif(doc) -> ["Test notify parameter, wrap"];
wrap_notif(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap},
				   {size, {100,4}}, {notify, true}]),
    ?line B = mk_bytes(60),
    ?line disk_log:log(a, B),
    ?line disk_log:alog(a, B),
    ?line disk_log:alog(a, B),
    ?line disk_log:log(a, B),
    ?line disk_log:log(a, B),
    ?line rec(3, {disk_log, node(), a, {wrap, 0}}),
    ?line rec(1, {disk_log, node(), a, {wrap, 1}}),
    disk_log:close(a),
    del(File, 4).

full_notif(suite) -> [];
full_notif(doc) -> ["Test notify parameter, wrap, filled file"];
full_notif(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    file:delete(File),

    ?line {ok, a} = disk_log:open([{name, a}, {file, File}, {type, halt},
				   {size, 100}, {notify, true}]),
    ?line B = mk_bytes(60),
    ?line disk_log:log(a, B),
    ?line disk_log:alog(a, B),
    ?line rec(1, {disk_log, node(), a, full}),
    disk_log:close(a),
    file:delete(File).

trunc_notif(suite) -> [];
trunc_notif(doc) -> ["Test notify parameter, wrap, truncated file"];
trunc_notif(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    File2 = filename:join(Dir, "a.DUMP"),
    ?line {ok, a} = disk_log:open([{name, a}, {file, File}, {type, halt},
				   {size, 100}, {notify, true}]),
    ?line B = mk_bytes(60),
    ?line disk_log:log(a, B),
    ?line disk_log:truncate(a),
    ?line rec(1, {disk_log, node(), a, {truncated, 1}}),
    ?line disk_log:log(a, B),
    ?line ok = disk_log:reopen(a, File2),
    ?line rec(1, {disk_log, node(), a, {truncated, 1}}),
    disk_log:close(a),
    file:delete(File),
    file:delete(File2).

blocked_notif(suite) -> [];
blocked_notif(doc) -> 
    ["Test notify parameters 'format_external' and 'blocked_log"];
blocked_notif(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "n.LOG"),
    No = 4,
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {notify, true}, 
				   {format, external}]),
    ?line B = mk_bytes(60),
    ?line Error1 = {error,{format_external,n}} = disk_log:log(n, B),
    ?line "The requested operation" ++ _ = format_error(Error1),
    ?line ok = disk_log:blog(n, B),
    ?line ok = disk_log:alog(n, B),
    ?line rec(1, {disk_log, node(), n, {format_external, term_to_binary(B)}}),
    ?line ok = disk_log:alog_terms(n, [B,B,B,B]),
    ?line rec(1, {disk_log, node(), n, {format_external, 
				 lists:map(fun term_to_binary/1, [B,B,B,B])}}),
    ?line ok = disk_log:block(n, false),
    ?line ok = disk_log:alog(n, B),
    ?line rec(1, {disk_log, node(), n, {blocked_log, term_to_binary(B)}}),
    ?line ok = disk_log:balog(n, B),
    ?line rec(1, {disk_log, node(), n, {blocked_log, list_to_binary(B)}}),
    ?line ok = disk_log:balog_terms(n, [B,B,B,B]),
    ?line disk_log:close(n),
    ?line rec(1, {disk_log, node(), n, {blocked_log, 
				 lists:map(fun list_to_binary/1, [B,B,B,B])}}),
    ?line del(File, No).


new_idx_vsn(suite) -> [];
new_idx_vsn(doc) -> ["Test the new version of the .idx file"];
new_idx_vsn(Conf) when is_list(Conf) ->
    DataDir = ?datadir(Conf),
    PrivDir = ?privdir(Conf),
    File = filename:join(PrivDir, "new_vsn.LOG"),
    Kurt = filename:join(PrivDir, "kurt.LOG"),
    Kurt2 = filename:join(PrivDir, "kurt2.LOG"),

    %% Test that a wrap log file can have more than 255 files
    ?line {ok, new_vsn} = disk_log:open([{file, File}, {name, new_vsn}, 
					 {type, wrap}, {size, {40, 270}}]),
    ?line ok = log(new_vsn, 280),
    ?line {ok, Bin} = file:read_file(add_ext(File, "idx")),
    ?line <<0,0:32,2,10:32,1:64,1:64,_/binary>> = Bin,
    ?line disk_log:close(new_vsn),
    ?line del(File, 270),

    %% convert a very old version (0) of wrap log file to the new format (2)
    copy_wrap_log("kurt.LOG", 4, DataDir, PrivDir),

    ?line {repaired, kurt, {recovered, 1}, {badbytes, 0}} =
	disk_log:open([{file, Kurt}, {name, kurt}, 
		       {type, wrap}, {size, {40, 4}}]),
    ?line ok = disk_log:log(kurt, "this is a logged message number X"),
    ?line ok = disk_log:log(kurt, "this is a logged message number Y"),
    ?line {ok, BinK} = file:read_file(add_ext(Kurt, "idx")),
    ?line <<0,0:32,2,2:32,1:64,1:64,1:64,1:64>> = BinK,
    ?line {{40,4}, 2} = disk_log_1:read_size_file_version(Kurt),
    disk_log:close(kurt),
    ?line del(Kurt, 4),

    %% keep the old format (1)
    copy_wrap_log("kurt2.LOG", 4, DataDir, PrivDir),

    ?line {repaired, kurt2, {recovered, 1}, {badbytes, 0}} =
	disk_log:open([{file, Kurt2}, {name, kurt2}, 
		       {type, wrap}, {size, {40, 4}}]),
    ?line ok = disk_log:log(kurt2, "this is a logged message number X"),
    ?line ok = disk_log:log(kurt2, "this is a logged message number Y"),
    ?line {ok, BinK2} = file:read_file(add_ext(Kurt2, "idx")),
    ?line <<0,2:32,1:32,1:32,1:32,1:32>> = BinK2,
    ?line {{40,4}, 1} = disk_log_1:read_size_file_version(Kurt2),
    disk_log:close(kurt2),
    ?line del(Kurt2, 4),

    ok.

reopen(suite) -> [];
reopen(doc) -> 
    ["Test reopen/1 on halt and wrap logs."];
reopen(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line NewFile = filename:join(Dir, "nn.LOG"),
    ?line B = mk_bytes(60),
    
    ?line file:delete(File),    % cleanup
    ?line file:delete(NewFile), % cleanup
    ?line Q = qlen(),

    %% External halt log.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {notify, true}, {head, "header"}, 
				   {size, infinity},{format, external}]),
    ?line ok = disk_log:blog(n, B),
    ?line ok = disk_log:breopen(n, NewFile, "head"),
    ?line rec(1, {disk_log, node(), n, {truncated, 2}}),
    ?line ok = disk_log:blog(n, B),
    ?line ok = disk_log:blog(n, B),
    ?line ok = disk_log:breopen(n, NewFile, "head"),
    ?line rec(1, {disk_log, node(), n, {truncated, 3}}),
    ?line ok = disk_log:close(n),
    ?line {ok,BinaryFile} = file:read_file(File),
    ?line "head" = binary_to_list(BinaryFile),
    ?line file:delete(File),
    ?line file:delete(NewFile),

    %% Internal halt log.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {notify, true}, {head, header}, 
				   {size, infinity}]),
    ?line ok = disk_log:log(n, B),
    ?line Error1 = {error, {same_file_name, n}} = disk_log:reopen(n, File),
    ?line "Current and new" ++ _ = format_error(Error1),
    ?line ok = disk_log:reopen(n, NewFile),
    ?line rec(1, {disk_log, node(), n, {truncated, 2}}),
    ?line ok = disk_log:log(n, B),
    ?line ok = disk_log:log(n, B),
    ?line ok = disk_log:reopen(n, NewFile),
    ?line rec(1, {disk_log, node(), n, {truncated, 3}}),
    ?line ok = disk_log:close(n),
    ?line [header, _B, _B] = get_all_terms(nn, NewFile, halt),
    ?line file:delete(File),
    ?line file:delete(NewFile),

    %% Internal wrap log.
    ?line No = 4,
    ?line del(File, No),	% cleanup
    ?line del(NewFile, No),	% cleanup

    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, 
				   {head, header}, {size, {100, No}}]),
    ?line ok = disk_log:log(n, B),
    ?line ok = disk_log:log_terms(n, [B,B,B]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(3, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line ok = disk_log:reopen(n, NewFile, new_header),
    ?line rec(1, {disk_log, node(), n, {truncated, 8}}),
    ?line ok = disk_log:log_terms(n, [B,B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:close(n),
    ?line [header, _, header, _, header, _, header, _] = 
	get_all_terms(nn, NewFile, wrap),
    ?line [new_header, _, header, _, header, _] = get_all_terms(n, File, wrap),

    ?line del(NewFile, No),
    ?line file:delete(File ++ ".2"),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, 
				   {head, header}, {size, {100, No}}]),
    %% One file is missing...
    ?line ok = disk_log:reopen(n, NewFile),
    ?line rec(1, {disk_log, node(), n, {truncated, 6}}),
    ?line ok = disk_log:close(n),

    ?line del(File, No),
    ?line del(NewFile, No),
    ?line Q = qlen(),
    ok.


block_blocked(suite) -> [];
block_blocked(doc) -> 
    ["Test block/1 on external and internal logs."];
block_blocked(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line B = mk_bytes(60),
    Halt = join(Dir, "halt.LOG"),

    % External logs.
    ?line file:delete(Halt), % cleanup
    ?line {ok, halt} = disk_log:open([{name, halt}, {type, halt}, 
				      {format, external}, {file, Halt}]),
    ?line ok = disk_log:sync(halt),
    ?line ok = disk_log:block(halt, false),
    ?line Error1 = {error, {blocked_log, halt}} = disk_log:block(halt),
    ?line "The blocked disk" ++ _ = format_error(Error1),
    ?line {error, {blocked_log, halt}} = disk_log:sync(halt),
    ?line {error, {blocked_log, halt}} = disk_log:truncate(halt),
    ?line {error, {blocked_log, halt}} = disk_log:change_size(halt, infinity),
    ?line {error, {blocked_log, halt}} = 
        disk_log:change_notify(halt, self(), false),
    ?line {error, {blocked_log, halt}} = 
        disk_log:change_header(halt, {head, header}),
    ?line {error, {blocked_log, halt}} = disk_log:reopen(halt, "foo"),
    ?line ok = disk_log:close(halt),

    ?line {ok, halt} = disk_log:open([{name, halt}, {type, halt}, 
				      {format, external}]),
    ?line ok = disk_log:sync(halt),
    ?line ok = disk_log:block(halt, true),
    ?line {error, {blocked_log, halt}} = disk_log:blog(halt, B),
    ?line {error, {blocked_log, halt}} = disk_log:blog(halt, B),
    ?line {error, {blocked_log, halt}} = disk_log:block(halt),
    ?line {error, {blocked_log, halt}} = disk_log:sync(halt),
    ?line {error, {blocked_log, halt}} = disk_log:truncate(halt),
    ?line {error, {blocked_log, halt}} = disk_log:change_size(halt, infinity),
    ?line {error, {blocked_log, halt}} = 
        disk_log:change_notify(halt, self(), false),
    ?line {error, {blocked_log, halt}} = 
        disk_log:change_header(halt, {head, header}),
    ?line {error, {blocked_log, halt}} = disk_log:reopen(halt, "foo"),

    ?line ok = disk_log:unblock(halt),
    ?line ok = disk_log:close(halt),
    ?line file:delete(Halt),

    % Internal logs.
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No), % cleanup
    ?line {ok, halt} = disk_log:open([{name, halt}, {file, File}, {type, wrap},
				      {size, {100, No}}]),
    ?line ok = disk_log:block(halt, true),
    ?line eof = disk_log:chunk(halt, start),
    ?line Error2 = {error, end_of_log} = disk_log:chunk_step(halt, start, 1),
    ?line "An attempt" ++ _ = format_error(Error2),
    ?line {error, {blocked_log, halt}} = disk_log:log(halt, B),
    ?line {error, {blocked_log, halt}} = disk_log:inc_wrap_file(halt),
    ?line ok = disk_log:unblock(halt),
    ?line ok = disk_log:block(halt, false),
    ?line {error, {blocked_log, halt}} = disk_log:log(halt, B),
    ?line {error, {blocked_log, halt}} = disk_log:inc_wrap_file(halt),
    ?line Parent = self(),
    ?line Pid = 
        spawn_link(fun() -> 
                           {error, {blocked_log, halt}} = 
                               disk_log:chunk(halt, start),
                           {error, {blocked_log, halt}} = 
                               disk_log:chunk_step(halt, start, 1),
                           Parent ! {self(), stopped}
                   end),
    ?line receive {Pid,stopped} -> ok end,
    ?line ok = disk_log:close(halt),
    ?line del(File, No).

block_queue(suite) -> [];
block_queue(doc) -> 
    ["Run commands from the queue by unblocking."];
block_queue(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line Q = qlen(),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No), % cleanup
    ?line B = mk_bytes(60),    

    ?line Pid = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid, {open, File}),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {blog, B}),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line 1 = no_written_items(n),
    ?line Error1 = {error,{not_blocked,n}} = disk_log:unblock(n),
    ?line "The disk log" ++ _ = format_error(Error1),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {balog, "one string"}),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line 2 = no_written_items(n),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, sync),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    
    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, truncate),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line 0 = no_items(n),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {block, false}),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line {error, {blocked_log, _}} = disk_log:blog(n, B),
    ?line ok = sync_do(Pid, unblock),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {change_notify, Pid, true}),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line [{_, true}] = owners(n),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {change_notify, Pid, false}),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line [{_, false}] = owners(n),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {change_header, {head, header}}),
    ?line ok = disk_log:unblock(n),
    ?line {error, {badarg, head}} = get_reply(),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {change_size, 17}),
    ?line ok = disk_log:unblock(n),
    ?line {error, {badarg, size}} = get_reply(),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, inc_wrap_file),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),

    ?line ok = sync_do(Pid, close),
    ?line del(File, No),

    ?line _Pid2 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid, {int_open, File}),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {chunk, start}),
    ?line ok = disk_log:unblock(n),
    ?line eof = get_reply(),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {chunk_step, start, 100}),
    ?line ok = disk_log:unblock(n),
    ?line {ok, _Cont} = get_reply(),

    ?line ok = disk_log:block(n, true),
    ?line async_do(Pid, {log,a_term}),
    ?line ok = disk_log:unblock(n),
    ?line ok = get_reply(),
    ?line 1 = no_written_items(n),

    ?line ok = sync_do(Pid, close),
    ?line sync_do(Pid, terminate),
    ?line del(File, No),

    %% Test of the queue. Three processes involved here. Pid1's block
    %% request is queued. Pid2's log requests are put in the queue.
    %% When unblock is executed, Pid1's block request is granted.
    %% Pid2's log requests are executed when Pid1 unblocks.
    %% (This example should show that the pair 'queue' and 'messages' 
    %% in State does the trick - one does not need a "real" queue.)
    ?line P0 = pps(),
    Name = n,
    ?line Pid1 = spawn_link(?MODULE, lserv, [Name]),
    ?line {ok, Name} = sync_do(Pid1, {int_open, File, {1000,2}}),
    ?line Pid2 = spawn_link(?MODULE, lserv, [Name]),
    ?line {ok, Name} = sync_do(Pid2, {int_open, File, {1000,2}}),
    ?line ok = disk_log:block(Name),
    ?line async_do(Pid1, {alog,{1,a}}),
    ?line ok = get_reply(),
    ?line async_do(Pid1, {alog,{2,b}}),
    ?line ok = get_reply(),
    ?line async_do(Pid1, {alog,{3,c}}),
    ?line ok = get_reply(),
    ?line async_do(Pid1, {alog,{4,d}}),
    ?line ok = get_reply(),
    ?line async_do(Pid1, block),
    ?line async_do(Pid2, {alog,{5,e}}),
    ?line ok = get_reply(),
    ?line async_do(Pid2, {alog,{6,f}}),
    ?line ok = get_reply(),
    ?line ok = disk_log:unblock(Name),
    ?line ok = get_reply(),
    ?line async_do(Pid2, {alog,{7,g}}),
    ?line ok = get_reply(),
    ?line async_do(Pid2, {alog,{8,h}}),
    ?line ok = get_reply(),
    ?line async_do(Pid1, unblock),
    ?line ok = get_reply(),
    ?line ok = sync_do(Pid1, close),
    ?line ok = sync_do(Pid2, close),
    ?line sync_do(Pid1, terminate),
    ?line sync_do(Pid2, terminate),
    Terms = get_all_terms(Name, File, wrap),
    ?line true = [{1,a},{2,b},{3,c},{4,d},{5,e},{6,f},{7,g},{8,h}] == Terms,
    del(File, 2),
    ?line Q = qlen(),
    ?line true = (P0 == pps()),
    ok.

block_queue2(suite) -> [];
block_queue2(doc) -> 
    ["OTP-4880. Blocked processes did not get disk_log_stopped message."];
block_queue2(Conf) when is_list(Conf) ->
    ?line Q = qlen(),
    ?line P0 = pps(),
    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,

    %% log requests are queued, and processed when the log is closed
    ?line Pid = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid, {open, File}),
    ?line ok = sync_do(Pid, block),
    %% Asynchronous stuff is ignored.
    ?line ok = disk_log:balog_terms(n, [<<"foo">>,<<"bar">>]),
    ?line ok = disk_log:balog_terms(n, [<<"more">>,<<"terms">>]),
    Parent = self(),
    ?line Fun = 
        fun() ->
                {error,no_such_log} = disk_log:sync(n),
                receive {disk_log, _, {error, disk_log_stopped}} -> ok end,
                Parent ! disk_log_stopped_ok
        end,
    ?line spawn(Fun),
    ?line ok = sync_do(Pid, close),
    ?line receive disk_log_stopped_ok -> ok end,
    ?line sync_do(Pid, terminate),
    ?line {ok,<<>>} = file:read_file(File ++ ".1"),
    ?line del(File, No),    
    ?line Q = qlen(),
    ?line true = (P0 == pps()),
    ok.


unblock(suite) -> [];
unblock(doc) -> 
    ["Test unblock/1."];
unblock(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "n.LOG"),
    No = 1,
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {notify, true}, 
				   {format, external}]),
    ?line ok = disk_log:block(n),
    ?line spawn_link(?MODULE, try_unblock, [n]),
    ?line timer:sleep(100),
    ?line disk_log:close(n),
    ?line del(File, No).

try_unblock(Log) ->
    ?line Error = {error, {not_blocked_by_pid, n}} = disk_log:unblock(Log),
    ?line "The disk log" ++ _ = format_error(Error).


open_overwrite(suite) -> [];
open_overwrite(doc) -> 
    ["Test open/1 when old files exist."];
open_overwrite(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No), % cleanup

    % read write
    ?line First = "n.LOG.1",
    ?line make_file(Dir, First, 8),

    ?line Error1 = {error, {not_a_log_file, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, internal}, {size, {100, No}}]),
    ?line "The file" ++ _ = format_error(Error1),
    ?line del(File, No),

    ?line make_file(Dir, First, 4),

    ?line {error, {not_a_log_file, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, internal}, {size, {100, No}}]),
    ?line del(File, No),

    ?line make_file(Dir, First, 0),

    ?line {error, {not_a_log_file, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap}, 
		       {format, internal}, {size, {100, No}}]),
    % read only
    ?line make_file(Dir, First, 6),

    ?line {error, {not_a_log_file, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},{mode, read_only},
		       {format, internal}, {size, {100, No}}]),
    ?line del(File, No),

    ?line make_file(Dir, First, 0),

    ?line {error, {not_a_log_file, _}} = 
	disk_log:open([{name, n}, {file, File},{type, wrap}, 
		       {mode, read_only}, {format, internal},
		       {size, {100, No}}]),
    ?line del(File, No),
    
    ?line {error, _} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				      {mode, read_only},
				      {format, internal},{size, {100, No}}]),

    file:delete(File),
    ?line {ok,n} = disk_log:open([{name,n},{file,File},
                                  {mode,read_write},{type,halt}]),
    ?line ok = disk_log:close(n),
    ?line ok = unwritable(File),
    ?line {error, {file_error, File, _}} = 
       disk_log:open([{name,n},{file,File},{mode,read_write},{type,halt}]),
    ?line ok = writable(File),
    file:delete(File),

    ?line {ok,n} = disk_log:open([{name,n},{file,File},{format,external},
                                  {mode,read_write},{type,halt}]),
    ?line ok = disk_log:close(n),
    ?line ok = unwritable(File),
    ?line {error, {file_error, File, _}} = 
        disk_log:open([{name,n},{file,File},{format,external},
                       {mode,read_write},{type,halt}]),
    ?line ok = writable(File),
    file:delete(File),

    ok.


make_file(Dir, File, N) ->
    {ok, F} = file:open(filename:join(Dir, File), 
                        [raw, binary, read, write]),
    ok = file:truncate(F),
    case N of 
	0 ->
	    true;
	_Else ->
	    ok = file:write(F, [lists:seq(1,N)])
    end,
    ok = file:close(F).

open_size(suite) -> [];
open_size(doc) -> 
    ["Test open/1 option size."];
open_size(Conf) when is_list(Conf) ->

    ?line Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),

    ?line No = 4,
    ?line file:delete(File),
    ?line del(File, No),	% cleanup

    %% missing size option
    ?line {error, {badarg, size}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, internal}]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
			     {format, internal},{size, {100, No}}]),
    ?line B = mk_bytes(60),    
    ?line ok = disk_log:log_terms(n, [B, B, B, B]),
    ?line ok = disk_log:sync(n),
    ?line ok = disk_log:block(n),

    %% size option does not match existing size file, read_only
    ?line Error1 = {error, {size_mismatch, _, _}} = 
	disk_log:open([{name, nn}, {file, File}, {type, wrap}, 
		       {mode, read_only}, {format, internal},
		       {size, {100, No + 1}}]),
    ?line "The given size" ++ _ = format_error(Error1),
    ?line {ok, nn} = disk_log:open([{name, nn}, {file, File}, {type, wrap},
			      {mode, read_only},
			      {format, internal},{size, {100, No}}]),
    ?line [_, _, _, _] = get_all_terms1(nn, start, []),
    ?line disk_log:close(nn),

    ?line ok = disk_log:unblock(n),
    ?line ok = disk_log:close(n),

    %% size option does not match existing size file, read_write
    ?line {error, {size_mismatch, _, _}} = 
	disk_log:open([{name, nn}, {file, File}, {type, wrap}, 
		       {format, internal}, {size, {100, No + 1}}]),
    %% size option does not match existing size file, truncating
    ?line {ok, nn} = 
	disk_log:open([{name, nn}, {file, File}, {type, wrap}, 
		       {repair, truncate}, {format, internal}, 
		       {size, {100, No + 1}}]),
    ?line ok = disk_log:close(nn),

    ?line del(File, No),
    ok.


open_truncate(suite) -> [];
open_truncate(doc) -> 
    ["Test open/1 with {repair, truncate}."];
open_truncate(Conf) when is_list(Conf) ->

    ?line Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No),	% cleanup

    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal},{size, {100, No}}]),
    ?line B = mk_bytes(60),    
    ?line ok = disk_log:log_terms(n, [B, B, B, B]),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {repair,truncate},
				   {format, internal},{size, {100, No}}]),
    ?line ok = disk_log:close(n),
    ?line [] = get_all_terms(n, File, wrap),
    ?line del(File, No),
    ok.
    

open_error(suite) -> [];
open_error(doc) -> 
    ["Try some invalid open/1 options."];
open_error(Conf) when is_list(Conf) ->
    ?line Dir = ?privdir(Conf),

    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No),	% cleanup

    ?line {error, {badarg, name}} = disk_log:open([{file, File}]),
    ?line {error, {badarg, file}} = disk_log:open([{name,{foo,bar}}]),
    ?line {error, {badarg, [{foo,bar}]}} = disk_log:open([{foo,bar}]),

    %% external logs, read_only.
    ?line {error, {file_error, _, enoent}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {size, {100,No}},
		       {format, external}, {mode, read_only}]),
    ?line Error5 = {error, {file_error, _, enoent}} = 
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {size, 100},
		       {format, external}, {mode, read_only}]),
    ?line true = lists:prefix("\"" ++ File, format_error(Error5)),

    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
			     {format, external},{size, {100, No}}]),
    %% Already owner, ignored.
    ?line {ok, n} =
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, external}, {size, {100, No}}]),
    ?line Error2 = {error, {name_already_open, n}} =
	disk_log:open([{name, n}, {file, another_file}, {type, wrap},
		       {format, external}, {size, {100, No}}]),
    ?line "The disk log" ++ _ = format_error(Error2),
    ?line Error1 = {error, {arg_mismatch, notify, false, true}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, external}, {size, {100, No}}, {notify, true}]),
    ?line "The value" ++ _ = format_error(Error1),
    ?line Error3 = {error, {open_read_write, n}} =
	disk_log:open([{name, n}, {file, File}, {type, wrap}, 
		       {mode, read_only},
		       {format, external}, {size, {100, No}}]),
    ?line "The disk log" ++ _ = format_error(Error3),
    ?line {error, {badarg, size}} = 
        disk_log:open([{name, n}, {file, File}, {type, halt},
                       {format, external}, {size, {100, No}}]),
    ?line {error, {arg_mismatch, type, wrap, halt}} = 
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, external}]),
    ?line {error, {arg_mismatch, format, external, internal}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, internal}, {size, {100, No}}]),
    ?line {error, {arg_mismatch, repair, true, false}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, external}, {repair, false}]),
    ?line {error, {size_mismatch, {100,4}, {1000,4}}} =
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, external}, {size, {1000, No}}]),
    ?line {error, {arg_mismatch, head, none, _}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {head, "header"},
		       {format, external}, {size, {100, No}}]),
    ?line {error, {badarg, size}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, external}, {size, 100}]),

    ?line ok = disk_log:close(n),

    ?line {ok, n} =
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {mode, read_only},
		       {format, external}, {size, {100, No}}]),
    ?line Error4 = {error, {open_read_only, n}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {mode, read_write},
		       {format, external}, {size, {100, No}}]),
    ?line "The disk log" ++ _ = format_error(Error4),
    ?line ok = disk_log:close(n),

    ?line del(File, No).    


close_race(suite) -> [];
close_race(doc) -> 
    ["Do something quickly after close/1"];
close_race(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 1,
    ?line del(File, No),  % cleanup
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {notify, true}, 
				   {format, internal}]),
    ?line ok = disk_log:close(n),
    ?line Error1 = {error, no_such_log} = disk_log:close(n),
    ?line "There is no disk" ++ _ = format_error(Error1),

    % Pid1 blocks, Pid2 closes without being suspended.
    ?line Pid1 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid2 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid1, {open, File}),
    ?line {ok, n} = sync_do(Pid2, {open, File}),
    ?line ok = sync_do(Pid1, block),
    ?line [{_, false}, {_, false}] = sync_do(Pid1, owners),
    ?line ok = sync_do(Pid2, close),
    ?line [{_, false}] = sync_do(Pid1, owners),
    ?line ok = sync_do(Pid1, close),
    ?line sync_do(Pid1, terminate),
    ?line sync_do(Pid2, terminate),
    ?line {error, no_such_log} = disk_log:info(n),

    % Pid3 blocks, Pid3 closes. Pid4 should still be ablo to use log.
    ?line Pid3 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid4 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid3, {open, File}),
    ?line {ok, n} = sync_do(Pid4, {open, File}),
    ?line ok = sync_do(Pid3, block),
    ?line ok = sync_do(Pid3, close),
    ?line [{_Pid4, false}] = sync_do(Pid4, owners),
    ?line sync_do(Pid3, terminate),
    ?line sync_do(Pid4, terminate),
    ?line {error, no_such_log} = disk_log:info(n),

    % Pid5 blocks, Pid5 terminates. Pid6 should still be ablo to use log.
    ?line Pid5 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid6 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid5, {open, File}),
    ?line {ok, n} = sync_do(Pid6, {open, File}),
    ?line ok = sync_do(Pid5, block),
    ?line sync_do(Pid5, terminate),
    ?line [{_Pid6, false}] = sync_do(Pid6, owners),
    ?line sync_do(Pid6, terminate),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line del(File, No),  % cleanup
    ok.

close_block(suite) -> [];
close_block(doc) -> 
    ["Block, unblock, close, terminate."];
close_block(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    No = 1,
    del(File, No),	% cleanup

    P0 = pps(),
    %% One of two owners terminates.
    ?line Pid1 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid2 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid1, {open, File}),
    ?line {ok, n} = sync_do(Pid2, {open, File}),
    ?line [_, _] = sync_do(Pid1, owners),
    ?line [_, _] = sync_do(Pid2, owners),
    ?line 0 = sync_do(Pid1, users),
    ?line 0 = sync_do(Pid2, users),
    ?line sync_do(Pid1, terminate),
    ?line [_] = sync_do(Pid2, owners),
    ?line 0 = sync_do(Pid2, users),
    ?line sync_do(Pid2, terminate),    
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    %% Users terminate (no link...).
    ?line Pid3 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid4 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid3, {open, File, none}),
    ?line {ok, n} = sync_do(Pid4, {open, File, none}),
    ?line [] = sync_do(Pid3, owners),
    ?line [] = sync_do(Pid4, owners),
    ?line 2 = sync_do(Pid3, users),
    ?line 2 = sync_do(Pid4, users),
    ?line sync_do(Pid3, terminate),
    ?line [] = sync_do(Pid4, owners),
    ?line 2 = sync_do(Pid4, users),
    ?line sync_do(Pid4, terminate),    
    ?line disk_log:close(n),
    ?line disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    % Blocking owner terminates.
    ?line Pid5 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {linkto, none},{size, {100,No}},
				   {format, external}]),
    ?line {ok, n} = sync_do(Pid5, {open, File}),
    ?line ok = sync_do(Pid5, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line sync_do(Pid5, terminate),
    ?line ok = status(n),
    ?line [] = owners(n),
    ?line 1 = users(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    % Blocking user terminates.
    ?line Pid6 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {format, external}]),
    ?line {ok, n} = sync_do(Pid6, {open, File, none}),
    ?line ok = sync_do(Pid6, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line sync_do(Pid6, terminate), % very silently...
    ?line ok = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line ok = disk_log:close(n),
    ?line [] = owners(n),
    ?line 1 = users(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    % Blocking owner terminates.
    ?line Pid7 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {linkto, none},
				   {size, {100,No}}, {format, external}]),
    ?line {ok, n} = sync_do(Pid7, {open, File}),
    ?line ok = sync_do(Pid7, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line sync_do(Pid7, terminate),
    ?line ok = status(n),
    ?line [] = owners(n),
    ?line 1 = users(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    %% Two owners, the blocking one terminates.
    ?line Pid8 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid9 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = sync_do(Pid8, {open, File}),
    ?line {ok, n} = sync_do(Pid9, {open, File}),
    ?line ok = sync_do(Pid8, block),
    ?line {blocked, true} = status(n),
    ?line sync_do(Pid8, terminate),
    ?line ok = status(n),
    ?line [_] = sync_do(Pid9, owners),
    ?line 0 = sync_do(Pid9, users),
    ?line sync_do(Pid9, terminate),    
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    % Blocking user closes.
    ?line Pid10 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {format, external}]),
    ?line {ok, n} = sync_do(Pid10, {open, File, none}),
    ?line ok = sync_do(Pid10, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line ok = sync_do(Pid10, close),
    ?line ok = status(n),
    ?line [_] = owners(n),
    ?line 0 = users(n),
    ?line ok = disk_log:close(n),
    ?line sync_do(Pid10, terminate),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line true = (P0 == pps()),    

    % Blocking user unblocks and closes.
    ?line Pid11 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {format, external}]),
    ?line {ok, n} = sync_do(Pid11, {open, File, none}),
    ?line ok = sync_do(Pid11, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line ok = sync_do(Pid11, unblock),
    ?line ok = sync_do(Pid11, close),
    ?line ok = status(n),
    ?line [_] = owners(n),
    ?line 0 = users(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line sync_do(Pid11, terminate),
    ?line true = (P0 == pps()),    

    % Blocking owner closes.
    ?line Pid12 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {linkto, none},
				   {size, {100,No}}, {format, external}]),
    ?line {ok, n} = sync_do(Pid12, {open, File}),
    ?line ok = sync_do(Pid12, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line ok = sync_do(Pid12, close),
    ?line ok = status(n),
    ?line [] = owners(n),
    ?line 1 = users(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line sync_do(Pid12, terminate),
    ?line true = (P0 == pps()),    

    % Blocking owner unblocks and closes.
    ?line Pid13 = spawn_link(?MODULE, lserv, [n]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {linkto, none},
				   {size, {100,No}}, {format, external}]),
    ?line {ok, n} = sync_do(Pid13, {open, File}),
    ?line ok = sync_do(Pid13, block),
    ?line {blocked, true} = status(n),
    ?line [_] = owners(n),
    ?line 1 = users(n),
    ?line ok = sync_do(Pid13, unblock),
    ?line ok = sync_do(Pid13, close),
    ?line ok = status(n),
    ?line [] = owners(n),
    ?line 1 = users(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line sync_do(Pid13, terminate),
    ?line true = (P0 == pps()),

    del(File, No),	% cleanup
    ok.

close_deadlock(suite) -> [];
close_deadlock(doc) -> 
    ["OTP-4745. Deadlock with just an ordinary log could happen."];
close_deadlock(Conf) when is_list(Conf) ->
    ?line true = is_alive(),

    ?line PrivDir = ?privdir(Conf),

    ?line F1 = filename:join(PrivDir, "a.LOG"),
    ?line file:delete(F1),
    Self = self(),

    %% One process opens the log at the same time as another process
    %% closes the log. Used to always cause deadlock before OTP-4745.
    Name = a,
    Fun = fun() -> open_close(Self, Name, F1) end,
    P = spawn(Fun),
    ?line receive {P, Name} -> ok end,
    ?line {ok, L} = disk_log:open([{name,Name},{file,F1}]),
    ?line ok = disk_log:close(L),
    ?line receive {P, done} -> ok end,    
    ?line file:delete(F1),

    %% One process opens the log at the same time as another process
    %% closes the log due to file error while truncating.
    %% This test is time dependent, but does not fail when it does not
    %% "work". When it works, as it seems to do right now :), the
    %% disk_log_server gets {error, no_such_log}, receives the EXIT
    %% message caused by truncate, and tries to open the log again.
    ?line No = 4,
    ?line LDir = F1 ++ ".2",
    ?line file:del_dir(LDir),
    ?line del(F1, No),
    ?line ok = file:make_dir(LDir),
    Fun2 = fun() -> open_truncate(Self, Name, F1, No) end,
    P2 = spawn(Fun2),
    ?line receive {P2, Name} -> ok end,
    ?line {ok, L} = disk_log:open([{name, Name}, {file, F1}, {type, wrap},
				   {format, external}]),
    %% Note: truncate causes the disk log process to terminate. One
    %% cannot say if open above happened before, after, or during the
    %% termination. The link to the owner is removed before termination.
    ?line case disk_log:close(L) of
              ok -> ok;
              {error,no_such_log} -> 
                  ok
          end,
    ?line receive {P2, done} -> ok end,
    ?line del(F1, No),
    ?line file:del_dir(LDir),

    %% To the same thing, this time using distributed logs.
    %% (Does not seem to work very well, unfortunately.)
    FunD = fun() -> open_close_dist(Self, Name, F1) end,
    PD = spawn(FunD),
    receive {PD, Name} -> ok end,
    ?line {[_], []} = disk_log:open([{name,Name},{file,F1},
                                     {distributed,[node()]}]),
    ?line ok = disk_log:close(L),
    receive {PD, done} -> ok end,
    ?line file:delete(F1),

    ok.

open_close(Pid, Name, File) ->
    {ok, L} = disk_log:open([{name,Name},{file,File}]),
    Pid ! {self(), Name},
    ok = disk_log:close(L),
    Pid ! {self(), done}.

open_truncate(Pid, Name, File, No) ->
    {ok, L} = disk_log:open([{name, Name}, {file, File}, {type, wrap},
                             {format, external},{size, {100, No}}]),
    Pid ! {self(), Name},
    {error, {file_error, _, _}} = disk_log:truncate(L),
    %% The file has been closed, the disklog process has terminated.
    Pid ! {self(), done}.

open_close_dist(Pid, Name, File) ->
    {[{_,{ok,L}}], []} = disk_log:open([{name,Name},{file,File},
                                        {distributed,[node()]}]),
    Pid ! {self(), Name},
    ok = disk_log:close(L),
    Pid ! {self(), done}.

async_do(Pid, Req) ->
    Pid ! {self(), Req},
    %% make sure the request is queued
    timer:sleep(100).

get_reply() ->
    receive Reply -> 
	    Reply 
    end.

sync_do(Pid, Req) ->
    Pid ! {self(), Req},
    receive
        Reply when Req =:= terminate ->
            timer:sleep(500),
            Reply;
	Reply ->
	    Reply
    end.

lserv(Log) ->
    ?line receive 
	{From, {open, File}} ->
	    From ! disk_log:open([{name, Log}, {file, File}, {type, wrap},
				  {size, {100,1}}, {format, external}]);
	{From, {open, File, LinkTo}} ->
	    From ! disk_log:open([{name, Log}, {file, File}, {type, wrap},
				  {linkto, LinkTo}, {size, {100,1}}, 
				  {format, external}]);
	{From, {int_open, File}} ->
	    From ! disk_log:open([{name, Log}, {file, File}, {type, wrap},
				  {size, {100,1}}]);
	{From, {int_open, File, Size}} ->
	    From ! disk_log:open([{name, Log}, {file, File}, {type, wrap},
				  {size, Size}]);
	{From, {dist_open, File, Node}} ->
	    From ! disk_log:open([{name, Log}, {file, File}, {type, wrap},
				  {size, {100,1}}, {distributed, [Node]}]);
	{From, {dist_open, File, LinkTo, Node}} ->
	    From ! disk_log:open([{name, Log}, {file, File}, {type, wrap},
				  {linkto, LinkTo}, {size, {100,1}}, 
				  {distributed, [Node]}]);
	{From, block} ->
	    From ! disk_log:block(Log);
	{From, {block, Bool}} ->
	    From ! disk_log:block(Log, Bool);
	{From, unblock} ->
	    From ! disk_log:unblock(Log);
	{From, close} ->
	    From ! disk_log:close(Log);
	{From, owners} ->
	    From ! owners(Log);
	{From, users} ->
	    From ! users(Log);
	{From, sync} ->
	    From ! disk_log:sync(Log);
	{From, truncate} ->
	    From ! disk_log:truncate(Log);
	{From, terminate} ->
	    From ! terminated,
	    exit(normal);
	{From, {log, B}} ->
	    From ! disk_log:log(Log, B);
	{From, {blog, B}} ->
	    From ! disk_log:blog(Log, B);
	{From, {alog, B}} ->
	    From ! disk_log:alog(Log, B);
	{From, {balog, B}} ->
	    From ! disk_log:balog(Log, B);
	{From, {change_notify, Pid, Bool}} ->
	    From ! disk_log:change_notify(Log, Pid, Bool);
	{From, {change_header, Header}} ->
	    From ! disk_log:change_header(Log, Header);
	{From, {change_size, Size}} ->
	    From ! disk_log:change_size(Log, Size);
	{From, inc_wrap_file} ->
	    From ! disk_log:inc_wrap_file(Log);
	{From, {chunk, Cont}} ->
	    From ! disk_log:chunk(Log, Cont);
	{From, {chunk_step, Cont, N}} ->
	    From ! disk_log:chunk_step(Log, Cont, N);
	Any ->
	    io:format("invalid request ~p~n", [Any]),
	    exit(abnormal)
    end,
    lserv(Log).


error_repair(suite) -> [];
error_repair(doc) -> 
    ["Error while repairing."];
error_repair(Conf) when is_list(Conf) ->
    % not all error situations are covered by this test

    DataDir = ?datadir(Conf),
    PrivDir = ?privdir(Conf),

    ?line File = filename:join(PrivDir, "n.LOG"),
    ?line No = 4,
    ?line file:delete(File),
    ?line del(File, No),	% cleanup

    % kurt.LOG is not closed and has four logged items, one is recovered
    ?line copy_wrap_log("kurt.LOG", "n.LOG", No, DataDir, PrivDir),
    ?line {repaired,n,{recovered,1},{badbytes,0}} =
	disk_log:open([{name, n}, {file, File}, {type, wrap}, {size,{40,No}}]),
    ?line 1 = cur_cnt(n),
    ?line 53 = curb(n),
    ?line 4 = no_items(n),
    ?line ok = disk_log:close(n),
    
    % temporary repair file cannot be created
    ?line copy_wrap_log("kurt.LOG", "n.LOG", No, DataDir, PrivDir),
    ?line Dir = File ++ ".4" ++ ".TMP",
    ?line ok = file:make_dir(Dir),
    ?line P0 = pps(),
    ?line {error, {file_error, _, _}} =
	disk_log:open([{name, n}, {file, File}, {type, wrap}, {size,{40,4}}]),
    ?line true = (P0 == pps()),
    ?line del(File, No),
    ?line ok = file:del_dir(Dir),

    %% repair a file
    ?line P1 = pps(),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {40,No}}]),
    ?line ok = disk_log:log_terms(n, [{this,is}]), % first file full
    ?line ok = disk_log:log_terms(n, [{some,terms}]), % second file full
    ?line ok = disk_log:close(n),
    ?line BadFile = add_ext(File, 2), % current file
    ?line set_opened(BadFile),
    ?line crash(BadFile, 28), % the binary is now invalid
    ?line {repaired,n,{recovered,0},{badbytes,26}} =
	 disk_log:open([{name, n}, {file, File}, {type, wrap},
			{format, internal}, {size, {40,No}}]),
    ?line ok = disk_log:close(n),
    ?line true = (P1 == pps()),
    ?line del(File, No),

    %% yet another repair
    ?line P2 = pps(),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {4000,No}}]),
    ?line ok = disk_log:log_terms(n, [{this,is},{some,terms}]),
    ?line ok = disk_log:close(n),
    ?line BadFile2 = add_ext(File, 1), % current file
    ?line set_opened(BadFile2),
    ?line crash(BadFile2, 51), % the second binary is now invalid
    ?line {repaired,n,{recovered,1},{badbytes,26}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, internal}, {size, {4000,No}}]),
    ?line ok = disk_log:close(n),
    ?line true = (P2 == pps()),
    ?line del(File, No),

    %% Repair, large term
    ?line Big = term_to_binary(lists:duplicate(66000,$a)),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {40,No}}]),
    ?line ok = disk_log:log_terms(n, [Big]),
    ?line ok = disk_log:close(n),
    ?line set_opened(add_ext(File, 1)),
    ?line {repaired,n,{recovered,1},{badbytes,0}} = 
	disk_log:open([{name, n}, {file, File}, {type, wrap},
		       {format, internal}, {size, {40,No}}]),
    ?line {_, [Got]} = disk_log:chunk(n, start),
    ?line ok = disk_log:close(n),
    ?line Got = Big,
    ?line del(File, No),

    %% A term a little smaller than a chunk, then big terms.
    ?line BigSmall = mk_bytes(1024*64-8-12),
    ?line file:delete(File),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line ok = disk_log:log_terms(n, [BigSmall, Big, Big]),
    ?line ok = disk_log:close(n),    
    ?line set_opened(File),
    ?line FileSize = file_size(File),
    ?line crash(File, FileSize-byte_size(Big)-4),
    ?line Error1 = {error, {need_repair, _}} = 
        disk_log:open([{name, n}, {file, File}, {repair, false},
                       {type, halt}, {format, internal}]),
    ?line "The disk log" ++ _ = format_error(Error1),
    ?line {repaired,n,{recovered,2},{badbytes,132013}} = 
        disk_log:open([{name, n}, {file, File}, {repair, true},
                       {type, halt}, {format, internal}]),
    ?line ok = disk_log:close(n),
    ?line file:delete(File),

    %% The header is recovered.
    ?line {ok,n} =
	disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, internal},
		       {head_func, {?MODULE, head_fun, [{ok,"head"}]}}]),
    ?line ok = disk_log:log_terms(n, [list,'of',terms]),
    ?line ["head",list,'of',terms] = get_all_terms(n),
    ?line ok = disk_log:close(n),
    ?line set_opened(File),
    ?line crash(File, 30),
    ?line {repaired,n,{recovered,3},{badbytes,16}} = 
        disk_log:open([{name, n}, {file, File}, {type, halt},
		       {format, internal},{repair,true},
		       {head_func, {?MODULE, head_fun, [{ok,"head"}]}}]),
    ?line ["head",'of',terms] = get_all_terms(n),
    ?line ok = disk_log:close(n),

    file:delete(File),

    ok.
    
set_opened(File) ->
    {ok, Fd} = file:open(File, [raw, binary, read, write]),
    ok = file:write(Fd, [?LOGMAGIC, ?OPENED]),
    ok = file:close(Fd).

error_log(suite) -> [];
error_log(doc) -> 
    ["Error while repairing."];
error_log(Conf) when is_list(Conf) ->
    ?line Dir = ?privdir(Conf),

    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line file:delete(File),
    ?line del(File, No),	% cleanup
    ?line LDir = File ++ ".2",

    ?line Q = qlen(),
    % dummy just to get all processes "above" disk_log going
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external},{size, {100, No}}]),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    % inc_wrap_file fails, the external log is not terminated
    ?line P0 = pps(),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external},{size, {100, No}}]),
    ?line ok = file:make_dir(LDir),
    ?line {error, {file_error, _, _}} = disk_log:inc_wrap_file(n),
    ?line timer:sleep(500),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    % inc_wrap_file fails, the internal log is not terminated, ./File.2/ exists
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal},{size, {100, No}}]),
    ?line {error, {file_error, _, _}} = disk_log:inc_wrap_file(n),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    % truncate fails, the log is terminated, ./File.2/ exists
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external},{size, {100, No}}]),
    ?line {error, {file_error, _, _}} = disk_log:truncate(n),
    ?line true = (P0 == pps()),
    ?line del(File, No),

    %% OTP-4880.
    % reopen (rename) fails, the log is terminated, ./File.2/ exists
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, external},{size, 100000}]),
    ?line {error, {file_error, _, eisdir}} = disk_log:reopen(n, LDir),
    ?line true = (P0 == pps()),
    ?line file:delete(File),

    ?line B = mk_bytes(60),

    %% OTP-4880. reopen a wrap log, rename fails
    ?line File2 = filename:join(Dir, "n.LOG2"),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File2}, {type, wrap},
				   {format, external},{size, {100, No}}]),
    ?line ok = disk_log:blog_terms(n, [B,B,B]),
    ?line {error, {file_error, _, eisdir}} = disk_log:reopen(n, File),
    ?line {error, no_such_log} = disk_log:close(n),
    ?line del(File2, No),
    ?line del(File, No),

    % log, external wrap log, ./File.2/ exists
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external},{size, {100, No}}]),
    ?line {error, {file_error, _, _}} = disk_log:blog_terms(n, [B,B,B]),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    % log, internal wrap log, ./File.2/ exists
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal},{size, {100, No}}]),
    ?line {error, {file_error, _, _}} = disk_log:log_terms(n, [B,B,B]),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    ?line ok = file:del_dir(LDir),

    % can't remove file when changing size
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal},{size, {100, No}}]),
    ?line ok = disk_log:log_terms(n, [B,B,B,B]),
    ?line ok = disk_log:change_size(n, {100, No-2}),
    ?line Three = File ++ ".3",
    ?line ok = file:delete(Three),
    ?line ok = file:make_dir(Three),
    ?line {error, {file_error, _, _}} = disk_log:log_terms(n, [B,B,B]),
    ?line timer:sleep(500),
    ?line ok = disk_log:close(n),
    ?line ok = file:del_dir(Three),
    ?line del(File, No),
    ?line Q = qlen(),
    ok.
    
chunk(suite) -> [];
chunk(doc) -> 
    ["Test chunk and chunk_step."];
chunk(Conf) when is_list(Conf) ->
    %% See also halt_ro_crash/1 above.

    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    No = 4,
    ?line B = mk_bytes(60),
    ?line BB = mk_bytes(64000), % 64 kB chunks
    ?line del(File, No),% cleanup

    %% Make sure chunk_step skips the rest of the binary.
    %% OTP-3716. This was a bug...
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {50,No}}]),
    %% 1, 2 and 3 on file one, 4 on file two.
    ?line ok = disk_log:log_terms(n, [1,2,3,4]),
    ?line {I1, [1]} = disk_log:chunk(n, start, 1),
    ?line [{node,Node}] = disk_log:chunk_info(I1),
    ?line Node = node(),
    ?line Error1 = {error, {no_continuation, foobar}} = 
        disk_log:chunk_info(foobar),
    ?line "The term" ++ _ = format_error(Error1),
    ?line {ok, I2} = disk_log:chunk_step(n, I1, 1),
    ?line {error, {badarg, continuation}} = disk_log:chunk_step(n, foobar, 1),
    ?line {I3, [4]} = disk_log:chunk(n, I2, 1),
    ?line {ok, I4} = disk_log:chunk_step(n, I3, -1),
    ?line {_, [1]} = disk_log:chunk(n, I4, 1),
    ?line {error, {badarg, continuation}} = disk_log:bchunk(n, 'begin'),
    ?line {Ib1, [Bin1,Bin2]} = disk_log:bchunk(n, start, 2),
    ?line 1 = binary_to_term(Bin1),
    ?line 2 = binary_to_term(Bin2),
    ?line {ok, Ib2} = disk_log:chunk_step(n, Ib1, 1),
    ?line {Ib3, [Bin3]} = disk_log:bchunk(n, Ib2, 1),
    ?line 4 = binary_to_term(Bin3),
    ?line {ok, Ib4} = disk_log:chunk_step(n, Ib3, -1),
    ?line {_, [Bin4]} = disk_log:bchunk(n, Ib4, 1),
    ?line 1 = binary_to_term(Bin4),
    ?line {Ib5, [Bin1, Bin2, Bin17]} = disk_log:bchunk(n, start),
    ?line 3 = binary_to_term(Bin17),
    ?line {Ib6, [Bin3]} = disk_log:bchunk(n, Ib5, infinity),
    ?line eof = disk_log:bchunk(n, Ib6, infinity),
    ?line ok = disk_log:close(n),
    ?line del(File, No), % cleanup

    %% external log, cannot read chunks
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external}, {size, {100,No}}]),
    ?line {error, {badarg, continuation}} = disk_log:chunk(n, 'begin'),
    ?line {error, {format_external, n}} = disk_log:chunk(n, start),
    ?line Error2 = {error, {not_internal_wrap, n}} = 
        disk_log:chunk_step(n, start, 1),
    ?line "The requested" ++ _ = format_error(Error2),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% wrap, read_write
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {100,No}}]),
    ?line ok = disk_log:log_terms(n, [B,B,B,B]),
    ?line {C1, [_]} = disk_log:chunk(n, start),
    ?line {C2, [_]} = disk_log:chunk(n, C1),
    ?line {C3, [_]} = disk_log:chunk(n, C2),
    ?line {C4, [_]} = disk_log:chunk(n, C3, 1),
    ?line eof = disk_log:chunk(n, C4),
    ?line {C5, [_]} = disk_log:chunk(n, start),
    ?line {ok, C6} = disk_log:chunk_step(n, C5, 1),
    ?line {C7, [_]} = disk_log:chunk(n, C6),
    ?line {ok, C8} = disk_log:chunk_step(n, C7, 1),
    ?line {_, [_]} = disk_log:chunk(n, C8),
    ?line ok = disk_log:close(n),

    %% wrap, read_only
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {mode, read_only},
				   {format, internal}, {size, {100,No}}]),
    ?line {CC1, [_]} = disk_log:chunk(n, start),
    ?line {CC2, [_]} = disk_log:chunk(n, CC1),
    ?line {CC3, [_]} = disk_log:chunk(n, CC2),
    ?line {CC4, [_]} = disk_log:chunk(n, CC3, 1),
    ?line eof = disk_log:chunk(n, CC4),
    ?line {CC5, [_]} = disk_log:chunk(n, start),
    ?line {ok, CC6} = disk_log:chunk_step(n, CC5, 1),
    ?line {CC7, [_]} = disk_log:chunk(n, CC6),
    ?line {ok, CC8} = disk_log:chunk_step(n, CC7, 1),
    ?line {_, [_]} = disk_log:chunk(n, CC8),
    ?line ok = disk_log:close(n),

    %% OTP-3716. A bug: {Error, List} and {Error, List, Bad} could be
    %% returned from chunk/2.
    %% Magic bytes not OK.
    %% File header (8 bytes) OK, item header not OK.
    ?line InvalidFile = add_ext(File, 1),
    ?line crash(InvalidFile, 15),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {mode, read_only},
				   {format, internal}, {size, {100,No}}]),
    ?line {_, [], 61} = disk_log:chunk(n, start),
    ?line ok = disk_log:close(n),
    %% read_write...
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {100,No}}]),
    ?line Error3 = {error, {corrupt_log_file, Culprit}} = 
        disk_log:chunk(n, start),
    ?line "The disk log file" ++ _ = format_error(Error3),
    ?line Culprit = InvalidFile,
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% Two wrap log files, writing the second one, then reading the first
    %% one, where a bogus term resides.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {40,No}}]),
    ?line ok = disk_log:log_terms(n, [{this,is}]), % first file full
    ?line ok = disk_log:log_terms(n, [{some,terms}]), % second file full
    ?line 2 = curf(n),
    ?line BadFile = add_ext(File, 1),
    ?line crash(BadFile, 28), % the _binary_ is now invalid
    ?line {error, {corrupt_log_file, BFile}} = disk_log:chunk(n, start, 1),
    ?line BadFile = BFile,
    ?line ok = disk_log:close(n),
    %% The same, with a halt log.
    ?line file:delete(File), % cleanup
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line ok = disk_log:log_terms(n, [{this,is}]),
    ?line ok = disk_log:sync(n),
    ?line crash(File, 28), % the _binary_ is now invalid
    ?line {error, {corrupt_log_file, File2}} = disk_log:chunk(n, start, 1),
    ?line crash(File, 10),
    ?line {error,{corrupt_log_file,_}} = disk_log:bchunk(n, start, 1),
    ?line true = File == File2,
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% halt, read_write
    ?line file:delete(File), % cleanup
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line ok = disk_log:log_terms(n, [BB,BB,BB,BB]),
    ?line {D1, [Ch1]} = disk_log:chunk(n, start, 1),
    ?line Ch1 = BB,
    ?line {D2, [Ch2]} = disk_log:chunk(n, D1, 1),
    ?line Ch2 = BB,
    ?line {D3, [Ch3]} = disk_log:chunk(n, D2, 1),
    ?line Ch3 = BB,
    ?line {D4, [Ch4]} = disk_log:chunk(n, D3, 1),
    ?line Ch4 = BB,
    ?line eof = disk_log:chunk(n, D4),
    ?line ok = disk_log:close(n),

    %% halt, read_only
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal},{mode,read_only}]),
    ?line {E1, [Ch5]} = disk_log:chunk(n, start, 1),
    ?line Ch5 = BB,
    ?line {E2, [Ch6]} = disk_log:chunk(n, E1, 1),
    ?line Ch6 = BB,
    ?line {E3, [Ch7]} = disk_log:chunk(n, E2, 1),
    ?line Ch7 = BB,
    ?line {E4, [Ch8]} = disk_log:chunk(n, E3, 1),
    ?line Ch8 = BB,
    ?line eof = disk_log:chunk(n, E4),
    ?line ok = disk_log:close(n),
    ?line file:delete(File), % cleanup

    %% More than 64 kB term.
    ?line BBB = term_to_binary(lists:duplicate(66000,$a)),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line ok = disk_log:log_terms(n, [BBB]),
    ?line {F1, [BBB1]} = disk_log:chunk(n, start),
    ?line BBB1 = BBB,
    ?line eof = disk_log:chunk(n, F1),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_only}]),
    ?line {F1r, [BBB2]} = disk_log:chunk(n, start),
    ?line BBB2 = BBB,
    ?line eof = disk_log:chunk(n, F1r),
    ?line ok = disk_log:close(n),

    ?line truncate(File, 8192),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line {error, {corrupt_log_file, _}} = disk_log:chunk(n, start),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_only}]),
    ?line {K1, [], 8176} = disk_log:chunk(n, start),
    ?line eof = disk_log:chunk(n, K1),
    ?line ok = disk_log:close(n),
    ?line file:delete(File), % cleanup

    %% OTP-3716. A bug: eof in the middle of the last element is not ok.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line ok = disk_log:log_terms(n, [B,BB]),
    ?line ok = disk_log:close(n),
    ?line truncate(File, 80),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line {G1, [_]} = disk_log:chunk(n, start, 1),
    ?line {error, {corrupt_log_file, _}} = disk_log:chunk(n, G1, 1),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_only}]),
    ?line {G1r, [_]} = disk_log:chunk(n, start, 1),
    ?line {_, [], 4} = disk_log:chunk(n, G1r, 1),
    ?line ok = disk_log:close(n),
    ?line file:delete(File), % cleanup

    %% Opening a wrap log read-only. The second of four terms is destroyed.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {size, {4000,No}}]),
    ?line ok = disk_log:log_terms(n, 
				  [{this,is},{some,terms},{on,a},{wrap,file}]),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, internal}, {mode, read_only}]),
    ?line CrashFile = add_ext(File, 1),
    ?line crash(CrashFile, 51), % the binary term {some,terms} is now bad
    ?line {H1, [{this,is}], 18} = disk_log:chunk(n, start, 10),
    ?line {H2, [{on,a},{wrap,file}]} = disk_log:chunk(n, H1),
    ?line eof = disk_log:chunk(n, H2),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% The same as last, but with a halt log.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_write}]),
    ?line ok = disk_log:alog_terms(n, [{this,is},{some,terms}]),
    ?line ok = disk_log:log_terms(n, [{on,a},{halt,file}]),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_only}]),
    ?line crash(File, 51), % the binary term {some,terms} is now bad
    ?line {J1, [{this,is}], 18} = disk_log:chunk(n, start, 10),
    ?line {J2, [{on,a},{halt,file}]} = disk_log:chunk(n, J1),
    ?line eof = disk_log:chunk(n, J2),
    ?line ok = disk_log:close(n),
    ?line file:delete(File),

    %% OTP-7641. Same as last one, but the size of the bad term is
    %% less than ?HEADERSz (8) bytes.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_write}]),
    ?line ok = disk_log:alog_terms(n, [{this,is},{s}]),
    ?line ok = disk_log:log_terms(n, [{on,a},{halt,file}]),
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}, {mode, read_only}]),
    ?line crash(File, 44), % the binary term {s} is now bad
    ?line {J11, [{this,is}], 7} = disk_log:chunk(n, start, 10),
    ?line {J21, [{on,a},{halt,file}]} = disk_log:chunk(n, J11),
    ?line eof = disk_log:chunk(n, J21),
    ?line ok = disk_log:close(n),
    ?line file:delete(File),

    %% Minimal MD5-proctected term, and maximal unprotected term.
    %% A chunk ends in the middle of the MD5-sum.
    ?line MD5term = mk_bytes(64*1024-8),
    ?line NotMD5term = mk_bytes((64*1024-8)-1),
    ?line Term2 = mk_bytes((64*1024-8)-16),
    ?line MD5L = [MD5term,NotMD5term,Term2,MD5term,MD5term,NotMD5term],
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line ok = disk_log:log_terms(n, MD5L),
    ?line true = MD5L == get_all_terms(n),
    ?line ok = disk_log:close(n),
    ?line true = MD5L == get_all_terms(n, File, halt),
    ?line crash(File, 21), % the MD5-sum of the first term is now bad
    ?line true = {tl(MD5L),64*1024-8} == get_all_terms_and_bad(n, File, halt),
    ?line {_,64*1024-8} = get_all_binary_terms_and_bad(n, File, halt),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, halt},
				   {format, internal}]),
    ?line {error, {corrupt_log_file, _}} = disk_log:chunk(n, start),
    ?line ok = disk_log:close(n),
    ?line file:delete(File),

    %% A file with "old" terms (magic word is MAGICINT).
    DataDir = ?datadir(Conf),
    OldTermsFileOrig = filename:join(DataDir, "old_terms.LOG"),
    OldTermsFile = filename:join(Dir, "old_terms.LOG"),
    ?line copy_file(OldTermsFileOrig, OldTermsFile),
    ?line {[_,_,_,_],0} = get_all_terms_and_bad(n, OldTermsFile, halt),
    ?line {ok, n} = disk_log:open([{name, n}, {file, OldTermsFile}, 
                                   {type, halt}, {format, internal}]),
    ?line [_,_,_,_] = get_all_terms(n),
    ?line ok = disk_log:close(n),
    ?line file:delete(OldTermsFile),

    ok.

error_index(suite) -> [];
error_index(doc) -> 
    ["OTP-5558. Keep the contents of index files after disk crash."];
error_index(Conf) when is_list(Conf) ->
    ?line Dir = ?privdir(Conf),

    ?line File = filename:join(Dir, "n.LOG"),
    ?line IdxFile = File ++ ".idx",
    ?line No = 4,
    ?line file:delete(File),
    ?line del(File, No),	% cleanup

    Args = [{name,n},{type,wrap},{size,{100,No}},{file,File}],
    ?line {ok, n} = disk_log:open(Args),
    ?line ok = disk_log:close(n),
    ?line Q = qlen(),
    P0 = pps(),
    ?line ok = file:write_file(IdxFile, <<"abc">>),
    ?line {error, {invalid_index_file, _}} = disk_log:open(Args),
    ?line {error, {invalid_index_file, _}} = disk_log:open(Args),
    ?line {error, {invalid_index_file, _}} = disk_log:open(Args),

    ?line del(File, No),
    ?line true = (P0 == pps()),
    ?line true = (Q == qlen()),
    ok.
    
truncate(suite) -> [];
truncate(doc) -> 
    ["Test truncate/1 on halt and wrap logs."];
truncate(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),

    ?line Q = qlen(),
    Halt = join(Dir, "halt.LOG"),
    % Halt logs.

    ?line file:delete(Halt), % cleanup
    ?line {ok, halt} = disk_log:open([{name, halt}, {type, halt}, {file, Halt},
				      {head, header}, {notify, true}]),
    ?line infinity = sz(halt),
    ?line ok = disk_log:truncate(halt, tjohej),
    ?line rec(1, {disk_log, node(), halt, {truncated, 1}}),
    ?line ok = disk_log:change_size(halt, 10000),
    ?line 10000 = sz(halt),
    ?line disk_log:close(halt),
    ?line [tjohej] = get_all_terms(halt, Halt, halt),
    ?line file:delete(Halt),

    ?line {ok, halt} = disk_log:open([{name, halt}, {type, halt}, {file, Halt},
				      {head, header}, {notify, true}]),
    ?line ok = disk_log:truncate(halt),
    ?line rec(1, {disk_log, node(), halt, {truncated, 1}}),
    ?line disk_log:close(halt),
    ?line [header] = get_all_terms(halt, Halt, halt),
    ?line file:delete(Halt),
    
    ?line {ok, halt} = disk_log:open([{name, halt}, {type, halt}, 
                                      {file, Halt}, {format, external},
				      {head, "header"}, {notify, false}]),
    ?line ok = disk_log:btruncate(halt, "apa"),
    ?line disk_log:close(halt),
    ?line 3 = file_size(Halt),
    ?line file:delete(Halt),
    
    %% Wrap logs.
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line B = mk_bytes(60),
    ?line del(File, No),	% cleanup

    %% Internal with header.
    ?line Size = {100, No},
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {head, header}, {notify, true}, 
				   {size, Size}]),
    ?line ok = disk_log:log_terms(n, [B,B,B]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:truncate(n, apa),
    ?line rec(1, {disk_log, node(), n, {truncated, 6}}),
    ?line {0, 0} = no_overflows(n),
    ?line 23 = curb(n),
    ?line 1 = curf(n),
    ?line 1 = cur_cnt(n),
    ?line true = (Size == sz(n)),

    ?line ok = disk_log:log_terms(n, [B, B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:close(n),
    ?line [apa, _, header, _] = get_all_terms(n, File, wrap),
    ?line del(File, No),
    
    %% Internal without general header.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, 
				   {size, {100, No}}]),
    ?line ok = disk_log:log_terms(n, [B,B,B]),
    ?line rec(2, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:truncate(n, apa),
    ?line rec(1, {disk_log, node(), n, {truncated, 3}}),
    ?line {0, 0} = no_overflows(n),
    ?line 23 = curb(n),
    ?line 1 = curf(n),
    ?line 1 = cur_cnt(n),
    ?line true = (Size == sz(n)),

    ?line ok = disk_log:log_terms(n, [B, B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:close(n),
    ?line [apa, _, _] = get_all_terms(n, File, wrap),
    ?line del(File, No),

    %% Internal without any header.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, 
				   {size, {100, No}}]),
    ?line ok = disk_log:log_terms(n, [B,B,B]),
    ?line rec(2, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:truncate(n),
    ?line rec(1, {disk_log, node(), n, {truncated, 3}}),
    ?line {0, 0} = no_overflows(n),
    ?line 8 = curb(n),
    ?line 1 = curf(n),
    ?line 0 = cur_cnt(n),
    ?line true = (Size == sz(n)),

    ?line ok = disk_log:log_terms(n, [B, B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:close(n),
    ?line [_, _] = get_all_terms(n, File, wrap),
    ?line del(File, No),
    ?line Q = qlen(),
    ok.


many_users(suite) -> [];
many_users(doc) -> 
    ["Test many users logging and sync:ing at the same time."];
many_users(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    N = 100,
    NoClients = 10,
    Fun1 = fun(Name, Pid, I) -> disk_log:log(Name, {Pid, I}) end,
    Fun2 = fun(Name, Pid, I) -> ok = disk_log:log(Name, {Pid, I}),
				disk_log:sync(Name) end,
    ?line {C1, T1} = many(Fun2, NoClients, N, halt, internal, infinity, Dir),
    ?line true = lists:duplicate(NoClients, ok) == C1,
    ?line true = length(T1) == N*NoClients,
    ?line {C2, T2} = many(Fun1, NoClients, N, halt, internal, 1000, Dir),
    ?line true = lists:duplicate(NoClients, {error, {full,"log.LOG"}}) == C2,
    ?line true = length(T2) > 0,
    ?line {C3, T3} = many(Fun2, NoClients, N, wrap, internal, 
			  {300*NoClients,20}, Dir),
    ?line true = lists:duplicate(NoClients, ok) == C3,
    ?line true = length(T3) == N*NoClients,
    ok.

many(Fun, NoClients, N, Type, Format, Size, Dir) ->
    Name = "log.LOG",
    File = filename:join(Dir, Name),
    del_files(Size, File),
    ?line Q = qlen(),
    ?line {ok, _} = disk_log:open([{name,Name}, {type,Type}, {size,Size}, 
				   {format,Format}, {file,File}]),
    ?line Pids = spawn_clients(NoClients, client, [self(), Name, N, Fun]),
    ?line Checked = check_clients(Pids),
    ?line ok = disk_log:close(Name),
    ?line Terms = get_all_terms(Name, File, Type),
    ?line del_files(Size, File),
    ?line Q = qlen(),
    ?line {Checked, Terms}.

spawn_clients(0, _F, _A) ->
    [];
spawn_clients(I, F, A) ->
    [spawn_link(?MODULE, F, A) | spawn_clients(I-1, F, A)].

check_clients(Pids) ->
    lists:map(fun(Pid) -> receive {Pid, Reply} -> Reply end end, Pids).

client(From, _Name, 0, _Fun) ->
    From ! {self(), ok};
client(From, Name, N, Fun) ->
    %% Fun is called N times.
    case Fun(Name, self(), N) of
	ok -> client(From, Name, N-1, Fun);
	Else -> From ! {self(), Else}
    end.

del_files({_NoBytes,NoFiles}, File) ->
    del(File, NoFiles);
del_files(_Size, File) ->
    file:delete(File).




info_current(suite) -> [];
info_current(doc) -> 
    ["Test no_current_{bytes, items} as returned by info/0."];
info_current(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    No = 4,
    B = mk_bytes(60),
    BB = mk_bytes(160), % bigger than a single wrap log file
    SB = mk_bytes(10),  % much smaller than a single wrap log file
    ?line del(File, No),% cleanup

    ?line Q = qlen(),
    %% Internal with header.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {head, header}, {size, {100,No}}]),
    ?line {26, 1} = {curb(n), cur_cnt(n)},
    ?line {1, 1}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log(n, B),
    ?line {94, 2} = {curb(n), cur_cnt(n)},
    ?line {2, 2}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, 
				   {head, header}, {size, {100,No}}]),
    ?line {94, 2} = {curb(n), cur_cnt(n)},
    ?line {0, 2}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {94, 2} = {curb(n), cur_cnt(n)},
    ?line {2, 4}  = {no_written_items(n), no_items(n)},
    ?line disk_log:inc_wrap_file(n),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {26, 1} = {curb(n), cur_cnt(n)},
    ?line {3, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [B,B,B]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line {94, 2} = {curb(n), cur_cnt(n)},
    ?line {8, 7}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line {94, 2} = {curb(n), cur_cnt(n)},
    ?line {12, 7}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [BB,BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 2}}),
    ?line {194, 2} = {curb(n), cur_cnt(n)},
    ?line {16, 7}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [SB,SB,SB]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line {80, 4} = {curb(n), cur_cnt(n)},
    ?line {20, 9}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% Internal without header.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}]),
    ?line {8, 0} = {curb(n), cur_cnt(n)},
    ?line {0, 0}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log(n, B),
    ?line {76, 1} = {curb(n), cur_cnt(n)},
    ?line {1, 1}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, {size, {100,No}}]),
    ?line {76, 1} = {curb(n), cur_cnt(n)},
    ?line {0, 1}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {76, 1} = {curb(n), cur_cnt(n)},
    ?line {1, 2}  = {no_written_items(n), no_items(n)},
    ?line disk_log:inc_wrap_file(n),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {8, 0} = {curb(n), cur_cnt(n)},
    ?line {1, 2}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [B,B,B]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line {76, 1} = {curb(n), cur_cnt(n)},
    ?line {4, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line ok = disk_log:log_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line {76, 1} = {curb(n), cur_cnt(n)},
    ?line {6, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [BB,BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 1}}),
    ?line {176, 1} = {curb(n), cur_cnt(n)},
    ?line {8, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:log_terms(n, [SB,SB,SB]),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line {62, 3} = {curb(n), cur_cnt(n)},
    ?line {11, 6}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% External with header.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external}, {head, "header"}, 
				   {size, {100,No}}]),
    ?line {6, 1} = {curb(n), cur_cnt(n)},
    ?line {1, 1}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog(n, B),
    ?line {62, 2} = {curb(n), cur_cnt(n)},
    ?line {2, 2}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external}, {head, "header"}, 
				   {notify, true}, {size, {100,No}}]),
    ?line {62, 2} = {curb(n), cur_cnt(n)},
    ?line {0, 2}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {62, 2} = {curb(n), cur_cnt(n)},
    ?line {2, 4}  = {no_written_items(n), no_items(n)},
    ?line disk_log:inc_wrap_file(n),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {6, 1} = {curb(n), cur_cnt(n)},
    ?line {3, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [B,B,B]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line {62, 2} = {curb(n), cur_cnt(n)},
    ?line {8, 7}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line ok = disk_log:blog_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line {62, 2} = {curb(n), cur_cnt(n)},
    ?line {12, 7}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [BB,BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 2}}),
    ?line {162, 2} = {curb(n), cur_cnt(n)},
    ?line {16, 7}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [SB,SB,SB]),

    ?line rec(1, {disk_log, node(), n, {wrap, 2}}),
    ?line {24, 4} = {curb(n), cur_cnt(n)},
    ?line {20, 9}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% External without header.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {format, external}, {size, {100,No}}]),
    ?line {0, 0} = {curb(n), cur_cnt(n)},
    ?line {0, 0}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog(n, B),
    ?line {56, 1} = {curb(n), cur_cnt(n)},
    ?line {1, 1}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {notify, true}, 
				   {format, external}, {size, {100,No}}]),
    ?line {56, 1} = {curb(n), cur_cnt(n)},
    ?line {0, 1}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {56, 1} = {curb(n), cur_cnt(n)},
    ?line {1, 2}  = {no_written_items(n), no_items(n)},
    ?line disk_log:inc_wrap_file(n),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line {0, 0} = {curb(n), cur_cnt(n)},
    ?line {1, 2}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [B,B,B]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line {56, 1} = {curb(n), cur_cnt(n)},
    ?line {4, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line ok = disk_log:blog_terms(n, [B]),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line {56, 1} = {curb(n), cur_cnt(n)},
    ?line {6, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [BB,BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 1}}),
    ?line {156, 1} = {curb(n), cur_cnt(n)},
    ?line {8, 4}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:blog_terms(n, [SB,SB,SB]),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line {18, 3} = {curb(n), cur_cnt(n)},
    ?line {11, 6}  = {no_written_items(n), no_items(n)},
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    ?line Q = qlen(),
    ok.



change_size_before(suite) -> [];
change_size_before(doc) -> 
    ["Change size of a wrap log file before we have reached "
     "to the file index corresponding to the new size"];
change_size_before(Conf) when is_list(Conf) ->

    Log_1_1 = "first log  first message",
    Log_1_2 = "first log  second message",
    Log_2_1 = "second log  first message",
    Log_2_2 = "second log  second message",
    Log_3_1 = "third log  first message",
    Log_3_2 = "third log  second message",
    Log_4_1 = "fourth log  first message",
    Log_4_2 = "fourth log  second message",
    Log_5_1 = "fifth log  first message",
    Log_5_2 = "fifth log  second message",
    Log_1_2_1 = "first log  second round 1",
    Log_1_2_2 = "first log  second round 2",


    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    del(File, 5),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, 
				   {type, wrap}, {size, {100,5}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:change_size(a, {100, 3}),
    ?line [Log_1_1, Log_1_2,
	   Log_2_1, Log_2_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_1_2_1),
    ?line disk_log:log(a, Log_1_2_2),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_1_2_1, Log_1_2_2] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
				   {size, {100,3}}]),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_1_2_1, Log_1_2_2] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
				   {size, {60,5}}, {format, external}]),
    ?line disk_log:blog(a, Log_1_1),
    ?line disk_log:blog(a, Log_1_2),
    ?line disk_log:blog(a, Log_2_1),
    ?line disk_log:blog(a, Log_2_2),
    ?line disk_log:change_size(a, {60, 3}),
    ?line ok = disk_log:sync(a),
    ?line {ok, Fd1} = file:open(File ++ ".1", [read]),
    ?line Log11_12 = Log_1_1 ++ Log_1_2,
    ?line {ok,Log11_12} = file:read(Fd1, 200),
    ?line ok = file:close(Fd1),
    ?line {ok, Fd2} = file:open(File ++ ".2", [read]),
%    ?t:format(0, "~p~n",[file:read(Fd2, 200)]),
    ?line Log21_22 = Log_2_1 ++ Log_2_2,
    ?line {ok,Log21_22} = file:read(Fd2, 200),
    ?line ok = file:close(Fd2),
    ?line disk_log:blog(a, Log_3_1),
    ?line disk_log:blog(a, Log_3_2),
    ?line disk_log:blog(a, Log_1_2_1),
    ?line disk_log:blog(a, Log_1_2_2),
    ?line ok = disk_log:sync(a),
    ?line {ok, Fd2a} = file:open(File ++ ".2", [read]),
    ?line {ok,Log21_22} = file:read(Fd2a, 200),
    ?line ok = file:close(Fd2a),
    ?line {ok, Fd3a} = file:open(File ++ ".3", [read]),
    ?line Log31_32 = Log_3_1 ++ Log_3_2,
    ?line {ok,Log31_32} = file:read(Fd3a, 200),
    ?line ok = file:close(Fd3a),
    ?line {ok, Fd1a} = file:open(File ++ ".1", [read]),
    ?line Log121_122 = Log_1_2_1 ++ Log_1_2_2,
    ?line {ok,Log121_122} = file:read(Fd1a, 200),
    ?line ok = file:close(Fd1a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
				   {size, {60,3}}, {format, external}]),
    ?line {ok, Fd2b} = file:open(File ++ ".2", [read]),
    ?line {ok,Log21_22} = file:read(Fd2b, 200),
    ?line ok = file:close(Fd2b),
    ?line {ok, Fd3b} = file:open(File ++ ".3", [read]),
    ?line {ok,Log31_32} = file:read(Fd3b, 200),
    ?line ok = file:close(Fd3b),
    ?line {ok, Fd1b} = file:open(File ++ ".1", [read]),
    ?line {ok,Log121_122} = file:read(Fd1b, 200),
    ?line ok = file:close(Fd1b),
    disk_log:close(a),
    del(File, 5),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100,5}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:change_size(a, {60, 3}),
    ?line [Log_1_1, Log_1_2,
	   Log_2_1, Log_2_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_1_2_1),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1,
	   Log_1_2_1] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {60,3}}]),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1,
	   Log_1_2_1] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {60, 3}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:change_size(a, {100, 5}),
    ?line [Log_1_1,
	   Log_2_1] = get_all_terms(a),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:log(a, Log_5_1),
    ?line disk_log:log(a, Log_5_2),
    ?line disk_log:log(a, Log_1_2_1),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_5_1, Log_5_2,
	   Log_1_2_1] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100, 5}}]),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_5_1, Log_5_2,
	   Log_1_2_1] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5).



change_size_during(suite) -> [];
change_size_during(doc) -> ["Change size of a wrap log file while logging  "
			    "to a file index between the old and the new size"];
change_size_during(Conf) when is_list(Conf) ->

    Log_1_1 = "first log  first message",
    Log_1_2 = "first log  second message",
    Log_2_1 = "second log  first message",
    Log_2_2 = "second log  second message",
    Log_3_1 = "third log  first message",
    Log_3_2 = "third log  second message",
    Log_4_1 = "fourth log  first message",
    Log_4_2 = "fourth log  second message",
    Log_5_1 = "fifth log  first message",
    Log_5_2 = "fifth log  second message",
    Log_1_2_1 = "first log  second round 1",
    Log_1_2_2 = "first log  second round 2",
    Log_2_2_1 = "second log  second round 1",
    Log_2_2_2 = "second log  second round 2",
    Log_3_2_1 = "third log  second round 1",
    Log_3_2_2 = "third log  second round 2",
    Log_1_3_1 = "first log  third round 1",
    Log_1_3_2 = "first log  third round 2",

    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100,5}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:log(a, Log_5_1),
    ?line disk_log:log(a, Log_5_2),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:change_size(a, {100, 3}),
    ?line [Log_5_1, Log_5_2,
	   Log_1_1, Log_1_2,
	   Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_4_1, Log_4_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_1_2_1),
    ?line disk_log:log(a, Log_1_2_2),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_1_2_1, Log_1_2_2] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100,3}}]),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_1_2_1, Log_1_2_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_2_2_1),
    ?line disk_log:log(a, Log_2_2_2),
    ?line disk_log:log(a, Log_3_2_1),
    ?line disk_log:log(a, Log_3_2_2),
    ?line disk_log:log(a, Log_1_3_1),
    ?line disk_log:log(a, Log_1_3_2),
    ?line [Log_2_2_1, Log_2_2_2,
	   Log_3_2_1, Log_3_2_2,
	   Log_1_3_1, Log_1_3_2] = get_all_terms(a),
    disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100,3}}]),
    ?line [Log_2_2_1, Log_2_2_2,
	   Log_3_2_1, Log_3_2_2,
	   Log_1_3_1, Log_1_3_2] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100,5}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:log(a, Log_5_1),
    ?line disk_log:log(a, Log_5_2),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:log(a, Log_5_1),
    ?line disk_log:log(a, Log_5_2),
    ?line disk_log:change_size(a, {100, 3}),
    ?line [Log_1_1, Log_1_2,
	   Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_5_1, Log_5_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_1_2_1),
    ?line disk_log:log(a, Log_1_2_2),
    ?line disk_log:log(a, Log_2_2_1),
    ?line disk_log:log(a, Log_2_2_2),
    ?line disk_log:log(a, Log_3_2_1),
    ?line disk_log:log(a, Log_3_2_2),
    ?line disk_log:log(a, Log_1_3_1),
    ?line disk_log:log(a, Log_1_3_2),
    ?line [Log_2_2_1, Log_2_2_2,
	   Log_3_2_1, Log_3_2_2,
	   Log_1_3_1, Log_1_3_2] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, {size, {100,3}}]),
    ?line [Log_2_2_1, Log_2_2_2,
	   Log_3_2_1, Log_3_2_2,
	   Log_1_3_1, Log_1_3_2] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5).


change_size_after(suite) -> [];
change_size_after(doc) -> 
    ["Change size of a wrap log file before we have reached "
     "(on the second round) "
     "to the file index corresponding to the new size"];
change_size_after(Conf) when is_list(Conf) ->

    Log_1_1 = "first log  first message",
    Log_1_2 = "first log  second message",
    Log_2_1 = "second log  first message",
    Log_2_2 = "second log  second message",
    Log_3_1 = "third log  first message",
    Log_3_2 = "third log  second message",
    Log_4_1 = "fourth log  first message",
    Log_4_2 = "fourth log  second message",
    Log_5_1 = "fifth log  first message",
    Log_5_2 = "fifth log  second message",
    Log_1_2_1 = "first log  second round 1",
    Log_1_2_2 = "first log  second round 2",

    Dir = ?privdir(Conf),
    File = filename:join(Dir, "a.LOG"),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
                                   {size, {100,5}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:log(a, Log_5_1),
    ?line disk_log:log(a, Log_5_2),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:change_size(a, {100, 3}),
    ?line [Log_3_1,Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_5_1, Log_5_2,
	   Log_1_1, Log_1_2,
	   Log_2_1, Log_2_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_1_2_1),
    ?line disk_log:log(a, Log_1_2_2),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_1_2_1, Log_1_2_2] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
                                   {size, {100,3}}]),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1, Log_3_2,
	   Log_1_2_1, Log_1_2_2] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
                                   {size, {100,5}}]),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_3_2),
    ?line disk_log:log(a, Log_4_1),
    ?line disk_log:log(a, Log_4_2),
    ?line disk_log:log(a, Log_5_1),
    ?line disk_log:log(a, Log_5_2),
    ?line disk_log:log(a, Log_1_1),
    ?line disk_log:log(a, Log_1_2),
    ?line disk_log:log(a, Log_2_1),
    ?line disk_log:log(a, Log_2_2),
    ?line disk_log:change_size(a, {60, 3}),
    ?line [Log_3_1,Log_3_2,
	   Log_4_1, Log_4_2,
	   Log_5_1, Log_5_2,
	   Log_1_1, Log_1_2,
	   Log_2_1, Log_2_2] = get_all_terms(a),
    ?line disk_log:log(a, Log_3_1),
    ?line disk_log:log(a, Log_1_2_1),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1,
	   Log_1_2_1] = get_all_terms(a),

    ?line disk_log:close(a),
    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
                                   {size, {60,3}}]),
    ?line [Log_2_1, Log_2_2,
	   Log_3_1,
	   Log_1_2_1] = get_all_terms(a),
    disk_log:close(a),
    del(File, 5).



default_size(suite) -> [];
default_size(doc) -> ["Open an existing wrap log without size option "];
default_size(Conf) when is_list(Conf) ->
    ?line Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "a.LOG"),
    ?line {error, {badarg, size}} = disk_log:open([{name,a}, {file, File}, 
                                                   {type, wrap}]),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}, 
                                   {size, {100,5}}]),
    ?line disk_log:close(a),

    ?line {ok, a} = disk_log:open([{name,a}, {file, File}, {type, wrap}]),
    ?line {100, 5} = disk_log_1:read_size_file(File),
    ?line ok = disk_log:close(a),
    ?line del(File, 5).

change_size2(suite) -> [];
change_size2(doc) -> ["Testing change_size/2 a bit more..."];
change_size2(Conf) when is_list(Conf) ->
    
    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No),	% cleanup

    %% External halt.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {size, 100000},
                                   {format, external}, {type, halt}]),
    ?line B = mk_bytes(60), % 56 actually...
    ?line ok = disk_log:blog_terms(n, [B,list_to_binary(B),B]),
    ?line Error1 = {error, {new_size_too_small,n,168}} = 
        disk_log:change_size(n, 167),
    ?line "The current size" ++ _ = format_error(Error1),
    ?line ok = disk_log:change_size(n, infinity),
    ?line ok = disk_log:change_size(n, 168),
    ?line ok = disk_log:close(n),
    ?line file:delete(File), % cleanup

    %% External wrap.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {notify, true},
				   {format, external}]),
    ?line BB = mk_bytes(160),
    ?line ok = disk_log:blog_terms(n, [BB, BB, BB, BB]), % create all files
    %% Used to be one message, but now one per wrapped file.
    ?line rec(3, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:blog_terms(n, [BB, BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 1}}),
    ?line ok = disk_log:change_size(n, {100, 2}),
    ?line ok = disk_log:change_size(n, {100, 2}),
    ?line {100, 2} = sz(n),
    ?line ok = disk_log:balog_terms(n, [BB, BB]),
    ?line ok = disk_log:balog_terms(n, [BB]),
    ?line ok = disk_log:blog_terms(n, [BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(4, {disk_log, node(), n, {wrap, 1}}),
    ?line ok = disk_log:change_size(n, {100, 4}),
    ?line ok = disk_log:close(n),
    ?line del(File, No),

    %% Internal wrap.
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {notify, true},
				   {format, internal}]),
    ?line ok = disk_log:blog_terms(n, [BB, BB, BB, BB]), % create all files
    %% Used to be one message, but now one per wrapped file.
    ?line rec(3, {disk_log, node(), n, {wrap, 0}}),
    ?line ok = disk_log:blog_terms(n, [BB, BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(2, {disk_log, node(), n, {wrap, 1}}),
    ?line ok = disk_log:change_size(n, {100, 2}),
    ?line {100, 2} = sz(n),
    ?line ok = disk_log:blog_terms(n, [BB, BB, BB, BB]),
    %% Used to be one message, but now one per wrapped file.
    ?line rec(4, {disk_log, node(), n, {wrap, 1}}),
    ?line ok = disk_log:close(n),
    ?line del(File, No).

change_size_truncate(suite) -> [];
change_size_truncate(doc) -> ["OTP-3484: truncating index file"];
change_size_truncate(Conf) when is_list(Conf) ->
    
    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "bert.LOG"),
    ?line No = 3,
    ?line B = mk_bytes(60),

    %% The problem here is truncation of the index file. One cannot easily
    %% check that the index file is correctly updated, but print_index_file()
    %% can be used to follow the progress more closely.

    %% Part 1.
    %% Change the size immediately after creating the log, while there
    %% are no log files. This used to write stuff a negative offset
    %% from the beginning of the file.
    ?line del(File, No+1),
    ?line {ok, bert} = disk_log:open([{name,bert}, {type,wrap}, {file, File},
				      {notify, true}, {size,{1000,255}}]),
    ?line ok = disk_log:change_size(bert,{100,No}),
    ?line ok = disk_log:blog(bert, B),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 0}}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 0}}),
    ?line 3 = curf(bert),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line 1 = curf(bert),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),

    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),

    % Three items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),
    ?line 3 = curf(bert),
    ?line ok = disk_log:change_size(bert,{100,1}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    % Three items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    % One item expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),

    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line ok = disk_log:close(bert),
    ?line del(File, No),

    %% Part 2.
    %% Change the size twice, the second time while the the effects of
    %% the first changed have not yet been handled. Finally close before
    %% the index file has been truncated.

    ?line del(File, No),
    ?line {ok, bert} = disk_log:open([{name,bert}, {type,wrap}, {file, File},
				      {notify, true}, {size,{100,No}}]),
    ?line ok = disk_log:blog(bert, B),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 0}}),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 0}}),

    ?line 3 = curf(bert),
    ?line ok = disk_log:change_size(bert,{100,No-1}),

    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),

    ?line 1 = curf(bert),
    ?line ok = disk_log:change_size(bert,{100,No+1}),    

    % Three items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),

    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),

    % Three items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),

    ?line 2 = curf(bert),
    ?line ok = disk_log:change_size(bert,{100,1}),    

    % Three items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),

    ?line ok = disk_log:close(bert),
    
    % State: .siz is 1, current file is 2, index file size is 3...

    ?line {ok, bert} = disk_log:open([{name,bert}, {file, File}, 
				      {type,wrap}, {notify, true}]),
    
    % Three items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),
    
    ?line 2 = curf(bert),
    ?line ok = disk_log:blog(bert, B),
    ?line rec(1, {disk_log, node(), bert, {wrap, 1}}),
    ?line ok = disk_log:close(bert),

    ?line {ok, bert} = disk_log:open([{name,bert}, {file, File}, 
				      {type,wrap}, {notify, true}]),
    
    % Two items expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),
    
    ?line 1 = curf(bert),
    ?line ok = disk_log:blog(bert, B),
    %% Expect {wrap 0}. Nothing lost now, last wrap notification
    %% reported one lost item.
    ?line rec(1, {disk_log, node(), bert, {wrap, 0}}),

    % One item expected.
    % disk_log_1:print_index_file("bert.LOG.idx"),
    ?line ok = disk_log:close(bert),

    ?line del(File, No),
    ok.

change_attribute(suite) -> [];
change_attribute(doc) -> 
    ["Change notify and head"];
change_attribute(Conf) when is_list(Conf) ->

    Dir = ?privdir(Conf),
    ?line File = filename:join(Dir, "n.LOG"),
    ?line No = 4,
    ?line del(File, No),	% cleanup
    ?line B = mk_bytes(60),

    ?line Q = qlen(),

    % test change_notify
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}]),
    ?line {ok, n} = disk_log:open([{name, n}]), % ignored...
    ?line ok = disk_log:log_terms(n, [B,B]),
    ?line {error, {badarg, notify}} = disk_log:change_notify(n, self(), wrong),
    ?line ok = disk_log:change_notify(n, self(), false),
    ?line ok = disk_log:change_notify(n, self(), true),
    ?line Error1 = {error, {not_owner, _}} = 
        disk_log:change_notify(n, none, true),
    ?line "The pid" ++ _ = format_error(Error1),
    ?line 2 = no_written_items(n),
    ?line 0 = users(n),
    ?line Parent = self(),
    ?line Pid = spawn(fun() -> disk_log:close(n), Parent ! {self(),done} end),
    ?line receive {Pid, done} -> ok end,
    ?line 0 = users(n),
    ?line 1 = length(owners(n)),

    % test change_header
    ?line {error, {badarg, head}} = disk_log:change_header(n, none),
    ?line {error, {badarg, head}} = 
	disk_log:change_header(n, {head_func, {1,2,3}}),
    ?line ok = disk_log:change_header(n, {head, header}),
    ?line ok = disk_log:log(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line 4 = no_written_items(n),
    ?line ok = disk_log:change_header(n, {head, none}),
    ?line ok = disk_log:log(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 0}}),
    ?line 5 = no_written_items(n),
    ?line ok = disk_log:change_header(n, 
			     {head_func, {?MODULE, head_fun, [{ok,header}]}}),
    ?line ok = disk_log:log(n, B),
    ?line rec(1, {disk_log, node(), n, {wrap, 1}}),
    ?line 7 = no_written_items(n),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:close(n),
    ?line del(File, No),
    ?line file:delete(File), % cleanup
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {format, external},
				   {type, halt}]),
    ?line {error, {badarg, head}} = disk_log:change_header(n, {head, header}),
    ?line ok = disk_log:change_header(n, {head, "header"}),
    ?line ok = disk_log:close(n),
    ?line file:delete(File),
    
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}]),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}]),
    ?line ok = disk_log:change_notify(n, self(), true),
    ?line ok = disk_log:change_header(n, {head, tjolahopp}),
    ?line {ok, n} = disk_log:open([{name, n}, {file, File}, {type, wrap},
				   {size, {100,No}}, {notify, true}]),
    ?line ok = disk_log:close(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line Q = qlen(),
    ?line del(File, No).
    

dist_open(suite) -> [];
dist_open(doc) -> 
    ["Open a distributed log"];
dist_open(Conf) when is_list(Conf) ->
    ?line PrivDir = ?privdir(Conf),
    ?line true = is_alive(),

    ?line Q = qlen(),
    ?line File = filename:join(PrivDir, "n.LOG"),
    ?line File1 = filename:join(PrivDir, "n1.LOG"),
    ?line No = 3,
    ?line file:delete(File),
    ?line del(File, No),	% cleanup
    ?line del(File1, No),	% cleanup
    ?line B = mk_bytes(60),

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node} = start_node(disk_log, "-pa " ++ PA),
    ?line wait_for_ready_net(),

    %% open non-distributed on this node:
    ?line {ok,n} = disk_log:open([{name, n}, {file, File}, {type, halt},
                                  {distributed, []}]),

    ?line Error1 = {error, {halt_log, n}} = disk_log:inc_wrap_file(n),
    ?line "The halt log" ++ _ = format_error(Error1),
    ?line ok = disk_log:lclose(n),
    ?line file:delete(File),

    %% open distributed on this node:
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File}, {type, halt},
				    {distributed, [node()]}]),
    %% the error message is ignored:
    ?line ok = disk_log:inc_wrap_file(n),
    ?line ok = disk_log:close(n),
    ?line file:delete(File),

    %% open a wrap log on this node, write something on this node
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [node()]}]),
    ?line ok = disk_log:log(n, B),
    ?line ok = disk_log:close(n),

    %% open a wrap log on this node and aother node, write something
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [node()]}]),
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File1},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [Node]}]),
    ?line ok = disk_log:log(n, B),
    ?line ok = rpc:call(Node, disk_log, log, [n, B]),
    ?line ok = disk_log:close(n),
    ?line del(File, No),
    ?line del(File1, No),
    ?line file:delete(File),

    %% open a wrap log on this node and another node, use lclose
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [node()]}]),
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [node()]},
                                    {linkto,none}]),
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File1},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [Node]}]),
    ?line [_, _] = distributed(n),
    ?line ok = disk_log:lclose(n, Node),
    ?line [_] = distributed(n),
    ?line ok = disk_log:lclose(n),
    ?line ok = disk_log:lclose(n),
    ?line {error, no_such_log} = disk_log:info(n),
    ?line del(File, No),
    ?line del(File1, No),
    ?line file:delete(File),

    % open an invalid log file, and see how error are handled
    ?line First = "n.LOG.1",
    ?line make_file(PrivDir, First, 8),

    ?line {[], [_,_]} = disk_log:open([{name, n}, {file, File},
				       {type, wrap}, {size, {50, No}},
				       {distributed, [Node,node()]}]),
    ?line del(File, No),
    ?line file:delete(File),

    % open a wrap on one other node (not on this node)
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [Node]}]),
    ?line ok = rpc:call(Node, disk_log, log, [n, B]),
    ?line {error, no_such_log} = disk_log:lclose(n),
    ?line ok = disk_log:close(n),

    ?line Q = qlen(),

    ?line {error, no_such_log} = disk_log:info(n),
    ?line del(File, No),
    ?line file:delete(File),
    ?line stop_node(Node),
    ok.
    
dist_error_open(suite) -> [];
dist_error_open(doc) -> 
    ["Open a log distributed and not distributed"];
dist_error_open(Conf) when is_list(Conf) ->
    ?line PrivDir = ?privdir(Conf),
    ?line true = is_alive(),

    ?line Q = qlen(),
    ?line File = filename:join(PrivDir, "bert.LOG"),
    ?line File1 = filename:join(PrivDir, "bert1.LOG"),
    ?line No = 3,
    ?line file:delete(File),
    ?line del(File, No),	% cleanup
    ?line del(File1, No),	% cleanup

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node} = start_node(disk_log, "-pa " ++ PA),
    ?line wait_for_ready_net(),

    % open non-distributed on this node:
    ?line {ok,n} = disk_log:open([{name, n}, {file, File},
				  {type, wrap}, {size, {50, No}}]),

    % trying to open distributed on this node (error):
    ?line {[],[Error1={ENode,{error,{node_already_open,n}}}]} = 
	disk_log:open([{name, n}, {file, File},
		       {type, wrap}, {size, {50, No}},
		       {distributed, [node()]}]),
    ?line true = 
        lists:prefix(lists:flatten(io_lib:format("~p: The distribution", 
                                                 [ENode])),
                     format_error(Error1)),
    ?line ok = disk_log:lclose(n),

    % open distributed on this node:
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [node()]}]),
    
    % trying to open non-distributed on this node (error):
    ?line {_,{node_already_open,n}} = 
	disk_log:open([{name, n}, {file, File},
		       {type, wrap}, {size, {50, No}}]),

    ?line ok = disk_log:close(n),
    ?line Q = qlen(),

    ?line del(File, No),
    ?line del(File1, No),
    ?line file:delete(File),
    ?line stop_node(Node),
    ok.

dist_notify(suite) -> [];
dist_notify(doc) -> 
    ["Notification from other node"];
dist_notify(Conf) when is_list(Conf) ->
    ?line PrivDir = ?privdir(Conf),
    ?line true = is_alive(),
    
    ?line File = filename:join(PrivDir, "bert.LOG"),
    ?line File1 = filename:join(PrivDir, "bert1.LOG"),
    ?line No = 3,
    ?line B = mk_bytes(60),
    ?line file:delete(File),
    ?line file:delete(File1),
    ?line del(File, No),	% cleanup
    ?line del(File1, No),

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node} = start_node(disk_log, "-pa " ++ PA),
    ?line wait_for_ready_net(),

    % opening distributed on this node:
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File}, {notify, false},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [node()]}]),

    % opening distributed on other node:
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File1}, 
				    {notify, true}, {linkto, self()},
				    {type, wrap}, {size, {50, No}},
				    {distributed, [Node]}]),
    ?line disk_log:alog(n, B),
    ?line disk_log:alog(n, B),
    ?line ok = disk_log:sync(n),
    ?line rec(1, {disk_log, Node, n, {wrap, 0}}),
    ?line ok = disk_log:close(n),

    ?line del(File, No),
    ?line del(File1, No),
    ?line file:delete(File),
    ?line stop_node(Node),
    ok.

dist_terminate(suite) -> [];
dist_terminate(doc) -> 
    ["Terminating nodes with distributed logs"];
dist_terminate(Conf) when is_list(Conf) ->
    ?line Dir = ?privdir(Conf),
    ?line true = is_alive(),

    ?line File = filename:join(Dir, "n.LOG"),
    ?line File1 = filename:join(Dir, "n1.LOG"),
    No = 1,
    del(File, No),	% cleanup
    del(File1, No),	% cleanup

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node} = start_node(disk_log, "-pa " ++ PA),
    ?line wait_for_ready_net(),

    %% Distributed versions of two of the situations in close_block(/1.

    %% One of two owners terminates.
    ?line Pid1 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid2 = spawn_link(?MODULE, lserv, [n]),
    ?line {[{_, {ok, n}}], []} = sync_do(Pid1, {dist_open, File, node()}),
    ?line {[{_, {ok, n}}], []} = sync_do(Pid2, {dist_open, File1, Node}),
    ?line [_] = sync_do(Pid1, owners),
    ?line [_] = sync_do(Pid2, owners),
    ?line 0 = sync_do(Pid1, users),
    ?line 0 = sync_do(Pid2, users),
    ?line sync_do(Pid1, terminate),
    ?line [_] = sync_do(Pid2, owners),
    ?line 0 = sync_do(Pid2, users),
    ?line sync_do(Pid2, terminate),    
    ?line {error, no_such_log} = disk_log:info(n),

    %% Users terminate (no link...).
    ?line Pid3 = spawn_link(?MODULE, lserv, [n]),
    ?line Pid4 = spawn_link(?MODULE, lserv, [n]),
    ?line {[{_, {ok, n}}], []} = 
	sync_do(Pid3, {dist_open, File, none, node()}),
    ?line {[{_, {ok, n}}], []} = 
	sync_do(Pid4, {dist_open, File1, none, Node}),
    ?line [] = sync_do(Pid3, owners),
    ?line [] = sync_do(Pid4, owners),
    ?line 1 = sync_do(Pid3, users),
    ?line 1 = sync_do(Pid4, users),
    ?line sync_do(Pid3, terminate),
    ?line [] = sync_do(Pid4, owners),
    ?line 1 = sync_do(Pid4, users),
    ?line sync_do(Pid4, terminate),    
    ?line ok = disk_log:close(n), % closing all nodes
    ?line {error, no_such_log} = disk_log:info(n),
    
    ?line del(File, No),
    ?line del(File1, No),
    ?line stop_node(Node),
    ok.

dist_accessible(suite) -> [];
dist_accessible(doc) -> 
    ["Accessible logs on nodes"];
dist_accessible(Conf) when is_list(Conf) ->
    ?line PrivDir = ?privdir(Conf),

    ?line true = is_alive(),

    ?line F1 = filename:join(PrivDir, "a.LOG"),
    ?line file:delete(F1),
    ?line F2 = filename:join(PrivDir, "b.LOG"),
    ?line file:delete(F2),
    ?line F3 = filename:join(PrivDir, "c.LOG"),
    ?line file:delete(F3),
    ?line F4 = filename:join(PrivDir, "d.LOG"),
    ?line file:delete(F1),
    ?line F5 = filename:join(PrivDir, "e.LOG"),
    ?line file:delete(F2),
    ?line F6 = filename:join(PrivDir, "f.LOG"),
    ?line file:delete(F3),

    ?line {[],[]} = disk_log:accessible_logs(),
    ?line {ok, a} = disk_log:open([{name, a}, {type, halt}, {file, F1}]),
    ?line {[a],[]} = disk_log:accessible_logs(),
    ?line {ok, b} = disk_log:open([{name, b}, {type, halt}, {file, F2}]),
    ?line {[a,b],[]} = disk_log:accessible_logs(),
    ?line {ok, c} = disk_log:open([{name, c}, {type, halt}, {file, F3}]),
    ?line {[a,b,c],[]} = disk_log:accessible_logs(),

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node} = start_node(disk_log, "-pa " ++ PA),
    ?line wait_for_ready_net(),

    ?line {[_],[]} = disk_log:open([{name, a}, {file, F4}, {type, halt},
				    {distributed, [Node]}]),
    ?line {[a,b,c],[]} = disk_log:accessible_logs(),
    ?line {[],[a]} = rpc:call(Node, disk_log, accessible_logs, []),
    ?line {[_],[]} = disk_log:open([{name, b}, {file, F5}, {type, halt},
				    {distributed, [Node]}]),
    ?line {[],[a,b]} = rpc:call(Node, disk_log, accessible_logs, []),
    ?line {[_],[]} = disk_log:open([{name, c}, {file, F6}, {type, halt},
				    {distributed, [Node]}]),
    ?line {[],[a,b,c]} = rpc:call(Node, disk_log, accessible_logs, []),
    ?line {[a,b,c],[]} = disk_log:accessible_logs(),
    ?line ok = disk_log:close(a),
    ?line {[b,c],[a]} = disk_log:accessible_logs(),
    ?line ok = disk_log:close(b),
    ?line {[c],[a,b]} = disk_log:accessible_logs(),
    ?line ok = disk_log:close(b),
    ?line {[c],[a]} = disk_log:accessible_logs(),
    ?line {[],[a,c]} = rpc:call(Node, disk_log, accessible_logs, []),
    ?line ok = disk_log:close(c),
    ?line {[],[a,c]} = disk_log:accessible_logs(),
    ?line ok = disk_log:close(c),
    ?line {[],[a]} = disk_log:accessible_logs(),
    ?line {[],[a]} = rpc:call(Node, disk_log, accessible_logs, []),
    ?line ok = disk_log:close(a),
    ?line {[],[]} = disk_log:accessible_logs(),
    ?line {[],[]} = rpc:call(Node, disk_log, accessible_logs, []),

    ?line file:delete(F1),
    ?line file:delete(F2),
    ?line file:delete(F3),
    ?line file:delete(F4),
    ?line file:delete(F5),
    ?line file:delete(F6),

    ?line stop_node(Node),
    ok.

dist_deadlock(suite) -> [];
dist_deadlock(doc) -> 
    ["OTP-4405. Deadlock between two nodes could happen."];
dist_deadlock(Conf) when is_list(Conf) ->
    ?line PrivDir = ?privdir(Conf),

    ?line true = is_alive(),

    ?line F1 = filename:join(PrivDir, "a.LOG"),
    ?line file:delete(F1),
    ?line F2 = filename:join(PrivDir, "b.LOG"),
    ?line file:delete(F2),

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node1} = start_node(disk_log_node1, "-pa " ++ PA),
    ?line {ok, Node2} = start_node(disk_log_node2, "-pa " ++ PA),
    ?line wait_for_ready_net(),

    Self = self(),
    Fun1 = fun() -> dist_dl(Node2, a, F1, Self) end,
    Fun2 = fun() -> dist_dl(Node1, b, F2, Self) end,
    P1 = spawn(Node1, Fun1),
    P2 = spawn(Node2, Fun2),
    receive {P1, a} -> ok end,
    receive {P2, b} -> ok end,

    ?line stop_node(Node1),
    ?line stop_node(Node2),

    ?line file:delete(F1),
    ?line file:delete(F2),
    ok.

dist_dl(Node, Name, File, Pid) ->
    {[{Node,{ok,Log}}], []} =
	disk_log:open([{name,Name},{file,File},{distributed,[Node]}]),
    timer:sleep(50), % give the nodes chance to exchange pg2 information
    ok = disk_log:close(Log),
    Pid ! {self(), Name},
    ok.

dist_open2(suite) -> [];
dist_open2(doc) -> 
    ["OTP-4480. Opening several logs simultaneously."];
dist_open2(Conf) when is_list(Conf) ->
    ?line true = is_alive(),
    ?line {ok, _Pg2} = pg2:start(),

    dist_open2_1(Conf, 0),
    dist_open2_1(Conf, 100),

    dist_open2_2(Conf, 0),
    dist_open2_2(Conf, 100),

    PrivDir = ?privdir(Conf),
    Log = n,

    %% Open a log three times (very fast). Two of the opening
    %% processes will be put on hold (pending). The first one failes
    %% to open the log. The second one succeeds, and the third one is
    %% attached.
    P0 = pps(),
    ?line File0 = "n.LOG",
    ?line File = filename:join(PrivDir, File0),
    ?line make_file(PrivDir, File0, 8),

    Parent = self(),
    F1 = fun() -> R = disk_log:open([{name, Log}, {file, File},
                                     {type, halt}, {format,internal},
                                     {distributed, [node()]}]),
                  Parent ! {self(), R}
         end,
    F2 = fun() -> R = disk_log:open([{name, Log}, {file, File},
                                     {type, halt}, {format,external},
                                     {distributed, [node()]}]),
                  Parent ! {self(), R},
                  timer:sleep(300)
         end,
    ?line Pid1 = spawn(F1),
    timer:sleep(10),
    ?line Pid2 = spawn(F2),
    ?line Pid3 = spawn(F2),

    ?line receive {Pid1,R1} -> {[],[_]} = R1 end,
    ?line receive {Pid2,R2} -> {[_],[]} = R2 end,
    ?line receive {Pid3,R3} -> {[_],[]} = R3 end,

    timer:sleep(500),
    ?line file:delete(File),
    ?line true = (P0 == pps()),

    %% This time the first process has a naughty head_func. This test
    %% does not add very much. Perhaps it should be removed. However,
    %% a head_func like this is why it's necessary to have an separate
    %% process calling disk_log:internal_open: the server cannot wait
    %% for the reply, but the call must be monitored, and this is what
    %% is accomplished by having a proxy process.
    F3 = fun() ->
                 R = disk_log:open([{name,Log},{file,File},
                                    {format,internal},
                                    {head_func,{?MODULE,head_exit,[]}},
                                    {type,halt}, {linkto,none}]),
                 Parent ! {self(), R}
         end,
    F4 = fun() ->
                 R = disk_log:open([{name,Log},{file,File},
                                    {format,internal},
                                    {type,halt}]),
                 Parent ! {self(), R}
         end,
    ?line Pid4 = spawn(F3),
    timer:sleep(10),
    ?line Pid5 = spawn(F4),
    ?line Pid6 = spawn(F4),
    %% The timing is crucial here.
    ?line R = case receive {Pid4,R4} -> R4 end of
                  {error, no_such_log} ->
                      ?line R5 = receive {Pid5, R5a} -> R5a end,
                      ?line R6 = receive {Pid6, R6a} -> R6a end,
                      case {R5, R6} of
                          {{repaired, _, _, _}, {ok, Log}} -> ok;
                          {{ok, Log}, {repaired, _, _, _}} -> ok;
                          _ -> test_server_fail({bad_replies, R5, R6})
                      end,
                      ok;
                  {ok, Log} -> % uninteresting case
                      ?line receive {Pid5,_R5} -> ok end,
                      ?line receive {Pid6,_R6} -> ok end,
                      {comment, 
                       "Timing dependent test did not check anything."}
              end,

    timer:sleep(100),
    ?line {error, no_such_log} = disk_log:close(Log),
    file:delete(File),
    ?line true = (P0 == pps()),

    No = 2,
    Log2 = n2,
    File2 = filename:join(PrivDir, "b.LOG"),
    file:delete(File2),
    del(File, No),
    
    %% If a client takes a long time when writing the header, other
    %% processes should be able to attach to other log without having to
    %% wait.

    ?line {ok,Log} = 
        disk_log:open([{name,Log},{file,File},{type,wrap},{size,{100,No}}]),
    Pid = spawn(fun() -> 
                        receive {HeadPid, start} -> ok end,
                        {ok,Log2} = disk_log:open([{name,Log2},{file,File2},
                                                   {type,halt}]),
                        HeadPid ! {self(), done}
                end),
    HeadFunc = {?MODULE, slow_header, [Pid]},
    ?line ok = disk_log:change_header(Log, {head_func, HeadFunc}),
    ?line ok = disk_log:inc_wrap_file(Log), % header is written

    timer:sleep(100),
    ?line ok = disk_log:close(Log),

    file:delete(File2),
    del(File, No),
    ?line true = (P0 == pps()),

    R.
    
dist_open2_1(Conf, Delay) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "n.LOG"),
    Log = n,

    A0 = [{name,Log},{file,File},{type,halt}],
    ?line create_opened_log(File, A0),
    P0 = pps(),

    Log2 = log2,
    File2 = "log2.LOG",
    ?line file:delete(File2),
    ?line {ok,Log2} = disk_log:open([{name,Log2},{file,File2},{type,halt}]),

    Parent = self(),
    F = fun() -> 
                R = disk_log:open(A0),
                timer:sleep(Delay),
                Parent ! {self(), R}
        end,
    ?line Pid1 = spawn(F),
    timer:sleep(10),
    ?line Pid2 = spawn(F),
    ?line Pid3 = spawn(F),
    ?line {error, no_such_log} = disk_log:log(Log, term), % is repairing now
    ?line 0 = qlen(),

    %% The file is already open, so this will not take long.
    ?line {ok,Log2} = disk_log:open([{name,Log2},{file,File2},{type,halt}]),
    ?line 0 = qlen(), % still repairing
    ?line ok = disk_log:close(Log2),
    ?line {error, no_such_log} = disk_log:close(Log2),
    ?line file:delete(File2),

    ?line receive {Pid1,R1} -> {repaired,_,_,_} = R1 end,
    ?line receive {Pid2,R2} -> {ok,_} = R2 end,
    ?line receive {Pid3,R3} -> {ok,_} = R3 end,
    timer:sleep(500),
    ?line {error, no_such_log} = disk_log:info(Log),

    file:delete(File),
    ?line true = (P0 == pps()),

    ok.

dist_open2_2(Conf, Delay) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "n.LOG"),
    Log = n,

    ?line PA = filename:dirname(code:which(?MODULE)),
    ?line {ok, Node1} = start_node(disk_log_node2, "-pa " ++ PA),
    ?line wait_for_ready_net(),
    P0 = pps(),

    A0 = [{name,Log},{file,File},{type,halt}],
    ?line create_opened_log(File, A0),

    Log2 = log2,
    File2 = "log2.LOG",
    ?line file:delete(File2),
    ?line {[{Node1,{ok,Log2}}],[]} = 
        disk_log:open([{name,Log2},{file,File2},{type,halt},
                       {distributed,[Node1]}]),

    Parent = self(),
    F = fun() ->
                %% It would be nice to slow down the repair. head_func
                %% cannot be used since it is not called when repairing.
                R = disk_log:open([{distributed,[Node1]} | A0]),
                timer:sleep(Delay),
                Parent ! {self(), R}
        end,
    %% And {priority, ...} probably has no effect either.
    ?line Pid1 = spawn_opt(F, [{priority, low}]),
    % timer:sleep(1), % no guarantee that Pid1 will return {repaired, ...}
    ?line Pid2 = spawn_opt(F, [{priority, low}]),
    ?line {error, no_such_log} = 
        disk_log:log(Log, term), % maybe repairing now
    ?line 0 = qlen(),

    %% The file is already open, so this will not take long.
    ?line {[{Node1,{ok,Log2}}],[]} = 
        disk_log:open([{name,Log2},{file,File2},{type,halt},
                       {distributed,[Node1]}]),
    ?line 0 = qlen(), % probably still repairing
    ?line ok = disk_log:close(Log2),
    ?line file:delete(File2),

    ?line receive {Pid1,R1} -> R1 end,
    ?line receive {Pid2,R2} -> R2 end,
    ?line case {R1, R2} of
	      {{[{Node1,{repaired,_,_,_}}],[]}, 
	       {[{Node1,{ok,Log}}],[]}}         -> ok;
	      {{[{Node1,{ok,Log}}],[]}, 
	       {[{Node1,{repaired,_,_,_}}],[]}} -> ok
	  end,

    ?line true = (P0 == pps()),
    ?line stop_node(Node1),
    file:delete(File),
    ok.

head_exit() ->
    process_flag(trap_exit, false), % Don't do like this!
    spawn_link(fun() -> exit(helfel) end),
    {ok,"123"}.

slow_header(Pid) ->
    Pid ! {self(), start},
    receive {Pid, done} -> ok end,
    {ok, <<>>}.

create_opened_log(File, Args) ->
    Log = n,
    file:delete(File),
    {ok, Log} = disk_log:open(Args),
    log_terms(Log, 400000),
    ok = disk_log:close(Log),
    mark(File, ?OPENED),
    ok.

log_terms(_Log, 0) ->
    ok;
log_terms(Log, N) when N > 100 ->
    Terms = [{term,I} || I <- lists:seq(N-99, N)],
    ok = disk_log:log_terms(Log, Terms),
    log_terms(Log, N-100);
log_terms(Log, N) ->
    ok = disk_log:log(Log, {term, N}),
    log_terms(Log, N-1).

other_groups(suite) -> [];
other_groups(doc) -> 
    ["OTP-5810. Cope with pg2 groups that are not disk logs."];
other_groups(Conf) when is_list(Conf) ->
    ?line true = is_alive(),
    ?line PrivDir = ?privdir(Conf),

    ?line File = filename:join(PrivDir, "n.LOG"),
    ?line file:delete(File),

    ?line {[],[]} = disk_log:accessible_logs(),
    ?line {[_],[]} = disk_log:open([{name, n}, {file, File}, {type, halt},
				    {distributed, [node()]}]),
    ?line {[],[n]} = disk_log:accessible_logs(),
    Group = grupp,
    ?line pg2:create(Group),
    ?line ok = pg2:join(Group, self()),
    ?line {[],[n]} = disk_log:accessible_logs(),
    ?line [_] = 
        lists:filter(fun(P) -> disk_log:pid2name(P) =/= undefined end, 
                     erlang:processes()),
    ?line pg2:delete(Group),
    ?line {[],[n]} = disk_log:accessible_logs(),
    ?line ok = disk_log:close(n),
    ?line {[],[]} = disk_log:accessible_logs(),
    ?line file:delete(File),

    ok.

-define(MAX, 16384). % MAX in disk_log_1.erl
evil(suite) -> [];
evil(doc) -> ["Evil cases such as closed file descriptor port."];
evil(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "n.LOG"),
    Log = n,

    %% Not a very thorough test.

    ?line ok = setup_evil_filled_cache_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:log(Log, apa),
    ?line ok = disk_log:close(Log),

    ?line ok = setup_evil_filled_cache_halt(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:truncate(Log, apa),
    ?line ok = stop_evil(Log),

    %% White box test. 
    file:delete(File),
    ?line Ports0 = erlang:ports(),
    ?line {ok, Log} = disk_log:open([{name,Log},{file,File},{type,halt},
                                     {size,?MAX+50},{format,external}]),
    ?line [Fd] = erlang:ports() -- Ports0,
    ?line {B,_} = x_mk_bytes(30),
    ?line ok = disk_log:blog(Log, <<0:(?MAX+1)/unit:8>>),
    ?line exit(Fd, kill),
    ?line {error, {file_error,_,einval}} = disk_log:blog_terms(Log, [B,B]),
    ?line ok= disk_log:close(Log),
    file:delete(File),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:close(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:log(Log, apa),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_halt(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:log(Log, apa),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:reopen(Log, apa),
    ?line {error, {file_error,_,einval}} = disk_log:reopen(Log, apa),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:reopen(Log, apa),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:inc_wrap_file(Log),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:chunk(Log, start),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:truncate(Log),
    ?line ok = stop_evil(Log),

    ?line ok = setup_evil_wrap(Log, Dir),
    ?line {error, {file_error,_,einval}} = disk_log:chunk_step(Log, start, 1),
    ?line ok = stop_evil(Log),

    io:format("messages: ~p~n", [erlang:process_info(self(), messages)]),
    del(File, 2),
    file:delete(File),
    ok.

setup_evil_wrap(Log, Dir) ->
    setup_evil(Log, [{type,wrap},{size,{100,2}}], Dir).

setup_evil_halt(Log, Dir) ->
    setup_evil(Log, [{type,halt},{size,10000}], Dir).

setup_evil(Log, Args, Dir) ->
    File = filename:join(Dir, lists:concat([Log, ".LOG"])),
    file:delete(File),
    del(File, 2),
    ok = disk_log:start(),
    Ports0 = erlang:ports(),
    {ok, Log} = disk_log:open([{name,Log},{file,File} | Args]),
    [Fd] = erlang:ports() -- Ports0,
    exit(Fd, kill),
    ok = disk_log:log_terms(n, [<<0:10/unit:8>>]),
    timer:sleep(2500), % TIMEOUT in disk_log_1.erl is 2000
    ok.

stop_evil(Log) ->
    {error, _} = disk_log:close(Log),
    ok.

setup_evil_filled_cache_wrap(Log, Dir) ->
    setup_evil_filled_cache(Log, [{type,wrap},{size,{?MAX,2}}], Dir).

setup_evil_filled_cache_halt(Log, Dir) ->
    setup_evil_filled_cache(Log, [{type,halt},{size,infinity}], Dir).

%% The cache is filled, and the file descriptor port gone.
setup_evil_filled_cache(Log, Args, Dir) ->
    File = filename:join(Dir, lists:concat([Log, ".LOG"])),
    file:delete(File),
    del(File, 2),
    ok = disk_log:start(),
    Ports0 = erlang:ports(),
    {ok, Log} = disk_log:open([{name,Log},{file,File} | Args]),
    [Fd] = erlang:ports() -- Ports0,
    ok = disk_log:log_terms(n, [<<0:?MAX/unit:8>>]),
    exit(Fd, kill),
    ok.

otp_6278(suite) -> [];
otp_6278(doc) -> ["OTP-6278. open/1 creates no status or crash report."];
otp_6278(Conf) when is_list(Conf) ->
    Dir = ?privdir(Conf),
    File = filename:join(Dir, "no_such_dir/no_such_file"),
    ?line error_logger:add_report_handler(?MODULE, self()),
    ?line {error, {file_error, _, _}} = 
        disk_log:open([{name,n},{file,File}]),
    receive
	{crash_report,_Pid,Report} ->
	    ?line io:format("Unexpected: ~p\n", [Report]),
	    ?line ?t:fail()
    after 1000 ->
            ok
    end,
    ?line error_logger:delete_report_handler(?MODULE).

mark(FileName, What) ->
    {ok,Fd} = file:open(FileName, [raw, binary, read, write]),
    {ok,_} = file:position(Fd, 4),
    ok = file:write(Fd, What),
    ok = file:close(Fd).

crash(File, Where) ->
    {ok, Fd} = file:open(File, [read,write]),
    file:position(Fd, Where),
    ok = file:write(Fd, [10]),
    ok = file:close(Fd).

unwritable(Fname) ->
    {ok, Info} = file:read_file_info(Fname),
    Mode = Info#file_info.mode - 8#00200,
    file:write_file_info(Fname, Info#file_info{mode = Mode}).

writable(Fname) ->
    {ok, Info} = file:read_file_info(Fname),
    Mode = Info#file_info.mode bor 8#00200,
    file:write_file_info(Fname, Info#file_info{mode = Mode}).

truncate(File, Where) ->
    {ok, Fd} = file:open(File, [read,write]),
    file:position(Fd, Where),
    ok = file:truncate(Fd),
    ok = file:close(Fd).

file_size(File) ->
    {ok, F} = file:read_file_info(File),
    F#file_info.size.

copy_wrap_log(FromName, N, FromDir, ToDir) ->
    copy_wrap_log(FromName, FromName, N, FromDir, ToDir).

copy_wrap_log(FromName, ToName, N, FromDir, ToDir) ->
    Fun = fun(E) ->
	     From = join(FromDir, io_lib:format("~s.~p", [FromName, E])),
	     To = join(ToDir, io_lib:format("~s.~p", [ToName, E])),
	     case file:read_file_info(From) of
                 {ok, _FileInfo} ->
                     copy_file(From, To);
                 _Else ->
                     ok
             end
	  end,
    Exts = [idx, siz | lists:seq(1, N)],
    lists:foreach(Fun, Exts).

-define(BUFSIZE, 8192).

copy_file(Src, Dest) ->
    % ?t:format("copying from ~p to ~p~n", [Src, Dest]),
    {ok, InFd} = file:open(Src, [raw, binary, read]),
    {ok, OutFd} = file:open(Dest, [raw, binary, write]),
    ok = copy_file1(InFd, OutFd),
    file:close(InFd),
    file:close(OutFd),
    ok = file:change_mode(Dest, 8#0666).

copy_file1(InFd, OutFd) ->
    case file:read(InFd, ?BUFSIZE) of
        {ok, Bin} ->
            ok = file:write(OutFd, Bin),
            copy_file1(InFd, OutFd);
        eof  ->
            ok
    end.


join(A, B) ->
    filename:nativename(filename:join(A, B)).

add_ext(Name, Ext) ->
    lists:concat([Name, ".", Ext]).

log(_Name, 0) ->
    ok;
log(Name, N) ->
    ok = disk_log:log(Name, "this is a logged message number " ++ 
                      integer_to_list(N)),
    log(Name, N-1).

format_error(E) ->
    lists:flatten(disk_log:format_error(E)).

pps() ->
    timer:sleep(100),
    {erlang:ports(), lists:filter(fun(P) -> erlang:is_process_alive(P) end,
				  processes())}.

qlen() ->
    {_, {_, N}} = lists:keysearch(message_queue_len, 1, process_info(self())),
    N.

owners(Log) ->
%%     io:format("owners ~p~n", [info(Log, owners, -1)]),
    info(Log, owners, -1).
users(Log) ->
%%     io:format("users ~p~n", [info(Log, users, -1)]),
    info(Log, users, -1).
status(Log) ->
%%     io:format("status ~p~n", [info(Log, status, -1)]),
    info(Log, status, -1).
distributed(Log) ->
%%     io:format("distributed ~p~n", [info(Log, distributed, -1)]),
    info(Log, distributed, -1).
no_items(Log) ->
%%     io:format("no_items ~p~n", [info(Log, no_items, -1)]),
    info(Log, no_items, -1).
no_written_items(Log) ->
%%     io:format("no_written_items ~p~n", [info(Log, no_written_items, -1)]),
    info(Log, no_written_items, -1).
sz(Log) -> 
%%     io:format("sz ~p~n", [info(Log, size, -1)]),
    info(Log, size, -1).
curb(Log) -> 
%%     io:format("curb ~p~n", [info(Log, no_current_bytes, -1)]),
    info(Log, no_current_bytes, -1).
curf(Log) -> 
%%     io:format("curf ~p~n", [info(Log, current_file, -1)]),
    info(Log, current_file, -1).
cur_cnt(Log) -> 
%%     io:format("cur_cnt ~p~n", [info(Log, no_current_items, -1)]),
    info(Log, no_current_items, -1).
no_overflows(Log) ->
%%     io:format("no_overflows ~p~n", [info(Log, no_overflows, -1)]),
    info(Log, no_overflows, -1).

info(Log, What, Undef) ->
    case lists:keysearch(What, 1, disk_log:info(Log)) of
        {value, {What, Value}} -> Value;
        false -> Undef
    end.

rec(0, _) ->
     ok;
rec(N, Msg) ->
    receive
	Msg ->
	    rec(N-1, Msg)
    after 100 ->
	    test_server_fail({no_msg, N, Msg})
    end.

%% Copied from global_SUITE.erl.
-define(UNTIL(Seq), loop_until_true(fun() -> Seq end)).

loop_until_true(Fun) ->
    case Fun() of
	true ->
	    ok;
	_ ->
	    timer:sleep(1000),
	    loop_until_true(Fun)
    end.

wait_for_ready_net() ->
    Nodes = lists:sort([node() | nodes()]),
    ?UNTIL(begin
               lists:all(fun(N) -> Nodes =:= get_known(N) end, Nodes) and
               lists:all(fun(N) -> 
                                 LNs = rpc:call(N, erlang, nodes, []),
                                 Nodes =:= lists:sort([N | LNs])
                         end, Nodes)
           end).

get_known(Node) ->
    case catch gen_server:call({global_name_server,Node}, get_known) of
        {'EXIT', _} ->
            [list, without, nodenames];
        Known -> 
            lists:sort([Node | Known])
    end.

%% Copied from erl_distribution_SUITE.erl:
start_node(Name, Param) ->
    ?t:start_node(Name, slave, [{args, Param}]).

stop_node(Node) ->
    ?t:stop_node(Node).

%from(H, [H | T]) -> T;
%from(H, [_ | T]) -> from(H, T);
%from(_H, []) -> [].


%% Check for NFS cache size, this is called from init_per_testcase() and
%% makes different tests run depending on the size of the NFS cache on 
%% VxWorks. Possibly this could be adopted to Windows too, but we seldom use
%% NFS when testing on windows, so I can find better things to do.
%% The port program used simply reads the nfsCacheSize variable on the board.
%% If the board is configured without NFS, the port program will fail to load
%% and this will return 0, which may or may not be the wrong thing to do.

check_nfs(Config) ->
    case (catch check_cache(Config)) of
	N when is_integer(N) ->
	    N;
	_ ->
	    0
    end.

check_cache(Config) ->
    ?line Check = filename:join(?datadir(Config), "nfs_check"),
    ?line P = open_port({spawn, Check}, [{line,100}, eof]),
    ?line Size = receive
		     {P,{data,{eol,S}}} ->
			 list_to_integer(S)
		 after 1000 ->
			 erlang:display(got_timeout),
			 exit(timeout)
		 end,
    ?line receive
	      {P, eof} ->
		  ok
	  end,
    ?line P ! {self(), close},
    ?line receive
	      {P, closed} -> ok
	  end,
    Size.

skip_expand([]) ->
    [];
skip_expand([Case | T]) ->
    case (catch apply(?MODULE, Case, [suite])) of
	{'EXIT', _} ->
	    [Case | skip_expand(T)];
	[] ->
	    [Case | skip_expand(T)];
	Res ->
	    skip_expand(Res) ++ skip_expand(T)
    end.
	    
    
skip_list(Config) ->
    case check_nfs(Config) of
	0 ->
	    skip_expand(?SKIP_NO_CACHE);
	_ ->
	    skip_expand(?SKIP_LARGE_CACHE)
    end.

should_skip(Test,Config) ->
    case os:type() of
	vxworks ->
	    lists:member(Test, skip_list(Config));
	_ ->
	    false
    end.

%%-----------------------------------------------------------------
%% The error_logger handler used.
%% (Copied from stdlib/test/proc_lib_SUITE.erl.)
%%-----------------------------------------------------------------
init(Tester) ->
    {ok, Tester}.
    
handle_event({error_report, _GL, {Pid, crash_report, Report}}, Tester) ->
    Tester ! {crash_report, Pid, Report},
    {ok, Tester};
handle_event(_Event, State) ->
    {ok, State}.

handle_info(_, State) ->
    {ok, State}.

handle_call(_Query, State) -> {ok, {error, bad_query}, State}.

terminate(_Reason, State) ->
    State.