mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Fix FileStore.backup_file() not to backup same file
FileStore.backup_file() docstring claimed not to store a copy of the same file but the behavior of the method did not match this description. This commit makes the backed-up file filename derivation deterministic by hashing its content by SHA-256, thus it should not back up two files with the same filename and content. Reviewed-By: Aleksei Slaikovskii <aslaikov@redhat.com>
This commit is contained in:
parent
631d3152fe
commit
364ffd5a0f
@ -29,6 +29,8 @@ import os.path
|
|||||||
import shutil
|
import shutil
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
from hashlib import sha256
|
||||||
|
|
||||||
import six
|
import six
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
@ -111,7 +113,7 @@ class FileStore(object):
|
|||||||
p.write(f)
|
p.write(f)
|
||||||
|
|
||||||
def backup_file(self, path):
|
def backup_file(self, path):
|
||||||
"""Create a copy of the file at @path - so long as a copy
|
"""Create a copy of the file at @path - as long as an exact copy
|
||||||
does not already exist - which will be restored to its
|
does not already exist - which will be restored to its
|
||||||
original location by restore_files().
|
original location by restore_files().
|
||||||
"""
|
"""
|
||||||
@ -126,11 +128,11 @@ class FileStore(object):
|
|||||||
|
|
||||||
_reldir, backupfile = os.path.split(path)
|
_reldir, backupfile = os.path.split(path)
|
||||||
|
|
||||||
filename = ""
|
with open(path, 'rb') as f:
|
||||||
for _i in range(8):
|
cont_hash = sha256(f.read()).hexdigest()
|
||||||
h = "%02x" % self.random.randint(0,255)
|
|
||||||
filename += h
|
filename = "{hexhash}-{bcppath}".format(
|
||||||
filename += "-"+backupfile
|
hexhash=cont_hash, bcppath=backupfile)
|
||||||
|
|
||||||
backup_path = os.path.join(self._path, filename)
|
backup_path = os.path.join(self._path, filename)
|
||||||
if os.path.exists(backup_path):
|
if os.path.exists(backup_path):
|
||||||
|
Loading…
Reference in New Issue
Block a user