annotate base/TempDirectory.cpp @ 101:ce1d385f4f89

* Use kill(pid, 0) instead of /proc or sysctl blather for looking up pids * Add OpenProcess call for Win32
author Chris Cannam
date Fri, 05 May 2006 12:34:51 +0000
parents 604bd4ee3ed4
children 68ff162fdbc3
rev   line source
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@98 17 #include "System.h"
Chris@81 18
Chris@81 19 #include <QDir>
Chris@81 20 #include <QFile>
Chris@81 21 #include <QMutexLocker>
Chris@81 22
Chris@81 23 #include <iostream>
Chris@81 24 #include <cassert>
Chris@81 25
Chris@81 26 TempDirectory *
Chris@81 27 TempDirectory::m_instance = new TempDirectory;
Chris@81 28
Chris@81 29 TempDirectory *
Chris@81 30 TempDirectory::instance()
Chris@81 31 {
Chris@81 32 return m_instance;
Chris@81 33 }
Chris@81 34
Chris@81 35 TempDirectory::DirectoryCreationFailed::DirectoryCreationFailed(QString directory) throw() :
Chris@81 36 m_directory(directory)
Chris@81 37 {
Chris@81 38 std::cerr << "ERROR: Directory creation failed for directory: "
Chris@81 39 << directory.toLocal8Bit().data() << std::endl;
Chris@81 40 }
Chris@81 41
Chris@81 42 const char *
Chris@81 43 TempDirectory::DirectoryCreationFailed::what() const throw()
Chris@81 44 {
Chris@81 45 return QString("Directory creation failed for \"%1\"")
Chris@81 46 .arg(m_directory).toLocal8Bit().data();
Chris@81 47 }
Chris@81 48
Chris@81 49 TempDirectory::TempDirectory() :
Chris@81 50 m_tmpdir("")
Chris@81 51 {
Chris@81 52 }
Chris@81 53
Chris@81 54 TempDirectory::~TempDirectory()
Chris@81 55 {
Chris@81 56 std::cerr << "TempDirectory::~TempDirectory" << std::endl;
Chris@81 57
Chris@81 58 cleanup();
Chris@81 59 }
Chris@81 60
Chris@81 61 void
Chris@81 62 TempDirectory::cleanup()
Chris@81 63 {
Chris@81 64 cleanupDirectory("");
Chris@81 65 }
Chris@81 66
Chris@81 67 QString
Chris@81 68 TempDirectory::getPath()
Chris@81 69 {
Chris@81 70 QMutexLocker locker(&m_mutex);
Chris@81 71
Chris@81 72 if (m_tmpdir != "") return m_tmpdir;
Chris@81 73
Chris@98 74 QString svDirBase = ".sv";
Chris@98 75 QString svDir = QDir::home().filePath(svDirBase);
Chris@98 76 if (!QFileInfo(svDir).exists()) {
Chris@98 77 if (!QDir::home().mkdir(svDirBase)) {
Chris@98 78 throw DirectoryCreationFailed(QString("%1 directory in $HOME")
Chris@98 79 .arg(svDirBase));
Chris@98 80 }
Chris@98 81 } else if (!QFileInfo(svDir).isDir()) {
Chris@98 82 throw DirectoryCreationFailed(QString("$HOME/%1 is not a directory")
Chris@98 83 .arg(svDirBase));
Chris@98 84 }
Chris@98 85
Chris@98 86 cleanupAbandonedDirectories(svDir);
Chris@98 87
Chris@98 88 return createTempDirectoryIn(svDir);
Chris@98 89 }
Chris@98 90
Chris@98 91 QString
Chris@98 92 TempDirectory::createTempDirectoryIn(QString dir)
Chris@98 93 {
Chris@98 94 // Entered with mutex held.
Chris@98 95
Chris@98 96 QDir tempDirBase(dir);
Chris@81 97
Chris@81 98 // Generate a temporary directory. Qt4.1 doesn't seem to be able
Chris@81 99 // to do this for us, and mkdtemp is not standard. This method is
Chris@81 100 // based on the way glibc does mkdtemp.
Chris@81 101
Chris@81 102 static QString chars =
Chris@81 103 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Chris@81 104
Chris@81 105 QString suffix;
Chris@81 106 int padlen = 6, attempts = 100;
Chris@81 107 unsigned int r = time(0) ^ getpid();
Chris@81 108
Chris@81 109 for (int i = 0; i < padlen; ++i) {
Chris@81 110 suffix += "X";
Chris@81 111 }
Chris@81 112
Chris@81 113 for (int j = 0; j < attempts; ++j) {
Chris@81 114
Chris@81 115 unsigned int v = r;
Chris@81 116
Chris@81 117 for (int i = 0; i < padlen; ++i) {
Chris@81 118 suffix[i] = chars[v % 62];
Chris@81 119 v /= 62;
Chris@81 120 }
Chris@81 121
Chris@81 122 QString candidate = QString("sv_%1").arg(suffix);
Chris@81 123
Chris@86 124 if (tempDirBase.mkpath(candidate)) {
Chris@86 125 m_tmpdir = tempDirBase.filePath(candidate);
Chris@81 126 break;
Chris@81 127 }
Chris@81 128
Chris@81 129 r = r + 7777;
Chris@81 130 }
Chris@81 131
Chris@81 132 if (m_tmpdir == "") {
Chris@81 133 throw DirectoryCreationFailed(QString("temporary subdirectory in %1")
Chris@86 134 .arg(tempDirBase.canonicalPath()));
Chris@81 135 }
Chris@81 136
Chris@98 137 QString pidpath = QDir(m_tmpdir).filePath(QString("%1.pid").arg(getpid()));
Chris@98 138 QFile pidfile(pidpath);
Chris@98 139
Chris@98 140 if (!pidfile.open(QIODevice::WriteOnly)) {
Chris@98 141 throw DirectoryCreationFailed(QString("pid file creation in %1")
Chris@98 142 .arg(m_tmpdir));
Chris@98 143 } else {
Chris@98 144 pidfile.close();
Chris@98 145 }
Chris@98 146
Chris@81 147 return m_tmpdir;
Chris@81 148 }
Chris@81 149
Chris@81 150 QString
Chris@81 151 TempDirectory::getSubDirectoryPath(QString subdir)
Chris@81 152 {
Chris@81 153 QString tmpdirpath = getPath();
Chris@81 154
Chris@81 155 QMutexLocker locker(&m_mutex);
Chris@81 156
Chris@81 157 QDir tmpdir(tmpdirpath);
Chris@81 158 QFileInfo fi(tmpdir.filePath(subdir));
Chris@81 159
Chris@81 160 if (!fi.exists()) {
Chris@81 161 if (!tmpdir.mkdir(subdir)) {
Chris@81 162 throw DirectoryCreationFailed(fi.filePath());
Chris@81 163 } else {
Chris@81 164 return fi.filePath();
Chris@81 165 }
Chris@81 166 } else if (fi.isDir()) {
Chris@81 167 return fi.filePath();
Chris@81 168 } else {
Chris@81 169 throw DirectoryCreationFailed(fi.filePath());
Chris@81 170 }
Chris@81 171 }
Chris@81 172
Chris@81 173 void
Chris@81 174 TempDirectory::cleanupDirectory(QString tmpdir)
Chris@81 175 {
Chris@81 176 bool isRoot = false;
Chris@81 177
Chris@81 178 if (tmpdir == "") {
Chris@81 179
Chris@81 180 m_mutex.lock();
Chris@81 181
Chris@81 182 isRoot = true;
Chris@81 183 tmpdir = m_tmpdir;
Chris@81 184
Chris@81 185 if (tmpdir == "") {
Chris@81 186 m_mutex.unlock();
Chris@81 187 return;
Chris@81 188 }
Chris@81 189 }
Chris@81 190
Chris@81 191 QDir dir(tmpdir);
Chris@83 192 dir.setFilter(QDir::Dirs | QDir::Files);
Chris@81 193
Chris@81 194 for (unsigned int i = 0; i < dir.count(); ++i) {
Chris@81 195
Chris@83 196 if (dir[i] == "." || dir[i] == "..") continue;
Chris@81 197 QFileInfo fi(dir.filePath(dir[i]));
Chris@81 198
Chris@81 199 if (fi.isDir()) {
Chris@81 200 cleanupDirectory(fi.absoluteFilePath());
Chris@81 201 } else {
Chris@81 202 if (!QFile(fi.absoluteFilePath()).remove()) {
Chris@81 203 std::cerr << "WARNING: TempDirectory::cleanup: "
Chris@81 204 << "Failed to unlink file \""
Chris@81 205 << fi.absoluteFilePath().toStdString() << "\""
Chris@81 206 << std::endl;
Chris@81 207 }
Chris@81 208 }
Chris@81 209 }
Chris@81 210
Chris@81 211 QString dirname = dir.dirName();
Chris@81 212 if (dirname != "") {
Chris@81 213 if (!dir.cdUp()) {
Chris@81 214 std::cerr << "WARNING: TempDirectory::cleanup: "
Chris@81 215 << "Failed to cd to parent directory of "
Chris@81 216 << tmpdir.toStdString() << std::endl;
Chris@81 217 return;
Chris@81 218 }
Chris@81 219 if (!dir.rmdir(dirname)) {
Chris@81 220 std::cerr << "WARNING: TempDirectory::cleanup: "
Chris@81 221 << "Failed to remove directory "
Chris@81 222 << dirname.toStdString() << std::endl;
Chris@81 223 }
Chris@81 224 }
Chris@81 225
Chris@81 226 if (isRoot) {
Chris@81 227 m_tmpdir = "";
Chris@81 228 m_mutex.unlock();
Chris@81 229 }
Chris@81 230 }
Chris@98 231
Chris@98 232 void
Chris@98 233 TempDirectory::cleanupAbandonedDirectories(QString svDir)
Chris@98 234 {
Chris@98 235 QDir dir(svDir, "sv_*", QDir::Name, QDir::Dirs);
Chris@98 236
Chris@98 237 for (unsigned int i = 0; i < dir.count(); ++i) {
Chris@98 238
Chris@98 239 QDir subdir(dir.filePath(dir[i]), "*.pid", QDir::Name, QDir::Files);
Chris@98 240
Chris@98 241 for (unsigned int j = 0; j < subdir.count(); ++j) {
Chris@98 242
Chris@98 243 bool ok = false;
Chris@98 244 int pid = QFileInfo(subdir[j]).baseName().toInt(&ok);
Chris@98 245 if (!ok) continue;
Chris@98 246
Chris@98 247 if (GetProcessStatus(pid) == ProcessNotRunning) {
Chris@98 248 std::cerr << "INFO: Found abandoned temporary directory from "
Chris@98 249 << "an old Sonic Visualiser process\n(pid=" << pid
Chris@98 250 << ", directory=\""
Chris@98 251 << dir.filePath(dir[i]).toStdString()
Chris@98 252 << "\"). Removing it..." << std::endl;
Chris@98 253 cleanupDirectory(dir.filePath(dir[i]));
Chris@98 254 std::cerr << "...done." << std::endl;
Chris@98 255 break;
Chris@98 256 }
Chris@98 257 }
Chris@98 258 }
Chris@98 259 }
Chris@98 260
Chris@98 261
Chris@98 262