MCPcopy Index your code
hub / github.com/nodejs/node / openSync

Method openSync

lib/internal/vfs/providers/memory.js:494–545  ·  view source on GitHub ↗
(path, flags, mode)

Source from the content-addressed store, hash-verified

492 }
493
494 openSync(path, flags, mode) {
495 const normalized = this.#normalizePath(path);
496
497 // Normalize numeric flags to string
498 flags = normalizeFlags(flags);
499
500 // Handle create and exclusive modes
501 const isCreate = flags === 'w' || flags === 'w+' ||
502 flags === 'a' || flags === 'a+' ||
503 flags === 'wx' || flags === 'wx+' ||
504 flags === 'ax' || flags === 'ax+';
505 const isExclusive = flags === 'wx' || flags === 'wx+' ||
506 flags === 'ax' || flags === 'ax+';
507 const isWritable = flags !== 'r';
508
509 // Check readonly for any writable mode
510 if (this.readonly && isWritable) {
511 throw createEROFS('open', path);
512 }
513
514 let entry;
515 try {
516 entry = this.#getEntry(normalized, 'open');
517 // Exclusive flag: file must not exist
518 if (isExclusive) {
519 throw createEEXIST('open', path);
520 }
521 } catch (err) {
522 if (err.code !== 'ENOENT' || !isCreate) throw err;
523 // Create the file
524 const parent = this.#ensureParent(normalized, false, 'open');
525 const name = pathPosix.basename(normalized);
526 entry = new MemoryEntry(TYPE_FILE, { mode });
527 entry.content = Buffer.alloc(0);
528 parent.children.set(name, entry);
529 const now = DateNow();
530 parent.mtime = now;
531 parent.ctime = now;
532 }
533
534 if (entry.isDirectory()) {
535 throw createEISDIR('open', path);
536 }
537
538 if (entry.isSymbolicLink()) {
539 // Should have been resolved already, but just in case
540 throw createEINVAL('open', path);
541 }
542
543 const getStats = (size) => this.#createStats(entry, size);
544 return new MemoryFileHandle(normalized, flags, mode ?? entry.mode, entry.content, entry, getStats);
545 }
546
547 async open(path, flags, mode) {
548 return this.openSync(path, flags, mode);

Callers 1

openMethod · 0.95

Calls 12

#normalizePathMethod · 0.95
#getEntryMethod · 0.95
#ensureParentMethod · 0.95
normalizeFlagsFunction · 0.85
createEROFSFunction · 0.85
createEEXISTFunction · 0.85
createEISDIRFunction · 0.85
createEINVALFunction · 0.85
allocMethod · 0.80
setMethod · 0.45
isDirectoryMethod · 0.45
isSymbolicLinkMethod · 0.45

Tested by

no test coverage detected