Chris@81
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@81
|
2
|
Chris@81
|
3 /*
|
Chris@81
|
4 Sonic Visualiser
|
Chris@81
|
5 An audio file viewer and annotation editor.
|
Chris@81
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@81
|
7 This file copyright 2006 Chris Cannam.
|
Chris@81
|
8
|
Chris@81
|
9 This program is free software; you can redistribute it and/or
|
Chris@81
|
10 modify it under the terms of the GNU General Public License as
|
Chris@81
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@81
|
12 License, or (at your option) any later version. See the file
|
Chris@81
|
13 COPYING included with this distribution for more information.
|
Chris@81
|
14 */
|
Chris@81
|
15
|
Chris@81
|
16 #include "TempDirectory.h"
|
Chris@81
|
17
|
Chris@81
|
18 #include <QDir>
|
Chris@81
|
19 #include <QFile>
|
Chris@81
|
20 #include <QMutexLocker>
|
Chris@81
|
21
|
Chris@81
|
22 #include <iostream>
|
Chris@81
|
23 #include <cassert>
|
Chris@81
|
24
|
Chris@81
|
25 TempDirectory *
|
Chris@81
|
26 TempDirectory::m_instance = new TempDirectory;
|
Chris@81
|
27
|
Chris@81
|
28 TempDirectory *
|
Chris@81
|
29 TempDirectory::instance()
|
Chris@81
|
30 {
|
Chris@81
|
31 return m_instance;
|
Chris@81
|
32 }
|
Chris@81
|
33
|
Chris@81
|
34 TempDirectory::DirectoryCreationFailed::DirectoryCreationFailed(QString directory) throw() :
|
Chris@81
|
35 m_directory(directory)
|
Chris@81
|
36 {
|
Chris@81
|
37 std::cerr << "ERROR: Directory creation failed for directory: "
|
Chris@81
|
38 << directory.toLocal8Bit().data() << std::endl;
|
Chris@81
|
39 }
|
Chris@81
|
40
|
Chris@81
|
41 const char *
|
Chris@81
|
42 TempDirectory::DirectoryCreationFailed::what() const throw()
|
Chris@81
|
43 {
|
Chris@81
|
44 return QString("Directory creation failed for \"%1\"")
|
Chris@81
|
45 .arg(m_directory).toLocal8Bit().data();
|
Chris@81
|
46 }
|
Chris@81
|
47
|
Chris@81
|
48 TempDirectory::TempDirectory() :
|
Chris@81
|
49 m_tmpdir("")
|
Chris@81
|
50 {
|
Chris@81
|
51 }
|
Chris@81
|
52
|
Chris@81
|
53 TempDirectory::~TempDirectory()
|
Chris@81
|
54 {
|
Chris@81
|
55 std::cerr << "TempDirectory::~TempDirectory" << std::endl;
|
Chris@81
|
56
|
Chris@81
|
57 cleanup();
|
Chris@81
|
58 }
|
Chris@81
|
59
|
Chris@81
|
60 void
|
Chris@81
|
61 TempDirectory::cleanup()
|
Chris@81
|
62 {
|
Chris@81
|
63 cleanupDirectory("");
|
Chris@81
|
64 }
|
Chris@81
|
65
|
Chris@81
|
66 QString
|
Chris@81
|
67 TempDirectory::getPath()
|
Chris@81
|
68 {
|
Chris@81
|
69 QMutexLocker locker(&m_mutex);
|
Chris@81
|
70
|
Chris@81
|
71 if (m_tmpdir != "") return m_tmpdir;
|
Chris@81
|
72
|
Chris@81
|
73 QDir systemTempDir = QDir::temp();
|
Chris@81
|
74
|
Chris@81
|
75 // Generate a temporary directory. Qt4.1 doesn't seem to be able
|
Chris@81
|
76 // to do this for us, and mkdtemp is not standard. This method is
|
Chris@81
|
77 // based on the way glibc does mkdtemp.
|
Chris@81
|
78
|
Chris@81
|
79 static QString chars =
|
Chris@81
|
80 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
Chris@81
|
81
|
Chris@81
|
82 QString suffix;
|
Chris@81
|
83 int padlen = 6, attempts = 100;
|
Chris@81
|
84 unsigned int r = time(0) ^ getpid();
|
Chris@81
|
85
|
Chris@81
|
86 for (int i = 0; i < padlen; ++i) {
|
Chris@81
|
87 suffix += "X";
|
Chris@81
|
88 }
|
Chris@81
|
89
|
Chris@81
|
90 for (int j = 0; j < attempts; ++j) {
|
Chris@81
|
91
|
Chris@81
|
92 unsigned int v = r;
|
Chris@81
|
93
|
Chris@81
|
94 for (int i = 0; i < padlen; ++i) {
|
Chris@81
|
95 suffix[i] = chars[v % 62];
|
Chris@81
|
96 v /= 62;
|
Chris@81
|
97 }
|
Chris@81
|
98
|
Chris@81
|
99 QString candidate = QString("sv_%1").arg(suffix);
|
Chris@81
|
100
|
Chris@81
|
101 if (QDir::temp().mkpath(candidate)) {
|
Chris@81
|
102 m_tmpdir = systemTempDir.filePath(candidate);
|
Chris@81
|
103 break;
|
Chris@81
|
104 }
|
Chris@81
|
105
|
Chris@81
|
106 r = r + 7777;
|
Chris@81
|
107 }
|
Chris@81
|
108
|
Chris@81
|
109 if (m_tmpdir == "") {
|
Chris@81
|
110 throw DirectoryCreationFailed(QString("temporary subdirectory in %1")
|
Chris@81
|
111 .arg(systemTempDir.canonicalPath()));
|
Chris@81
|
112 }
|
Chris@81
|
113
|
Chris@81
|
114 return m_tmpdir;
|
Chris@81
|
115 }
|
Chris@81
|
116
|
Chris@81
|
117 QString
|
Chris@81
|
118 TempDirectory::getSubDirectoryPath(QString subdir)
|
Chris@81
|
119 {
|
Chris@81
|
120 QString tmpdirpath = getPath();
|
Chris@81
|
121
|
Chris@81
|
122 QMutexLocker locker(&m_mutex);
|
Chris@81
|
123
|
Chris@81
|
124 QDir tmpdir(tmpdirpath);
|
Chris@81
|
125 QFileInfo fi(tmpdir.filePath(subdir));
|
Chris@81
|
126
|
Chris@81
|
127 if (!fi.exists()) {
|
Chris@81
|
128 if (!tmpdir.mkdir(subdir)) {
|
Chris@81
|
129 throw DirectoryCreationFailed(fi.filePath());
|
Chris@81
|
130 } else {
|
Chris@81
|
131 return fi.filePath();
|
Chris@81
|
132 }
|
Chris@81
|
133 } else if (fi.isDir()) {
|
Chris@81
|
134 return fi.filePath();
|
Chris@81
|
135 } else {
|
Chris@81
|
136 throw DirectoryCreationFailed(fi.filePath());
|
Chris@81
|
137 }
|
Chris@81
|
138 }
|
Chris@81
|
139
|
Chris@81
|
140 void
|
Chris@81
|
141 TempDirectory::cleanupDirectory(QString tmpdir)
|
Chris@81
|
142 {
|
Chris@81
|
143 bool isRoot = false;
|
Chris@81
|
144
|
Chris@81
|
145 if (tmpdir == "") {
|
Chris@81
|
146
|
Chris@81
|
147 m_mutex.lock();
|
Chris@81
|
148
|
Chris@81
|
149 isRoot = true;
|
Chris@81
|
150 tmpdir = m_tmpdir;
|
Chris@81
|
151
|
Chris@81
|
152 if (tmpdir == "") {
|
Chris@81
|
153 m_mutex.unlock();
|
Chris@81
|
154 return;
|
Chris@81
|
155 }
|
Chris@81
|
156 }
|
Chris@81
|
157
|
Chris@81
|
158 QDir dir(tmpdir);
|
Chris@81
|
159 dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
Chris@81
|
160
|
Chris@81
|
161 for (unsigned int i = 0; i < dir.count(); ++i) {
|
Chris@81
|
162
|
Chris@81
|
163 QFileInfo fi(dir.filePath(dir[i]));
|
Chris@81
|
164
|
Chris@81
|
165 if (fi.isDir()) {
|
Chris@81
|
166 cleanupDirectory(fi.absoluteFilePath());
|
Chris@81
|
167 } else {
|
Chris@81
|
168 if (!QFile(fi.absoluteFilePath()).remove()) {
|
Chris@81
|
169 std::cerr << "WARNING: TempDirectory::cleanup: "
|
Chris@81
|
170 << "Failed to unlink file \""
|
Chris@81
|
171 << fi.absoluteFilePath().toStdString() << "\""
|
Chris@81
|
172 << std::endl;
|
Chris@81
|
173 }
|
Chris@81
|
174 }
|
Chris@81
|
175 }
|
Chris@81
|
176
|
Chris@81
|
177 QString dirname = dir.dirName();
|
Chris@81
|
178 if (dirname != "") {
|
Chris@81
|
179 if (!dir.cdUp()) {
|
Chris@81
|
180 std::cerr << "WARNING: TempDirectory::cleanup: "
|
Chris@81
|
181 << "Failed to cd to parent directory of "
|
Chris@81
|
182 << tmpdir.toStdString() << std::endl;
|
Chris@81
|
183 return;
|
Chris@81
|
184 }
|
Chris@81
|
185 if (!dir.rmdir(dirname)) {
|
Chris@81
|
186 std::cerr << "WARNING: TempDirectory::cleanup: "
|
Chris@81
|
187 << "Failed to remove directory "
|
Chris@81
|
188 << dirname.toStdString() << std::endl;
|
Chris@81
|
189 }
|
Chris@81
|
190 }
|
Chris@81
|
191
|
Chris@81
|
192 if (isRoot) {
|
Chris@81
|
193 m_tmpdir = "";
|
Chris@81
|
194 m_mutex.unlock();
|
Chris@81
|
195 }
|
Chris@81
|
196 }
|