new structure and library structure
This commit is contained in:
46
src/rt/rfwfs/collection.ts
Normal file
46
src/rt/rfwfs/collection.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { type Entry } from "./main"
|
||||
|
||||
import entry_search from "./index"
|
||||
|
||||
type Files<T> = Entry<T>[]
|
||||
|
||||
interface EntryCollectionManipulate {
|
||||
pop: <T extends Entry<T>>(file_name: string) => Entry<T> | undefined,
|
||||
get: <T extends Entry<T>>(file_name: string) => Entry<T> | undefined
|
||||
push: <T extends Entry<T>>(entry: Entry<T>) => boolean,
|
||||
sort: () => void,
|
||||
}
|
||||
interface EntryCollection<T> extends EntryCollectionManipulate {
|
||||
readonly inner: Files<T>
|
||||
}
|
||||
|
||||
function entry_trait<T extends Entry<T>>() {
|
||||
const trait = {} as EntryCollection<T>
|
||||
trait.sort = function() {
|
||||
this.inner.sort((a,z) => a.name.localeCompare(z.name))
|
||||
}
|
||||
trait.push = function(entry) {
|
||||
const no_duplicates = entry_search(this.inner, entry.name)
|
||||
if (!no_duplicates) {
|
||||
this.push(entry)
|
||||
this.sort()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
trait.pop = function(file_name) {
|
||||
const file_search = entry_search(this.inner, file_name)
|
||||
if (file_search) {
|
||||
this.inner.splice(file_search.index, 1)
|
||||
return file_search.result
|
||||
}
|
||||
return
|
||||
}
|
||||
return trait
|
||||
}
|
||||
|
||||
export {
|
||||
entry_trait,
|
||||
type EntryCollection,
|
||||
type Files,
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
import rfwfs_search from "./index"
|
||||
import rfwfs_entry_trait, {EntryType, type Entry, type EntryTree} from "./entry"
|
||||
|
||||
|
||||
|
||||
function Entry<T = File>(name: string, inner: T, permissions: Permissions): Entry<T> {
|
||||
return {
|
||||
type: typeof inner == "object" ? EntryType.Directory : EntryType.File,
|
||||
inner: inner,
|
||||
name: name,
|
||||
permissions: permissions,
|
||||
timestamp: Math.floor(Date.now()/1000)
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
EntryType,
|
||||
Permissions,
|
||||
Entry
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
import rfwfs_search from "./index"
|
||||
|
||||
const enum EntryType {
|
||||
Directory,
|
||||
File
|
||||
}
|
||||
|
||||
const enum Permissions {
|
||||
r,
|
||||
w,
|
||||
rw,
|
||||
none
|
||||
}
|
||||
|
||||
type Files<T> = Entry<T>[]
|
||||
type File = string
|
||||
|
||||
interface EntryListManipulate {
|
||||
push: <T extends Entry<T>>(entry: Entry<T>) => boolean,
|
||||
pop: <T extends Entry<T>>(file_name: string) => Entry<T> | undefined,
|
||||
sort: () => void,
|
||||
}
|
||||
|
||||
interface EntryList<T> extends EntryListManipulate {
|
||||
readonly tree: Files<T>
|
||||
}
|
||||
|
||||
interface Entry<T = File> {
|
||||
readonly inner: EntryList<T>,
|
||||
readonly name: string,
|
||||
readonly type: EntryType,
|
||||
readonly timestamp: number,
|
||||
readonly permissions: Permissions,
|
||||
}
|
||||
|
||||
function rfwfs_entry_trait<T extends Entry<T>>(): EntryList<T> {
|
||||
const trait = {} as EntryList<T>
|
||||
trait.sort = function() {
|
||||
this.tree.sort((a,z) => a.name.localeCompare(z.name))
|
||||
}
|
||||
trait.push = function(entry) {
|
||||
const no_duplicates = rfwfs_search.binary_fs_name(this.tree, entry.name)
|
||||
if (!no_duplicates) {
|
||||
this.tree.push(entry)
|
||||
this.sort()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
trait.pop = function(file_name) {
|
||||
const file_search = rfwfs_search.binary_fs_name(this.tree, file_name)
|
||||
if (file_search) {
|
||||
this.tree.splice(file_search.binary_index, 1)
|
||||
return file_search.item
|
||||
}
|
||||
return
|
||||
}
|
||||
return trait
|
||||
}
|
||||
|
||||
function rfwfs_new_entry_tree<T>(files: Files<T>) {
|
||||
|
||||
}
|
||||
|
||||
function rfwfs_new_entry<T extends EntryList<T>>(
|
||||
name: string,
|
||||
inner: T,
|
||||
permissions: Permissions,
|
||||
timestamp: number = Math.floor(Date.now()/1000)
|
||||
): Entry<T> {
|
||||
return {
|
||||
type: typeof inner == "object" ? EntryType.Directory : EntryType.File,
|
||||
timestamp: timestamp,
|
||||
name: name,
|
||||
permissions: permissions,
|
||||
inner: inner,
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
rfwfs_entry_trait,
|
||||
rfwfs_new_entry,
|
||||
EntryType,
|
||||
Permissions,
|
||||
type EntryList,
|
||||
type Entry,
|
||||
type Files,
|
||||
type File,
|
||||
}
|
@ -1,77 +1,28 @@
|
||||
import { type Entry, type Files } from "./entry"
|
||||
import { type Entry } from "./main"
|
||||
import { type Files } from "./collection"
|
||||
|
||||
interface SearchResult<T> {
|
||||
item: T,
|
||||
binary_index: number
|
||||
readonly result: T,
|
||||
readonly index: number
|
||||
}
|
||||
interface Search {
|
||||
binary_fs_name: <T extends Entry<T>>(cloned_list: Files<T>, file_name: string) => SearchResult<Entry<T>> | undefined,
|
||||
binary_nsort: <T>(list: T[], find: T, start?: number, end?: number) => SearchResult<T> | undefined,
|
||||
binary: <T>(list: T[], find: T) => SearchResult<T> | undefined,
|
||||
linear: <T>(list: T[], find: T) => SearchResult<T> | undefined,
|
||||
function wrap_result<T>(result: T, index: number): SearchResult<T> {
|
||||
return { result: result, index: index }
|
||||
}
|
||||
|
||||
function wrap_result<T>(item: T, binary_index: number): SearchResult<T> {
|
||||
return { item: item, binary_index: binary_index }
|
||||
}
|
||||
|
||||
const rfwfs_search = {} as Search
|
||||
|
||||
rfwfs_search.binary = function(list, find) {
|
||||
list.sort()
|
||||
export default function entry_search<T>(cloned_file_collection: Files<T>, file_name: string): SearchResult<Entry<T>> | undefined {
|
||||
let start = 0
|
||||
let end = list.length-1
|
||||
let end = cloned_file_collection.length-1
|
||||
while (start<=end) {
|
||||
const median = (start+end)>>1
|
||||
if (list[median] === find) {
|
||||
return wrap_result(list[median], median)
|
||||
} else if (list[median]<find) {
|
||||
start = median+1
|
||||
} else {
|
||||
end = median-1
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
const median_name = cloned_file_collection[median].name
|
||||
|
||||
rfwfs_search.binary_nsort = function(list, find, start = 0, end = list.length-1) {
|
||||
if (start>end) { return }
|
||||
const median = (start+end)>>1
|
||||
if (list[median] === find) {
|
||||
return wrap_result(list[median], median)
|
||||
}
|
||||
if (list[median]>find) {
|
||||
return this.binary_nsort(list, find, start, median-1)
|
||||
} else {
|
||||
return this.binary_nsort(list, find, median+1, end)
|
||||
}
|
||||
}
|
||||
|
||||
rfwfs_search.binary_fs_name = function(cloned_entry_list, file_name) {
|
||||
let start = 0
|
||||
let end = cloned_entry_list.length-1
|
||||
while (start<=end) {
|
||||
const median = (start+end)>>1
|
||||
const median_name = cloned_entry_list[median].name
|
||||
|
||||
if (median_name === file_name) {
|
||||
return wrap_result(cloned_entry_list[median], median)
|
||||
if (median_name == file_name) { //left == right && (T == U) is not necessary
|
||||
return wrap_result(cloned_file_collection[median], median)
|
||||
} else if (median_name<file_name) {
|
||||
start = median+1
|
||||
} else {
|
||||
end = median-1
|
||||
}
|
||||
}
|
||||
return
|
||||
return undefined
|
||||
}
|
||||
|
||||
rfwfs_search.linear = function(list, find) {
|
||||
for (let ind = 0; ind<list.length; ind++) {
|
||||
if (list[ind] === find) {
|
||||
return wrap_result(list[ind], ind)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export default rfwfs_search
|
@ -1,85 +0,0 @@
|
||||
import { type FsDirectory, type FsEntry } from "./core"
|
||||
|
||||
import fstree from "./tree"
|
||||
import index from "./index"
|
||||
|
||||
let cached_dir = fstree[0] //start at root
|
||||
let working_path = ["/", "home", "user"]
|
||||
|
||||
const clone_working_path = () => [...working_path]
|
||||
|
||||
function get_working_dir_name() {
|
||||
return working_path[working_path.length-1]
|
||||
}
|
||||
|
||||
function get_working_dir_name_full(): string {
|
||||
const w_dir_clone = clone_working_path()
|
||||
const root = w_dir_clone.shift()
|
||||
if (root) {
|
||||
return root+w_dir_clone.join("/")
|
||||
}
|
||||
return "shift-error"
|
||||
}
|
||||
|
||||
const enum SetDirStatus {
|
||||
Valid,
|
||||
NotFound,
|
||||
NotADirectory,
|
||||
Invalid
|
||||
}
|
||||
interface FsIterEntry {
|
||||
readonly entry: FsDirectory | null,
|
||||
readonly status: SetDirStatus
|
||||
}
|
||||
function find_fs_dir(working_dir_path_clone: string[], find_dir_name: string): FsIterEntry {
|
||||
let cached_dir_clone = cached_dir.inner
|
||||
|
||||
for (let path_i = 0; path_i<working_dir_path_clone.length; path_i++) {
|
||||
if (cached_dir_clone) {
|
||||
let cached_dir_file_names: string[] = []
|
||||
cached_dir_clone.forEach((file, file_i) => cached_dir_file_names.push(file.name))
|
||||
|
||||
const search_result = index.binary(cached_dir_clone, fstree[0])
|
||||
|
||||
if (working_dir_path_clone[path_i] === find_dir_name) {
|
||||
cached_dir_clone = cached_dir_clone
|
||||
if (path_i === working_dir_path_clone.length) {
|
||||
const search_result = index.binary(cached_dir_file_names, find_dir_name)
|
||||
if (search_result) {
|
||||
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
return { entry: null, status: SetDirStatus.Invalid }
|
||||
}
|
||||
return { entry: null, status: SetDirStatus.NotFound }
|
||||
}
|
||||
|
||||
function set_working_dir(name: string): SetDirStatus {
|
||||
if (name === ".") { return SetDirStatus.Valid }
|
||||
|
||||
const w_dir_clone = clone_working_path()
|
||||
if (name === "..") {
|
||||
w_dir_clone.pop()
|
||||
|
||||
} else {
|
||||
w_dir_clone.push(name)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
function get_working_dir_entries(): FsEntry[] {
|
||||
|
||||
}
|
||||
|
||||
export {
|
||||
get_working_dir_name,
|
||||
get_working_dir_name_full,
|
||||
get_working_dir_entries,
|
||||
set_working_dir,
|
||||
SetDirStatus
|
||||
}
|
47
src/rt/rfwfs/main.ts
Normal file
47
src/rt/rfwfs/main.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { type EntryCollection } from "./collection"
|
||||
|
||||
const enum EntryType {
|
||||
Directory,
|
||||
File
|
||||
}
|
||||
const enum Permissions {
|
||||
r,
|
||||
w,
|
||||
rw,
|
||||
none
|
||||
}
|
||||
|
||||
interface Entry<T> {
|
||||
readonly name: string,
|
||||
readonly type: EntryType,
|
||||
readonly inner: T,
|
||||
readonly timestamp: number,
|
||||
readonly collection?: EntryCollection<T>,
|
||||
readonly permissions: Permissions,
|
||||
}
|
||||
interface Rfwfs {
|
||||
new_entry: <T extends Entry<T>>(name: string, permissions: Permissions, timestamp: number, inner: T) => Entry<T>,
|
||||
entry_trait: <T extends Entry<T>>(inner: T) => EntryCollection<T>
|
||||
new_entry_tree: <T>(files: T) => T,
|
||||
}
|
||||
|
||||
const rfwfs = {} as Rfwfs
|
||||
|
||||
rfwfs.new_entry = function(name, permissions, timestamp, inner) {
|
||||
const file_type = typeof inner == "object" ? EntryType.Directory : EntryType.File
|
||||
return {
|
||||
name: name,
|
||||
type: file_type,
|
||||
inner: inner,
|
||||
timestamp: timestamp ? timestamp : Math.floor(Date.now()/1000),
|
||||
collection: file_type === EntryType.Directory ? this.entry_trait(inner) : undefined,
|
||||
permissions: permissions,
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
EntryType,
|
||||
Permissions,
|
||||
type EntryCollection,
|
||||
type Entry,
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
import { Entry, Permissions } from "./core"
|
||||
|
||||
const user = [
|
||||
Entry("about_me.txt", "about me inside", Permissions.rw),
|
||||
Entry("services.txt", "services inside", Permissions.rw),
|
||||
Entry("hi", [], Permissions.rw)
|
||||
]
|
||||
const home = [
|
||||
Entry("user", user, Permissions.rw)
|
||||
]
|
||||
const root = [
|
||||
Entry("home", home, Permissions.r),
|
||||
Entry("bin", {}, Permissions.r),
|
||||
]
|
||||
const fstree = [
|
||||
Entry("/", root, Permissions.r)
|
||||
]
|
||||
|
||||
export default fstree
|
Reference in New Issue
Block a user