Use path_to_node for all operations.

This commit is contained in:
Roberto Rosario
2015-07-28 19:38:58 -04:00
parent a361311a29
commit 6039ea86ec

View File

@@ -24,9 +24,14 @@ logger = logging.getLogger(__name__)
class IndexFS(LoggingMixIn, Operations):
@staticmethod
def path_to_node(path, index):
def path_to_node(path, index, directory_only=True):
logger.debug('path: %s', path)
logger.debug('directory_only: %s', directory_only)
parts = path.split('/')
logger.debug('parts: %s', parts)
directory = index.instance_root
if len(parts) > 1 and parts[1] != '':
@@ -34,11 +39,22 @@ class IndexFS(LoggingMixIn, Operations):
try:
directory = directory.children.get(value=part)
except IndexInstanceNode.DoesNotExist:
print '{} does exists'.format(part)
try:
return directory.documents.get(label=part)
except Document.DoesNotExist:
logger.debug('%s does not exists', part)
if directory_only:
return None
else:
try:
result = directory.documents.get(label=part)
logger.debug('path %s is a valid file path', path)
return result
except Document.DoesNotExist:
logger.debug('path %s is a file, but is not found', path)
return None
logger.debug('directory: %s', directory)
#logger.debug('directory children: %s', get_children())
logger.debug('directory is root: %s', directory.is_root_node())
return directory
@@ -48,16 +64,17 @@ class IndexFS(LoggingMixIn, Operations):
self.index = Index.objects.first()
def getattr(self, path, fh=None):
result = IndexFS.path_to_node(path=path, index=self.index)
now = time()
result = IndexFS.path_to_node(path=path, index=self.index, directory_only=False)
if not result:
raise FuseOSError(ENOENT)
if isinstance(result, IndexInstanceNode):
now = time()
return dict(
st_mode=(S_IFDIR | 0755), st_ctime=now, st_mtime=now, st_atime=now, st_nlink=2
)
return {
'st_mode': (S_IFDIR | 0755), 'st_ctime': now, 'st_mtime': now,
'st_atime': now, 'st_nlink': 2
}
else:
now = time()
@@ -73,19 +90,23 @@ class IndexFS(LoggingMixIn, Operations):
return ''
def open(self, path, flags):
result = IndexFS.path_to_node(path=path, index=self.index)
result = IndexFS.path_to_node(path=path, index=self.index, directory_only=False)
if isinstance(result, Document):
self.fd_count += 1
self.fd[self.fd_count] = result.open()
return self.fd_count
return None
else:
raiseFuseOSError(ENOENT)
def read(self, path, size, offset, fh):
return self.fd[self.fd_count].read(size)
def readdir(self, path, fh):
logger.debug('path: %s', path)
'''
parts = path.split('/')
directory = self.index.instance_root
@@ -95,9 +116,17 @@ class IndexFS(LoggingMixIn, Operations):
try:
directory = directory.children.get(value=part)
except IndexInstanceNode.DoesNotExist:
print '{} does exists'.format(part)
logger.debug('%s does not exists', part)
raise FuseOSError(ENOENT)
logger.debug('directory: %s', directory)
'''
directory = IndexFS.path_to_node(path=path, index=self.index, directory_only=True)
if not directory:
raiseFuseOSError(ENOENT)
result = ['.', '..']
directories = directory.get_children().order_by('value')