- Added a safety check in serializer.ts to look for fields containing undefined

- Updated the fs json to contain the new result from the fixed serializer that parses the link counts
- Updated ls to use proper link counts and sizes in the long format (apart from dir sizes which remain at arbitrary 4096)
This commit is contained in:
2026-11-14 10:49:51 +01:00
parent de1ce64226
commit d4ec6b0c49
4 changed files with 26 additions and 29 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
"main": "index.ts", "main": "index.ts",
"scripts": { "scripts": {
"test": "eslint . && vitest", "test": "eslint . && vitest",
"dev": "npx tsx src/dev.ts" "dev": "pnpx tsx src/dev.ts"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
+11 -27
View File
@@ -274,6 +274,7 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
if (options.recursive) { if (options.recursive) {
for (const c of children) { for (const c of children) {
if (c.name.match(/^[.]{1,2}$/)) continue;
const child = state.filesystem.getNode(c.ptr)!; const child = state.filesystem.getNode(c.ptr)!;
if (child.type === NodeType.Directory) { if (child.type === NodeType.Directory) {
let path; let path;
@@ -293,16 +294,6 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
const buff: Uint8Array[] = []; const buff: Uint8Array[] = [];
const nodes: DirEntry[] = [...children]; const nodes: DirEntry[] = [...children];
/* if (options.all) {
const parentNode = state.filesystem.getNode(current.parent)!;
const parent: NodePointer = { ...parentNode, name: ".." };
nodes.unshift(parent);
const cwd: TreeNode = { ...current, name: "." };
nodes.unshift(cwd);
} */
// Calculate the string size of the biggerst node in the array; // Calculate the string size of the biggerst node in the array;
// Sets 4 length for directories because the arbitrary size is 4096 later in the code // Sets 4 length for directories because the arbitrary size is 4096 later in the code
const maxInodeWidth = Math.max(...nodes.map(c => c.ptr.inode.toString().length)); const maxInodeWidth = Math.max(...nodes.map(c => c.ptr.inode.toString().length));
@@ -315,23 +306,21 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
), ),
); );
const maxChildrenWidth = Math.max( const maxLinksWidth = Math.max(
...nodes.map(c => ...nodes.map((c): number => {
state.filesystem.getNode(c.ptr)!.type === NodeType.Directory const node = state.filesystem.getNode(c.ptr)!;
? Object.entries((state.filesystem.getNode(c.ptr) as DirectoryNode)!.children).length const o = node.link_count.toString().length;
: 2, return o;
), }),
); );
const namePadding = nodes.every(c => !c.name.includes(" ")); const namePadding = nodes.every(c => !c.name.includes(" "));
for (const n of nodes) { for (const n of nodes) {
//Inode
const node = state.filesystem.getNode(n.ptr)!; const node = state.filesystem.getNode(n.ptr)!;
if (options.inode) { if (options.inode) {
const inode = node.inode.toString().padStart(maxInodeWidth, " "); const inode = node.inode.toString().padStart(maxInodeWidth, " ");
buff.push(e.encode(inode + " ")); buff.push(e.encode(inode + " "));
//io.writeStdout(e.encode(inode + " "));
} }
// Mode // Mode
@@ -346,13 +335,9 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
break; break;
} }
// Children amnt (later will be amnt of links) //Link count
const childAmnt = const linkCount = node.link_count.toString().padStart(maxLinksWidth, " ");
node.type === NodeType.Directory buff.push(e.encode(linkCount));
? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} `
: `${"10".padStart(maxChildrenWidth, " ")} `;
buff.push(e.encode(childAmnt));
// owner group // owner group
if (options.noGroup) { if (options.noGroup) {
@@ -384,7 +369,6 @@ async function main(io: CommandIO, state: ShellState, args: string[]): Promise<C
buff.push(e.encode(size)); buff.push(e.encode(size));
//Timestamp //Timestamp
// for now only mtime, need a helper function for all the formatting later on
const date = getDateTimestamp(node, options.timestamp); const date = getDateTimestamp(node, options.timestamp);
if (options.fullTime) { if (options.fullTime) {
buff.push( buff.push(
+1 -1
View File
File diff suppressed because one or more lines are too long
+13
View File
@@ -52,6 +52,11 @@ export function deserializeFileSystem(json: string): { dstore: IDataStore; mstor
atime: tree.atime, atime: tree.atime,
children, children,
}; };
if(Object.values(directoryNode).some(e => e === undefined))
throw new Error(`Serialization failed, object contains undefined fields - ${directoryNode}`);
children.set('..', parent); children.set('..', parent);
children.set('.', tree.inode); children.set('.', tree.inode);
map.set(tree.inode, Object.freeze(directoryNode)); map.set(tree.inode, Object.freeze(directoryNode));
@@ -72,6 +77,10 @@ export function deserializeFileSystem(json: string): { dstore: IDataStore; mstor
content: uuid, content: uuid,
size: tree.size, size: tree.size,
}; };
if(Object.values(fileNode).some(e => e === undefined))
throw new Error(`Serialization failed, object contains undefined fields - ${fileNode}`);
const buff: Uint8Array<ArrayBuffer> = Uint8Array.from(tree.content); const buff: Uint8Array<ArrayBuffer> = Uint8Array.from(tree.content);
store.put(uuid, new Blob([buff])); store.put(uuid, new Blob([buff]));
map.set(tree.inode, Object.freeze(fileNode)); map.set(tree.inode, Object.freeze(fileNode));
@@ -90,6 +99,10 @@ export function deserializeFileSystem(json: string): { dstore: IDataStore; mstor
atime: tree.atime, atime: tree.atime,
target: tree.target, target: tree.target,
}; };
if(Object.values(symlinkNode).some(e => e === undefined))
throw new Error(`Serialization failed, object contains undefined fields - ${symlinkNode}`);
map.set(tree.inode, Object.freeze(symlinkNode)); map.set(tree.inode, Object.freeze(symlinkNode));
break; break;
} }