summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Mayberry <mojofunk@gmail.com>2007-05-18 02:46:12 +0000
committerTim Mayberry <mojofunk@gmail.com>2007-05-18 02:46:12 +0000
commit04c1ce19607eb04a1c5b306952e0c8e29f64525c (patch)
tree252d363ba4631f14a0cfb99376385e2cf45c12d6
parent113b80adb029ae543824d40a7c78eb13e42dd934 (diff)
Add PBD::sys::path class that has a similar API to boost::filesystem::path but using glib/mm filesystem utility functions
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
-rw-r--r--libs/ardour/SConscript1
-rw-r--r--libs/ardour/ardour/session_directory.h101
-rw-r--r--libs/ardour/directory_names.cc2
-rw-r--r--libs/ardour/session.cc11
-rw-r--r--libs/ardour/session_directory.cc126
-rw-r--r--libs/pbd/SConscript1
-rw-r--r--libs/pbd/filesystem.cc100
-rw-r--r--libs/pbd/pbd/filesystem.h66
8 files changed, 402 insertions, 6 deletions
diff --git a/libs/ardour/SConscript b/libs/ardour/SConscript
index 23d67074b7..dba845f218 100644
--- a/libs/ardour/SConscript
+++ b/libs/ardour/SConscript
@@ -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
index 0000000000..a5eecffe99
--- /dev/null
+++ b/libs/ardour/ardour/session_directory.h
@@ -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
diff --git a/libs/ardour/directory_names.cc b/libs/ardour/directory_names.cc
index ca8ed45643..82f420a144 100644
--- a/libs/ardour/directory_names.cc
+++ b/libs/ardour/directory_names.cc
@@ -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");
diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc
index 9a858daca0..3c43a70165 100644
--- a/libs/ardour/session.cc
+++ b/libs/ardour/session.cc
@@ -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
index 0000000000..a4dbc286e6
--- /dev/null
+++ b/libs/ardour/session_directory.cc
@@ -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
diff --git a/libs/pbd/SConscript b/libs/pbd/SConscript
index 3aaeb1bf1b..de914e0f7a 100644
--- a/libs/pbd/SConscript
+++ b/libs/pbd/SConscript
@@ -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
index 0000000000..f376fa72f0
--- /dev/null
+++ b/libs/pbd/filesystem.cc
@@ -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
index 0000000000..11d62be632
--- /dev/null
+++ b/libs/pbd/pbd/filesystem.h
@@ -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