Compare commits

...

3 Commits

Author SHA1 Message Date
9d0b12b47c replace tree with fs/root.ts 2025-03-08 17:41:14 -05:00
e8645d4f64 directory.clone(file_name) 2025-03-08 17:41:05 -05:00
fc0ef23bdb types/entry.d.ts not needed 2025-03-08 17:40:47 -05:00
6 changed files with 121 additions and 72 deletions

51
src/rt/rfwfs/fs/root.ts Normal file
View File

@ -0,0 +1,51 @@
import { Permissions } from "../enum"
import rfwfs from "../main"
const time_now = (Date.now()/1000)|0
// ------------ Home ------------
// /home/user/.config
const config = rfwfs.directory(".config", Permissions.rw, time_now)
// /home/user/.local
const local = rfwfs.directory(".local", Permissions.rw, time_now)
// /home/user/Downloads
const downloads = rfwfs.directory("Downloads", Permissions.rw, time_now)
// /home/user/Pictures
const pictures = rfwfs.directory("Pictures", Permissions.rw, time_now)
// /home/user/Videos
const videos = rfwfs.directory("Videos", Permissions.rw, time_now)
// /home/user/Music
const music = rfwfs.directory("Music", Permissions.rw, time_now)
// /home/user/Desktop
const desktop = rfwfs.directory("Desktop", Permissions.rw, time_now)
// /home/user
const user = rfwfs.directory("user", Permissions.r, time_now, [
config,
local,
downloads,
pictures,
videos,
music,
desktop,
])
// /home/
const home = rfwfs.directory("home", Permissions.r, time_now, [user])
// ------------
// ------------ root ------------
const bin = rfwfs.directory("bin", Permissions.r, time_now, [])
// ------------
export default rfwfs.directory("/", Permissions.r, time_now, [
bin,
home,
])

View File

