summaryrefslogtreecommitdiff
path: root/libs/ardour/file_source.cc
blob: 8b503d4894d4d207c02fa6db42d5ee718f605a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
    Copyright (C) 2006-2009 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <vector>

#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <glib.h>
#include <glib/gstdio.h>

#include "pbd/convert.h"
#include "pbd/basename.h"
#include "pbd/stl_delete.h"
#include "pbd/strsplit.h"
#include "pbd/shortpath.h"
#include "pbd/enumwriter.h"
#include "pbd/file_utils.h"

#include <glibmm/miscutils.h>
#include <glibmm/fileutils.h>
#include <glibmm/threads.h>

#include "ardour/data_type.h"
#include "ardour/file_source.h"
#include "ardour/session.h"
#include "ardour/source.h"
#include "ardour/utils.h"

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;
using namespace Glib;

PBD::Signal2<int,std::string,std::vector<std::string> > 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 (!origin.empty()) // if origin is left unspecified (empty string) then file must exist
	, _channel (0)
	, _origin (origin)
{
	set_within_session_from_path (path);
}

FileSource::FileSource (Session& session, const XMLNode& node, bool /*must_exist*/)
	: Source (session, node)
	, _file_is_new (false)
	, _channel (0)
{
	/* this setting of _path is temporary - we expect derived classes
	   to call ::init() which will actually locate the file
	   and reset _path and _within_session correctly.
	*/

	_path = _name;
	_within_session = true;
}

FileSource::~FileSource()
{
}

void
FileSource::existence_check ()
{
        if (Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) {
		prevent_deletion ();
	}
}

void
FileSource::prevent_deletion ()
{
	if (!(_flags & Destructive)) {
		mark_immutable ();
	} else {
		_flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
	}
}

bool
FileSource::removable () const
{
        bool r = ((_flags & Removable)
                  && ((_flags & RemoveAtDestroy) ||
                      ((_flags & RemovableIfEmpty) && empty())));

        return r;
}

int
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);
		}
	}

	set_within_session_from_path (_path);

        _name = Glib::path_get_basename (_path);

	if (must_exist) {
		if (!Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) {
			throw MissingSource (pathstr, _type);
		}
	}

	return 0;
}

int
FileSource::set_state (const XMLNode& node, int /*version*/)
{
	XMLProperty const * prop;

	if ((prop = node.property (X_("channel"))) != 0) {
		_channel = atoi (prop->value());
	} else {
		_channel = 0;
	}

	if ((prop = node.property (X_("origin"))) != 0) {
		_origin = prop->value();
	}

	return 0;
}

void
FileSource::mark_take (const string& id)
{
	if (writable ()) {
		_take_id = id;
	}
}

int
FileSource::move_to_trash (const string& trash_dir_name)
{
	if (!within_session() || !writable()) {
		return -1;
	}

	/* don't move the file across filesystems, just stick it in the
	   trash_dir_name directory on whichever filesystem it was already on
	*/

        vector<string> v;
	v.push_back (Glib::path_get_dirname (Glib::path_get_dirname (_path)));
        v.push_back (trash_dir_name);
	v.push_back (Glib::path_get_basename (_path));

	string newpath = Glib::build_filename (v);

	/* the new path already exists, try versioning */

	if (Glib::file_test (newpath.c_str(), Glib::FILE_TEST_EXISTS)) {
		char buf[PATH_MAX+1];
		int version = 1;
		string newpath_v;

		snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
		newpath_v = buf;

		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;
		}

		if (version == 999) {
			PBD::error << string_compose (
					_("there are already 1000 files with names like %1; versioning discontinued"),
					newpath) << endmsg;
		} else {
			newpath = newpath_v;
		}
	}

	if (::g_rename (_path.c_str(), newpath.c_str()) != 0) {
		PBD::error << string_compose (
				_("cannot rename file source from %1 to %2 (%3)"),
				_path, newpath, g_strerror (errno)) << endmsg;
		return -1;
	}

	if (move_dependents_to_trash() != 0) {
		/* try to back out */
		::g_rename (newpath.c_str(), _path.c_str());
		return -1;
	}

	_path = newpath;

	/* file can not be removed twice, since the operation is not idempotent */
	_flags = Flag (_flags & ~(RemoveAtDestroy|Removable|RemovableIfEmpty));

	return 0;
}

/** Find the actual source file based on \a filename.
 *
 * If the source is within the session tree, \a path should be a simple filename (no slashes).
 * If the source is external, \a path should be a full path.
 * In either case, found_path is set to the complete absolute path of the source file.
 * \return true if the file was found.
 */
