standardnotes-fs/sn_fuse.py

144 lines
3.7 KiB
Python
Raw Normal View History

2017-10-01 23:11:39 +00:00
from __future__ import print_function, absolute_import, division
import errno
2017-10-04 06:34:27 +00:00
import iso8601
2017-10-01 23:11:39 +00:00
import logging
import os
from stat import S_IFDIR, S_IFREG
from sys import argv, exit
2017-10-05 02:36:27 +00:00
from datetime import datetime
2017-10-01 23:11:39 +00:00
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
2017-10-04 06:34:27 +00:00
from itemmanager import ItemManager
2017-10-01 23:11:39 +00:00
2017-10-07 20:38:59 +00:00
class StandardNotesFUSE(LoggingMixIn, Operations):
def __init__(self, sn_api, path='.'):
self.item_manager = ItemManager(sn_api)
2017-10-01 23:11:39 +00:00
self.uid = os.getuid()
self.gid = os.getgid()
2017-10-05 02:36:27 +00:00
now = datetime.now().timestamp()
2017-10-01 23:11:39 +00:00
self.dir_stat = dict(st_mode=(S_IFDIR | 0o755), st_ctime=now,
st_mtime=now, st_atime=now, st_nlink=2,
st_uid=self.uid, st_gid=self.gid)
self.note_stat = dict(st_mode=(S_IFREG | 0o644), st_ctime=now,
st_mtime=now, st_atime=now, st_nlink=1,
st_uid=self.uid, st_gid=self.gid)
def getattr(self, path, fh=None):
2017-10-05 02:36:27 +00:00
self.notes = self.item_manager.getNotes()
2017-10-01 23:11:39 +00:00
st = self.note_stat
path_parts = path.split('/')
note_name = path_parts[1]
if path == '/':
return self.dir_stat
elif note_name in self.notes:
2017-10-04 06:34:27 +00:00
note = self.notes[note_name]
st['st_size'] = len(note['text'])
st['st_ctime'] = iso8601.parse_date(note['created']).timestamp()
st['st_mtime'] = iso8601.parse_date(note['modified']).timestamp()
2017-10-01 23:11:39 +00:00
return st
else:
raise FuseOSError(errno.ENOENT)
def readdir(self, path, fh):
2017-10-05 02:36:27 +00:00
self.notes = self.item_manager.getNotes()
2017-10-01 23:11:39 +00:00
dirents = ['.', '..']
if path == '/':
dirents.extend(list(self.notes.keys()))
return dirents
def read(self, path, size, offset, fh):
2017-10-05 02:36:27 +00:00
self.notes = self.item_manager.getNotes()
2017-10-01 23:11:39 +00:00
path_parts = path.split('/')
note_name = path_parts[1]
2017-10-04 06:34:27 +00:00
note = self.notes[note_name]
2017-10-01 23:11:39 +00:00
2017-10-04 06:34:27 +00:00
return note['text'][offset : offset + size].encode()
2017-10-01 23:11:39 +00:00
2017-10-05 02:36:27 +00:00
def write(self, path, data, offset, fh):
self.notes = self.item_manager.getNotes()
path_parts = path.split('/')
note_name = path_parts[1]
note = self.notes[note_name]
text = note['text'][:offset] + data.decode()
uuid = note['uuid']
self.item_manager.writeNote(uuid, text)
return len(data)
2017-10-05 07:05:47 +00:00
def create(self, path, mode):
path_parts = path.split('/')
note_name = path_parts[1]
2017-10-08 21:30:46 +00:00
# disallow hidden files (usually editor / OS files)
if note_name[0] == '.':
raise FuseOSError(errno.EPERM)
2017-10-05 07:05:47 +00:00
now = datetime.utcnow().isoformat()[:-3] + 'Z' # hack
self.item_manager.createNote(note_name, now)
2017-10-01 23:11:39 +00:00
return 0
2017-10-06 03:36:12 +00:00
def unlink(self, path):
self.notes = self.item_manager.getNotes()
path_parts = path.split('/')
note_name = path_parts[1]
note = self.notes[note_name]
uuid = note['uuid']
self.item_manager.deleteNote(uuid)
return 0
2017-10-05 07:05:47 +00:00
def chmod(self, path, mode):
2017-10-01 23:11:39 +00:00
return 0
2017-10-05 07:05:47 +00:00
def chown(self, path, uid, gid):
2017-10-01 23:11:39 +00:00
return 0
def destroy(self, path):
return 0
def mkdir(self, path, mode):
return 0
def readlink(self, path):
return 0
def rename(self, old, new):
return 0
def rmdir(self, path):
return 0
def symlink(self, target, source):
return 0
def truncate(self, path, length, fh=None):
return 0
def utimens(self, path, times=None):
return 0
if __name__ == '__main__':
if len(argv) != 2:
print('usage: %s <mountpoint>' % argv[0])
exit(1)
logging.basicConfig(level=logging.DEBUG)
2017-10-07 20:38:59 +00:00
fuse = FUSE(StandardNotesFUSE(), argv[1], foreground=True, nothreads=True)