]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
Warn if trying to overwrite read-only config file.
authorToni Wilen <twilen@winuae.net>
Sat, 6 Mar 2021 15:02:20 +0000 (17:02 +0200)
committerToni Wilen <twilen@winuae.net>
Sat, 6 Mar 2021 15:02:20 +0000 (17:02 +0200)
include/fsdb.h
od-win32/fsdb_mywin32.cpp
od-win32/resources/resource.h
od-win32/resources/winuae.rc
od-win32/win32gui.cpp

index 8308aa20824cbd8e0500e699475af758b18ce390..16b7435e82c2fc77adae937cf42f4c6c1c4e2796 100644 (file)
@@ -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*);
index 22ef1384bf024766199c8cc60c6c79853088234f..0fab6fca89efb98560b6dc3481b6219709bebc79 100644 (file)
@@ -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;
index e728965aa0f5f042d8a21cfc3415ac3e9e12a9a4..28df0c62b2503a72f7ab11ff9e6dc6324c317f2f 100644 (file)
 #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
index 9a3fea1f36a961353124ac14d8dc60efa9d6e2f4..c210d46b543decda0a71fd1f90c062f48255c989 100644 (file)
@@ -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
index 504bbb871d22c3db0cbc5020d00570181b44f40b..6d79184f54e295293958cafd7b35bfdec7748a0f 100644 (file)
@@ -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);
                }