Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setters for ctime, mtime and atime. #50

Merged
merged 3 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about this change?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this change is necessary to allow compressing big files (our files are huge).
BTW longlong is the type that actually matches the C signature.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't match the signature. The archive_entry_set_* functions don't actually return anything (void), so the last argument to ffi should probably be None. I'm not sure why it's currently c_int.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I thought about this commit f301a92
yes, it's completely unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Changaco should I change it back to c_int or to None?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed them to None. The tests passed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I was at it I changed the argument type of entry_set_filetype from c_int to c_uint since the C signature says unsigned int.

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)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't seem to be used, why do you want to add it?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not used directly by any of context managers in python-libarchive-c, but we use it in our project to allow cancelling creation of archive. Since this library is a convenient wrapper around libarchive we think that it's the best place for it (and make this wrapper more complete).
Turns out that if you want to discard the archive you were creating, you can not just stop creating it. Context managers will close the archive, and libarchive will try to fill the missing contents with 000000000000000000..., and that can cause memory overflows. You have to mark the archive as failed (by that function) to tell libarchive that it should give up any work on it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then shouldn't the context managers call write_fail when an exception is raised?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a separate issue for that: #51.

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]