Skip to content

Commit

Permalink
atime and ctime (#49)
Browse files Browse the repository at this point in the history
add support and tests for ctime and atime
  • Loading branch information
zeroos authored and Changaco committed Dec 17, 2016
1 parent 95b3499 commit e1d8a23
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
8 changes: 8 additions & 0 deletions libarchive/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,18 @@ def issock(self):
def isdev(self):
return self.ischr or self.isblk or self.isfifo or self.issock

@property
def atime(self):
return ffi.entry_atime(self._entry_p)

@property
def mtime(self):
return ffi.entry_mtime(self._entry_p)

@property
def ctime(self):
return ffi.entry_ctime(self._entry_p)

def _getpathname(self):
return (ffi.entry_pathname_w(self._entry_p) or
ffi.entry_pathname(self._entry_p))
Expand Down
2 changes: 2 additions & 0 deletions libarchive/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def ffi(name, argtypes, restype, errcheck=None):
ffi('entry_new', [], c_archive_entry_p, check_null)

ffi('entry_filetype', [c_archive_entry_p], c_int)
ffi('entry_atime', [c_archive_entry_p], c_int)
ffi('entry_mtime', [c_archive_entry_p], c_int)
ffi('entry_ctime', [c_archive_entry_p], c_int)
ffi('entry_pathname', [c_archive_entry_p], c_char_p)
ffi('entry_pathname_w', [c_archive_entry_p], c_wchar_p)
ffi('entry_sourcepath', [c_archive_entry_p], c_char_p)
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def stat_dict(path):
return {k: v for k, v in locals().items() if k in keys}


def treestat(d):
def treestat(d, stat_dict=stat_dict):
r = {}
for dirpath, dirnames, filenames in walk(d):
r[dirpath] = stat_dict(dirpath)
Expand Down
53 changes: 53 additions & 0 deletions tests/test_atime_ctime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from __future__ import division, print_function, unicode_literals

from copy import copy
from os import stat

from libarchive import (file_reader, file_writer, memory_reader,
memory_writer)

from . import treestat


def check_atime_ctime(archive, tree):
tree2 = copy(tree)
for e in archive:
epath = str(e).rstrip('/')
assert epath in tree2
estat = tree2.pop(epath)
assert e.atime == int(estat['atime'])
assert e.ctime == int(estat['ctime'])


def stat_dict(path):
_, _, _, _, _, _, _, atime, _, ctime = stat(path)
return {"atime": atime, "ctime": ctime}


def test_memory_atime_ctime():
# Collect information on what should be in the archive
tree = treestat('libarchive', stat_dict)

# Create an archive of our libarchive/ directory
buf = bytes(bytearray(1000000))
with memory_writer(buf, 'zip') as archive1:
archive1.add_files('libarchive/')

# Check the data
with memory_reader(buf) as archive2:
check_atime_ctime(archive2, tree)


def test_file_atime_ctime(tmpdir):
archive_path = tmpdir.strpath+'/test.zip'

# Collect information on what should be in the archive
tree = treestat('libarchive', stat_dict)

# Create an archive of our libarchive/ directory
with file_writer(archive_path, 'zip') as archive:
archive.add_files('libarchive/')

# Read the archive and check that the data is correct
with file_reader(archive_path) as archive:
check_atime_ctime(archive, tree)

0 comments on commit e1d8a23

Please sign in to comment.