Skip to content

Commit 2c86cc2

Browse files
MartyMacGyverChangaco
authored andcommitted
Allow timestamps for add_file_from_memory
Closes #73
1 parent e9324f5 commit 2c86cc2

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

libarchive/write.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ def add_files(self, *paths, **kw):
7878
def add_file_from_memory(
7979
self, entry_path, entry_size, entry_data,
8080
filetype=REGULAR_FILE,
81-
permission=DEFAULT_UNIX_PERMISSION
81+
permission=DEFAULT_UNIX_PERMISSION,
82+
atime=None,
83+
mtime=None,
84+
ctime=None,
85+
birthtime=None,
8286
):
8387
""""Add file from memory to archive.
8488
@@ -93,6 +97,14 @@ def add_file_from_memory(
9397
:type filetype: octal number
9498
:param permission: with which permission should entry be created
9599
:type permission: octal number
100+
:param atime: Last access time
101+
:type atime: int seconds or tuple (int seconds, int nanoseconds)
102+
:param mtime: Last modified time
103+
:type mtime: int seconds or tuple (int seconds, int nanoseconds)
104+
:param ctime: Creation time
105+
:type ctime: int seconds or tuple (int seconds, int nanoseconds)
106+
:param birthtime: Birth time (for archive formats that support it)
107+
:type birthtime: int seconds or tuple (int seconds, int nanoseconds)
96108
"""
97109
archive_pointer = self._pointer
98110

@@ -110,6 +122,23 @@ def add_file_from_memory(
110122
entry_set_size(archive_entry_pointer, entry_size)
111123
entry_set_filetype(archive_entry_pointer, filetype)
112124
entry_set_perm(archive_entry_pointer, permission)
125+
126+
if atime is not None:
127+
archive_entry.set_atime(*(
128+
(atime, 0)
129+
if isinstance(atime, (int, float)) else atime))
130+
if mtime is not None:
131+
archive_entry.set_mtime(*(
132+
(mtime, 0)
133+
if isinstance(mtime, (int, float)) else mtime))
134+
if ctime is not None:
135+
archive_entry.set_ctime(*(
136+
(ctime, 0)
137+
if isinstance(ctime, (int, float)) else ctime))
138+
if birthtime is not None:
139+
archive_entry.set_birthtime(*(
140+
(birthtime, 0)
141+
if isinstance(birthtime, (int, float)) else birthtime))
113142
write_header(archive_pointer, archive_entry_pointer)
114143

115144
for chunk in entry_data:

tests/test_rwx.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,23 @@ def test_adding_entry_from_memory(archfmt, data_bytes):
131131

132132
blocks = []
133133

134+
archfmt = 'zip'
135+
has_birthtime = archfmt != 'zip'
136+
137+
atime = (1482144741, 495628118)
138+
mtime = (1482155417, 659017086)
139+
ctime = (1482145211, 536858081)
140+
btime = (1482144740, 495628118) if has_birthtime else None
141+
134142
def write_callback(data):
135143
blocks.append(data[:])
136144
return len(data)
137145

138146
with libarchive.custom_writer(write_callback, archfmt) as archive:
139-
archive.add_file_from_memory(entry_path, entry_size, entry_data)
147+
archive.add_file_from_memory(
148+
entry_path, entry_size, entry_data,
149+
atime=atime, mtime=mtime, ctime=ctime, birthtime=btime
150+
)
140151

141152
buf = b''.join(blocks)
142153
with libarchive.memory_reader(buf) as memory_archive:

0 commit comments

Comments
 (0)