@@ -22,7 +22,7 @@ use log::{debug, error, info};
22
22
use serde:: { Deserialize , Serialize } ;
23
23
use std:: ffi:: OsString ;
24
24
use std:: fs;
25
- use std:: io:: { Read , Write } ;
25
+ use std:: io:: { BufWriter , Read , Write } ;
26
26
use std:: path:: { Path , PathBuf } ;
27
27
use std:: process:: Command ;
28
28
@@ -904,12 +904,14 @@ fn write_pkg_metadata<P>(pkg_path: P, meta: &PackageMetadata) -> MainResult<()>
904
904
where
905
905
P : AsRef < Path > ,
906
906
{
907
- let meta_path = get_pkg_metadata_path ( pkg_path) ;
907
+ let meta_path = get_pkg_metadata_path ( & pkg_path) ;
908
908
debug ! ( "meta_path: {:?}" , meta_path) ;
909
- let mut meta_file = fs:: File :: create ( & meta_path) ?;
910
- let meta_str = serde_json:: to_string ( meta) . map_err ( |err| err. to_string ( ) ) ?;
911
- write ! ( & mut meta_file, "{}" , meta_str) ?;
912
- meta_file. flush ( ) ?;
909
+ let mut temp_file = tempfile:: NamedTempFile :: new_in ( & pkg_path) ?;
910
+ serde_json:: to_writer ( BufWriter :: new ( & temp_file) , meta) . map_err ( |err| err. to_string ( ) ) ?;
911
+ temp_file. flush ( ) ?;
912
+ temp_file
913
+ . persist ( & meta_path)
914
+ . map_err ( |err| err. to_string ( ) ) ?;
913
915
Ok ( ( ) )
914
916
}
915
917
@@ -1141,9 +1143,14 @@ where
1141
1143
}
1142
1144
1143
1145
debug ! ( ".. hashes differ; new_hash: {:?}" , new_hash) ;
1144
- let mut file = fs:: File :: create ( path) ?;
1145
- write ! ( & mut file, "{}" , content) ?;
1146
- file. flush ( ) ?;
1146
+ let dir = path
1147
+ . as_ref ( )
1148
+ . parent ( )
1149
+ . ok_or ( "The given path should be a file" ) ?;
1150
+ let mut temp_file = tempfile:: NamedTempFile :: new_in ( dir) ?;
1151
+ temp_file. write_all ( content. as_bytes ( ) ) ?;
1152
+ temp_file. flush ( ) ?;
1153
+ temp_file. persist ( path) . map_err ( |e| e. to_string ( ) ) ?;
1147
1154
Ok ( FileOverwrite :: Changed { new_hash } )
1148
1155
}
1149
1156
0 commit comments