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 _session.ensure_search_path_includes (Glib::path_get_dirname (pathstr), _type);
111 _name = Glib::path_get_basename (pathstr);
113 if (_file_is_new && must_exist) {
121 FileSource::set_state (const XMLNode& node, int /*version*/)
123 const XMLProperty* prop;
125 if ((prop = node.property (X_("channel"))) != 0) {
126 _channel = atoi (prop->value());
135 FileSource::mark_take (const string& id)
143 FileSource::move_to_trash (const string& trash_dir_name)
145 if (!within_session() || !writable()) {
149 /* don't move the file across filesystems, just stick it in the
150 trash_dir_name directory on whichever filesystem it was already on
154 v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
155 v.push_back (trash_dir_name);
156 v.push_back (Glib::path_get_basename (_path));
158 string newpath = Glib::build_filename (v);
160 /* the new path already exists, try versioning */
162 if (Glib::file_test (newpath.c_str(), Glib::FILE_TEST_EXISTS)) {
163 char buf[PATH_MAX+1];
167 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
170 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
171 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
175 if (version == 999) {
176 PBD::error << string_compose (
177 _("there are already 1000 files with names like %1; versioning discontinued"),
184 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
185 PBD::error << string_compose (
186 _("cannot rename file source from %1 to %2 (%3)"),
187 _path, newpath, strerror (errno)) << endmsg;
191 if (move_dependents_to_trash() != 0) {
192 /* try to back out */
193 rename (newpath.c_str(), _path.c_str());
199 /* file can not be removed twice, since the operation is not idempotent */
200 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
205 /** Find the actual source file based on \a filename.
207 * If the source is within the session tree, \a filename should be a simple filename (no slashes).
208 * If the source is external, \a filename should be a full path.
209 * In either case, found_path is set to the complete absolute path of the source file.
210 * \return true iff the file was found.
213 FileSource::find (Session& s, DataType type, const string& path, bool must_exist,
214 bool& isnew, uint16_t& chan, string& found_path)
221 if (!Glib::path_is_absolute (path)) {
227 string search_path = s.source_search_path (type);
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 fullpath = Glib::build_filename (*i, path);
243 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
245 hits.push_back (fullpath);
252 int which = FileSource::AmbiguousFileName (path, search_path, hits).get_value_or (-1);
257 keeppath = hits[which];
260 } else if (cnt == 0) {
263 error << string_compose(
264 _("Filesource: cannot find required file (%1): while searching %2"),
265 path, search_path) << endmsg;
275 /* Current find() is unable to parse relative path names to yet non-existant
276 sources. QuickFix(tm)
278 if (keeppath == "") {
280 error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl;
286 found_path = keeppath;
294 /** Find the actual source file based on \a filename.
296 * If the source is within the session tree, \a filename should be a simple filename (no slashes).
297 * If the source is external, \a filename should be a full path.
298 * In either case, found_path is set to the complete absolute path of the source file.
299 * \return true iff the file was found.
302 FileSource::find_2X (Session& s, DataType type, const string& path, bool must_exist,
303 bool& isnew, uint16_t& chan, string& found_path)
305 string search_path = s.source_search_path (type);
307 string pathstr = path;
308 string::size_type pos;
313 if (!Glib::path_is_absolute (pathstr)) {
315 /* non-absolute pathname: find pathstr in search path */
322 if (search_path.length() == 0) {
323 error << _("FileSource: search path not set") << endmsg;
327 split (search_path, dirs, ':');
331 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
333 fullpath = Glib::build_filename (*i, pathstr);
335 /* i (paul) made a nasty design error by using ':' as a special character in
336 Ardour 0.99 .. this hack tries to make things sort of work.
339 if ((pos = pathstr.find_last_of (':')) != string::npos) {
341 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
343 /* its a real file, no problem */
352 /* might be an older session using file:channel syntax. see if the version
353 without the :suffix exists
356 string shorter = pathstr.substr (0, pos);
357 fullpath = Glib::build_filename (*i, shorter);
359 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
360 chan = atoi (pathstr.substr (pos+1));
368 /* new derived file (e.g. for timefx) being created in a newer session */
375 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
384 error << string_compose (
385 _("FileSource: \"%1\" is ambigous when searching %2\n\t"),
386 pathstr, search_path) << endmsg;
389 } else if (cnt == 0) {
392 error << string_compose(
393 _("Filesource: cannot find required file (%1): while searching %2"),
394 pathstr, search_path) << endmsg;
401 /* Current find() is unable to parse relative path names to yet non-existant
402 sources. QuickFix(tm) */
403 if (keeppath == "") {
405 error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl;
411 found_path = keeppath;
417 /* external files and/or very very old style sessions include full paths */
419 /* ugh, handle ':' situation */
421 if ((pos = pathstr.find_last_of (':')) != string::npos) {
423 string shorter = pathstr.substr (0, pos);
425 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
426 chan = atoi (pathstr.substr (pos+1));
431 found_path = pathstr;
433 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
435 /* file does not exist or we cannot read it */
438 error << string_compose(
439 _("Filesource: cannot find required file (%1): %2"),
440 path, strerror (errno)) << endmsg;
444 if (errno != ENOENT) {
445 error << string_compose(
446 _("Filesource: cannot check for existing file (%1): %2"),
447 path, strerror (errno)) << endmsg;
467 FileSource::set_source_name (const string& newname, bool destructive)
469 Glib::Mutex::Lock lm (_lock);
470 string oldpath = _path;
471 string newpath = _session.change_source_path_by_name (oldpath, _name, newname, destructive);
473 if (newpath.empty()) {
474 error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
478 // Test whether newpath exists, if yes notify the user but continue.
479 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
480 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;
484 if (::rename (oldpath.c_str(), newpath.c_str()) != 0) {
485 error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg;
489 _name = Glib::path_get_basename (newpath);
496 FileSource::mark_immutable ()
498 /* destructive sources stay writable, and their other flags don't change. */
499 if (!(_flags & Destructive)) {
500 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
505 FileSource::mark_nonremovable ()
507 _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
511 FileSource::set_within_session_from_path (const std::string& path)
513 _within_session = _session.path_is_within_session (path);
517 FileSource::unstubify ()
519 string::size_type pos = _path.find (stub_dir_name);
521 if (pos == string::npos || (_flags & Destructive)) {
527 v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
528 v.push_back (Glib::path_get_basename(_path));
530 string newpath = Glib::build_filename (v);
532 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
533 error << string_compose (_("rename from %1 to %2 failed: %3)"), _path, newpath, strerror (errno)) << endmsg;
543 FileSource::set_path (const std::string& newpath)
549 FileSource::inc_use_count ()
551 Source::inc_use_count ();