Ticket #205: mtd_cleanup.patch

File mtd_cleanup.patch, 17.0 KB (added by Anduin Withers <awithers@…>, 20 years ago)
  • jobthread.cpp

     
    2424#include <qdir.h>
    2525#include <qapplication.h>
    2626#include <qdeepcopy.h>
     27#include <qprocess.h>
    2728
    2829#include <mythtv/mythcontext.h>
    2930#include <mythtv/mythdbcon.h>
     
    211212---------------------------------------------------------------------
    212213*/
    213214
     215namespace
     216{
     217    class MutexUnlocker {
     218      public:
     219        MutexUnlocker(QMutex *mutex) : m_mutex(mutex)
     220        {
     221            if (!(m_mutex && m_mutex->locked()))
     222            {
     223                cerr << __FILE__ << ": Invalid mutext passed to MutexUnlocker"
     224                        << endl;
     225            }
     226        }
     227
     228        ~MutexUnlocker()
     229        {
     230            m_mutex->unlock();
     231        }
     232
     233      private:
     234        QMutex *m_mutex;
     235    };
     236
     237    template <typename HTYPE, typename CLEANF_RET = void>
     238    class HandleCloser
     239    {
     240        typedef CLEANF_RET (*clean_fun_t)(HTYPE);
     241
     242      public:
     243        HandleCloser(HTYPE handle, clean_fun_t cleaner) : m_handle(handle),
     244                m_cleaner(cleaner)
     245        {
     246        }
     247
     248        ~HandleCloser() {
     249            if (m_handle)
     250            {
     251                m_cleaner(m_handle);
     252            }
     253        }
     254
     255        HTYPE getHandle()
     256        {
     257            return m_handle;
     258        }
     259
     260      private:
     261        HTYPE m_handle;
     262        clean_fun_t m_cleaner;
     263    };
     264}
     265
    214266DVDThread::DVDThread(MTD *owner,
    215267                     QMutex *drive_mutex,
    216268                     const QString &dvd_device,
     
    225277    dvd_device_location = dvd_device;
    226278    dvd_title = track - 1;
    227279    destination_file_string = dest_file;
    228     ripfile = NULL;
    229280    rip_name = name;
    230281}
    231282
     
    269320        }
    270321    }   
    271322
     323    MutexUnlocker qmul(dvd_device_access);
     324
    272325    if(!keepGoing())
    273326    {
    274327        problem("abandoned job because master control said we need to shut down");
    275         dvd_device_access->unlock();
    276328        return false;
    277329    }
    278330
     
    284336    //  Lets open our destination file
    285337    //
    286338   
    287     if(ripfile)
     339    RipFile ripfile(to_location, extension, true);
     340    if(!ripfile.open(IO_WriteOnly | IO_Raw | IO_Truncate, multiple_files))
    288341    {
    289         cerr << "jobthread.o: How is it that you already have a ripfile set?" << endl;
    290         delete ripfile;
    291     }
    292    
    293     ripfile = new RipFile(to_location, extension);
    294     if(!ripfile->open(IO_WriteOnly | IO_Raw | IO_Truncate, multiple_files))
    295     {
    296         problem(QString("DVDPerfectThread could not open output file: %1").arg(ripfile->name()));
    297         dvd_device_access->unlock();
     342        problem(QString("DVDPerfectThread could not open output file: %1")
     343                .arg(ripfile.name()));
    298344        return false;
    299345    }
    300346
     
    305351   
    306352    int angle = 0;  // Change that at some point
    307353
    308     the_dvd = DVDOpen(dvd_device_location);
     354    HandleCloser<dvd_reader_t *> the_dvd_closer(DVDOpen(dvd_device_location),
     355            DVDClose);
     356    dvd_reader_t *the_dvd = the_dvd_closer.getHandle();
    309357    if(!the_dvd)
    310358    {
    311359        problem(QString("DVDPerfectThread could not access this dvd device: %1").arg(dvd_device_location));
    312         ripfile->remove();
    313         delete ripfile;
    314         ripfile = NULL;
    315         dvd_device_access->unlock();
    316360        return false;
    317361    }
    318362
    319363
    320     ifo_handle_t *vmg_file = ifoOpen( the_dvd, 0);
     364    HandleCloser<ifo_handle_t *> vmg_file_closer(ifoOpen(the_dvd, 0),
     365            ifoClose);
     366    ifo_handle_t *vmg_file = vmg_file_closer.getHandle();
    321367    if(!vmg_file)
    322368    {
    323369        problem("DVDPerfectThread could not open VMG info.");
    324         ripfile->remove();
    325         delete ripfile;
    326         ripfile = NULL;
    327         DVDClose(the_dvd);
    328         dvd_device_access->unlock();
    329370        return false;
    330371    }
    331372    tt_srpt_t *tt_srpt = vmg_file->tt_srpt;
     
    337378    if(title_number < 0 || title_number > tt_srpt->nr_of_srpts )
    338379    {
    339380        problem(QString("DVDPerfectThread could not open title number %1").arg(title_number + 1));
    340         ripfile->remove();
    341         delete ripfile;
    342         ripfile = NULL;
    343         ifoClose(vmg_file);
    344         DVDClose(the_dvd);
    345         dvd_device_access->unlock();
    346381        return false;
    347382    }
    348383
    349     ifo_handle_t *vts_file = ifoOpen(the_dvd, tt_srpt->title[title_number].title_set_nr);
     384    HandleCloser<ifo_handle_t *> vts_file_cleaner(
     385            ifoOpen(the_dvd, tt_srpt->title[title_number].title_set_nr),
     386            ifoClose);
     387    ifo_handle_t *vts_file = vts_file_cleaner.getHandle();
    350388    if(!vts_file)
    351389    {
    352390        problem("DVDPerfectThread could not open the title's info file");
    353         ripfile->remove();
    354         delete ripfile;
    355         ripfile = NULL;
    356         ifoClose(vmg_file);
    357         DVDClose(the_dvd);
    358         dvd_device_access->unlock();
    359391        return false;
    360392    }
    361393
     
    388420    //  OK ... now actually open
    389421    //   
    390422   
    391     title = DVDOpenFile(the_dvd,
    392                         tt_srpt->title[ title_number ].title_set_nr,
    393                         DVD_READ_TITLE_VOBS );
     423    HandleCloser<dvd_file_t *> title_closer(DVDOpenFile(the_dvd,
     424                    tt_srpt->title[ title_number ].title_set_nr,
     425                    DVD_READ_TITLE_VOBS), DVDCloseFile);
     426    dvd_file_t *title = title_closer.getHandle();
    394427    if(!title)
    395428    {
    396429        problem("DVDPerfectThread could not open the title's actual VOB(s)");
    397         ripfile->remove();
    398         delete ripfile;
    399         ripfile = NULL;
    400         ifoClose(vts_file);
    401         ifoClose(vmg_file);
    402         DVDClose(the_dvd);
    403         dvd_device_access->unlock();
    404430        return false;
    405431    }
    406432   
     
    408434
    409435    QTime job_time;
    410436    job_time.start();
     437    unsigned char video_data[ 1024 * DVD_VIDEO_LB_LEN ];
    411438   
    412439    int next_cell = start_cell;   
    413440    for(int cur_cell = start_cell; next_cell < cur_pgc->nr_of_cells; )
     
    449476            if( len != 1)
    450477            {
    451478                problem(QString("DVDPerfectThread read failed for block %1").arg(cur_pack));
    452                 ifoClose(vts_file);
    453                 ifoClose(vmg_file);
    454                 DVDCloseFile( title );
    455                 DVDClose(the_dvd);
    456                 dvd_device_access->unlock();
    457                 ripfile->remove();
    458                 delete ripfile;
    459                 ripfile = NULL;
    460479                return false;
    461480            }
    462481           
     
    503522                        .arg(cur_output_size)
    504523                        .arg(cur_pack)
    505524                       );
    506                 ripfile->remove();
    507                 delete ripfile;
    508                 ripfile = NULL;
    509                 ifoClose(vts_file);
    510                 ifoClose(vmg_file);
    511                 DVDCloseFile( title );
    512                 DVDClose(the_dvd);
    513                 dvd_device_access->unlock();
    514525                return false;
    515526            }
    516527           
     
    518529            overall_progress = subjob_progress * sub_to_overall_multiple;
    519530            updateSubjobString(job_time.elapsed() / 1000,
    520531                               QObject::tr("Ripping to file ~"));
    521             if(!ripfile->writeBlocks(video_data, cur_output_size * DVD_VIDEO_LB_LEN))
     532            if(!ripfile.writeBlocks(video_data, cur_output_size * DVD_VIDEO_LB_LEN))
    522533            {
    523534                problem("Couldn't write blocks during a rip. Filesystem size exceeded? Disc full?");
    524                 ripfile->remove();
    525                 delete ripfile;
    526                 ripfile = NULL;
    527                 DVDCloseFile( title );
    528                 ifoClose(vts_file);
    529                 ifoClose(vmg_file);
    530                 DVDClose(the_dvd);
    531                 dvd_device_access->unlock();
    532535                return false;
    533536            }
    534537
     
    544547            if(!keepGoing())
    545548            {
    546549                problem("abandoned job because master control said we need to shut down");
    547                 ripfile->remove();
    548                 delete ripfile;
    549                 ripfile = NULL;
    550                 DVDCloseFile( title );
    551                 ifoClose(vts_file);
    552                 ifoClose(vmg_file);
    553                 DVDClose(the_dvd);
    554                 dvd_device_access->unlock();
    555550                return false;
    556551            }
    557552        }
     
    561556    //  Wow, we're done.
    562557    //
    563558
    564     ifoClose(vts_file);
    565     ifoClose(vmg_file);
    566     DVDCloseFile( title );
    567     DVDClose(the_dvd);
    568     ripfile->close();
    569     dvd_device_access->unlock();
     559    ripfile.close();
    570560    sendLoggingEvent("job thread finished ripping dvd title");
    571561    return true;
    572562}
    573563
    574564DVDThread::~DVDThread()
    575565{
    576     if(ripfile);
    577     {
    578         delete ripfile;
    579     }
    580566}
    581567
    582568/*
     
    645631        }
    646632    }
    647633
     634    MutexUnlocker qmul(dvd_device_access);
    648635    if(!keepGoing())
    649636    {
    650637        problem("abandoned job because master control said we need to shut down");
    651         dvd_device_access->unlock();
    652638        return false;
    653639    }
    654640
    655     ripfile = new RipFile(destination_file_string, ".iso");
    656     if(!ripfile->open(IO_WriteOnly | IO_Raw | IO_Truncate, false))
     641    RipFile ripfile(destination_file_string, ".iso", true);
     642    if(!ripfile.open(IO_WriteOnly | IO_Raw | IO_Truncate, false))
    657643    {
    658         problem(QString("DVDISOCopyThread could not open output file: %1").arg(ripfile->name()));
    659         dvd_device_access->unlock();
     644        problem(QString("DVDISOCopyThread could not open output file: %1")
     645                .arg(ripfile.name()));
    660646        return false;
    661647    }
    662648
    663     sendLoggingEvent(QString("ISO DVD image copy to: %1").arg(ripfile->name()));
     649    sendLoggingEvent(QString("ISO DVD image copy to: %1").arg(ripfile.name()));
    664650
    665651    int file = open( dvd_device_location, O_RDONLY );
    666652    if(file == -1)
    667653    {
    668654        problem(QString("DVDISOCopyThread could not open dvd device: %1").arg(dvd_device_location));
    669         ripfile->remove();
    670         delete ripfile;
    671         ripfile = NULL;
    672         dvd_device_access->unlock();
    673655        return false;
    674656    }
    675657
     
    690672        {
    691673            perror("read");
    692674            problem(QString("DVDISOCopyThread dvd device read error"));
    693             ripfile->remove();
    694             if (ripfile)
    695             {
    696                 delete ripfile;
    697                 ripfile = NULL;
    698             }
    699             dvd_device_access->unlock();
    700675            return false;
    701676        }
    702677        if(bytes_read == 0)
     
    704679            break;
    705680        }
    706681
    707         if(!ripfile->writeBlocks(buffer, bytes_read))
     682        if(!ripfile.writeBlocks(buffer, bytes_read))
    708683        {
    709684            problem(QString("DVDISOCopyThread rip file write error"));
    710             ripfile->remove();
    711             if (ripfile)
    712             {
    713                 delete ripfile;
    714                 ripfile = NULL;
    715             }               
    716             dvd_device_access->unlock();
    717685            return false;
    718686        }
    719687
     
    732700        if(!keepGoing())
    733701        {
    734702            problem("abandoned job because master control said we need to shut down");
    735             ripfile->remove();
    736             if (ripfile)
    737             {
    738                 delete ripfile;
    739                 ripfile = NULL;
    740             }               
    741             dvd_device_access->unlock();
    742703            return false;
    743704        }
    744705    }
    745706
    746     ripfile->close();
    747     if (ripfile)
    748     {
    749         delete ripfile;
    750         ripfile = NULL;
    751     }       
    752     dvd_device_access->unlock();
     707    ripfile.close();
    753708    sendLoggingEvent("job thread finished copying ISO image");
    754709    return true;
    755710}
     
    14511406    //
    14521407    if(working_directory)
    14531408    {
    1454         if(ripfile)
    1455         {
    1456             ripfile->remove();
    1457             if (ripfile)
    1458             {
    1459                 delete ripfile;
    1460                 ripfile = NULL;
    1461             }               
    1462         }
    1463            
    14641409        if(two_pass)
    14651410        {
    14661411            working_directory->remove("twopass.log");
     
    14951440    {
    14961441        delete tc_process;
    14971442    }
    1498    
    1499     if(ripfile)
    1500     {
    1501         delete ripfile;
    1502     }
    15031443}
    1504 
  • dvdprobe.cpp

     
    2121#include <mythtv/mythcontext.h>
    2222#include <mythtv/mythdbcon.h>
    2323
     24#include <dvdread/ifo_read.h>
    2425
    2526DVDSubTitle::DVDSubTitle(int subtitle_id, const QString &a_language)
    2627{
  • jobthread.h

     
    1212
    1313#include <qthread.h>
    1414#include <qstringlist.h>
    15 #include <qfile.h>
    16 #include <qprocess.h>
    17 #include <dvdread/dvd_reader.h>
    18 #include <dvdread/ifo_types.h>
    19 #include <dvdread/ifo_read.h>
    20 #include <dvdread/nav_read.h>
    2115
    2216#include "fileobs.h"
    2317
     
    113107                          const QString &extension,
    114108                          bool multiple_files);
    115109 
    116     RipFile      *ripfile;
    117110    QMutex       *dvd_device_access;
    118111    QString      dvd_device_location;
    119112    QString      destination_file_string;
    120113    int          dvd_title;
    121     dvd_reader_t *the_dvd;
    122     dvd_file_t   *title;
    123     unsigned char video_data[ 1024 * DVD_VIDEO_LB_LEN ];
    124114    QString      rip_name;
    125115};
    126116
     
    216206    int          quality;
    217207    QDir         *working_directory;
    218208    QStringList  tc_arguments;
    219     QProcess     *tc_process;
     209    class QProcess     *tc_process;
    220210    bool         two_pass;
    221211    int          audio_track;
    222212    int          length_in_seconds;
  • dvdprobe.h

     
    1313#include <qstring.h>
    1414#include <qptrlist.h>
    1515
    16 #include <dvdread/dvd_reader.h>
    17 #include <dvdread/ifo_read.h>
     16#include <dvdread/ifo_types.h>
    1817
    1918class DVDSubTitle
    2019{
  • serversocket.cpp

     
    88
    99*/
    1010
     11#include <qsocket.h>
     12
    1113#include <stdlib.h>
    1214#include <iostream>
    1315using namespace std;
  • fileobs.cpp

     
    1717#include "fileobs.h"
    1818#include "qdir.h"
    1919
    20 RipFile::RipFile(const QString &a_base, const QString &an_extension)
     20RipFile::RipFile(const QString &a_base, const QString &an_extension,
     21        bool auto_remove_bad) : base_name(a_base), extension(an_extension),
     22                                   auto_remove_bad_rips(auto_remove_bad)
    2123{
    2224    filesize = gContext->GetNumSetting("MTDRipSize", 0) * 1024 * 1024;
    23     base_name = a_base;
    24     extension = an_extension;
    2525    active_file = NULL;
    2626    files.clear();
    2727    files.setAutoDelete(true);
     
    4646
    4747void RipFile::close()
    4848{
     49    auto_remove_bad_rips = false;
    4950    if(active_file)
    5051    {
    5152        active_file->close();
     
    155156
    156157RipFile::~RipFile()
    157158{
     159    if (active_file && auto_remove_bad_rips)
     160    {
     161        remove();
     162    }
    158163    files.clear();
    159164}
    160 
    161 
  • serversocket.h

     
    1010
    1111*/
    1212
    13 #include <qsocket.h>
    1413#include <qserversocket.h>
    1514
     15class QSocket;
    1616
    17 
    1817class MTDServerSocket : public QServerSocket
    1918{
    2019
  • mtd.cpp

     
    1111#include <qstringlist.h>
    1212#include <qregexp.h>
    1313#include <qdir.h>
     14#include <qtimer.h>
    1415
    1516#include <mythtv/util.h>
    1617#include <mythtv/mythcontext.h>
  • fileobs.h

     
    2121
    2222  public:
    2323 
    24     RipFile(const QString &a_base, const QString &an_extension);
     24    RipFile(const QString &a_base, const QString &an_extension,
     25            bool auto_remove_bad);
    2526    ~RipFile();
    2627   
    2728    bool    open(int mode, bool multiple_files);
     
    4041    int             access_mode;
    4142    QPtrList<QFile> files;
    4243    bool            use_multiple_files;
     44    bool            auto_remove_bad_rips;
    4345};
    4446
    4547#endif  // fileobs_h_
  • mtd.h

     
    1111*/
    1212
    1313#include <qobject.h>
    14 #include <qstringlist.h>
    1514#include <qptrlist.h>
    16 #include <qtimer.h>
    1715
    1816#include "logging.h"
    1917#include "serversocket.h"
     
    2119#include "dvdprobe.h"
    2220#include "threadevents.h"
    2321
     22class QStringList;
     23class QTimer;
     24
    2425class DiscCheckingThread : public QThread
    2526{
    2627
  • logging.h

     
    99        Headers for logging object of the myth transcoding daemon
    1010
    1111*/
    12 #include <unistd.h>
    13 #include <iostream>
    14 using namespace std;
    1512
    1613#include <qobject.h>
    1714#include <qstring.h>
    1815#include <qfile.h>
    19 #include <qtextstream.h>
    2016
    2117
    2218class MTDLogger : public QObject