// Delete all temporary directories for an application int PathUtils::removeTemporaryApplicationDirs(QString appName) { if (appName.isNull()) { appName = qApp->applicationName(); } auto dirName = TEMP_DIR_FORMAT.arg(appName).arg("*").arg("*"); QDir rootTempDir = QDir::tempPath(); auto dirs = rootTempDir.entryInfoList({ dirName }, QDir::Dirs); int removed = 0; for (auto& dir : dirs) { auto dirName = dir.fileName(); auto absoluteDirPath = QDir(dir.absoluteFilePath()); QRegularExpression re { "^" + QRegularExpression::escape(appName) + "\\-(?<pid>\\d+)\\-(?<timestamp>\\d+)$" }; auto match = re.match(dirName); if (match.hasMatch()) { auto pid = match.capturedRef("pid").toLongLong(); auto timestamp = match.capturedRef("timestamp"); if (!processIsRunning(pid)) { qDebug() << " Removing old temporary directory: " << dir.absoluteFilePath(); absoluteDirPath.removeRecursively(); removed++; } else { qDebug() << " Not removing (process is running): " << dir.absoluteFilePath(); } } } return removed; }
void PropagateLocalRemove::start() { if (_propagator->_abortRequested.fetchAndAddRelaxed(0)) return; QString filename = _propagator->_localDir + _item._file; if( _propagator->localFileNameClash(_item._file)) { done(SyncFileItem::NormalError, tr("Could not remove %1 because of a local file name clash") .arg(QDir::toNativeSeparators(filename))); return; } if (_item._isDirectory) { QString error; if (QDir(filename).exists() && !removeRecursively(filename, error)) { done(SyncFileItem::NormalError, error); return; } } else { QFile file(filename); if (file.exists() && !file.remove()) { done(SyncFileItem::NormalError, file.errorString()); return; } } emit progress(_item, 0); _propagator->_journal->deleteFileRecord(_item._originalFile, _item._isDirectory); _propagator->_journal->commit("Local remove"); done(SyncFileItem::Success); }
// Code copied from Qt5's QDir::removeRecursively // (and modified to report the error) static bool removeRecursively(const QString &path, QString &error) { bool success = true; QDirIterator di(path, QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot); while (di.hasNext()) { di.next(); const QFileInfo& fi = di.fileInfo(); bool ok; if (fi.isDir() && !fi.isSymLink()) { ok = removeRecursively(di.filePath(), error); // recursive } else { QFile f(di.filePath()); ok = f.remove(); if (!ok) { error += PropagateLocalRemove::tr("Error removing '%1': %2; "). arg(QDir::toNativeSeparators(f.fileName()), f.errorString()); qDebug() << "Error removing " << f.fileName() << ':' << f.errorString(); } } if (!ok) success = false; } if (success) { success = QDir().rmdir(path); if (!success) { error += PropagateLocalRemove::tr("Could not remove directory '%1'; ") .arg(QDir::toNativeSeparators(path)); qDebug() << "Error removing directory" << path; } } return success; }
bool MaemoGlobal::removeRecursively(const QString &filePath, QString &error) { error.clear(); QFileInfo fileInfo(filePath); if (!fileInfo.exists()) return true; QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser); if (fileInfo.isDir()) { QDir dir(filePath); QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot); foreach (const QString &fileName, fileNames) { if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error)) return false; } dir.cdUp(); if (!dir.rmdir(fileInfo.fileName())) { error = tr("Failed to remove directory '%1'.") .arg(QDir::toNativeSeparators(filePath)); return false; } } else {
void CmaEvent::vitaEventDeleteObject(vita_event_t *event, int eventId) { qDebug("Event recieved in %s, code: 0x%x, id: %d", Q_FUNC_INFO, event->Code, eventId); QMutexLocker locker(&m_db->mutex); int ohfi = event->Param2; metadata_t metadata; if(!m_db->getObjectMetadata(ohfi, metadata)) { qWarning("OHFI %d not found", ohfi); VitaMTP_ReportResult(device, eventId, PTP_RC_VITA_Invalid_OHFI); return; } QString fullpath = m_db->getAbsolutePath(ohfi); qDebug() << QString("Deleting %1, OHFI: %2").arg(fullpath, QString::number(ohfi)); removeRecursively(fullpath); m_db->deleteEntry(ohfi); VitaMTP_ReportResult(device, eventId, PTP_RC_OK); }
bool PathUtils::deleteMyTemporaryDir(QString dirName) { QDir rootTempDir = QDir::tempPath(); QString appName = qApp->applicationName(); QRegularExpression re { "^" + QRegularExpression::escape(appName) + "\\-(?<pid>\\d+)\\-(?<timestamp>\\d+)$" }; auto match = re.match(dirName); auto pid = match.capturedRef("pid").toLongLong(); if (match.hasMatch() && rootTempDir.exists(dirName) && pid == qApp->applicationPid()) { auto absoluteDirPath = QDir(rootTempDir.absoluteFilePath(dirName)); bool success = absoluteDirPath.removeRecursively(); if (success) { qDebug() << " Removing temporary directory: " << absoluteDirPath.absolutePath(); } else { qDebug() << " Failed to remove temporary directory: " << absoluteDirPath.absolutePath(); } return success; } return false; }
void LinkedListAccount::removeRecursively(const Account* account) { if (account != NULL) { removeRecursively(account->number()); } }
quint16 CmaEvent::processAllObjects(metadata_t &parent_metadata, quint32 handle) { qDebug("Called %s, handle: %d, parent name: %s", Q_FUNC_INFO, handle, parent_metadata.name); char *name; int dataType; quint32 *p_handles; unsigned int p_len; if(VitaMTP_GetObject_Info(device, handle, &name, &dataType) != PTP_RC_OK) { qWarning("Cannot get object for handle %d", handle); return PTP_RC_VITA_Invalid_Data; } if(dataType & Folder) { if(VitaMTP_GetObject_Folder(device, handle, &p_handles, &p_len) != PTP_RC_OK) { qWarning("Cannot get folder handles for handle %d", handle); return PTP_RC_VITA_Invalid_Data; } } else { } int ohfi = m_db->getPathId(name, parent_metadata.ohfi); if(ohfi > 0) { const QString fullpath = m_db->getAbsolutePath(ohfi); qDebug() << "Deleting" << fullpath; removeRecursively(fullpath); m_db->deleteEntry(ohfi); } QString fullpath = m_db->getAbsolutePath(parent_metadata.ohfi); QDir dir(fullpath); if(dataType & Folder) { if(!dir.mkpath(name)) { qWarning("Cannot create directory: %s", name); free(name); return PTP_RC_VITA_Failed_Operate_Object; } } else { m_file = new QFile(dir.absoluteFilePath(name)); if(!m_file->open(QIODevice::WriteOnly)) { qWarning("Cannot write to %s", name); free(name); delete m_file; return PTP_RC_VITA_Invalid_Permission; } else { // the size gets ignored because we can also get it via info.size() uint64_t size; VitaMTP_GetObject_Callback(device, handle, &size, CmaEvent::writeCallback); m_file->close(); delete m_file; } } int new_ohfi = m_db->insertObjectEntry(fullpath, name, parent_metadata.ohfi); qDebug("Added object %s with OHFI %i to database", name, new_ohfi); free(name); if(dataType & Folder) { metadata_t folder_metadata; m_db->getObjectMetadata(new_ohfi, folder_metadata); folder_metadata.handle = handle; for(unsigned int i = 0; i < p_len; i++) { quint16 ret = processAllObjects(folder_metadata, p_handles[i]); if(ret != PTP_RC_OK) { qDebug("Deleteting object with OHFI %d", new_ohfi); m_db->deleteEntry(new_ohfi); return ret; } } } return PTP_RC_OK; }