extension: fix truncate in defineFile; refactor/encap defineFile

truncate was still assuming fixed path instead of passing whole req
object on to setData/getData.
This commit is contained in:
Omar Rizwan 2021-03-26 02:21:14 -07:00
parent 383096da00
commit 8fbfa9a7c9

View file

@ -61,6 +61,12 @@ const utf8ArrayToString = (function() {
return utf8 => decoder.decode(utf8);
})();
// Helper function: generates a full set of file operations that you
// can use as a route handler (so clients can read and write
// sections of the file, stat it to get its size and see it show up
// in ls, etc), given getData and setData functions that define the
// contents of the entire file.
const defineFile = (function() {
const Cache = {
// used when you open a file to cache the content we got from the
// browser until you close that file. (so we can respond to
@ -77,16 +83,13 @@ const Cache = {
setObjectForHandle(handle, object) { this.store[handle] = object; },
removeObjectForHandle(handle) { delete this.store[handle]; }
};
function toUtf8Array(stringOrArray) {
if (typeof stringOrArray == 'string') { return stringToUtf8Array(stringOrArray); }
else { return stringOrArray; }
}
const defineFile = (getData, setData) => ({
// Generates a full set of file operations (so clients can read and
// write sections of the file, stat it to get its size and see it
// show up in ls, etc), given getData and setData functions that
// define the contents of the entire file.
return (getData, setData) => ({
// getData: (req: Request U Vars) -> Promise<contentsOfFile: String|Uint8Array>
// setData [optional]: (req: Request U Vars, newContentsOfFile: String) -> Promise<>
@ -131,19 +134,20 @@ const defineFile = (getData, setData) => ({
},
async release({fh}) { Cache.removeObjectForHandle(fh); return {}; },
async truncate({path, size}) {
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(path));
if (size !== arr.length) {
const newArr = new Uint8Array(size);
newArr.set(arr.slice(0, Math.min(size, arr.length)));
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;
}
await setData(path, utf8ArrayToString(arr)); return {};
await setData(req, utf8ArrayToString(arr)); return {};
}
});
})();
const Routes = {};
@ -183,10 +187,12 @@ Routes["/tabs/by-id"] = {
// echo true > mnt/tabs/by-id/1644/active
// cat mnt/tabs/by-id/1644/active
Routes["/tabs/by-id/#TAB_ID/active"] = withTab(tab => JSON.stringify(tab.active) + '\n',
Routes["/tabs/by-id/#TAB_ID/active"] = withTab(
tab => JSON.stringify(tab.active) + '\n',
// WEIRD: we do startsWith because you might end up with buf
// being "truee" (if it was "false", then someone wrote "true")
buf => ({ active: buf.startsWith("true") }));
buf => ({ active: buf.startsWith("true") })
);
})();
(function() {
const evals = {};