OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The CPIO file system implementation.""" | 2 """The CPIO file system implementation.""" |
3 | 3 |
| 4 from __future__ import unicode_literals |
| 5 |
4 # This is necessary to prevent a circular import. | 6 # This is necessary to prevent a circular import. |
5 import dfvfs.vfs.cpio_file_entry | 7 import dfvfs.vfs.cpio_file_entry |
6 | 8 |
7 from dfvfs.lib import cpio | 9 from dfvfs.lib import cpio |
8 from dfvfs.lib import definitions | 10 from dfvfs.lib import definitions |
9 from dfvfs.lib import errors | 11 from dfvfs.lib import errors |
10 from dfvfs.path import cpio_path_spec | 12 from dfvfs.path import cpio_path_spec |
11 from dfvfs.resolver import resolver | 13 from dfvfs.resolver import resolver |
12 from dfvfs.vfs import file_system | 14 from dfvfs.vfs import file_system |
13 | 15 |
14 | 16 |
15 class CPIOFileSystem(file_system.FileSystem): | 17 class CPIOFileSystem(file_system.FileSystem): |
16 """Class that implements a file system object using CPIOArchiveFile.""" | 18 """Class that implements a file system object using CPIOArchiveFile.""" |
17 | 19 |
18 LOCATION_ROOT = u'/' | 20 LOCATION_ROOT = '/' |
19 TYPE_INDICATOR = definitions.TYPE_INDICATOR_CPIO | 21 TYPE_INDICATOR = definitions.TYPE_INDICATOR_CPIO |
20 | 22 |
21 def __init__(self, resolver_context, encoding=u'utf-8'): | 23 def __init__(self, resolver_context, encoding='utf-8'): |
22 """Initializes a file system object. | 24 """Initializes a file system object. |
23 | 25 |
24 Args: | 26 Args: |
25 resolver_context: the resolver context (instance of resolver.Context). | 27 resolver_context: the resolver context (instance of resolver.Context). |
26 encoding: optional file entry name encoding. | 28 encoding: optional file entry name encoding. |
27 """ | 29 """ |
28 super(CPIOFileSystem, self).__init__(resolver_context) | 30 super(CPIOFileSystem, self).__init__(resolver_context) |
29 self._cpio_archive_file = None | 31 self._cpio_archive_file = None |
30 self._file_object = None | 32 self._file_object = None |
31 self.encoding = encoding | 33 self.encoding = encoding |
(...skipping 18 matching lines...) Expand all Loading... |
50 mode: optional file access mode. The default is 'rb' read-only binary. | 52 mode: optional file access mode. The default is 'rb' read-only binary. |
51 | 53 |
52 Raises: | 54 Raises: |
53 AccessError: if the access to open the file was denied. | 55 AccessError: if the access to open the file was denied. |
54 IOError: if the file system object could not be opened. | 56 IOError: if the file system object could not be opened. |
55 PathSpecError: if the path specification is incorrect. | 57 PathSpecError: if the path specification is incorrect. |
56 ValueError: if the path specification is invalid. | 58 ValueError: if the path specification is invalid. |
57 """ | 59 """ |
58 if not path_spec.HasParent(): | 60 if not path_spec.HasParent(): |
59 raise errors.PathSpecError( | 61 raise errors.PathSpecError( |
60 u'Unsupported path specification without parent.') | 62 'Unsupported path specification without parent.') |
61 | 63 |
62 file_object = resolver.Resolver.OpenFileObject( | 64 file_object = resolver.Resolver.OpenFileObject( |
63 path_spec.parent, resolver_context=self._resolver_context) | 65 path_spec.parent, resolver_context=self._resolver_context) |
64 | 66 |
65 cpio_archive_file = cpio.CPIOArchiveFile() | 67 cpio_archive_file = cpio.CPIOArchiveFile() |
66 try: | 68 try: |
67 cpio_archive_file.Open(file_object) | 69 cpio_archive_file.Open(file_object) |
68 except: | 70 except: |
69 file_object.close() | 71 file_object.close() |
70 raise | 72 raise |
71 | 73 |
72 self._file_object = file_object | 74 self._file_object = file_object |
73 self._cpio_archive_file = cpio_archive_file | 75 self._cpio_archive_file = cpio_archive_file |
74 | 76 |
75 def FileEntryExistsByPathSpec(self, path_spec): | 77 def FileEntryExistsByPathSpec(self, path_spec): |
76 """Determines if a file entry for a path specification exists. | 78 """Determines if a file entry for a path specification exists. |
77 | 79 |
78 Args: | 80 Args: |
79 path_spec: a path specification (instance of PathSpec). | 81 path_spec: a path specification (instance of PathSpec). |
80 | 82 |
81 Returns: | 83 Returns: |
82 Boolean indicating if the file entry exists. | 84 Boolean indicating if the file entry exists. |
83 """ | 85 """ |
84 location = getattr(path_spec, u'location', None) | 86 location = getattr(path_spec, 'location', None) |
85 | 87 |
86 if (location is None or | 88 if (location is None or |
87 not location.startswith(self.LOCATION_ROOT)): | 89 not location.startswith(self.LOCATION_ROOT)): |
88 return | 90 return |
89 | 91 |
90 if len(location) == 1: | 92 if len(location) == 1: |
91 return True | 93 return True |
92 | 94 |
93 return self._cpio_archive_file.FileEntryExistsByPath(location[1:]) | 95 return self._cpio_archive_file.FileEntryExistsByPath(location[1:]) |
94 | 96 |
95 def GetFileEntryByPathSpec(self, path_spec): | 97 def GetFileEntryByPathSpec(self, path_spec): |
96 """Retrieves a file entry for a path specification. | 98 """Retrieves a file entry for a path specification. |
97 | 99 |
98 Args: | 100 Args: |
99 path_spec: a path specification (instance of PathSpec). | 101 path_spec: a path specification (instance of PathSpec). |
100 | 102 |
101 Returns: | 103 Returns: |
102 A file entry (instance of vfs.CPIOFileEntry) or None. | 104 A file entry (instance of vfs.CPIOFileEntry) or None. |
103 """ | 105 """ |
104 cpio_archive_file_entry = None | 106 cpio_archive_file_entry = None |
105 location = getattr(path_spec, u'location', None) | 107 location = getattr(path_spec, 'location', None) |
106 | 108 |
107 if (location is None or | 109 if (location is None or |
108 not location.startswith(self.LOCATION_ROOT)): | 110 not location.startswith(self.LOCATION_ROOT)): |
109 return | 111 return |
110 | 112 |
111 if len(location) == 1: | 113 if len(location) == 1: |
112 return dfvfs.vfs.cpio_file_entry.CPIOFileEntry( | 114 return dfvfs.vfs.cpio_file_entry.CPIOFileEntry( |
113 self._resolver_context, self, path_spec, is_root=True, | 115 self._resolver_context, self, path_spec, is_root=True, |
114 is_virtual=True) | 116 is_virtual=True) |
115 | 117 |
(...skipping 15 matching lines...) Expand all Loading... |
131 location=self.LOCATION_ROOT, parent=self._path_spec.parent) | 133 location=self.LOCATION_ROOT, parent=self._path_spec.parent) |
132 return self.GetFileEntryByPathSpec(path_spec) | 134 return self.GetFileEntryByPathSpec(path_spec) |
133 | 135 |
134 def GetCPIOArchiveFile(self): | 136 def GetCPIOArchiveFile(self): |
135 """Retrieves the CPIO archive file object. | 137 """Retrieves the CPIO archive file object. |
136 | 138 |
137 Returns: | 139 Returns: |
138 The CPIO archvie file object (instance of cpio.CPIOArchiveFile). | 140 The CPIO archvie file object (instance of cpio.CPIOArchiveFile). |
139 """ | 141 """ |
140 return self._cpio_archive_file | 142 return self._cpio_archive_file |
OLD | NEW |