@@ -277,64 +277,81 @@ impl FilesystemStoreInner {
277277 let tmp_file_ext = format ! ( "{}.tmp" , self . tmp_file_counter. fetch_add( 1 , Ordering :: AcqRel ) ) ;
278278 tmp_file_path. set_extension ( tmp_file_ext) ;
279279
280- {
281- let mut tmp_file = fs :: File :: create ( & tmp_file_path ) ? ;
282- tmp_file. write_all ( & buf) ?;
283-
284- // If we need to preserve the original mtime (for updates), set it before fsync.
285- if let Some ( mtime) = mtime {
286- let times = fs:: FileTimes :: new ( ) . set_modified ( mtime) ;
287- tmp_file. set_times ( times) ?;
288- }
280+ let tmp_file_res = match fs :: File :: create ( & tmp_file_path ) {
281+ Ok ( mut tmp_file) => ( || -> lightning :: io :: Result < ( ) > {
282+ tmp_file. write_all ( & buf) ?;
283+
284+ // If we need to preserve the original mtime (for updates), set it before fsync.
285+ if let Some ( mtime) = mtime {
286+ let times = fs:: FileTimes :: new ( ) . set_modified ( mtime) ;
287+ tmp_file. set_times ( times) ?;
288+ }
289289
290- tmp_file. sync_all ( ) ?;
290+ tmp_file. sync_all ( ) ?;
291+ Ok ( ( ) )
292+ } ) ( ) ,
293+ Err ( e) => return Err ( e. into ( ) ) ,
294+ } ;
295+ if let Err ( e) = tmp_file_res {
296+ let _ = fs:: remove_file ( & tmp_file_path) ;
297+ return Err ( e) ;
291298 }
292299
293- self . execute_locked_write ( inner_lock_ref, dest_file_path. clone ( ) , version, || {
294- #[ cfg( not( target_os = "windows" ) ) ]
295- {
296- fs:: rename ( & tmp_file_path, & dest_file_path) ?;
297- let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( & parent_directory) ?;
298- dir_file. sync_all ( ) ?;
299- Ok ( ( ) )
300- }
300+ let mut tmp_file_needs_cleanup = true ;
301+ let write_res =
302+ self . execute_locked_write ( inner_lock_ref, dest_file_path. clone ( ) , version, || {
303+ #[ cfg( not( target_os = "windows" ) ) ]
304+ {
305+ fs:: rename ( & tmp_file_path, & dest_file_path) ?;
306+ tmp_file_needs_cleanup = false ;
307+ let dir_file = fs:: OpenOptions :: new ( ) . read ( true ) . open ( & parent_directory) ?;
308+ dir_file. sync_all ( ) ?;
309+ Ok ( ( ) )
310+ }
301311
302- #[ cfg( target_os = "windows" ) ]
303- {
304- let res = if dest_file_path. exists ( ) {
305- call ! ( unsafe {
306- windows_sys:: Win32 :: Storage :: FileSystem :: ReplaceFileW (
312+ #[ cfg( target_os = "windows" ) ]
313+ {
314+ let res = if dest_file_path. exists ( ) {
315+ call ! ( unsafe {
316+ windows_sys:: Win32 :: Storage :: FileSystem :: ReplaceFileW (
307317 path_to_windows_str( & dest_file_path) . as_ptr( ) ,
308318 path_to_windows_str( & tmp_file_path) . as_ptr( ) ,
309319 std:: ptr:: null( ) ,
310320 windows_sys:: Win32 :: Storage :: FileSystem :: REPLACEFILE_IGNORE_MERGE_ERRORS ,
311321 std:: ptr:: null_mut( ) as * const core:: ffi:: c_void,
312322 std:: ptr:: null_mut( ) as * const core:: ffi:: c_void,
313323 )
314- } )
315- } else {
316- call ! ( unsafe {
317- windows_sys:: Win32 :: Storage :: FileSystem :: MoveFileExW (
324+ } )
325+ } else {
326+ call ! ( unsafe {
327+ windows_sys:: Win32 :: Storage :: FileSystem :: MoveFileExW (
318328 path_to_windows_str( & tmp_file_path) . as_ptr( ) ,
319329 path_to_windows_str( & dest_file_path) . as_ptr( ) ,
320330 windows_sys:: Win32 :: Storage :: FileSystem :: MOVEFILE_WRITE_THROUGH
321331 | windows_sys:: Win32 :: Storage :: FileSystem :: MOVEFILE_REPLACE_EXISTING ,
322332 )
323- } )
324- } ;
325-
326- match res {
327- Ok ( ( ) ) => {
328- // We fsync the dest file in hopes this will also flush the metadata to disk.
329- let dest_file =
330- fs:: OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( & dest_file_path) ?;
331- dest_file. sync_all ( ) ?;
332- Ok ( ( ) )
333- } ,
334- Err ( e) => Err ( e. into ( ) ) ,
333+ } )
334+ } ;
335+
336+ match res {
337+ Ok ( ( ) ) => {
338+ tmp_file_needs_cleanup = false ;
339+ // We fsync the dest file in hopes this will also flush the metadata to disk.
340+ let dest_file = fs:: OpenOptions :: new ( )
341+ . read ( true )
342+ . write ( true )
343+ . open ( & dest_file_path) ?;
344+ dest_file. sync_all ( ) ?;
345+ Ok ( ( ) )
346+ } ,
347+ Err ( e) => Err ( e. into ( ) ) ,
348+ }
335349 }
336- }
337- } )
350+ } ) ;
351+ if tmp_file_needs_cleanup {
352+ let _ = fs:: remove_file ( & tmp_file_path) ;
353+ }
354+ write_res
338355 }
339356
340357 fn remove_version (
0 commit comments