better comments
[ardour.git] / libs / ardour / session_directory.cc
1 /*
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>
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #include <cerrno>
25
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include "pbd/error.h"
30 #include "pbd/compose.h"
31 #include "pbd/file_utils.h"
32 #include "pbd/openuri.h"
33
34 #include "ardour/directory_names.h"
35 #include "ardour/session_directory.h"
36 #include "ardour/utils.h"
37
38 #include "pbd/i18n.h"
39
40 namespace ARDOUR {
41
42 using namespace std;
43 using namespace PBD::sys;
44
45
46 /* keep a static cache because SessionDirectory is used in various places. */
47 std::map<std::string,std::string> SessionDirectory::root_cache;
48
49 SessionDirectory::SessionDirectory (const std::string& session_path)
50         : m_root_path(session_path)
51 {
52
53 }
54
55 SessionDirectory&
56 SessionDirectory::operator= (const std::string& newpath)
57 {
58         m_root_path = newpath;
59         root_cache.clear ();
60         return *this;
61 }
62
63 bool
64 SessionDirectory::create ()
65 {
66         vector<std::string> sub_dirs = sub_directories ();
67         for (vector<std::string>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
68         {
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;
71                         return false;
72                 }
73         }
74
75         return true;
76 }
77
78 bool
79 SessionDirectory::is_valid () const
80 {
81         if (!Glib::file_test (m_root_path, Glib::FILE_TEST_IS_DIR)) return false;
82
83         vector<std::string> sub_dirs = sub_directories ();
84
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;
88                         return false;
89                 }
90         }
91         return true;
92 }
93
94 const std::string
95 SessionDirectory::old_sound_path () const
96 {
97         return Glib::build_filename (m_root_path, old_sound_dir_name);
98 }
99
100 const std::string
101 SessionDirectory::sources_root () const
102 {
103         if (root_cache.find (m_root_path) != root_cache.end()) {
104                 return root_cache[m_root_path];
105         }
106
107         root_cache.clear ();
108
109         std::string p = m_root_path;
110
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);
113
114         if (filename == ".") {
115                 p = PBD::get_absolute_path (m_root_path);
116         }
117
118         const string legalized_root (legalize_for_path (Glib::path_get_basename(p)));
119
120         std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
121
122         /* check the interchange folder:
123          *
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.
128          *
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:
132          *
133          *    The session object is created with the "snapshot-name", only
134          *    when loading the .ardour session file, the actual name is set.
135          *
136          *    SessionDirectory is created with the session itself
137          *    and picks up the wrong inital name.
138          *
139          *    SessionDirectory is also used directly by the AudioRegionImporter,
140          *    and the peak-file background thread (session.cc).
141          *
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.
145          */
146         try {
147                 Glib::Dir dir(sources_root_path);
148
149                 std::list<std::string> entries;
150
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) == '.') {
154                                 continue;
155                         }
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)) {
159                                 continue;
160                         }
161                         entries.push_back(*di);
162                 }
163
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;
167                         }
168                         root_cache[m_root_path] = Glib::build_filename (sources_root_path, entries.front());
169                 }
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
174                 }
175         } catch (Glib::FileError const&) {
176                 ;
177         }
178
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);
181         }
182
183         return root_cache[m_root_path];
184 }
185
186 const std::string
187 SessionDirectory::sources_root_2X () const
188 {
189         std::string p = m_root_path;
190         std::string filename = Glib::path_get_basename(p);
191
192         if (filename == ".") {
193                 p = PBD::get_absolute_path (m_root_path);
194         }
195
196         const string legalized_root (legalize_for_path_2X (Glib::path_get_basename(p)));
197
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);
200 }
201
202 const std::string
203 SessionDirectory::sound_path () const
204 {
205         if (Glib::file_test (old_sound_path (), Glib::FILE_TEST_IS_DIR)) return old_sound_path();
206
207         // the new style sound directory
208         return Glib::build_filename (sources_root(), sound_dir_name);
209 }
210
211 const std::string
212 SessionDirectory::sound_path_2X () const
213 {
214         return Glib::build_filename (sources_root_2X(), sound_dir_name);
215 }
216
217 const std::string
218 SessionDirectory::midi_path () const
219 {
220         return Glib::build_filename (sources_root(), midi_dir_name);
221 }
222
223 const std::string
224 SessionDirectory::midi_patch_path () const
225 {
226         return Glib::build_filename (sources_root(), midi_patch_dir_name);
227 }
228
229 const std::string
230 SessionDirectory::video_path () const
231 {
232         return Glib::build_filename (sources_root(), video_dir_name);
233 }
234
235 const std::string
236 SessionDirectory::peak_path () const
237 {
238         return Glib::build_filename (m_root_path, peak_dir_name);
239 }
240
241 const std::string
242 SessionDirectory::dead_path () const
243 {
244         return Glib::build_filename (m_root_path, dead_dir_name);
245 }
246
247 const std::string
248 SessionDirectory::export_path () const
249 {
250         return Glib::build_filename (m_root_path, export_dir_name);
251 }
252
253 const std::string
254 SessionDirectory::backup_path () const
255 {
256         return Glib::build_filename (m_root_path, backup_dir_name);
257 }
258
259 const vector<std::string>
260 SessionDirectory::sub_directories () const
261 {
262         vector<std::string> tmp_paths;
263
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 ());
271
272         return tmp_paths;
273 }
274
275 } // namespace ARDOUR