Skip to content

Commit 2b73f43

Browse files
authored
Merge pull request #44 from cortex-command-community/filesystem-function-improvements
Let filesystem functions be slightly less strict
2 parents aeb99f3 + 6341417 commit 2b73f43

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
120120

121121
- New `GameActivity::LockControlledActor` Lua function to allow grab player input in the way menus (buy menu/full inventorymenu) do.
122122

123-
- New `LuaMan` Lua I/O functions `DirectoryCreate`, `FileExists`, `DirectoryExists`, `FileRemove`, `DirectoryRemove`, `FileRename`, `DirectoryRename` and `IsValidModulePath`.
123+
- New `LuaMan` Lua I/O functions `DirectoryCreate`, `FileExists`, `DirectoryExists`, `FileRemove`, `DirectoryRemove`, `FileRename` and `DirectoryRename`.
124124

125125
- New `FrameMan` Lua functions `SetHudDisabled(disabled, screenId)` and `IsHudDisabled(screenId)` that allows disabling a given screen's HUD, and checking whether it's currently disabled. The screenId parameters are optional and default to screen 0.
126126

Source/Managers/LuaMan.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ void LuaStateWrapper::Initialize() {
7878
.def("GetFileList", &LuaStateWrapper::FileList, luabind::adopt(luabind::return_value) + luabind::return_stl_iterator)
7979
.def("FileExists", &LuaStateWrapper::FileExists)
8080
.def("DirectoryExists", &LuaStateWrapper::DirectoryExists)
81-
.def("IsValidModulePath", &LuaStateWrapper::IsValidModulePath)
8281
.def("FileOpen", &LuaStateWrapper::FileOpen)
8382
.def("FileClose", &LuaStateWrapper::FileClose)
8483
.def("FileRemove", &LuaStateWrapper::FileRemove)
@@ -278,7 +277,6 @@ const std::vector<std::string>* LuaStateWrapper::DirectoryList(const std::string
278277
const std::vector<std::string>* LuaStateWrapper::FileList(const std::string& path) { return g_LuaMan.FileList(path); }
279278
bool LuaStateWrapper::FileExists(const std::string& path) { return g_LuaMan.FileExists(path); }
280279
bool LuaStateWrapper::DirectoryExists(const std::string& path) { return g_LuaMan.DirectoryExists(path); }
281-
bool LuaStateWrapper::IsValidModulePath(const std::string& path) { return g_LuaMan.IsValidModulePath(path); }
282280
int LuaStateWrapper::FileOpen(const std::string& path, const std::string& accessMode) { return g_LuaMan.FileOpen(path, accessMode); }
283281
void LuaStateWrapper::FileClose(int fileIndex) { return g_LuaMan.FileClose(fileIndex); }
284282
void LuaStateWrapper::FileCloseAll() { return g_LuaMan.FileCloseAll(); }
@@ -885,17 +883,17 @@ LuaMan::~LuaMan() {
885883
}
886884

887885
const std::vector<std::string>* LuaMan::DirectoryList(const std::string& path) {
888-
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
886+
std::string fullPath = System::GetWorkingDirectory() + path;
889887
auto* directoryPaths = new std::vector<std::string>();
890888

891-
if (IsValidModulePath(fullPath)) {
889+
if (fullPath.find("..") == std::string::npos) {
892890
#ifndef _WIN32
893891
fullPath = GetCaseInsensitiveFullPath(fullPath);
894892
#endif
895893
if (std::filesystem::exists(fullPath)) {
896-
for (const std::filesystem::directory_entry& directoryEntry: std::filesystem::directory_iterator(fullPath)) {
897-
if (directoryEntry.is_directory()) {
898-
directoryPaths->emplace_back(directoryEntry.path().filename().generic_string());
894+
for (const auto& entry: std::filesystem::directory_iterator(fullPath)) {
895+
if (entry.is_directory()) {
896+
directoryPaths->emplace_back(entry.path().filename().generic_string());
899897
}
900898
}
901899
}
@@ -904,17 +902,17 @@ const std::vector<std::string>* LuaMan::DirectoryList(const std::string& path) {
904902
}
905903

906904
const std::vector<std::string>* LuaMan::FileList(const std::string& path) {
907-
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
905+
std::string fullPath = System::GetWorkingDirectory() + path;
908906
auto* filePaths = new std::vector<std::string>();
909907

910-
if (IsValidModulePath(fullPath)) {
908+
if (fullPath.find("..") == std::string::npos) {
911909
#ifndef _WIN32
912910
fullPath = GetCaseInsensitiveFullPath(fullPath);
913911
#endif
914912
if (std::filesystem::exists(fullPath)) {
915-
for (const std::filesystem::directory_entry& directoryEntry: std::filesystem::directory_iterator(fullPath)) {
916-
if (directoryEntry.is_regular_file()) {
917-
filePaths->emplace_back(directoryEntry.path().filename().generic_string());
913+
for (const auto& entry: std::filesystem::directory_iterator(fullPath)) {
914+
if (entry.is_regular_file()) {
915+
filePaths->emplace_back(entry.path().filename().generic_string());
918916
}
919917
}
920918
}
@@ -924,7 +922,7 @@ const std::vector<std::string>* LuaMan::FileList(const std::string& path) {
924922

925923
bool LuaMan::FileExists(const std::string& path) {
926924
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
927-
if (IsValidModulePath(fullPath)) {
925+
if (fullPath.find("..") == std::string::npos) {
928926
#ifndef _WIN32
929927
fullPath = GetCaseInsensitiveFullPath(fullPath);
930928
#endif
@@ -935,7 +933,7 @@ bool LuaMan::FileExists(const std::string& path) {
935933

936934
bool LuaMan::DirectoryExists(const std::string& path) {
937935
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
938-
if (IsValidModulePath(fullPath)) {
936+
if (fullPath.find("..") == std::string::npos) {
939937
#ifndef _WIN32
940938
fullPath = GetCaseInsensitiveFullPath(fullPath);
941939
#endif
@@ -1049,7 +1047,7 @@ bool LuaMan::FileRemove(const std::string& path) {
10491047

10501048
bool LuaMan::DirectoryCreate(const std::string& path, bool recursive) {
10511049
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
1052-
if (IsValidModulePath(fullPath)) {
1050+
if (fullPath.find("..") == std::string::npos) {
10531051
#ifndef _WIN32
10541052
fullPath = GetCaseInsensitiveFullPath(fullPath);
10551053
#endif
@@ -1067,7 +1065,7 @@ bool LuaMan::DirectoryCreate(const std::string& path, bool recursive) {
10671065

10681066
bool LuaMan::DirectoryRemove(const std::string& path, bool recursive) {
10691067
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
1070-
if (IsValidModulePath(fullPath)) {
1068+
if (fullPath.find("..") == std::string::npos) {
10711069
#ifndef _WIN32
10721070
fullPath = GetCaseInsensitiveFullPath(fullPath);
10731071
#endif
@@ -1109,7 +1107,7 @@ bool LuaMan::FileRename(const std::string& oldPath, const std::string& newPath)
11091107
bool LuaMan::DirectoryRename(const std::string& oldPath, const std::string& newPath) {
11101108
std::string fullOldPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(oldPath);
11111109
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
1112-
if (IsValidModulePath(fullOldPath) && IsValidModulePath(fullNewPath)) {
1110+
if (fullOldPath.find("..") == std::string::npos && fullNewPath.find("..") == std::string::npos) {
11131111
#ifndef _WIN32
11141112
fullOldPath = GetCaseInsensitiveFullPath(fullOldPath);
11151113
fullNewPath = GetCaseInsensitiveFullPath(fullNewPath);

Source/Managers/LuaMan.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ namespace RTE {
214214
const std::vector<std::string>* FileList(const std::string& path);
215215
bool FileExists(const std::string& path);
216216
bool DirectoryExists(const std::string& path);
217-
bool IsValidModulePath(const std::string& path);
218217
int FileOpen(const std::string& path, const std::string& accessMode);
219218
void FileClose(int fileIndex);
220219
void FileCloseAll();

0 commit comments

Comments
 (0)