@ -1,4 +1,4 @@
import { type Entry } from "./types/entry"
import { type Entry } from "./main"
import { wrap_bsearch, type WrapBSearch } from "./wrap"
export default function directory_search<T extends Entry>(entry_collection: T[], file_name: string): WrapBSearch<T> | undefined {

View File

@ -1,14 +1,43 @@
import { EntryType, PushStatus, ReadStatus, Permissions } from "./enum"
import { Entry, EntryCollection, FileInner, EntryFile, EntryCollectionManipulate, EntryFileInner } from "./types/entry"
import directory_search from "./index"
import { wrap_entry, wrap_none, WrapResultEntry, WrapResultNone } from "./wrap"
import directory_search from "./index"
type FileInner = string | number
interface Entry {
name: string,
timestamp: number,
permissions: Permissions,
//please do not change the inner values directly on entries or else there will be catastrophic consequences
readonly type: EntryType,
}
interface EntryFileInner {
__body: FileInner,
write: (item: FileInner) => boolean,
read: () => FileInner | undefined,
}
interface EntryFile extends Entry {
inner: EntryFileInner,
hash: string,
}
interface EntryCollection<T extends Entry> extends Entry {
inner: EntryCollectionManipulate<T>,
}
interface EntryCollectionManipulate<T extends Entry> {
__body: T[],
clone: (file_name: string) => WrapResultEntry<T, ReadStatus>
find: (file_name: string) => WrapResultEntry<T, ReadStatus>
push: (entry: Entry) => WrapResultNone<PushStatus>,
sort: () => void,
pop: (file_name: string) => WrapResultEntry<T, ReadStatus>,
}
interface Rfwfs {
directory: <T extends Entry>(name: string, permissions: Permissions, timestamp: number, inner_default: T[]) => EntryCollection<T>,
directory: <T extends Entry>(default_name: string, permissions: Permissions, timestamp: number, inner_default?: T[]) => EntryCollection<T>,
is_file: <T extends Entry>(entry: T) => boolean,
is_dir: <T extends Entry>(entry: T) => boolean,
file: (name: string, permissions: Permissions, timestamp: number, inner_default: FileInner) => EntryFile,
file: (default_name: string, permissions: Permissions, timestamp: number, inner_default?: FileInner) => EntryFile,
}
function read_write_access(permissions: Permissions): boolean {
@ -61,6 +90,17 @@ function directory_pop<E extends Entry>(self: EntryCollection<E>, file_name: str
return wrap_entry(ReadStatus.Denied)
}
function directory_clone<E extends Entry>(self: EntryCollection<E>, file_name: string): WrapResultEntry<E, ReadStatus> {
if (read_write_access(self.permissions)) {
const clone_find = directory_search(self.inner.__body, file_name)
if (clone_find) {
return wrap_entry(ReadStatus.Ok, { ...clone_find.result })
}
return wrap_entry(ReadStatus.NotFound)
}
return wrap_entry(ReadStatus.Denied)
}
function file_inner_read(self: EntryFileInner, permissions: Permissions): FileInner | undefined {
return read_access(permissions) ? self.__body : undefined
}
@ -82,10 +122,11 @@ function file_inner(permissions: Permissions, inner_default: FileInner): EntryFi
function dir_inner<T extends Entry>(self: EntryCollection<T>, collection: T[]): EntryCollectionManipulate<T> {
const collection_trait = { __body: collection } as EntryCollectionManipulate<T>
collection_trait.pop = function(file_name) { return directory_pop(self, file_name) }
collection_trait.find = function(file_name) { return directory_find(self, file_name) }
collection_trait.push = function(entry) { return directory_push(self, entry) }
collection_trait.sort = function() { return directory_sort(this) }
collection_trait.clone = function(file_name) { return directory_clone(self, file_name) }
collection_trait.pop = function(file_name) { return directory_pop(self, file_name) }
collection_trait.find = function(file_name) { return directory_find(self, file_name) }
collection_trait.push = function(entry) { return directory_push(self, entry) }
collection_trait.sort = function() { return directory_sort(this) }
collection_trait.sort() //the default collection is automatically sorted on directory creation.
return collection_trait
}
@ -99,23 +140,31 @@ rfwfs.is_file = function(entry) {
return entry.type === EntryType.File
}
rfwfs.file = function(name, permissions, timestamp, inner_default) {
rfwfs.file = function(default_name, permissions, timestamp, inner_default) {
const file = { type: EntryType.File } as EntryFile
file.permissions = permissions
file.timestamp = timestamp
file.inner = file_inner(permissions, inner_default)
file.name = name
file.inner = file_inner(permissions, inner_default ? inner_default : "")
file.name = default_name
file.hash = "0"
return file
}
rfwfs.directory = function<T extends Entry>(name: string, permissions: Permissions, timestamp: number, inner_default: T[]): EntryCollection<T> {
rfwfs.directory = function<T extends Entry>(default_name: string, permissions: Permissions, timestamp: number, inner_default?: T[]): EntryCollection<T> {
const directory = { type: EntryType.Directory } as EntryCollection<T>
directory.permissions = permissions
directory.timestamp = timestamp
directory.inner = dir_inner(directory, inner_default)
directory.name = name
directory.inner = dir_inner(directory, inner_default ? inner_default : [])
directory.name = default_name
return directory
}
export default rfwfs
export default rfwfs
export {
type FileInner,
type Entry,
type EntryFile,
type EntryFileInner,
type EntryCollection,
type EntryCollectionManipulate,
}

View File

@ -1,11 +0,0 @@
import rfwfs, { Permissions } from "./main";
const time_now = (Date.now()/1000)|0
const fs = rfwfs.new_collection([
rfwfs.new_entry("/", Permissions.r, time_now, rfwfs.new_collection([
rfwfs.new_entry("home", Permissions.r, time_now, "hi")
]))
])
export default fs

View File

@ -1,42 +0,0 @@
import { Permissions, ReadStatus } from "../enum"
import { WrapResultEntry } from "../wrap"
type FileInner = string | number
type ConstEnum = number
interface Entry {
name: string,
timestamp: number,
permissions: Permissions,
//please do not change the inner values directly on entries or else there will be catastrophic consequences
readonly type: EntryType,
}
interface EntryFileInner {
__body: FileInner,
write: (item: FileInner) => boolean,
read: () => FileInner | undefined,
}
interface EntryFile extends Entry {
inner: EntryFileInner,
hash: string,
}
interface EntryCollection<T extends Entry> extends Entry {
inner: EntryCollectionManipulate<T>,
}
interface EntryCollectionManipulate<T extends Entry> {
__body: T[],
find: (file_name: string) => WrapResultEntry<T, ReadStatus>
push: (entry: Entry) => PushStatus,
sort: () => void,
pop: (file_name: string) => WrapResultEntry<T, ReadStatus>,
}
export {
FileInner,
ConstEnum,
Entry,
EntryFile,
EntryFileInner,
EntryCollection,
EntryCollectionManipulate,
}

View File

@ -1,5 +1,7 @@
import { Result } from "./enum"
import { ConstEnum, Entry } from "./types/entry"
import { type Entry } from "./main"
type ConstEnum = number
type WrapResultEntry<T extends Entry, U> = WrapResult<T | undefined, U>
type WrapBSearch<T extends Entry> = WrapResult<T, number>
@ -32,7 +34,7 @@ export {
wrap_entry,
wrap_none,
type WrapResultEntry,
type WrapBSearch,
type WrapResultNone,
type WrapBSearch,
type WrapResult,
}