~ubuntu-branches/debian/experimental/nzbget/experimental

« back to all changes in this revision

Viewing changes to daemon/postprocess/PrePostProcessor.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Moog
  • Date: 2014-12-25 12:58:06 UTC
  • mfrom: (1.2.1) (3.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20141225125806-vnzgajhm7mju9933
Tags: 14.1+dfsg-1
* New Upstream release (Closes: #768863)
* debian/patches:
  - Remove 0010_unnecessary_gcryptdep.patch, included upstream
  - Refresh remaining patches
* debian/control:
  - Remove no longer needed build-depends on libpar2-dev and libsigc++-dev
* debian/nzbget.conf
  - Update sample configuration file to include new options introduced by
    new upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of nzbget
 
3
 *
 
4
 *  Copyright (C) 2007-2014 Andrey Prygunkov <hugbug@users.sourceforge.net>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
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
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 *
 
20
 * $Revision: 1139 $
 
21
 * $Date: 2014-10-09 23:11:42 +0200 (Thu, 09 Oct 2014) $
 
22
 *
 
23
 */
 
24
 
 
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include "config.h"
 
28
#endif
 
29
 
 
30
#ifdef WIN32
 
31
#include "win32.h"
 
32
#endif
 
33
 
 
34
#include <stdlib.h>
 
35
#include <string.h>
 
36
#include <stdio.h>
 
37
#ifdef WIN32
 
38
#include <direct.h>
 
39
#else
 
40
#include <unistd.h>
 
41
#endif
 
42
#include <set>
 
43
#include <algorithm>
 
44
 
 
45
#include "nzbget.h"
 
46
#include "PrePostProcessor.h"
 
47
#include "Options.h"
 
48
#include "Log.h"
 
49
#include "HistoryCoordinator.h"
 
50
#include "DupeCoordinator.h"
 
51
#include "PostScript.h"
 
52
#include "Util.h"
 
53
#include "Scheduler.h"
 
54
#include "Scanner.h"
 
55
#include "Unpack.h"
 
56
#include "NZBFile.h"
 
57
#include "StatMeter.h"
 
58
#include "QueueScript.h"
 
59
 
 
60
extern HistoryCoordinator* g_pHistoryCoordinator;
 
61
extern DupeCoordinator* g_pDupeCoordinator;
 
62
extern Options* g_pOptions;
 
63
extern Scheduler* g_pScheduler;
 
64
extern Scanner* g_pScanner;
 
65
extern StatMeter* g_pStatMeter;
 
66
extern QueueScriptCoordinator* g_pQueueScriptCoordinator;
 
67
 
 
68
PrePostProcessor::PrePostProcessor()
 
69
{
 
70
        debug("Creating PrePostProcessor");
 
71
 
 
72
        m_iJobCount = 0;
 
73
        m_pCurJob = NULL;
 
74
        m_szPauseReason = NULL;
 
75
 
 
76
        m_DownloadQueueObserver.m_pOwner = this;
 
77
        DownloadQueue* pDownloadQueue = DownloadQueue::Lock();
 
78
        pDownloadQueue->Attach(&m_DownloadQueueObserver);
 
79
        DownloadQueue::Unlock();
 
80
}
 
81
 
 
82
PrePostProcessor::~PrePostProcessor()
 
83
{
 
84
        debug("Destroying PrePostProcessor");
 
85
}
 
86
 
 
87
void PrePostProcessor::Run()
 
88
{
 
89
        debug("Entering PrePostProcessor-loop");
 
90
 
 
91
        while (!DownloadQueue::IsLoaded())
 
92
        {
 
93
                usleep(20 * 1000);
 
94
        }
 
95
 
 
96
        if (g_pOptions->GetServerMode() && g_pOptions->GetSaveQueue() && g_pOptions->GetReloadQueue())
 
97
        {
 
98
                DownloadQueue* pDownloadQueue = DownloadQueue::Lock();
 
99
                SanitisePostQueue(pDownloadQueue);
 
100
                DownloadQueue::Unlock();
 
101
        }
 
102
 
 
103
        g_pScheduler->FirstCheck();
 
104
 
 
105
        int iDiskSpaceInterval = 1000;
 
106
        int iSchedulerInterval = 1000;
 
107
        int iHistoryInterval = 600000;
 
108
        const int iStepMSec = 200;
 
109
 
 
110
        while (!IsStopped())
 
111
        {
 
112
                // check incoming nzb directory
 
113
                g_pScanner->Check();
 
114
 
 
115
                if (!g_pOptions->GetPauseDownload() && 
 
116
                        g_pOptions->GetDiskSpace() > 0 && !g_pStatMeter->GetStandBy() && 
 
117
                        iDiskSpaceInterval >= 1000)
 
118
                {
 
119
                        // check free disk space every 1 second
 
120
                        CheckDiskSpace();
 
121
                        iDiskSpaceInterval = 0;
 
122
                }
 
123
                iDiskSpaceInterval += iStepMSec;
 
124
 
 
125
                // check post-queue every 200 msec
 
126
                CheckPostQueue();
 
127
 
 
128
                if (iSchedulerInterval >= 1000)
 
129
                {
 
130
                        // check scheduler tasks every 1 second
 
131
                        g_pScheduler->IntervalCheck();
 
132
                        iSchedulerInterval = 0;
 
133
                }
 
134
                iSchedulerInterval += iStepMSec;
 
135
 
 
136
                if (iHistoryInterval >= 600000)
 
137
                {
 
138
                        // check history (remove old entries) every 10 minutes
 
139
                        g_pHistoryCoordinator->IntervalCheck();
 
140
                        iHistoryInterval = 0;
 
141
                }
 
142
                iHistoryInterval += iStepMSec;
 
143
 
 
144
                Util::SetStandByMode(!m_pCurJob);
 
145
 
 
146
                usleep(iStepMSec * 1000);
 
147
        }
 
148
 
 
149
        g_pHistoryCoordinator->Cleanup();
 
150
 
 
151
        debug("Exiting PrePostProcessor-loop");
 
152
}
 
153
 
 
154
void PrePostProcessor::Stop()
 
155
{
 
156
        Thread::Stop();
 
157
        DownloadQueue::Lock();
 
158
 
 
159
#ifndef DISABLE_PARCHECK
 
160
        m_ParCoordinator.Stop();
 
161
#endif
 
162
 
 
163
        if (m_pCurJob && m_pCurJob->GetPostInfo() &&
 
164
                (m_pCurJob->GetPostInfo()->GetStage() == PostInfo::ptUnpacking ||
 
165
                 m_pCurJob->GetPostInfo()->GetStage() == PostInfo::ptExecutingScript) && 
 
166
                m_pCurJob->GetPostInfo()->GetPostThread())
 
167
        {
 
168
                Thread* pPostThread = m_pCurJob->GetPostInfo()->GetPostThread();
 
169
                m_pCurJob->GetPostInfo()->SetPostThread(NULL);
 
170
                pPostThread->SetAutoDestroy(true);
 
171
                pPostThread->Stop();
 
172
        }
 
173
 
 
174
        DownloadQueue::Unlock();
 
175
}
 
176
 
 
177
void PrePostProcessor::DownloadQueueUpdate(Subject* Caller, void* Aspect)
 
178
{
 
179
        if (IsStopped())
 
180
        {
 
181
                return;
 
182
        }
 
183
 
 
184
        DownloadQueue::Aspect* pQueueAspect = (DownloadQueue::Aspect*)Aspect;
 
185
        if (pQueueAspect->eAction == DownloadQueue::eaNzbFound)
 
186
        {
 
187
                NZBFound(pQueueAspect->pDownloadQueue, pQueueAspect->pNZBInfo);
 
188
        }
 
189
        else if (pQueueAspect->eAction == DownloadQueue::eaNzbAdded)
 
190
        {
 
191
                NZBAdded(pQueueAspect->pDownloadQueue, pQueueAspect->pNZBInfo);
 
192
        }
 
193
        else if (pQueueAspect->eAction == DownloadQueue::eaNzbDeleted &&
 
194
                pQueueAspect->pNZBInfo->GetDeleting() &&
 
195
                !pQueueAspect->pNZBInfo->GetPostInfo() &&
 
196
                !pQueueAspect->pNZBInfo->GetParCleanup() &&
 
197
                pQueueAspect->pNZBInfo->GetFileList()->empty())
 
198
        {
 
199
                // the deleting of nzbs is usually handled via eaFileDeleted-event, but when deleting nzb without
 
200
                // any files left the eaFileDeleted-event is not fired and we need to process eaNzbDeleted-event instead
 
201
                info("Collection %s deleted from queue", pQueueAspect->pNZBInfo->GetName());
 
202
                NZBDeleted(pQueueAspect->pDownloadQueue, pQueueAspect->pNZBInfo);
 
203
        }
 
204
        else if ((pQueueAspect->eAction == DownloadQueue::eaFileCompleted ||
 
205
                pQueueAspect->eAction == DownloadQueue::eaFileDeleted))
 
206
        {
 
207
                if (pQueueAspect->eAction == DownloadQueue::eaFileCompleted && !pQueueAspect->pNZBInfo->GetPostInfo())
 
208
                {
 
209
                        g_pQueueScriptCoordinator->EnqueueScript(pQueueAspect->pNZBInfo, QueueScriptCoordinator::qeFileDownloaded);
 
210
                }
 
211
 
 
212
                if (
 
213
#ifndef DISABLE_PARCHECK
 
214
                        !m_ParCoordinator.AddPar(pQueueAspect->pFileInfo, pQueueAspect->eAction == DownloadQueue::eaFileDeleted) &&
 
215
#endif
 
216
                        IsNZBFileCompleted(pQueueAspect->pNZBInfo, true, false) &&
 
217
                        !pQueueAspect->pNZBInfo->GetPostInfo() &&
 
218
                        (!pQueueAspect->pFileInfo->GetPaused() || IsNZBFileCompleted(pQueueAspect->pNZBInfo, false, false)))
 
219
                {
 
220
                        if ((pQueueAspect->eAction == DownloadQueue::eaFileCompleted ||
 
221
                                (pQueueAspect->pFileInfo->GetAutoDeleted() &&
 
222
                                 IsNZBFileCompleted(pQueueAspect->pNZBInfo, false, true))) &&
 
223
                                 pQueueAspect->pFileInfo->GetNZBInfo()->GetDeleteStatus() != NZBInfo::dsHealth)
 
224
                        {
 
225
                                info("Collection %s completely downloaded", pQueueAspect->pNZBInfo->GetName());
 
226
                                g_pQueueScriptCoordinator->EnqueueScript(pQueueAspect->pNZBInfo, QueueScriptCoordinator::qeNzbDownloaded);
 
227
                                NZBDownloaded(pQueueAspect->pDownloadQueue, pQueueAspect->pNZBInfo);
 
228
                        }
 
229
                        else if ((pQueueAspect->eAction == DownloadQueue::eaFileDeleted ||
 
230
                                (pQueueAspect->eAction == DownloadQueue::eaFileCompleted &&
 
231
                                 pQueueAspect->pFileInfo->GetNZBInfo()->GetDeleteStatus() > NZBInfo::dsNone)) &&
 
232
                                !pQueueAspect->pNZBInfo->GetParCleanup() &&
 
233
                                IsNZBFileCompleted(pQueueAspect->pNZBInfo, false, true))
 
234
                        {
 
235
                                info("Collection %s deleted from queue", pQueueAspect->pNZBInfo->GetName());
 
236
                                NZBDeleted(pQueueAspect->pDownloadQueue, pQueueAspect->pNZBInfo);
 
237
                        }
 
238
                }
 
239
        }
 
240
}
 
241
 
 
242
void PrePostProcessor::NZBFound(DownloadQueue* pDownloadQueue, NZBInfo* pNZBInfo)
 
243
{
 
244
        if (g_pOptions->GetDupeCheck() && pNZBInfo->GetDupeMode() != dmForce)
 
245
        {
 
246
                g_pDupeCoordinator->NZBFound(pDownloadQueue, pNZBInfo);
 
247
        }
 
248
}
 
249
 
 
250
void PrePostProcessor::NZBAdded(DownloadQueue* pDownloadQueue, NZBInfo* pNZBInfo)
 
251
{
 
252
        if (g_pOptions->GetParCheck() != Options::pcForce)
 
253
        {
 
254
                m_ParCoordinator.PausePars(pDownloadQueue, pNZBInfo);
 
255
        }
 
256
 
 
257
        if (g_pOptions->GetDupeCheck() && pNZBInfo->GetDupeMode() != dmForce &&
 
258
                pNZBInfo->GetDeleteStatus() == NZBInfo::dsDupe)
 
259
        {
 
260
                NZBCompleted(pDownloadQueue, pNZBInfo, false);
 
261
        }
 
262
        else
 
263
        {
 
264
                g_pQueueScriptCoordinator->EnqueueScript(pNZBInfo, QueueScriptCoordinator::qeNzbAdded);
 
265
        }
 
266
}
 
267
 
 
268
void PrePostProcessor::NZBDownloaded(DownloadQueue* pDownloadQueue, NZBInfo* pNZBInfo)
 
269
{
 
270
        if (!pNZBInfo->GetPostInfo() && g_pOptions->GetDecode())
 
271
        {
 
272
                info("Queueing %s for post-processing", pNZBInfo->GetName());
 
273
 
 
274
                pNZBInfo->EnterPostProcess();
 
275
                m_iJobCount++;
 
276
 
 
277
                if (pNZBInfo->GetParStatus() == NZBInfo::psNone &&
 
278
                        g_pOptions->GetParCheck() != Options::pcAlways &&
 
279
                        g_pOptions->GetParCheck() != Options::pcForce)
 
280
                {
 
281
                        pNZBInfo->SetParStatus(NZBInfo::psSkipped);
 
282
                }
 
283
 
 
284
                if (pNZBInfo->GetRenameStatus() == NZBInfo::rsNone && !g_pOptions->GetParRename())
 
285
                {
 
286
                        pNZBInfo->SetRenameStatus(NZBInfo::rsSkipped);
 
287
                }
 
288
 
 
289
                pDownloadQueue->Save();
 
290
        }
 
291
        else
 
292
        {
 
293
                NZBCompleted(pDownloadQueue, pNZBInfo, true);
 
294
        }
 
295
}
 
296
 
 
297
void PrePostProcessor::NZBDeleted(DownloadQueue* pDownloadQueue, NZBInfo* pNZBInfo)
 
298
{
 
299
        if (pNZBInfo->GetDeleteStatus() == NZBInfo::dsNone)
 
300
        {
 
301
                pNZBInfo->SetDeleteStatus(NZBInfo::dsManual);
 
302
        }
 
303
        pNZBInfo->SetDeleting(false);
 
304
 
 
305
        DeleteCleanup(pNZBInfo);
 
306
 
 
307
        if (pNZBInfo->GetDeleteStatus() == NZBInfo::dsHealth ||
 
308
                pNZBInfo->GetDeleteStatus() == NZBInfo::dsBad)
 
309
        {
 
310
                NZBDownloaded(pDownloadQueue, pNZBInfo);
 
311
        }
 
312
        else
 
313
        {
 
314
                NZBCompleted(pDownloadQueue, pNZBInfo, true);
 
315
        }
 
316
}
 
317
 
 
318
void PrePostProcessor::NZBCompleted(DownloadQueue* pDownloadQueue, NZBInfo* pNZBInfo, bool bSaveQueue)
 
319
{
 
320
        bool bAddToHistory = g_pOptions->GetKeepHistory() > 0 && !pNZBInfo->GetAvoidHistory();
 
321
        if (bAddToHistory)
 
322
        {
 
323
                g_pHistoryCoordinator->AddToHistory(pDownloadQueue, pNZBInfo);
 
324
        }
 
325
        pNZBInfo->SetAvoidHistory(false);
 
326
 
 
327
        bool bNeedSave = bAddToHistory;
 
328
 
 
329
        if (g_pOptions->GetDupeCheck() && pNZBInfo->GetDupeMode() != dmForce &&
 
330
                (pNZBInfo->GetDeleteStatus() == NZBInfo::dsNone ||
 
331
                 pNZBInfo->GetDeleteStatus() == NZBInfo::dsHealth ||
 
332
                 pNZBInfo->GetDeleteStatus() == NZBInfo::dsBad))
 
333
        {
 
334
                g_pDupeCoordinator->NZBCompleted(pDownloadQueue, pNZBInfo);
 
335
                bNeedSave = true;
 
336
        }
 
337
 
 
338
        if (!bAddToHistory)
 
339
        {
 
340
                g_pHistoryCoordinator->DeleteDiskFiles(pNZBInfo);
 
341
                pDownloadQueue->GetQueue()->Remove(pNZBInfo);
 
342
                delete pNZBInfo;
 
343
        }
 
344
 
 
345
        if (bSaveQueue && bNeedSave)
 
346
        {
 
347
                pDownloadQueue->Save();
 
348
        }
 
349
}
 
350
 
 
351
void PrePostProcessor::DeleteCleanup(NZBInfo* pNZBInfo)
 
352
{
 
353
        if ((g_pOptions->GetDeleteCleanupDisk() && pNZBInfo->GetCleanupDisk()) ||
 
354
                pNZBInfo->GetDeleteStatus() == NZBInfo::dsDupe)
 
355
        {
 
356
                // download was cancelled, deleting already downloaded files from disk
 
357
                for (CompletedFiles::reverse_iterator it = pNZBInfo->GetCompletedFiles()->rbegin(); it != pNZBInfo->GetCompletedFiles()->rend(); it++)
 
358
                {
 
359
                        CompletedFile* pCompletedFile = *it;
 
360
 
 
361
                        char szFullFileName[1024];
 
362
                        snprintf(szFullFileName, 1024, "%s%c%s", pNZBInfo->GetDestDir(), (int)PATH_SEPARATOR, pCompletedFile->GetFileName());
 
363
                        szFullFileName[1024-1] = '\0';
 
364
 
 
365
                        if (Util::FileExists(szFullFileName))
 
366
                        {
 
367
                                detail("Deleting file %s", pCompletedFile->GetFileName());
 
368
                                remove(szFullFileName);
 
369
                        }
 
370
                }
 
371
 
 
372
                // delete .out.tmp-files and _brokenlog.txt
 
373
                DirBrowser dir(pNZBInfo->GetDestDir());
 
374
                while (const char* szFilename = dir.Next())
 
375
                {
 
376
                        int iLen = strlen(szFilename);
 
377
                        if ((iLen > 8 && !strcmp(szFilename + iLen - 8, ".out.tmp")) || !strcmp(szFilename, "_brokenlog.txt"))
 
378
                        {
 
379
                                char szFullFilename[1024];
 
380
                                snprintf(szFullFilename, 1024, "%s%c%s", pNZBInfo->GetDestDir(), PATH_SEPARATOR, szFilename);
 
381
                                szFullFilename[1024-1] = '\0';
 
382
 
 
383
                                detail("Deleting file %s", szFilename);
 
384
                                remove(szFullFilename);
 
385
                        }
 
386
                }
 
387
        
 
388
                // delete old directory (if empty)
 
389
                if (Util::DirEmpty(pNZBInfo->GetDestDir()))
 
390
                {
 
391
                        rmdir(pNZBInfo->GetDestDir());
 
392
                }
 
393
        }
 
394
}
 
395
 
 
396
void PrePostProcessor::CheckDiskSpace()
 
397
{
 
398
        long long lFreeSpace = Util::FreeDiskSize(g_pOptions->GetDestDir());
 
399
        if (lFreeSpace > -1 && lFreeSpace / 1024 / 1024 < g_pOptions->GetDiskSpace())
 
400
        {
 
401
                warn("Low disk space on %s. Pausing download", g_pOptions->GetDestDir());
 
402
                g_pOptions->SetPauseDownload(true);
 
403
        }
 
404
 
 
405
        if (!Util::EmptyStr(g_pOptions->GetInterDir()))
 
406
        {
 
407
                lFreeSpace = Util::FreeDiskSize(g_pOptions->GetInterDir());
 
408
                if (lFreeSpace > -1 && lFreeSpace / 1024 / 1024 < g_pOptions->GetDiskSpace())
 
409
                {
 
410
                        warn("Low disk space on %s. Pausing download", g_pOptions->GetInterDir());
 
411
                        g_pOptions->SetPauseDownload(true);
 
412
                }
 
413
        }
 
414
}
 
415
 
 
416
void PrePostProcessor::CheckPostQueue()
 
417
{
 
418
        DownloadQueue* pDownloadQueue = DownloadQueue::Lock();
 
419
 
 
420
        if (!m_pCurJob && m_iJobCount > 0)
 
421
        {
 
422
                m_pCurJob = GetNextJob(pDownloadQueue);
 
423
        }
 
424
 
 
425
        if (m_pCurJob)
 
426
        {
 
427
                PostInfo* pPostInfo = m_pCurJob->GetPostInfo();
 
428
                if (!pPostInfo->GetWorking() && !IsNZBFileDownloading(m_pCurJob))
 
429
                {
 
430
#ifndef DISABLE_PARCHECK
 
431
                        if (pPostInfo->GetRequestParCheck() &&
 
432
                                (pPostInfo->GetNZBInfo()->GetParStatus() <= NZBInfo::psSkipped ||
 
433
                                 (pPostInfo->GetForceRepair() && !pPostInfo->GetNZBInfo()->GetParFull())) &&
 
434
                                g_pOptions->GetParCheck() != Options::pcManual)
 
435
                        {
 
436
                                pPostInfo->SetForceParFull(pPostInfo->GetNZBInfo()->GetParStatus() > NZBInfo::psSkipped);
 
437
                                pPostInfo->GetNZBInfo()->SetParStatus(NZBInfo::psNone);
 
438
                                pPostInfo->SetRequestParCheck(false);
 
439
                                pPostInfo->SetStage(PostInfo::ptQueued);
 
440
                                pPostInfo->GetNZBInfo()->GetScriptStatuses()->Clear();
 
441
                                DeletePostThread(pPostInfo);
 
442
                        }
 
443
                        else if (pPostInfo->GetRequestParCheck() && pPostInfo->GetNZBInfo()->GetParStatus() <= NZBInfo::psSkipped &&
 
444
                                g_pOptions->GetParCheck() == Options::pcManual)
 
445
                        {
 
446
                                pPostInfo->SetRequestParCheck(false);
 
447
                                pPostInfo->GetNZBInfo()->SetParStatus(NZBInfo::psManual);
 
448
                                DeletePostThread(pPostInfo);
 
449
 
 
450
                                if (!pPostInfo->GetNZBInfo()->GetFileList()->empty())
 
451
                                {
 
452
                                        info("Downloading all remaining files for manual par-check for %s", pPostInfo->GetNZBInfo()->GetName());
 
453
                                        pDownloadQueue->EditEntry(pPostInfo->GetNZBInfo()->GetID(), DownloadQueue::eaGroupResume, 0, NULL);
 
454
                                        pPostInfo->SetStage(PostInfo::ptFinished);
 
455
                                }
 
456
                                else
 
457
                                {
 
458
                                        info("There are no par-files remain for download for %s", pPostInfo->GetNZBInfo()->GetName());
 
459
                                        pPostInfo->SetStage(PostInfo::ptQueued);
 
460
                                }
 
461
                        }
 
462
                        
 
463
#endif
 
464
                        if (pPostInfo->GetDeleted())
 
465
                        {
 
466
                                pPostInfo->SetStage(PostInfo::ptFinished);
 
467
                        }
 
468
 
 
469
                        if (pPostInfo->GetStage() == PostInfo::ptQueued &&
 
470
                                (!g_pOptions->GetPausePostProcess() || pPostInfo->GetNZBInfo()->GetForcePriority()))
 
471
                        {
 
472
                                DeletePostThread(pPostInfo);
 
473
                                StartJob(pDownloadQueue, pPostInfo);
 
474
                        }
 
475
                        else if (pPostInfo->GetStage() == PostInfo::ptFinished)
 
476
                        {
 
477
                                UpdatePauseState(false, NULL);
 
478
                                JobCompleted(pDownloadQueue, pPostInfo);
 
479
                        }
 
480
                        else if (!g_pOptions->GetPausePostProcess())
 
481
                        {
 
482
                                error("Internal error: invalid state in post-processor");
 
483
                                // TODO: cancel (delete) current job
 
484
                        }
 
485
                }
 
486
        }
 
487
        
 
488
        DownloadQueue::Unlock();
 
489
}
 
490
 
 
491
NZBInfo* PrePostProcessor::GetNextJob(DownloadQueue* pDownloadQueue)
 
492
{
 
493
        NZBInfo* pNZBInfo = NULL;
 
494
 
 
495
        for (NZBList::iterator it = pDownloadQueue->GetQueue()->begin(); it != pDownloadQueue->GetQueue()->end(); it++)
 
496
        {
 
497
                NZBInfo* pNZBInfo1 = *it;
 
498
                if (pNZBInfo1->GetPostInfo() && !g_pQueueScriptCoordinator->HasJob(pNZBInfo1->GetID()) &&
 
499
                        (!pNZBInfo || pNZBInfo1->GetPriority() > pNZBInfo->GetPriority()) &&
 
500
                        (!g_pOptions->GetPausePostProcess() || pNZBInfo1->GetForcePriority()))
 
501
                {
 
502
                        pNZBInfo = pNZBInfo1;
 
503
                }
 
504
        }
 
505
 
 
506
        return pNZBInfo;
 
507
}
 
508
 
 
509
/**
 
510
 * Reset the state of items after reloading from disk and
 
511
 * delete items which could not be resumed.
 
512
 * Also count the number of post-jobs.
 
513
 */
 
514
void PrePostProcessor::SanitisePostQueue(DownloadQueue* pDownloadQueue)
 
515
{
 
516
        for (NZBList::iterator it = pDownloadQueue->GetQueue()->begin(); it != pDownloadQueue->GetQueue()->end(); it++)
 
517
        {
 
518
                NZBInfo* pNZBInfo = *it;
 
519
                PostInfo* pPostInfo = pNZBInfo->GetPostInfo();
 
520
                if (pPostInfo)
 
521
                {
 
522
                        m_iJobCount++;
 
523
                        if (pPostInfo->GetStage() == PostInfo::ptExecutingScript ||
 
524
                                !Util::DirectoryExists(pNZBInfo->GetDestDir()))
 
525
                        {
 
526
                                pPostInfo->SetStage(PostInfo::ptFinished);
 
527
                        }
 
528
                        else
 
529
                        {
 
530
                                pPostInfo->SetStage(PostInfo::ptQueued);
 
531
                        }
 
532
                }
 
533
        }
 
534
}
 
535
 
 
536
void PrePostProcessor::DeletePostThread(PostInfo* pPostInfo)
 
537
{
 
538
        delete pPostInfo->GetPostThread();
 
539
        pPostInfo->SetPostThread(NULL);
 
540
}
 
541
 
 
542
void PrePostProcessor::StartJob(DownloadQueue* pDownloadQueue, PostInfo* pPostInfo)
 
543
{
 
544
        if (!pPostInfo->GetStartTime())
 
545
        {
 
546
                pPostInfo->SetStartTime(time(NULL));
 
547
        }
 
548
 
 
549
#ifndef DISABLE_PARCHECK
 
550
        if (pPostInfo->GetNZBInfo()->GetRenameStatus() == NZBInfo::rsNone &&
 
551
                pPostInfo->GetNZBInfo()->GetDeleteStatus() == NZBInfo::dsNone)
 
552
        {
 
553
                UpdatePauseState(g_pOptions->GetParPauseQueue(), "par-rename");
 
554
                m_ParCoordinator.StartParRenameJob(pPostInfo);
 
555
                return;
 
556
        }
 
557
        else if (pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psNone &&
 
558
                pPostInfo->GetNZBInfo()->GetDeleteStatus() == NZBInfo::dsNone)
 
559
        {
 
560
                if (m_ParCoordinator.FindMainPars(pPostInfo->GetNZBInfo()->GetDestDir(), NULL))
 
561
                {
 
562
                        UpdatePauseState(g_pOptions->GetParPauseQueue(), "par-check");
 
563
                        m_ParCoordinator.StartParCheckJob(pPostInfo);
 
564
                }
 
565
                else
 
566
                {
 
567
                        info("Nothing to par-check for %s", pPostInfo->GetNZBInfo()->GetName());
 
568
                        pPostInfo->GetNZBInfo()->SetParStatus(NZBInfo::psSkipped);
 
569
                        pPostInfo->SetWorking(false);
 
570
                        pPostInfo->SetStage(PostInfo::ptQueued);
 
571
                }
 
572
                return;
 
573
        }
 
574
        else if (pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psSkipped &&
 
575
                pPostInfo->GetNZBInfo()->CalcHealth() < pPostInfo->GetNZBInfo()->CalcCriticalHealth(false) &&
 
576
                pPostInfo->GetNZBInfo()->CalcCriticalHealth(false) < 1000 &&
 
577
                m_ParCoordinator.FindMainPars(pPostInfo->GetNZBInfo()->GetDestDir(), NULL))
 
578
        {
 
579
                warn("Skipping par-check for %s due to health %.1f%% below critical %.1f%%", pPostInfo->GetNZBInfo()->GetName(),
 
580
                        pPostInfo->GetNZBInfo()->CalcHealth() / 10.0, pPostInfo->GetNZBInfo()->CalcCriticalHealth(false) / 10.0);
 
581
                pPostInfo->GetNZBInfo()->SetParStatus(NZBInfo::psFailure);
 
582
                return;
 
583
        }
 
584
        else if (pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psSkipped &&
 
585
                pPostInfo->GetNZBInfo()->GetFailedSize() - pPostInfo->GetNZBInfo()->GetParFailedSize() > 0 &&
 
586
                m_ParCoordinator.FindMainPars(pPostInfo->GetNZBInfo()->GetDestDir(), NULL))
 
587
        {
 
588
                info("Collection %s with health %.1f%% needs par-check",
 
589
                        pPostInfo->GetNZBInfo()->GetName(), pPostInfo->GetNZBInfo()->CalcHealth() / 10.0);
 
590
                pPostInfo->SetRequestParCheck(true);
 
591
                return;
 
592
        }
 
593
#endif
 
594
 
 
595
        NZBParameter* pUnpackParameter = pPostInfo->GetNZBInfo()->GetParameters()->Find("*Unpack:", false);
 
596
        bool bUnpackParam = !(pUnpackParameter && !strcasecmp(pUnpackParameter->GetValue(), "no"));
 
597
        bool bUnpack = bUnpackParam && pPostInfo->GetNZBInfo()->GetUnpackStatus() == NZBInfo::usNone &&
 
598
                pPostInfo->GetNZBInfo()->GetDeleteStatus() == NZBInfo::dsNone;
 
599
 
 
600
        bool bParFailed = pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psFailure ||
 
601
                pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psRepairPossible ||
 
602
                pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psManual;
 
603
 
 
604
        bool bCleanup = !bUnpack &&
 
605
                pPostInfo->GetNZBInfo()->GetCleanupStatus() == NZBInfo::csNone &&
 
606
                ((pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psSuccess &&
 
607
                  pPostInfo->GetNZBInfo()->GetUnpackStatus() != NZBInfo::usFailure &&
 
608
                  pPostInfo->GetNZBInfo()->GetUnpackStatus() != NZBInfo::usSpace &&
 
609
                  pPostInfo->GetNZBInfo()->GetUnpackStatus() != NZBInfo::usPassword) ||
 
610
                 (pPostInfo->GetNZBInfo()->GetUnpackStatus() == NZBInfo::usSuccess &&
 
611
                  pPostInfo->GetNZBInfo()->GetParStatus() != NZBInfo::psFailure)) &&
 
612
                !Util::EmptyStr(g_pOptions->GetExtCleanupDisk());
 
613
 
 
614
        bool bMoveInter = !bUnpack &&
 
615
                pPostInfo->GetNZBInfo()->GetMoveStatus() == NZBInfo::msNone &&
 
616
                pPostInfo->GetNZBInfo()->GetUnpackStatus() != NZBInfo::usFailure &&
 
617
                pPostInfo->GetNZBInfo()->GetUnpackStatus() != NZBInfo::usSpace &&
 
618
                pPostInfo->GetNZBInfo()->GetUnpackStatus() != NZBInfo::usPassword &&
 
619
                pPostInfo->GetNZBInfo()->GetParStatus() != NZBInfo::psFailure &&
 
620
                pPostInfo->GetNZBInfo()->GetParStatus() != NZBInfo::psManual &&
 
621
                pPostInfo->GetNZBInfo()->GetDeleteStatus() == NZBInfo::dsNone &&
 
622
                !Util::EmptyStr(g_pOptions->GetInterDir()) &&
 
623
                !strncmp(pPostInfo->GetNZBInfo()->GetDestDir(), g_pOptions->GetInterDir(), strlen(g_pOptions->GetInterDir()));
 
624
 
 
625
        bool bPostScript = true;
 
626
 
 
627
        if (bUnpack && bParFailed)
 
628
        {
 
629
                warn("Skipping unpack for %s due to %s", pPostInfo->GetNZBInfo()->GetName(),
 
630
                        pPostInfo->GetNZBInfo()->GetParStatus() == NZBInfo::psManual ? "required par-repair" : "par-failure");
 
631
                pPostInfo->GetNZBInfo()->SetUnpackStatus(NZBInfo::usSkipped);
 
632
                bUnpack = false;
 
633
        }
 
634
 
 
635
        if (!bUnpack && !bMoveInter && !bPostScript)
 
636
        {
 
637
                pPostInfo->SetStage(PostInfo::ptFinished);
 
638
                return;
 
639
        }
 
640
 
 
641
        pPostInfo->SetProgressLabel(bUnpack ? "Unpacking" : bMoveInter ? "Moving" : "Executing post-process-script");
 
642
        pPostInfo->SetWorking(true);
 
643
        pPostInfo->SetStage(bUnpack ? PostInfo::ptUnpacking : bMoveInter ? PostInfo::ptMoving : PostInfo::ptExecutingScript);
 
644
        pPostInfo->SetFileProgress(0);
 
645
        pPostInfo->SetStageProgress(0);
 
646
 
 
647
        pDownloadQueue->Save();
 
648
 
 
649
        pPostInfo->SetStageTime(time(NULL));
 
650
 
 
651
        if (bUnpack)
 
652
        {
 
653
                UpdatePauseState(g_pOptions->GetUnpackPauseQueue(), "unpack");
 
654
                UnpackController::StartJob(pPostInfo);
 
655
        }
 
656
        else if (bCleanup)
 
657
        {
 
658
                UpdatePauseState(g_pOptions->GetUnpackPauseQueue() || g_pOptions->GetScriptPauseQueue(), "cleanup");
 
659
                CleanupController::StartJob(pPostInfo);
 
660
        }
 
661
        else if (bMoveInter)
 
662
        {
 
663
                UpdatePauseState(g_pOptions->GetUnpackPauseQueue() || g_pOptions->GetScriptPauseQueue(), "move");
 
664
                MoveController::StartJob(pPostInfo);
 
665
        }
 
666
        else
 
667
        {
 
668
                UpdatePauseState(g_pOptions->GetScriptPauseQueue(), "post-process-script");
 
669
                PostScriptController::StartJob(pPostInfo);
 
670
        }
 
671
}
 
672
 
 
673
void PrePostProcessor::JobCompleted(DownloadQueue* pDownloadQueue, PostInfo* pPostInfo)
 
674
{
 
675
        NZBInfo* pNZBInfo = pPostInfo->GetNZBInfo();
 
676
 
 
677
        if (pPostInfo->GetStartTime() > 0)
 
678
        {
 
679
                pNZBInfo->SetPostTotalSec((int)(time(NULL) - pPostInfo->GetStartTime()));
 
680
                pPostInfo->SetStartTime(0);
 
681
        }
 
682
 
 
683
        DeletePostThread(pPostInfo);
 
684
        pNZBInfo->LeavePostProcess();
 
685
 
 
686
        if (IsNZBFileCompleted(pNZBInfo, true, false))
 
687
        {
 
688
                // Cleaning up queue if par-check was successful or unpack was successful or
 
689
                // health is 100% (if unpack and par-check were not performed)
 
690
                // or health is below critical health
 
691
                bool bCanCleanupQueue =
 
692
                        ((pNZBInfo->GetParStatus() == NZBInfo::psSuccess ||
 
693
                          pNZBInfo->GetParStatus() == NZBInfo::psRepairPossible) &&
 
694
                         pNZBInfo->GetUnpackStatus() != NZBInfo::usFailure &&
 
695
                         pNZBInfo->GetUnpackStatus() != NZBInfo::usSpace &&
 
696
                         pNZBInfo->GetUnpackStatus() != NZBInfo::usPassword) ||
 
697
                        (pNZBInfo->GetUnpackStatus() == NZBInfo::usSuccess &&
 
698
                         pNZBInfo->GetParStatus() != NZBInfo::psFailure) ||
 
699
                        (pNZBInfo->GetUnpackStatus() <= NZBInfo::usSkipped &&
 
700
                         pNZBInfo->GetParStatus() != NZBInfo::psFailure &&
 
701
                         pNZBInfo->GetFailedSize() - pNZBInfo->GetParFailedSize() == 0) ||
 
702
                        (pNZBInfo->CalcHealth() < pNZBInfo->CalcCriticalHealth(false) &&
 
703
                         pNZBInfo->CalcCriticalHealth(false) < 1000);
 
704
                if (g_pOptions->GetParCleanupQueue() && bCanCleanupQueue && !pNZBInfo->GetFileList()->empty())
 
705
                {
 
706
                        info("Cleaning up download queue for %s", pNZBInfo->GetName());
 
707
                        pNZBInfo->SetParCleanup(true);
 
708
                        pDownloadQueue->EditEntry(pNZBInfo->GetID(), DownloadQueue::eaGroupDelete, 0, NULL);
 
709
                }
 
710
 
 
711
                if (pNZBInfo->GetUnpackCleanedUpDisk())
 
712
                {
 
713
                        pNZBInfo->ClearCompletedFiles();
 
714
                }
 
715
 
 
716
                NZBCompleted(pDownloadQueue, pNZBInfo, false);
 
717
        }
 
718
 
 
719
        if (pNZBInfo == m_pCurJob)
 
720
        {
 
721
                m_pCurJob = NULL;
 
722
        }
 
723
        m_iJobCount--;
 
724
 
 
725
        pDownloadQueue->Save();
 
726
}
 
727
 
 
728
bool PrePostProcessor::IsNZBFileCompleted(NZBInfo* pNZBInfo, bool bIgnorePausedPars, bool bAllowOnlyOneDeleted)
 
729
{
 
730
        int iDeleted = 0;
 
731
 
 
732
        for (FileList::iterator it = pNZBInfo->GetFileList()->begin(); it != pNZBInfo->GetFileList()->end(); it++)
 
733
        {
 
734
                FileInfo* pFileInfo = *it;
 
735
                if (pFileInfo->GetDeleted())
 
736
                {
 
737
                        iDeleted++;
 
738
                }
 
739
                if (((!pFileInfo->GetPaused() || !bIgnorePausedPars || !pFileInfo->GetParFile()) &&
 
740
                        !pFileInfo->GetDeleted()) ||
 
741
                        (bAllowOnlyOneDeleted && iDeleted > 1))
 
742
                {
 
743
                        return false;
 
744
                }
 
745
        }
 
746
 
 
747
        return true;
 
748
}
 
749
 
 
750
bool PrePostProcessor::IsNZBFileDownloading(NZBInfo* pNZBInfo)
 
751
{
 
752
        if (pNZBInfo->GetActiveDownloads())
 
753
        {
 
754
                return true;
 
755
        }
 
756
 
 
757
        for (FileList::iterator it = pNZBInfo->GetFileList()->begin(); it != pNZBInfo->GetFileList()->end(); it++)
 
758
        {
 
759
                FileInfo* pFileInfo = *it;
 
760
                if (!pFileInfo->GetPaused())
 
761
                {
 
762
                        return true;
 
763
                }
 
764
        }
 
765
 
 
766
        return false;
 
767
}
 
768
 
 
769
void PrePostProcessor::UpdatePauseState(bool bNeedPause, const char* szReason)
 
770
{
 
771
        if (bNeedPause && !g_pOptions->GetTempPauseDownload())
 
772
        {
 
773
                info("Pausing download before %s", szReason);
 
774
        }
 
775
        else if (!bNeedPause && g_pOptions->GetTempPauseDownload())
 
776
        {
 
777
                info("Unpausing download after %s", m_szPauseReason);
 
778
        }
 
779
        g_pOptions->SetTempPauseDownload(bNeedPause);
 
780
        m_szPauseReason = szReason;
 
781
}
 
782
 
 
783
bool PrePostProcessor::EditList(DownloadQueue* pDownloadQueue, IDList* pIDList, DownloadQueue::EEditAction eAction, int iOffset, const char* szText)
 
784
{
 
785
        debug("Edit-command for post-processor received");
 
786
        switch (eAction)
 
787
        {
 
788
                case DownloadQueue::eaPostDelete:
 
789
                        return PostQueueDelete(pDownloadQueue, pIDList);
 
790
 
 
791
                default:
 
792
                        return false;
 
793
        }
 
794
}
 
795
 
 
796
bool PrePostProcessor::PostQueueDelete(DownloadQueue* pDownloadQueue, IDList* pIDList)
 
797
{
 
798
        bool bOK = false;
 
799
 
 
800
        for (IDList::iterator itID = pIDList->begin(); itID != pIDList->end(); itID++)
 
801
        {
 
802
                int iID = *itID;
 
803
 
 
804
                for (NZBList::iterator it = pDownloadQueue->GetQueue()->begin(); it != pDownloadQueue->GetQueue()->end(); it++)
 
805
                {
 
806
                        NZBInfo* pNZBInfo = *it;
 
807
                        PostInfo* pPostInfo = pNZBInfo->GetPostInfo();
 
808
                        if (pPostInfo && pNZBInfo->GetID() == iID)
 
809
                        {
 
810
                                if (pPostInfo->GetWorking())
 
811
                                {
 
812
                                        info("Deleting active post-job %s", pPostInfo->GetNZBInfo()->GetName());
 
813
                                        pPostInfo->SetDeleted(true);
 
814
#ifndef DISABLE_PARCHECK
 
815
                                        if (PostInfo::ptLoadingPars <= pPostInfo->GetStage() && pPostInfo->GetStage() <= PostInfo::ptRenaming)
 
816
                                        {
 
817
                                                if (m_ParCoordinator.Cancel())
 
818
                                                {
 
819
                                                        bOK = true;
 
820
                                                }
 
821
                                        }
 
822
                                        else
 
823
#endif
 
824
                                        if (pPostInfo->GetPostThread())
 
825
                                        {
 
826
                                                debug("Terminating %s for %s", (pPostInfo->GetStage() == PostInfo::ptUnpacking ? "unpack" : "post-process-script"), pPostInfo->GetNZBInfo()->GetName());
 
827
                                                pPostInfo->GetPostThread()->Stop();
 
828
                                                bOK = true;
 
829
                                        }
 
830
                                        else
 
831
                                        {
 
832
                                                error("Internal error in PrePostProcessor::QueueDelete");
 
833
                                        }
 
834
                                }
 
835
                                else
 
836
                                {
 
837
                                        info("Deleting queued post-job %s", pPostInfo->GetNZBInfo()->GetName());
 
838
                                        JobCompleted(pDownloadQueue, pPostInfo);
 
839
                                        bOK = true;
 
840
                                }
 
841
                                break;
 
842
                        }
 
843
                }
 
844
        }
 
845
 
 
846
        return bOK;
 
847
}