Implement truncate, utimens and use pathlib to handle paths

This commit is contained in:
Tanner Collin 2017-10-09 22:27:52 -06:00
parent 4d8c20e274
commit 2a2e608bde
2 changed files with 34 additions and 36 deletions

View File

@ -60,6 +60,11 @@ class ItemManager:
uuid=item['uuid']) uuid=item['uuid'])
return notes return notes
def touchNote(self, uuid):
item = self.items[uuid]
item['dirty'] = True
self.syncItems()
def writeNote(self, uuid, text): def writeNote(self, uuid, text):
item = self.items[uuid] item = self.items[uuid]
item['content']['text'] = text.strip() item['content']['text'] = text.strip()
@ -77,7 +82,7 @@ class ItemManager:
updated_at=time, updated_at=time,
enc_item_key='', enc_item_key='',
content=content) content=content)
return 0 self.syncItems()
def deleteNote(self, uuid): def deleteNote(self, uuid):
item = self.items[uuid] item = self.items[uuid]

View File

@ -8,6 +8,7 @@ import os
from stat import S_IFDIR, S_IFREG from stat import S_IFDIR, S_IFREG
from sys import argv, exit from sys import argv, exit
from datetime import datetime from datetime import datetime
from pathlib import PurePath
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
from itemmanager import ItemManager from itemmanager import ItemManager
@ -15,6 +16,7 @@ from itemmanager import ItemManager
class StandardNotesFUSE(LoggingMixIn, Operations): class StandardNotesFUSE(LoggingMixIn, Operations):
def __init__(self, sn_api, path='.'): def __init__(self, sn_api, path='.'):
self.item_manager = ItemManager(sn_api) self.item_manager = ItemManager(sn_api)
self.notes = self.item_manager.getNotes()
self.uid = os.getuid() self.uid = os.getuid()
self.gid = os.getgid() self.gid = os.getgid()
@ -28,28 +30,29 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
st_mtime=now, st_atime=now, st_nlink=1, st_mtime=now, st_atime=now, st_nlink=1,
st_uid=self.uid, st_gid=self.gid) st_uid=self.uid, st_gid=self.gid)
def _pathToNote(self, path):
pp = PurePath(path)
note_name = pp.parts[1]
note = self.notes[note_name]
return note, note['uuid']
def getattr(self, path, fh=None): def getattr(self, path, fh=None):
self.notes = self.item_manager.getNotes() self.notes = self.item_manager.getNotes()
st = self.note_stat
path_parts = path.split('/')
note_name = path_parts[1]
if path == '/': if path == '/':
return self.dir_stat return self.dir_stat
elif note_name in self.notes:
note = self.notes[note_name] try:
note, uuid = self._pathToNote(path)
st = self.note_stat
st['st_size'] = len(note['text']) st['st_size'] = len(note['text'])
st['st_ctime'] = iso8601.parse_date(note['created']).timestamp() st['st_ctime'] = iso8601.parse_date(note['created']).timestamp()
st['st_mtime'] = iso8601.parse_date(note['modified']).timestamp() st['st_mtime'] = iso8601.parse_date(note['modified']).timestamp()
return st return st
else: except KeyError:
raise FuseOSError(errno.ENOENT) raise FuseOSError(errno.ENOENT)
def readdir(self, path, fh): def readdir(self, path, fh):
self.notes = self.item_manager.getNotes()
dirents = ['.', '..'] dirents = ['.', '..']
if path == '/': if path == '/':
@ -57,21 +60,17 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
return dirents return dirents
def read(self, path, size, offset, fh): def read(self, path, size, offset, fh):
self.notes = self.item_manager.getNotes() note, uuid = self._pathToNote(path)
path_parts = path.split('/')
note_name = path_parts[1]
note = self.notes[note_name]
return note['text'][offset : offset + size].encode() return note['text'][offset : offset + size].encode()
def write(self, path, data, offset, fh): def truncate(self, path, length, fh=None):
self.notes = self.item_manager.getNotes() note, uuid = self._pathToNote(path)
text = note['text'][:length]
path_parts = path.split('/') self.item_manager.writeNote(uuid, text)
note_name = path_parts[1]
note = self.notes[note_name] def write(self, path, data, offset, fh):
uuid = note['uuid'] note, uuid = self._pathToNote(path)
try: try:
text = note['text'][:offset] + data.decode() text = note['text'][:offset] + data.decode()
@ -98,21 +97,21 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
return 0 return 0
def unlink(self, path): def unlink(self, path):
self.notes = self.item_manager.getNotes() note, uuid = self._pathToNote(path)
path_parts = path.split('/')
note_name = path_parts[1]
note = self.notes[note_name]
uuid = note['uuid']
self.item_manager.deleteNote(uuid) self.item_manager.deleteNote(uuid)
return 0 return 0
def mkdir(self, path, mode): def mkdir(self, path, mode):
logging.error('Creation of directories is disabled.') logging.error('Creation of directories is disabled.')
raise FuseOSError(errno.EPERM) raise FuseOSError(errno.EPERM)
def utimens(self, path, times=None):
note, uuid = self._pathToNote(path)
self.item_manager.touchNote(uuid)
return 0
def chmod(self, path, mode): def chmod(self, path, mode):
return 0 return 0
@ -133,9 +132,3 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
def symlink(self, target, source): def symlink(self, target, source):
return 0 return 0
def truncate(self, path, length, fh=None):
return 0
def utimens(self, path, times=None):
return 0