diskFileSystemProvider.ts ×17

Frontier kind: Code frontier

unlabeled · c_aa164b63193f

13 tests · 14162 LOC · 64 files · introduces 0 tests · 105 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
17 ranges105 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
2227 ranges14162 lines · 64 files · Browse complete extent
All tests (intent)
13 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 105 introduced LOC across 17 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/platform/files/node/diskFileSystemProvider.ts 105 introduced LOC · 17 ranges

Open complete file

350
351 async open(resource: URI, opts: IFileOpenOptions, disableWriteLock?: boolean): Promise<number> {
352 > const filePath = this.toFilePath(resource); diskFileSystemProvider.ts
353 >
354 > // Writes: guard multiple writes to the same resource
355 > // behind a single lock to prevent races when writing
356 > // from multiple places at the same time to the same file
357 > let lock: IDisposable | undefined = undefined;
358 > if (isFileOpenForWriteOptions(opts) && !disableWriteLock) {
359 lock = await this.createResourceLock(resource);
360 }
362 > let fd: number | undefined = undefined;
363 > try {
364 >
365 > // Determine whether to unlock the file (write only)
366 > if (isFileOpenForWriteOptions(opts) && opts.unlock) {
367 try {
368 const { stat } = await SymlinkSupport.stat(filePath);
376 }
377 }
379 > // Windows gets special treatment (write only, but not for append)
380 > if (isWindows && isFileOpenForWriteOptions(opts) && !opts.append) {
381 try {
382
409 }
410 }
412 > if (typeof fd !== 'number') {
413 > fd = await Promises.open(filePath, isFileOpenForWriteOptions(opts) ?
414 // We take `opts.create` as a hint that the file is opened for writing
415 // as such we use 'w' to truncate an existing or create the
421 // the file.
422 'r'
424 > }
425 >
426 > } catch (error) {
427
428 // Release lock because we have no valid handle
437 }
438 }
440 > // Remember this handle to track file position of the handle
441 > // we init the position to 0 since the file descriptor was
442 > // just created and the position was not moved so far (see
443 > // also http://man7.org/linux/man-pages/man2/open.2.html -
444 > // "The file offset is set to the beginning of the file.")
445 > this.mapHandleToPos.set(fd, 0);
446 >
447 > // remember that this handle was used for writing
448 > if (isFileOpenForWriteOptions(opts)) {
449 this.writeHandles.set(fd, resource);
450 }
452 > if (lock) {
453 const previousLock = this.mapHandleToLock.get(fd);
454
468 }
469 }
471 > return fd;
472 > }
473
474 async close(fd: number): Promise<void> {
476 > // It is very important that we keep any associated lock
477 > // for the file handle before attempting to call `fs.close(fd)`
478 > // because of a possible race condition: as soon as a file
479 > // handle is released, the OS may assign the same handle to
480 > // the next `fs.open` call and as such it is possible that our
481 > // lock is getting overwritten
482 > const lockForHandle = this.mapHandleToLock.get(fd);
483 >
484 > try {
485 >
486 > // Remove this handle from map of positions
487 > this.mapHandleToPos.delete(fd);
488 >
489 > // If a handle is closed that was used for writing, ensure
490 > // to flush the contents to disk if possible.
491 > if (this.writeHandles.delete(fd) && DiskFileSystemProvider.canFlush) {
492 try {
493 await Promises.fdatasync(fd); // https://github.com/microsoft/vscode/issues/9589
499 }
500 }
502 > return await Promises.close(fd);
503 > } catch (error) {
504 throw this.toFileSystemProviderError(error);
505 > } finally { diskFileSystemProvider.ts
506 > if (lockForHandle) {
507 if (this.mapHandleToLock.get(fd) === lockForHandle) {
508 this.traceLock(`[Disk FileSystemProvider]: close() - resource lock removed from handle-lock map ${fd}`);
513 lockForHandle.dispose();
514 }
516 > }
517
518 async read(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {
532
533 private normalizePos(fd: number, pos: number): number | null {
535 > // When calling fs.read/write we try to avoid passing in the "pos" argument and
536 > // rather prefer to pass in "null" because this avoids an extra seek(pos)
537 > // call that in some cases can even fail (e.g. when opening a file over FTP -
538 > // see https://github.com/microsoft/vscode/issues/73884).
539 > //
540 > // as such, we compare the passed in position argument with our last known
541 > // position for the file descriptor and use "null" if they match.
542 > if (pos === this.mapHandleToPos.get(fd)) {
543 > return null;
544 > }
545
546 return pos;
548
549 private updatePos(fd: number, pos: number | null, bytesLength: number | null): void {
550 > const lastKnownPos = this.mapHandleToPos.get(fd); diskFileSystemProvider.ts
551 > if (typeof lastKnownPos === 'number') {
552 >
553 > // pos !== null signals that previously a position was used that is
554 > // not null. node.js documentation explains, that in this case
555 > // the internal file pointer is not moving and as such we do not move
556 > // our position pointer.
557 > //
558 > // Docs: "If position is null, data will be read from the current file position,
559 > // and the file position will be updated. If position is an integer, the file position
560 > // will remain unchanged."
561 > if (typeof pos === 'number') {
562 // do not modify the position
563 }
565 > // bytesLength = number is a signal that the read/write operation was
566 > // successful and as such we need to advance the position in the Map
567 > //
568 > // Docs (http://man7.org/linux/man-pages/man2/read.2.html):
569 > // "On files that support seeking, the read operation commences at the
570 > // file offset, and the file offset is incremented by the number of
571 > // bytes read."
572 > //
573 > // Docs (http://man7.org/linux/man-pages/man2/write.2.html):
574 > // "For a seekable file (i.e., one to which lseek(2) may be applied, for
575 > // example, a regular file) writing takes place at the file offset, and
576 > // the file offset is incremented by the number of bytes actually
577 > // written."
578 > else if (typeof bytesLength === 'number') {
579 > this.mapHandleToPos.set(fd, lastKnownPos + bytesLength);
580 > }
581
582 // bytesLength = null signals an error in the read/write operation
586 this.mapHandleToPos.delete(fd);
587 }
589 > }
590
591 async write(fd: number, pos: number, data: Uint8Array, offset: number, length: number): Promise<number> {