Skip to content

Commit

Permalink
Changes to replace stat with attributes log2timeline#52
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Jun 26, 2017
1 parent ee20c5e commit c1bf266
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 178 deletions.
8 changes: 4 additions & 4 deletions dfvfs/vfs/cpio_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def __init__(self, resolver_context, encoding=u'utf-8'):
"""Initializes a file system object.
Args:
resolver_context: the resolver context (instance of resolver.Context).
encoding: optional file entry name encoding.
resolver_context (Context): resolver context.
encoding (Optional[str]): file entry name encoding.
"""
super(CPIOFileSystem, self).__init__(resolver_context)
self._cpio_archive_file = None
Expand All @@ -46,8 +46,8 @@ def _Open(self, path_spec, mode='rb'):
"""Opens the file system object defined by path specification.
Args:
path_spec: a path specification (instance of PathSpec).
mode: optional file access mode. The default is 'rb' read-only binary.
path_spec (PathSpec): path specification.
mode (Optional[str]): file access mode.
Raises:
AccessError: if the access to open the file was denied.
Expand Down
83 changes: 54 additions & 29 deletions dfvfs/vfs/fake_file_entry.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# -*- coding: utf-8 -*-
"""The fake file entry implementation."""

from dfdatetime import fake_time as dfdatetime_fake_time

from dfvfs.lib import definitions
from dfvfs.file_io import fake_file_io
from dfvfs.path import fake_path_spec
from dfvfs.vfs import file_entry


class FakeDirectory(file_entry.Directory):
"""Class that implements a fake directory object."""
"""Fake file system directory."""

def _EntriesGenerator(self):
"""Retrieves directory entries.
Expand All @@ -17,7 +19,7 @@ def _EntriesGenerator(self):
a generator is more memory efficient.
Yields:
A path specification (instance of path.FakePathSpec).
FakePathSpec: a path specification.
"""
location = getattr(self.path_spec, u'location', None)
if location is None:
Expand All @@ -42,50 +44,69 @@ def _EntriesGenerator(self):


class FakeFileEntry(file_entry.FileEntry):
"""Class that implements a fake file entry object."""
"""Fake file system file entry."""

TYPE_INDICATOR = definitions.TYPE_INDICATOR_FAKE

def __init__(self, resolver_context, file_system, path_spec, is_root=False):
"""Initializes the file entry object.
def __init__(
self, resolver_context, file_system, path_spec, file_entry_type=None,
is_root=False):
"""Initializes a file entry.
Args:
resolver_context: the resolver context (instance of resolver.Context).
file_system: the file system object (instance of FileSystem).
path_spec: the path specification object (instance of PathSpec).
is_root: optional boolean value to indicate if the file entry is
the root file entry of the corresponding file system.
resolver_context (Context): resolver context.
file_system (FileSystem): file system.
path_spec (PathSpec): path specification.
file_entry_type (Optional[str]): file entry type.
is_root (Optional[bool]): True if the file entry is the root file entry
of the corresponding file system.
"""
super(FakeFileEntry, self).__init__(
resolver_context, file_system, path_spec, is_root=is_root,
is_virtual=True)
self._date_time = dfdatetime_fake_time.FakeTime()
self._name = None
self._type = file_entry_type

def _GetDirectory(self):
"""Retrieves a directory.
Returns:
A directory object (instance of Directory) or None.
FakeDirectoyr: a directory or None if not available.
"""
if self._stat_object is None:
self._stat_object = self._GetStat()

if (self._stat_object and
self._stat_object.type == self._stat_object.TYPE_DIRECTORY):
if self._type == definitions.FILE_ENTRY_TYPE_DIRECTORY:
return FakeDirectory(self._file_system, self.path_spec)
return

def _GetStat(self):
"""Retrieves the stat object (instance of vfs.VFSStat)."""
"""Retrieves information about the file entry.
Returns:
VFSStat: a stat object.
"""
stat_object = super(FakeFileEntry, self)._GetStat()

location = getattr(self.path_spec, u'location', None)
if location is None:
return
if location:
file_data = self._file_system.GetDataByPath(location)

return self._file_system.GetStatObjectByPath(location)
if file_data is not None:
stat_object.size = len(file_data)

return stat_object

@property
def access_time(self):
"""dfdatetime.DateTimeValues: access time or None if not available."""
return self._date_time

@property
def change_time(self):
"""dfdatetime.DateTimeValues: change time or None if not available."""
return self._date_time

@property
def link(self):
"""The full path of the linked file entry."""
"""str: full path of the linked file entry."""
if not self.IsLink():
return u''

Expand All @@ -95,9 +116,14 @@ def link(self):

return self._file_system.GetDataByPath(location)

@property
def modification_time(self):
"""dfdatetime.DateTimeValues: modification time or None if not available."""
return self._date_time

@property
def name(self):
"""The name of the file entry, which does not include the full path."""
"""str: name of the file entry, without the full path."""
if self._name is None:
location = getattr(self.path_spec, u'location', None)
if location is not None:
Expand All @@ -106,7 +132,7 @@ def name(self):

