dev: decouple FSEntryFetcher by adding hasChild

Adding the hasChild method of FSNodeContext as well as a corresponding
method to filesystem providers is prerequisite to moving
FSEntryFetcher's logic into the new puterfs extension.
This commit is contained in:
KernelDeimos
2025-11-13 15:07:37 -05:00
committed by Eric Dubé
parent cdcce31d03
commit d3881f2440
5 changed files with 22 additions and 11 deletions
+11
View File
@@ -614,6 +614,17 @@ export default class PuterFSProvider {
return child_uuids;
}
async directory_has_name ({ parent, name }) {
const uid = await parent.get('uid');
/* eslint-disable */
let check_dupe = await db.read(
'SELECT `id` FROM `fsentries` WHERE `parent_uid` = ? AND name = ? LIMIT 1',
[uid, name],
);
/* eslint-enable */
return !!check_dupe[0];
}
/**
* Write a new file to the filesystem. Throws an error if the destination
* already exists.
@@ -614,6 +614,10 @@ module.exports = class FSNodeContext {
}
if ( key === 'uid' ) {
const uidSelector = this.get_selector_of_type(NodeUIDSelector);
if ( uidSelector ) {
return uidSelector.value;
}
await this.fetchEntry();
return this.uid;
}
@@ -737,6 +741,10 @@ module.exports = class FSNodeContext {
return await this.fs.node(new NodeChildSelector(this.selector, name));
}
async hasChild(name) {
return await this.provider.directory_has_name({ parent: this, name });
}
async getTarget() {
await this.fetchEntry();
@@ -173,14 +173,11 @@ class HLCopy extends HLFilesystemOperation {
}
if ( values.dedupe_name ) {
const fsEntryFetcher = context.get('services').get('fsEntryFetcher');
const target_ext = _path.extname(target_name);
const target_noext = _path.basename(target_name, target_ext);
for ( let i=1 ;; i++ ) {
const try_new_name = `${target_noext} (${i})${target_ext}`;
const exists = await fsEntryFetcher.nameExistsUnderParent(
parent.uid, try_new_name
);
const exists = await parent.hasChild(try_new_name);
if ( ! exists ) {
target_name = try_new_name;
break;
@@ -152,14 +152,11 @@ class HLMove extends HLFilesystemOperation {
}
if ( values.dedupe_name ) {
const svc_fsEntryFetcher = svc.get('fsEntryFetcher');
const target_ext = _path.extname(target_name);
const target_noext = _path.basename(target_name, target_ext);
for ( let i=1 ;; i++ ) {
const try_new_name = `${target_noext} (${i})${target_ext}`;
const exists = await svc_fsEntryFetcher.nameExistsUnderParent(
parent.uid, try_new_name
);
const exists = await parent.hasChild(try_new_name);
if ( ! exists ) {
target_name = try_new_name;
break;
@@ -251,9 +251,7 @@ class HLWrite extends HLFilesystemOperation {
const target_noext = _path.basename(target_name, target_ext);
for ( let i=1 ;; i++ ) {
const try_new_name = `${target_noext} (${i})${target_ext}`;
const exists = await fsEntryFetcher.nameExistsUnderParent(
parent.uid, try_new_name
);
const exists = await parent.hasChild(try_new_name);
if ( ! exists ) {
target_name = try_new_name;
break;