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 {
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