Add PBD::sys::path class that has a similar API to boost::filesystem::path but using...
authorTim Mayberry <mojofunk@gmail.com>
Fri, 18 May 2007 02:46:12 +0000 (02:46 +0000)
committerTim Mayberry <mojofunk@gmail.com>
Fri, 18 May 2007 02:46:12 +0000 (02:46 +0000)
Add ARDOUR::SessionDirectory class

Use SessionDirectory to create the session directory structure when creating a new session

git-svn-id: svn://localhost/ardour2/trunk@1874 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/SConscript
libs/ardour/ardour/session_directory.h [new file with mode: 0644]
libs/ardour/directory_names.cc
libs/ardour/session.cc
libs/ardour/session_directory.cc [new file with mode: 0644]
libs/pbd/SConscript
libs/pbd/filesystem.cc [new file with mode: 0644]
libs/pbd/pbd/filesystem.h [new file with mode: 0644]

index 23d67074b750686f0670e60baa8d81f1636c5778..dba845f2188be70c544868a3a195058a397ee7f4 100644 (file)
@@ -96,6 +96,7 @@ session.cc
 session_butler.cc
 session_click.cc
 session_command.cc
+session_directory.cc
 session_events.cc
 session_export.cc
 session_midi.cc
diff --git a/libs/ardour/ardour/session_directory.h b/libs/ardour/ardour/session_directory.h
new file mode 100644 (file)
index 0000000..a5eecff
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+       Copyright (C) 2007 Tim Mayberry 
+
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; either version 2 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program; if not, write to the Free Software
+       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#ifndef __ardour_session_directory_h__
+#define __ardour_session_directory_h__
+
+#include <string>
+#include <vector>
+
+#include <pbd/filesystem.h>
+
+namespace ARDOUR {
+
+using std::string;
+using std::vector;
+using PBD::sys::path;
+
+class SessionDirectory
+{
+public:
+       /**
+        * @param session_path An absolute path to a session directory.
+        */
+       SessionDirectory (const string& session_path);
+
+       /**
+        * @return the absolute path to the root directory of the session
+        */
+       const path root_path() const { return m_root_path; }
+
+       /**
+        * @return the absolute path to the directory in which 
+        * the session stores audio files.
+        *
+        * If the session is an older session with an existing
+        * "sounds" directory then it will return a path to that
+        * directory otherwise it will return the new location
+        * of root_path()/interchange/session_name/audiofiles
+        */
+       const path sound_path () const;
+
+       /**
+        * @return The absolute path to the directory in which all
+        * peak files are stored for a session.
+        */
+       const path peak_path () const;
+
+       /**
+        * @return The absolute path to the directory that audio
+        * files are moved to when they are no longer part of the
+        * session.
+        */
+       const path dead_sound_path () const;
+
+       /**
+        * @return true if session directory and all the required 
+        * subdirectories exist.
+        */
+       bool is_valid () const;
+
+       /**
+        * @return true If a new session directory and all the 
+        * subdirectories were created, otherwise false.
+        */
+       bool create ();
+
+protected:
+
+       /**
+        * @return The path to the old style sound directory.
+        * It isn't created by create().
+        */
+       const path old_sound_path () const;
+
+       /**
+        * @return a vector containing the fullpath of all subdirectories.
+        */
+       const vector<PBD::sys::path> sub_directories () const;
+
+       /// The path to the root of the session directory.
+       const path m_root_path;
+};
+
+} // namespace ARDOUR
+
+#endif
index ca8ed456439ec81b9ced4757b1a32996726b96ee..82f420a14404587269bddc81da7c692f033cfccd 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "i18n.h"
 
