When loading sessions, create any missing session directories rather than throwing an exception.
Change the meaning of the return value of SessionDirectory::create and add documentation to explain usage. Add PBD::sys::filesystem_error to indicate a filesystem error and throw it where necessary. Change the semantics of PBD::sys::create_directory/ies functions to match boost::filesystem git-svn-id: svn://localhost/ardour2/trunk@1884 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
parent
fd6408e6ba
commit
b99c6c6e1d
|
@ -80,8 +80,15 @@ public:
|
||||||
bool is_valid () const;
|
bool is_valid () const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true If a new session directory and all the
|
* Create the session directory and all the subdirectories.
|
||||||
* subdirectories were created, otherwise false.
|
*
|
||||||
|
* @throw PBD::sys::filesystem_error if the directories were
|
||||||
|
* not able to be created.
|
||||||
|
*
|
||||||
|
* @return true If a new session directory was created, otherwise
|
||||||
|
* (if it already existed) false.
|
||||||
|
*
|
||||||
|
* @post is_valid ()
|
||||||
*/
|
*/
|
||||||
bool create ();
|
bool create ();
|
||||||
|
|
||||||
|
|
|
@ -133,23 +133,48 @@ Session::Session (AudioEngine &eng,
|
||||||
|
|
||||||
SessionDirectory sdir(fullpath);
|
SessionDirectory sdir(fullpath);
|
||||||
|
|
||||||
if (mix_template &&
|
if(mix_template) {
|
||||||
sdir.create() &&
|
// try and create a new session directory
|
||||||
create_session_file_from_template (*mix_template)) {
|
try
|
||||||
|
{
|
||||||
|
if(!sdir.create()) {
|
||||||
|
// an existing session.
|
||||||
|
// throw a_more_meaningful_exception()
|
||||||
|
destroy ();
|
||||||
|
throw failed_constructor ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(sys::filesystem_error& ex)
|
||||||
|
{
|
||||||
|
destroy ();
|
||||||
|
throw failed_constructor ();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!create_session_file_from_template (*mix_template)) {
|
||||||
|
destroy ();
|
||||||
|
throw failed_constructor ();
|
||||||
|
}
|
||||||
|
|
||||||
cerr << "Creating session " << fullpath
|
cerr << "Creating session " << fullpath
|
||||||
<<" using template" << *mix_template
|
<<" using template" << *mix_template
|
||||||
<< endl;
|
<< endl;
|
||||||
|
} else {
|
||||||
} else if (sdir.is_valid ()) {
|
// must be an existing session
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// ensure the necessary session subdirectories exist
|
||||||
|
// in case the directory structure has changed etc.
|
||||||
|
sdir.create();
|
||||||
|
}
|
||||||
|
catch(sys::filesystem_error& ex)
|
||||||
|
{
|
||||||
|
destroy ();
|
||||||
|
throw failed_constructor ();
|
||||||
|
}
|
||||||
|
|
||||||
cerr << "Loading session " << fullpath
|
cerr << "Loading session " << fullpath
|
||||||
<< " using snapshot " << snapshot_name << " (1)"
|
<< " using snapshot " << snapshot_name << " (1)"
|
||||||
<< endl;
|
<< endl;
|
||||||
|
|
||||||
} else {
|
|
||||||
destroy ();
|
|
||||||
throw failed_constructor ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (second_stage_init (false)) {
|
if (second_stage_init (false)) {
|
||||||
|
|
|
@ -43,23 +43,29 @@ SessionDirectory::SessionDirectory (const string& session_path)
|
||||||
bool
|
bool
|
||||||
SessionDirectory::create ()
|
SessionDirectory::create ()
|
||||||
{
|
{
|
||||||
if (is_directory (m_root_path))
|
bool is_new = false;
|
||||||
{
|
|
||||||
PBD::error << string_compose(_("Cannot create Session directory at path %1 as it already exists"), m_root_path.to_string()) << endmsg;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<path> sub_dirs = sub_directories ();
|
vector<path> sub_dirs = sub_directories ();
|
||||||
for (vector<path>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
|
for (vector<path>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
|
||||||
{
|
{
|
||||||
if (!create_directories(*i))
|
try
|
||||||
{
|
{
|
||||||
PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), (*i).to_string(), g_strerror (errno)) << endmsg;
|
if(create_directories(*i)) {
|
||||||
return false;
|
PBD::info << string_compose(_("Created Session directory at path %1"), (*i).to_string()) << endmsg;
|
||||||
|
is_new = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (PBD::sys::filesystem_error& ex)
|
||||||
|
{
|
||||||
|
// log the error
|
||||||
|
PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), (*i).to_string(), ex.what()) << endmsg;
|
||||||
|
|
||||||
|
// and rethrow
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return is_new;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <glib/gstdio.h>
|
#include <glib/gstdio.h>
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
#include <glibmm/fileutils.h>
|
#include <glibmm/fileutils.h>
|
||||||
#include <glibmm/miscutils.h>
|
#include <glibmm/miscutils.h>
|
||||||
|
|
||||||
|
@ -67,24 +69,28 @@ is_directory (const path & p)
|
||||||
bool
|
bool
|
||||||
create_directory(const path & p)
|
create_directory(const path & p)
|
||||||
{
|
{
|
||||||
if (g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO) != 0)
|
if(is_directory(p)) return false;
|
||||||
{
|
|
||||||
warning << "Unable to create directory at path: " << p.to_string() << endmsg;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
int error = g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
|
||||||
|
|
||||||
|
if(error == -1)
|
||||||
|
{
|
||||||
|
throw filesystem_error(g_strerror(errno), errno);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
create_directories(const path & p)
|
create_directories(const path & p)
|
||||||
{
|
{
|
||||||
if (g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO) != 0)
|
if(is_directory(p)) return false;
|
||||||
{
|
|
||||||
warning << "Unable to create directory at path: " << p.to_string() << endmsg;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
int error = g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
|
||||||
|
|
||||||
|
if(error == -1)
|
||||||
|
{
|
||||||
|
throw filesystem_error(g_strerror(errno), errno);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#ifndef __filesystem_h__
|
#ifndef __filesystem_h__
|
||||||
#define __filesystem_h__
|
#define __filesystem_h__
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace PBD {
|
namespace PBD {
|
||||||
|
@ -50,11 +51,47 @@ private:
|
||||||
string m_path;
|
string m_path;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class filesystem_error : public std::runtime_error
|
||||||
|
{
|
||||||
|
const int m_error_code;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit filesystem_error(const std::string & what, int error_code=0)
|
||||||
|
: std::runtime_error(what), m_error_code(error_code) { }
|
||||||
|
|
||||||
|
int system_error() const { return m_error_code; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @return true if path at p exists
|
||||||
bool exists(const path & p);
|
bool exists(const path & p);
|
||||||
|
|
||||||
|
/// @return true if path at p is a directory.
|
||||||
bool is_directory(const path & p);
|
bool is_directory(const path & p);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to create a directory at p as if by the glib function g_mkdir
|
||||||
|
* with a second argument of S_IRWXU|S_IRWXG|S_IRWXO
|
||||||
|
*
|
||||||
|
* @throw filesystem_error if mkdir fails for any other reason other than
|
||||||
|
* the directory already exists.
|
||||||
|
*
|
||||||
|
* @return true If the directory p was created, otherwise false
|
||||||
|
*
|
||||||
|
* @post is_directory(p)
|
||||||
|
*/
|
||||||
bool create_directory(const path & p);
|
bool create_directory(const path & p);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to create a directory at p as if by the glib function
|
||||||
|
* g_mkdir_with_parents with a second argument of S_IRWXU|S_IRWXG|S_IRWXO
|
||||||
|
*
|
||||||
|
* @throw filesystem_error if g_mkdir_with_parents fails for any other
|
||||||
|
* reason other than the directory already exists.
|
||||||
|
*
|
||||||
|
* @return true If the directory at p was created, otherwise false
|
||||||
|
*
|
||||||
|
* @post is_directory(p)
|
||||||
|
*/
|
||||||
bool create_directories(const path & p);
|
bool create_directories(const path & p);
|
||||||
|
|
||||||
string basename (const path& p);
|
string basename (const path& p);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user