begining of MD5 implementation and some more work on the other code
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { User } from '../shell/shell';
|
||||
import { MD5 } from '../util/hash';
|
||||
|
||||
export class FileSystem {
|
||||
static #instance: FileSystem;
|
||||
@@ -6,61 +7,62 @@ export class FileSystem {
|
||||
private _root: number;
|
||||
|
||||
private constructor() {
|
||||
const
|
||||
|
||||
this._fs = this._jsonToNodeTable();
|
||||
this._root = this._fs.values().next().value!.inode;
|
||||
|
||||
if (!this._root) throw new Error('First entry in the filesystem map empty!');
|
||||
}
|
||||
|
||||
private async _fetchFsSignature(path: string): Promise<any> {
|
||||
try {
|
||||
const response = await fetch(path);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.text();
|
||||
return data.slice(0, 36); //32 characters of uuid + 4 dashes
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch the file system signature', error);
|
||||
public static async Init() {
|
||||
const fsJson: JSON = await this._fetchFileSystem('/temp/path');
|
||||
const checksum: any = MD5.hash(JSON.stringify(fsJson));
|
||||
}
|
||||
}
|
||||
|
||||
private async _fetchFileSystem(path: string): Promise<any> {
|
||||
const response = await fetch(path);
|
||||
if (!response.ok) throw new Error('Failed to fetch the file system json');
|
||||
private static async _fetchFileSystem(path: string): Promise<JSON> {
|
||||
const response = await fetch(path);
|
||||
if (!response.ok) throw new Error('Failed to fetch the file system json');
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
}
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
private async _fetchFsSignature(path: string): Promise<any> {
|
||||
try {
|
||||
const response = await fetch(path);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.text();
|
||||
return data.slice(0, 36); //32 characters of uuid + 4 dashes
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch the file system signature', error);
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchFsJson(sig: string): Promise<JSON> {
|
||||
const signature: string | null = localStorage.getItem('signature');
|
||||
const signature: string | null = localStorage.getItem('signature');
|
||||
|
||||
if (signature !== sig || signature === null || localStorage.getItem('fs') === null) {
|
||||
const fs: JSON = await this._fetchFileSystem('/src/lib/assets/fs/fs.json');
|
||||
localStorage.setItem('signature', sig);
|
||||
localStorage.setItem('fs', JSON.stringify(fs));
|
||||
if (signature !== sig || signature === null || localStorage.getItem('fs') === null) {
|
||||
const fs: JSON = await this._fetchFileSystem('/src/lib/assets/fs/fs.json');
|
||||
localStorage.setItem('signature', sig);
|
||||
localStorage.setItem('fs', JSON.stringify(fs));
|
||||
|
||||
console.info('FS fetched from file, new sinature:', sig);
|
||||
console.info('FS fetched from file, new sinature:', sig);
|
||||
|
||||
return fs;
|
||||
} else {
|
||||
const fs: string | null = localStorage.getItem('fs');
|
||||
if (fs === null) throw new Error('FS in LocalStorage is null!');
|
||||
return fs;
|
||||
} else {
|
||||
const fs: string | null = localStorage.getItem('fs');
|
||||
if (fs === null) throw new Error('FS in LocalStorage is null!');
|
||||
|
||||
console.info('FS fetched from localStorage with signature:', sig);
|
||||
console.info('FS fetched from localStorage with signature:', sig);
|
||||
|
||||
return JSON.parse(fs);
|
||||
return JSON.parse(fs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _jsonToNodeTable(data: JSON): Map<number, TreeNode> {
|
||||
const FsTable: Map<number, TreeNode> = new Map<number, TreeNode>();
|
||||
const entryList = Object.entries(data);
|
||||
|
||||
|
||||
for (let i = 0; i < entryList.length; i++) {
|
||||
const object: any = entryList[i][1];
|
||||
const node: TreeNode = {
|
||||
@@ -99,7 +101,7 @@ export class FileSystem {
|
||||
},
|
||||
parent: object.Parent
|
||||
};
|
||||
|
||||
|
||||
FsTable.set(object.Inode, node);
|
||||
}
|
||||
return FsTable;
|
||||
@@ -152,19 +154,23 @@ export class FileSystem {
|
||||
this._fs.delete(inode);
|
||||
}
|
||||
|
||||
private _iNodeToPathString(inode: number): string | null {
|
||||
private _iNodeToPathString(inode: number): string {
|
||||
const currentNode = this._fs.get(inode);
|
||||
|
||||
if (!currentNode) return null;
|
||||
if (!currentNode)
|
||||
throw new Error(`Failed to parse the path from inode, inode doesnt exist - ${inode}`);
|
||||
if (!currentNode.parent || currentNode.inode === this._root) return '/';
|
||||
|
||||
const parentPath: string | null = this._iNodeToPathString(currentNode.parent);
|
||||
if (!parentPath) return null;
|
||||
if (!parentPath)
|
||||
throw new Error(
|
||||
`Failed to parse the path from inode, inode doesnt exist - ${currentNode.parent}`
|
||||
);
|
||||
|
||||
return parentPath === '/' ? `/${currentNode.name}` : `${parentPath}/${currentNode.name}`;
|
||||
}
|
||||
|
||||
private _pathStringToNode(path: string): TreeNode | null {
|
||||
private _pathStringToNode(path: string): TreeNode {
|
||||
// Truncates all slatshes at the start and end of the string
|
||||
const normalizedPath: string = path.replace(/^\/+|\/+$/g, '');
|
||||
|
||||
@@ -172,18 +178,19 @@ export class FileSystem {
|
||||
const pathComponents: string[] = normalizedPath.split('/').filter((node) => node.length > 0);
|
||||
|
||||
let currentNode: TreeNode | undefined = this._fs.get(this._root);
|
||||
if (!currentNode) return null;
|
||||
if (!currentNode)
|
||||
throw new Error(`WARNING! - Root node not found under FileSystem._root - ${this._root}`);
|
||||
if (pathComponents.length === 0) return currentNode;
|
||||
|
||||
for (const component of pathComponents) {
|
||||
const childINode: number | undefined = this._getChildNodeByName(
|
||||
currentNode!,
|
||||
component
|
||||
)?.inode;
|
||||
if (!childINode) return null;
|
||||
const childINode: number = this._getChildNodeByName(currentNode!, component)?.inode;
|
||||
|
||||
if (!childINode)
|
||||
throw new Error(`Failed to parse the child node from path, is the path correct?`);
|
||||
|
||||
const nextNode = this._fs.get(childINode);
|
||||
if (!nextNode) return null;
|
||||
if (!nextNode)
|
||||
throw new Error(`Failed to parse the child node from path, is the path correct?`);
|
||||
|
||||
currentNode = nextNode;
|
||||
}
|
||||
@@ -191,19 +198,21 @@ export class FileSystem {
|
||||
return currentNode;
|
||||
}
|
||||
|
||||
private _getChildNodeByName(node: TreeNode, name: string): TreeNode | null {
|
||||
private _getChildNodeByName(node: TreeNode, name: string): TreeNode {
|
||||
for (const inode of node.children) {
|
||||
const child = this._fs.get(inode);
|
||||
if (child && child.name === name) return child;
|
||||
}
|
||||
return null;
|
||||
throw new Error(
|
||||
`Node under the name ${name} not found in the child list of ${node.name} - ${node.inode}`
|
||||
);
|
||||
}
|
||||
|
||||
private _isAbsolutePath = (path: string): boolean => {
|
||||
return typeof path === 'string' && path.startsWith('/');
|
||||
};
|
||||
|
||||
public getPathByINode(inode: number): string | null {
|
||||
public getPathByINode(inode: number): string {
|
||||
return this._iNodeToPathString(inode);
|
||||
}
|
||||
|
||||
@@ -212,7 +221,13 @@ export class FileSystem {
|
||||
return path;
|
||||
}
|
||||
|
||||
public resolvePath(path: string, user: User): TreeNode | null {
|
||||
public getNodeByInode(inode: number): TreeNode {
|
||||
const node: TreeNode | undefined = this._fs.get(inode);
|
||||
if (!node) throw new Error(`No node exists under inode - ${inode}`);
|
||||
return node;
|
||||
}
|
||||
|
||||
public resolvePath(path: string, user: User): TreeNode {
|
||||
if (path === '/') return this._fs.get(this._root)!;
|
||||
|
||||
let parsedPath: string = path;
|
||||
@@ -242,13 +257,14 @@ export class FileSystem {
|
||||
callback: (param: TreeNode) => void,
|
||||
childIndex?: number[],
|
||||
depthIndex?: number
|
||||
): void | null {
|
||||
): void {
|
||||
if (!depthIndex) depthIndex = 0;
|
||||
if (!childIndex) childIndex = [0];
|
||||
|
||||
if (node.type != Type.File && node.children[childIndex[depthIndex]]) {
|
||||
let unNode = this._fs.get(node.children[childIndex[depthIndex]]);
|
||||
if (!unNode) return null;
|
||||
if (!unNode)
|
||||
throw new Error(`Node does not exist under inode ${node.children[childIndex[depthIndex]]}`);
|
||||
node = unNode;
|
||||
|
||||
depthIndex++;
|
||||
@@ -257,7 +273,7 @@ export class FileSystem {
|
||||
callback(node);
|
||||
} else {
|
||||
let unNode = this._fs.get(node.parent);
|
||||
if (!unNode) return null;
|
||||
if (!unNode) throw new Error(`Node does not exist under inode ${node.parent}`);
|
||||
node = unNode;
|
||||
|
||||
childIndex[depthIndex] = 0;
|
||||
@@ -271,12 +287,11 @@ export class FileSystem {
|
||||
}
|
||||
|
||||
public static get instance(): FileSystem {
|
||||
if(!FileSystem.#instance) {
|
||||
if (!FileSystem.#instance) {
|
||||
FileSystem.#instance = new FileSystem();
|
||||
}
|
||||
|
||||
return FileSystem.instance;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user