Update of /cvsroot/audacity/audacity-src/src
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv14899

Modified Files:
        Legacy.cpp Project.cpp Tags.cpp 
Log Message:
* XMLFileWriter now throws an exception if writing the .xml file fails
* Add error checking (exception handling) to most places where XML files are 
written
* If the new .aup file was successfully saved, delete the old .aup.bak file in 
order not to confuse users
* Never open .aup.bak files, show an informational message instead

Index: Project.cpp
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/Project.cpp,v
retrieving revision 1.419
retrieving revision 1.420
diff -u -d -r1.419 -r1.420
--- Project.cpp 20 Apr 2009 17:27:22 -0000      1.419
+++ Project.cpp 1 May 2009 18:26:33 -0000       1.420
@@ -2085,7 +2085,20 @@
    // On Win32, we may be given a short (DOS-compatible) file name on rare
    // occassions (e.g. stuff like "C:\PROGRA~1\AUDACI~1\PROJEC~1.AUP"). We
    // convert these to long file name first.
-   fileName = 
PlatformCompatibility::ConvertSlashInFileName(PlatformCompatibility::GetLongFileName(fileName));
+   fileName = PlatformCompatibility::ConvertSlashInFileName(
+      PlatformCompatibility::GetLongFileName(fileName));
+
+   // Data loss may occur if users mistakenly try to open ".aup.bak" files
+   // left over from an unsuccessful save or by previous versions of Audacity.
+   // So we always refuse to open such files.
+   if (fileName.Lower().EndsWith(wxT(".aup.bak")))
+   {
+      wxMessageBox(
+         _("You are trying to open an automatically created backup file. Doing 
this may result in severe dataloss. Please open the actual Audacity project 
file instead."),
+         _("Backup file detected"),
+         wxOK | wxCENTRE, this);
+      return;
+   }
 
    // We want to open projects using wxTextFile, but if it's NOT a project
    // file (but actually a WAV file, for example), then wxTextFile will spin
@@ -2803,8 +2816,8 @@
          wxMessageBox(wxString::Format(_("Could not save project. Perhaps %s 
\nis not writable or the disk is full."),
                                        project.c_str()),
                       _("Error saving project"),
-                      wxOK | wxCENTRE, this);
-         if (safetyFileName)
+                      wxICON_ERROR, this);
+         if (safetyFileName != wxT(""))
             wxRename(safetyFileName, mFileName);
          
          return false;
@@ -2814,22 +2827,35 @@
    // Write the AUP file. 
    XMLFileWriter saveFile;
 
-   saveFile.Open(mFileName, wxT("wb"));
-   if (!saveFile.IsOpened()) {
-      wxMessageBox(_("Couldn't write to file: ") + mFileName,
-                   _("Error saving project"),
-                   wxOK | wxCENTRE, this);
+   try
+   {
+      saveFile.Open(mFileName, wxT("wb"));
 
-      if (safetyFileName)
+      WriteXMLHeader(saveFile);
+      WriteXML(saveFile);
+
+      saveFile.Close();
+   }
+   catch (XMLFileWriterException* pException)
+   {
+      wxMessageBox(wxString::Format(
+         _("Couldn't write to file \"%s\": %s"),
+         mFileName.c_str(), pException->GetMessage().c_str()),
+         _("Error saving project"), wxICON_ERROR);
+
+      delete pException;
+
+      // When XMLWriter throws an exception, it tries to close it before,
+      // so we can at least try to delete the incomplete file and move the
+      // backup file over.
+      if (safetyFileName != wxT(""))
+      {
+         wxRemove(mFileName);
          wxRename(safetyFileName, mFileName);
+      }
       
       return false;
    }
-
-   WriteXMLHeader(saveFile);
-   WriteXML(saveFile);
-
-   saveFile.Close();
    
 #ifdef __WXMAC__
    wxFileName fn(mFileName);
@@ -2887,6 +2913,12 @@
       mUndoManager.StateSaved();
    }
 
+   // If we get here, saving the project was successful, so we can delete
+   // the .bak file (because it now does not fit our block files anymore
+   // anyway).
+   if (safetyFileName != wxT(""))
+      wxRemoveFile(safetyFileName);
+
    mStatusBar->SetStatusText(wxString::Format(_("Saved %s"),
                                               mFileName.c_str()));
    
@@ -3889,27 +3921,34 @@
    
    XMLFileWriter saveFile;
 
-   saveFile.Open(fn + wxT(".tmp"), wxT("wb"));
-   if (!saveFile.IsOpened())
+   try
    {
-      wxMessageBox(_("Couldn't write to file: ") + fn + wxT(".tmp"),
-                   _("Error writing autosave file"),
-                   wxICON_STOP, this);
-      return;
-   }
+      saveFile.Open(fn + wxT(".tmp"), wxT("wb"));
+
+      {
+         VarSetter<bool> setter(&mAutoSaving, true, false);
+         WriteXMLHeader(saveFile);
+         WriteXML(saveFile);
+      }
 
+      // JKC Calling XMLFileWriter::Close will close the <project> scope.
+      // We certainly don't want to do that, if we're doing recordingrecovery,
+      // because the recordingrecovery tags need to be inside 
<project></project>.
+      // So instead we do not call Close() but CloseWithoutEndingTags().
+      saveFile.CloseWithoutEndingTags();
+   }
+   catch (XMLFileWriterException* pException)
    {
-      VarSetter<bool> setter(&mAutoSaving, true, false);
-      WriteXMLHeader(saveFile);
-      WriteXML(saveFile);
+      wxMessageBox(wxString::Format(
+         _("Couldn't write to file \"%s\": %s"),
+         (fn + wxT(".tmp")).c_str(), pException->GetMessage().c_str()),
+         _("Error writing autosave file"), wxICON_ERROR, this);
+
+      delete pException;
+
+      return;
    }
 
