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.
This commit is contained in:
Nicholas Colclasure 2021-12-17 19:59:41 -08:00
parent eb002d655a
commit 8ba5199e54
2 changed files with 7 additions and 2 deletions

@ -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;

@ -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) {