#include <sstream>
#include <vector>
+#include <cerrno>
#include <boost/algorithm/string.hpp>
#include <openssl/x509.h>
#include <openssl/ssl.h>
{
FILE* f = fopen_boost (filename, "r");
if (!f) {
- throw FileError ("could not open file", filename);
+ throw FileError ("could not open file", filename, errno);
}
if (!PEM_read_X509 (f, &_certificate, 0, 0)) {
try {
cpl.reset (new parse::CPL (file));
} catch (FileError& e) {
- boost::throw_exception (FileError ("could not load CPL file", file));
+ boost::throw_exception (FileError ("could not load CPL file", file, e.number ()));
}
/* Now cherry-pick the required bits into our own data structure */
}
} catch (FileError& e) {
- boost::throw_exception (FileError ("could not load AssetMap file", _files.asset_map));
+ boost::throw_exception (FileError ("could not load AssetMap file", _files.asset_map, e.number ()));
}
for (list<shared_ptr<libdcp::parse::AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
}
if (_files.cpls.empty ()) {
- boost::throw_exception (FileError ("no CPL files found", ""));
+ boost::throw_exception (DCPReadError ("no CPL files found"));
}
if (_files.pkl.empty ()) {
- boost::throw_exception (FileError ("no PKL file found", ""));
+ boost::throw_exception (DCPReadError ("no PKL file found"));
}
shared_ptr<parse::PKL> pkl;
try {
pkl.reset (new parse::PKL (_files.pkl));
} catch (FileError& e) {
- boost::throw_exception (FileError ("could not load PKL file", _files.pkl));
+ boost::throw_exception (FileError ("could not load PKL file", _files.pkl, e.number ()));
}
_asset_maps.push_back (make_pair (boost::filesystem::absolute (_directory).string(), asset_map));
#define LIBDCP_EXCEPTIONS_H
#include <boost/filesystem.hpp>
+#include "compose.hpp"
/** @file src/exceptions.h
* @brief Exceptions thrown by libdcp.
class FileError : public std::exception
{
public:
- FileError (std::string const & message, boost::filesystem::path filename)
- : _message (message + " (" + filename.string() + ")")
+ FileError (std::string const & message, boost::filesystem::path filename, int number)
+ : _message (String::compose ("%1 (error %2) (%3)", message, filename.string(), number))
, _filename (filename)
+ , _number (number)
{}
~FileError () throw () {}
return _filename;
}
+ /** @return error number of the error */
+ int number () const {
+ return _number;
+ }
+
private:
/** message part */
std::string _message;
/** filename of file that was involved */
boost::filesystem::path _filename;
+ int _number;
};
/** @brief An exception related to an MXF file */
class MXFFileError : public FileError
{
public:
- MXFFileError (std::string const & message, boost::filesystem::path filename)
- : FileError (message, filename)
+ MXFFileError (std::string const & message, boost::filesystem::path filename, int number)
+ : FileError (message, filename, number)
{}
};
FILE* private_key_file = fopen_boost (private_key, "r");
if (!private_key_file) {
- throw FileError ("could not find RSA private key file", private_key);
+ throw FileError ("could not find RSA private key file", private_key, errno);
}
RSA* rsa = PEM_read_RSAPrivateKey (private_key_file, 0, 0, 0);
fclose (private_key_file);
if (!rsa) {
- throw FileError ("could not read RSA private key file", private_key);
+ throw FileError ("could not read RSA private key file", private_key, errno);
}
/* Use it to decrypt the keys */
{
ASDCP::JP2K::CodestreamParser j2k_parser;
ASDCP::JP2K::FrameBuffer frame_buffer (4 * Kumu::Megabyte);
- if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (get_path(0).string().c_str(), frame_buffer))) {
- boost::throw_exception (FileError ("could not open JPEG2000 file for reading", get_path (0)));
+ Kumu::Result_t r = j2k_parser.OpenReadFrame (get_path(0).string().c_str(), frame_buffer);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open JPEG2000 file for reading", get_path(0), r));
}
ASDCP::JP2K::PictureDescriptor picture_desc;
fill_writer_info (&writer_info, _uuid, _interop, _metadata);
ASDCP::JP2K::MXFWriter mxf_writer;
- if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for writing", path().string()));
+ r = mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc, 16384, false);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for writing", path().string(), r));
}
for (int i = 0; i < _intrinsic_duration; ++i) {
boost::filesystem::path const path = get_path (i);
- if (ASDCP_FAILURE (j2k_parser.OpenReadFrame (path.string().c_str(), frame_buffer))) {
- boost::throw_exception (FileError ("could not open JPEG2000 file for reading", path));
+ Kumu::Result_t r = j2k_parser.OpenReadFrame (path.string().c_str(), frame_buffer);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open JPEG2000 file for reading", path, r));
}
- if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0))) {
- boost::throw_exception (MXFFileError ("error in writing video MXF", this->path().string()));
+ r = mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in writing video MXF", this->path().string(), r));
}
if (_progress) {
}
}
- if (ASDCP_FAILURE (mxf_writer.Finalize())) {
- boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string()));
+ r = mxf_writer.Finalize();
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in finalising video MXF", path().string(), r));
}
}
MonoPictureAsset::read ()
{
ASDCP::JP2K::MXFReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ Kumu::Result_t r = reader.OpenRead (path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::JP2K::PictureDescriptor desc;
}
ASDCP::JP2K::MXFReader reader_A;
- if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ Kumu::Result_t r = reader_A.OpenRead (path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::JP2K::MXFReader reader_B;
- if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ r = reader_B.OpenRead (other->path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::JP2K::PictureDescriptor desc_A;
string hash;
ASDCP::Result_t const r = _state->mxf_writer.WriteFrame (_state->frame_buffer, _state->encryption_context, 0, &hash);
if (ASDCP_FAILURE (r)) {
- boost::throw_exception (MXFFileError ("error in writing video MXF (" + lexical_cast<string> (int(r)) + ")", _asset->path().string()));
+ boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string(), r));
}
++_frames_written;
assert (_started);
assert (!_finalized);
- if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
- boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
+ Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string(), r));
}
++_frames_written;
{
assert (!_finalized);
- if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
- boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
+ Kumu::Result_t r = _state->mxf_writer.Finalize();
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string(), r));
}
_finalized = true;
MonoPictureFrame::MonoPictureFrame (boost::filesystem::path mxf_path, int n, ASDCP::AESDecContext* c)
{
ASDCP::JP2K::MXFReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (mxf_path.string().c_str()))) {
- boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path));
+ Kumu::Result_t r = reader.OpenRead (mxf_path.string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
}
/* XXX: unfortunate guesswork on this buffer size */
asset->fill_writer_info (&state->writer_info, asset->uuid(), writer->_asset->interop(), writer->_asset->metadata());
- if (ASDCP_FAILURE (state->mxf_writer.OpenWrite (
- asset->path().string().c_str(),
- state->writer_info,
- state->picture_descriptor,
- 16384,
- writer->_overwrite)
- )) {
-
- boost::throw_exception (MXFFileError ("could not open MXF file for writing", asset->path().string()));
+ Kumu::Result_t r = state->mxf_writer.OpenWrite (
+ asset->path().string().c_str(),
+ state->writer_info,
+ state->picture_descriptor,
+ 16384,
+ writer->_overwrite
+ );
+
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for writing", asset->path().string(), r));
}
writer->_started = true;
signature_context->signKey = xmlSecCryptoAppKeyLoad (_key.string().c_str(), xmlSecKeyDataFormatPem, 0, 0, 0);
if (signature_context->signKey == 0) {
- throw FileError ("could not load private key file", _key);
+ throw FileError ("could not load private key file", _key, 0);
}
/* XXX: set key name to the file name: is this right? */
SoundAsset::read ()
{
ASDCP::PCM::MXFReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ Kumu::Result_t r = reader.OpenRead (path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::PCM::AudioDescriptor desc;
for (int i = 0; i < _channels; ++i) {
pcm_parser_channel[i] = new ASDCP::PCM::WAVParser ();
}
- if (pcm_parser_channel[0]->OpenRead (get_path(LEFT).string().c_str(), asdcp_edit_rate)) {
- boost::throw_exception (FileError ("could not open WAV file for reading", get_path(LEFT)));
+
+ Kumu::Result_t r = pcm_parser_channel[0]->OpenRead (get_path(LEFT).string().c_str(), asdcp_edit_rate);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open WAV file for reading", get_path(LEFT), r));
}
ASDCP::PCM::AudioDescriptor audio_desc;
boost::filesystem::path const path = get_path (channels[i]);
- if (ASDCP_FAILURE (pcm_parser_channel[i]->OpenRead (path.string().c_str(), asdcp_edit_rate))) {
- boost::throw_exception (FileError ("could not open WAV file for reading", path));
+ Kumu::Result_t r = pcm_parser_channel[i]->OpenRead (path.string().c_str(), asdcp_edit_rate);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open WAV file for reading", path, r));
}
pcm_parser_channel[i]->FillAudioDescriptor (*audio_desc_channel[i]);
MXFAsset::fill_writer_info (&writer_info, _uuid, _interop, _metadata);
ASDCP::PCM::MXFWriter mxf_writer;
- if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc))) {
- boost::throw_exception (FileError ("could not open audio MXF for writing", path().string()));
+ r = mxf_writer.OpenWrite (path().string().c_str(), writer_info, audio_desc);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open audio MXF for writing", path().string(), r));
}
for (int i = 0; i < _intrinsic_duration; ++i) {
}
ASDCP::PCM::MXFReader reader_A;
- if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ Kumu::Result_t r = reader_A.OpenRead (path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::PCM::MXFReader reader_B;
- if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ r = reader_B.OpenRead (other->path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::PCM::AudioDescriptor desc_A;
_asset->fill_writer_info (&_state->writer_info, _asset->uuid (), _asset->interop(), _asset->metadata());
- if (ASDCP_FAILURE (_state->mxf_writer.OpenWrite (_asset->path().string().c_str(), _state->writer_info, _state->audio_desc))) {
- boost::throw_exception (FileError ("could not open audio MXF for writing", _asset->path().string()));
+ Kumu::Result_t r = _state->mxf_writer.OpenWrite (_asset->path().string().c_str(), _state->writer_info, _state->audio_desc);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open audio MXF for writing", _asset->path().string(), r));
}
}
SoundFrame::SoundFrame (string mxf_path, int n, ASDCP::AESDecContext* c)
{
ASDCP::PCM::MXFReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (mxf_path.c_str()))) {
- boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path));
+ Kumu::Result_t r = reader.OpenRead (mxf_path.c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
}
/* XXX: unfortunate guesswork on this buffer size */
}
ASDCP::JP2K::MXFSReader reader_A;
- if (ASDCP_FAILURE (reader_A.OpenRead (path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ Kumu::Result_t r = reader_A.OpenRead (path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::JP2K::MXFSReader reader_B;
- if (ASDCP_FAILURE (reader_B.OpenRead (other->path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ r = reader_B.OpenRead (other->path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::JP2K::PictureDescriptor desc_A;
StereoPictureAsset::read ()
{
ASDCP::JP2K::MXFSReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) {
- boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string()));
+ Kumu::Result_t r = reader.OpenRead (path().string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("could not open MXF file for reading", path().string(), r));
}
ASDCP::JP2K::PictureDescriptor desc;
uint64_t const before_offset = _state->mxf_writer.Tell ();
string hash;
- if (ASDCP_FAILURE (
- _state->mxf_writer.WriteFrame (
- _state->frame_buffer,
- _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
- _state->encryption_context,
- 0,
- &hash)
- )) {
-
- boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
+ Kumu::Result_t r = _state->mxf_writer.WriteFrame (
+ _state->frame_buffer,
+ _next_eye == EYE_LEFT ? ASDCP::JP2K::SP_LEFT : ASDCP::JP2K::SP_RIGHT,
+ _state->encryption_context,
+ 0,
+ &hash
+ );
+
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string(), r));
}
_next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
assert (_started);
assert (!_finalized);
- if (ASDCP_FAILURE (_state->mxf_writer.FakeWriteFrame (size))) {
- boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string()));
+ Kumu::Result_t r = _state->mxf_writer.FakeWriteFrame (size);
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in writing video MXF", _asset->path().string(), r));
}
_next_eye = _next_eye == EYE_LEFT ? EYE_RIGHT : EYE_LEFT;
{
assert (!_finalized);
- if (ASDCP_FAILURE (_state->mxf_writer.Finalize())) {
- boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string()));
+ Kumu::Result_t r = _state->mxf_writer.Finalize();
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (MXFFileError ("error in finalizing video MXF", _asset->path().string(), r));
}
_finalized = true;
StereoPictureFrame::StereoPictureFrame (boost::filesystem::path mxf_path, int n)
{
ASDCP::JP2K::MXFSReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (mxf_path.string().c_str()))) {
- boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path));
+ Kumu::Result_t r = reader.OpenRead (mxf_path.string().c_str());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open MXF file for reading", mxf_path, r));
}
/* XXX: unfortunate guesswork on this buffer size */
libdcp::make_digest (string filename, boost::function<void (float)>* progress)
{
Kumu::FileReader reader;
- if (ASDCP_FAILURE (reader.OpenRead (filename.c_str ()))) {
- boost::throw_exception (FileError ("could not open file to compute digest", filename));
+ Kumu::Result_t r = reader.OpenRead (filename.c_str ());
+ if (ASDCP_FAILURE (r)) {
+ boost::throw_exception (FileError ("could not open file to compute digest", filename, r));
}
SHA_CTX sha;
if (r == Kumu::RESULT_ENDOFFILE) {
break;
} else if (ASDCP_FAILURE (r)) {
- boost::throw_exception (FileError ("could not read file to compute digest", filename));
+ boost::throw_exception (FileError ("could not read file to compute digest", filename, r));
}
SHA1_Update (&sha, read_buffer.Data(), read);