Skip to content

Commit e1d8a23

Browse files
zeroosChangaco
authored andcommitted
atime and ctime (#49)
add support and tests for ctime and atime
1 parent 95b3499 commit e1d8a23

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

libarchive/entry.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,18 @@ def issock(self):
8989
def isdev(self):
9090
return self.ischr or self.isblk or self.isfifo or self.issock
9191

92+
@property
93+
def atime(self):
94+
return ffi.entry_atime(self._entry_p)
95+
9296
@property
9397
def mtime(self):
9498
return ffi.entry_mtime(self._entry_p)
9599

100+
@property
101+
def ctime(self):
102+
return ffi.entry_ctime(self._entry_p)
103+
96104
def _getpathname(self):
97105
return (ffi.entry_pathname_w(self._entry_p) or
98106
ffi.entry_pathname(self._entry_p))

libarchive/ffi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ def ffi(name, argtypes, restype, errcheck=None):
110110
ffi('entry_new', [], c_archive_entry_p, check_null)
111111

112112
ffi('entry_filetype', [c_archive_entry_p], c_int)
113+
ffi('entry_atime', [c_archive_entry_p], c_int)
113114
ffi('entry_mtime', [c_archive_entry_p], c_int)
115+
ffi('entry_ctime', [c_archive_entry_p], c_int)
114116
ffi('entry_pathname', [c_archive_entry_p], c_char_p)
115117
ffi('entry_pathname_w', [c_archive_entry_p], c_wchar_p)
116118
ffi('entry_sourcepath', [c_archive_entry_p], c_char_p)

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def stat_dict(path):
117117
return {k: v for k, v in locals().items() if k in keys}
118118

119119

120-
def treestat(d):
120+
def treestat(d, stat_dict=stat_dict):
121121
r = {}
122122
for dirpath, dirnames, filenames in walk(d):
123123
r[dirpath] = stat_dict(dirpath)

tests/test_atime_ctime.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from __future__ import division, print_function, unicode_literals
2+
3+
from copy import copy
4+
from os import stat
5+
6+
from libarchive import (file_reader, file_writer, memory_reader,
7+
memory_writer)
8+
9+
from . import treestat
10+
11+
12+
def check_atime_ctime(archive, tree):
13+
tree2 = copy(tree)
14+
for e in archive:
15+
epath = str(e).rstrip('/')
16+
assert epath in tree2
17+
estat = tree2.pop(epath)
18+
assert e.atime == int(estat['atime'])
19+
assert e.ctime == int(estat['ctime'])
20+
21+
22+
def stat_dict(path):
23+
_, _, _, _, _, _, _, atime, _, ctime = stat(path)
24+
return {"atime": atime, "ctime": ctime}
25+
26+
27+
def test_memory_atime_ctime():
28+
# Collect information on what should be in the archive
29+
tree = treestat('libarchive', stat_dict)
30+
31+
# Create an archive of our libarchive/ directory
32+
buf = bytes(bytearray(1000000))
33+
with memory_writer(buf, 'zip') as archive1:
34+
archive1.add_files('libarchive/')
35+
36+
# Check the data
37+
with memory_reader(buf) as archive2:
38+
check_atime_ctime(archive2, tree)
39+
40+
41+
def test_file_atime_ctime(tmpdir):
42+
archive_path = tmpdir.strpath+'/test.zip'
43+
44+
# Collect information on what should be in the archive
45+
tree = treestat('libarchive', stat_dict)
46+
47+
# Create an archive of our libarchive/ directory
48+
with file_writer(archive_path, 'zip') as archive:
49+
archive.add_files('libarchive/')
50+
51+
# Read the archive and check that the data is correct
52+
with file_reader(archive_path) as archive:
53+
check_atime_ctime(archive, tree)

0 commit comments

Comments
 (0)