Skip to content

Commit

Permalink
[PR #6274/14b19afc backport][stable-5] archive: Generate crc32 over 1…
Browse files Browse the repository at this point in the history
…6MiB chunks (#6325)

archive: Generate crc32 over 16MiB chunks (#6274)

* archive: Generate crc32 over 16MiB chunks

Running crc32 over the whole content of the compressed file potentially
requires a lot of RAM. The crc32 function in zlib allows for calculating
the checksum in chunks. This changes the code to calculate the checksum
over 16 MiB chunks instead. 16 MiB is the value also used by
shutil.copyfileobj().

* Update changelogs/fragments/6199-archive-generate-checksum-in-chunks.yml

Change the type of change to bugfix

Co-authored-by: Felix Fontein <[email protected]>

* Update changelogs/fragments/6199-archive-generate-checksum-in-chunks.yml

Co-authored-by: Felix Fontein <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
(cherry picked from commit 14b19af)

Co-authored-by: Nils Meyer <[email protected]>
  • Loading branch information
patchback[bot] and nilsmeyer authored Apr 13, 2023
1 parent cb49b96 commit 72b282f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- archive - reduce RAM usage by generating CRC32 checksum over chunks (https://github.com/ansible-collections/community.general/pull/6274).
8 changes: 7 additions & 1 deletion plugins/modules/files/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,13 @@ def _get_checksums(self, path):
# The python implementations of gzip, bz2, and lzma do not support restoring compressed files
# to their original names so only file checksum is returned
f = self._open_compressed_file(_to_native_ascii(path), 'r')
checksums = set([(b'', crc32(f.read()))])
checksum = 0
while True:
chunk = f.read(16 * 1024 * 1024)
if not chunk:
break
checksum = crc32(chunk, checksum)
checksums = set([(b'', checksum)])
f.close()
except Exception:
checksums = set()
Expand Down

0 comments on commit 72b282f

Please sign in to comment.