Skip to content

Commit

Permalink
Merge branch 'atime-and-ctime'
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroos committed Dec 20, 2016
2 parents bc17c99 + c7dcd4a commit 14f6f24
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 55 deletions.
12 changes: 12 additions & 0 deletions libarchive/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,26 @@ def isdev(self):
def atime(self):
return ffi.entry_atime(self._entry_p)

def set_atime(self, timestamp_sec, timestamp_nsec):
return ffi.entry_set_atime(self._entry_p,
timestamp_sec, timestamp_nsec)

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

def set_mtime(self, timestamp_sec, timestamp_nsec):
return ffi.entry_set_mtime(self._entry_p,
timestamp_sec, timestamp_nsec)

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

def set_ctime(self, timestamp_sec, timestamp_nsec):
return ffi.entry_set_ctime(self._entry_p,
timestamp_sec, timestamp_nsec)

def _getpathname(self):
return (ffi.entry_pathname_w(self._entry_p) or
ffi.entry_pathname(self._entry_p))
Expand Down
9 changes: 7 additions & 2 deletions libarchive/ffi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import division, print_function, unicode_literals

from ctypes import (
c_char_p, c_int, c_uint, c_longlong, c_size_t, c_void_p,
c_char_p, c_int, c_uint, c_long, c_longlong, c_size_t, c_void_p,
c_wchar_p, CFUNCTYPE, POINTER,
)

Expand Down Expand Up @@ -127,9 +127,12 @@ def ffi(name, argtypes, restype, errcheck=None):
ffi('entry_rdevmajor', [c_archive_entry_p], c_uint)
ffi('entry_rdevminor', [c_archive_entry_p], c_uint)

ffi('entry_set_size', [c_archive_entry_p, c_longlong], c_int)
ffi('entry_set_size', [c_archive_entry_p, c_longlong], c_longlong)
ffi('entry_set_filetype', [c_archive_entry_p, c_int], c_int)
ffi('entry_set_perm', [c_archive_entry_p, c_int], c_int)
ffi('entry_set_atime', [c_archive_entry_p, c_int, c_long], None)
ffi('entry_set_mtime', [c_archive_entry_p, c_int, c_long], None)
ffi('entry_set_ctime', [c_archive_entry_p, c_int, c_long], None)

ffi('entry_update_pathname_utf8', [c_archive_entry_p, c_char_p], None)

Expand Down Expand Up @@ -240,5 +243,7 @@ def ffi(name, argtypes, restype, errcheck=None):
c_int, check_int)
ffi('write_finish_entry', [c_archive_p], c_int, check_int)

ffi('write_fail', [c_archive_p], c_int, check_int)

ffi('write_close', [c_archive_p], c_int, check_int)
ffi('write_free', [c_archive_p], c_int, check_int)
53 changes: 0 additions & 53 deletions tests/test_atime_ctime.py

This file was deleted.

105 changes: 105 additions & 0 deletions tests/test_atime_mtime_ctime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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 entry in archive:
epath = str(entry).rstrip('/')
assert epath in tree2
estat = tree2.pop(epath)
assert entry.atime == int(estat['atime'])
assert entry.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)


def test_memory_time_setters():
# Create an archive of our libarchive/ directory

atimestamp = (1482144741, 495628118)
mtimestamp = (1482155417, 659017086)
ctimestamp = (1482145211, 536858081)
buf = bytes(bytearray(1000000))
with memory_writer(buf, "zip") as archive1:
archive1.add_files('libarchive/')

buf2 = bytes(bytearray(1000000))
with memory_reader(buf) as archive1:
with memory_writer(buf2, "zip") as archive2:
for entry in archive1:
entry.set_atime(*atimestamp)
entry.set_mtime(*mtimestamp)
entry.set_ctime(*ctimestamp)
archive2.add_entries([entry])

with memory_reader(buf2) as archive2:
for entry in archive2:
assert entry.atime == atimestamp[0]
assert entry.mtime == mtimestamp[0]
assert entry.ctime == ctimestamp[0]


def test_file_time_setters(tmpdir):
# Create an archive of our libarchive/ directory
archive_path = tmpdir.join('/test.zip').strpath
archive2_path = tmpdir.join('/test2.zip').strpath

atimestamp = (1482144741, 495628118)
mtimestamp = (1482155417, 659017086)
ctimestamp = (1482145211, 536858081)
with file_writer(archive_path, "zip") as archive1:
archive1.add_files('libarchive/')

with file_reader(archive_path) as archive1:
with file_writer(archive2_path, "zip") as archive2:
for entry in archive1:
entry.set_atime(*atimestamp)
entry.set_mtime(*mtimestamp)
entry.set_ctime(*ctimestamp)
archive2.add_entries([entry])

with file_reader(archive2_path) as archive2:
for entry in archive2:
assert entry.atime == atimestamp[0]
assert entry.mtime == mtimestamp[0]
assert entry.ctime == ctimestamp[0]

0 comments on commit 14f6f24

Please sign in to comment.