2 Copyright (C) 2006 Paul Davis
3 Written by Dave Robillard, 2006
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <pbd/mountpoint.h>
29 #include <pbd/pathscanner.h>
30 #include <pbd/stl_delete.h>
31 #include <pbd/strsplit.h>
33 #include <glibmm/miscutils.h>
35 #include <ardour/smf_source.h>
36 #include <ardour/session.h>
40 using namespace ARDOUR;
42 string SMFSource::_search_path;
44 /*sigc::signal<void,struct tm*, time_t> SMFSource::HeaderPositionOffsetChanged;
45 bool SMFSource::header_position_negative;
46 uint64_t SMFSource::header_position_offset;
49 SMFSource::SMFSource (std::string path, Flag flags)
50 : MidiSource (region_name_from_path(path)), _flags (flags)
52 /* constructor used for new internal-to-session files. file cannot exist */
54 if (init (path, false)) {
55 throw failed_constructor ();
58 assert(_name.find("/") == string::npos);
60 SourceCreated (this); /* EMIT SIGNAL */
63 SMFSource::SMFSource (const XMLNode& node)
64 : MidiSource (node), _flags (Flag (Writable|CanRename))
66 /* constructor used for existing internal-to-session files. file must exist */
68 if (set_state (node)) {
69 throw failed_constructor ();
72 if (init (_name, true)) {
73 throw failed_constructor ();
76 assert(_name.find("/") == string::npos);
78 SourceCreated (this); /* EMIT SIGNAL */
81 SMFSource::~SMFSource ()
84 unlink (_path.c_str());
89 SMFSource::removable () const
91 return (_flags & Removable) && ((_flags & RemoveAtDestroy) ||
92 ((_flags & RemovableIfEmpty) && is_empty (_path)));
96 SMFSource::init (string pathstr, bool must_exist)
98 //bool is_new = false;
101 if (!find (pathstr, must_exist, is_new)) {
102 cerr << "cannot find " << pathstr << " with me = " << must_exist << endl;
106 if (is_new && must_exist) {
111 // Yeah, we found it. Swear.
113 assert(_name.find("/") == string::npos);
118 SMFSource::update_header (jack_nframes_t when, struct tm&, time_t)
124 SMFSource::flush_header ()
130 SMFSource::read_unlocked (MidiBuffer& dst, jack_nframes_t start, jack_nframes_t cnt) const
137 SMFSource::write_unlocked (MidiBuffer& src, jack_nframes_t cnt)
139 ViewDataRangeReady (_length, cnt); /* EMIT SIGNAL */
145 SMFSource::get_state ()
147 XMLNode& root (MidiSource::get_state());
149 snprintf (buf, sizeof (buf), "0x%x", (int)_flags);
150 root.add_property ("flags", buf);
155 SMFSource::set_state (const XMLNode& node)
157 const XMLProperty* prop;
159 if (MidiSource::set_state (node)) {
163 if ((prop = node.property (X_("flags"))) != 0) {
166 sscanf (prop->value().c_str(), "0x%x", &ival);
167 _flags = Flag (ival);
175 assert(_name.find("/") == string::npos);
181 SMFSource::mark_for_remove ()
186 _flags = Flag (_flags | RemoveAtDestroy);
190 SMFSource::mark_streaming_write_completed ()
196 Glib::Mutex::Lock lm (_lock);
199 next_peak_clear_should_notify = true;
201 if (_peaks_built || pending_peak_builds.empty()) {
203 PeaksReady (); /* EMIT SIGNAL */
209 SMFSource::mark_take (string id)
217 SMFSource::move_to_trash (const string trash_dir_name)
225 /* don't move the file across filesystems, just
226 stick it in the `trash_dir_name' directory
227 on whichever filesystem it was already on.
230 newpath = Glib::path_get_dirname (_path);
231 newpath = Glib::path_get_dirname (newpath);
234 newpath += trash_dir_name;
236 newpath += Glib::path_get_basename (_path);
238 if (access (newpath.c_str(), F_OK) == 0) {
240 /* the new path already exists, try versioning */
242 char buf[PATH_MAX+1];
246 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
249 while (access (newpath_v.c_str(), F_OK) == 0 && version < 999) {
250 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
254 if (version == 999) {
255 PBD::error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
264 /* it doesn't exist, or we can't read it or something */
268 if (::rename (_path.c_str(), newpath.c_str()) != 0) {
269 PBD::error << string_compose (_("cannot rename audio file source from %1 to %2 (%3)"),
270 _path, newpath, strerror (errno))
275 if (::unlink (peakpath.c_str()) != 0) {
276 PBD::error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
277 peakpath, _path, strerror (errno))
279 /* try to back out */
280 rename (newpath.c_str(), _path.c_str());
287 /* file can not be removed twice, since the operation is not idempotent */
289 _flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));
294 // FIXME: Merge this with audiofilesource somehow (make a generic filesource?)
296 SMFSource::find (string pathstr, bool must_exist, bool& isnew)
298 string::size_type pos;
303 /* clean up PATH:CHANNEL notation so that we are looking for the correct path */
305 if ((pos = pathstr.find_last_of (':')) == string::npos) {
308 pathstr = pathstr.substr (0, pos);
311 if (pathstr[0] != '/') {
313 /* non-absolute pathname: find pathstr in search path */
320 if (_search_path.length() == 0) {
321 PBD::error << _("FileSource: search path not set") << endmsg;
325 split (_search_path, dirs, ':');
329 for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
332 if (fullpath[fullpath.length()-1] != '/') {
337 if (access (fullpath.c_str(), R_OK) == 0) {
345 PBD::error << string_compose (_("FileSource: \"%1\" is ambigous when searching %2\n\t"), pathstr, _search_path) << endmsg;
348 } else if (cnt == 0) {
351 PBD::error << string_compose(_("Filesource: cannot find required file (%1): while searching %2"), pathstr, _search_path) << endmsg;
364 /* external files and/or very very old style sessions include full paths */
367 _name = pathstr.substr (pathstr.find_last_of ('/') + 1);
369 if (access (_path.c_str(), R_OK) != 0) {
371 /* file does not exist or we cannot read it */
374 PBD::error << string_compose(_("Filesource: cannot find required file (%1): %2"), _path, strerror (errno)) << endmsg;
378 if (errno != ENOENT) {
379 PBD::error << string_compose(_("Filesource: cannot check for existing file (%1): %2"), _path, strerror (errno)) << endmsg;
401 SMFSource::set_search_path (string p)
408 SMFSource::set_allow_remove_if_empty (bool yn)
411 _allow_remove_if_empty = yn;
416 SMFSource::set_name (string newname, bool destructive)
418 //Glib::Mutex::Lock lm (_lock); FIXME
419 string oldpath = _path;
420 string newpath = Session::change_audio_path_by_name (oldpath, _name, newname, destructive);
422 if (newpath.empty()) {
423 PBD::error << string_compose (_("programming error: %1"), "cannot generate a changed audio path") << endmsg;
427 if (rename (oldpath.c_str(), newpath.c_str()) != 0) {
428 PBD::error << string_compose (_("cannot rename audio file for %1 to %2"), _name, newpath) << endmsg;
432 _name = Glib::path_get_basename (newpath);
435 return 0;//rename_peakfile (peak_path (_path));
439 SMFSource::is_empty (string path)