From 2a9ab35d6c666acd71acc8f3a8ff803a188d85b2 Mon Sep 17 00:00:00 2001 From: Toni Wilen Date: Sat, 6 Mar 2021 17:02:20 +0200 Subject: [PATCH] Warn if trying to overwrite read-only config file. --- include/fsdb.h | 1 + od-win32/fsdb_mywin32.cpp | 16 ++++++++++++++++ od-win32/resources/resource.h | 1 + od-win32/resources/winuae.rc | 2 ++ od-win32/win32gui.cpp | 33 ++++++++++++++++++++++++++++++--- 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/include/fsdb.h b/include/fsdb.h index 8308aa20..16b7435e 100644 --- a/include/fsdb.h +++ b/include/fsdb.h @@ -158,6 +158,7 @@ extern int my_rename (const TCHAR*, const TCHAR*); extern int my_setcurrentdir (const TCHAR *curdir, TCHAR *oldcur); bool my_isfilehidden (const TCHAR *path); void my_setfilehidden (const TCHAR *path, bool hidden); +int my_readonlyfile(const TCHAR *path); extern struct my_openfile_s *my_open (const TCHAR*, int); extern void my_close (struct my_openfile_s*); diff --git a/od-win32/fsdb_mywin32.cpp b/od-win32/fsdb_mywin32.cpp index 22ef1384..0fab6fca 100644 --- a/od-win32/fsdb_mywin32.cpp +++ b/od-win32/fsdb_mywin32.cpp @@ -356,6 +356,22 @@ int my_existsdir (const TCHAR *name) return 0; } +int my_readonlyfile(const TCHAR *name) +{ + HANDLE h; + + if (!my_existsfile(name)) + return -1; + h = CreateFile(name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) { + DWORD err = GetLastError(); + if (err == ERROR_ACCESS_DENIED || err == ERROR_WRITE_PROTECT) + return true; + } + CloseHandle(h); + return false; +} + struct my_openfile_s *my_open (const TCHAR *name, int flags) { struct my_openfile_s *mos; diff --git a/od-win32/resources/resource.h b/od-win32/resources/resource.h index e728965a..28df0c62 100644 --- a/od-win32/resources/resource.h +++ b/od-win32/resources/resource.h @@ -415,6 +415,7 @@ #define IDS_QUIT_WARNING 418 #define IDS_UNMAPPED_ADDRESS 419 #define IDS_GENLOCK_OPTIONS 420 +#define IDS_READONLYCONFIRMATION 421 #define IDS_QS_MODELS 1000 #define IDS_QS_MODEL_A500 1001 #define IDS_QS_MODEL_A500P 1002 diff --git a/od-win32/resources/winuae.rc b/od-win32/resources/winuae.rc index 9a3fea1f..c210d46b 100644 --- a/od-win32/resources/winuae.rc +++ b/od-win32/resources/winuae.rc @@ -2164,6 +2164,8 @@ BEGIN IDS_QUIT_WARNING "Are you sure you want to quit WinUAE?" IDS_UNMAPPED_ADDRESS "Floating\nAll zeros\nAll ones\n" IDS_GENLOCK_OPTIONS "Noise (built-in)\nTest card (built-in)\nImage file (png)\nVideo file\nCapture device\nAmerican Laser Games LaserDisc Player\nSony LaserDisc Player\nPioneer LaserDisc Player\n" + IDS_READONLYCONFIRMATION + "Selected configuration file is write protected.\nDo you want to overwrite it?\n%s\n" END STRINGTABLE diff --git a/od-win32/win32gui.cpp b/od-win32/win32gui.cpp index 504bbb87..6d79184f 100644 --- a/od-win32/win32gui.cpp +++ b/od-win32/win32gui.cpp @@ -2176,6 +2176,31 @@ void target_multipath_modified(struct uae_prefs *p) memcpy(&currprefs.path_rom, &p->path_rom, sizeof(struct multipath)); } +static bool cfgfile_can_write(HWND hDlg, const TCHAR *path) +{ + for (;;) { + int v = my_readonlyfile(path); + if (v <= 0) + return true; + TCHAR szMessage[MAX_DPATH], msg[MAX_DPATH], szTitle[MAX_DPATH]; + WIN32GUI_LoadUIString(IDS_READONLYCONFIRMATION, szMessage, MAX_DPATH); + _stprintf(msg, szMessage, path); + WIN32GUI_LoadUIString(IDS_ERRORTITLE, szTitle, MAX_DPATH); + if (MessageBox(hDlg, msg, szTitle, MB_YESNO | MB_ICONWARNING | MB_APPLMODAL | MB_SETFOREGROUND) == IDYES) { + DWORD flags = GetFileAttributesSafe(path); + if (!(flags & FILE_ATTRIBUTE_READONLY)) { + return true; + } + flags &= ~FILE_ATTRIBUTE_READONLY; + SetFileAttributesSafe(path, flags); + continue; + } + break; + + } + return false; +} + int target_cfgfile_load (struct uae_prefs *p, const TCHAR *filename, int type, int isdefault) { int v, i, type2; @@ -3124,8 +3149,10 @@ int DiskSelection_2 (HWND hDlg, WPARAM wParam, int flag, struct uae_prefs *prefs } break; case IDC_SAVE: - SetDlgItemText (hDlg, IDC_EDITNAME, full_path); - cfgfile_save (&workprefs, full_path, 0); + if (cfgfile_can_write(hDlg, full_path)) { + SetDlgItemText(hDlg, IDC_EDITNAME, full_path); + cfgfile_save(&workprefs, full_path, 0); + } break; case IDC_ROMFILE: _tcscpy (workprefs.romfile, full_path); @@ -4086,7 +4113,7 @@ static TCHAR *HandleConfiguration (HWND hDlg, int flag, struct ConfigStruct *con TCHAR szMessage[MAX_DPATH]; WIN32GUI_LoadUIString(IDS_MUSTENTERNAME, szMessage, MAX_DPATH); pre_gui_message (szMessage); - } else { + } else if (cfgfile_can_write(hDlg, path)) { _tcscpy (workprefs.description, desc); cfgfile_save (&workprefs, path, configtypepanel); } -- 2.47.3