From b948c235b83e43441faeed257cfdffa2008326d9 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Tue, 6 Apr 2021 13:48:48 -0700 Subject: fs,extension: Make truncate work on cached file content. We now remember the path of each cached file, so when someone truncates that path, we can truncate all open instances of it in cache. Important when someone pipes to stomp a file / opens with O_TRUNC (which FUSE disaggregates into an open() and then a truncate() call). Gets rid of the need for FUSE_CAP_ATOMIC_O_TRUNC, which never worked on macOS anyway. Fixes #40. --- extension/background.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'extension') diff --git a/extension/background.js b/extension/background.js index 2a59630..6f4d108 100644 --- a/extension/background.js +++ b/extension/background.js @@ -17,9 +17,6 @@ const unix = { S_IFREG: 0100000, // regular S_IFLNK: 0120000, // symbolic link S_IFSOCK: 0140000, // socket - - // Open flags - O_TRUNC: 01000, } class UnixError extends Error { constructor(error) { super(); this.name = "UnixError"; this.error = error; } @@ -74,14 +71,21 @@ const defineFile = (function() { // whole new conversation with the browser and regenerating the // content -- important for taking a screenshot, for instance) store: {}, nextHandle: 0, - storeObject(object) { + storeObject(path, object) { const handle = ++this.nextHandle; - this.store[handle] = object; + this.store[handle] = {path, object}; return handle; }, - getObjectForHandle(handle) { return this.store[handle]; }, - setObjectForHandle(handle, object) { this.store[handle] = object; }, - removeObjectForHandle(handle) { delete this.store[handle]; } + getObjectForHandle(handle) { return this.store[handle].object; }, + setObjectForHandle(handle, object) { this.store[handle].object = object; }, + removeObjectForHandle(handle) { delete this.store[handle]; }, + setObjectForPath(path, object) { + for (let storedObject of Object.values(this.store)) { + if (storedObject.path === path) { + storedObject.object = object; + } + } + } }; function toUtf8Array(stringOrArray) { @@ -110,11 +114,11 @@ const defineFile = (function() { // We call getData() once when the file is opened, then cache that // data for all subsequent reads from that application. async open(req) { - const data = !(req.flags & unix.O_TRUNC) ? await getData(req) : ""; - return { fh: Cache.storeObject(toUtf8Array(data)) }; + const data = await getData(req); + return { fh: Cache.storeObject(req.path, toUtf8Array(data)) }; }, async read({fh, size, offset}) { - return { buf: String.fromCharCode(...Cache.getObjectForHandle(fh).slice(offset, offset + size)) } + return { buf: String.fromCharCode(...Cache.getObjectForHandle(fh).slice(offset, offset + size)) }; }, async write(req) { const {fh, offset, buf} = req; @@ -135,15 +139,13 @@ const defineFile = (function() { async release({fh}) { Cache.removeObjectForHandle(fh); return {}; }, async truncate(req) { - // TODO: weird case if they truncate while the file is open - // (but `echo hi > foo.txt`, the main thing I care about, uses - // O_TRUNC which thankfully doesn't do that) let arr = toUtf8Array(await getData(req)); if (req.size !== arr.length) { const newArr = new Uint8Array(req.size); newArr.set(arr.slice(0, Math.min(req.size, arr.length))); arr = newArr; } + Cache.setObjectForPath(req.path, arr); await setData(req, utf8ArrayToString(arr)); return {}; } }); -- cgit v1.2.3