Browse Source

Use platform.SaveFile for repository, cache and key files

Fixes #1060
Marian Beermann 9 years ago
parent
commit
f4be2b3523
3 changed files with 9 additions and 7 deletions
  1. 5 4
      src/borg/cache.py
  2. 2 1
      src/borg/key.py
  3. 2 2
      src/borg/repository.py

+ 5 - 4
src/borg/cache.py

@@ -19,6 +19,7 @@ from .helpers import yes
 from .item import Item
 from .key import PlaintextKey
 from .locking import UpgradableLock
+from .platform import SaveFile
 from .remote import cache_if_remote
 
 ChunkListEntry = namedtuple('ChunkListEntry', 'id size csize')
@@ -141,11 +142,11 @@ Chunk index:    {0.total_unique_chunks:20d} {0.total_chunks:20d}"""
         config.set('cache', 'version', '1')
         config.set('cache', 'repository', self.repository.id_str)
         config.set('cache', 'manifest', '')
-        with open(os.path.join(self.path, 'config'), 'w') as fd:
+        with SaveFile(os.path.join(self.path, 'config'), binary=False) as fd:
             config.write(fd)
         ChunkIndex().write(os.path.join(self.path, 'chunks').encode('utf-8'))
         os.makedirs(os.path.join(self.path, 'chunks.archive.d'))
-        with open(os.path.join(self.path, 'files'), 'wb') as fd:
+        with SaveFile(os.path.join(self.path, 'files')) as fd:
             pass  # empty file
 
     def _do_open(self):
@@ -212,7 +213,7 @@ Chunk index:    {0.total_unique_chunks:20d} {0.total_chunks:20d}"""
         if not self.txn_active:
             return
         if self.files is not None:
-            with open(os.path.join(self.path, 'files'), 'wb') as fd:
+            with SaveFile(os.path.join(self.path, 'files')) as fd:
                 for path_hash, item in self.files.items():
                     # Discard cached files with the newest mtime to avoid
                     # issues with filesystem snapshots and mtime precision
@@ -223,7 +224,7 @@ Chunk index:    {0.total_unique_chunks:20d} {0.total_chunks:20d}"""
         self.config.set('cache', 'timestamp', self.manifest.timestamp)
         self.config.set('cache', 'key_type', str(self.key.TYPE))
         self.config.set('cache', 'previous_location', self.repository._location.canonical_path())
-        with open(os.path.join(self.path, 'config'), 'w') as fd:
+        with SaveFile(os.path.join(self.path, 'config'), binary=False) as fd:
             self.config.write(fd)
         self.chunks.write(os.path.join(self.path, 'chunks').encode('utf-8'))
         os.rename(os.path.join(self.path, 'txn.active'),

+ 2 - 1
src/borg/key.py

@@ -22,6 +22,7 @@ from .helpers import get_keys_dir
 from .helpers import bin_to_hex
 from .helpers import CompressionDecider2, CompressionSpec
 from .item import Key, EncryptedKey
+from .platform import SaveFile
 
 
 PREFIX = b'\0' * 8
@@ -470,7 +471,7 @@ class KeyfileKey(KeyfileKeyBase):
 
     def save(self, target, passphrase):
         key_data = self._save(passphrase)
-        with open(target, 'w') as fd:
+        with SaveFile(target, binary=False) as fd:
             fd.write('%s %s\n' % (self.FILE_ID, bin_to_hex(self.repository_id)))
             fd.write(key_data)
             fd.write('\n')

+ 2 - 2
src/borg/repository.py

@@ -23,7 +23,7 @@ from .helpers import ProgressIndicatorPercent
 from .helpers import bin_to_hex
 from .locking import UpgradableLock, LockError, LockErrorT
 from .lrucache import LRUCache
-from .platform import SyncFile, sync_dir
+from .platform import SaveFile, SyncFile, sync_dir
 
 MAX_OBJECT_SIZE = 20 * 1024 * 1024
 MAGIC = b'BORG_SEG'
@@ -160,7 +160,7 @@ class Repository:
 
     def save_config(self, path, config):
         config_path = os.path.join(path, 'config')
-        with open(config_path, 'w') as fd:
+        with SaveFile(config_path, binary=False) as fd:
             config.write(fd)
 
     def save_key(self, keydata):