Created
January 15, 2020 03:28
-
-
Save Fleker/4fc688fb2bd347a0aa0b111456a1b6af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MemoryBlock from "./memory-block"; | |
import BitmapMemory from "./bitmap-memory"; | |
import LinkedList from "./linked-list"; | |
const PATH_VOLATILE = '.volatile' | |
// The FILE SYSTEM is an extension of a memory architecture which allows data | |
// to be stored in a globally accessible* tree of files and directories. | |
// | |
// * Restrictions on files may be present to prevent malicious access | |
export interface FileSystemOptions { | |
memory: BitmapMemory | |
inodeSize: number | |
fileVolatile?: string | |
} | |
export default class FileSystem { | |
memory: BitmapMemory | |
inodeSize: number | |
fileVolatile?: string | |
root: File | |
constructor(options: FileSystemOptions) { | |
this.memory = options.memory | |
this.inodeSize = options.inodeSize | |
this.fileVolatile = options.fileVolatile | |
this.root = new File({path: '', children: []}) | |
// Attach imported files into the root | |
if (this.root.children) { | |
const volatileMemory = new File({path: PATH_VOLATILE}) | |
volatileMemory.metadata = { | |
created: Date.now(), | |
access: 777, | |
lock: '' | |
} | |
this.root.children.push(volatileMemory) | |
} | |
} | |
private traverse(files: File[], path: string) { | |
files.forEach(file => { | |
if (file.path === path) return file | |
}) | |
return undefined | |
} | |
open(path: string) { | |
const traversal = path.split('/') | |
let node; | |
traversal.forEach(directory => { | |
}) | |
} | |
} | |
export interface FileOptions { | |
path: string | |
access?: number | |
created?: number | |
lock?: string | |
inode?: LinkedList<MemoryBlock> | |
children?: number[] // Number pointing to File memory blocks | |
} | |
// A file is a special type of allocated memory. | |
// To ensure flexibility in storing large datasets, | |
// memory is broken up into smaller memory modules | |
// called inodes. | |
export class File { | |
metadata: { | |
access: number // See Linux access numbers (10B) | |
created: number // Timestamp (41B) | |
lock: string // Is there a process locking this file (~8B) | |
} | |
path: string | |
children?: number[] // Directories have children | |
inode?: LinkedList<MemoryBlock> // First Inode is metadata | |
constructor(options: FileOptions) { | |
this.children = options.children | |
this.inode = options.inode | |
this.path = options.path | |
this.metadata = { | |
access: options.access || 777, | |
created: options.created || Date.now(), | |
lock: options.lock || '' | |
} | |
} | |
isFile() { | |
return this.children === undefined | |
} | |
isDirectory() { | |
return this.children !== undefined | |
} | |
isLocked() { | |
return this.metadata.lock !== '' | |
} | |
write(datum: string, memAddress: number, inodeSize: number) { | |
if (this.isDirectory()) throw new Error() | |
// Split data into inodeSize | |
this.inode = new LinkedList(memAddress) | |
for (let i = 0; i < datum.length; i += inodeSize) { | |
} | |
} | |
read(): string { return '' } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment