avoid shredding empty files, also x/0
also cleaned up unused import and exception var
This commit is contained in:
parent
627dec716b
commit
f26adcc7da
1 changed files with 17 additions and 15 deletions
|
@ -71,7 +71,7 @@ try:
|
|||
except ImportError:
|
||||
pass
|
||||
|
||||
from ansible.compat.six import PY3, byte2int
|
||||
from ansible.compat.six import PY3
|
||||
from ansible.utils.unicode import to_unicode, to_bytes
|
||||
|
||||
HAS_ANY_PBKDF2HMAC = HAS_PBKDF2 or HAS_PBKDF2HMAC
|
||||
|
@ -236,22 +236,24 @@ class VaultEditor:
|
|||
"""
|
||||
|
||||
file_len = os.path.getsize(tmp_path)
|
||||
max_chunk_len = min(1024*1024*2, file_len)
|
||||
|
||||
passes = 3
|
||||
with open(tmp_path, "wb") as fh:
|
||||
for _ in range(passes):
|
||||
fh.seek(0, 0)
|
||||
# get a random chunk of data, each pass with other length
|
||||
chunk_len = random.randint(max_chunk_len//2, max_chunk_len)
|
||||
data = os.urandom(chunk_len)
|
||||
if file_len > 0: # avoid work when file was empty
|
||||
max_chunk_len = min(1024*1024*2, file_len)
|
||||
|
||||
for _ in range(0, file_len // chunk_len):
|
||||
fh.write(data)
|
||||
fh.write(data[:file_len % chunk_len])
|
||||
passes = 3
|
||||
with open(tmp_path, "wb") as fh:
|
||||
for _ in range(passes):
|
||||
fh.seek(0, 0)
|
||||
# get a random chunk of data, each pass with other length
|
||||
chunk_len = random.randint(max_chunk_len//2, max_chunk_len)
|
||||
data = os.urandom(chunk_len)
|
||||
|
||||
assert(fh.tell() == file_len) # FIXME remove this assert once we have unittests to check its accuracy
|
||||
os.fsync(fh)
|
||||
for _ in range(0, file_len // chunk_len):
|
||||
fh.write(data)
|
||||
fh.write(data[:file_len % chunk_len])
|
||||
|
||||
assert(fh.tell() == file_len) # FIXME remove this assert once we have unittests to check its accuracy
|
||||
os.fsync(fh)
|
||||
|
||||
|
||||
def _shred_file(self, tmp_path):
|
||||
|
@ -273,7 +275,7 @@ class VaultEditor:
|
|||
|
||||
try:
|
||||
r = call(['shred', tmp_path])
|
||||
except OSError as e:
|
||||
except OSError:
|
||||
# shred is not available on this system, or some other error occured.
|
||||
r = 1
|
||||
|
||||
|
|
Loading…
Reference in a new issue