Skip to content

Commit

Permalink
Add autosync capability
Browse files Browse the repository at this point in the history
  • Loading branch information
JelteF committed Feb 15, 2016
1 parent f2d14e8 commit 6bc1234
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions easyfuse/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import errno
import os
import threading

from .filesystem import Directory, File

Expand All @@ -29,7 +30,8 @@ class Operations(LlfuseOperations):
same.
"""

def __init__(self, dir_class=Directory, filesystem=None, *args, **kwargs):
def __init__(self, dir_class=Directory, filesystem=None, *args,
autosync_delay=3, **kwargs):
"""
Args
----
Expand All @@ -43,6 +45,10 @@ def __init__(self, dir_class=Directory, filesystem=None, *args, **kwargs):
cases. For performance a class could be used that has a dict like
interface to another storage option, such as a database.
autosync_delay: `int` or `float`
Automatically sync all dirty files after no `~.write` has occured
for this amount of seconds. If this is set to `None`, autosync will
be disabled.
"""

super().__init__(*args, **kwargs)
Expand All @@ -52,9 +58,35 @@ def __init__(self, dir_class=Directory, filesystem=None, *args, **kwargs):

self.fs = filesystem
self.dir_class = dir_class
self.autosync_delay = autosync_delay

self.dir_class('', filesystem, None, inode=ROOT_INODE)

_autosync_timer = None

def fullsync(self):
"""Sync all dirty files using `~.fsync`."""
self.fsyncdir(ROOT_INODE, True)
self._autosync_timer = None

def start_autosync_timer(self):
"""Start an autosync timer and cancel previously enabled ones.
This is done by calling `~.fsyncdir` on the `llfuse.ROOT_INODE`.
"""
if self.autosync_delay is not None:
# TODO: Do some locking
self.cancel_autosync_timer()
self._autosync_timer = threading.Timer(self.autosync_delay,
self.fullsync)
self._autosync_timer.start()

def cancel_autosync_timer(self):
"""Cancel a possibly initiated autosync timer."""
if self._autosync_timer is not None:
self._autosync_timer.cancel()
self._autosync_timer = None

def getattr(self, inode, ctx=None):
"""Basic gettatr method.
Expand Down Expand Up @@ -218,12 +250,14 @@ def setattr(self, inode, attr, fields, fh, ctx=None):
def write(self, inode, offset, buf):
"""A basic implementation of the `llfuse.Operations.write` method."""
logging.debug('write')

self.cancel_autosync_timer()
file = self.fs[inode]
original = file.content

file.content = original[:offset] + buf + original[offset + len(buf):]
file.update_modified()

self.start_autosync_timer()
return len(buf)

def unlink(self, parent_inode, name, ctx=None):
Expand Down

0 comments on commit 6bc1234

Please sign in to comment.