From 8e57578251638d2696c1c9e9af28fb3565321253 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Fri, 27 Nov 2020 13:18:37 -0600 Subject: [PATCH] Add support for fgetattr and symlinks --- app/enum.js | 9 ++++++++- app/models/fs/Node.model.js | 8 +++++--- app/ws/routes/fs.fgetattr.js | 31 +++++++++++++++++++++++++++++++ app/ws/routes/fs.mkdir.js | 4 ++-- app/ws/routes/fs.readlink.js | 24 ++++++++++++++++++++++++ app/ws/routes/fs.symlink.js | 31 +++++++++++++++++++++++++++++++ app/ws/routes/fs.unlink.js | 2 +- 7 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 app/ws/routes/fs.fgetattr.js create mode 100644 app/ws/routes/fs.readlink.js create mode 100644 app/ws/routes/fs.symlink.js diff --git a/app/enum.js b/app/enum.js index d745e39..32afee6 100644 --- a/app/enum.js +++ b/app/enum.js @@ -1,6 +1,13 @@ const NodeDescriptorType = { File: 'file', Directory: 'directory', + Symlink: 'symlink', } -module.exports = exports = { NodeDescriptorType } \ No newline at end of file +const NodeModeType = { + File: 33188, + Directory: 16877, + Symlink: 40960, +} + +module.exports = exports = { NodeDescriptorType, NodeModeType } \ No newline at end of file diff --git a/app/models/fs/Node.model.js b/app/models/fs/Node.model.js index 184de06..e30953f 100644 --- a/app/models/fs/Node.model.js +++ b/app/models/fs/Node.model.js @@ -1,6 +1,6 @@ const uuid = require('uuid').v4 const { Model } = require('flitter-orm') -const { NodeDescriptorType } = require('../../enum') +const { NodeDescriptorType, NodeModeType } = require('../../enum') class Node extends Model { static get services() { @@ -16,12 +16,14 @@ class Node extends Model { mtime: { type: Date, default: () => new Date }, atime: { type: Date, default: () => new Date }, ctime: { type: Date, default: () => new Date }, - mode: { type: Number, default: 33188 }, + mode: { type: Number, default: NodeModeType.File }, size: { type: Number, default: 0 }, descriptor_type: { type: String, default: NodeDescriptorType.File }, uploaded_file_id: String, + symlink_path: String, // If NodeDescriptorType.Symlink, the path to resolve + extended_attributes: [{ name: String, value: String, // base64 encoded binary buffer value @@ -50,7 +52,7 @@ class Node extends Model { if ( !root ) { root = new this({ pied_name: '/', - mode: 16877, + mode: NodeModeType.Directory, root: true, descriptor_type: NodeDescriptorType.Directory, }) diff --git a/app/ws/routes/fs.fgetattr.js b/app/ws/routes/fs.fgetattr.js new file mode 100644 index 0000000..2013f62 --- /dev/null +++ b/app/ws/routes/fs.fgetattr.js @@ -0,0 +1,31 @@ +const { Errors } = require('../../shared') +const { NodeDescriptorType } = require('../../enum') + +// TODO should this return size info for the temp write file? +module.exports = exports = async (message, di) => { + const Node = di.models.get('fs:Node') + const { descriptor } = message.data() + + const node_uuid = message.socket.session.file_descriptors?.[descriptor] + if ( !node_uuid ) { + return message.send_response( + message.fresh().error(Errors.NodeDoesNotExist) + ) + } + + const node = await Node.findOne({ + uuid: node_uuid, + deleted: false, + descriptor_type: NodeDescriptorType.File, + }) + + if ( !node ) { + return message.send_response( + message.fresh().error(Errors.NodeDoesNotExist) + ) + } + + message.send_response( + message.fresh().data({ node: node.to_api() }) + ) +} diff --git a/app/ws/routes/fs.mkdir.js b/app/ws/routes/fs.mkdir.js index beb21e7..fcbd1fc 100644 --- a/app/ws/routes/fs.mkdir.js +++ b/app/ws/routes/fs.mkdir.js @@ -1,5 +1,5 @@ const { Errors } = require('../../shared') -const { NodeDescriptorType } = require('../../enum') +const { NodeDescriptorType, NodeModeType } = require('../../enum') module.exports = exports = async (message, di) => { const Node = di.models.get('fs:Node') @@ -29,7 +29,7 @@ module.exports = exports = async (message, di) => { pied_name, pied_parent_path, overlay_name: message.socket.session.overlay_name || 'mainline', - mode: 16877, // TODO account for the mode from the client! + mode: NodeModeType.Directory, // TODO account for the mode from the client! descriptor_type: NodeDescriptorType.Directory, size: 100, }) diff --git a/app/ws/routes/fs.readlink.js b/app/ws/routes/fs.readlink.js new file mode 100644 index 0000000..faeb85e --- /dev/null +++ b/app/ws/routes/fs.readlink.js @@ -0,0 +1,24 @@ +const { Errors } = require('../../shared') +const { NodeDescriptorType } = require('../../enum') + +module.exports = exports = async (message, di) => { + const Node = di.models.get('fs:Node') + const { path } = message.data() + + const node = await Node.get_path(path, message.socket.session.overlay_name || 'mainline') + if ( !node ) { + return message.send_response( + message.fresh().error(Errors.NodeDoesNotExist) + ) + } + + if ( node.descriptor_type !== NodeDescriptorType.Symlink ) { + return message.send_response( + message.fresh().error(Errors.NodeAlreadyExists) + ) + } + + message.send_response( + message.fresh().data({ node: node.to_api(), link: node.symlink_path }) + ) +} diff --git a/app/ws/routes/fs.symlink.js b/app/ws/routes/fs.symlink.js new file mode 100644 index 0000000..d1d13bf --- /dev/null +++ b/app/ws/routes/fs.symlink.js @@ -0,0 +1,31 @@ +const { Errors } = require('../../shared') +const { NodeDescriptorType, NodeModeType } = require('../../enum') + +module.exports = exports = async (message, di) => { + const Node = di.models.get('fs:Node') + const { source, destination } = message.data() + + const destination_node = await Node.get_path(destination, message.socket.session.overlay_name || 'mainline') + if ( destination_node ) { + destination_node.deleted = true + await destination_node.save() + } + + const [pied_parent_path, pied_name] = Node.path_parts(destination) + const node = new Node({ + pied_name, + pied_parent_path, + overlay_name: message.socket.session.overlay_name || 'mainline', + descriptor_type: NodeDescriptorType.Symlink, + symlink_path: source, + mode: NodeModeType.Symlink, + }) + + await node.save() + +console.log('symlink node', node) + + message.send_response( + message.fresh().data({ node: node.to_api() }) + ) +} diff --git a/app/ws/routes/fs.unlink.js b/app/ws/routes/fs.unlink.js index 127f808..130351c 100644 --- a/app/ws/routes/fs.unlink.js +++ b/app/ws/routes/fs.unlink.js @@ -24,7 +24,7 @@ module.exports = exports = async (message, di) => { ) } - if ( node.descriptor_type !== NodeDescriptorType.File ) { + if ( node.descriptor_type === NodeDescriptorType.Directory ) { return message.send_response( message.fresh().error(Errors.IsDirectoryDescriptor) )