2 Copyright (C) 2006-2009 Paul Davis
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.
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.
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.
24 #include <stdio.h> // for rename(), sigh
29 #include "pbd/convert.h"
30 #include "pbd/basename.h"
31 #include "pbd/mountpoint.h"
32 #include "pbd/stl_delete.h"
33 #include "pbd/strsplit.h"
34 #include "pbd/shortpath.h"
35 #include "pbd/enumwriter.h"
37 #include <glibmm/miscutils.h>
38 #include <glibmm/fileutils.h>
39 #include <glibmm/thread.h>
41 #include "ardour/file_source.h"
42 #include "ardour/directory_names.h"
43 #include "ardour/session.h"
44 #include "ardour/session_directory.h"
45 #include "ardour/source_factory.h"
46 #include "ardour/filename_extensions.h"
51 using namespace ARDOUR;
55 PBD::Signal3<int,std::string,std::string,std::vector<std::string> > FileSource::AmbiguousFileName;
57 FileSource::FileSource (Session& session, DataType type, const string& path, Source::Flag flag)
58 : Source(session, type, path, flag)
63 set_within_session_from_path (path);
66 FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/)
67 : Source(session, node)
68 , _file_is_new (false)
70 /* this setting of _path is temporary - we expect derived classes
71 to call ::init() which will actually locate the file
72 and reset _path and _within_session correctly.
76 _within_session = true;
80 FileSource::removable () const
82 bool r = (_path.find (stub_dir_name) != string::npos) ||
84 && ((_flags & RemoveAtDestroy) ||
85 ((_flags & RemovableIfEmpty) && empty() == 0)));
91 FileSource::init (const string& pathstr, bool must_exist)
93 _timeline_position = 0;
95 if (Stateful::loading_state_version < 3000) {
96 if (!find_2X (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) {
97 throw MissingSource (pathstr, _type);
100 if (!find (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) {
101 throw MissingSource (pathstr, _type);
105 set_within_session_from_path (pathstr);
107 if (_within_session) {
108 _name = Glib::path_get_basename (_name);
111 if (_file_is_new && must_exist) {
119 FileSource::set_state (const XMLNode& node, int /*version*/)
121 const XMLProperty* prop;
123 if ((prop = node.property (X_("channel"))) != 0) {
124 _channel = atoi (prop->value());
133 FileSource::mark_take (const string& id)
141 FileSource::move_to_trash (const string& trash_dir_name)
143 if (!within_session() || !writable()) {
147 /* don't move the file across filesystems, just stick it in the
148 trash_dir_name directory on whichever filesystem it was already on
152 v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
153 v.push_back (trash_dir_name);
154 v.push_back (Glib::path_get_basename (_path));
156 string newpath = Glib::build_filename (v);
158 /* the new path already exists, try versioning */
160 if (Glib::file_test (newpath.c_str(), Glib::FILE_TEST_EXISTS)) {
161 char buf[PATH_MAX+1];
165 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
168 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
169 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
173 if (version == 999) {
174 PBD::error << string_compose (
175 _("there are already 1000 files with names like %1; versioning discontinued"),
182 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
183 PBD::error << string_compose (
184 _("cannot rename file source from %1 to %2 (%3)"),
185 _path, newpath, strerror (errno)) << endmsg;
189 if (move_dependents_to_trash() != 0) {
190 /* try to back out */
191 rename (newpath.c_str(), _path.c_str());
197 /* file can not be removed twice, since the operation is not idempotent */
198 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
203 /** Find the actual source file based on \a filename.
205 * If the source is within the session tree, \a filename should be a simple filename (no slashes).
206 * If the source is external, \a filename should be a full path.
207 * In either case, found_path is set to the complete absolute path of the source file.
208 * \return true iff the file was found.
211 FileSource::find (Session& s, DataType type, const string& path, bool must_exist,
212 bool& isnew, uint16_t& chan, string& found_path)
214 string search_path = s.source_search_path (type);
216 string pathstr = path;
219 cerr << "Searching along " << search_path << endl;
229 if (search_path.length() == 0) {
230 error << _("FileSource: search path not set") << endmsg;
234 split (search_path, dirs, ':');
239 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
241 cerr << "Searching in " << *i << " for " << pathstr << endl;
243 fullpath = Glib::build_filename (*i, pathstr);
245 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
247 hits.push_back (fullpath);
254 int which = FileSource::AmbiguousFileName (pathstr, search_path, hits).get_value_or (-1);
259 keeppath = hits[which];
262 } else if (cnt == 0) {
265 error << string_compose(
266 _("Filesource: cannot find required file (%1): while searching %2"),
267 pathstr, search_path) << endmsg;
274 /* Current find() is unable to parse relative path names to yet non-existant
275 sources. QuickFix(tm)
277 if (keeppath == "") {
279 error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl;
285 found_path = keeppath;
293 /** Find the actual source file based on \a filename.
295 * If the source is within the session tree, \a filename should be a simple filename (no slashes).
296 * If the source is external, \a filename should be a full path.
297 * In either case, found_path is set to the complete absolute path of the source file.
298 * \return true iff the file was found.
301 FileSource::find_2X (Session& s, DataType type, const string& path, bool must_exist,
302 bool& isnew, uint16_t& chan, string& found_path)
304 string search_path = s.source_search_path (type);
306 string pathstr = path;
307 string::size_type pos;
312 if (!Glib::path_is_absolute (pathstr)) {
314 /* non-absolute pathname: find pathstr in search path */
321 if (search_path.length() == 0) {
322 error << _("FileSource: search path not set") << endmsg;
326 split (search_path, dirs, ':');
330 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
332 fullpath = Glib::build_filename (*i, pathstr);
334 /* i (paul) made a nasty design error by using ':' as a special character in
335 Ardour 0.99 .. this hack tries to make things sort of work.
338 if ((pos = pathstr.find_last_of (':')) != string::npos) {
340 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
342 /* its a real file, no problem */
351 /* might be an older session using file:channel syntax. see if the version
352 without the :suffix exists
355 string shorter = pathstr.substr (0, pos);
356 fullpath = Glib::build_filename (*i, shorter);
358 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
359 chan = atoi (pathstr.substr (pos+1));
367 /* new derived file (e.g. for timefx) being created in a newer session */
374 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
383 error << string_compose (
384 _("FileSource: \"%1\" is ambigous when searching %2\n\t"),
385 pathstr, search_path) << endmsg;
388 } else if (cnt == 0) {
391 error << string_compose(
392 _("Filesource: cannot find required file (%1): while searching %2"),
393 pathstr, search_path) << endmsg;
400 /* Current find() is unable to parse relative path names to yet non-existant
401 sources. QuickFix(tm) */
402 if (keeppath == "") {
404 error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl;
410 found_path = keeppath;
416 /* external files and/or very very old style sessions include full paths */
418 /* ugh, handle ':' situation */
420 if ((pos = pathstr.find_last_of (':')) != string::npos) {
422 string shorter = pathstr.substr (0, pos);
424 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
425 chan = atoi (pathstr.substr (pos+1));
430 found_path = pathstr;
432 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
434 /* file does not exist or we cannot read it */
437 error << string_compose(
438 _("Filesource: cannot find required file (%1): %2"),
439 path, strerror (errno)) << endmsg;
443 if (errno != ENOENT) {
444 error << string_compose(
445 _("Filesource: cannot check for existing file (%1): %2"),
446 path, strerror (errno)) << endmsg;
466 FileSource::set_source_name (const string& newname, bool destructive)
468 Glib::Mutex::Lock lm (_lock);
469 string oldpath = _path;
470 string newpath = _session.change_source_path_by_name (oldpath, _name, newname, destructive);
472 if (newpath.empty()) {
473 error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
477 // Test whether newpath exists, if yes notify the user but continue.
478 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
479 error << string_compose (_("Programming error! %1 tried to rename a file over another file! It's safe to continue working, but please report this to the developers."), PROGRAM_NAME) << endmsg;
483 if (::rename (oldpath.c_str(), newpath.c_str()) != 0) {
484 error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg;
488 _name = Glib::path_get_basename (newpath);
495 FileSource::mark_immutable ()
497 /* destructive sources stay writable, and their other flags don't change. */
498 if (!(_flags & Destructive)) {
499 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
504 FileSource::mark_nonremovable ()
506 _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
510 FileSource::set_within_session_from_path (const std::string& path)
512 _within_session = _session.path_is_within_session (path);
516 FileSource::unstubify ()
518 string::size_type pos = _path.find (stub_dir_name);
520 if (pos == string::npos || (_flags & Destructive)) {
526 v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
527 v.push_back (Glib::path_get_basename(_path));
529 string newpath = Glib::build_filename (v);
531 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
532 error << string_compose (_("rename from %1 to %2 failed: %3)"), _path, newpath, strerror (errno)) << endmsg;
542 FileSource::set_path (const std::string& newpath)
548 FileSource::inc_use_count ()
550 Source::inc_use_count ();