X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Ffile_source.cc;h=e35ee81364ce17afd9353b5137d16fa1ec633ca8;hb=dd577004459b7810daae2b4ad6741e3a2b858565;hp=d0eb64b8aafdac4dd9f13554dfc5334a56d0348f;hpb=7ab8dbc2fe679d75821ca585dd7c2663f640116e;p=ardour.git diff --git a/libs/ardour/file_source.cc b/libs/ardour/file_source.cc index d0eb64b8aa..e35ee81364 100644 --- a/libs/ardour/file_source.cc +++ b/libs/ardour/file_source.cc @@ -52,19 +52,23 @@ using namespace ARDOUR; using namespace PBD; using namespace Glib; -map FileSource::search_paths; +PBD::Signal3 > FileSource::AmbiguousFileName; -FileSource::FileSource (Session& session, DataType type, const ustring& path, Source::Flag flag) +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) , _channel (0) + , _origin (origin) + , _open (false) { set_within_session_from_path (path); + + prevent_deletion (); } 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 @@ -74,35 +78,57 @@ FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist _path = _name; _within_session = true; + + prevent_deletion (); +} + +void +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 (); + } else { + _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy)); + } + } } bool FileSource::removable () const { - bool r = (_path.find (stub_dir_name) != string::npos) || - ((_flags & Removable) - && ((_flags & RemoveAtDestroy) || - ((_flags & RemovableIfEmpty) && empty() == 0))); - - cerr << "is " << _path << " removable ? " << r << endl; + bool r = ((_flags & Removable) + && ((_flags & RemoveAtDestroy) || + ((_flags & RemovableIfEmpty) && empty() == 0))); return r; } int -FileSource::init (const ustring& pathstr, bool must_exist) +FileSource::init (const string& pathstr, bool must_exist) { _timeline_position = 0; - if (!find (_type, pathstr, must_exist, _file_is_new, _channel, _path)) { - throw MissingSource (); - } + 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 (pathstr); + set_within_session_from_path (_path); - if (_within_session) { - _name = Glib::path_get_basename (_name); - } + 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; @@ -122,11 +148,15 @@ FileSource::set_state (const XMLNode& node, int /*version*/) _channel = 0; } + if ((prop = node.property (X_("origin"))) != 0) { + _origin = prop->value(); + } + return 0; } void -FileSource::mark_take (const ustring& id) +FileSource::mark_take (const string& id) { if (writable ()) { _take_id = id; @@ -134,7 +164,7 @@ FileSource::mark_take (const ustring& id) } int -FileSource::move_to_trash (const ustring& trash_dir_name) +FileSource::move_to_trash (const string& trash_dir_name) { if (!within_session() || !writable()) { return -1; @@ -156,12 +186,12 @@ FileSource::move_to_trash (const ustring& trash_dir_name) if (Glib::file_test (newpath.c_str(), Glib::FILE_TEST_EXISTS)) { char buf[PATH_MAX+1]; int version = 1; - ustring newpath_v; + string newpath_v; 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; } @@ -204,25 +234,152 @@ FileSource::move_to_trash (const ustring& trash_dir_name) * \return true iff the file was found. */ bool -FileSource::find (DataType type, const ustring& path, bool must_exist, - bool& isnew, uint16_t& chan, ustring& found_path) +FileSource::find (Session& s, DataType type, const string& path, bool must_exist, + 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; + string fullpath; + + string search_path = s.source_search_path (type); + + if (search_path.length() == 0) { + error << _("FileSource: search path not set") << endmsg; + goto out; + } + + split (search_path, dirs, ':'); + + 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); + } + } + + /* 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. + */ + + vector de_duped_hits; + + for (vector::iterator i = hits.begin(); i != hits.end(); ++i) { + + vector::iterator j = i; + ++j; + + while (j != hits.end()) { + + struct stat bufA; + int const rA = stat (i->c_str(), &bufA); + struct stat bufB; + int const rB = stat (j->c_str(), &bufB); + + if (rA == 0 && rB == 0 && bufA.st_ino == bufB.st_ino) { + /* *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, search_path, de_duped_hits).get_value_or (-1); + + if (which < 0) { + goto out; + } else { + keeppath = de_duped_hits[which]; + } + + } 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; + 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) + */ + if (keeppath == "") { + if (must_exist) { + error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl; + } else { + keeppath = path; + } + } + + found_path = keeppath; + + ret = true; + + out: + return ret; +} + +/** Find the actual source file based on \a filename. + * + * If the source is within the session tree, \a filename should be a simple filename (no slashes). + * If the source is external, \a filename should be a full path. + * In either case, found_path is set to the complete absolute path of the source file. + * \return true iff the file was found. + */ +bool +FileSource::find_2X (Session& s, DataType type, const string& path, bool must_exist, + bool& isnew, uint16_t& chan, string& found_path) { - Glib::ustring search_path = search_paths[type]; + string search_path = s.source_search_path (type); - ustring pathstr = path; - ustring::size_type pos; + string pathstr = path; + string::size_type pos; bool ret = false; isnew = false; - if (pathstr[0] != '/') { + if (!Glib::path_is_absolute (pathstr)) { /* non-absolute pathname: find pathstr in search path */ - vector dirs; + vector dirs; int cnt; - ustring fullpath; - ustring keeppath; + string fullpath; + string keeppath; if (search_path.length() == 0) { error << _("FileSource: search path not set") << endmsg; @@ -233,19 +390,15 @@ FileSource::find (DataType type, const ustring& path, bool must_exist, cnt = 0; - for (vector::iterator i = dirs.begin(); i != dirs.end(); ++i) { - fullpath = *i; - if (fullpath[fullpath.length()-1] != '/') { - fullpath += '/'; - } + for (vector::iterator i = dirs.begin(); i != dirs.end(); ++i) { - fullpath += pathstr; + fullpath = Glib::build_filename (*i, pathstr); /* i (paul) made a nasty design error by using ':' as a special character in Ardour 0.99 .. this hack tries to make things sort of work. */ - if ((pos = pathstr.find_last_of (':')) != ustring::npos) { + if ((pos = pathstr.find_last_of (':')) != string::npos) { if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) { @@ -262,14 +415,8 @@ FileSource::find (DataType type, const ustring& path, bool must_exist, without the :suffix exists */ - ustring shorter = pathstr.substr (0, pos); - fullpath = *i; - - if (fullpath[fullpath.length()-1] != '/') { - fullpath += '/'; - } - - fullpath += shorter; + string shorter = pathstr.substr (0, pos); + fullpath = Glib::build_filename (*i, shorter); if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) { chan = atoi (pathstr.substr (pos+1)); @@ -333,9 +480,9 @@ FileSource::find (DataType type, const ustring& path, bool must_exist, /* ugh, handle ':' situation */ - if ((pos = pathstr.find_last_of (':')) != ustring::npos) { + if ((pos = pathstr.find_last_of (':')) != string::npos) { - ustring shorter = pathstr.substr (0, pos); + string shorter = pathstr.substr (0, pos); if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) { chan = atoi (pathstr.substr (pos+1)); @@ -379,11 +526,11 @@ out: } int -FileSource::set_source_name (const ustring& newname, bool destructive) +FileSource::set_source_name (const string& newname, bool destructive) { Glib::Mutex::Lock lm (_lock); - ustring oldpath = _path; - ustring newpath = Session::change_source_path_by_name (oldpath, _name, newname, destructive); + string oldpath = _path; + string newpath = _session.change_source_path_by_name (oldpath, _name, newname, destructive); if (newpath.empty()) { error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg; @@ -395,7 +542,7 @@ FileSource::set_source_name (const ustring& 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; @@ -407,12 +554,6 @@ FileSource::set_source_name (const ustring& newname, bool destructive) return 0; } -void -FileSource::set_search_path (DataType type, const ustring& p) -{ - search_paths[type] = p; -} - void FileSource::mark_immutable () { @@ -434,41 +575,15 @@ FileSource::set_within_session_from_path (const std::string& path) _within_session = _session.path_is_within_session (path); } -int -FileSource::unstubify () -{ - string::size_type pos = _path.find (stub_dir_name); - - if (pos == string::npos || (_flags & Destructive)) { - return 0; - } - - vector v; - - v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path))); - v.push_back (Glib::path_get_basename(_path)); - - string newpath = Glib::build_filename (v); - - if (::rename (_path.c_str(), newpath.c_str()) != 0) { - error << string_compose (_("rename from %1 to %2 failed: %3)"), _path, newpath, strerror (errno)) << endmsg; - return -1; - } - - set_path (newpath); - - return 0; -} - void FileSource::set_path (const std::string& newpath) { _path = newpath; } -void +void FileSource::inc_use_count () { Source::inc_use_count (); } - +