Skip to content

Commit

Permalink
filesystem, terminal vars
Browse files Browse the repository at this point in the history
  • Loading branch information
stjet committed Nov 5, 2023
1 parent 4f5f924 commit c0f4d77
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 37 deletions.
66 changes: 57 additions & 9 deletions src/mingde/fs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

//file system stuff

export type Path = `/${string}`;

type FileObject = string;
export type FileObject = string;

//type DirectoryObject = Record<string, DirectoryObject | FileObject>; is illegal apparently
interface DirectoryObject {
export interface DirectoryObject {
[name: string]: FileObject | DirectoryObject,
}

Expand Down Expand Up @@ -42,8 +41,6 @@ export class FileSystemObject {
mod_parts = [];
} else {
let possible_path: string[] = current_path.split("/").slice(0, -up);
console.log(current_path.split("/"), up)
console.log(possible_path.length, 'a');
if (possible_path.length === 0) {
//went up more times than possible, outside of root, so just give them root
path = "/";
Expand All @@ -60,16 +57,67 @@ export class FileSystemObject {
} else {
path = current_path;
}
console.log(mod_parts);
for (let k = 0; k < mod_parts.length; k++) {
//do not add / if the path is just a /
path += path === "/" ? mod_parts[k] : "/" + mod_parts[k];
}
return path;
}
get_path_contents(_path: Path): DirectoryObject | FileObject | undefined {
//
return;
get_path_contents(path: Path): DirectoryObject | FileObject | undefined {
if (path === "/") {
return this.file_system;
} else {
const parts: string[] = path.split("/").slice(1);
let current_location: DirectoryObject | FileObject = this.file_system;
for (let i = 0; i < parts.length; i++) {
current_location = current_location[parts[i]];
if (typeof current_location === "undefined") {
return;
}
}
return current_location;
}
}
remove_path(path: Path) {
if (path === "/") {
//no!
return false;
} else {
const parts: string[] = path.split("/").slice(1);
//current_location is a pointer to this.file_system
let current_location: DirectoryObject | FileObject = this.file_system;
for (let i = 0; i < parts.length - 1; i++) {
current_location = current_location[parts[i]];
if (typeof current_location === "undefined") {
return false;
}
}
const final_part: string = parts[parts.length - 1];
delete current_location[final_part];
return true;
}
}
//returns success/failure
write_path(path: Path, new_content: FileObject | DirectoryObject): boolean {
if (path === "/") {
//no!
return false;
} else {
const parts: string[] = path.split("/").slice(1);
//current_location is a pointer to this.file_system
let current_location: DirectoryObject | FileObject = this.file_system;
for (let i = 0; i < parts.length - 1; i++) {
current_location = current_location[parts[i]];
if (typeof current_location === "undefined") {
return;
}
}
const final_part: string = parts[parts.length - 1];
//directory cannot be written into a file and vice versa, exception if the path doesn't exist yet (so files/directories can still be made)
if (typeof current_location[final_part] !== typeof new_content && typeof current_location[final_part] !== "undefined") return false;
current_location[final_part] = new_content;
return true;
}
}
};

17 changes: 14 additions & 3 deletions src/mingde/guards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WindowChangeEvent, WindowLike, WindowLikeType, WindowManager } from './wm.js';
import { DesktopBackgroundTypes, DesktopBackgroundInfo, Themes, THEMES_LIST } from './themes.js';
import { OpenWindowValue, ChangeCursorValue, ChangeCoordsValue, FocusWindowValue, ChangeThemeValue, ChangeSettingsValue, ReadFileSystemValue, CursorType } from './requests.js';
import { OpenWindowValue, ChangeCursorValue, ChangeCoordsValue, FocusWindowValue, ChangeThemeValue, ChangeSettingsValue, ReadFileSystemValue, WriteFileSystemValue, RemoveFileSystemValue, CursorType } from './requests.js';
import { DesktopTime } from './utils.js';
import { SETTINGS_KEYS } from './mutables.js';

Expand Down Expand Up @@ -110,13 +110,24 @@ export function isChangeSettingsValue(maybe_change_settings: any): maybe_change_
return true;
}

const FILE_SYSTEM_PERMISSIONS_ALL = ["read_all_file_system", "read_usr_file_system", "read_prg_file_system"];
export function isReadFileSystemValue(maybe_read_file_system: any): maybe_read_file_system is ReadFileSystemValue {
if (!FILE_SYSTEM_PERMISSIONS_ALL.includes(maybe_read_file_system?.permission_type) && typeof maybe_read_file_system?.path !== "string") return false;
if (!maybe_read_file_system?.permission_type.startsWith("read_") || !maybe_read_file_system?.permission_type.endsWith("_file_system") || typeof maybe_read_file_system?.path !== "string") return false;
if (!maybe_read_file_system?.path.startsWith('/')) return false;
return true;
}

