diff --git a/lib/9p-filer.js b/lib/9p-filer.js index db95d549e2..1bd8cbee98 100644 --- a/lib/9p-filer.js +++ b/lib/9p-filer.js @@ -14,6 +14,8 @@ const VIRTIO_9P_F_MOUNT_TAG = 0; // Assumed max tag length in bytes. const VIRTIO_9P_MAX_TAGLEN = 254; +const FSCACHE = new Map() + // TODO // flush @@ -954,19 +956,37 @@ Virtio9p.prototype.ReceiveRequest = async function(bufchain) { _read(data); return; } + if (!FSCACHE.has(path)) + fs.readFile(path, function(err, data) { + if (self.shouldAbortRequest(tag)) return; - fs.readFile(path, function(err, data) { - if (self.shouldAbortRequest(tag)) return; + if (err) { + self.SendError(tag, err); + self.SendReply(bufchain) + return; + } - if (err) { - self.SendError(tag, err); - self.SendReply(bufchain) - return; - } + self.pendingTags[tag].data = data; + FSCACHE.set(path, {data: data, remaining: data.byteLength}) + _read(data); + }); + else { + const file = FSCACHE.get(path); + file.remaining -= count; self.pendingTags[tag].data = data; - _read(data); - }); + _read(file.data) + setTimeout(() => { + if (FSCACHE.has(path)) { + // console.log("Deleted FSCACHE entry for: " + path + "\nReason: over 1 minute"); + FSCACHE.delete(path); + } + }, 60 * 1000) + if (file.remaining <= 0) { + // console.log("Deleted FSCACHE entry for: " + path + "\nReason: Used up"); + FSCACHE.delete(path); + } + } break; case 118: // write