@@ -17,34 +17,51 @@ static QByteArray calcHash(QString procDir) {
17
17
return hash.result ();
18
18
}
19
19
20
- static bool isDigit (QString s) {
21
- for (auto i : s) {
22
- if (!isdigit (i.toLatin1 ())) {
20
+ bool check () {
21
+ QFile lockFile = QFile (LOCK_FILE);
22
+ if (! lockFile.exists ()) {
23
+ if (!lockFile.open (QIODevice::WriteOnly | QIODevice::Text | QIODevice::NewOnly)) {
24
+ QMessageBox::critical (nullptr , " Error" , " Cannot open lock file!" );
25
+ QCoreApplication::exit (1 );
26
+ }
27
+ lockFile.write (QByteArray ().fromStdString (std::to_string (THIS_PID)));
28
+ lockFile.close ();
29
+ return false ;
30
+ } else {
31
+ // if the lock file exists, there are 2 situations:
32
+ // 1. another process is running
33
+ // 2. another process didn't exit properly and didn't delete the lock file
34
+ // So we need to check if the pid in the lock file is exists and represents a QDesktopPet instance.
35
+ if (!lockFile.open (QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
36
+ QMessageBox::critical (nullptr , " Error" , " Cannot open lock file!" );
37
+ QCoreApplication::exit (1 );
38
+ }
39
+ int pid = lockFile.readLine ().toInt ();
40
+ QDir processDir = QDir (QString (" /proc/$1" ).arg (pid));
41
+ if (! processDir.exists ()) {
42
+ // situation 2 and the process doesn't exist. we just need to update the pid
43
+ lockFile.write (QByteArray ().fromStdString (std::to_string (THIS_PID)));
44
+ lockFile.close ();
23
45
return false ;
46
+ } else {
47
+ // check if the process is a QDesktopPet instance
48
+ QDir thisProcess = QDir (QString (" /proc/$1" ).arg (THIS_PID));
49
+ if (calcHash (thisProcess.absolutePath ()) == calcHash (processDir.absolutePath ())) {
50
+ // situation 1, process exists and process is a QDesktopPet instance
51
+ lockFile.close ();
52
+ return true ;
53
+ } else {
54
+ // situation 2 and the process is not a QDesktopPet instance
55
+ // update the pid and continue
56
+ lockFile.write (QByteArray ().fromStdString (std::to_string (THIS_PID)));
57
+ lockFile.close ();
58
+ return false ;
59
+ }
24
60
}
25
61
}
26
- return true ;
27
62
}
28
63
29
- bool check () {
30
- qint64 selfPid = QCoreApplication::applicationPid ();
31
- std::cout << selfPid << std::endl;
32
- QString procDir = " /proc/" ;
33
- QString selfProcDir = procDir + QString::number (selfPid);
34
- QByteArray selfMD5 = calcHash (selfProcDir);
35
- QDirIterator iter (" /proc" , QDirIterator::Subdirectories);
36
- while (iter.hasNext ()) {
37
- QString aProcDir = iter.next ();
38
- if (isDigit (aProcDir.right (aProcDir.lastIndexOf (' /' )))) {
39
- int pid = aProcDir.right (aProcDir.lastIndexOf (' /' )).toInt ();
40
- if (pid > 50 && pid < selfPid) {
41
- QByteArray procHash = calcHash (aProcDir);
42
- if (procHash == selfMD5) {
43
- std::cout<< aProcDir.toStdString () << std::endl;
44
- return true ;
45
- }
46
- }
47
- }
48
- }
49
- return false ;
64
+ void deleteLock () {
65
+ QFile lockFile = QFile (LOCK_FILE);
66
+ lockFile.remove ();
50
67
}
0 commit comments