/
fscleanup.cpp
115 lines (105 loc) · 3.8 KB
/
fscleanup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/****************************************************************************
**
** This file is part of commhistory-daemon.
**
** Copyright (C) 2014 Jolla Ltd.
** Contact: Slava Monich <slava.monich@jolla.com>
**
** This library is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License version 2.1
** as published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
****************************************************************************/
#include "fscleanup.h"
#include "debug.h"
#include <CommHistory/commhistorydatabasepath.h>
#include <CommHistory/databaseio.h>
#include <CommHistory/constants.h>
#include <QDirIterator>
#include <QDBusConnection>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(lcFsCleanup, "commhistoryd.fscleanup", QtWarningMsg)
FsCleanup::FsCleanup(QObject* aParent) :
QObject(aParent)
{
QDBusConnection dbus(QDBusConnection::sessionBus());
dbus.connect(QString(), QString(), COMM_HISTORY_INTERFACE,
EVENT_DELETED_SIGNAL, this, SLOT(onEventDeleted(int)));
dbus.connect(QString(), QString(), COMM_HISTORY_INTERFACE,
GROUPS_DELETED_SIGNAL, this, SLOT(onGroupsDeleted(QList<int>)));
fullCleanup();
}
void FsCleanup::onEventDeleted(int aEventId)
{
CommHistory::DatabaseIO* io = CommHistory::DatabaseIO::instance();
if (!io->eventExists(aEventId)) {
qCDebug(lcFsCleanup) << "FsCleanup: Event" << aEventId << "deleted";
deleteFiles(aEventId);
} else {
// Ignore deleteEvent signals emitted by EventModel::moveEvent
qCDebug(lcFsCleanup) << "FsCleanup: Ignoring delete signal for" << aEventId;
}
}
void FsCleanup::onGroupsDeleted(QList<int> aGroupIds)
{
qCDebug(lcFsCleanup) << "FsCleanup:" << aGroupIds.count() << "group(s) deleted";
fullCleanup();
}
void FsCleanup::fullCleanup()
{
qCDebug(lcFsCleanup) << "FsCleanup: Running full cleanup";
CommHistory::DatabaseIO* io = CommHistory::DatabaseIO::instance();
QDirIterator it(CommHistoryDatabasePath::dataDir(),
QDir::Dirs | QDir::NoDotAndDotDot);
while (it.hasNext()) {
it.next();
bool ok = false;
int id = it.fileName().toInt(&ok);
if (ok && !io->eventExists(id)) {
deleteFiles(id);
}
}
qCDebug(lcFsCleanup) << "FsCleanup: Cleanup done";
}
void FsCleanup::deleteFiles(int aEventId)
{
removeDir(CommHistoryDatabasePath::dataDir(aEventId));
}
bool FsCleanup::removeDir(QString aDirPath)
{
bool result = true;
QDir dir(aDirPath);
if (dir.exists()) {
qCDebug(lcFsCleanup) << "FsCleanup: Removing" << aDirPath;
QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot |
QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files,
QDir::DirsFirst);
for (int i=0; i<list.count(); i++) {
QFileInfo info(list.at(i));
QString path(info.absoluteFilePath());
if (info.isDir()) {
result = removeDir(path);
} else {
result = QFile::remove(path);
}
if (!result) {
qWarning() << "FsCleanup: Failed to remove" << path;
return result;
}
}
result = dir.rmdir(aDirPath);
if (!result) {
qWarning() << "FsCleanup: Failed to remove" << aDirPath;
}
}
return result;
}