Skip to content

Commit

Permalink
mavftpfuse #129: cache file attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
vooon committed Apr 5, 2015
1 parent fd43333 commit 32f9013
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mavros/src/mavros/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def mkdir(path):

def rmdir(path):
"""Remove directory :path:"""
rmdir_ = _get_proxy('rmdi', FileRemoveDir)
rmdir_ = _get_proxy('rmdir', FileRemoveDir)
try:
ret = rmdir_(dir_path=path)
except rospy.ServiceException as ex:
Expand Down
41 changes: 31 additions & 10 deletions mavros_extras/scripts/mavftpfuse
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MavFtp(RosLoggingMixIn, Operations):
self._attr_cache = {}

@staticmethod
def _make_attr(file_entry):
def _make_attr(dir_path, file_entry):
"""
Make stat attr dict from FileEntry object.
Expand All @@ -68,7 +68,7 @@ class MavFtp(RosLoggingMixIn, Operations):
uid, gid, pid = fuse_get_context()

dir_mode, file_mode = S_IFDIR | 0555, S_IFREG | 0444
if file_entry.name.startswith('/fs/microsd'):
if dir_path.startswith('/fs/microsd'):
dir_mode, file_mode = S_IFDIR | 0755, S_IFREG | 0644

return {
Expand All @@ -78,14 +78,19 @@ class MavFtp(RosLoggingMixIn, Operations):
'st_gid': gid,
}

def _update_attr_cache(self, dir_path, fe_list):
rospy.logdebug("update attr cache for: %s", ', '.join((os.path.join(dir_path, fe.name) for fe in fe_list)))
for fe in fe_list:
key = os.path.join(dir_path, fe.name)
self._attr_cache[key] = MavFtp._make_attr(dir_path, fe)

def create(self, path, mode):
return ftp.open(path, 'cw')

def destroy(self, path):
pass

def getattr(self, path, fh=None):
# XXX add chaching!
if path == '/':
uid, gid, pid = fuse_get_context()
return {
Expand All @@ -94,29 +99,45 @@ class MavFtp(RosLoggingMixIn, Operations):
'st_gid': gid,
}

if self._attr_cache.has_key(path):
return self._attr_cache[path]

try:
fn = os.path.basename(path)
for it in ftp.listdir(os.path.dirname(path)):
if it.name == fn:
return MavFtp._make_attr(it)

raise FuseOSError(ENOENT)
dir_path = os.path.dirname(path)
fe_list = ftp.listdir(dir_path)
self._update_attr_cache(dir_path, fe_list)
return self._attr_cache[path]
except IOError as e:
raise OSError(e.errno, e.message)
except KeyError:
raise FuseOSError(ENOENT)

def readdir(self, path, fh):
try:
l = ftp.listdir(path)
return ['.', '..'] + [it.name for it in l if it not in ('.', '..')]
fe_list = ftp.listdir(path)
self._update_attr_cache(path, fe_list)
return ['.', '..'] + [fe.name for fe in fe_list if fe not in ('.', '..')]
except IOError as e:
raise OSError(e.errno, e.message)

def fsyncdir(self, path, datasync, fh):
for k in self._attr_cache.keys():
if k.startswith(path):
del self._attr_cache[k]

def mkdir(self, path, mode):
try:
ftp.mkdir(path)
except IOError as e:
raise OSError(e.errno, e.message)

def rmdir(self, path):
self.fsyncdir(path, None, None)
try:
ftp.rmdir(path)
except IOError as e:
raise OSError(e.errno, e.message)


def main():
Expand Down

0 comments on commit 32f9013

Please sign in to comment.