@@ -806,30 +806,22 @@ impl Config {
806806 . expect ( "every module should have a filename" ) ;
807807 let output_path = target. join ( file_name) ;
808808
809- let previous_content = fs:: read ( & output_path) ;
810-
811- if previous_content
812- . map ( |previous_content| previous_content == content. as_bytes ( ) )
813- . unwrap_or ( false )
814- {
815- trace ! ( "unchanged: {:?}" , file_name) ;
816- } else {
817- trace ! ( "writing: {:?}" , file_name) ;
818- fs:: write ( output_path, content) ?;
819- }
809+ write_file_if_changed ( & output_path, content. as_bytes ( ) ) ?;
820810 }
821811
822812 if let Some ( ref include_file) = self . include_file {
823- trace ! ( "Writing include file: {:?}" , target. join( include_file) ) ;
824- let mut file = fs:: File :: create ( target. join ( include_file) ) ?;
825- self . write_line ( & mut file, 0 , "// This file is @generated by prost-build." ) ?;
813+ let path = target. join ( include_file) ;
814+ trace ! ( "Writing include file: {:?}" , path) ;
815+ let mut buffer = Vec :: new ( ) ;
816+ self . write_line ( & mut buffer, 0 , "// This file is @generated by prost-build." ) ?;
826817 self . write_includes (
827818 modules. keys ( ) . collect ( ) ,
828- & mut file ,
819+ & mut buffer ,
829820 if target_is_env { None } else { Some ( & target) } ,
830821 & file_names,
831822 ) ?;
832- file. flush ( ) ?;
823+
824+ write_file_if_changed ( & path, & buffer) ?;
833825 }
834826
835827 Ok ( ( ) )
@@ -1072,6 +1064,26 @@ impl Config {
10721064 }
10731065}
10741066
1067+ /// Write a slice as the entire contents of a file.
1068+ ///
1069+ /// This function will create a file if it does not exist,
1070+ /// and will entirely replace its contents if it does. When
1071+ /// the contents is already correct, it doesn't touch to the file.
1072+ fn write_file_if_changed ( path : & Path , content : & [ u8 ] ) -> std:: io:: Result < ( ) > {
1073+ let previous_content = fs:: read ( path) ;
1074+
1075+ if previous_content
1076+ . map ( |previous_content| previous_content == content)
1077+ . unwrap_or ( false )
1078+ {
1079+ trace ! ( "unchanged: {:?}" , path) ;
1080+ Ok ( ( ) )
1081+ } else {
1082+ trace ! ( "writing: {:?}" , path) ;
1083+ fs:: write ( path, content)
1084+ }
1085+ }
1086+
10751087impl default:: Default for Config {
10761088 fn default ( ) -> Config {
10771089 Config {
0 commit comments