mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
Merge pull request #3428 from Chris380/bugfix/2367_reject_cd_to_invalid_folders
Reject cd to invalid folders
This commit is contained in:
@@ -51,3 +51,14 @@ export function getSubdirectories(serv: BaseServer, dir: string): string[] {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true, if the server's directory itself or one of its subdirectory contains files.
|
||||
*/
|
||||
export function containsFiles(server: BaseServer, dir: string): boolean {
|
||||
const dirWithTrailingSlash = dir + (dir.slice(-1) === "/" ? "" : "/");
|
||||
|
||||
return [...server.scripts.map((s) => s.filename), ...server.textFiles.map((t) => t.fn)].some((filename) =>
|
||||
filename.startsWith(dirWithTrailingSlash),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { IPlayer } from "../../PersonObjects/IPlayer";
|
||||
import { BaseServer } from "../../Server/BaseServer";
|
||||
|
||||
import { evaluateDirectoryPath, removeTrailingSlash } from "../DirectoryHelpers";
|
||||
import { containsFiles } from "../DirectoryServerHelpers";
|
||||
|
||||
export function cd(
|
||||
terminal: ITerminal,
|
||||
@@ -31,10 +32,7 @@ export function cd(
|
||||
}
|
||||
|
||||
const server = player.getCurrentServer();
|
||||
if (
|
||||
!server.scripts.some((script) => script.filename.startsWith(evaledDir + "")) &&
|
||||
!server.textFiles.some((file) => file.fn.startsWith(evaledDir + ""))
|
||||
) {
|
||||
if (!containsFiles(server, evaledDir)) {
|
||||
terminal.error("Invalid path. Failed to change directories");
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user