epgcache: add possibility to change the default epg cache read/write path
[vuplus_dvbapp] / lib / dvb / epgcache.cpp
index 48cbfbf..2b688c2 100644 (file)
@@ -232,6 +232,30 @@ eEPGCache::eEPGCache()
                        timeUpdated();
        }
        instance=this;
+
+       memset(m_filename, 0, sizeof(m_filename));
+
+       FILE *f = fopen("/etc/enigma2/epg.dat.src", "r");
+       if (f)
+       {
+               int rd = fread(m_filename, 1, 255, f);
+               if (rd > 0)
+               {
+                       m_filename[rd] = 0;
+                       char *p=strchr(m_filename, '\n');
+                       if (p)
+                               m_filename[p-m_filename] = 0;
+                       p=strchr(m_filename, '\t');
+                       if (p)
+                               m_filename[p-m_filename] = 0;
+               }
+               fclose(f);
+       }
+
+       if (!strlen(m_filename))
+               strcpy(m_filename, "/hdd/epg.dat");
+
+       eDebug("[EPGC] read/write epg data from/to '%s'", m_filename);
 }
 
 void eEPGCache::timeUpdated()
@@ -962,30 +986,13 @@ void eEPGCache::thread()
 
 void eEPGCache::load()
 {
-       FILE *f = fopen("/hdd/epg.dat", "r");
+       FILE *f = fopen(m_filename, "r");
        if (f)
        {
-               unlink("/hdd/epg.dat");
+               unlink(m_filename);
                int size=0;
                int cnt=0;
-#if 0
-               unsigned char md5_saved[16];
-               unsigned char md5[16];
-               bool md5ok=false;
 
-               if (!md5_file("/hdd/epg.dat", 1, md5))
-               {
-                       FILE *f = fopen("/hdd/epg.dat.md5", "r");
-                       if (f)
-                       {
-                               fread( md5_saved, 16, 1, f);
-                               fclose(f);
-                               if ( !memcmp(md5_saved, md5, 16) )
-                                       md5ok=true;
-                       }
-               }
-               if ( md5ok )
-#endif
                {
                        unsigned int magic=0;
                        fread( &magic, sizeof(int), 1, f);
@@ -1027,7 +1034,7 @@ void eEPGCache::load()
                                        eventDB[key]=std::pair<eventMap,timeMap>(evMap,tmMap);
                                }
                                eventData::load(f);
-                               eDebug("[EPGC] %d events read from /hdd/epg.dat", cnt);
+                               eDebug("[EPGC] %d events read from %s", cnt, m_filename);
 #ifdef ENABLE_PRIVATE_EPG
                                char text2[11];
                                fread( text2, 11, 1, f);
@@ -1075,9 +1082,15 @@ void eEPGCache::load()
 
 void eEPGCache::save()
 {
+       char *buf = realpath(m_filename, NULL);
+       if (!buf)
+               return;
+
+       eDebug("[EPGC] store epg to '%s'", buf);
+
        struct statfs s;
        off64_t tmp;
-       if (statfs("/hdd", &s)<0)
+       if (statfs(buf, &s)<0)
                tmp=0;
        else
        {
@@ -1085,6 +1098,8 @@ void eEPGCache::save()
                tmp*=s.f_bsize;
        }
 
+       free(buf);
+
        // prevent writes to builtin flash
        if ( tmp < 1024*1024*50 ) // storage size < 50MB
                return;
@@ -1095,7 +1110,7 @@ void eEPGCache::save()
        if ( tmp < (eventData::CacheSize*12)/10 ) // 20% overhead
                return;
 
-       FILE *f = fopen("/hdd/epg.dat", "w");
+       FILE *f = fopen(m_filename, "w");
        int cnt=0;
        if ( f )
        {
@@ -1120,7 +1135,7 @@ void eEPGCache::save()
                                ++cnt;
                        }
                }
-               eDebug("[EPGC] %d events written to /hdd/epg.dat", cnt);
+               eDebug("[EPGC] %d events written to %s", cnt, m_filename);
                eventData::save(f);
 #ifdef ENABLE_PRIVATE_EPG
                const char* text3 = "PRIVATE_EPG";
@@ -1154,18 +1169,6 @@ void eEPGCache::save()
                fseek(f, sizeof(int), SEEK_SET);
                fwrite("ENIGMA_EPG_V7", 13, 1, f);
                fclose(f);
-#if 0
-               unsigned char md5[16];
-               if (!md5_file("/hdd/epg.dat", 1, md5))
-               {
-                       FILE *f = fopen("/hdd/epg.dat.md5", "w");
-                       if (f)
-                       {
-                               fwrite( md5, 16, 1, f);
-                               fclose(f);
-                       }
-               }
-#endif
        }
 }
 
@@ -1457,7 +1460,7 @@ void eEPGCache::channel_data::readData( const __u8 *data)
        }
        tidMap &seenSections = this->seenSections[map];
        tidMap &calcedSections = this->calcedSections[map];
-       if ( state == 1 && calcedSections == seenSections || state > 1 )
+       if ( (state == 1 && calcedSections == seenSections) || state > 1 )
        {
                eDebugNoNewLine("[EPGC] ");
                switch (source)
@@ -2562,7 +2565,7 @@ void eEPGCache::PMTready(eDVBServicePMTHandler *pmthandler)
                                                                {
                                                                        __u8 buffer[10];
                                                                        (*desc)->writeToBuffer(buffer);
-                                                                       if (!strncmp((unsigned char*)buffer+2, "EPGDATA", 7))
+                                                                       if (!strncmp((const char *)buffer+2, "EPGDATA", 7))
                                                                        {
                                                                                eServiceReferenceDVB ref;
                                                                                if (!pmthandler->getServiceReference(ref))
@@ -2571,7 +2574,7 @@ void eEPGCache::PMTready(eDVBServicePMTHandler *pmthandler)
                                                                                        messages.send(Message(Message::got_mhw2_channel_pid, ref, pid));
                                                                                }
                                                                        }
-                                                                       else if(!strncmp((unsigned char*)buffer+2, "FICHAS", 6))
+                                                                       else if(!strncmp((const char *)buffer+2, "FICHAS", 6))
                                                                        {
                                                                                eServiceReferenceDVB ref;
                                                                                if (!pmthandler->getServiceReference(ref))
@@ -2580,7 +2583,7 @@ void eEPGCache::PMTready(eDVBServicePMTHandler *pmthandler)
                                                                                        messages.send(Message(Message::got_mhw2_summary_pid, ref, pid));
                                                                                }
                                                                        }
-                                                                       else if(!strncmp((unsigned char*)buffer+2, "GENEROS", 7))
+                                                                       else if(!strncmp((const char *)buffer+2, "GENEROS", 7))
                                                                        {
                                                                                eServiceReferenceDVB ref;
                                                                                if (!pmthandler->getServiceReference(ref))