Archive module does not adds empty(zero-size) files to dest archive #34569
OS / ENVIRONMENT
Archive module does not add empty(zero-size) files to archive. I think it is caused by this condition https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/archive.py#L319
In this condition we are comparing file that we adding to archive with just created archive. In case of empty file it should not work. Why do we need this condition?
STEPS TO REPRODUCE
- hosts: all tasks: - name: Test archive archive: path: /home/user/test-archive-bug dest: /home/user/test.tar.gz format: gz
That all files including empty file will be added to archive
Empty(zero-size) file was not added to archive
Looking through git history 2aa3f52 seems to introduce the filecmp.cmp
Looks like that was added to handle some corner cases with empty archives and similar
Haven't reproduced or bisected the bug yet, so thats not necessarily the cause.
I did some research, looks like filecmp.cmp causes this issue. In my test case file with name 3.txt is zero size and filecmp.cmp says that it is equal with my just created tar.gz archive :)
import tarfile import filecmp import os import time epoch_time = int(time.time()) path = '/home/user/test-archive-bug/' dest = '/home/user/test' + str(epoch_time) + '.tar.gz' arcfile = tarfile.open(dest, 'w|gz') for dirpath, dirnames, filenames in os.walk(path, topdown=True): for filename in filenames: fullpath = dirpath + filename print('Comparing ' + fullpath + ' with ' + dest + ' --- result is ' + str(filecmp.cmp(fullpath,dest))) arcfile.close()
I can reproduce this:
--- - hosts: localhost gather_facts: false tasks: - name: Test archive archive: path: test-archive-bug dest: the_dest/test.tar.gz format: gz
bisecting is kind of ambiguous (lots of api changes ) but narrows down commits to:
Of those, these look like suspects:
Fix for this bug should be stating both files and If they are different number of bytes, treating them as different. if they are both zero bytes, treating them as the same. else run filecmp on them to see if they are the same or different. It's a reasonably easy fix but this module is in need of a maintainer to work on it.
just to make sure I understand logic correct if file and archive are different we are currently handle them (added to archive) but if they are the same we don't add file to archive. so maybe better just to write file in case zero byte size too (because we have situation when archive and file zero byte and treated as the same ) ? I got in all cases that we compare some file with newly created archive I am not sure do we need compare file (which we would like to add to archive) with dest archive at all?