Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@537
|
7 This file copyright 2006-2009 Chris Cannam and QMUL.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "MatrixFile.h"
|
Chris@148
|
17 #include "base/TempDirectory.h"
|
Chris@150
|
18 #include "system/System.h"
|
Chris@148
|
19 #include "base/Profiler.h"
|
Chris@148
|
20 #include "base/Exceptions.h"
|
Chris@408
|
21 #include "base/Thread.h"
|
Chris@148
|
22
|
Chris@148
|
23 #include <sys/types.h>
|
Chris@148
|
24 #include <sys/stat.h>
|
Chris@148
|
25 #include <fcntl.h>
|
Chris@148
|
26 #include <unistd.h>
|
Chris@148
|
27
|
Chris@148
|
28 #include <iostream>
|
Chris@148
|
29
|
Chris@148
|
30 #include <cstdio>
|
Chris@148
|
31 #include <cassert>
|
Chris@148
|
32
|
Chris@405
|
33 #include <cstdlib>
|
Chris@405
|
34
|
Chris@148
|
35 #include <QFileInfo>
|
Chris@148
|
36 #include <QDir>
|
Chris@148
|
37
|
Chris@537
|
38 #define DEBUG_MATRIX_FILE 1
|
Chris@403
|
39 //#define DEBUG_MATRIX_FILE_READ_SET 1
|
Chris@148
|
40
|
Chris@148
|
41 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@153
|
42 #ifndef DEBUG_MATRIX_FILE
|
Chris@148
|
43 #define DEBUG_MATRIX_FILE 1
|
Chris@148
|
44 #endif
|
Chris@153
|
45 #endif
|
Chris@148
|
46
|
Chris@148
|
47 std::map<QString, int> MatrixFile::m_refcount;
|
Chris@537
|
48 QMutex MatrixFile::m_createMutex;
|
Chris@148
|
49
|
Chris@148
|
50 static size_t totalStorage = 0;
|
Chris@148
|
51 static size_t totalCount = 0;
|
Chris@537
|
52 static size_t openCount = 0;
|
Chris@148
|
53
|
Chris@148
|
54 MatrixFile::MatrixFile(QString fileBase, Mode mode,
|
Chris@537
|
55 size_t cellSize, size_t width, size_t height) :
|
Chris@148
|
56 m_fd(-1),
|
Chris@148
|
57 m_mode(mode),
|
Chris@148
|
58 m_flags(0),
|
Chris@148
|
59 m_fmode(0),
|
Chris@148
|
60 m_cellSize(cellSize),
|
Chris@537
|
61 m_width(width),
|
Chris@537
|
62 m_height(height),
|
Chris@537
|
63 m_headerSize(2 * sizeof(size_t))
|
Chris@148
|
64 {
|
Chris@148
|
65 Profiler profiler("MatrixFile::MatrixFile", true);
|
Chris@148
|
66
|
Chris@537
|
67 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
68 std::cerr << "MatrixFile::MatrixFile(" << fileBase.toStdString() << ", " << int(mode) << ", " << cellSize << ", " << width << ", " << height << ")" << std::endl;
|
Chris@537
|
69 #endif
|
Chris@148
|
70
|
Chris@548
|
71 m_createMutex.lock();
|
Chris@148
|
72
|
Chris@148
|
73 QDir tempDir(TempDirectory::getInstance()->getPath());
|
Chris@148
|
74 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase)));
|
Chris@148
|
75 bool newFile = !QFileInfo(fileName).exists();
|
Chris@148
|
76
|
Chris@148
|
77 if (newFile && m_mode == ReadOnly) {
|
Chris@148
|
78 std::cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode "
|
Chris@148
|
79 << "specified, but cache file does not exist" << std::endl;
|
Chris@148
|
80 throw FileNotFound(fileName);
|
Chris@148
|
81 }
|
Chris@148
|
82
|
Chris@537
|
83 if (!newFile && m_mode == WriteOnly) {
|
Chris@537
|
84 std::cerr << "ERROR: MatrixFile::MatrixFile: Write-only mode "
|
Chris@537
|
85 << "specified, but file already exists" << std::endl;
|
Chris@537
|
86 throw FileOperationFailed(fileName, "create");
|
Chris@148
|
87 }
|
Chris@148
|
88
|
Chris@148
|
89 m_flags = 0;
|
Chris@148
|
90 m_fmode = S_IRUSR | S_IWUSR;
|
Chris@148
|
91
|
Chris@537
|
92 if (m_mode == WriteOnly) {
|
Chris@537
|
93 m_flags = O_WRONLY | O_CREAT;
|
Chris@148
|
94 } else {
|
Chris@148
|
95 m_flags = O_RDONLY;
|
Chris@148
|
96 }
|
Chris@148
|
97
|
Chris@233
|
98 #ifdef _WIN32
|
Chris@233
|
99 m_flags |= O_BINARY;
|
Chris@233
|
100 #endif
|
Chris@233
|
101
|
Chris@148
|
102 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
103 std::cerr << "MatrixFile(" << this << ")::MatrixFile: opening " << fileName.toStdString() << "..." << std::endl;
|
Chris@148
|
104 #endif
|
Chris@148
|
105
|
Chris@148
|
106 if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) {
|
Chris@148
|
107 ::perror("Open failed");
|
Chris@148
|
108 std::cerr << "ERROR: MatrixFile::MatrixFile: "
|
Chris@148
|
109 << "Failed to open cache file \""
|
Chris@148
|
110 << fileName.toStdString() << "\"";
|
Chris@537
|
111 if (m_mode == WriteOnly) std::cerr << " for writing";
|
Chris@148
|
112 std::cerr << std::endl;
|
Chris@148
|
113 throw FailedToOpenFile(fileName);
|
Chris@148
|
114 }
|
Chris@148
|
115
|
Chris@548
|
116 m_createMutex.unlock();
|
Chris@548
|
117
|
Chris@537
|
118 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
119 std::cerr << "MatrixFile(" << this << ")::MatrixFile: fd is " << m_fd << std::endl;
|
Chris@537
|
120 #endif
|
Chris@537
|
121
|
Chris@148
|
122 if (newFile) {
|
Chris@537
|
123 initialise(); // write header and "unwritten" column tags
|
Chris@148
|
124 } else {
|
Chris@148
|
125 size_t header[2];
|
Chris@148
|
126 if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) {
|
Chris@236
|
127 ::perror("MatrixFile::MatrixFile: read failed");
|
Chris@148
|
128 std::cerr << "ERROR: MatrixFile::MatrixFile: "
|
Chris@148
|
129 << "Failed to read header (fd " << m_fd << ", file \""
|
Chris@148
|
130 << fileName.toStdString() << "\")" << std::endl;
|
Chris@148
|
131 throw FileReadFailed(fileName);
|
Chris@148
|
132 }
|
Chris@537
|
133 if (header[0] != m_width || header[1] != m_height) {
|
Chris@537
|
134 std::cerr << "ERROR: MatrixFile::MatrixFile: "
|
Chris@537
|
135 << "Dimensions in file header (" << header[0] << "x"
|
Chris@537
|
136 << header[1] << ") differ from expected dimensions "
|
Chris@537
|
137 << m_width << "x" << m_height << std::endl;
|
Chris@537
|
138 throw FailedToOpenFile(fileName);
|
Chris@537
|
139 }
|
Chris@148
|
140 }
|
Chris@148
|
141
|
Chris@148
|
142 m_fileName = fileName;
|
Chris@148
|
143 ++m_refcount[fileName];
|
Chris@148
|
144
|
Chris@537
|
145 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
146 std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: File " << fileName.toStdString() << ", ref " << m_refcount[fileName] << std::endl;
|
Chris@148
|
147
|
Chris@537
|
148 std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl;
|
Chris@537
|
149 #endif
|
Chris@148
|
150
|
Chris@148
|
151 ++totalCount;
|
Chris@537
|
152 ++openCount;
|
Chris@148
|
153 }
|
Chris@148
|
154
|
Chris@148
|
155 MatrixFile::~MatrixFile()
|
Chris@148
|
156 {
|
Chris@148
|
157 if (m_fd >= 0) {
|
Chris@148
|
158 if (::close(m_fd) < 0) {
|
Chris@148
|
159 ::perror("MatrixFile::~MatrixFile: close failed");
|
Chris@148
|
160 }
|
Chris@546
|
161 openCount --;
|
Chris@148
|
162 }
|
Chris@148
|
163
|
Chris@537
|
164 QMutexLocker locker(&m_createMutex);
|
Chris@537
|
165
|
Chris@148
|
166 if (m_fileName != "") {
|
Chris@148
|
167
|
Chris@148
|
168 if (--m_refcount[m_fileName] == 0) {
|
Chris@148
|
169
|
Chris@148
|
170 if (::unlink(m_fileName.toLocal8Bit())) {
|
Chris@537
|
171 std::cerr << "WARNING: MatrixFile::~MatrixFile: reference count reached 0, but failed to unlink file \"" << m_fileName.toStdString() << "\"" << std::endl;
|
Chris@148
|
172 } else {
|
Chris@537
|
173 std::cerr << "deleted " << m_fileName.toStdString() << std::endl;
|
Chris@148
|
174 }
|
Chris@148
|
175 }
|
Chris@148
|
176 }
|
Chris@537
|
177
|
Chris@537
|
178 if (m_mode == WriteOnly) {
|
Chris@537
|
179 totalStorage -= (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
|
Chris@537
|
180 }
|
Chris@148
|
181 totalCount --;
|
Chris@148
|
182
|
Chris@537
|
183 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
184 std::cerr << "MatrixFile[" << m_fd << "]::~MatrixFile: " << std::endl;
|
Chris@537
|
185 std::cerr << "MatrixFile: Total storage now " << totalStorage/1024 << "K in " << totalCount << " instances (" << openCount << " open)" << std::endl;
|
Chris@537
|
186 #endif
|
Chris@148
|
187 }
|
Chris@148
|
188
|
Chris@148
|
189 void
|
Chris@537
|
190 MatrixFile::initialise()
|
Chris@148
|
191 {
|
Chris@537
|
192 Profiler profiler("MatrixFile::initialise", true);
|
Chris@148
|
193
|
Chris@537
|
194 assert(m_mode == WriteOnly);
|
Chris@148
|
195
|
Chris@537
|
196 off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width;
|
Chris@148
|
197
|
Chris@148
|
198 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
199 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << std::endl;
|
Chris@148
|
200 #endif
|
Chris@148
|
201
|
Chris@537
|
202 if (::lseek(m_fd, off - 1, SEEK_SET) < 0) {
|
Chris@537
|
203 ::perror("ERROR: MatrixFile::initialise: seek to end failed");
|
Chris@537
|
204 throw FileOperationFailed(m_fileName, "lseek");
|
Chris@148
|
205 }
|
Chris@148
|
206
|
Chris@537
|
207 unsigned char byte = 0;
|
Chris@537
|
208 if (::write(m_fd, &byte, 1) != 1) {
|
Chris@537
|
209 ::perror("ERROR: MatrixFile::initialise: write at end failed");
|
Chris@537
|
210 throw FileOperationFailed(m_fileName, "write");
|
Chris@537
|
211 }
|
Chris@148
|
212
|
Chris@537
|
213 if (::lseek(m_fd, 0, SEEK_SET) < 0) {
|
Chris@547
|
214 ::perror("ERROR: MatrixFile::initialise: Seek to write header failed");
|
Chris@148
|
215 throw FileOperationFailed(m_fileName, "lseek");
|
Chris@148
|
216 }
|
Chris@148
|
217
|
Chris@148
|
218 size_t header[2];
|
Chris@537
|
219 header[0] = m_width;
|
Chris@537
|
220 header[1] = m_height;
|
Chris@148
|
221 if (::write(m_fd, header, 2 * sizeof(size_t)) != 2 * sizeof(size_t)) {
|
Chris@547
|
222 ::perror("ERROR: MatrixFile::initialise: Failed to write header");
|
Chris@148
|
223 throw FileOperationFailed(m_fileName, "write");
|
Chris@148
|
224 }
|
Chris@148
|
225
|
Chris@537
|
226 if (m_mode == WriteOnly) {
|
Chris@537
|
227 totalStorage += (m_headerSize + (m_width * m_height * m_cellSize) + m_width);
|
Chris@148
|
228 }
|
Chris@148
|
229
|
Chris@537
|
230 #ifdef DEBUG_MATRIX_FILE
|
Chris@547
|
231 std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): storage "
|
Chris@537
|
232 << (m_headerSize + m_width * m_height * m_cellSize + m_width) << std::endl;
|
Chris@148
|
233
|
Chris@537
|
234 std::cerr << "MatrixFile: Total storage " << totalStorage/1024 << "K" << std::endl;
|
Chris@148
|
235 #endif
|
Chris@148
|
236
|
Chris@148
|
237 seekTo(0, 0);
|
Chris@148
|
238 }
|
Chris@148
|
239
|
Chris@148
|
240 void
|
Chris@537
|
241 MatrixFile::close()
|
Chris@148
|
242 {
|
Chris@537
|
243 #ifdef DEBUG_MATRIX_FILE
|
Chris@537
|
244 std::cerr << "MatrixFile::close()" << std::endl;
|
Chris@537
|
245 #endif
|
Chris@537
|
246 if (m_fd >= 0) {
|
Chris@537
|
247 if (::close(m_fd) < 0) {
|
Chris@537
|
248 ::perror("MatrixFile::close: close failed");
|
Chris@537
|
249 }
|
Chris@537
|
250 m_fd = -1;
|
Chris@537
|
251 -- openCount;
|
Chris@148
|
252 }
|
Chris@148
|
253 }
|
Chris@148
|
254
|
Chris@148
|
255 void
|
Chris@148
|
256 MatrixFile::getColumnAt(size_t x, void *data)
|
Chris@148
|
257 {
|
Chris@537
|
258 assert(m_mode == ReadOnly);
|
Chris@537
|
259
|
Chris@537
|
260 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
261 std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << ")" << std::endl;
|
Chris@537
|
262 #endif
|
Chris@537
|
263
|
Chris@408
|
264 Profiler profiler("MatrixFile::getColumnAt");
|
Chris@148
|
265
|
Chris@537
|
266 unsigned char set = 0;
|
Chris@537
|
267 if (!seekTo(x, 0)) {
|
Chris@537
|
268 std::cerr << "ERROR: MatrixFile::getColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@537
|
269 throw FileOperationFailed(m_fileName, "seek");
|
Chris@148
|
270 }
|
Chris@148
|
271
|
Chris@537
|
272 ssize_t r = -1;
|
Chris@537
|
273 r = ::read(m_fd, &set, 1);
|
Chris@537
|
274 if (r < 0) {
|
Chris@537
|
275 ::perror("MatrixFile::getColumnAt: read failed");
|
Chris@537
|
276 throw FileReadFailed(m_fileName);
|
Chris@537
|
277 }
|
Chris@537
|
278 if (!set) {
|
Chris@537
|
279 std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << "): Column has not been set" << std::endl;
|
Chris@537
|
280 return;
|
Chris@537
|
281 }
|
Chris@537
|
282
|
Chris@537
|
283 r = ::read(m_fd, data, m_height * m_cellSize);
|
Chris@537
|
284 if (r < 0) {
|
Chris@537
|
285 ::perror("MatrixFile::getColumnAt: read failed");
|
Chris@537
|
286 throw FileReadFailed(m_fileName);
|
Chris@537
|
287 }
|
Chris@537
|
288 }
|
Chris@537
|
289
|
Chris@537
|
290 bool
|
Chris@537
|
291 MatrixFile::haveSetColumnAt(size_t x) const
|
Chris@537
|
292 {
|
Chris@537
|
293 assert(m_mode == ReadOnly);
|
Chris@537
|
294
|
Chris@537
|
295 Profiler profiler("MatrixFile::haveSetColumnAt");
|
Chris@537
|
296
|
Chris@537
|
297 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
298 std::cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << std::endl;
|
Chris@537
|
299 // std::cerr << ".";
|
Chris@148
|
300 #endif
|
Chris@148
|
301
|
Chris@537
|
302 unsigned char set = 0;
|
Chris@537
|
303 if (!seekTo(x, 0)) {
|
Chris@537
|
304 std::cerr << "ERROR: MatrixFile::haveSetColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@537
|
305 throw FileOperationFailed(m_fileName, "seek");
|
Chris@537
|
306 }
|
Chris@148
|
307
|
Chris@537
|
308 ssize_t r = -1;
|
Chris@537
|
309 r = ::read(m_fd, &set, 1);
|
Chris@148
|
310 if (r < 0) {
|
Chris@537
|
311 ::perror("MatrixFile::haveSetColumnAt: read failed");
|
Chris@148
|
312 throw FileReadFailed(m_fileName);
|
Chris@148
|
313 }
|
Chris@148
|
314
|
Chris@537
|
315 return set;
|
Chris@148
|
316 }
|
Chris@148
|
317
|
Chris@148
|
318 void
|
Chris@148
|
319 MatrixFile::setColumnAt(size_t x, const void *data)
|
Chris@148
|
320 {
|
Chris@537
|
321 assert(m_mode == WriteOnly);
|
Chris@148
|
322
|
Chris@148
|
323 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
324 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl;
|
Chris@537
|
325 // std::cerr << ".";
|
Chris@148
|
326 #endif
|
Chris@148
|
327
|
Chris@148
|
328 ssize_t w = 0;
|
Chris@148
|
329
|
Chris@537
|
330 if (!seekTo(x, 0)) {
|
Chris@537
|
331 std::cerr << "ERROR: MatrixFile::setColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@537
|
332 throw FileOperationFailed(m_fileName, "seek");
|
Chris@148
|
333 }
|
Chris@148
|
334
|
Chris@537
|
335 unsigned char set = 0;
|
Chris@537
|
336 w = ::write(m_fd, &set, 1);
|
Chris@537
|
337 if (w != 1) {
|
Chris@537
|
338 ::perror("WARNING: MatrixFile::setColumnAt: write failed (1)");
|
Chris@148
|
339 throw FileOperationFailed(m_fileName, "write");
|
Chris@537
|
340 }
|
Chris@537
|
341
|
Chris@537
|
342 w = ::write(m_fd, data, m_height * m_cellSize);
|
Chris@537
|
343 if (w != ssize_t(m_height * m_cellSize)) {
|
Chris@537
|
344 ::perror("WARNING: MatrixFile::setColumnAt: write failed (2)");
|
Chris@537
|
345 throw FileOperationFailed(m_fileName, "write");
|
Chris@537
|
346 }
|
Chris@537
|
347 /*
|
Chris@537
|
348 if (x == 0) {
|
Chris@537
|
349 std::cerr << "Wrote " << m_height * m_cellSize << " bytes, as follows:" << std::endl;
|
Chris@537
|
350 for (int i = 0; i < m_height * m_cellSize; ++i) {
|
Chris@537
|
351 std::cerr << (int)(((char *)data)[i]) << " ";
|
Chris@537
|
352 }
|
Chris@537
|
353 std::cerr << std::endl;
|
Chris@537
|
354 }
|
Chris@537
|
355 */
|
Chris@537
|
356 if (!seekTo(x, 0)) {
|
Chris@537
|
357 std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Seek failed" << std::endl;
|
Chris@148
|
358 throw FileOperationFailed(m_fileName, "seek");
|
Chris@537
|
359 }
|
Chris@537
|
360
|
Chris@537
|
361 set = 1;
|
Chris@537
|
362 w = ::write(m_fd, &set, 1);
|
Chris@537
|
363 if (w != 1) {
|
Chris@537
|
364 ::perror("WARNING: MatrixFile::setColumnAt: write failed (3)");
|
Chris@537
|
365 throw FileOperationFailed(m_fileName, "write");
|
Chris@148
|
366 }
|
Chris@148
|
367 }
|
Chris@148
|
368
|
Chris@537
|
369 bool
|
Chris@537
|
370 MatrixFile::seekTo(size_t x, size_t y) const
|
Chris@148
|
371 {
|
Chris@537
|
372 if (m_fd < 0) {
|
Chris@537
|
373 std::cerr << "ERROR: MatrixFile::seekTo: File not open" << std::endl;
|
Chris@537
|
374 return false;
|
Chris@148
|
375 }
|
Chris@148
|
376
|
Chris@537
|
377 off_t off = m_headerSize + x * m_height * m_cellSize + x + y * m_cellSize;
|
Chris@148
|
378
|
Chris@148
|
379 #ifdef DEBUG_MATRIX_FILE_READ_SET
|
Chris@537
|
380 std::cerr << "MatrixFile[" << m_fd << "]::seekTo(" << x << "," << y << "): off = " << off << std::endl;
|
Chris@148
|
381 #endif
|
Chris@148
|
382
|
Chris@148
|
383 if (::lseek(m_fd, off, SEEK_SET) == (off_t)-1) {
|
Chris@148
|
384 ::perror("Seek failed");
|
Chris@148
|
385 std::cerr << "ERROR: MatrixFile::seekTo(" << x << ", " << y
|
Chris@537
|
386 << ") = " << off << " failed" << std::endl;
|
Chris@148
|
387 return false;
|
Chris@148
|
388 }
|
Chris@148
|
389
|
Chris@148
|
390 return true;
|
Chris@148
|
391 }
|
Chris@148
|
392
|