-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
117 lines (105 loc) · 3.46 KB
/
main.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
116
117
#include <QCoreApplication>
#include <QLocalServer>
#include <QLocalSocket>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QCryptographicHash>
#define SOCKET_FILE "2safe.sock"
void computeHash(QString path);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto server = new QLocalServer();
QObject::connect(server, &QLocalServer::newConnection, [=](){
auto socket = server->nextPendingConnection();
while (socket->bytesAvailable() < 1) {
socket->waitForReadyRead();
}
QObject::connect(socket, &QLocalSocket::disconnected, &QLocalSocket::deleteLater);
if(!socket->isValid() || socket->bytesAvailable() < 1) {
return;
}
qDebug() << "Handling incoming connection";
QTextStream in(socket);
QString data(in.readAll());
qDebug() << "Data read:" << data;
/* JSON PARSE */
QJsonParseError json_error;
QJsonDocument message = QJsonDocument::fromJson(data.toLatin1(), &json_error);
if(json_error.error) {
qWarning() << "JSON error:" << json_error.errorString();
return;
}
/* LOGIC */
QString verb = message.object().value("verb").toString();
QString path = message.object().value("path").toString();
qDebug() << "Command:" << verb;
qDebug() << "Arg0:" << path;
if(verb == "hash") {
computeHash(path);
} else {
qWarning() << "Unknown command:" << verb;
}
/* ----- */
});
/* the file we trying to bind to */
QFile sock_file(QDir::tempPath() + QDir::separator() + SOCKET_FILE);
if(!server->listen(SOCKET_FILE)) {
/* remove old socket file */
if(sock_file.exists() && sock_file.remove()) {
/* retry bind */
if(!server->listen(SOCKET_FILE)) {
qWarning() << "Unable to bind socket to" << SOCKET_FILE << ", will quit";
delete server;
return -1;
} else {
qDebug() << "Local socket created at" << server->fullServerName();
}
} else {
/* somthing went wrong */
qWarning() << "Unable to bind socket on" << SOCKET_FILE
<< ", try to remove it:" << QFileInfo(sock_file).canonicalFilePath();
delete server;
return -1;
}
} else {
qDebug() << "Local socket created at" << server->fullServerName();
}
a.exec();
/* exit */
server->close();
if(sock_file.exists()) {
sock_file.remove();
}
delete server;
return 0;
}
void computeHash(QString path) {
QFile file(path);
QFile outfile(path + ".md5.txt");
if(!file.exists()) {
qWarning() << "No such file exist" << path;
return;
}
if(!file.open(QFile::ReadOnly)) {
qWarning() << "Unable to read file" << path;
return;
}
if(!outfile.open(QFile::WriteOnly)) {
qWarning() << "Unable to write hash file" << QFileInfo(outfile).canonicalFilePath();
return;
}
qDebug() << "Computing MD5 hash of file" << path;
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(&file);
QString hexHashStr(hash.result().toHex());
qDebug() << "MD5:" << hexHashStr << "\n";
file.close();
QTextStream out(&outfile);
out << hexHashStr;
out.flush();
outfile.close();
}