elaborate on prev. commit: ignore irrelevant files in the interchange dir.
[ardour.git] / libs / ardour / session_directory.cc
1 /*
2         Copyright (C) 2007 Tim Mayberry
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cerrno>
20
21 #include <glibmm/fileutils.h>
22 #include <glibmm/miscutils.h>
23
24 #include "pbd/error.h"
25 #include "pbd/compose.h"
26 #include "pbd/file_utils.h"
27
28 #include "ardour/directory_names.h"
29 #include "ardour/session_directory.h"
30 #include "ardour/utils.h"
31
32 #include "i18n.h"
33
34 namespace ARDOUR {
35
36 using namespace std;
37 using namespace PBD::sys;
38
39
40 /* keep a static cache because SessionDirectory is used in various places. */
41 std::map<std::string,std::string> SessionDirectory::root_cache;
42
43 SessionDirectory::SessionDirectory (const std::string& session_path)
44         : m_root_path(session_path)
45 {
46
47 }
48
49 SessionDirectory&
50 SessionDirectory::operator= (const std::string& newpath)
51 {
52         m_root_path = newpath;
53         root_cache.clear ();
54         return *this;
55 }
56
57 bool
58 SessionDirectory::create ()
59 {
60         vector<std::string> sub_dirs = sub_directories ();
61         for (vector<std::string>::const_iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i)
62         {
63                 if (g_mkdir_with_parents (i->c_str(), 0755) != 0) {
64                         PBD::error << string_compose(_("Cannot create Session directory at path %1 Error: %2"), *i, g_strerror(errno)) << endmsg;
65                         return false;
66                 }
67         }
68
69         return true;
70 }
71
72 bool
73 SessionDirectory::is_valid () const
74 {
75         if (!Glib::file_test (m_root_path, Glib::FILE_TEST_IS_DIR)) return false;
76
77         vector<std::string> sub_dirs = sub_directories ();
78
79         for (vector<std::string>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
80                 if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
81                         PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), *i) << endmsg;
82                         return false;
83                 }
84         }
85         return true;
86 }
87
88 const std::string
89 SessionDirectory::old_sound_path () const
90 {
91         return Glib::build_filename (m_root_path, old_sound_dir_name);
92 }
93
94 const std::string
95 SessionDirectory::sources_root () const
96 {
97         if (root_cache.find (m_root_path) != root_cache.end()) {
98                 return root_cache[m_root_path];
99         }
100
101         root_cache.clear ();
102
103         std::string p = m_root_path;
104
105         // TODO ideally we'd use the session's name() here, and not the containing folder's name.
106         std::string filename = Glib::path_get_basename(p);
107
108         if (filename == ".") {
109                 p = PBD::get_absolute_path (m_root_path);
110         }
111
112         const string legalized_root (legalize_for_path (Glib::path_get_basename(p)));
113
114         std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
115
116         /* check the interchange folder:
117          *
118          * 1) if a single subdir exists, use it, regardless of the name
119          * 2) if more than one dir is in interchange: abort, blame the user
120          * 3) if interchange does not exist or no subdir is present,
121          *    use the session-name to create one.
122          *
123          *    We use the name of the containing folder, not the actual
124          *    session name. The latter would require some API changes and
125          *    careful libardour updates:
126          *
127          *    The session object is created with the "snapshot-name", only
128          *    when loading the .ardour session file, the actual name is set.
129          *
130          *    SessionDirectory is created with the session itself
131          *    and picks up the wrong inital name.
132          *
133          *    SessionDirectory is also used directly by the AudioRegionImporter,
134          *    and the peak-file background thread (session.cc).
135          *
136          *        There is no actual benefit to use the session-name instead of
137          *        the folder-name. Under normal circumstances they are always
138          *        identical.  But it would be consistent to prefer the name.
139          */
140         try {
141                 Glib::Dir dir(sources_root_path);
142
143                 std::list<std::string> entries;
144
145                 for (Glib::DirIterator di = dir.begin(); di != dir.end(); di++) {
146                         // ignore hidden files (eg. OS X ".DS_Store")
147                         if ((*di).at(0) == '.') {
148                                 continue;
149                         }
150                         // and skip regular files (eg. Win Thumbs.db)
151                         string fullpath = Glib::build_filename (sources_root_path, *di);
152                         if (!Glib::file_test (fullpath, Glib::FILE_TEST_IS_DIR)) {
153                                 continue;
154                         }
155                         entries.push_back(*di);
156                 }
157
158                 if (entries.size() == 1) {
159                         if (entries.front() != legalized_root) {
160                                 PBD::info << _("session-dir and session-name mismatch. Please use 'Menu > Session > Rename' in the future to rename sessions.") << endmsg;
161                         }
162                         root_cache[m_root_path] = Glib::build_filename (sources_root_path, entries.front());
163                 }
164                 else if (entries.size() > 1) {
165                         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;
166                         assert (0); // not reached
167                 }
168         } catch (Glib::FileError) {
169                 ;
170         }
171
172         if (root_cache.find (m_root_path) == root_cache.end()) {
173                 root_cache[m_root_path] = Glib::build_filename (sources_root_path, legalized_root);
174         }
175
176         return root_cache[m_root_path];
177 }
178
179 const std::string
180 SessionDirectory::sources_root_2X () const
181 {
182         std::string p = m_root_path;
183         std::string filename = Glib::path_get_basename(p);
184
185         if (filename == ".") {
186                 p = PBD::get_absolute_path (m_root_path);
187         }
188
189         const string legalized_root (legalize_for_path_2X (Glib::path_get_basename(p)));
190
191         std::string sources_root_path = Glib::build_filename (m_root_path, interchange_dir_name);
192         return Glib::build_filename (sources_root_path, legalized_root);
193 }
194
195 const std::string
196 SessionDirectory::sound_path () const
197 {
198         if (Glib::file_test (old_sound_path (), Glib::FILE_TEST_IS_DIR)) return old_sound_path();
199
200         // the new style sound directory
201         return Glib::build_filename (sources_root(), sound_dir_name);
202 }
203
204 const std::string
205 SessionDirectory::sound_path_2X () const
206 {
207         return Glib::build_filename (sources_root_2X(), sound_dir_name);
208 }
209
210 const std::string
211 SessionDirectory::midi_path () const
212 {
213         return Glib::build_filename (sources_root(), midi_dir_name);
214 }
215
216 const std::string
217 SessionDirectory::midi_patch_path () const
218 {
219         return Glib::build_filename (sources_root(), midi_patch_dir_name);
220 }
221
222 const std::string
223 SessionDirectory::video_path () const
224 {
225         return Glib::build_filename (sources_root(), video_dir_name);
226 }
227
228 const std::string
229 SessionDirectory::peak_path () const
230 {
231         return Glib::build_filename (m_root_path, peak_dir_name);
232 }
233
234 const std::string
235 SessionDirectory::dead_path () const
236 {
237         return Glib::build_filename (m_root_path, dead_dir_name);
238 }
239
240 const std::string
241 SessionDirectory::export_path () const
242 {
243         return Glib::build_filename (m_root_path, export_dir_name);
244 }
245
246 const vector<std::string>
247 SessionDirectory::sub_directories () const
248 {
249         vector<std::string> tmp_paths;
250
251         tmp_paths.push_back (sound_path ());
252         tmp_paths.push_back (midi_path ());
253         tmp_paths.push_back (video_path ());
254         tmp_paths.push_back (peak_path ());
255         tmp_paths.push_back (dead_path ());
256         tmp_paths.push_back (export_path ());
257
258         return tmp_paths;
259 }
260
261 } // namespace ARDOUR