@property
def sub_file_entries(self):
"""The sub file entries (generator of instance of vfs.FakeFileEntry)."""
"""generator[FakeFileEntry]: sub file entries."""
if self._directory is None:
self._directory = self._GetDirectory()

Expand All @@ -119,12 +145,11 @@ def GetFileObject(self, data_stream_name=u''):
"""Retrieves the file-like object.
Args:
data_stream_name: optional data stream name. The default is
an empty string which represents the default
data stream.
data_stream_name (Optional[str]): name of the data stream, where an empty
string represents the default data stream.
Returns:
A file-like object (instance of file_io.FileIO) or None.
FakeFileIO: a file-like object or None if not available.
Raises:
IOError: if the file entry is not a file.
Expand All @@ -148,7 +173,7 @@ def GetParentFileEntry(self):
"""Retrieves the root file entry.
Returns:
The parent file entry (instance of FileEntry) or None.
FakeFileEntry: parent file entry or None if not available.
"""
location = getattr(self.path_spec, u'location', None)
if location is None:
Expand Down
63 changes: 9 additions & 54 deletions dfvfs/vfs/fake_file_system.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# -*- coding: utf-8 -*-
"""The fake file system implementation."""

from dfdatetime import fake_time as dfdatetime_fake_time

from dfvfs.lib import definitions
from dfvfs.lib import errors
from dfvfs.path import fake_path_spec
from dfvfs.vfs import file_system
from dfvfs.vfs import fake_file_entry
from dfvfs.vfs import vfs_stat


class FakeFileSystem(file_system.FileSystem):
"""Class that implements a fake file system object."""
"""Fake file system."""

LOCATION_ROOT = u'/'

Expand All @@ -22,7 +19,7 @@ def __init__(self, resolver_context):
"""Initializes a file system object.
Args:
resolver_context: the resolver context (instance of resolver.Context).
resolver_context (Context): resolver context.
"""
super(FakeFileSystem, self).__init__(resolver_context)
self._paths = {}
Expand All @@ -41,7 +38,7 @@ def _Open(self, path_spec, mode='rb'):
"""Opens the file system object defined by path specification.
Args:
path_spec (PathSpec): a path specification.
path_spec (PathSpec): path specification.
mode (Optional[str]): file access mode.
Raises:
Expand Down Expand Up @@ -79,39 +76,14 @@ def AddFileEntry(
if link_data and file_entry_type != definitions.FILE_ENTRY_TYPE_LINK:
raise ValueError(u'Link data set for non-link file entry type.')

stat_object = vfs_stat.VFSStat()

# File data stat information.
if file_data is not None:
stat_object.size = len(file_data)

# Date and time stat information.
date_time_values = dfdatetime_fake_time.FakeTime()

stat_time, stat_time_nano = date_time_values.CopyToStatTimeTuple()
if stat_time is not None:
stat_object.atime = stat_time
stat_object.atime_nano = stat_time_nano
stat_object.ctime = stat_time
stat_object.ctime_nano = stat_time_nano
stat_object.mtime = stat_time
stat_object.mtime_nano = stat_time_nano

# Ownership and permissions stat information.

# File entry type stat information.
stat_object.type = file_entry_type

# Other stat information.

if file_data:
path_data = file_data
elif link_data:
path_data = link_data
else:
path_data = None

self._paths[path] = (stat_object, path_data)
self._paths[path] = (file_entry_type, path_data)

def FileEntryExistsByPath(self, path):
"""Determines if a file entry for a path exists.
Expand Down Expand Up @@ -160,12 +132,14 @@ def GetFileEntryByPath(self, path):
if path is None:
return

if not self.FileEntryExistsByPath(path):
file_entry_type, _ = self._paths.get(path, (None, None))
if not file_entry_type:
return

path_spec = fake_path_spec.FakePathSpec(location=path)
return fake_file_entry.FakeFileEntry(
self._resolver_context, self, path_spec)
self._resolver_context, self, path_spec,
file_entry_type=file_entry_type)

def GetFileEntryByPathSpec(self, path_spec):
"""Retrieves a file entry for a path specification.
Expand All @@ -177,14 +151,7 @@ def GetFileEntryByPathSpec(self, path_spec):
FileEntry: a file entry or None if not available.
"""
location = getattr(path_spec, u'location', None)
if location is None:
return

if not self.FileEntryExistsByPathSpec(path_spec):
return

return fake_file_entry.FakeFileEntry(
self._resolver_context, self, path_spec)
return self.GetFileEntryByPath(location)

def GetPaths(self):
"""Retrieves the paths dictionary.
Expand All @@ -202,15 +169,3 @@ def GetRootFileEntry(self):
"""
path_spec = fake_path_spec.FakePathSpec(location=self.LOCATION_ROOT)
return self.GetFileEntryByPathSpec(path_spec)

def GetStatObjectByPath(self, path):
"""Retrieves the stat object for a path.
Args:
path (str): path of the file entry.
Returns:
VFSStat: stat object or None.
"""
stat_object, _ = self._paths.get(path, (None, None))
return stat_object
Loading

0 comments on commit c1bf266

Please sign in to comment.