OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The gzip file entry implementation.""" | 2 """The gzip file entry implementation.""" |
3 | 3 |
4 from __future__ import unicode_literals | 4 from __future__ import unicode_literals |
5 | 5 |
| 6 from dfdatetime import posix_time as dfdatetime_posix_time |
| 7 |
6 from dfvfs.lib import definitions | 8 from dfvfs.lib import definitions |
7 from dfvfs.lib import errors | 9 from dfvfs.lib import errors |
| 10 from dfvfs.resolver import resolver |
8 from dfvfs.vfs import root_only_file_entry | 11 from dfvfs.vfs import root_only_file_entry |
9 from dfvfs.vfs import vfs_stat | |
10 | 12 |
11 | 13 |
12 class GzipFileEntry(root_only_file_entry.RootOnlyFileEntry): | 14 class GzipFileEntry(root_only_file_entry.RootOnlyFileEntry): |
13 """Class that implements a file entry object using gzip.""" | 15 """File system file entry that uses gzip.""" |
14 | 16 |
15 TYPE_INDICATOR = definitions.TYPE_INDICATOR_GZIP | 17 TYPE_INDICATOR = definitions.TYPE_INDICATOR_GZIP |
16 | 18 |
17 def _GetStat(self): | 19 def __init__( |
18 """Retrieves the stat object. | 20 self, resolver_context, file_system, path_spec, is_root=False, |
| 21 is_virtual=False): |
| 22 """Initializes a file entry. |
19 | 23 |
20 Returns: | 24 Args: |
21 The stat object (instance of vfs.VFSStat). | 25 resolver_context (Context): resolver context. |
| 26 file_system (FileSystem): file system. |
| 27 path_spec (PathSpec): path specification. |
| 28 is_root (Optional[bool]): True if the file entry is the root file entry |
| 29 of the corresponding file system. |
| 30 is_virtual (Optional[bool]): True if the file entry is a virtual file |
22 | 31 |
23 Raises: | 32 Raises: |
24 BackEndError: when the gzip file is missing. | 33 BackEndError: when the gzip file is missing. |
25 """ | 34 """ |
26 gzip_file = self.GetFileObject() | 35 gzip_file = resolver.Resolver.OpenFileObject( |
| 36 path_spec, resolver_context=resolver_context) |
27 if not gzip_file: | 37 if not gzip_file: |
28 raise errors.BackEndError( | 38 raise errors.BackEndError('Missing gzip file.') |
29 'Unable to open gzip file: {0:s}.'.format(self.path_spec.comparable)) | |
30 | 39 |
31 try: | 40 super(GzipFileEntry, self).__init__( |
32 stat_object = vfs_stat.VFSStat() | 41 resolver_context, file_system, path_spec, is_root=is_root, |
| 42 is_virtual=is_virtual) |
| 43 self._gzip_file = gzip_file |
| 44 self._type = definitions.FILE_ENTRY_TYPE_FILE |
33 | 45 |
34 # File data stat information. | 46 def __del__(self): |
35 stat_object.size = gzip_file.uncompressed_data_size | 47 """Cleans up the file entry.""" |
| 48 # __del__ can be invoked before __init__ has completed. |
| 49 if hasattr(self, '_gzip_file'): |
| 50 self._gzip_file.close() |
| 51 self._gzip_file = None |
36 | 52 |
37 # Date and time stat information. | 53 super(GzipFileEntry, self).__del__() |
38 stat_object.mtime = gzip_file.modification_time | |
39 | 54 |
40 # Ownership and permissions stat information. | 55 def _GetStat(self): |
| 56 """Retrieves information about the file entry. |
41 | 57 |
42 # File entry type stat information. | 58 Returns: |
43 stat_object.type = stat_object.TYPE_FILE | 59 VFSStat: a stat object. |
| 60 """ |
| 61 stat_object = super(GzipFileEntry, self)._GetStat() |
44 | 62 |
45 # Other stat information. | 63 if self._gzip_file: |
46 # gzip_file.comment | 64 stat_object.size = self._gzip_file.uncompressed_data_size |
47 # gzip_file.operating_system | |
48 # gzip_file.original_filename | |
49 | 65 |
50 finally: | 66 # Other stat information. |
51 gzip_file.close() | 67 # gzip_file.comment |
| 68 # gzip_file.operating_system |
| 69 # gzip_file.original_filename |
52 | 70 |
53 return stat_object | 71 return stat_object |
| 72 |
| 73 @property |
| 74 def modification_time(self): |
| 75 """dfdatetime.DateTimeValues: modification time or None if not available.""" |
| 76 timestamp = getattr(self._gzip_file, 'modification_time', None) |
| 77 if timestamp is not None: |
| 78 return dfdatetime_posix_time.PosixTime(timestamp=timestamp) |
OLD | NEW |