diff --git a/abstractattachment.cpp b/abstractattachment.cpp index c366b17..ea7d661 100644 --- a/abstractattachment.cpp +++ b/abstractattachment.cpp @@ -4,6 +4,7 @@ #include "./exceptions.h" #include +#include #include @@ -48,7 +49,7 @@ StreamDataBlock::StreamDataBlock(const std::function &stream, m_endOffset = s.tellg(); s.seekg(currentPos); if(m_endOffset < m_startOffset) { - throw ios_base::failure("End offset is less then start offset."); + IoUtilities::throwIoFailure("End offset is less then start offset."); } } diff --git a/avc/avcinfo.cpp b/avc/avcinfo.cpp index a773b3a..8be63ba 100644 --- a/avc/avcinfo.cpp +++ b/avc/avcinfo.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -200,7 +201,8 @@ void SpsInfo::parse(BinaryReader &reader, uint32 maxSize) pictureSize.setWidth(mbSize.width() * 16); pictureSize.setHeight((2 - frameMbsOnly) * mbSize.height() * 16); } - } catch(const ios_base::failure &) { + } catch(...) { + catchIoFailure(); throw TruncatedDataException(); } } @@ -239,7 +241,8 @@ void PpsInfo::parse(BinaryReader &reader, uint32 maxSize) spsId = bitReader.readUnsignedExpGolombCodedBits(); bitReader.skipBits(1); // entropy coding mode flag picOrderPresent = bitReader.readBit(); - } catch(const ios_base::failure &) { + } catch(...) { + catchIoFailure(); throw TruncatedDataException(); } } diff --git a/backuphelper.cpp b/backuphelper.cpp index 747004f..ad5c9b1 100644 --- a/backuphelper.cpp +++ b/backuphelper.cpp @@ -2,6 +2,7 @@ #include "./mediafileinfo.h" #include +#include #ifdef PLATFORM_WINDOWS # include @@ -16,6 +17,7 @@ using namespace std; using namespace ConversionUtilities; +using namespace IoUtilities; namespace Media { @@ -72,7 +74,7 @@ void restoreOriginalFileFromBackupFile(const std::string &originalPath, const st if(backupStream.is_open()) { backupStream.close(); } else { - throw ios_base::failure("Backup/temporary file has not been created."); + throwIoFailure("Backup/temporary file has not been created."); } // remove original file and restore backup std::remove(originalPath.c_str()); @@ -86,8 +88,9 @@ void restoreOriginalFileFromBackupFile(const std::string &originalPath, const st originalStream.open(originalPath, ios_base::out | ios_base::binary); originalStream << backupStream.rdbuf(); // TODO: callback for progress updates - } catch(const ios_base::failure &) { - throw ios_base::failure("Unable to restore original file from backup file \"" + backupPath + "\" after failure."); + } catch(...) { + catchIoFailure(); + throwIoFailure(("Unable to restore original file from backup file \"" + backupPath + "\" after failure.").data()); } } } @@ -201,8 +204,9 @@ void createBackupFile(const std::string &originalPath, std::string &backupPath, backupStream << originalStream.rdbuf(); // streams are closed in the next try-block // TODO: callback for progress updates - } catch(const ios_base::failure &) { - throw ios_base::failure("Unable to rename original file before rewriting it."); + } catch(...) { + catchIoFailure(); + throwIoFailure("Unable to rename original file before rewriting it."); } } try { @@ -217,13 +221,14 @@ void createBackupFile(const std::string &originalPath, std::string &backupPath, // open backup stream backupStream.exceptions(ios_base::failbit | ios_base::badbit); backupStream.open(backupPath, ios_base::in | ios_base::binary); - } catch(const ios_base::failure &) { + } catch(...) { + catchIoFailure(); // can't open the new file // -> try to re-rename backup file in the error case to restore previous state if(std::rename(backupPath.c_str(), originalPath.c_str()) != 0) { - throw ios_base::failure("Unable to restore original file from backup file \"" + backupPath + "\" after failure."); + throwIoFailure(("Unable to restore original file from backup file \"" + backupPath + "\" after failure.").data()); } else { - throw ios_base::failure("Unable to open backup file."); + throwIoFailure("Unable to open backup file."); } } } @@ -263,8 +268,8 @@ void handleFailureAfterFileModified(MediaFileInfo &fileInfo, const std::string & try { restoreOriginalFileFromBackupFile(fileInfo.path(), backupPath, outputStream, backupStream); fileInfo.addNotification(NotificationType::Information, "The original file has been restored.", context); - } catch(const ios_base::failure &ex) { - fileInfo.addNotification(NotificationType::Critical, ex.what(), context); + } catch(...) { + fileInfo.addNotification(NotificationType::Critical, catchIoFailure(), context); } } else { fileInfo.addNotification(NotificationType::Information, "Applying new tag information has been aborted.", context); @@ -278,29 +283,29 @@ void handleFailureAfterFileModified(MediaFileInfo &fileInfo, const std::string & try { restoreOriginalFileFromBackupFile(fileInfo.path(), backupPath, outputStream, backupStream); fileInfo.addNotification(NotificationType::Information, "The original file has been restored.", context); - } catch(const ios_base::failure &ex) { - fileInfo.addNotification(NotificationType::Critical, ex.what(), context); + } catch(...) { + fileInfo.addNotification(NotificationType::Critical, catchIoFailure(), context); } } else { fileInfo.addNotification(NotificationType::Critical, "Applying new tag information failed.", context); } throw; - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); if(!backupPath.empty()) { // a temp/backup file has been created -> restore original file fileInfo.addNotification(NotificationType::Critical, "An IO error occured when rewriting the file to apply changed tag information.", context); try { restoreOriginalFileFromBackupFile(fileInfo.path(), backupPath, outputStream, backupStream); fileInfo.addNotification(NotificationType::Information, "The original file has been restored.", context); - } catch(const ios_base::failure &ex) { - fileInfo.addNotification(NotificationType::Critical, ex.what(), context); + } catch(...) { + fileInfo.addNotification(NotificationType::Critical, catchIoFailure(), context); } } else { fileInfo.addNotification(NotificationType::Critical, "An IO error occured when applying tag information.", context); } - throw; - + throwIoFailure(what); } } diff --git a/matroska/matroskacontainer.cpp b/matroska/matroskacontainer.cpp index 709044d..f9e6d7e 100644 --- a/matroska/matroskacontainer.cpp +++ b/matroska/matroskacontainer.cpp @@ -12,6 +12,7 @@ #include "resources/config.h" #include +#include #include #include @@ -1319,9 +1320,10 @@ nonRewriteCalculations: } catch(const Failure &) { addNotification(NotificationType::Critical, "Parsing the original file failed.", context); throw; - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "An IO error occured when parsing the original file.", context); - throw; + throwIoFailure(what); } if(isAborted()) { @@ -1346,9 +1348,10 @@ nonRewriteCalculations: BackupHelper::createBackupFile(fileInfo().path(), backupPath, outputStream, backupStream); // recreate original file, define buffer variables outputStream.open(fileInfo().path(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Creation of temporary file (to rewrite the original file) failed.", context); - throw; + throwIoFailure(what); } } else { // open the current file as backupStream and create a new outputStream at the specified "save file path" @@ -1357,9 +1360,10 @@ nonRewriteCalculations: backupStream.open(fileInfo().path(), ios_base::in | ios_base::binary); fileInfo().close(); outputStream.open(fileInfo().saveFilePath(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening streams to write output file failed.", context); - throw; + throwIoFailure(what); } } @@ -1378,9 +1382,10 @@ nonRewriteCalculations: try { fileInfo().close(); outputStream.open(fileInfo().path(), ios_base::in | ios_base::out | ios_base::binary); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening the file with write permissions failed.", context); - throw; + throwIoFailure(what); } } diff --git a/mediafileinfo.cpp b/mediafileinfo.cpp index 923bb6c..f2105b5 100644 --- a/mediafileinfo.cpp +++ b/mediafileinfo.cpp @@ -31,6 +31,7 @@ #include "./flac/flacmetadata.h" #include +#include #include #include @@ -1507,7 +1508,7 @@ void MediaFileInfo::makeMp3File() reportSizeChanged(size() - 128); } else { addNotification(NotificationType::Critical, "Unable to truncate file to remove ID3v1 tag.", context); - throw ios_base::failure("Unable to truncate file to remove ID3v1 tag."); + throwIoFailure("Unable to truncate file to remove ID3v1 tag."); } } @@ -1613,9 +1614,10 @@ void MediaFileInfo::makeMp3File() BackupHelper::createBackupFile(path(), backupPath, outputStream, backupStream); // recreate original file, define buffer variables outputStream.open(path(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Creation of temporary file (to rewrite the original file) failed.", context); - throw; + throwIoFailure(what); } } else { // open the current file as backupStream and create a new outputStream at the specified "save file path" @@ -1623,9 +1625,10 @@ void MediaFileInfo::makeMp3File() backupStream.exceptions(ios_base::badbit | ios_base::failbit); backupStream.open(path(), ios_base::in | ios_base::binary); outputStream.open(m_saveFilePath, ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening streams to write output file failed.", context); - throw; + throwIoFailure(what); } } @@ -1634,9 +1637,10 @@ void MediaFileInfo::makeMp3File() try { close(); outputStream.open(path(), ios_base::in | ios_base::out | ios_base::binary); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening the file with write permissions failed.", context); - throw; + throwIoFailure(what); } } diff --git a/mp4/mp4container.cpp b/mp4/mp4container.cpp index c91492d..ccdd4bc 100644 --- a/mp4/mp4container.cpp +++ b/mp4/mp4container.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -498,9 +499,10 @@ calculatePadding: BackupHelper::createBackupFile(fileInfo().path(), backupPath, outputStream, backupStream); // recreate original file, define buffer variables outputStream.open(fileInfo().path(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Creation of temporary file (to rewrite the original file) failed.", context); - throw; + throwIoFailure(what); } } else { // open the current file as backupStream and create a new outputStream at the specified "save file path" @@ -509,9 +511,10 @@ calculatePadding: backupStream.open(fileInfo().path(), ios_base::in | ios_base::binary); fileInfo().close(); outputStream.open(fileInfo().saveFilePath(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening streams to write output file failed.", context); - throw; + throwIoFailure(what); } } @@ -525,9 +528,10 @@ calculatePadding: try { fileInfo().close(); outputStream.open(fileInfo().path(), ios_base::in | ios_base::out | ios_base::binary); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening the file with write permissions failed.", context); - throw; + throwIoFailure(what); } } diff --git a/mp4/mp4track.cpp b/mp4/mp4track.cpp index 967ac20..50dbc97 100644 --- a/mp4/mp4track.cpp +++ b/mp4/mp4track.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -645,16 +646,17 @@ unique_ptr Mp4Track::parseAudioSpecificConfig(StatusPr audioCfg->psPresent = bitReader.readBit(); } } - } catch(ios_base::failure &) { + } catch(const NotImplementedException &) { + statusProvider.addNotification(NotificationType::Information, "Not implemented for the format of audio track.", context); + } catch(...) { + const char *what = catchIoFailure(); if(stream.fail()) { // IO error caused by input stream - throw; + throwIoFailure(what); } else { // IO error caused by bitReader statusProvider.addNotification(NotificationType::Critical, "Audio specific configuration is truncated.", context); } - } catch(NotImplementedException &) { - statusProvider.addNotification(NotificationType::Information, "Not implemented for the format of audio track.", context); } return audioCfg; } diff --git a/ogg/oggcontainer.cpp b/ogg/oggcontainer.cpp index e17b9d2..ef0801c 100644 --- a/ogg/oggcontainer.cpp +++ b/ogg/oggcontainer.cpp @@ -6,6 +6,7 @@ #include "../backuphelper.h" #include +#include #include using namespace std; @@ -338,9 +339,10 @@ void OggContainer::internalMakeFile() BackupHelper::createBackupFile(fileInfo().path(), backupPath, fileInfo().stream(), backupStream); // recreate original file, define buffer variables fileInfo().stream().open(fileInfo().path(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Creation of temporary file (to rewrite the original file) failed.", context); - throw; + throwIoFailure(what); } } else { // open the current file as backupStream and create a new outputStream at the specified "save file path" @@ -349,9 +351,10 @@ void OggContainer::internalMakeFile() backupStream.open(fileInfo().path(), ios_base::in | ios_base::binary); fileInfo().close(); fileInfo().stream().open(fileInfo().saveFilePath(), ios_base::out | ios_base::binary | ios_base::trunc); - } catch(const ios_base::failure &) { + } catch(...) { + const char *what = catchIoFailure(); addNotification(NotificationType::Critical, "Opening streams to write output file failed.", context); - throw; + throwIoFailure(what); } } diff --git a/vorbis/vorbiscommentfield.cpp b/vorbis/vorbiscommentfield.cpp index 806f511..31842b0 100644 --- a/vorbis/vorbiscommentfield.cpp +++ b/vorbis/vorbiscommentfield.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -80,15 +81,16 @@ void VorbisCommentField::internalParse(StreamType &stream, uint64 &maxSize) FlacMetaDataBlockPicture pictureBlock(value()); pictureBlock.parse(bufferStream, decoded.second); setTypeInfo(pictureBlock.pictureType()); - } catch(const ios_base::failure &) { - addNotification(NotificationType::Critical, "An IO error occured when reading the METADATA_BLOCK_PICTURE struct.", context); - throw Failure(); } catch(const TruncatedDataException &) { addNotification(NotificationType::Critical, "METADATA_BLOCK_PICTURE is truncated.", context); throw; } catch(const ConversionException &) { addNotification(NotificationType::Critical, "Base64 coding of METADATA_BLOCK_PICTURE is invalid.", context); throw InvalidDataException(); + } catch(...) { + catchIoFailure(); + addNotification(NotificationType::Critical, "An IO error occured when reading the METADATA_BLOCK_PICTURE struct.", context); + throw Failure(); } } else if(id().size() + 1 < size) { // extract other values (as string) @@ -186,7 +188,8 @@ bool VorbisCommentField::make(BinaryWriter &writer, VorbisCommentFlags flags) pictureBlock.make(bufferStream); valueString = encodeBase64(reinterpret_cast(buffer.get()), requiredSize); - } catch (const ios_base::failure &) { + } catch(...) { + catchIoFailure(); addNotification(NotificationType::Critical, "An IO error occured when writing the METADATA_BLOCK_PICTURE struct.", context); throw Failure(); }