2 * Copyright (C) 2007-2009 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2012 Tim Mayberry <mojofunk@gmail.com>
4 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
6 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
7 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
29 #include "pbd/error.h"
30 #include "pbd/compose.h"
31 #include "pbd/file_utils.h"
32 #include "pbd/openuri.h"
34 #include "ardour/directory_names.h"
35 #include "ardour/session_directory.h"
36 #include "ardour/utils.h"
43 using namespace PBD::sys;
46 /* keep a static cache because SessionDirectory is used in various places. */
47 std::map<std::string,std::string> SessionDirectory::root_cache;
49 SessionDirectory::SessionDirectory (const std::string& session_path)
50 : m_root_path(session_path)
56 SessionDirectory::operator= (const std::string& newpath)
58 m_root_path = newpath;
64 SessionDirectory::create ()
66 vector<std::string> sub_dirs = sub_directories ();
67 for (vector<std::string>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
69 if (g_mkdir_with_parents (i->c_str(), 0755) != 0) {
70 PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), *i, g_strerror(errno)) << endmsg;
79 SessionDirectory::is_valid () const
81 if (!Glib::file_test (m_root_path, Glib::FILE_TEST_IS_DIR)) return false;
83 vector<std::string> sub_dirs = sub_directories ();
85 for (vector<std::string>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
86 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
87 PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), *i) << endmsg;
95 SessionDirectory::old_sound_path () const
97 return Glib::build_filename (m_root_path, old_sound_dir_name);
101 SessionDirectory::sources_root () const
103 if (root_cache.find (m_root_path) != root_cache.end()) {
104 return root_cache[m_root_path];
109 std::string p = m_root_path;
111 // TODO ideally we'd use the session's name() here, and not the containing folder's name.
112 std::string filename = Glib::path_get_basename(p);
114 if (filename == ".") {
115 p = PBD::get_absolute_path (m_root_path);
118 const string legalized_root (legalize_for_path (Glib::path_get_basename(p)));
120 std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
122 /* check the interchange folder:
124 * 1) if a single subdir exists, use it, regardless of the name
125 * 2) if more than one dir is in interchange: abort, blame the user
126 * 3) if interchange does not exist or no subdir is present,
127 * use the session-name to create one.
129 * We use the name of the containing folder, not the actual
130 * session name. The latter would require some API changes and
131 * careful libardour updates:
133 * The session object is created with the "snapshot-name", only
134 * when loading the .ardour session file, the actual name is set.
136 * SessionDirectory is created with the session itself
137 * and picks up the wrong inital name.
139 * SessionDirectory is also used directly by the AudioRegionImporter,
140 * and the peak-file background thread (session.cc).
142 * There is no actual benefit to use the session-name instead of
143 * the folder-name. Under normal circumstances they are always
144 * identical. But it would be consistent to prefer the name.
147 Glib::Dir dir(sources_root_path);
149 std::list<std::string> entries;
151 for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
152 // ignore hidden files (eg. OS X ".DS_Store")
153 if ((*di).at(0) == '.') {
156 // and skip regular files (eg. Win Thumbs.db)
157 string fullpath = Glib::build_filename (sources_root_path, *di);
158 if (!Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR)) {
161 entries.push_back(*di);
164 if (entries.size() == 1) {
165 if (entries.front() != legalized_root) {
166 PBD::info << _("session-dir and session-name mismatch. Please use 'Menu > Session > Rename' in the future to rename sessions.") << endmsg;
168 root_cache[m_root_path] = Glib::build_filename (sources_root_path, entries.front());
170 else if (entries.size() > 1) {
171 PBD::open_folder (sources_root_path);
172 PBD::fatal << string_compose (_("The session's interchange dir is tainted.\nThere is more than one folder in '%1'.\nPlease remove extra subdirs to reduce possible filename ambiguties."), sources_root_path) << endmsg;
173 assert (0); // not reached
175 } catch (Glib::FileError const&) {
179 if (root_cache.find (m_root_path) == root_cache.end()) {
180 root_cache[m_root_path] = Glib::build_filename (sources_root_path, legalized_root);
183 return root_cache[m_root_path];
187 SessionDirectory::sources_root_2X () const
189 std::string p = m_root_path;
190 std::string filename = Glib::path_get_basename(p);
192 if (filename == ".") {
193 p = PBD::get_absolute_path (m_root_path);
196 const string legalized_root (legalize_for_path_2X (Glib::path_get_basename(p)));
198 std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
199 return Glib::build_filename (sources_root_path, legalized_root);
203 SessionDirectory::sound_path () const
205 if (Glib::file_test (old_sound_path (), Glib::FILE_TEST_IS_DIR)) return old_sound_path();
207 // the new style sound directory
208 return Glib::build_filename (sources_root(), sound_dir_name);
212 SessionDirectory::sound_path_2X () const
214 return Glib::build_filename (sources_root_2X(), sound_dir_name);
218 SessionDirectory::midi_path () const
220 return Glib::build_filename (sources_root(), midi_dir_name);
224 SessionDirectory::midi_patch_path () const
226 return Glib::build_filename (sources_root(), midi_patch_dir_name);
230 SessionDirectory::video_path () const
232 return Glib::build_filename (sources_root(), video_dir_name);
236 SessionDirectory::peak_path () const
238 return Glib::build_filename (m_root_path, peak_dir_name);
242 SessionDirectory::dead_path () const
244 return Glib::build_filename (m_root_path, dead_dir_name);
248 SessionDirectory::export_path () const
250 return Glib::build_filename (m_root_path, export_dir_name);
254 SessionDirectory::backup_path () const
256 return Glib::build_filename (m_root_path, backup_dir_name);
259 const vector<std::string>
260 SessionDirectory::sub_directories () const
262 vector<std::string> tmp_paths;
264 tmp_paths.push_back (sound_path ());
265 tmp_paths.push_back (midi_path ());
266 tmp_paths.push_back (video_path ());
267 tmp_paths.push_back (peak_path ());
268 tmp_paths.push_back (dead_path ());
269 tmp_paths.push_back (export_path ());
270 tmp_paths.push_back (backup_path ());
275 } // namespace ARDOUR