export function isWriteFileSystemValue(maybe_write_file_system: any): maybe_write_file_system is WriteFileSystemValue {
if (!maybe_write_file_system?.permission_type.startsWith("write_") || !maybe_write_file_system?.permission_type.endsWith("_file_system") || typeof maybe_write_file_system?.path !== "string" || (typeof maybe_write_file_system?.content !== "string" && typeof maybe_write_file_system?.content !== "object")) return false;
if (!maybe_write_file_system?.path.startsWith('/')) return false;
return true;
}

export function isRemoveFileSystemValue(maybe_remove_file_system: any): maybe_remove_file_system is RemoveFileSystemValue {
if (!maybe_remove_file_system?.permission_type.startsWith("write_") || !maybe_remove_file_system?.permission_type.endsWith("_file_system") || typeof maybe_remove_file_system?.path !== "string") return false;
if (!maybe_remove_file_system?.path.startsWith('/')) return false;
return true;
}

//typescript doesn't autodetect that render_view_window exists on Window? idk why, hope this doesn't cause any problems
export function isWindow(maybe_window: any): maybe_window is Window {
//instanceof Window doesn't work here, idk why
Expand Down
7 changes: 6 additions & 1 deletion src/mingde/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export interface Permission {
read_all_file_system?: boolean;
read_usr_file_system?: boolean;
read_prg_file_system?: boolean;
write_all_file_system?: boolean;
write_usr_file_system?: boolean;
write_prg_file_system?: boolean;
}

export type FILE_SYSTEM_PERMISSIONS = "read_all_file_system" | "read_usr_file_system" | "read_prg_file_system";
export type READ_FILE_SYSTEM_PERMISSIONS = `read_${string}_file_system`;

export type WRITE_FILE_SYSTEM_PERMISSIONS = `write_${string}_file_system`;

export type Permissions = Record<string, Permission>;

Expand Down
22 changes: 19 additions & 3 deletions src/mingde/requests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Themes } from './themes.js';
import type { WindowManagerSettings } from './mutables.js';
import type { FILE_SYSTEM_PERMISSIONS } from './registry.js';
import type { Path } from './fs.js';
import type { READ_FILE_SYSTEM_PERMISSIONS, WRITE_FILE_SYSTEM_PERMISSIONS } from './registry.js';
import type { Path, FileObject, DirectoryObject } from './fs.js';

//requests are messages but propogate up from windowlike to wm
export enum WindowRequest {
Expand All @@ -14,6 +14,8 @@ export enum WindowRequest {
ChangeTheme = "ChangeTheme", //also a WindowMessage called this, that won't be confusing at all...
ChangeSettings = "ChangeSettings",
ReadFileSystem = "ReadFileSystem",
WriteFileSystem = "WriteFileSystem",
RemoveFileSystem = "RemoveFileSystem",
}

//should be extended if requests need additional values, eg, OpenWindow request needs to know what window to open
Expand Down Expand Up @@ -64,7 +66,19 @@ export interface ChangeSettingsValue extends WindowRequestValue {
}

export interface ReadFileSystemValue extends WindowRequestValue {
permission_type: FILE_SYSTEM_PERMISSIONS;
permission_type: READ_FILE_SYSTEM_PERMISSIONS;
path: Path;
}

//append
export interface WriteFileSystemValue extends WindowRequestValue {
permission_type: WRITE_FILE_SYSTEM_PERMISSIONS;
path: Path;
content: FileObject | DirectoryObject;
}

export interface RemoveFileSystemValue extends WindowRequestValue {
permission_type: WRITE_FILE_SYSTEM_PERMISSIONS;
path: Path;
}

Expand All @@ -78,5 +92,7 @@ export interface WindowRequestValues {
[WindowRequest.ChangeTheme]: ChangeThemeValue;
[WindowRequest.ChangeSettings]: ChangeSettingsValue;
[WindowRequest.ReadFileSystem]: ReadFileSystemValue;
[WindowRequest.WriteFileSystem]: WriteFileSystemValue;
[WindowRequest.RemoveFileSystem]: RemoveFileSystemValue;
}

Loading

0 comments on commit c0f4d77

Please sign in to comment.