13
0

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:
Tim Mayberry 2007-05-19 11:31:27 +00:00
parent fd6408e6ba
commit b99c6c6e1d
5 changed files with 111 additions and 30 deletions

View File

@ -80,8 +80,15 @@ public:
bool is_valid () const;
/**
* @return true If a new session directory and all the
* subdirectories were created, otherwise false.
* Create the session directory and all the subdirectories.
*
* @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 ();

View File

@ -133,23 +133,48 @@ Session::Session (AudioEngine &eng,
SessionDirectory sdir(fullpath);
if (mix_template &&
sdir.create() &&
create_session_file_from_template (*mix_template)) {
if(mix_template) {
// try and create a new session directory
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
<<" using template" << *mix_template
<< endl;
} else if (sdir.is_valid ()) {
} else {
// 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
<< " using snapshot " << snapshot_name << " (1)"
<< endl;
} else {
destroy ();
throw failed_constructor ();
}
if (second_stage_init (false)) {

View File

@ -43,23 +43,29 @@ SessionDirectory::SessionDirectory (const string& session_path)
bool
SessionDirectory::create ()
{
if (is_directory (m_root_path))
{
PBD::error << string_compose(_("Cannot create Session directory at path %1 as it already exists"), m_root_path.to_string()) << endmsg;
return false;
}
bool is_new = false;
vector<path> sub_dirs = sub_directories ();
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;
return false;
if(create_directories(*i)) {
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

View File

@ -21,6 +21,8 @@
#include <glib.h>
#include <glib/gstdio.h>
#include <cerrno>
#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h>
@ -67,24 +69,28 @@ is_directory (const path & p)
bool
create_directory(const path & p)
{
if (g_mkdir (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO) != 0)
{
warning << "Unable to create directory at path: " << p.to_string() << endmsg;
return false;
}
if(is_directory(p)) 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;
}
bool
create_directories(const path & p)
{
if (g_mkdir_with_parents (p.to_string().c_str(), S_IRWXU|S_IRWXG|S_IRWXO) != 0)
{
warning << "Unable to create directory at path: " << p.to_string() << endmsg;
return false;
}
if(is_directory(p)) 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;
}

View File

@ -19,6 +19,7 @@
#ifndef __filesystem_h__
#define __filesystem_h__
#include <stdexcept>
#include <string>
namespace PBD {
@ -50,11 +51,47 @@ private:
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);
/// @return true if path at p is a directory.
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);
/**
* 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);
string basename (const path& p);