@@ -31,6 +31,7 @@ const { HLFilesystemOperation } = require('./definitions');
3131const { is_valid_path } = require ( '../validation' ) ;
3232const { HLRemove } = require ( './hl_remove' ) ;
3333const { LLMkdir } = require ( '../ll_operations/ll_mkdir' ) ;
34+ const { MemoryFSService } = require ( '../../modules/puterfs/customfs/MemoryFSService' ) ;
3435
3536class MkTree extends HLFilesystemOperation {
3637 static DESCRIPTION = `
@@ -287,7 +288,7 @@ class HLMkdir extends HLFilesystemOperation {
287288 // "top_parent" is the immediate parent of the target directory
288289 // (e.g: /home/foo/bar -> /home/foo)
289290 const top_parent = values . create_missing_parents
290- ? await this . _create_top_parent ( { top_parent : parent_node } )
291+ ? await this . _create_dir ( parent_node )
291292 : await this . _get_existing_top_parent ( { top_parent : parent_node } )
292293 ;
293294
@@ -299,6 +300,10 @@ class HLMkdir extends HLFilesystemOperation {
299300 } ) ;
300301 }
301302
303+ if ( values . dedupe_name ) {
304+ console . log ( 'DEDUPE NAME' , values . dedupe_name ) ;
305+ }
306+
302307 // `parent_node` becomes the parent of the last directory name
303308 // specified under `path`.
304309 parent_node = await this . _create_parents ( {
@@ -317,6 +322,10 @@ class HLMkdir extends HLFilesystemOperation {
317322
318323 await existing . fetchEntry ( ) ;
319324
325+ if ( values . dedupe_name ) {
326+ console . log ( 'DEDUPE NAME' , values . dedupe_name ) ;
327+ }
328+
320329 if ( existing . found ) {
321330 const { overwrite, dedupe_name, create_missing_parents } = values ;
322331 if ( overwrite ) {
@@ -331,13 +340,17 @@ class HLMkdir extends HLFilesystemOperation {
331340 } ) ;
332341 }
333342 else if ( dedupe_name ) {
334- const fsEntryFetcher = context . get ( 'services' ) . get ( 'fsEntryFetcher' ) ;
343+ if ( parent_node . provider instanceof MemoryFSService ) {
344+ console . log ( 'MEMORYFS DEDUPE' ) ;
345+ }
346+
347+ const fs = context . get ( 'services' ) . get ( 'filesystem' ) ;
348+ const parent_selector = parent_node . selector ;
335349 for ( let i = 1 ; ; i ++ ) {
336350 let try_new_name = `${ target_basename } (${ i } )` ;
337- const exists = await fsEntryFetcher . nameExistsUnderParent (
338- existing . entry . parent_uid , try_new_name
339- ) ;
340- if ( ! exists ) {
351+ const selector = new NodeChildSelector ( parent_selector , try_new_name ) ;
352+ const node = await fs . node ( selector ) ;
353+ if ( ! await node . exists ( ) ) {
341354 target_basename = try_new_name ;
342355 break ;
343356 }
@@ -468,28 +481,44 @@ class HLMkdir extends HLFilesystemOperation {
468481 return node ;
469482 }
470483
471- async _create_top_parent ( { top_parent } ) {
472- if ( await top_parent . exists ( ) ) {
473- if ( ! top_parent . entry . is_dir ) {
484+ /**
485+ * Creates a directory and all its ancestors.
486+ *
487+ * @param {FSNodeContext } dir - The directory to create.
488+ * @returns {Promise<FSNodeContext> } The created directory.
489+ */
490+ async _create_dir ( dir ) {
491+ console . log ( 'CREATING DIR' , dir . selector . describe ( ) ) ;
492+
493+ if ( await dir . exists ( ) ) {
494+ if ( ! dir . entry . is_dir ) {
474495 throw APIError . create ( 'dest_is_not_a_directory' ) ;
475496 }
476- return top_parent ;
497+ return dir ;
477498 }
478499
479500 const maybe_path_selector =
480- top_parent . get_selector_of_type ( NodePathSelector ) ;
501+ dir . get_selector_of_type ( NodePathSelector ) ;
481502
482503 if ( ! maybe_path_selector ) {
483504 throw APIError . create ( 'dest_does_not_exist' ) ;
484505 }
485506
486- const path = maybe_path_selector . value ;
507+ let path = maybe_path_selector . value ;
487508
488509 const fs = this . context . get ( 'services' ) . get ( 'filesystem' ) ;
489510
511+ let parent = await fs . node ( new RootNodeSelector ( ) ) ;
512+ // if ( mountpoint && mountpoint !== '/' ) {
513+ // parent = await fs.node(new NodePathSelector(mountpoint));
514+
515+ // // remove the mountpoint from the path
516+ // path = path.replace(mountpoint, '');
517+ // }
518+
490519 const tree_op = new MkTree ( ) ;
491520 await tree_op . run ( {
492- parent : await fs . node ( new RootNodeSelector ( ) ) ,
521+ parent : parent ,
493522 tree : [ path ] ,
494523 } ) ;
495524
0 commit comments