Skip to content

Commit

Permalink
Allow timestamps for add_file_from_memory
Browse files Browse the repository at this point in the history
Closes #73
  • Loading branch information
MartyMacGyver committed Apr 14, 2019
1 parent 853a1fd commit 09e262b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
31 changes: 30 additions & 1 deletion libarchive/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ def add_files(self, *paths, **kw):
def add_file_from_memory(
self, entry_path, entry_size, entry_data,
filetype=REGULAR_FILE,
permission=DEFAULT_UNIX_PERMISSION
permission=DEFAULT_UNIX_PERMISSION,
atime=None,
mtime=None,
ctime=None,
birthtime=None,
):
""""Add file from memory to archive.
Expand All @@ -93,6 +97,14 @@ def add_file_from_memory(
:type filetype: octal number
:param permission: with which permission should entry be created
:type permission: octal number
:param atime: Last access time
:type atime: int seconds or tuple (int seconds, int nanoseconds)
:param mtime: Last modified time
:type mtime: int seconds or tuple (int seconds, int nanoseconds)
:param ctime: Creation time
:type ctime: int seconds or tuple (int seconds, int nanoseconds)
:param birthtime: Birth time (for archive formats that support it)
:type birthtime: int seconds or tuple (int seconds, int nanoseconds)
"""
archive_pointer = self._pointer

Expand All @@ -103,6 +115,23 @@ def add_file_from_memory(
entry_set_size(archive_entry_pointer, entry_size)
entry_set_filetype(archive_entry_pointer, filetype)
entry_set_perm(archive_entry_pointer, permission)

if atime is not None:
archive_entry.set_atime(*(
(atime, 0)
if isinstance(atime, (int, float)) else atime))
if mtime is not None:
archive_entry.set_mtime(*(
(mtime, 0)
if isinstance(mtime, (int, float)) else mtime))
if ctime is not None:
archive_entry.set_ctime(*(
(ctime, 0)
if isinstance(ctime, (int, float)) else ctime))
if birthtime is not None:
archive_entry.set_birthtime(*(
(birthtime, 0)
if isinstance(birthtime, (int, float)) else birthtime))
write_header(archive_pointer, archive_entry_pointer)

for chunk in entry_data:
Expand Down
14 changes: 12 additions & 2 deletions tests/test_rwx.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,22 @@ def test_adding_entry_from_memory():

blocks = []

archfmt = 'zip'
has_birthtime = archfmt != 'zip'

atime = (1482144741, 495628118)
mtime = (1482155417, 659017086)
ctime = (1482145211, 536858081)
btime = (1482144740, 495628118) if has_birthtime else None

def write_callback(data):
blocks.append(data[:])
return len(data)

with libarchive.custom_writer(write_callback, 'zip') as archive:
archive.add_file_from_memory(entry_path, entry_size, entry_data)
with libarchive.custom_writer(write_callback, archfmt) as archive:
archive.add_file_from_memory(
entry_path, entry_size, entry_data,
atime=atime, mtime=mtime, ctime=ctime, birthtime=btime)

buf = b''.join(blocks)
with libarchive.memory_reader(buf) as memory_archive:
Expand Down

0 comments on commit 09e262b

Please sign in to comment.