mirror of
https://github.com/osnr/TabFS.git
synced 2024-05-03 22:33:11 +02:00
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:
parent
383096da00
commit
8fbfa9a7c9
|
@ -61,7 +61,13 @@ const utf8ArrayToString = (function() {
|
|||
return utf8 => decoder.decode(utf8);
|
||||
})();
|
||||
|
||||
const Cache = {
|
||||
// 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
|
||||
// individual chunk read() and write() requests without doing a
|
||||
|
@ -76,17 +82,14 @@ const Cache = {
|
|||
getObjectForHandle(handle) { return this.store[handle]; },
|
||||
setObjectForHandle(handle, object) { this.store[handle] = object; },
|
||||
removeObjectForHandle(handle) { delete this.store[handle]; }
|
||||
};
|
||||
function toUtf8Array(stringOrArray) {
|
||||
};
|
||||
|
||||
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 = {};
|
||||
|
|
Loading…
Reference in a new issue