bool
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<string> hits;
                string fullpath;
		std::vector<std::string> dirs = s.source_search_path (type);

                if (dirs.size() == 0) {
                        error << _("FileSource: search path not set") << endmsg;
                        goto out;
                }

                for (vector<string>::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.

		   I suppose this is not necessary on Windows.
		*/

		vector<string> de_duped_hits;

		for (vector<string>::iterator i = hits.begin(); i != hits.end(); ++i) {

			vector<string>::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 = de_duped_hits[which];
                        }

                } else if (de_duped_hits.size() == 0) {

			/* no match: error */

                        if (must_exist) {
				/* do not generate an error here, leave that to
				   whoever deals with the false return value.
				*/
                                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.empty()) {
		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)
{
	string pathstr = path;
	string::size_type pos;
	bool ret = false;

	isnew = false;

	if (!Glib::path_is_absolute (pathstr)) {

		/* non-absolute pathname: find pathstr in search path */

		vector<string> dirs = s.source_search_path (type);

		int cnt;
		string fullpath;
		string keeppath;

		if (dirs.size() == 0) {
			error << _("FileSource: search path not set") << endmsg;
			goto out;
		}

		cnt = 0;

		for (vector<string>::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
			   Ardour 0.99 .. this hack tries to make things sort of work.
			*/

			if ((pos = pathstr.find_last_of (':')) != string::npos) {

				if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {

					/* its a real file, no problem */

					keeppath = fullpath;
					++cnt;

				} else {

					if (must_exist) {

						/* might be an older session using file:channel syntax. see if the version
						   without the :suffix exists
						 */

						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));
							pathstr = shorter;
							keeppath = fullpath;
							++cnt;
						}

					} else {

						/* new derived file (e.g. for timefx) being created in a newer session */

					}
				}

			} else {

				if (Glib::file_test (fullpath, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
					keeppath = fullpath;
					++cnt;
				}
			}
		}

		if (cnt > 1) {

			error << string_compose (
					_("FileSource: \"%1\" is ambiguous when searching\n\t"), pathstr) << endmsg;
			goto out;

		} else if (cnt == 0) {

			if (must_exist) {
				error << string_compose(
						_("Filesource: cannot find required file (%1)"), pathstr) << endmsg;
				goto out;
			} else {
				isnew = true;
			}
		}

		/* 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 = pathstr;
			}
		}

		found_path = keeppath;

		ret = true;

	} else {

		/* external files and/or very very old style sessions include full paths */

		/* ugh, handle ':' situation */

		if ((pos = pathstr.find_last_of (':')) != string::npos) {

			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));
				pathstr = shorter;
			}
		}

		found_path = pathstr;

		if (!Glib::file_test (pathstr, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {

			/* file does not exist or we cannot read it */

			if (must_exist) {
				error << string_compose(
						_("Filesource: cannot find required file (%1): %2"),
						path, g_strerror (errno)) << endmsg;
				goto out;
			}

#ifndef PLATFORM_WINDOWS
			if (errno != ENOENT) {
				error << string_compose(
						_("Filesource: cannot check for existing file (%1): %2"),
						path, g_strerror (errno)) << endmsg;
				goto out;
			}
#endif
			/* a new file */
			isnew = true;
			ret = true;

		} else {

			/* already exists */
			ret = true;
		}
	}

out:
	return ret;
}

void
FileSource::mark_immutable ()
{
	/* destructive sources stay writable, and their other flags don't change.  */
	if (!(_flags & Destructive)) {
		_flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
	}
}

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 ()
{
        _flags = Flag (_flags & ~(Removable|RemovableIfEmpty|RemoveAtDestroy));
}

void
FileSource::set_within_session_from_path (const std::string& path)
{
	_within_session = _session.path_is_within_session (path);
}

void
FileSource::set_path (const std::string& newpath)
{
	close ();
        _path = newpath;
	set_within_session_from_path (newpath);
	if (_within_session) {
		_origin = Glib::path_get_basename (newpath);
	} else {
		_origin = newpath;
	}
}


void
FileSource::replace_file (const std::string& newpath)
{
	close ();
	_path = newpath;
	_name = Glib::path_get_basename (newpath);
}

void
FileSource::inc_use_count ()
{
        Source::inc_use_count ();
}

bool
FileSource::is_stub () const
{
	if (!empty()) {
		return false;
	}

	if (!removable()) {
		return false;
	}

	if (Glib::file_test (_path, Glib::FILE_TEST_EXISTS)) {
		return false;
	}

	return true;
}

int
FileSource::rename (const string& newpath)
{
	Glib::Threads::Mutex::Lock lm (_lock);
	string oldpath = _path;

	// Test whether newpath exists, if yes notify the user but continue.
	if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
		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 (Glib::file_test (oldpath.c_str(), Glib::FILE_TEST_EXISTS)) {
		/* rename only needed if file exists on disk */
		if (::g_rename (oldpath.c_str(), newpath.c_str()) != 0) {
			error << string_compose (_("cannot rename file %1 to %2 (%3)"), oldpath, newpath, g_strerror(errno)) << endmsg;
			return -1;
		}
	}

	_name = Glib::path_get_basename (newpath);
	_path = newpath;

	return 0;
}