OSDN Git Service

Updated CA certificates file for cURL.
[lamexp/LameXP.git] / src / Thread_DiskObserver.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2022 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Thread_DiskObserver.h"
24
25 //Internal
26 #include "Global.h"
27 #include "Model_Progress.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
32 #include <MUtils/Exception.h>
33
34 //Qt
35 #include <QDir>
36
37 #define MIN_DISKSPACE 104857600ui64 //100 MB
38
39 ////////////////////////////////////////////////////////////
40 // Constructor & Destructor
41 ////////////////////////////////////////////////////////////
42
43 DiskObserverThread::DiskObserverThread(const QString &path)
44 :
45         m_path(makeRootDir(path))
46 {
47 }
48
49 DiskObserverThread::~DiskObserverThread(void)
50 {
51 }
52
53 ////////////////////////////////////////////////////////////
54 // Protected functions
55 ////////////////////////////////////////////////////////////
56
57 void DiskObserverThread::run(void)
58 {
59         qDebug("DiskSpace observer started!");
60
61         try
62         {
63                 observe();
64         }
65         catch(const std::exception &error)
66         {
67                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
68                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
69         }
70         catch(...)
71         {
72                 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
73                 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
74         }
75
76         while(m_semaphore.available()) m_semaphore.tryAcquire();
77 }
78
79 void DiskObserverThread::observe(void)
80 {
81         quint64 minimumSpace = MIN_DISKSPACE;
82         quint64 previousSpace = quint64(-1);
83
84         forever
85         {
86                 quint64 freeSpace = 0ui64;
87                 if(MUtils::OS::free_diskspace(m_path, freeSpace))
88                 {
89                         if(freeSpace < minimumSpace)
90                         {
91                                 qWarning("Free diskspace on '%s' dropped below %s MB, only %s MB free!", MUTILS_UTF8(m_path), MUTILS_UTF8(QString::number(minimumSpace / 1048576ui64)), MUTILS_UTF8(QString::number(freeSpace / 1048576ui64)));
92                                 emit messageLogged(tr("Low diskspace on drive '%1' detected (only %2 MB are free), problems can occur!").arg(QDir::toNativeSeparators(m_path), QString::number(freeSpace / 1048576ui64)), ProgressModel::SysMsg_Warning);
93                                 minimumSpace = qMin(freeSpace, (minimumSpace >> 1));
94                         }
95                         if(freeSpace != previousSpace)
96                         {
97                                 emit freeSpaceChanged(freeSpace);
98                                 previousSpace = freeSpace;
99                         }
100                 }
101                 if(m_semaphore.tryAcquire(1, 2000)) break;
102         }
103 }
104
105 QString DiskObserverThread::makeRootDir(const QString &baseDir)
106 {
107         QDir dir(baseDir);
108         
109         if(!dir.exists())
110         {
111                 return baseDir;
112         }
113         
114         bool success = true;
115         
116         while(success)
117         {
118                 success = dir.cdUp();
119         }
120
121         return dir.canonicalPath();
122 }
123
124 ////////////////////////////////////////////////////////////
125 // SLOTS
126 ////////////////////////////////////////////////////////////
127
128 /*NONE*/
129
130 ////////////////////////////////////////////////////////////
131 // EVENTS
132 ////////////////////////////////////////////////////////////
133
134 /*NONE*/