Ticket #6255: mythtv-6255-Storage_Group_fallback.patch

File mythtv-6255-Storage_Group_fallback.patch, 6.4 KB (added by sphery <mtdean@…>, 15 years ago)
  • libs/libmyth/storagegroup.cpp

    old new  
    3838    Init(m_groupname, m_hostname);
    3939}
    4040
     41/** \fn StorageGroup::Init(const QString, const QString)
     42 *  \brief Initilizes the groupname, hostname, and dirlist
     43 *
     44 *   First attempts to find the Storage Group defined with the specified name
     45 *   for the given host.  If not found, checks for the named Storage Group, as
     46 *   defined across all hosts.  If not found, tries the "Default" Storage Group
     47 *   for the given host.  If not found, tries the "Default" Storage Group, as
     48 *   defined across all hosts.
     49 *
     50 *  \param group    The name of the Storage Group
     51 *  \param hostname The host whose Storage Group definition is desired
     52 */
    4153void StorageGroup::Init(const QString group, const QString hostname)
    4254{
    43     QString dirname;
    44     MSqlQuery query(MSqlQuery::InitCon());
    45 
     55    bool found = false;
    4656    m_groupname = group;    m_groupname.detach();
    4757    m_hostname  = hostname; m_hostname.detach();
    4858    m_dirlist.clear();
    4959
    50     QString sql = "SELECT DISTINCT dirname "
    51                   "FROM storagegroup ";
    52 
    53     if (!m_groupname.isEmpty())
    54     {
    55         sql.append("WHERE groupname = :GROUP");
    56         if (!m_hostname.isEmpty())
    57             sql.append(" AND hostname = :HOSTNAME");
    58     }
    59 
    60     query.prepare(sql);
    61     if (!m_groupname.isEmpty())
     60    found = FindDirs(m_groupname, m_hostname);
     61    if ((!found) && (!hostname.isEmpty()))
    6262    {
    63         query.bindValue(":GROUP", m_groupname);
    64         if (!m_hostname.isEmpty())
    65             query.bindValue(":HOSTNAME", m_hostname);
     63        VERBOSE(VB_FILE, LOC +
     64                QString("Unable to find any directories for the local "
     65                        "storage group '%1' on '%2', trying directories on "
     66                        "all hosts!").arg(group).arg(hostname));
     67        found = FindDirs(m_groupname, "");
     68        if (found)
     69        {
     70            m_hostname = "";
     71            m_hostname.detach();
     72        }
    6673    }
    67 
    68     if (!query.exec() || !query.isActive())
    69         MythDB::DBError("StorageGroup::StorageGroup()", query);
    70     else if (!query.next())
     74    if ((!found) && (group != "Default"))
    7175    {
    72         if (group != "Default")
     76        VERBOSE(VB_FILE, LOC +
     77                QString("Unable to find storage group '%1', trying "
     78                        "'Default' group!").arg(group));
     79        found = FindDirs("Default", m_hostname);
     80        if(found)
    7381        {
    74             VERBOSE(VB_FILE, LOC +
    75                     QString("Unable to find storage group '%1', trying "
    76                             "'Default' group!").arg(m_groupname));
    77             Init("Default", m_hostname);
    78             return;
     82            m_groupname = "Default";
     83            m_groupname.detach();
    7984        }
    80         else if (!m_hostname.isEmpty())
     85        else if (!hostname.isEmpty())
    8186        {
    8287            VERBOSE(VB_FILE, LOC +
    8388                    QString("Unable to find any directories for the local "
    84                             "Default storage group, trying directories in all "
    85                             "Default groups!").arg(m_groupname));
    86             Init("Default", "");
    87             return;
    88         }
    89     }
    90     else
    91     {
    92         do
    93         {
    94             dirname = query.value(0).toString();
    95             dirname.replace(QRegExp("^\\s*"), "");
    96             dirname.replace(QRegExp("\\s*$"), "");
    97             if (dirname.right(1) == "/")
    98                 dirname.remove(dirname.length() - 1, 1);
    99             m_dirlist << dirname;
     89                            "Default storage group on '%1', trying directories "
     90                            "in all Default groups!").arg(hostname));
     91            found = FindDirs("Default", "");
     92            if(found)
     93            {
     94                m_groupname = "Default";
     95                m_hostname = "";
     96                m_groupname.detach();
     97                m_hostname.detach();
     98            }
    10099        }
    101         while (query.next());
    102100    }
    103101
    104102    if (!m_dirlist.size())
     
    121119    }
    122120}
    123121
     122/** \fn StorageGroup::FindDirs(const QString, const QString)
     123 *  \brief Finds and initializes the directory list associated with the Storage
     124 *         Group
     125 *
     126 *   This function should only be called by StorageGroup::Init().
     127 *
     128 *  \param group    The name of the Storage Group
     129 *  \param hostname The host whose directory list should be checked, first
     130 *  \return         true if directories were found
     131 */
     132bool StorageGroup::FindDirs(const QString group, const QString hostname)
     133{
     134    bool found = false;
     135    QString dirname;
     136    MSqlQuery query(MSqlQuery::InitCon());
     137
     138    QString sql = "SELECT DISTINCT dirname "
     139                  "FROM storagegroup ";
     140
     141    if (!group.isEmpty())
     142    {
     143        sql.append("WHERE groupname = :GROUP");
     144        if (!hostname.isEmpty())
     145            sql.append(" AND hostname = :HOSTNAME");
     146    }
     147
     148    query.prepare(sql);
     149    if (!group.isEmpty())
     150    {
     151        query.bindValue(":GROUP", group);
     152        if (!hostname.isEmpty())
     153            query.bindValue(":HOSTNAME", hostname);
     154    }
     155
     156    if (!query.exec() || !query.isActive())
     157        MythDB::DBError("StorageGroup::StorageGroup()", query);
     158    else if (query.next())
     159    {
     160        do
     161        {
     162            dirname = query.value(0).toString();
     163            dirname.replace(QRegExp("^\\s*"), "");
     164            dirname.replace(QRegExp("\\s*$"), "");
     165            if (dirname.right(1) == "/")
     166                dirname.remove(dirname.length() - 1, 1);
     167            m_dirlist << dirname;
     168        }
     169        while (query.next());
     170        found = true;
     171    }
     172
     173    return found;
     174}
     175
    124176QString StorageGroup::FindRecordingFile(QString filename)
    125177{
    126178    VERBOSE(VB_FILE, LOC + QString("FindRecordingFile: Searching for '%1'")
  • libs/libmyth/storagegroup.h

    old new  
    3333    static QStringList getRecordingsGroups(void);
    3434
    3535  private:
     36    bool FindDirs(const QString group = "Default",
     37                  const QString hostname = "");
     38
    3639    QString      m_groupname;
    3740    QString      m_hostname;
    3841    QStringList  m_dirlist;