some bs work, not finished (reference notes board on email for info)
This commit is contained in:
@@ -15,6 +15,9 @@ export class FileSystem {
|
|||||||
private _BaseFS: ReadonlyMap<number, TreeNode>;
|
private _BaseFS: ReadonlyMap<number, TreeNode>;
|
||||||
private _OverlayFS: ReadonlyMap<number, TreeNode | null>;
|
private _OverlayFS: ReadonlyMap<number, TreeNode | null>;
|
||||||
|
|
||||||
|
private _SysVDisk: number;
|
||||||
|
private _Mounts: Map<number, number>;
|
||||||
|
|
||||||
private _INodeIndex: number;
|
private _INodeIndex: number;
|
||||||
|
|
||||||
private constructor(
|
private constructor(
|
||||||
|
|||||||
@@ -1,51 +1,60 @@
|
|||||||
import type { ImmutableBuffer } from "@core/util/immutable_buffer";
|
|
||||||
|
|
||||||
export type TreeNode = FileNode | DirectoryNode;
|
export type TreeNode = FileNode | DirectoryNode;
|
||||||
|
|
||||||
export interface FileNode {
|
export interface FileNode {
|
||||||
readonly type: TreeNodeType.File;
|
readonly type: TreeNodeType.File;
|
||||||
readonly inode: number;
|
readonly inode: number;
|
||||||
readonly name: string;
|
readonly link_count: number;
|
||||||
readonly parent: number;
|
|
||||||
readonly mode: number;
|
readonly mode: number;
|
||||||
readonly owner: number;
|
readonly owner: number;
|
||||||
readonly group: number;
|
readonly group: number;
|
||||||
readonly mtime: number;
|
readonly mtime: number;
|
||||||
readonly ctime: number;
|
readonly ctime: number;
|
||||||
readonly atime: number;
|
readonly atime: number;
|
||||||
readonly content: ImmutableBuffer;
|
readonly content: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DirectoryNode {
|
export interface DirectoryNode {
|
||||||
readonly type: TreeNodeType.Directory;
|
readonly type: TreeNodeType.Directory;
|
||||||
readonly inode: number;
|
readonly inode: number;
|
||||||
readonly name: string;
|
readonly link_count: number;
|
||||||
readonly parent: number;
|
|
||||||
readonly mode: number;
|
readonly mode: number;
|
||||||
readonly owner: number;
|
readonly owner: number;
|
||||||
readonly group: number;
|
readonly group: number;
|
||||||
readonly mtime: number;
|
readonly mtime: number;
|
||||||
readonly ctime: number;
|
readonly ctime: number;
|
||||||
readonly atime: number;
|
readonly atime: number;
|
||||||
readonly children: Readonly<Record<string, number>>;
|
readonly content: ReadonlyMap<string, number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* export interface SymlinkNode {
|
export interface SymlinkNode {
|
||||||
readonly type: TreeNodeType.Symlink;
|
readonly type: TreeNodeType.Symlink;
|
||||||
readonly inode: number;
|
readonly inode: number;
|
||||||
readonly name: string;
|
readonly link_count: number;
|
||||||
readonly parent: number;
|
|
||||||
readonly mode: number;
|
readonly mode: number;
|
||||||
readonly owner: number;
|
readonly owner: number;
|
||||||
readonly group: number;
|
readonly group: number;
|
||||||
readonly mtime: number;
|
readonly mtime: number;
|
||||||
readonly ctime: number;
|
readonly ctime: number;
|
||||||
readonly atime: number;
|
readonly atime: number;
|
||||||
readonly target: string;
|
readonly content: string;
|
||||||
} */
|
}
|
||||||
|
|
||||||
|
export interface DeviceNode {
|
||||||
|
readonly type: TreeNodeType.Device;
|
||||||
|
readonly inode: number;
|
||||||
|
readonly link_count: number;
|
||||||
|
readonly mode: number;
|
||||||
|
readonly owner: number;
|
||||||
|
readonly group: number;
|
||||||
|
readonly mtime: number;
|
||||||
|
readonly ctime: number;
|
||||||
|
readonly atime: number;
|
||||||
|
readonly content: number; // Device-ID
|
||||||
|
}
|
||||||
|
|
||||||
export enum TreeNodeType {
|
export enum TreeNodeType {
|
||||||
File = 0,
|
File = 0,
|
||||||
Directory = 1,
|
Directory = 1,
|
||||||
//Symlink = 2,
|
Symlink = 2,
|
||||||
|
Device = 3,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import type { ReadableStream } from "@core/util/stream";
|
||||||
|
import type { TreeNode } from "./treenode";
|
||||||
|
|
||||||
|
class DataStore {
|
||||||
|
public put(data: Uint8Array | ReadableStream): Promise<string>;
|
||||||
|
public get(hash: string): Promise<ReadableStream>;
|
||||||
|
public delete(hash: string): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface VDisk {
|
||||||
|
readonly id: number;
|
||||||
|
readonly store: DataStore; // The backing store (IndexedDB, S3, etc.)
|
||||||
|
readonly base: ReadonlyMap<number, TreeNode>; // Immutable base layer
|
||||||
|
readonly overlay: ReadonlyMap<number, TreeNode | null>; // COW layer
|
||||||
|
readonly inodeIndex: number; // Next free inode on this device
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createVDisk(
|
||||||
|
id: number,
|
||||||
|
base: Map<number, TreeNode>,
|
||||||
|
overlay: Map<number, TreeNode | null>,
|
||||||
|
index: number = 0,
|
||||||
|
): VDisk {
|
||||||
|
let db: IDBDatabase;
|
||||||
|
const request = indexedDB.open(`vd-${id}`);
|
||||||
|
|
||||||
|
request.onerror = () => {
|
||||||
|
throw new Error("This app needs IDB to function properly!");
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onsuccess = () => {
|
||||||
|
db = request.result;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
store =
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,24 +3,31 @@ import type { ShellState } from "@core/shell_state";
|
|||||||
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
||||||
|
|
||||||
|
/*
|
||||||
|
/home/grzegorz/Documents/homework
|
||||||
|
|
||||||
|
""
|
||||||
|
*/
|
||||||
|
|
||||||
export function resolvePath(state: ShellState, path: string): number {
|
export function resolvePath(state: ShellState, path: string): number {
|
||||||
const session = getCurrentSession(state);
|
const session = getCurrentSession(state);
|
||||||
const isAbsolute = path.startsWith("/");
|
const isAbsolute = path.startsWith("/"); // true
|
||||||
|
|
||||||
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
const normalizedPath: string = path.replace(/^\/+|\/+$/g, ""); // home/grzegorz/Documents/homework
|
||||||
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0); // ["home", "grzegorz", "Documents", "homework"]
|
||||||
|
|
||||||
|
// Fix to dynamically get the root inode VV
|
||||||
if (pathComponents.length === 0 && isAbsolute) return 0;
|
if (pathComponents.length === 0 && isAbsolute) return 0;
|
||||||
|
// Move behaviour into cd built-in
|
||||||
if (pathComponents.length === 0) return session.cwd;
|
if (pathComponents.length === 0) return session.cwd;
|
||||||
|
|
||||||
let currentNode = isAbsolute
|
let currentNode =
|
||||||
? state.filesystem.getRoot()
|
isAbsolute === true ? state.filesystem.getRoot() : (state.filesystem.getNode(session.cwd) as DirectoryNode);
|
||||||
: (state.filesystem.getNode(session.cwd) as DirectoryNode);
|
|
||||||
|
|
||||||
if (!currentNode) return -1;
|
if (!currentNode) return -1;
|
||||||
|
|
||||||
for (let i = 0; i < pathComponents.length; i++) {
|
for (let i = 0; i < pathComponents.length; i++) {
|
||||||
const name = pathComponents[i]!;
|
const name: string = pathComponents[i]!;
|
||||||
if (name === ".") continue;
|
if (name === ".") continue;
|
||||||
|
|
||||||
if (name === "..") {
|
if (name === "..") {
|
||||||
|
|||||||
@@ -0,0 +1,131 @@
|
|||||||
|
export class ReadableStream {
|
||||||
|
private _threshold: number;
|
||||||
|
private _buffer: Uint8Array[] = [];
|
||||||
|
private _state: "idle" | "open" | "waiting" | "closed" | "errored" = "idle";
|
||||||
|
private _pending: ((value: Uint8Array | null) => void) | null = null;
|
||||||
|
private _byteLength: number = 0;
|
||||||
|
private _error: Error | null = null;
|
||||||
|
|
||||||
|
// Default - 32 chunks of 64KB -> Buffer of 2MB
|
||||||
|
constructor(threshold: number = 1024 * 64 * 32) {
|
||||||
|
this._threshold = threshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
public push(chunk: Uint8Array): boolean {
|
||||||
|
if (this._state === "closed" || this._state === "errored") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._pending) {
|
||||||
|
const pending = this._pending;
|
||||||
|
this._pending = null;
|
||||||
|
|
||||||
|
pending(chunk);
|
||||||
|
|
||||||
|
this._state = this._buffer.length > 0 ? "open" : "idle";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._buffer.push(chunk);
|
||||||
|
this._byteLength += chunk.byteLength;
|
||||||
|
this._state = "open";
|
||||||
|
|
||||||
|
return this._byteLength < this._threshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
public close(): void {
|
||||||
|
if (this._state === "errored") return;
|
||||||
|
this._state = "closed";
|
||||||
|
|
||||||
|
if (this._pending) {
|
||||||
|
this._pending(null);
|
||||||
|
this._pending = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public error(err: Error): void {
|
||||||
|
this._error = err;
|
||||||
|
this._state = "errored";
|
||||||
|
if (this._pending) {
|
||||||
|
this._pending(null);
|
||||||
|
this._pending = null;
|
||||||
|
}
|
||||||
|
this._buffer = [];
|
||||||
|
this._byteLength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public read(): Promise<Uint8Array | null> {
|
||||||
|
if ((this._state = "errored")) {
|
||||||
|
return Promise.reject(this._error || new Error("Stream encountered an error"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._buffer.length > 0) {
|
||||||
|
const chunk: Uint8Array = this._buffer.shift()!;
|
||||||
|
this._byteLength -= chunk.byteLength;
|
||||||
|
|
||||||
|
if (this._buffer.length === 0) {
|
||||||
|
this._state = "idle";
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((this._state = "closed")) {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._state = "waiting";
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this._pending = resolve;
|
||||||
|
|
||||||
|
if (this._error) {
|
||||||
|
reject(this._error);
|
||||||
|
this._pending = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public pipe(writable: WritableStream): Promise<void> {
|
||||||
|
return new Promise(async (resolve, reject) => {
|
||||||
|
try {
|
||||||
|
for await (const chunk of this) {
|
||||||
|
await writable.write(chunk);
|
||||||
|
}
|
||||||
|
await writable.close();
|
||||||
|
resolve();
|
||||||
|
} catch (err) {
|
||||||
|
reject(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async *[Symbol.asyncIterator](): AsyncIterator<Uint8Array> {
|
||||||
|
while (true) {
|
||||||
|
const chunk = await this.read();
|
||||||
|
if (chunk === null) break;
|
||||||
|
yield chunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public get byteLength(): number {
|
||||||
|
return this._byteLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get isFull(): boolean {
|
||||||
|
return this._byteLength >= this._threshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get state(): string {
|
||||||
|
return this._state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WritableStream {
|
||||||
|
write(chunk: Uint8Array): void {}
|
||||||
|
|
||||||
|
close(): void {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DuplexStream {}
|
||||||
|
|
||||||
|
export class TransformStream {}
|
||||||
Reference in New Issue
Block a user