#include #include // for free #include #include #include "ant.h" #include "libdb.h" #include "libCfg.h" #include "pro_defs.h" #include "Entry.h" #include "cTime.h" Entry::Entry () { memset (_VInst, 0, sizeof (_VInst)); _source = 0; _Size = 0; _PreProc = '0'; _PostProc = '0'; memset (_Date, 0, sizeof (_Date)); _IDFSType = 0; _Status = OFFLINE; _OldStatus = OFFLINE; _shareStatus = GLOBAL; _RecNo = 0; } Entry::~Entry () { } void Entry::SetSource (SDDAS_ULONG source) { _source = source; } void Entry::SetBTime (SDDAS_UINT year, SDDAS_UINT day, SDDAS_ULONG msec) { _BegTm.SetTime (year, day, msec); } void Entry::SetETime (SDDAS_UINT year, SDDAS_UINT day, SDDAS_ULONG msec) { _EndTm.SetTime (year, day, msec); } void Entry::SetVirtualName (char *vinst) { strcpy (_VInst, vinst); } // The code that's common between the FillRemoteEntry and FillEntry void Entry::FillEntryCommon(SDDAS_INT idfsType, char *FullPathName) { _FName = FullPathName; _OldStatus = _Status; if (idfsType == _HD_FILE_) _IDFSType = _H_FILE_; else _IDFSType = idfsType; if (idfsType == _I_FILE_) _PostProc = '4'; else _PostProc = '2'; } void Entry::FillRemoteEntry(SDDAS_INT idfsType) { char FullPathName [512]; sprintf (FullPathName, "%s", MakeFileName (idfsType)); FillEntryCommon (idfsType, FullPathName); } void Entry::FillEntry (SDDAS_INT idfsType) { char FullPathName [512]; char *postTimePath; static SDDAS_ULONG old_data_key = 0; // cache datakey static char old_postTimePath [512]; // cache PostTimePath // if the PostTimePath for the source does not exist in the config files memset (FullPathName, 0, sizeof (FullPathName)); if (_source != old_data_key) { postTimePath = GetCFGElement ("PostTimePath"); if (postTimePath == NULL) { std::cerr << "ERROR: PostTimePath for this project is not in the DB.cfg!" << std::endl; _Status = BAD; return; } strcpy (old_postTimePath, postTimePath); old_data_key = _source; } sprintf (FullPathName, "%s/%s", old_postTimePath, MakeFileName (idfsType)); // First, check what they asked for // If it is not found, if it is a VIDF they want, look for the .v3 if (Online (FullPathName)) _Status = ONLINE; else _Status = OFFLINE; if ((_Status == OFFLINE) && (idfsType == _I_FILE_)) { strcpy (&(FullPathName [strlen (FullPathName) - 1]), "V.v3"); if (Online (FullPathName)) _Status = ONLINE; else strcpy (&(FullPathName [strlen (FullPathName) - 4]), "I"); // change it back } FillEntryCommon (idfsType, FullPathName); } void Entry::SetIDFSType (SDDAS_INT idfsType) { _IDFSType = idfsType; } char * Entry::GetCFGElement (const char *name) { // if the for the source does not exist in the config files char *path = dbCfgPath (_source, name); if (path == NULL) { std::cerr << "ERROR: Database not configured for " << name << " and " << dbHierarchyString (_source) << std::endl; _Status = BAD; return NULL; } return path; } int Entry::Online (const char *path) { if (access (path, R_OK) == 0) return 1; else return 0; } char * Entry::MakeFileName (int FileType) { static char str [128]; char tail [4]; memset (str, 0, sizeof (str)); switch (FileType) { case _V3_FILE_ : strcpy (tail, "V.v3"); break; case _I_FILE_ : strcpy (tail, "I"); break; case _D_FILE_ : strcpy (tail, "D"); break; case _H_FILE_ : strcpy (tail, "H"); break; case _HD_FILE_ : strcpy (tail, "H"); break; }; // switch /* the next two variables may be used in later versions sec = tm / 1000; msec = tm % 1000; **/ sprintf (str, "%s%s%s", _VInst, _BegTm.GetTimeStr (FILENAME), tail); return str; } SDDAS_ULONG Entry::GetSource () { return _source; } char * Entry::GetVInst () { return _VInst; } const char * Entry::GetFName () { return _FName.c_str (); } TimeObj * Entry::GetBegTime () { return &_BegTm; } TimeObj * Entry::GetEndTime () { return &_EndTm; } const FileStatusType & Entry::GetStatus () { return _Status; } const FileStatusType & Entry::GetOldStatus () { return _OldStatus; } const SDDAS_INT Entry::GetRecNo () { return _RecNo; } const SDDAS_INT Entry::GetIDFSType () { return _IDFSType; } const SDDAS_LONG Entry::GetSize () { return _Size; } const char * Entry::GetDate () { return _Date; } const char Entry::GetPreProc () { return _PreProc; } const char Entry::GetPostProc () { return _PostProc; } const shareFileStatusType Entry::GetShareStatus () { return _shareStatus; } // This routine will convert a header entry to the data entry or vice versa Entry * Entry::ConvertEntry () { // Create a new entry Entry *E; E = new Entry (*this); char *tmp; if (!_FName.empty ()) { tmp = strdup (_FName.c_str ()); // Change the filename char *tail = &tmp [strlen (tmp) - 1]; if (*tail == 'D') *tail = 'H'; else if (*tail == 'H') *tail = 'D'; E->_FName = tmp; // Check the online status if (Online (E->_FName.c_str ())) E->_Status = ONLINE; else E->_Status = OFFLINE; E->_OldStatus = E->_Status; free (tmp); } else { E->_Status = BAD; } // For the malloc'ed stuff, we must make new copies E->_BegTm = _BegTm; E->_EndTm = _EndTm; // Change the filetype if (E->_IDFSType == _D_FILE_) E->_IDFSType = _H_FILE_; else if (E->_IDFSType == _H_FILE_) E->_IDFSType = _D_FILE_; return (E); } int Entry::MakeRemotePIDFEntry(const SDDAS_ULONG src, shareFileStatusType shareType) { char FullPathName [512]; // The PIDF has a different filename and an off/online status strcpy (_VInst, dbVirtualName (src)); _source = src; switch (shareType) { case GLOBAL : sprintf (FullPathName, "%s.pidf.v2", _VInst); break; case SHARED : sprintf (FullPathName, "%s.pidf.v2", _VInst); break; case LOCAL : sprintf (FullPathName, "%s.pidf.v2", _VInst); break; } return MakePIDFEntryCommon(src, shareType, FullPathName); } int Entry::MakePIDFEntry (const SDDAS_ULONG src, shareFileStatusType shareType) { char FullPathName [512]; char *tmp; // The PIDF has a different filename and an off/online status strcpy (_VInst, dbVirtualName (src)); _source = src; switch (shareType) { case GLOBAL : tmp = GetCFGElement ("PidfPath"); if (tmp == NULL) return -1; sprintf (FullPathName, "%s/%s.pidf.v2", tmp, _VInst); break; case SHARED : tmp = GetEnv ("SDDAS_HOME"); if (tmp == NULL) return -1; sprintf (FullPathName, "%s/SCF/%s.pidf.v2", tmp, _VInst); break; case LOCAL : tmp = GetHomeDirectory (); if (tmp == NULL) return -1; sprintf (FullPathName, "%s/SCF/%s.pidf.v2", tmp, _VInst); break; } return MakePIDFEntryCommon(src, shareType, FullPathName); } int Entry::MakePIDFEntryCommon(const SDDAS_ULONG src, shareFileStatusType shareType, char* FullPathName) { _FName = FullPathName; if (Online (_FName.c_str ())) _Status = ONLINE; else _Status = OFFLINE; _OldStatus = _Status; _IDFSType = _P_FILE_; _shareStatus = shareType; return (1); } int Entry::MakeSCFEntry (char *fileName, shareFileStatusType shareType) { char FullPathName [512]; char *tmp; // The SCF has a different filename and is always online and is never global switch (shareType) { case GLOBAL : std::cerr << "Illegal call to MakeSCFEntry!" << std::endl; break; case SHARED : tmp = GetEnv ("SDDAS_HOME"); if (tmp == NULL) return -1; sprintf (FullPathName, "%s/SCF/%s", tmp, fileName); break; case LOCAL : tmp = GetHomeDirectory (); if (tmp == NULL) return -1; sprintf (FullPathName, "%s/SCF/%s", tmp, fileName); break; } _FName = FullPathName; _Status = ONLINE; _OldStatus = _Status; _IDFSType = _S_FILE_; _shareStatus = shareType; return (1); } // Will check an existing VIDF entry to see if it should be a new VIDF entry, needed by the catalog void Entry::CheckVIDFEntry () { if (_IDFSType == _I_FILE_) { char newPathName [255]; strcpy (newPathName, _FName.c_str ()); if (newPathName [strlen (newPathName) - 1] == 'I') { strcpy (&(newPathName [strlen (newPathName) - 1]), "V.v3"); if (Online (newPathName)) { _Status = ONLINE; _FName = newPathName; } } else if (newPathName [strlen (newPathName) - 1] == '3') { strcpy (&(newPathName [strlen (newPathName) - 4]), "I"); if (Online (newPathName)) { _Status = ONLINE; _FName = newPathName; } } } } void Entry::SetStatus (FileStatusType status) { _Status = status; } const bool Entry::FileExists () { if ((_Status == OFFLINE) && (_IDFSType == _I_FILE_)) { _FName.replace (_FName.end () - 1, _FName.end (), std::string ("V.v3")); if (Online (_FName.c_str ()) == 1) return true; else _FName.replace (_FName.end () - 4, _FName.end (), "I"); // change it back } return Online (_FName.c_str ()) == 1; }