PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/sonic-visualiser-1.9/svcore/data/fileio/MatrixFile.cpp

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