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/session.h"
43 #include "ardour/session_directory.h"
44 #include "ardour/source_factory.h"
45 #include "ardour/filename_extensions.h"
50 using namespace ARDOUR;
54 static const std::string PATH_SEP = "/"; // I don't do windows
56 map<DataType, ustring> FileSource::search_paths;
58 FileSource::FileSource (Session& session, DataType type,
59 const ustring& path, bool embedded, Source::Flag flag)
60 : Source(session, type, path, flag)
64 , _is_embedded(embedded)
68 FileSource::FileSource (Session& session, const XMLNode& node, bool must_exist)
69 : Source(session, node)
73 _is_embedded = (_path.find(PATH_SEP) != string::npos);
77 FileSource::removable () const
79 return (_flags & Removable)
80 && ( (_flags & RemoveAtDestroy)
81 || ((_flags & RemovableIfEmpty) && length(timeline_position()) == 0));
85 FileSource::init (const ustring& pathstr, bool must_exist)
87 _timeline_position = 0;
89 if (!find (_type, pathstr, must_exist, _file_is_new, _channel)) {
90 throw MissingSource ();
93 if (_file_is_new && must_exist) {
101 FileSource::set_state (const XMLNode& node)
103 const XMLProperty* prop;
105 if ((prop = node.property (X_("channel"))) != 0) {
106 _channel = atoi (prop->value());
111 _is_embedded = (_name.find(PATH_SEP) == string::npos);
117 FileSource::mark_take (const ustring& id)
125 FileSource::move_to_trash (const ustring& trash_dir_name)
128 cerr << "tried to move an embedded region to trash" << endl;
136 /* don't move the file across filesystems, just stick it in the
137 trash_dir_name directory on whichever filesystem it was already on
141 newpath = Glib::path_get_dirname (_path);
142 newpath = Glib::path_get_dirname (newpath);
144 newpath += string(PATH_SEP) + trash_dir_name + PATH_SEP;
145 newpath += Glib::path_get_basename (_path);
147 /* the new path already exists, try versioning */
148 if (access (newpath.c_str(), F_OK) == 0) {
149 char buf[PATH_MAX+1];
153 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
156 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
157 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
161 if (version == 999) {
162 PBD::error << string_compose (
163 _("there are already 1000 files with names like %1; versioning discontinued"),
170 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
171 PBD::error << string_compose (
172 _("cannot rename file source from %1 to %2 (%3)"),
173 _path, newpath, strerror (errno)) << endmsg;
177 if (move_dependents_to_trash() != 0) {
178 /* try to back out */
179 rename (newpath.c_str(), _path.c_str());
185 /* file can not be removed twice, since the operation is not idempotent */
186 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
191 /** Find the actual source file based on \a path.
193 * If the source is embedded, \a path should be a filename (no slashes).
194 * If the source is external, \a path should be a full path.
195 * In either case, _path is set to the complete absolute path of the source file.
196 * \return true iff the file was found.
199 FileSource::find (DataType type, const ustring& path, bool must_exist, bool& isnew, uint16_t& chan)
201 Glib::ustring search_path = search_paths[type];
203 ustring pathstr = path;
204 ustring::size_type pos;
209 if (pathstr[0] != '/') {
211 /* non-absolute pathname: find pathstr in search path */
213 vector<ustring> dirs;
218 if (search_path.length() == 0) {
219 error << _("FileSource: search path not set") << endmsg;
223 split (search_path, dirs, ':');
227 for (vector<ustring>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
229 if (fullpath[fullpath.length()-1] != '/') {
235 /* i (paul) made a nasty design error by using ':' as a special character in
236 Ardour 0.99 .. this hack tries to make things sort of work.
239 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
241 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
243 /* its a real file, no problem */
252 /* might be an older session using file:channel syntax. see if the version
253 without the :suffix exists
256 ustring shorter = pathstr.substr (0, pos);
259 if (fullpath[fullpath.length()-1] != '/') {
265 if (Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
266 chan = atoi (pathstr.substr (pos+1));
274 /* new derived file (e.g. for timefx) being created in a newer session */
281 if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
290 error << string_compose (
291 _("FileSource: \"%1\" is ambigous when searching %2\n\t"),
292 pathstr, search_path) << endmsg;
295 } else if (cnt == 0) {
298 error << string_compose(
299 _("Filesource: cannot find required file (%1): while searching %2"),
300 pathstr, search_path) << endmsg;
307 /* Current find() is unable to parse relative path names to yet non-existant
308 sources. QuickFix(tm) */
309 if (keeppath == "") {
311 error << "FileSource::find(), keeppath = \"\", but the file must exist" << endl;
323 /* external files and/or very very old style sessions include full paths */
325 /* ugh, handle ':' situation */
327 if ((pos = pathstr.find_last_of (':')) != ustring::npos) {
329 ustring shorter = pathstr.substr (0, pos);
331 if (Glib::file_test (shorter, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
332 chan = atoi (pathstr.substr (pos+1));
339 if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
341 /* file does not exist or we cannot read it */
344 error << string_compose(
345 _("Filesource: cannot find required file (%1): %2"),
346 _path, strerror (errno)) << endmsg;
350 if (errno != ENOENT) {
351 error << string_compose(
352 _("Filesource: cannot check for existing file (%1): %2"),
353 _path, strerror (errno)) << endmsg;
369 _name = Glib::path_get_basename (_name);
377 FileSource::set_source_name (const ustring& newname, bool destructive)
379 Glib::Mutex::Lock lm (_lock);
380 ustring oldpath = _path;
381 ustring newpath = Session::change_source_path_by_name (oldpath, _name, newname, destructive);
383 if (newpath.empty()) {
384 error << string_compose (_("programming error: %1"), "cannot generate a changed file path") << endmsg;
388 // Test whether newpath exists, if yes notify the user but continue.
389 if (access(newpath.c_str(),F_OK) == 0) {
390 error << _("Programming error! Ardour tried to rename a file over another file! It's safe to continue working, but please report this to the developers.") << endmsg;
394 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
395 error << string_compose (_("cannot rename audio file %1 to %2"), _name, newpath) << endmsg;
399 _name = Glib::path_get_basename (newpath);
406 FileSource::set_search_path (DataType type, const ustring& p)
408 search_paths[type] = p;
412 FileSource::mark_immutable ()
414 /* destructive sources stay writable, and their other flags don't change. */
415 if (!(_flags & Destructive)) {
416 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));