|
SASC-NG
DVBSoftCAM
|
#include <data.h>
Inheritance diagram for cStructLoader:Public Member Functions | |
| cStructLoader (const char *Type, const char *Filename, int Flags) | |
| virtual | ~cStructLoader () |
| void | AddItem (cStructItem *n, const char *com, cStructItem *ref) |
| void | DelItem (cStructItem *d, bool keep=false) |
| cStructItem * | NextValid (cStructItem *it) const |
| void | SetCfgDir (const char *cfgdir) |
| virtual void | Load (bool reload) |
| virtual void | Save (void) |
| void | Purge (void) |
| void | SafeClear (void) |
| void | Disable (void) |
Protected Member Functions | |
| void | CheckAccess (void) |
| bool | CheckUnmodified (void) |
| void | LoadFinished (void) |
| void | OpenFailed (void) |
| bool | CheckDoSave (void) |
| time_t | MTime (bool log) |
| virtual cStructItem * | ParseLine (char *line)=0 |
| void | Modified (bool mod=true) |
| bool | IsModified (void) const |
| void | ListLock (bool rw) |
| void | ListUnlock (void) |
| virtual void | PreLoad (void) |
| virtual void | PostLoad (void) |
Protected Attributes | |
| int | flags |
| const char * | type |
| const char * | filename |
| char * | path |
| time_t | mtime |
Friends | |
| class | cStructLoaders |
| cStructLoader::cStructLoader | ( | const char * | Type, |
| const char * | Filename, | ||
| int | Flags | ||
| ) |
| cStructLoader::~cStructLoader | ( | ) | [virtual] |
| void cStructLoader::AddItem | ( | cStructItem * | n, |
| const char * | com, | ||
| cStructItem * | ref | ||
| ) |
Definition at line 196 of file data.c.
References cSimpleListBase::Add(), cSimpleList< cStructItem >::First(), ListLock(), ListUnlock(), Modified(), cSimpleList< cStructItem >::Next(), cStructItem::SetComment(), and cStructItem::Special().
{
n->SetComment(com);
ListLock(true);
cStructItem *a=0;
if(ref) { // insert before reference
for(a=First(); a; a=Next(a))
if(Next(a)==ref) break;
}
if(!a) { // insert before first non-special
for(a=First(); a;) {
cStructItem *nn=Next(a);
if(nn && !nn->Special()) break;
a=nn;
}
}
Add(n,a);
Modified();
ListUnlock();
}
| void cStructLoader::CheckAccess | ( | void | ) | [protected] |
Definition at line 257 of file data.c.
References L_GEN_ERROR, L_GEN_WARN, path, PRINTF, SL_CLRFLAG, SL_NOACCESS, and SL_SETFLAG.
Referenced by cStructLoaderPlain::Load(), and Load().
{
if(access(path,R_OK|W_OK)!=0) {
if(errno!=EACCES)
PRINTF(L_GEN_ERROR,"failed access %s: %s",path,strerror(errno));
PRINTF(L_GEN_WARN,"no write permission on %s. Changes will not be saved!",path);
SL_SETFLAG(SL_NOACCESS);
}
else SL_CLRFLAG(SL_NOACCESS);
}
| bool cStructLoader::CheckDoSave | ( | void | ) | [protected] |
Definition at line 278 of file data.c.
References CheckUnmodified(), IsModified(), SL_DISABLED, SL_LOADED, SL_NOACCESS, SL_READWRITE, and SL_TSTFLAG.
Referenced by cStructLoaderPlain::Save(), and Save().
{
return !SL_TSTFLAG(SL_DISABLED) && SL_TSTFLAG(SL_READWRITE)
&& !SL_TSTFLAG(SL_NOACCESS) && SL_TSTFLAG(SL_LOADED)
&& IsModified() && CheckUnmodified();
}
| bool cStructLoader::CheckUnmodified | ( | void | ) | [protected] |
Definition at line 268 of file data.c.
References L_CORE_LOAD, mtime, MTime(), path, PRINTF, SL_TSTFLAG, and SL_WATCH.
Referenced by CheckDoSave().
{
time_t curr_mtime=MTime(false);
if(mtime && mtime<curr_mtime && SL_TSTFLAG(SL_WATCH)) {
PRINTF(L_CORE_LOAD,"abort save as file %s has been changed externaly",path);
return false;
}
return true;
}
| void cStructLoader::DelItem | ( | cStructItem * | d, |
| bool | keep = false |
||
| ) |
Definition at line 217 of file data.c.
References cSimpleListBase::Add(), cStructItem::Comment(), cStructItem::Delete(), ListLock(), ListUnlock(), Modified(), cStructItem::SetComment(), cString::sprintf(), and cStructItem::ToString().
Referenced by cEcmCache::Delete(), and Load().
{
if(d) {
d->Delete();
if(keep) {
cStructItem *n=new cCommentItem;
n->SetComment(cString::sprintf(";%s%s",*d->ToString(false),d->Comment()?d->Comment():""));
ListLock(true);
Add(n,d);
ListUnlock();
}
Modified();
}
}
| void cStructLoader::Disable | ( | void | ) | [inline] |
Definition at line 149 of file data.h.
References SL_DISABLED, and SL_SETFLAG.
Referenced by cSmartCards::Disable(), InitAll(), and cSoftCAM::Load().
{ SL_SETFLAG(SL_DISABLED); }
| bool cStructLoader::IsModified | ( | void | ) | const [inline, protected] |
Definition at line 132 of file data.h.
References SL_MODIFIED, and SL_TSTFLAG.
Referenced by CheckDoSave(), and Load().
{ return SL_TSTFLAG(SL_MODIFIED); }
| void cStructLoader::ListLock | ( | bool | rw | ) | [inline, protected] |
Definition at line 133 of file data.h.
References cRwLock::Lock().
Referenced by cOverrides::AddEmmPids(), AddItem(), cEcmCache::Delete(), DelItem(), cSmartCardDatas::Find(), cSystemLinkCardClient::FindBySysId(), cPlainKeys::FindKeyNoTrig(), cSmartCardSlots::FirstSlot(), cEcmCache::Flush(), cEcmCache::GetCached(), cOverrides::GetCat(), cOverrides::GetEcmPrio(), cTpsKeys::GetKey(), cSmartCardSlots::GetSlot(), cOverrides::Ignore(), cStructLoaderPlain::Load(), Load(), cEcmCache::New(), cPlainKeys::PostLoad(), cStructLoaderPlain::PreLoad(), Purge(), SafeClear(), cStructLoaderPlain::Save(), Save(), and cOverrides::UpdateEcm().
{ lock.Lock(rw); }
| void cStructLoader::ListUnlock | ( | void | ) | [inline, protected] |
Definition at line 134 of file data.h.
References cRwLock::Unlock().
Referenced by cOverrides::AddEmmPids(), AddItem(), cEcmCache::Delete(), DelItem(), cSmartCardDatas::Find(), cSystemLinkCardClient::FindBySysId(), cPlainKeys::FindKeyNoTrig(), cEcmCache::Flush(), cEcmCache::GetCached(), cOverrides::GetCat(), cOverrides::GetEcmPrio(), cTpsKeys::GetKey(), cOverrides::Ignore(), cStructLoaderPlain::Load(), Load(), cEcmCache::New(), cPlainKeys::PostLoad(), cStructLoaderPlain::PreLoad(), Purge(), cSmartCardSlots::Release(), SafeClear(), cStructLoaderPlain::Save(), Save(), and cOverrides::UpdateEcm().
{ lock.Unlock(); }
| void cStructLoader::Load | ( | bool | reload | ) | [virtual] |
Reimplemented in cStructLoaderPlain.
Definition at line 302 of file data.c.
References cSimpleListBase::Add(), CheckAccess(), cSimpleListBase::Clear(), DelItem(), cSimpleList< cStructItem >::First(), IsModified(), L_CORE_LOAD, L_GEN_ERROR, L_GEN_INFO, ListLock(), ListUnlock(), LoadFinished(), Modified(), mtime, MTime(), cSimpleList< cStructItem >::Next(), OpenFailed(), ParseLine(), path, PostLoad(), PreLoad(), PRINTF, cStructItem::SetComment(), skipspace(), SL_CLRFLAG, SL_DISABLED, SL_LOADED, SL_SETFLAG, SL_TSTFLAG, SL_WATCH, strreplace(), and type.
{
if(SL_TSTFLAG(SL_DISABLED) || (reload && !SL_TSTFLAG(SL_WATCH))) return;
FILE *f=fopen(path,"r");
if(f) {
PreLoad();
int curr_mtime=MTime(true);
ListLock(true);
bool doload=false;
if(!reload) {
Clear(); Modified(false);
mtime=curr_mtime;
doload=true;
}
else if(mtime<curr_mtime) {
PRINTF(L_CORE_LOAD,"detected change of %s",path);
if(IsModified())
PRINTF(L_CORE_LOAD,"discarding in-memory changes");
for(cStructItem *a=First(); a; a=Next(a)) DelItem(a);
Modified(false);
mtime=curr_mtime;
doload=true;
}
if(doload) {
SL_SETFLAG(SL_LOADED);
PRINTF(L_GEN_INFO,"loading %s from %s",type,path);
CheckAccess();
int lineNum=0, num=0;
char buff[4096];
while(fgets(buff,sizeof(buff),f)) {
lineNum++;
if(!index(buff,'\n') && !feof(f)) {
PRINTF(L_GEN_ERROR,"file %s readbuffer overflow line#%d",path,lineNum);
SL_CLRFLAG(SL_LOADED);
break;
}
strreplace(buff,'\n',0); strreplace(buff,'\r',0); // chomp
bool hasContent=false;
char *ls;
for(ls=buff; *ls; ls++) {
if(*ls==';' || *ls=='#') { // comment
if(hasContent)
while(ls>buff && ls[-1]<=' ') ls--; // search back to non-whitespace
break;
}
if(*ls>' ') hasContent=true; // line contains something usefull
}
cStructItem *it=0;
if(hasContent) {
char save=*ls;
*ls=0; it=ParseLine(skipspace(buff)); *ls=save;
if(!it) {
PRINTF(L_GEN_ERROR,"file %s has error in line #%d",path,lineNum);
ls=buff;
}
else num++;
}
else ls=buff;
if(!it) it=new cCommentItem;
if(it) {
it->SetComment(ls);
Add(it);
}
else {
PRINTF(L_GEN_ERROR,"out of memory loading file %s",path);
SL_CLRFLAG(SL_LOADED);
break;
}
}
ListUnlock();
PRINTF(L_CORE_LOAD,"loaded %d %s from %s",num,type,path);
PostLoad();
}
else ListUnlock();
fclose(f);
LoadFinished();
}
else
OpenFailed();
}
| void cStructLoader::LoadFinished | ( | void | ) | [protected] |
Definition at line 285 of file data.c.
References L_CORE_LOAD, path, PRINTF, SL_CLRFLAG, SL_LOADED, SL_SHUTUP, and SL_TSTFLAG.
Referenced by cStructLoaderPlain::Load(), and Load().
{
SL_CLRFLAG(SL_SHUTUP);
if(!SL_TSTFLAG(SL_LOADED))
PRINTF(L_CORE_LOAD,"loading %s terminated with error. Changes will not be saved!",path);
}
| void cStructLoader::Modified | ( | bool | mod = true | ) | [inline, protected] |
Definition at line 131 of file data.h.
References mod, SL_CLRFLAG, SL_MODIFIED, and SL_SETFLAG.
Referenced by AddItem(), DelItem(), cEcmCache::Flush(), Load(), cEcmCache::New(), cStructLoaderPlain::PreLoad(), cStructLoaderPlain::Save(), Save(), and cIrdCardInfos::Update().
{ if(mod) SL_SETFLAG(SL_MODIFIED); else SL_CLRFLAG(SL_MODIFIED); }
| time_t cStructLoader::MTime | ( | bool | log | ) | [protected] |
Definition at line 244 of file data.c.
References L_GEN_ERROR, L_GEN_WARN, path, and PRINTF.
Referenced by CheckUnmodified(), Load(), and Save().
{
struct stat64 st;
if(stat64(path,&st)!=0) {
if(log) {
PRINTF(L_GEN_ERROR,"failed fstat %s: %s",path,strerror(errno));
PRINTF(L_GEN_WARN,"automatic reload of %s disabled",path);
}
st.st_mtime=0;
}
return st.st_mtime;
}
| cStructItem * cStructLoader::NextValid | ( | cStructItem * | it | ) | const |
Definition at line 232 of file data.c.
References cSimpleList< cStructItem >::Next(), and cStructItem::Valid().
Referenced by cStructListPlain< cTpsKey >::First(), cStructList< cIrdCardInfo >::First(), cStructListPlain< cTpsKey >::Next(), and cStructList< cIrdCardInfo >::Next().
| void cStructLoader::OpenFailed | ( | void | ) | [protected] |
Definition at line 292 of file data.c.
References L_GEN_ERROR, path, PRINTF, SL_CLRFLAG, SL_LOADED, SL_MISSINGOK, SL_SETFLAG, SL_SHUTUP, SL_TSTFLAG, and SL_VERBOSE.
Referenced by cStructLoaderPlain::Load(), and Load().
{
if(SL_TSTFLAG(SL_VERBOSE) && !SL_TSTFLAG(SL_SHUTUP)) {
PRINTF(L_GEN_ERROR,"failed open %s: %s",path,strerror(errno));
SL_SETFLAG(SL_SHUTUP);
}
if(SL_TSTFLAG(SL_MISSINGOK)) SL_SETFLAG(SL_LOADED);
else SL_CLRFLAG(SL_LOADED);
}
| virtual cStructItem* cStructLoader::ParseLine | ( | char * | line | ) | [protected, pure virtual] |
Implemented in cStructLoaderPlain, cPlainKeys, cOverrides, cSmartCardDatas, cCardInfos< T >, cCardInfos< cViaccessCardInfo >, cCardInfos< cSecaCardInfo >, and cCardInfos< cIrdCardInfo >.
Referenced by Load().
| virtual void cStructLoader::PostLoad | ( | void | ) | [inline, protected, virtual] |
Reimplemented in cPlainKeys, cOverrides, and cTpsKeys.
Definition at line 136 of file data.h.
Referenced by cStructLoaderPlain::Load(), and Load().
{}
| virtual void cStructLoader::PreLoad | ( | void | ) | [inline, protected, virtual] |
Reimplemented in cStructLoaderPlain, cOverrides, and cTpsKeys.
Definition at line 135 of file data.h.
Referenced by Load().
{}
| void cStructLoader::Purge | ( | void | ) |
Definition at line 384 of file data.c.
References cSimpleListBase::Del(), cSimpleList< cStructItem >::First(), ListLock(), ListUnlock(), cSimpleList< cStructItem >::Next(), SL_DISABLED, SL_NOPURGE, and SL_TSTFLAG.
Referenced by cTpsKeys::Check().
{
if(!SL_TSTFLAG(SL_DISABLED) && !SL_TSTFLAG(SL_NOPURGE)) {
ListLock(true);
for(cStructItem *it=First(); it;) {
cStructItem *n=Next(it);
if(it->Deleted()) Del(it);
it=n;
}
ListUnlock();
}
}
| void cStructLoader::SafeClear | ( | void | ) |
Definition at line 414 of file data.c.
References cSimpleListBase::Clear(), ListLock(), and ListUnlock().
Referenced by cSystemLinkCardClient::Clean(), cSmartCards::Shutdown(), and cSoftCAM::Shutdown().
{
ListLock(true); Clear(); ListUnlock();
}
| void cStructLoader::Save | ( | void | ) | [virtual] |
Reimplemented in cStructLoaderPlain.
Definition at line 397 of file data.c.
References CheckDoSave(), cSafeFile::Close(), cSimpleList< cStructItem >::First(), L_CORE_LOAD, ListLock(), ListUnlock(), Modified(), MTime(), mtime, cSimpleList< cStructItem >::Next(), cSafeFile::Open(), path, PRINTF, and type.
{
if(CheckDoSave()) {
cSafeFile f(path);
if(f.Open()) {
ListLock(false);
for(cStructItem *it=First(); it; it=Next(it))
if(!it->Deleted() && !it->Save(f)) break;
f.Close();
mtime=MTime(true);
Modified(false);
ListUnlock();
PRINTF(L_CORE_LOAD,"saved %s to %s",type,path);
}
}
}
| void cStructLoader::SetCfgDir | ( | const char * | cfgdir | ) |
friend class cStructLoaders [friend] |
const char * cStructLoader::filename [protected] |
Definition at line 119 of file data.h.
Referenced by cStructLoader(), and SetCfgDir().
int cStructLoader::flags [protected] |
Definition at line 118 of file data.h.
Referenced by cStructLoader().
time_t cStructLoader::mtime [protected] |
Definition at line 121 of file data.h.
Referenced by CheckUnmodified(), cStructLoader(), Load(), and Save().
char* cStructLoader::path [protected] |
Definition at line 120 of file data.h.
Referenced by CheckAccess(), CheckUnmodified(), cStructLoader(), cStructLoaderPlain::Load(), Load(), LoadFinished(), MTime(), OpenFailed(), cStructLoaderPlain::Save(), Save(), SetCfgDir(), and ~cStructLoader().
const char* cStructLoader::type [protected] |
Definition at line 119 of file data.h.
Referenced by cStructLoader(), cStructLoaderPlain::Load(), Load(), cSmartCardSlots::ParseLinePlain(), cStructLoaderPlain::Save(), and Save().