-   // JKC Calling XMLFileWriter::Close will close the <project> scope.
-   // We certainly don't want to do that, if we're doing recordingrecovery,
-   // because the recordingrecovery tags need to be inside <project></project>
-   // So instead we close the file directly.
-   saveFile.wxFFile::Close();
-   
    // Now that we have a new auto-save file, delete the old one
    DeleteCurrentAutoSaveFile();
    

Index: Legacy.cpp
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/Legacy.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- Legacy.cpp  2 Sep 2008 15:50:34 -0000       1.17
+++ Legacy.cpp  1 May 2009 18:26:33 -0000       1.18
@@ -310,46 +310,62 @@
 
    wxString name = filename.GetFullPath();
 
-   xmlFile.Open(name, wxT("wb"));
-   if (!xmlFile.IsOpened())
+   try
+   {
+      xmlFile.Open(name, wxT("wb"));
+   }
+   catch (XMLFileWriterException* pException)
+   {
+      delete pException;
       return false;
+   }
 
    renamer.SetNewFile(xmlFile.fp());
 
-   xmlFile.Write(wxT("<?xml version=\"1.0\"?>\n"));
+   try
+   {
+      xmlFile.Write(wxT("<?xml version=\"1.0\"?>\n"));
 
-   wxString label;
-   wxString value;
+      wxString label;
+      wxString value;
 
-   if (f.GetFirstLine() != wxT("AudacityProject"))
-      return false;
-   if (f.GetNextLine() != wxT("Version"))
-      return false;
-   if (f.GetNextLine() != wxT("0.95"))
-      return false;
-   if (f.GetNextLine() != wxT("projName"))
-      return false;
+      if (f.GetFirstLine() != wxT("AudacityProject"))
+         return false;
+      if (f.GetNextLine() != wxT("Version"))
+         return false;
+      if (f.GetNextLine() != wxT("0.95"))
+         return false;
+      if (f.GetNextLine() != wxT("projName"))
+         return false;
 
-   xmlFile.StartTag(wxT("audacityproject"));
-   xmlFile.WriteAttr(wxT("projname"), f.GetNextLine());
-   xmlFile.WriteAttr(wxT("version"), wxT("1.1.0"));
-   xmlFile.WriteAttr(wxT("audacityversion"),AUDACITY_VERSION_STRING);
+      xmlFile.StartTag(wxT("audacityproject"));
+      xmlFile.WriteAttr(wxT("projname"), f.GetNextLine());
+      xmlFile.WriteAttr(wxT("version"), wxT("1.1.0"));
+      xmlFile.WriteAttr(wxT("audacityversion"),AUDACITY_VERSION_STRING);
 
-   label = f.GetNextLine();
-   while (label != wxT("BeginTracks")) {
-      xmlFile.WriteAttr(label, f.GetNextLine());
       label = f.GetNextLine();
-   }
+      while (label != wxT("BeginTracks")) {
+         xmlFile.WriteAttr(label, f.GetNextLine());
+         label = f.GetNextLine();
+      }
 
-   label = f.GetNextLine();
-   while (label != wxT("EndTracks")) {
-      bool success = ConvertLegacyTrack(&f, xmlFile);
-      if (!success)
-         return false;
       label = f.GetNextLine();
-   }
+      while (label != wxT("EndTracks")) {
+         bool success = ConvertLegacyTrack(&f, xmlFile);
+         if (!success)
+            return false;
+         label = f.GetNextLine();
+      }
 
-   xmlFile.EndTag(wxT("audacityproject"));
+      xmlFile.EndTag(wxT("audacityproject"));
+      xmlFile.Close();
+   }
+   catch (XMLFileWriterException* pException)
+   {
+      // Error writing XML file (e.g. disk full)
+      delete pException;
+      return false;
+   }
 
    renamer.Finished();
 

