23 #include <QMutexLocker> 47 SVDEBUG <<
"TempDirectory::~TempDirectory" << endl;
64 settings.beginGroup(
"TempDirectory");
65 QString svDirParent = settings.value(
"create-in",
"$HOME").toString();
69 if (svDirParent !=
"$HOME") {
71 svDir.replace(QDir::home().absolutePath(), svDirParent);
74 if (!QFileInfo(svDir).exists()) {
75 if (!QDir(svDirParent).mkpath(svDir)) {
77 .arg(svDir).arg(svDirParent));
79 }
else if (!QFileInfo(svDir).isDir()) {
102 QDir tempDirBase(dir);
108 static QString chars =
109 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
112 int padlen = 6, attempts = 100;
113 unsigned int r = (
unsigned int)(time(
nullptr) ^ getpid());
115 for (
int i = 0; i < padlen; ++i) {
119 for (
int j = 0; j < attempts; ++j) {
123 for (
int i = 0; i < padlen; ++i) {
124 suffix[i] = chars[v % 62];
128 QString candidate = QString(
"sv_%1").arg(suffix);
130 if (tempDirBase.mkpath(candidate)) {
131 m_tmpdir = tempDirBase.filePath(candidate);
140 .arg(tempDirBase.canonicalPath()));
143 QString pidpath = QDir(
m_tmpdir).filePath(QString(
"%1.pid").arg(getpid()));
144 QFile pidfile(pidpath);
146 if (!pidfile.open(QIODevice::WriteOnly)) {
159 QString tmpdirpath =
getPath();
163 QDir tmpdir(tmpdirpath);
164 QFileInfo fi(tmpdir.filePath(subdir));
167 if (!tmpdir.mkdir(subdir)) {
170 return fi.filePath();
172 }
else if (fi.isDir()) {
173 return fi.filePath();
198 dir.setFilter(QDir::Dirs | QDir::Files);
200 for (
unsigned int i = 0; i < dir.count(); ++i) {
202 if (dir[i] ==
"." || dir[i] ==
"..")
continue;
203 QFileInfo fi(dir.filePath(dir[i]));
208 if (!QFile(fi.absoluteFilePath()).
remove()) {
209 cerr <<
"WARNING: TempDirectory::cleanup: " 210 <<
"Failed to unlink file \"" 211 << fi.absoluteFilePath() <<
"\"" 217 QString dirname = dir.dirName();
220 cerr <<
"WARNING: TempDirectory::cleanup: " 221 <<
"Failed to cd to parent directory of " 225 if (!dir.rmdir(dirname)) {
226 cerr <<
"WARNING: TempDirectory::cleanup: " 227 <<
"Failed to remove directory " 241 QDir dir(svDir,
"sv_*", QDir::Name, QDir::Dirs);
243 for (
unsigned int i = 0; i < dir.count(); ++i) {
245 QString dirpath = dir.filePath(dir[i]);
247 QDir subdir(dirpath,
"*.pid", QDir::Name, QDir::Files);
249 if (subdir.count() == 0) {
250 cerr <<
"INFO: Found temporary directory with no .pid file in it!\n(directory=\"" 251 << dirpath <<
"\"). Removing it..." << endl;
253 cerr <<
"...done." << endl;
257 for (
unsigned int j = 0; j < subdir.count(); ++j) {
260 int pid = QFileInfo(subdir[j]).baseName().toInt(&ok);
264 cerr <<
"INFO: Found abandoned temporary directory from " 265 <<
"a previous, defunct process\n(pid=" << pid
268 <<
"\"). Removing it..." << endl;
270 cerr <<
"...done." << endl;
void cleanup()
Delete the temporary directory (before exiting).
ProcessStatus GetProcessStatus(int pid)
QString createTempDirectoryIn(QString inDir)
QString getPath()
Create the root temporary directory if necessary, and return its path.
A class that manages the creation and removal of a temporary directory tree to store data during the ...
static TempDirectory * getInstance()
static TempDirectory * m_instance
void cleanupDirectory(QString tmpDir)
void cleanupAbandonedDirectories(QString svDir)
QString getSubDirectoryPath(QString subdir)
Create an immediate subdirectory of the root temporary directory of the given name, if it doesn't already exist, and return its path.
QString getUserResourcePrefix()
Return the root path for user-specific resource installation for this application (i...
QString getContainingPath()
Return the path of the directory in which the temporary directory has been or will be created...