Skip to content

Commit 3134229

Browse files
committed
proper management of magic number
1 parent 2958e6c commit 3134229

File tree

4 files changed

+74
-70
lines changed

4 files changed

+74
-70
lines changed

jscomp/bin/bsb.ml

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7803,10 +7803,9 @@ type dep_info = {
78037803

78047804
type check_result =
78057805
| Good
7806-
| Bsb_file_not_exist (** We assume that it is a clean repo *)
7807-
| Bsb_version_mismatch
7806+
| Bsb_file_not_exist (** We assume that it is a clean repo *)
78087807
| Bsb_source_directory_changed
7809-
| Bsc_version_mismatch
7808+
| Bsb_bsc_version_mismatch
78107809
| Bsb_forced
78117810
| Other of string
78127811

@@ -7856,45 +7855,36 @@ type t =
78567855
}
78577856

78587857

7859-
let magic_number = "BS_DEP_INFOS_20161116"
7860-
let bsb_version = "20160121+dev"
7858+
let magic_number = "BS_DEP_INFOS_20170209"
7859+
let bsb_version = "20170209+dev"
7860+
(* TODO: for such small data structure, maybe text format is better *)
78617861

78627862
let write (fname : string) (x : t) =
78637863
let oc = open_out_bin fname in
78647864
output_string oc magic_number ;
78657865
output_value oc x ;
78667866
close_out oc
78677867

7868-
let read (fname : string) : t =
7869-
let ic = open_in_bin fname in (* Windows binary mode*)
7870-
let buffer = really_input_string ic (String.length magic_number) in
7871-
assert (buffer = magic_number);
7872-
let res : t = input_value ic in
7873-
close_in ic ;
7874-
res
7875-
78767868

78777869

78787870

78797871

78807872
type check_result =
78817873
| Good
78827874
| Bsb_file_not_exist (** We assume that it is a clean repo *)
7883-
| Bsb_version_mismatch
78847875
| Bsb_source_directory_changed
7885-
| Bsc_version_mismatch
7876+
| Bsb_bsc_version_mismatch
78867877
| Bsb_forced
78877878
| Other of string
78887879

78897880
let to_str (check_resoult : check_result) =
78907881
match check_resoult with
78917882
| Good -> Ext_string.empty
78927883
| Bsb_file_not_exist -> "File not found"
7893-
| Bsb_version_mismatch -> "Bsb version mismatch"
78947884
| Bsb_source_directory_changed ->
78957885
"Bsb source directory changed"
7896-
| Bsc_version_mismatch ->
7897-
"Bsc version mismatch"
7886+
| Bsb_bsc_version_mismatch ->
7887+
"bsc or bsb version mismatch"
78987888
| Bsb_forced ->
78997889
"Bsb forced rebuild "
79007890
| Other s ->
@@ -7910,33 +7900,46 @@ let rec check_aux xs i finish =
79107900
check_aux xs (i + 1 ) finish
79117901
else Other current_file
79127902

7913-
7903+
7904+
let read (fname : string) cont =
7905+
match open_in_bin fname with (* Windows binary mode*)
7906+
| ic ->
7907+
let buffer = really_input_string ic (String.length magic_number) in
7908+
if (buffer <> magic_number) then Bsb_bsc_version_mismatch
7909+
else
7910+
let res : t = input_value ic in
7911+
close_in ic ;
7912+
cont res
7913+
| exception _ -> Bsb_file_not_exist
7914+
7915+
79147916
(** check time stamp for all files
79157917
TODO: those checks system call can be saved later
79167918
Return a reason
79177919
Even forced, we still need walk through a little
79187920
bit in case we found a different version of compiler
79197921
*)
79207922
let check ~cwd forced file =
7921-
try
7922-
let {
7923-
file_stamps = xs; source_directory; bsb_version = old_version;
7924-
bsc_version
7925-
} = read file in
7926-
if old_version <> bsb_version then Bsb_version_mismatch else
7923+
read file begin function {
7924+
file_stamps = xs; source_directory; bsb_version = old_version;
7925+
bsc_version
7926+
} ->
7927+
if old_version <> bsb_version then Bsb_bsc_version_mismatch else
79277928
if cwd <> source_directory then Bsb_source_directory_changed else
7928-
if bsc_version <> Bs_version.version then Bsc_version_mismatch else
7929+
if bsc_version <> Bs_version.version then Bsb_bsc_version_mismatch else
79297930
if forced then Bsb_forced (* No need walk through *)
79307931
else
7931-
check_aux xs 0 (Array.length xs)
7932-
with _ -> Bsb_file_not_exist
7932+
try
7933+
check_aux xs 0 (Array.length xs)
7934+
with _ -> Bsb_file_not_exist
7935+
end
79337936

79347937
let store ~cwd name file_stamps =
79357938
write name
7936-
{ file_stamps ;
7937-
source_directory = cwd ;
7938-
bsb_version ;
7939-
bsc_version = Bs_version.version }
7939+
{ file_stamps ;
7940+
source_directory = cwd ;
7941+
bsb_version ;
7942+
bsc_version = Bs_version.version }
79407943

79417944
end
79427945
module Bsb_file : sig
@@ -9048,14 +9051,13 @@ let regenerate_ninja cwd bsc_dir forced =
90489051
begin match reason with
90499052
| Good -> None (* Fast path *)
90509053
| Bsb_forced
9051-
| Bsc_version_mismatch
9054+
| Bsb_bsc_version_mismatch
90529055
| Bsb_file_not_exist
9053-
| Bsb_version_mismatch
90549056
| Bsb_source_directory_changed
90559057
| Other _ ->
90569058
print_string "Regenerating build spec : ";
90579059
print_endline (Bsb_dep_infos.to_str reason) ;
9058-
if reason = Bsc_version_mismatch then begin
9060+
if reason = Bsb_bsc_version_mismatch then begin
90599061
print_endline "Also clean current repo due to we have detected a different compiler";
90609062
clean_self ();
90619063
end ;

