From 8ba5199e5462a1f7b9f3c09ade88e97e5a4e2301 Mon Sep 17 00:00:00 2001 From: Nicholas Colclasure Date: Fri, 17 Dec 2021 19:59:41 -0800 Subject: [PATCH 1/2] Fix problems finding root files in cat and ns.read Cat ends up translating the path it receives from relative to absolute twice, which I fix by changing the filename to an absolute path before it's passed to getTextFile with a leading "/" so that it doesn't interpret the filename as being relative. Read I fixed by causing getTextFile to remove the leading "/" from files that are in the root directory, since that is required to translate their name into the native "filesystem"s format. --- src/Terminal/commands/cat.ts | 2 +- src/TextFile.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Terminal/commands/cat.ts b/src/Terminal/commands/cat.ts index a0234e44b..eab054cfc 100644 --- a/src/Terminal/commands/cat.ts +++ b/src/Terminal/commands/cat.ts @@ -39,7 +39,7 @@ export function cat( } } } else if (filename.endsWith(".txt")) { - const txt = terminal.getTextFile(player, filename); + const txt = terminal.getTextFile(player, "/" + filename); if (txt != null) { txt.show(); return; diff --git a/src/TextFile.ts b/src/TextFile.ts index 42ae4e500..cf77058c3 100644 --- a/src/TextFile.ts +++ b/src/TextFile.ts @@ -1,6 +1,7 @@ import { dialogBoxCreate } from "./ui/React/DialogBox"; import { BaseServer } from "./Server/BaseServer"; import { Generic_fromJSON, Generic_toJSON, Reviver } from "./utils/JSONReviver"; +import { removeLeadingSlash, isInRootDirectory } from "./Terminal/DirectoryHelpers" /** * Represents a plain text file that is typically stored on a server. @@ -101,7 +102,11 @@ Reviver.constructors.TextFile = TextFile; */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export function getTextFile(fn: string, server: BaseServer): TextFile | null { - const filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn; + let filename: string = !fn.endsWith(".txt") ? `${fn}.txt` : fn; + + if (isInRootDirectory(filename)) { + filename = removeLeadingSlash(filename); + } for (const file of server.textFiles as TextFile[]) { if (file.fn === filename) { From 61dde4cfac5cb2c5a7daa0c4d4051260cfb588d5 Mon Sep 17 00:00:00 2001 From: Nicholas Colclasure Date: Sun, 19 Dec 2021 13:03:50 -0800 Subject: [PATCH 2/2] Fix issue introduced handling files in subdirectories ._. --- src/Terminal/DirectoryHelpers.ts | 2 +- src/Terminal/commands/cat.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Terminal/DirectoryHelpers.ts b/src/Terminal/DirectoryHelpers.ts index 00599feac..04ee8c64e 100644 --- a/src/Terminal/DirectoryHelpers.ts +++ b/src/Terminal/DirectoryHelpers.ts @@ -185,7 +185,7 @@ export function getDestinationFilepath(destination: string, source: string, cwd: return destination; } else { // Append the filename to the directory provided. - let t_path = removeTrailingSlash(dstDir); + const t_path = removeTrailingSlash(dstDir); const fileName = getFileName(source); return t_path + "/" + fileName; } diff --git a/src/Terminal/commands/cat.ts b/src/Terminal/commands/cat.ts index eab054cfc..22453571e 100644 --- a/src/Terminal/commands/cat.ts +++ b/src/Terminal/commands/cat.ts @@ -16,7 +16,8 @@ export function cat( terminal.error("Incorrect usage of cat command. Usage: cat [file]"); return; } - const filename = terminal.getFilepath(args[0] + ""); + const relative_filename = args[0] + ""; + const filename = terminal.getFilepath(relative_filename); if (!filename.endsWith(".msg") && !filename.endsWith(".lit") && !filename.endsWith(".txt")) { terminal.error( "Only .msg, .txt, and .lit files are viewable with cat (filename must end with .msg, .txt, or .lit)", @@ -39,7 +40,7 @@ export function cat( } } } else if (filename.endsWith(".txt")) { - const txt = terminal.getTextFile(player, "/" + filename); + const txt = terminal.getTextFile(player, relative_filename); if (txt != null) { txt.show(); return;