From 2d28ebc9368b9c802108531b8ea9cbf8053786f5 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Wed, 3 Jun 2015 13:47:58 -0400 Subject: check size of peak file before attempting to use it. Rebuild from scratch if necessary. --- libs/ardour/audiosource.cc | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'libs/ardour/audiosource.cc') diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc index f9dcaf23c5..beb53a79bb 100644 --- a/libs/ardour/audiosource.cc +++ b/libs/ardour/audiosource.cc @@ -356,6 +356,33 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t framecnt_t read_npeaks = npeaks; framecnt_t zero_fill = 0; + GStatBuf statbuf; + + expected_peaks = (cnt / (double) samples_per_file_peak); + if (g_stat (peakpath.c_str(), &statbuf) != 0) { + error << string_compose (_("Cannot open peakfile @ %1 for size check (%2)"), peakpath, strerror (errno)) << endmsg; + return -1; + } + + /* check actual size of the peakfile is at least large enough for all + * the data in the audio file. if it is too short, assume that a crash + * or other error truncated it, and rebuild it from scratch. + */ + + const off_t expected_file_size = (_length / (double) samples_per_file_peak) * sizeof (PeakData); + + if (statbuf.st_size < expected_file_size) { + warning << string_compose (_("peak file %1 is truncated from %2 to %3"), peakpath, expected_file_size, statbuf.st_size) << endmsg; + const_cast(this)->build_peaks_from_scratch (); + if (g_stat (peakpath.c_str(), &statbuf) != 0) { + error << string_compose (_("Cannot open peakfile @ %1 for size check (%2) after rebuild"), peakpath, strerror (errno)) << endmsg; + } + if (statbuf.st_size < expected_file_size) { + fatal << "peak file is still truncated after rebuild" << endmsg; + /*NOTREACHED*/ + } + } + ScopedFileDescriptor sfd (::open (peakpath.c_str(), O_RDONLY)); if (sfd < 0) { @@ -363,9 +390,9 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, framecnt_t npeaks, framepos_t return -1; } - expected_peaks = (cnt / (double) samples_per_file_peak); scale = npeaks/expected_peaks; + DEBUG_TRACE (DEBUG::Peaks, string_compose (" ======>RP: npeaks = %1 start = %2 cnt = %3 len = %4 samples_per_visual_peak = %5 expected was %6 ... scale = %7 PD ptr = %8\n" , npeaks, start, cnt, _length, samples_per_visual_peak, expected_peaks, scale, peaks)); -- cgit v1.2.3