mirror of
https://github.com/HeyPuter/puter.git
synced 2026-01-05 20:50:22 -06:00
dev(puterfs): move quick_check to extension
This commit is contained in:
@@ -23,6 +23,7 @@ const svc_trace = extension.import('service:traceService');
|
||||
// TODO: these services ought to be part of this extension
|
||||
const svc_size = extension.import('service:sizeService');
|
||||
const svc_fsEntry = extension.import('service:fsEntryService');
|
||||
const svc_fsEntryFetcher = extension.import('service:fsEntryFetcher');
|
||||
|
||||
// TODO: depending on mountpoint service will not be necessary
|
||||
// once the storage provider is moved to this extension
|
||||
@@ -47,7 +48,51 @@ const {
|
||||
TYPE_DIRECTORY,
|
||||
} = extension.import('core').fs;
|
||||
|
||||
const {
|
||||
NodeChildSelector,
|
||||
NodeUIDSelector,
|
||||
NodeInternalIDSelector,
|
||||
|
||||
} = extension.import('core').fs.selectors;
|
||||
|
||||
class PuterFSProvider {
|
||||
/**
|
||||
* Check if a given node exists.
|
||||
*
|
||||
* @param {Object} param
|
||||
* @param {NodeSelector} param.selector - The selector used for checking.
|
||||
* @returns {Promise<boolean>} - True if the node exists, false otherwise.
|
||||
*/
|
||||
async quick_check ({
|
||||
selector,
|
||||
}) {
|
||||
// shortcut: has full path
|
||||
if ( selector?.path ) {
|
||||
const entry = await svc_fsEntryFetcher.findByPath(selector.path);
|
||||
return Boolean(entry);
|
||||
}
|
||||
|
||||
// shortcut: has uid
|
||||
if ( selector?.uid ) {
|
||||
const entry = await svc_fsEntryFetcher.findByUID(selector.uid);
|
||||
return Boolean(entry);
|
||||
}
|
||||
|
||||
// shortcut: parent uid + child name
|
||||
if ( selector instanceof NodeChildSelector && selector.parent instanceof NodeUIDSelector ) {
|
||||
return await svc_fsEntryFetcher.nameExistsUnderParent(selector.parent.uid,
|
||||
selector.name);
|
||||
}
|
||||
|
||||
// shortcut: parent id + child name
|
||||
if ( selector instanceof NodeChildSelector && selector.parent instanceof NodeInternalIDSelector ) {
|
||||
return await svc_fsEntryFetcher.nameExistsUnderParentID(selector.parent.id,
|
||||
selector.name);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async unlink ({ context, node, options = {} }) {
|
||||
if ( await node.get('type') === TYPE_DIRECTORY ) {
|
||||
throw new APIError(409, 'Cannot unlink a directory.');
|
||||
|
||||
@@ -89,36 +89,8 @@ class PuterFSProvider extends putility.AdvancedBase {
|
||||
async quick_check ({
|
||||
selector,
|
||||
}) {
|
||||
// a wrapper that access underlying database directly
|
||||
const fsEntryFetcher = this.#services.get('fsEntryFetcher');
|
||||
|
||||
// shortcut: has full path
|
||||
if ( selector?.path ) {
|
||||
const entry = await fsEntryFetcher.findByPath(selector.path);
|
||||
return Boolean(entry);
|
||||
}
|
||||
|
||||
// shortcut: has uid
|
||||
if ( selector?.uid ) {
|
||||
const entry = await fsEntryFetcher.findByUID(selector.uid);
|
||||
return Boolean(entry);
|
||||
}
|
||||
|
||||
// shortcut: parent uid + child name
|
||||
if ( selector instanceof NodeChildSelector && selector.parent instanceof NodeUIDSelector ) {
|
||||
return await fsEntryFetcher.nameExistsUnderParent(selector.parent.uid,
|
||||
selector.name);
|
||||
}
|
||||
|
||||
// shortcut: parent id + child name
|
||||
if ( selector instanceof NodeChildSelector && selector.parent instanceof NodeInternalIDSelector ) {
|
||||
return await fsEntryFetcher.nameExistsUnderParentID(selector.parent.id,
|
||||
selector.name);
|
||||
}
|
||||
|
||||
// TODO (xiaochen): we should fallback to stat but we cannot at this moment
|
||||
// since stat requires a valid `FSNodeContext` argument.
|
||||
return false;
|
||||
console.error('This .quick_check should not be called!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async stat ({
|
||||
|
||||
Reference in New Issue
Block a user