jscomp/bsb/bsb_dep_infos.ml

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,36 @@ type t =
3535
}
3636

3737

38-
let magic_number = "BS_DEP_INFOS_20161116"
39-
let bsb_version = "20160121+dev"
38+
let magic_number = "BS_DEP_INFOS_20170209"
39+
let bsb_version = "20170209+dev"
40+
(* TODO: for such small data structure, maybe text format is better *)
4041

4142
let write (fname : string) (x : t) =
4243
let oc = open_out_bin fname in
4344
output_string oc magic_number ;
4445
output_value oc x ;
4546
close_out oc
4647

47-
let read (fname : string) : t =
48-
let ic = open_in_bin fname in (* Windows binary mode*)
49-
let buffer = really_input_string ic (String.length magic_number) in
50-
assert (buffer = magic_number);
51-
let res : t = input_value ic in
52-
close_in ic ;
53-
res
54-
5548

5649

5750

5851

5952
type check_result =
6053
| Good
6154
| Bsb_file_not_exist (** We assume that it is a clean repo *)
62-
| Bsb_version_mismatch
6355
| Bsb_source_directory_changed
64-
| Bsc_version_mismatch
56+
| Bsb_bsc_version_mismatch
6557
| Bsb_forced
6658
| Other of string
6759

6860
let to_str (check_resoult : check_result) =
6961
match check_resoult with
7062
| Good -> Ext_string.empty
7163
| Bsb_file_not_exist -> "File not found"
72-
| Bsb_version_mismatch -> "Bsb version mismatch"
7364
| Bsb_source_directory_changed ->
7465
"Bsb source directory changed"
75-
| Bsc_version_mismatch ->
76-
"Bsc version mismatch"
66+
| Bsb_bsc_version_mismatch ->
67+
"bsc or bsb version mismatch"
7768
| Bsb_forced ->
7869
"Bsb forced rebuild "
7970
| Other s ->
@@ -89,30 +80,43 @@ let rec check_aux xs i finish =
8980
check_aux xs (i + 1 ) finish
9081
else Other current_file
9182

92-
83+
84+
let read (fname : string) cont =
85+
match open_in_bin fname with (* Windows binary mode*)
86+
| ic ->
87+
let buffer = really_input_string ic (String.length magic_number) in
88+
if (buffer <> magic_number) then Bsb_bsc_version_mismatch
89+
else
90+
let res : t = input_value ic in
91+
close_in ic ;
92+
cont res
93+
| exception _ -> Bsb_file_not_exist
94+
95+
9396
(** check time stamp for all files
9497
TODO: those checks system call can be saved later
9598
Return a reason
9699
Even forced, we still need walk through a little
97100
bit in case we found a different version of compiler
98101
*)
99102
let check ~cwd forced file =
100-
try
101-
let {
102-
file_stamps = xs; source_directory; bsb_version = old_version;
103-
bsc_version
104-
} = read file in
105-
if old_version <> bsb_version then Bsb_version_mismatch else
103+
read file begin function {
104+
file_stamps = xs; source_directory; bsb_version = old_version;
105+
bsc_version
106+
} ->
107+
if old_version <> bsb_version then Bsb_bsc_version_mismatch else
106108
if cwd <> source_directory then Bsb_source_directory_changed else
107-
if bsc_version <> Bs_version.version then Bsc_version_mismatch else
109+
if bsc_version <> Bs_version.version then Bsb_bsc_version_mismatch else
108110
if forced then Bsb_forced (* No need walk through *)
109111
else
110-
check_aux xs 0 (Array.length xs)
111-
with _ -> Bsb_file_not_exist
112+
try
113+
check_aux xs 0 (Array.length xs)
114+
with _ -> Bsb_file_not_exist
115+
end
112116

113117
let store ~cwd name file_stamps =
114118
write name
115-
{ file_stamps ;
116-
source_directory = cwd ;
117-
bsb_version ;
118-
bsc_version = Bs_version.version }
119+
{ file_stamps ;
120+
source_directory = cwd ;
121+
bsb_version ;
122+
bsc_version = Bs_version.version }

jscomp/bsb/bsb_dep_infos.mli

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ type dep_info = {
4545

4646
type check_result =
4747
| Good
48-
| Bsb_file_not_exist (** We assume that it is a clean repo *)
49-
| Bsb_version_mismatch
48+
| Bsb_file_not_exist (** We assume that it is a clean repo *)
5049
| Bsb_source_directory_changed
51-
| Bsc_version_mismatch
50+
| Bsb_bsc_version_mismatch
5251
| Bsb_forced
5352
| Other of string
5453

jscomp/bsb/bsb_main.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,13 @@ let regenerate_ninja cwd bsc_dir forced =
130130
begin match reason with
131131
| Good -> None (* Fast path *)
132132
| Bsb_forced
133-
| Bsc_version_mismatch
133+
| Bsb_bsc_version_mismatch
134134
| Bsb_file_not_exist
135-
| Bsb_version_mismatch
136135
| Bsb_source_directory_changed
137136
| Other _ ->
138137
print_string "Regenerating build spec : ";
139138
print_endline (Bsb_dep_infos.to_str reason) ;
140-
if reason = Bsc_version_mismatch then begin
139+
if reason = Bsb_bsc_version_mismatch then begin
141140
print_endline "Also clean current repo due to we have detected a different compiler";
142141
clean_self ();
143142
end ;

0 commit comments

Comments
 (0)