LEFT | RIGHT |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """The CPIO file system implementation.""" | 2 """The CPIO file system implementation.""" |
| 3 |
| 4 from __future__ import unicode_literals |
3 | 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 (Context): resolver context. | 27 resolver_context (Context): resolver context. |
26 encoding (Optional[str]): file entry name encoding. | 28 encoding (Optional[str]): 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[str]): file access mode. | 52 mode (Optional[str]): file access mode. |
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 |
(...skipping 10 matching lines...) Expand all Loading... |
105 | 107 |
106 Args: | 108 Args: |
107 path_spec (PathSpec): a path specification. | 109 path_spec (PathSpec): a path specification. |
108 | 110 |
109 Returns: | 111 Returns: |
110 CPIOArchiveFileEntry: CPIO archive file entry or None if not available. | 112 CPIOArchiveFileEntry: CPIO archive file entry or None if not available. |
111 | 113 |
112 Raises: | 114 Raises: |
113 PathSpecError: if the path specification is incorrect. | 115 PathSpecError: if the path specification is incorrect. |
114 """ | 116 """ |
115 location = getattr(path_spec, u'location', None) | 117 location = getattr(path_spec, 'location', None) |
116 if location is None: | 118 if location is None: |
117 raise errors.PathSpecError(u'Path specification missing location.') | 119 raise errors.PathSpecError('Path specification missing location.') |
118 | 120 |
119 if not location.startswith(self.LOCATION_ROOT): | 121 if not location.startswith(self.LOCATION_ROOT): |
120 raise errors.PathSpecError(u'Invalid location in path specification.') | 122 raise errors.PathSpecError('Invalid location in path specification.') |
121 | 123 |
122 if len(location) == 1: | 124 if len(location) == 1: |
123 return | 125 return |
124 | 126 |
125 return self._cpio_archive_file.GetFileEntryByPath(location[1:]) | 127 return self._cpio_archive_file.GetFileEntryByPath(location[1:]) |
126 | 128 |
127 def GetFileEntryByPathSpec(self, path_spec): | 129 def GetFileEntryByPathSpec(self, path_spec): |
128 """Retrieves a file entry for a path specification. | 130 """Retrieves a file entry for a path specification. |
129 | 131 |
130 Args: | 132 Args: |
131 path_spec: a path specification (instance of PathSpec). | 133 path_spec: a path specification (instance of PathSpec). |
132 | 134 |
133 Returns: | 135 Returns: |
134 A file entry (instance of vfs.CPIOFileEntry) or None. | 136 A file entry (instance of vfs.CPIOFileEntry) or None. |
135 """ | 137 """ |
136 cpio_archive_file_entry = None | 138 cpio_archive_file_entry = None |
137 location = getattr(path_spec, u'location', None) | 139 location = getattr(path_spec, 'location', None) |
138 | 140 |
139 if (location is None or | 141 if (location is None or |
140 not location.startswith(self.LOCATION_ROOT)): | 142 not location.startswith(self.LOCATION_ROOT)): |
141 return | 143 return |
142 | 144 |
143 if len(location) == 1: | 145 if len(location) == 1: |
144 return dfvfs.vfs.cpio_file_entry.CPIOFileEntry( | 146 return dfvfs.vfs.cpio_file_entry.CPIOFileEntry( |
145 self._resolver_context, self, path_spec, is_root=True, | 147 self._resolver_context, self, path_spec, is_root=True, |
146 is_virtual=True) | 148 is_virtual=True) |
147 | 149 |
148 cpio_archive_file_entry = self._cpio_archive_file.GetFileEntryByPath( | 150 cpio_archive_file_entry = self._cpio_archive_file.GetFileEntryByPath( |
149 location[1:]) | 151 location[1:]) |
150 if cpio_archive_file_entry is None: | 152 if cpio_archive_file_entry is None: |
151 return | 153 return |
152 return dfvfs.vfs.cpio_file_entry.CPIOFileEntry( | 154 return dfvfs.vfs.cpio_file_entry.CPIOFileEntry( |
153 self._resolver_context, self, path_spec, | 155 self._resolver_context, self, path_spec, |
154 cpio_archive_file_entry=cpio_archive_file_entry) | 156 cpio_archive_file_entry=cpio_archive_file_entry) |
155 | 157 |
156 def GetRootFileEntry(self): | 158 def GetRootFileEntry(self): |
157 """Retrieves the root file entry. | 159 """Retrieves the root file entry. |
158 | 160 |
159 Returns: | 161 Returns: |
160 A file entry (instance of vfs.FileEntry). | 162 A file entry (instance of vfs.FileEntry). |
161 """ | 163 """ |
162 path_spec = cpio_path_spec.CPIOPathSpec( | 164 path_spec = cpio_path_spec.CPIOPathSpec( |
163 location=self.LOCATION_ROOT, parent=self._path_spec.parent) | 165 location=self.LOCATION_ROOT, parent=self._path_spec.parent) |
164 return self.GetFileEntryByPathSpec(path_spec) | 166 return self.GetFileEntryByPathSpec(path_spec) |
LEFT | RIGHT |