Skip to content

Commit 026a908

Browse files
XiaochenCuiKernelDeimos
authored andcommitted
fs: ll_mkdir -> provider.mkdir
1 parent 1b1d22e commit 026a908

File tree

2 files changed

+93
-104
lines changed

2 files changed

+93
-104
lines changed

src/backend/src/filesystem/ll_operations/ll_mkdir.js

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -31,112 +31,13 @@ class LLMkdir extends LLFilesystemOperation {
3131
}
3232

3333
async _run () {
34-
const { context } = this;
35-
const { parent, name } = this.values;
36-
37-
this.checkpoint('lock requested');
38-
this.log.noticeme('GET FSLOCK');
39-
const svc_fslock = context.get('services').get('fslock');
40-
this.log.noticeme('REQUESTING LOCK', {
41-
parent: await parent.get('path'),
42-
name,
43-
});
44-
const lock_handle = await svc_fslock.lock_child(
45-
await parent.get('path'),
46-
name,
47-
MODE_WRITE,
48-
);
49-
this.log.noticeme('GOT LOCK', {
50-
parent: await parent.get('path'),
34+
const { parent, name, immutable } = this.values;
35+
return parent.provider.mkdir({
36+
context: this.context,
37+
parent,
5138
name,
39+
immutable,
5240
});
53-
this.checkpoint('lock acquired');
54-
55-
try {
56-
return await this._locked_run();
57-
} finally {
58-
await lock_handle.unlock();
59-
}
60-
}
61-
async _locked_run () {
62-
const { _path, uuidv4 } = this.modules;
63-
const { context } = this;
64-
const { parent, name, immutable, actor } = this.values;
65-
66-
const ts = Math.round(Date.now() / 1000);
67-
const uid = uuidv4();
68-
const resourceService = context.get('services').get('resourceService');
69-
const svc_fsEntry = context.get('services').get('fsEntryService');
70-
const svc_event = context.get('services').get('event');
71-
const fs = context.get('services').get('filesystem');
72-
73-
this.field('fsentry-uid', uid);
74-
75-
const existing = await fs.node(
76-
new NodeChildSelector(parent.selector, name)
77-
);
78-
79-
if ( await existing.exists() ) {
80-
throw APIError.create('item_with_same_name_exists', null, {
81-
entry_name: name,
82-
});
83-
}
84-
85-
this.checkpoint('before acl');
86-
const svc_acl = context.get('services').get('acl');
87-
if ( ! await parent.exists() ) {
88-
throw APIError.create('subject_does_not_exist');
89-
}
90-
if ( ! await svc_acl.check(actor, parent, 'write') ) {
91-
throw await svc_acl.get_safe_acl_error(actor, parent, 'write');
92-
}
93-
94-
resourceService.register({
95-
uid,
96-
status: RESOURCE_STATUS_PENDING_CREATE,
97-
});
98-
99-
const raw_fsentry = {
100-
is_dir: 1,
101-
uuid: uid,
102-
parent_uid: await parent.get('uid'),
103-
path: _path.join(await parent.get('path'), name),
104-
user_id: actor.type.user.id,
105-
name,
106-
created: ts,
107-
accessed: ts,
108-
modified: ts,
109-
immutable: immutable ?? false,
110-
...(this.values.thumbnail ? {
111-
thumbnail: this.values.thumbnail,
112-
} : {}),
113-
};
114-
115-
this.log.debug('creating fsentry', { fsentry: raw_fsentry })
116-
117-
this.checkpoint('about to enqueue insert');
118-
const entryOp = await svc_fsEntry.insert(raw_fsentry);
119-
120-
this.field('fsentry-created', false);
121-
122-
this.checkpoint('enqueued insert');
123-
// Asynchronous behaviour temporarily disabled
124-
// (async () => {
125-
await entryOp.awaitDone();
126-
this.log.debug('finished creating fsentry', { uid })
127-
resourceService.free(uid);
128-
this.field('fsentry-created', true);
129-
// })();
130-
131-
const node = await fs.node(new NodeUIDSelector(uid));
132-
133-
svc_event.emit('fs.create.directory', {
134-
node,
135-
context: Context.get(),
136-
});
137-
138-
this.checkpoint('returning node');
139-
return node
14041
}
14142
}
14243

src/backend/src/modules/puterfs/lib/PuterFSProvider.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const { ParallelTasks } = require('../../../util/otelutil');
3131

3232
const { TYPE_DIRECTORY } = require('../../../filesystem/FSNodeContext');
3333
const APIError = require('../../../api/APIError');
34+
const { MODE_WRITE } = require('../../../services/fs/FSLockService');
3435

3536
class PuterFSProvider extends putility.AdvancedBase {
3637
static MODULES = {
@@ -429,6 +430,93 @@ class PuterFSProvider extends putility.AdvancedBase {
429430

430431
await tasks.awaitAll();
431432
}
433+
434+
/**
435+
* Create a new directory.
436+
*
437+
* @param {Object} param
438+
* @param {Context} param.context
439+
* @param {FSNode} param.parent
440+
* @param {string} param.name
441+
* @param {boolean} param.immutable
442+
* @returns {Promise<FSNode>}
443+
*/
444+
async mkdir({ context, parent, name, immutable}) {
445+
const { actor, thumbnail } = context.values;
446+
447+
const svc_fslock = context.get('services').get('fslock');
448+
const lock_handle = await svc_fslock.lock_child(
449+
await parent.get('path'),
450+
name,
451+
MODE_WRITE,
452+
);
453+
454+
try {
455+
const { _path, uuidv4 } = this.modules;
456+
457+
const ts = Math.round(Date.now() / 1000);
458+
const uid = uuidv4();
459+
const resourceService = context.get('services').get('resourceService');
460+
const svc_fsEntry = context.get('services').get('fsEntryService');
461+
const svc_event = context.get('services').get('event');
462+
const fs = context.get('services').get('filesystem');
463+
464+
const existing = await fs.node(
465+
new NodeChildSelector(parent.selector, name)
466+
);
467+
468+
if (await existing.exists()) {
469+
throw APIError.create('item_with_same_name_exists', null, {
470+
entry_name: name,
471+
});
472+
}
473+
474+
const svc_acl = context.get('services').get('acl');
475+
if (! await parent.exists()) {
476+
throw APIError.create('subject_does_not_exist');
477+
}
478+
if (! await svc_acl.check(actor, parent, 'write')) {
479+
throw await svc_acl.get_safe_acl_error(actor, parent, 'write');
480+
}
481+
482+
resourceService.register({
483+
uid,
484+
status: RESOURCE_STATUS_PENDING_CREATE,
485+
});
486+
487+
const raw_fsentry = {
488+
is_dir: 1,
489+
uuid: uid,
490+
parent_uid: await parent.get('uid'),
491+
path: _path.join(await parent.get('path'), name),
492+
user_id: actor.type.user.id,
493+
name,
494+
created: ts,
495+
accessed: ts,
496+
modified: ts,
497+
immutable: immutable ?? false,
498+
...(thumbnail ? {
499+
thumbnail: thumbnail,
500+
} : {}),
501+
};
502+
503+
const entryOp = await svc_fsEntry.insert(raw_fsentry);
504+
505+
await entryOp.awaitDone();
506+
resourceService.free(uid);
507+
508+
const node = await fs.node(new NodeUIDSelector(uid));
509+
510+
svc_event.emit('fs.create.directory', {
511+
node,
512+
context: Context.get(),
513+
});
514+
515+
return node
516+
} finally {
517+
await lock_handle.unlock();
518+
}
519+
}
432520
}
433521

434522
module.exports = {

0 commit comments

Comments
 (0)