-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fad0b71
commit bddb791
Showing
37 changed files
with
2,286 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The APFS file-like object implementation.""" | ||
|
||
from __future__ import unicode_literals | ||
|
||
import os | ||
|
||
from dfvfs.file_io import file_io | ||
from dfvfs.resolver import resolver | ||
|
||
|
||
class APFSFile(file_io.FileIO): | ||
"""File-like object using pyfsapfs.""" | ||
|
||
def __init__(self, resolver_context): | ||
"""Initializes a file-like object. | ||
Args: | ||
resolver_context (Context): resolver context. | ||
""" | ||
super(APFSFile, self).__init__(resolver_context) | ||
self._file_system = None | ||
self._fsapfs_data_stream = None | ||
self._fsapfs_file_entry = None | ||
|
||
def _Close(self): | ||
"""Closes the file-like object.""" | ||
self._fsapfs_data_stream = None | ||
self._fsapfs_file_entry = None | ||
|
||
self._file_system.Close() | ||
self._file_system = None | ||
|
||
def _Open(self, path_spec=None, mode='rb'): | ||
"""Opens the file-like object defined by path specification. | ||
Args: | ||
path_spec (PathSpec): path specification. | ||
mode (Optional[str]): file access mode. | ||
Raises: | ||
AccessError: if the access to open the file was denied. | ||
IOError: if the file-like object could not be opened. | ||
OSError: if the file-like object could not be opened. | ||
PathSpecError: if the path specification is incorrect. | ||
ValueError: if the path specification is invalid. | ||
""" | ||
if not path_spec: | ||
raise ValueError('Missing path specification.') | ||
|
||
data_stream = getattr(path_spec, 'data_stream', None) | ||
|
||
self._file_system = resolver.Resolver.OpenFileSystem( | ||
path_spec, resolver_context=self._resolver_context) | ||
|
||
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec) | ||
if not file_entry: | ||
raise IOError('Unable to open file entry.') | ||
|
||
fsapfs_data_stream = None | ||
fsapfs_file_entry = file_entry.GetAPFSFileEntry() | ||
if not fsapfs_file_entry: | ||
raise IOError('Unable to open APFS file entry.') | ||
|
||
if data_stream: | ||
fsapfs_data_stream = fsapfs_file_entry.get_alternate_data_stream_by_name( | ||
data_stream) | ||
if not fsapfs_data_stream: | ||
raise IOError('Unable to open data stream: {0:s}.'.format( | ||
data_stream)) | ||
|
||
self._fsapfs_data_stream = fsapfs_data_stream | ||
self._fsapfs_file_entry = fsapfs_file_entry | ||
|
||
# Note: that the following functions do not follow the style guide | ||
# because they are part of the file-like object interface. | ||
# pylint: disable=invalid-name | ||
|
||
def read(self, size=None): | ||
"""Reads a byte string from the file-like object at the current offset. | ||
The function will read a byte string of the specified size or | ||
all of the remaining data if no size was specified. | ||
Args: | ||
size (Optional[int]): number of bytes to read, where None is all | ||
remaining data. | ||
Returns: | ||
bytes: data read. | ||
Raises: | ||
IOError: if the read failed. | ||
OSError: if the read failed. | ||
""" | ||
if not self._is_open: | ||
raise IOError('Not opened.') | ||
|
||
if self._fsapfs_data_stream: | ||
return self._fsapfs_data_stream.read(size=size) | ||
return self._fsapfs_file_entry.read(size=size) | ||
|
||
def seek(self, offset, whence=os.SEEK_SET): | ||
"""Seeks to an offset within the file-like object. | ||
Args: | ||
offset (int): offset to seek to. | ||
whence (Optional(int)): value that indicates whether offset is an absolute | ||
or relative position within the file. | ||
Raises: | ||
IOError: if the seek failed. | ||
OSError: if the seek failed. | ||
""" | ||
if not self._is_open: | ||
raise IOError('Not opened.') | ||
|
||
if self._fsapfs_data_stream: | ||
self._fsapfs_data_stream.seek(offset, whence) | ||
else: | ||
self._fsapfs_file_entry.seek(offset, whence) | ||
|
||
def get_offset(self): | ||
"""Retrieves the current offset into the file-like object. | ||
Return: | ||
int: current offset into the file-like object. | ||
Raises: | ||
IOError: if the file-like object has not been opened. | ||
OSError: if the file-like object has not been opened. | ||
""" | ||
if not self._is_open: | ||
raise IOError('Not opened.') | ||
|
||
if self._fsapfs_data_stream: | ||
return self._fsapfs_data_stream.get_offset() | ||
return self._fsapfs_file_entry.get_offset() | ||
|
||
def get_size(self): | ||
"""Retrieves the size of the file-like object. | ||
Returns: | ||
int: size of the file-like object data. | ||
Raises: | ||
IOError: if the file-like object has not been opened. | ||
OSError: if the file-like object has not been opened. | ||
""" | ||
if not self._is_open: | ||
raise IOError('Not opened.') | ||
|
||
if self._fsapfs_data_stream: | ||
return self._fsapfs_data_stream.get_size() | ||
return self._fsapfs_file_entry.get_size() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Helper functions for APFS container support.""" | ||
|
||
from __future__ import unicode_literals | ||
|
||
|
||
def APFSContainerPathSpecGetVolumeIndex(path_spec): | ||
"""Retrieves the volume index from the path specification. | ||
Args: | ||
path_spec (PathSpec): path specification. | ||
Returns: | ||
int: volume index. | ||
""" | ||
volume_index = getattr(path_spec, 'volume_index', None) | ||
if volume_index is not None: | ||
return volume_index | ||
|
||
location = getattr(path_spec, 'location', None) | ||
if location is None or not location.startswith('/apfs'): | ||
return None | ||
|
||
try: | ||
volume_index = int(location[5:], 10) - 1 | ||
except (TypeError, ValueError): | ||
volume_index = None | ||
|
||
if volume_index is None or volume_index < 0 or volume_index > 99: | ||
volume_index = None | ||
|
||
return volume_index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.