rc_t TarNode_MakeXML(const KXMLNode* xml_node, FSNode** self, char* errmsg, const char* rel_path) { rc_t rc = 0; if( xml_node == NULL || self == NULL ) { rc = RC(rcExe, rcNode, rcConstructing, rcParam, rcNull); } else { char name[4096]; size_t sz; rc = KXMLNodeReadAttrCString(xml_node, "name", name, sizeof(name), &sz); if( rc != 0 || sz == 0 || name[0] == '\0' ) { strcpy(errmsg, "attribute 'name'"); rc = rc ? rc : RC(rcExe, rcDoc, rcValidating, rcAttr, rcInvalid); } else if( (rc = FSNode_Make(self, name, &TarNode_vtbl)) == 0 ) { ((TarNode*)*self)->rel_path = rel_path; strcpy(errmsg, "processing Item(s)"); if( (rc = TarNode_MakeFileList(xml_node, &((TarNode*)*self)->files, errmsg, rel_path, (*self)->name)) != 0 ) { FSNode_Release(*self); *self = NULL; } } } return rc; }
static rc_t XML_Open(const char* path, const FSNode** tree) { rc_t rc = 0; char errmsg[4096] = ""; KDirectory *dir = NULL; PLOGMSG(klogInfo, (klogInfo, "Reading XML file '$(x)'", PLOG_S(x), path)); if( (rc = KDirectoryNativeDir(&dir)) == 0 ) { const KFile* file = NULL; if( (rc = KDirectoryOpenFileRead(dir, &file, "%s", path)) == 0 ) { if( (rc = FSNode_Make((FSNode**)tree, "ROOT", &RootNode_vtbl)) == 0 ) { const KXMLDoc* xmldoc = NULL; if( (rc = KXMLMgrMakeDocRead(g_xmlmgr, &xmldoc, file)) == 0 ) { const KXMLNodeset* ns = NULL; if( (rc = KXMLDocOpenNodesetRead(xmldoc, &ns, "/FUSE/*")) == 0 ) { uint32_t count = 0; if( (rc = KXMLNodesetCount(ns, &count)) == 0 ) { if( count == 0 ) { rc = RC(rcExe, rcDoc, rcValidating, rcData, rcEmpty); } else { uint32_t i = 0; while(rc == 0 && i < count) { const KXMLNode* n = NULL; if( (rc = KXMLNodesetGetNodeRead(ns, &n, i++)) == 0 ) { SRAConfigFlags flags = ~0; errmsg[0] = '\0'; rc = XML_ValidateNode((FSNode*)*tree, n, flags, errmsg); ReleaseComplain(KXMLNodeRelease, n); } } if( rc == 0 ) { rc = SRAList_NextVersion(); } } } ReleaseComplain(KXMLNodesetRelease, ns); } ReleaseComplain(KXMLDocRelease, xmldoc); } if( rc != 0 ) { FSNode_Release(*tree); *tree = NULL; } } ReleaseComplain(KFileRelease, file); } ReleaseComplain(KDirectoryRelease, dir); } if( rc == 0 ) { PLOGMSG(klogInfo, (klogInfo, "XML file '$(x)' ok", PLOG_S(x), path)); } else { if( strlen(errmsg) < 1 ) { strcpy(errmsg, path); } LOGERR(klogErr, rc, errmsg); } return rc; }
rc_t TarNode_MakeAuto(const FSNode** cself, const KDirectory* dir, const char* path, const char* file, const char* xml_path) { rc_t rc = 0; if( cself == NULL || dir == NULL || path == NULL || file == NULL || xml_path == NULL ) { rc = RC(rcExe, rcNode, rcConstructing, rcParam, rcNull); } else { if( (rc = FSNode_Make((FSNode**)cself, file, &TarNode_vtbl)) == 0 ) { ((char*)((*cself)->name))[strlen(file) - 4] = '\0'; /* -= .xml */ ((TarNode*)*cself)->xml_path = xml_path; ((TarNode*)*cself)->rel_path = path; if( (rc = FSNode_Touch(*cself)) != 0 ) { FSNode_Release(*cself); *cself = NULL; } } } return rc; }
static rc_t XMLThread( const KThread *self, void *data ) { KDirectory *dir = NULL; PLOGMSG(klogInfo, (klogInfo, "XML sync thread started with $(s) sec", PLOG_U32(s), g_xml_sync)); do { rc_t rc = 0; KTime_t dt = 0; DEBUG_MSG(8, ("XML sync thread checking %s\n", g_xml_path)); if( (rc = KDirectoryNativeDir(&dir)) == 0 ) { rc = KDirectoryDate(dir, &dt, "%s", g_xml_path); ReleaseComplain(KDirectoryRelease, dir); } if( rc == 0 ) { if( dt != g_xml_mtime ) { const FSNode* new_root = NULL; PLOGMSG(klogInfo, (klogInfo, "File $(f) changed ($(m) <> $(d)), updating...", PLOG_3(PLOG_S(f),PLOG_I64(m),PLOG_I64(d)), g_xml_path, g_xml_mtime, dt)); if( XML_Open(g_xml_path, &new_root) == 0 ) { if( (rc = XMLLock(true)) == 0 ) { const FSNode* old_root = g_root; g_root = new_root; g_xml_mtime = dt; XMLUnlock(); FSNode_Release(old_root); PLOGMSG(klogInfo, (klogInfo, "Data from $(f) updated successfully", PLOG_S(f), g_xml_path)); } } } else { DEBUG_MSG(8, ("XML sync thread up-to-date %s\n", g_xml_path)); } } else { LOGERR(klogErr, rc, g_xml_path); } SRAList_PostRefresh(); sleep(g_xml_sync); } while( g_xml_sync > 0 ); LOGMSG(klogInfo, "XML sync thread ended"); return 0; }
void XML_Fini(void) { g_xml_sync = 0; if( g_xml_thread != NULL ) { ReleaseComplain(KThreadCancel, g_xml_thread); ReleaseComplain(KThreadRelease, g_xml_thread); } ReleaseComplain(KXMLMgrRelease, g_xmlmgr); XMLLock(true); FSNode_Release(g_root); XMLUnlock(); ReleaseComplain(KRWLockRelease, g_lock); FREE(g_xml_path); g_root = NULL; g_lock = NULL; g_start_dir = NULL; g_xml_mtime = 0; g_xml_path = NULL; g_xml_thread = NULL; }