Skip to content

Commit

Permalink
Changed Ext2ImageFileTestCase to use preferred ext back-end log2timel…
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Nov 7, 2020
1 parent 1f6a983 commit 9104dd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
21 changes: 20 additions & 1 deletion dfvfs/resolver/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from __future__ import unicode_literals

import collections
import itertools

from dfvfs.lib import errors


Expand Down Expand Up @@ -62,7 +65,7 @@ def __init__(self, maximum_number_of_cached_values):

super(ObjectsCache, self).__init__()
self._maximum_number_of_cached_values = maximum_number_of_cached_values
self._values = {}
self._values = collections.OrderedDict()

def CacheObject(self, identifier, vfs_object):
"""Caches a VFS object.
Expand Down Expand Up @@ -129,6 +132,22 @@ def GetCacheValueByObject(self, vfs_object):

return None, None

def GetLastObject(self):
"""Retrieves the last cached object.
This method ignores the cache value reference count.
Returns:
object: the last cached VFS object or None if the cache is empty.
"""
if not self._values:
return None

# Get the last (or most recent added) cache value.
cache_value = next(itertools.islice(
self._values.values(), len(self._values) - 1, None))
return cache_value.vfs_object

def GetObject(self, identifier):
"""Retrieves a cached object based on the identifier.
Expand Down
5 changes: 5 additions & 0 deletions dfvfs/resolver/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def CacheFileSystem(self, path_spec, file_system):

def Empty(self):
"""Empties the caches."""
file_object = self._file_object_cache.GetLastObject()
while file_object:
file_object.close()
file_object = self._file_object_cache.GetLastObject()

self._file_object_cache.Empty()
self._file_system_cache.Empty()

Expand Down
2 changes: 2 additions & 0 deletions tests/resolver/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def testEmpty(self):
cache_object.Empty()
self.assertEqual(len(cache_object._values), 0)

# TODO: add tests for the GetLastObject method

def testGetObject(self):
"""Tests the GetObject method."""
cache_object = cache.ObjectsCache(1)
Expand Down

0 comments on commit 9104dd3

Please sign in to comment.