@@ -582,36 +582,39 @@ def _write_files_cache(self, files):
582582 discard_after = min (newest_cmtime , start_backup_time )
583583 ttl = int (os .environ .get ("BORG_FILES_CACHE_TTL" , 2 ))
584584 files_cache_logger .debug ("FILES-CACHE-SAVE: starting..." )
585- # TODO: use something like SaveFile here, but that didn't work due to SyncFile missing .seek().
586- with IntegrityCheckedFile (path = str (self .path / self .files_cache_name ()), write = True ) as fd :
587- entries = 0
588- age_discarded = 0
589- race_discarded = 0
590- for path_hash , entry in files .items ():
591- entry = self .decompress_entry (entry )
592- if entry .age == 0 : # current entries
593- if max (timestamp_to_int (entry .ctime ), timestamp_to_int (entry .mtime )) < discard_after :
594- # Only keep files seen in this backup that old enough not to suffer race conditions relating
595- # to filesystem snapshots and ctime/mtime granularity or being modified while we read them.
596- keep = True
597- else :
598- keep = False
599- race_discarded += 1
600- else : # old entries
601- if entry .age < ttl :
602- # Also keep files from older backups that have not reached BORG_FILES_CACHE_TTL yet.
603- keep = True
604- else :
605- keep = False
606- age_discarded += 1
607- if keep :
608- msgpack .pack ((path_hash , entry ), fd )
609- entries += 1
585+ cache_path = str (self .path / self .files_cache_name ())
586+ with SaveFile (cache_path , binary = True ) as sync_file :
587+ with IntegrityCheckedFile (path = cache_path , write = True , override_fd = sync_file ) as fd :
588+ entries = 0
589+ age_discarded = 0
590+ race_discarded = 0
591+ for path_hash , entry in files .items ():
592+ entry = self .decompress_entry (entry )
593+ if entry .age == 0 : # current entries
594+ if max (timestamp_to_int (entry .ctime ), timestamp_to_int (entry .mtime )) < discard_after :
595+ # Only keep files seen in this backup that old enough not to suffer race conditions
596+ # relating to filesystem snapshots and ctime/mtime granularity or being modified
597+ # while we read them.
598+ keep = True
599+ else :
600+ keep = False
601+ race_discarded += 1
602+ else : # old entries
603+ if entry .age < ttl :
604+ # Also keep files from older backups that have not reached BORG_FILES_CACHE_TTL yet.
605+ keep = True
606+ else :
607+ keep = False
608+ age_discarded += 1
609+ if keep :
610+ msgpack .pack ((path_hash , entry ), fd )
611+ entries += 1
612+ integrity_data = fd .integrity_data
610613 files_cache_logger .debug (f"FILES-CACHE-KILL: removed { age_discarded } entries with age >= TTL [{ ttl } ]" )
611614 t_str = datetime .fromtimestamp (discard_after / 1e9 , timezone .utc ).isoformat ()
612615 files_cache_logger .debug (f"FILES-CACHE-KILL: removed { race_discarded } entries with ctime/mtime >= { t_str } " )
613616 files_cache_logger .debug (f"FILES-CACHE-SAVE: finished, { entries } remaining entries saved." )
614- return fd . integrity_data
617+ return integrity_data
615618
616619 def file_known_and_unchanged (self , hashed_path , path_hash , st ):
617620 """
0 commit comments