Prevent un-prompted overwrite of files when exporting things from config (#1383).
authorCarl Hetherington <cth@carlh.net>
Wed, 17 Oct 2018 18:23:18 +0000 (19:23 +0100)
committerCarl Hetherington <cth@carlh.net>
Wed, 17 Oct 2018 18:23:18 +0000 (19:23 +0100)
src/wx/config_dialog.cc
src/wx/full_config_dialog.cc
src/wx/wx_util.cc
src/wx/wx_util.h

index 4195aa128880de01777cb28301ec612256b6d6d3..e55de008fb91b86ad2cf4742590a3a63820787ff 100644 (file)
@@ -526,14 +526,17 @@ CertificateChainEditor::export_certificate ()
        }
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
-               if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
-               }
+               optional<boost::filesystem::path> path = path_from_file_dialog (d, "pem");
+               if (path) {
+                       FILE* f = fopen_boost (*path, "w");
+                       if (!f) {
+                               throw OpenFileError (*path, errno, false);
+                       }
 
-               string const s = j->certificate (true);
-               fwrite (s.c_str(), 1, s.length(), f);
-               fclose (f);
+                       string const s = j->certificate (true);
+                       fwrite (s.c_str(), 1, s.length(), f);
+                       fclose (f);
+               }
        }
        d->Destroy ();
 }
@@ -698,14 +701,17 @@ CertificateChainEditor::export_private_key ()
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
-               if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
-               }
+               optional<boost::filesystem::path> path = path_from_file_dialog (d, "pem");
+               if (path) {
+                       FILE* f = fopen_boost (*path, "w");
+                       if (!f) {
+                               throw OpenFileError (*path, errno, false);
+                       }
 
-               string const s = _get()->key().get ();
-               fwrite (s.c_str(), 1, s.length(), f);
-               fclose (f);
+                       string const s = _get()->key().get ();
+                       fwrite (s.c_str(), 1, s.length(), f);
+                       fclose (f);
+               }
        }
        d->Destroy ();
 }
@@ -793,17 +799,20 @@ KeysPage::export_decryption_chain_and_key ()
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "dom"), "w");
-               if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
-               }
+               optional<boost::filesystem::path> path = path_from_file_dialog (d, "dom");
+               if (path) {
+                       FILE* f = fopen_boost (*path, "w");
+                       if (!f) {
+                               throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
+                       }
 
-               string const chain = Config::instance()->decryption_chain()->chain();
-               fwrite (chain.c_str(), 1, chain.length(), f);
-               optional<string> const key = Config::instance()->decryption_chain()->key();
-               DCPOMATIC_ASSERT (key);
-               fwrite (key->c_str(), 1, key->length(), f);
-               fclose (f);
+                       string const chain = Config::instance()->decryption_chain()->chain();
+                       fwrite (chain.c_str(), 1, chain.length(), f);
+                       optional<string> const key = Config::instance()->decryption_chain()->key();
+                       DCPOMATIC_ASSERT (key);
+                       fwrite (key->c_str(), 1, key->length(), f);
+                       fclose (f);
+               }
        }
        d->Destroy ();
 
@@ -870,14 +879,17 @@ KeysPage::export_decryption_chain ()
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
-               if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
-               }
+               optional<boost::filesystem::path> path = path_from_file_dialog (d, "pem");
+               if (path) {
+                       FILE* f = fopen_boost (*path, "w");
+                       if (!f) {
+                               throw OpenFileError (*path, errno, false);
+                       }
 
-               string const s = Config::instance()->decryption_chain()->chain();
-               fwrite (s.c_str(), 1, s.length(), f);
-               fclose (f);
+                       string const s = Config::instance()->decryption_chain()->chain();
+                       fwrite (s.c_str(), 1, s.length(), f);
+                       fclose (f);
+               }
        }
        d->Destroy ();
 }
@@ -891,14 +903,18 @@ KeysPage::export_decryption_certificate ()
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
-               if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
-               }
+               optional<boost::filesystem::path> path = path_from_file_dialog (d, "pem");
+               if (path) {
+                       FILE* f = fopen_boost (*path, "w");
+                       if (!f) {
+                               throw OpenFileError (*path, errno, false);
+                       }
 
-               string const s = Config::instance()->decryption_chain()->leaf().certificate (true);
-               fwrite (s.c_str(), 1, s.length(), f);
-               fclose (f);
+                       string const s = Config::instance()->decryption_chain()->leaf().certificate (true);
+                       fwrite (s.c_str(), 1, s.length(), f);
+                       fclose (f);
+               }
        }
+
        d->Destroy ();
 }
index bb4f43bea667c12b7a42fad3b3d55fe66d81b7c7..974635a8b407f6f6cd0ee2096740a55a0b420e8d 100644 (file)
@@ -196,7 +196,10 @@ private:
                 );
 
                if (d->ShowModal () == wxID_OK) {
-                       boost::filesystem::copy_file (Config::instance()->cinemas_file(), path_from_file_dialog (d, "xml"));
+                       optional<boost::filesystem::path> path = path_from_file_dialog (d, "xml");
+                       if (path) {
+                               boost::filesystem::copy_file (Config::instance()->cinemas_file(), *path);
+                       }
                }
                d->Destroy ();
        }
index c9185ec973df9565413acc25414b3951bfd01016..db63400315e899cb1488705dffa3dda15adad12d 100644 (file)
@@ -463,10 +463,15 @@ maybe_show_splash ()
        return splash;
 }
 
-boost::filesystem::path
+optional<boost::filesystem::path>
 path_from_file_dialog (wxFileDialog* dialog, string extension)
 {
-       return boost::filesystem::path(wx_to_std(dialog->GetPath())).replace_extension(extension);
+       boost::filesystem::path p(wx_to_std(dialog->GetPath()));
+       p.replace_extension(extension);
+       if (boost::filesystem::is_regular_file(p) && !confirm_dialog(dialog, wxString::Format(_("A file named %s already exists.  Do you want to replace it?"), std_to_wx(p.filename().string())))) {
+               return optional<boost::filesystem::path>();
+       }
+       return p;
 }
 
 double
index 1acca044a6fd815445de113955f4c88e373db4fb..ee06c380778c46896a3dd584b01a15893ef22bad 100644 (file)
@@ -86,7 +86,7 @@ extern std::string string_client_data (wxClientData* o);
 extern wxString time_to_timecode (DCPTime t, double fps);
 extern void setup_audio_channels_choice (wxChoice* choice, int minimum);
 extern wxSplashScreen* maybe_show_splash ();
-extern boost::filesystem::path path_from_file_dialog (wxFileDialog* dialog, std::string extension);
+extern boost::optional<boost::filesystem::path> path_from_file_dialog (wxFileDialog* dialog, std::string extension);
 extern double calculate_mark_interval (double start);
 
 extern void checked_set (FilePickerCtrl* widget, boost::filesystem::path value);