Index: Tags.cpp
===================================================================
RCS file: /cvsroot/audacity/audacity-src/src/Tags.cpp,v
retrieving revision 1.68
retrieving revision 1.69
diff -u -d -r1.68 -r1.69
--- Tags.cpp    1 Apr 2009 04:01:16 -0000       1.68
+++ Tags.cpp    1 May 2009 18:26:33 -0000       1.69
@@ -1157,46 +1157,50 @@
 
    // Create/Open the file
    XMLFileWriter writer;
-   writer.Open(fn, wxT("wb"));
 
-   // Complain if open failed
-   if (!writer.IsOpened())
+   try
    {
-      // Constructor will emit message
-      return;
-   }
+      writer.Open(fn, wxT("wb"));
 
-   // Remember title and track in case they're read only
-   wxString title = mLocal.GetTag(TAG_TITLE);
-   wxString track = mLocal.GetTag(TAG_TRACK);
+      // Remember title and track in case they're read only
+      wxString title = mLocal.GetTag(TAG_TITLE);
+      wxString track = mLocal.GetTag(TAG_TRACK);
 
-   // Clear title
-   if (!mEditTitle) {
-      mLocal.SetTag(TAG_TITLE, wxEmptyString);
-   }
+      // Clear title
+      if (!mEditTitle) {
+         mLocal.SetTag(TAG_TITLE, wxEmptyString);
+      }
 
-   // Clear track
-   if (!mEditTrack) {
-      mLocal.SetTag(TAG_TRACK, wxEmptyString);
-   }
+      // Clear track
+      if (!mEditTrack) {
+         mLocal.SetTag(TAG_TRACK, wxEmptyString);
+      }
 
-   // Write the metadata
-   mLocal.WriteXML(writer);
+      // Write the metadata
+      mLocal.WriteXML(writer);
 
-   // Restore title
-   if (!mEditTitle) {
-      mLocal.SetTag(TAG_TITLE, title);
-   }
+      // Restore title
+      if (!mEditTitle) {
+         mLocal.SetTag(TAG_TITLE, title);
+      }
 
-   // Restore track
-   if (!mEditTrack) {
-      mLocal.SetTag(TAG_TRACK, track);
-   }
+      // Restore track
+      if (!mEditTrack) {
+         mLocal.SetTag(TAG_TRACK, track);
+      }
 
-   // Close the file
-   writer.Close();
+      // Close the file
+      writer.Close();
+   }
+   catch (XMLFileWriterException* pException)
+   {
+      wxMessageBox(wxString::Format(
+         _("Couldn't write to file \"%s\": %s"),
+         fn.c_str(), pException->GetMessage().c_str()),
+         _("Error saving tags file"), wxICON_ERROR, this);
 
-   return;
+      delete pException;
+   }
 }
 
 void TagsEditor::OnSaveDefaults(wxCommandEvent & event)


------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
Audacity-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/audacity-cvs

Reply via email to