From b302b535c3abde738887a7ca4598d7b5a2c7179e Mon Sep 17 00:00:00 2001 From: krcroft Date: Sat, 15 Feb 2020 21:00:58 -0800 Subject: [PATCH] Add DEBUG messages for protected-file handling --- include/support.h | 2 ++ src/dos/drive_local.cpp | 43 ++++++++++++++++++++++++++++++++++++----- src/misc/support.cpp | 18 +++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/include/support.h b/include/support.h index 4da74234..65a33c4a 100644 --- a/include/support.h +++ b/include/support.h @@ -32,6 +32,8 @@ #define strncasecmp(a, b, n) _strnicmp(a, b, n) #endif +std::string get_basename(const std::string& filename); + // Include a message in assert, similar to static_assert: #define assertm(exp, msg) assert(((void)msg, exp)) // Use (void) to silent unused warnings. diff --git a/src/dos/drive_local.cpp b/src/dos/drive_local.cpp index 1f923890..e49be66a 100644 --- a/src/dos/drive_local.cpp +++ b/src/dos/drive_local.cpp @@ -16,6 +16,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +// Uncomment to enable file-open diagnostic messages +// #define DEBUG 1 #include #include @@ -103,6 +105,18 @@ bool localDrive::FileOpen(DOS_File** file, char * name, Bit32u flags) { FILE* fhandle = fopen(newname, type); +#ifdef DEBUG + std::string open_msg; + std::string flags_str; + switch (flags & 0xf) { + case OPEN_READ: flags_str = "R"; break; + case OPEN_WRITE: flags_str = "W"; break; + case OPEN_READWRITE: flags_str = "RW"; break; + case OPEN_READ_NO_MOD: flags_str = "RN"; break; + default: flags_str = "--"; + } +#endif + // If we couldn't open the file, then it's possibile that // the file is simply write-protected and the flags requested // RW access. So check if this is the case: @@ -110,9 +124,10 @@ bool localDrive::FileOpen(DOS_File** file, char * name, Bit32u flags) { // If yes, check if the file can be opened with Read-only access: fhandle = fopen_wrap(newname, "rb"); if (fhandle) { - // Ok! so the file is present but write-protected file. - // Re-apply the same flag DOS program requested: - flags &= ~(OPEN_READWRITE | OPEN_WRITE); + +#ifdef DEBUG + open_msg = "wanted writes but opened read-only"; +#else // Inform the user that the file is being protected against modification. // If the DOS program /really/ needs to write to the file, it will // crash/exit and this will be one of the last messages on the screen, @@ -121,11 +136,29 @@ bool localDrive::FileOpen(DOS_File** file, char * name, Bit32u flags) { if (IsFirstEncounter(newname)) { // For brevity and clarity to the user, we show just the // filename instead of the more cluttered absolute path. - const uint16_t delim = std::string(newname).find_last_of("/\\") + 1; - LOG_MSG("FILESYSTEM: protected from modification: %s", newname + delim); + LOG_MSG("FILESYSTEM: protected from modification: %s", + get_basename(newname).c_str()); } +#endif } + +#ifdef DEBUG + else { + open_msg += "failed desired and with read-only"; + } +#endif } + +#ifdef DEBUG + else { + open_msg = "succeeded with desired flags"; + } + LOG_MSG("FILESYSTEM: flags=%2s, %-12s %s", + flags_str.c_str(), + get_basename(newname).c_str(), + open_msg.c_str()); +#endif + if (fhandle) { *file = new localFile(name, fhandle); (*file)->flags = flags; // for the inheritance flag and maybe check for others. diff --git a/src/misc/support.cpp b/src/misc/support.cpp index 823067af..2bd8db41 100644 --- a/src/misc/support.cpp +++ b/src/misc/support.cpp @@ -34,6 +34,24 @@ #include "support.h" #include "video.h" +std::string get_basename(const std::string& filename) { + // Guard against corner cases: '', '/', '\', 'a' + if (filename.length() <= 1) + return filename; + + // Find the last slash, but if not is set to zero + size_t slash_pos = filename.find_last_of("/\\"); + + // If the slash is the last character + if (slash_pos == filename.length() - 1) + slash_pos = 0; + + // Otherwise if the slash is found mid-string + else if (slash_pos > 0) + slash_pos++; + return filename.substr(slash_pos); +} + void upcase(std::string &str) { int (*tf)(int) = std::toupper;