X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Ffile_source.cc;h=c7b03cbdb756fe8f989103fe7cd12a121e68f6d2;hb=9cbe231b920d92a2e44e08962a3ed6870b962f34;hp=5522031d691d6438db88fc9f513adc311bca9635;hpb=5e431d1d5806e8f617b9bfa10c5c8766e0b1a4ce;p=ardour.git diff --git a/libs/ardour/file_source.cc b/libs/ardour/file_source.cc index 5522031d69..c7b03cbdb7 100644 --- a/libs/ardour/file_source.cc +++ b/libs/ardour/file_source.cc @@ -28,22 +28,21 @@ #include "pbd/convert.h" #include "pbd/basename.h" -#include "pbd/mountpoint.h" #include "pbd/stl_delete.h" #include "pbd/strsplit.h" #include "pbd/shortpath.h" #include "pbd/enumwriter.h" +#include "pbd/file_utils.h" #include #include -#include +#include +#include "ardour/data_type.h" #include "ardour/file_source.h" -#include "ardour/directory_names.h" #include "ardour/session.h" -#include "ardour/session_directory.h" -#include "ardour/source_factory.h" -#include "ardour/filename_extensions.h" +#include "ardour/source.h" +#include "ardour/utils.h" #include "i18n.h" @@ -52,12 +51,12 @@ using namespace ARDOUR; using namespace PBD; using namespace Glib; -PBD::Signal3 > FileSource::AmbiguousFileName; +PBD::Signal2 > FileSource::AmbiguousFileName; FileSource::FileSource (Session& session, DataType type, const string& path, const string& origin, Source::Flag flag) : Source(session, type, path, flag) - , _path(path) - , _file_is_new(true) + , _path (path) + , _file_is_new (!origin.empty()) // origin empty => new file VS. origin !empty => new file , _channel (0) , _origin (origin) , _open (false) @@ -68,7 +67,7 @@ FileSource::FileSource (Session& session, DataType type, const string& path, con } FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/) - : Source(session, node) + : Source (session, node) , _file_is_new (false) { /* this setting of _path is temporary - we expect derived classes @@ -87,7 +86,7 @@ FileSource::prevent_deletion () { /* if this file already exists, it cannot be removed, ever */ - + if (Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) { if (!(_flags & Destructive)) { mark_immutable (); @@ -101,9 +100,9 @@ bool FileSource::removable () const { bool r = ((_flags & Removable) - && ((_flags & RemoveAtDestroy) || + && ((_flags & RemoveAtDestroy) || ((_flags & RemovableIfEmpty) && empty() == 0))); - + return r; } @@ -112,26 +111,24 @@ FileSource::init (const string& pathstr, bool must_exist) { _timeline_position = 0; - if (Stateful::loading_state_version < 3000) { - if (!find_2X (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) { - throw MissingSource (pathstr, _type); - } - } else { - if (!find (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) { - throw MissingSource (pathstr, _type); - } - } + if (Stateful::loading_state_version < 3000) { + if (!find_2X (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) { + throw MissingSource (pathstr, _type); + } + } else { + if (!find (_session, _type, pathstr, must_exist, _file_is_new, _channel, _path)) { + throw MissingSource (pathstr, _type); + } + } set_within_session_from_path (_path); - if (!within_session()) { - _session.ensure_search_path_includes (Glib::path_get_dirname (_path), _type); - } - _name = Glib::path_get_basename (_path); - if (_file_is_new && must_exist) { - return -1; + if (must_exist) { + if (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) { + throw MissingSource (pathstr, _type); + } } return 0; @@ -191,7 +188,7 @@ FileSource::move_to_trash (const string& trash_dir_name) snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version); newpath_v = buf; - while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) { + while (Glib::file_test (newpath_v, Glib::FILE_TEST_EXISTS) && version < 999) { snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version); newpath_v = buf; } @@ -235,69 +232,98 @@ FileSource::move_to_trash (const string& trash_dir_name) */ bool FileSource::find (Session& s, DataType type, const string& path, bool must_exist, - bool& isnew, uint16_t& chan, string& found_path) + bool& isnew, uint16_t& /* chan */, string& found_path) { bool ret = false; string keeppath; isnew = false; - + if (!Glib::path_is_absolute (path)) { - vector dirs; vector hits; - int cnt; string fullpath; + std::vector dirs = s.source_search_path (type); - string search_path = s.source_search_path (type); - - if (search_path.length() == 0) { + if (dirs.size() == 0) { error << _("FileSource: search path not set") << endmsg; goto out; } - split (search_path, dirs, ':'); - - cnt = 0; hits.clear (); - + for (vector::iterator i = dirs.begin(); i != dirs.end(); ++i) { - + fullpath = Glib::build_filename (*i, path); - + if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) { keeppath = fullpath; hits.push_back (fullpath); - ++cnt; } } - - if (cnt > 1) { - - int which = FileSource::AmbiguousFileName (path, search_path, hits).get_value_or (-1); - + + /* Remove duplicate inodes from the list of ambiguous files, since if there are symlinks + in the session path it is possible to arrive at the same file via more than one path. + + I suppose this is not necessary on Windows. + */ + + vector de_duped_hits; + + for (vector::iterator i = hits.begin(); i != hits.end(); ++i) { + + vector::iterator j = i; + ++j; + + while (j != hits.end()) { + if (PBD::equivalent_paths (*i, *j)) { + /* *i and *j are the same file; break out of the loop early */ + break; + } + + ++j; + } + + if (j == hits.end ()) { + de_duped_hits.push_back (*i); + } + } + + if (de_duped_hits.size() > 1) { + + /* more than one match: ask the user */ + + int which = FileSource::AmbiguousFileName (path, de_duped_hits).get_value_or (-1); + if (which < 0) { goto out; } else { - keeppath = hits[which]; + keeppath = de_duped_hits[which]; } - - } else if (cnt == 0) { - + + } else if (de_duped_hits.size() == 0) { + + /* no match: error */ + if (must_exist) { error << string_compose( - _("Filesource: cannot find required file (%1): while searching %2"), - path, search_path) << endmsg; + _("Filesource: cannot find required file (%1)"), path) << endmsg; goto out; } else { isnew = true; } - } + } else { + + /* only one match: happy days */ + + keeppath = de_duped_hits[0]; + } + } else { keeppath = path; } - + /* Current find() is unable to parse relative path names to yet non-existant - sources. QuickFix(tm) + sources. QuickFix(tm) */ if (keeppath == "") { if (must_exist) { @@ -306,11 +332,11 @@ FileSource::find (Session& s, DataType type, const string& path, bool must_exist keeppath = path; } } - + found_path = keeppath; - + ret = true; - + out: return ret; } @@ -326,8 +352,6 @@ bool FileSource::find_2X (Session& s, DataType type, const string& path, bool must_exist, bool& isnew, uint16_t& chan, string& found_path) { - string search_path = s.source_search_path (type); - string pathstr = path; string::size_type pos; bool ret = false; @@ -338,22 +362,21 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex /* non-absolute pathname: find pathstr in search path */ - vector dirs; + vector dirs = s.source_search_path (type); + int cnt; string fullpath; string keeppath; - if (search_path.length() == 0) { + if (dirs.size() == 0) { error << _("FileSource: search path not set") << endmsg; goto out; } - split (search_path, dirs, ':'); - cnt = 0; for (vector::iterator i = dirs.begin(); i != dirs.end(); ++i) { - + fullpath = Glib::build_filename (*i, pathstr); /* i (paul) made a nasty design error by using ':' as a special character in @@ -406,16 +429,15 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex if (cnt > 1) { error << string_compose ( - _("FileSource: \"%1\" is ambigous when searching %2\n\t"), - pathstr, search_path) << endmsg; + _("FileSource: \"%1\" is ambigous when searching\n\t"), pathstr) << endmsg; goto out; } else if (cnt == 0) { if (must_exist) { error << string_compose( - _("Filesource: cannot find required file (%1): while searching %2"), - pathstr, search_path) << endmsg; + _("Filesource: cannot find required file (%1): while searching") + , pathstr) << endmsg; goto out; } else { isnew = true; @@ -465,13 +487,14 @@ FileSource::find_2X (Session& s, DataType type, const string& path, bool must_ex goto out; } +#ifndef WIN32 if (errno != ENOENT) { error << string_compose( _("Filesource: cannot check for existing file (%1): %2"), path, strerror (errno)) << endmsg; goto out; } - +#endif /* a new file */ isnew = true; ret = true; @@ -490,7 +513,7 @@ out: int FileSource::set_source_name (const string& newname, bool destructive) { - Glib::Mutex::Lock lm (_lock); + Glib::Threads::Mutex::Lock lm (_lock); string oldpath = _path; string newpath = _session.change_source_path_by_name (oldpath, _name, newname, destructive); @@ -504,7 +527,7 @@ FileSource::set_source_name (const string& newname, bool destructive) 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; return -1; } - + if (::rename (oldpath.c_str(), newpath.c_str()) != 0) { error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, strerror(errno)) << endmsg; return -1; @@ -525,6 +548,15 @@ FileSource::mark_immutable () } } +void +FileSource::mark_immutable_except_write () +{ + /* destructive sources stay writable, and their other flags don't change. */ + if (!(_flags & Destructive)) { + _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename)); + } +} + void FileSource::mark_nonremovable () { @@ -543,9 +575,9 @@ FileSource::set_path (const std::string& newpath) _path = newpath; } -void +void FileSource::inc_use_count () { Source::inc_use_count (); } - +