-namespace ARDOUR {                       
+namespace ARDOUR {
 
 const char* const old_sound_dir_name = X_("sounds");
 const char* const sound_dir_name = X_("audiofiles");
index 9a858daca00739ea97ff6fe351eb99283a0e4a0c..3c43a701651b85973697c83aeed55cf421a952ae 100644 (file)
@@ -73,6 +73,7 @@
 #include <ardour/source_factory.h>
 #include <ardour/region_factory.h>
 #include <ardour/filename_extensions.h>
+#include <ardour/session_directory.h>
 
 #ifdef HAVE_LIBLO
 #include <ardour/osc.h>
@@ -213,11 +214,11 @@ Session::Session (AudioEngine &eng,
 
        initialize_start_and_end_locations(0, initial_length);
 
-       if (g_file_test (_path.c_str(), GFileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) ||
-                       !create_session_directory () ||
-                       !create_session_file ()) {
-                       destroy ();
-                       throw failed_constructor ();
+       SessionDirectory sdir(fullpath);
+       
+       if (!sdir.create () || !create_session_file ()) {
+               destroy ();
+               throw failed_constructor ();
        }
 
        {
diff --git a/libs/ardour/session_directory.cc b/libs/ardour/session_directory.cc
new file mode 100644 (file)
index 0000000..a4dbc28
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+       Copyright (C) 2007 Tim Mayberry 
+
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; either version 2 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program; if not, write to the Free Software
+       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <cerrno>
+
+#include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
+
+#include <pbd/error.h>
+#include <pbd/compose.h>
+#include <pbd/filesystem.h>
+
+#include <ardour/directory_names.h>
+#include <ardour/session_directory.h>
+
+#include "i18n.h"
+
+namespace ARDOUR {
+
+using namespace PBD::sys;
+
+SessionDirectory::SessionDirectory (const string& session_path)
+       : m_root_path(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;
+       }
+
+       vector<path> sub_dirs = sub_directories ();
+       for (vector<path>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
+       {
+               if (!create_directories(*i))
+               {
+                       PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), (*i).to_string(), g_strerror (errno)) << endmsg;
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+bool
+SessionDirectory::is_valid () const
+{
+       if (!is_directory (m_root_path)) return false;
+
+       vector<path> sub_dirs = sub_directories ();
+
+       for (vector<path>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
+               if (!is_directory (*i)) {
+                       PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), (*i).to_string()) << endmsg;
+                       return false;
+               }
+       }
+       return true;
+}
+
+const path
+SessionDirectory::old_sound_path () const
+{
+       return path(m_root_path) /= old_sound_dir_name;
+}
+
+const path
+SessionDirectory::sound_path () const
+{
+       if(is_directory (old_sound_path ())) return old_sound_path();
+
+       // the new style sound directory
+       path l_sound_path(m_root_path);
+
+       l_sound_path /= interchange_dir_name;
+       l_sound_path /= basename(m_root_path);
+       l_sound_path /= sound_dir_name;
+
+       return l_sound_path;
+}
+
+const path
+SessionDirectory::peak_path () const
+{
+       return path(m_root_path) /= peak_dir_name;
+}
+
+const path
+SessionDirectory::dead_sound_path () const
+{
+       return path(m_root_path) /= dead_sound_dir_name;
+}
+
+const vector<path>
+SessionDirectory::sub_directories () const
+{
+       vector<path> tmp_paths; 
+
+       tmp_paths.push_back ( sound_path () );
+       tmp_paths.push_back ( peak_path () );
+       tmp_paths.push_back ( dead_sound_path () );
+
+       return tmp_paths;
+}
+
+} // namespace ARDOUR
index 3aaeb1bf1b172e9a4389123f2ce0f97cb2e92fcb..de914e0f7aa20ac43dc1ef87222b68eb170fc1e8 100644 (file)
@@ -27,6 +27,7 @@ controllable.cc
 enumwriter.cc
 dmalloc.cc
 error.cc
+filesystem.cc
 id.cc
 mountpoint.cc
 path.cc
diff --git a/libs/pbd/filesystem.cc b/libs/pbd/filesystem.cc
new file mode 100644 (file)
index 0000000..f376fa7
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+       Copyright (C) 2007 Tim Mayberry 
+
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; either version 2 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program; if not, write to the Free Software
+       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <sys/stat.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include <glibmm/fileutils.h>
+#include <glibmm/miscutils.h>
+
+#include <pbd/filesystem.h>
+#include <pbd/error.h>
+
+namespace PBD {
+
+namespace sys {
+       
+path&
+path::operator/=(const path& rhs)
+{
+       m_path = Glib::build_filename(m_path, rhs.m_path);
+       return *this;
+}
+
+path&
+path::operator/=(const string& rhs)
+{
+       m_path = Glib::build_filename(m_path, rhs);
+       return *this;
+}
+
+path&
+path::operator/=(const char* rhs)
+{
+       m_path = Glib::build_filename(m_path, rhs);
+       return *this;
+}
+
+bool
+exists (const path & p)
+{
+       return Glib::file_test (p.to_string(), Glib::FILE_TEST_EXISTS);
+}
+
+bool
+is_directory (const path & p)
+{
+       return Glib::file_test (p.to_string(), Glib::FILE_TEST_IS_DIR);
+}
+
+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;
+       }
+
+       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;
+       }
+
+       return true;
+}
+
+string
+basename (const path & p)
+{
+       // I'm not sure if this works quite the same as boost::filesystem::basename
+       return Glib::path_get_basename (p.to_string ());
+}
+
+} // namespace sys
+
+} // namespace PBD
diff --git a/libs/pbd/pbd/filesystem.h b/libs/pbd/pbd/filesystem.h
new file mode 100644 (file)
index 0000000..11d62be
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+       Copyright (C) 2007 Tim Mayberry 
+
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; either version 2 of the License, or
+       (at your option) any later version.
+
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program; if not, write to the Free Software
+       Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#ifndef __filesystem_h__
+#define __filesystem_h__
+
+#include <string>
+
+namespace PBD {
+
+namespace sys {
+
+using std::string;
+
+class path
+{
+public:
+       path() : m_path("") { }
+       path(const path & p) : m_path(p.m_path) { }
+       path(const string & s) : m_path(s) { }
+       path(const char* s) : m_path(s) { }
+       
+       path& operator=(const path& p) { m_path = p.m_path; return *this;}
+       path& operator=(const string& s) { m_path = s; return *this; }
+       path& operator=(const char* s) { m_path = s; return *this; }
+
+       path& operator/=(const path& rhs);
+       path& operator/=(const string& s);
+       path& operator/=(const char* s);
+
+       const string to_string() const { return m_path; }
+
+private:
+
+       string m_path;
+};
+
+
+bool exists(const path & p);
+bool is_directory(const path & p);
+
+bool create_directory(const path & p);
+bool create_directories(const path & p);
+
+string basename (const path& p);
+
+} // namespace sys
+
+} // namespace PBD
+
+#endif