mirror of
https://github.com/lubosz/overte.git
synced 2025-04-27 21:55:28 +02:00
Add temp dir removal to asset server
This commit is contained in:
parent
7eb93f4777
commit
de7e3e60cd
5 changed files with 74 additions and 0 deletions
assignment-client/src/assets
libraries/shared/src
|
@ -416,6 +416,9 @@ void AssetServer::completeSetup() {
|
|||
if (assetsFilesizeLimit != 0 && assetsFilesizeLimit < MAX_UPLOAD_SIZE) {
|
||||
_filesizeLimit = assetsFilesizeLimit * BITS_PER_MEGABITS;
|
||||
}
|
||||
|
||||
PathUtils::removeTemporaryDirs();
|
||||
PathUtils::removeTemporaryDirs("Oven");
|
||||
}
|
||||
|
||||
void AssetServer::cleanupUnmappedFiles() {
|
||||
|
|
|
@ -19,8 +19,14 @@
|
|||
#include <QDir>
|
||||
#include <QUrl>
|
||||
#include <QtCore/QStandardPaths>
|
||||
#include <QRegularExpression>
|
||||
#include <mutex> // std::once
|
||||
#include "shared/GlobalAppProperties.h"
|
||||
#include "SharedUtil.h"
|
||||
|
||||
// Format: AppName-PID-Timestamp
|
||||
// Example: ...
|
||||
QString TEMP_DIR_FORMAT { "%1-%2-%3" };
|
||||
|
||||
const QString& PathUtils::resourcesPath() {
|
||||
#ifdef Q_OS_MAC
|
||||
|
@ -60,6 +66,7 @@ QString PathUtils::generateTemporaryDir() {
|
|||
QString appName = qApp->applicationName();
|
||||
for (auto i = 0; i < 64; ++i) {
|
||||
auto now = std::chrono::system_clock::now().time_since_epoch().count();
|
||||
auto dirName = TEMP_DIR_FORMAT.arg(appName).arg(qApp->applicationPid()).arg(now);
|
||||
QDir tempDir = rootTempDir.filePath(appName + "-" + QString::number(now));
|
||||
if (tempDir.mkpath(".")) {
|
||||
return tempDir.absolutePath();
|
||||
|
@ -68,6 +75,46 @@ QString PathUtils::generateTemporaryDir() {
|
|||
return "";
|
||||
}
|
||||
|
||||
// Delete all temporary directories for an application
|
||||
int PathUtils::removeTemporaryDirs(QString appName) {
|
||||
if (appName.isNull()) {
|
||||
appName = qApp->applicationName();
|
||||
}
|
||||
|
||||
auto dirName = TEMP_DIR_FORMAT.arg(appName).arg("*").arg("*");
|
||||
qDebug() << "Dirname format is: " << dirName;
|
||||
|
||||
QDir rootTempDir = QDir::tempPath();
|
||||
qDebug() << "Temp dir is: " << rootTempDir;
|
||||
auto dirs = rootTempDir.entryInfoList({ dirName }, QDir::Dirs);
|
||||
int removed = 0;
|
||||
for (auto& dir : dirs) {
|
||||
auto dirName = dir.fileName();
|
||||
auto absoluteDirPath = QDir(dir.absoluteFilePath());
|
||||
qDebug() << " Deleting: " << dirName << absoluteDirPath;
|
||||
QRegularExpression re { "^" + QRegularExpression::escape(appName) + "\\-(?<pid>\\d+)\\-(?<timestamp>\\d+)$" };
|
||||
|
||||
auto match = re.match(dirName);
|
||||
if (match.hasMatch()) {
|
||||
qDebug() << " Got match";
|
||||
auto pid = match.capturedRef("pid").toLongLong();
|
||||
auto timestamp = match.capturedRef("timestamp");
|
||||
qDebug() << " Is " << pid << " running?" << processIsRunning(pid);
|
||||
if (!processIsRunning(pid)) {
|
||||
qDebug() << " Removing: " << absoluteDirPath;
|
||||
absoluteDirPath.removeRecursively();
|
||||
removed++;
|
||||
} else {
|
||||
qDebug() << " Not removing (process is running): " << dir.absoluteDir();
|
||||
}
|
||||
} else {
|
||||
qDebug() << " NO MATCH";
|
||||
}
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions) {
|
||||
QString fileNameLowered = fileName.toLower();
|
||||
foreach (const QString possibleExtension, possibleExtensions) {
|
||||
|
|
|
@ -39,6 +39,8 @@ public:
|
|||
|
||||
static QString generateTemporaryDir();
|
||||
|
||||
static int removeTemporaryDirs(QString appName = QString::null);
|
||||
|
||||
static Qt::CaseSensitivity getFSCaseSensitivity();
|
||||
static QString stripFilename(const QUrl& url);
|
||||
// note: this is FS-case-sensitive version of parentURL.isParentOf(childURL)
|
||||
|
|
|
@ -44,6 +44,11 @@ extern "C" FILE * __cdecl __iob_func(void) {
|
|||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(Q_OS_LINUX) || defined(__APPLE__)
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
#include <QDateTime>
|
||||
#include <QElapsedTimer>
|
||||
|
@ -1078,6 +1083,21 @@ void setMaxCores(uint8_t maxCores) {
|
|||
#endif
|
||||
}
|
||||
|
||||
bool processIsRunning(int64_t pid) {
|
||||
#ifdef Q_OS_WIN
|
||||
HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
|
||||
if (process) {
|
||||
DWORD exitCodeOut;
|
||||
if (GetExitCodeProcess(process, &exitCodeOut) != 0) {
|
||||
return exitCodeOut == STILL_ACTIVE;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#elif defined(Q_OS_LINUX) || defined(__APPLE__)
|
||||
return kill(pid, 0) != ESRCH;
|
||||
#endif
|
||||
}
|
||||
|
||||
void quitWithParentProcess() {
|
||||
if (qApp) {
|
||||
qDebug() << "Parent process died, quitting";
|
||||
|
|
|
@ -238,6 +238,8 @@ void setMaxCores(uint8_t maxCores);
|
|||
const QString PARENT_PID_OPTION = "parent-pid";
|
||||
void watchParentProcess(int parentPID);
|
||||
|
||||
bool processIsRunning(int64_t pid);
|
||||
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
void* createProcessGroup();
|
||||
|
|
Loading…
Reference in a new issue