Skip to content

Commit

Permalink
JS runtime: move seekable check into MlNodeFd
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon authored and hhugo committed Feb 3, 2025
1 parent 610e20d commit a7bce7d
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions runtime/js/fs_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ MlNodeDevice.prototype.open = function (name, f, perms, raise_unix) {
}
try {
var fd = this.fs.openSync(this.nm(name), res, perms);
var isCharacterDevice = this.fs
.lstatSync(this.nm(name))
.isCharacterDevice();
f.isCharacterDevice = isCharacterDevice;
return new MlNodeFd(fd, f);
} catch (err) {
caml_raise_nodejs_error(err, raise_unix);
Expand Down Expand Up @@ -333,7 +329,10 @@ function MlNodeFd(fd, flags) {
this.fs = require("node:fs");
this.fd = fd;
this.flags = flags;
this.offset = this.flags.append ? this.length() : 0;
var stats = this.fs.fstatSync(fd);
flags.noSeek =
stats.isCharacterDevice() || stats.isFIFO() || stats.isSocket();
this.offset = this.flags.append ? stats.size : 0;
this.seeked = false;
}
MlNodeFd.prototype = new MlFile();
Expand All @@ -356,7 +355,7 @@ MlNodeFd.prototype.length = function () {
};
MlNodeFd.prototype.write = function (buf, buf_offset, len, raise_unix) {
try {
if (this.flags.isCharacterDevice || !this.seeked)
if (this.flags.noSeek || !this.seeked)
var written = this.fs.writeSync(this.fd, buf, buf_offset, len);
else
var written = this.fs.writeSync(
Expand All @@ -374,7 +373,7 @@ MlNodeFd.prototype.write = function (buf, buf_offset, len, raise_unix) {
};
MlNodeFd.prototype.read = function (a, buf_offset, len, raise_unix) {
try {
if (this.flags.isCharacterDevice || !this.seeked)
if (this.flags.noSeek || !this.seeked)
var read = this.fs.readSync(this.fd, a, buf_offset, len);
else var read = this.fs.readSync(this.fd, a, buf_offset, len, this.offset);
this.offset += read;
Expand All @@ -384,7 +383,7 @@ MlNodeFd.prototype.read = function (a, buf_offset, len, raise_unix) {
}
};
MlNodeFd.prototype.seek = function (offset, whence, raise_unix) {
if (this.flags.isCharacterDevice)
if (this.flags.noSeek)
caml_raise_system_error(raise_unix, "ESPIPE", "lseek", "illegal seek");
switch (whence) {
case 0:
Expand Down

0 comments on commit a7bce7d

Please sign in to comment.