summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBastian Wissler2009-06-18 10:46:36 +0200
committerBastian Wissler2009-06-18 10:46:36 +0200
commit45b942cea70dcb6e908e8494dc7d8b2f695cdce1 (patch)
tree24b8644e0b9db156494e6583146494f7306b2350
parentvmchooser: (diff)
downloadvmchooser-45b942cea70dcb6e908e8494dc7d8b2f695cdce1.tar.gz
vmchooser-45b942cea70dcb6e908e8494dc7d8b2f695cdce1.tar.xz
vmchooser-45b942cea70dcb6e908e8494dc7d8b2f695cdce1.zip
vmchooser binary source changes:
* added support for feature #426 (nodes from group specific xml in /etc/opt/openslx/vmchooser-${ENV}.xml gets added into xml to run) -> printers.sh / scanners.sh are still usable - but no more warning messages are printed. * removed some unnecessary output (also about missing printers.sh/scanners.sh) * new version 0.0.10 ;-)) * Changed CMakeLists.txt to produce a "release" binary * changed svn:ignore to ignore eclipse project files etc. git-svn-id: http://svn.openslx.org/svn/openslx/openslx-src-tools/vmchooser/trunk@2965 95ad53e4-c205-0410-b2fa-d234c58c8868
-rw-r--r--vmchooser/CMakeLists.txt2
-rw-r--r--vmchooser/SWindow.cxx4
-rw-r--r--vmchooser/addInfo.cxx445
-rw-r--r--vmchooser/addPrinters.cxx49
-rw-r--r--vmchooser/addScanners.cxx50
-rw-r--r--vmchooser/inc/functions.h7
-rw-r--r--vmchooser/main.cxx9
-rw-r--r--vmchooser/readXmlDir.cxx159
-rw-r--r--vmchooser/runImage.cxx39
9 files changed, 520 insertions, 244 deletions
diff --git a/vmchooser/CMakeLists.txt b/vmchooser/CMakeLists.txt
index 6082824..fd84407 100644
--- a/vmchooser/CMakeLists.txt
+++ b/vmchooser/CMakeLists.txt
@@ -2,7 +2,7 @@
cmake_minimum_required(VERSION 2.6)
project(vmchooser)
-set(CMAKE_BUILD_TYPE Debug)
+#set(CMAKE_BUILD_TYPE Debug)
#set(CMAKE_VERBOSE_MAKEFILE ON)
set(OPENSLX_ROOT "../../../..")
diff --git a/vmchooser/SWindow.cxx b/vmchooser/SWindow.cxx
index 278fbfb..8c079b2 100644
--- a/vmchooser/SWindow.cxx
+++ b/vmchooser/SWindow.cxx
@@ -83,7 +83,7 @@ void SWindow::cb_return()
if(curr != 0 && curr->user_data()) {
DataEntry* dat = (DataEntry*) curr->user_data();
- cout << dat->short_description << endl;
+ //cout << dat->short_description << endl;
if(dat) {
runImage(curr, dat);
}
@@ -112,7 +112,7 @@ void SWindow::cb_select()
if( curr == oldcurr ) {
// start image if it has data associated
// -> double click
- cout << ((DataEntry*)curr->user_data())->short_description << endl;
+ //cout << ((DataEntry*)curr->user_data())->short_description << endl;
if(curr->user_data()) {
runImage(curr, (DataEntry*) curr->user_data() );
}
diff --git a/vmchooser/addInfo.cxx b/vmchooser/addInfo.cxx
index d470ebd..aa1f16f 100644
--- a/vmchooser/addInfo.cxx
+++ b/vmchooser/addInfo.cxx
@@ -1,13 +1,20 @@
#include "inc/functions.h"
#include <iostream>
+#include <fstream>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include "boost/filesystem.hpp"
-namespace bfs=boost::filesystem;
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+
+namespace bfs = boost::filesystem;
+
+extern string env;
/******************************************
* Adds user info and hostname to xml
@@ -17,135 +24,311 @@ namespace bfs=boost::filesystem;
* computername: needed for bootpgm <computername .../>
******************************************/
void addInfo(xmlNode* node, DataEntry* dat) {
-
- if(node == NULL) {
- return;
- }
-
- bool user = false;
- bool host = false;
-
- const int MAX_LENGTH = 200;
- char hostname[MAX_LENGTH];
- uid_t id;
- passwd *pwd;
-
- string strline;
- xmlNodePtr cur = node->children;
- xmlNodePtr usernode = NULL;
- xmlNodePtr hostnamenode = NULL;
- xmlNodePtr compnamenode = NULL;
- xmlNodePtr filenamenode = NULL;
- xmlNodePtr firstchild = node->children;
-
- // just use some standard Linux functions here ...
- id = geteuid(); // gets effective user id
- pwd = getpwuid(id); // gets passwd struct (including username)
- gethostname(hostname, MAX_LENGTH-1); // gets hostname
-
- // Get <username> node and add "username#param" attribute
- while(cur != NULL) {
- if (!xmlStrcmp(cur->name, (const xmlChar *)"username")){
- user = true;
- usernode = cur;
- break;
- }
- cur = cur->next;
- }
- if(! user) {
- usernode = xmlNewNode(NULL, (const xmlChar*) "username");
- if(usernode != NULL ) {
- xmlNewProp(usernode, (const xmlChar*) "param", (const xmlChar*) pwd->pw_name);
- xmlAddChild(node, usernode);
- }
- else {
- cerr << "<username> node could not be created!" << endl;
- }
- }
- else {
- // set param attribute in <username>
- xmlSetProp(usernode, (const xmlChar*) "param", (const xmlChar*) pwd->pw_name);
- }
-
- cur = node->children;
-
- // Get <hostname> node and add "hostname#param" attribute
- while(cur != NULL) {
- if (!xmlStrcmp(cur->name, (const xmlChar *)"hostname")){
- host = true;
- hostnamenode = cur;
- break;
- }
- cur = cur->next;
- }
- if(! host) {
- hostnamenode = xmlNewNode(NULL, (const xmlChar*) "hostname");
- if(hostnamenode != NULL ) {
- xmlNewProp(hostnamenode, (const xmlChar*) "param", (const xmlChar*) hostname);
- xmlAddChild(node, hostnamenode);
- }
- else {
- cerr << "<hostname> node could not be created!" << endl;
- }
- }
- else {
- // add param value to existant hostname-node
- xmlSetProp(hostnamenode, (const xmlChar*) "param", (xmlChar*) hostname);
- }
-
- // We need to add computername-node as the first node
- compnamenode = xmlNewNode(NULL, (const xmlChar*) "computername");
- if(compnamenode != NULL) {
- xmlNewProp(compnamenode, (const xmlChar*) "param", (const xmlChar*) hostname);
- // Add this node to the beginning of available children
- // -> that is because bootpgm only looks in the first 500 chars
- if(firstchild != NULL) {
- xmlAddPrevSibling(firstchild, compnamenode);
- }
- //xmlFreeNode(compnamenode);
- }
- else {
- cerr << "<computername> node could not be created!" << endl;
- }
-
-
-
- // We need to add the filename to the xml
- cout << "XML file name: " << dat->xml_name << endl;
- if(dat->xml_name.empty()) return;
- bfs::path path(dat->xml_name);
- std::string folder = path.branch_path().string();
- folder.append("/");
-
- cout << "XML folder name: " << folder << endl;
-
- if(folder.empty()) return;
- cur = node->children;
-
- // Get <hostname> node and add "hostname#param" attribute
- while(cur != NULL) {
- if (!xmlStrcmp(cur->name, (const xmlChar *)"image_name")){
- filenamenode = cur;
- break;
- }
- cur = cur->next;
- }
- if(! filenamenode) {
- cerr << "There is no node called 'image_name'. " << endl;
- }
- else {
- // add param value to existant hostname-node
- xmlChar* bla = xmlGetProp(filenamenode, (const xmlChar*) "param");
- if(!bla) {
- cerr << "Could not read Attribute 'param' in 'image_name' node." << endl;
- return;
- }
- else {
- xmlSetProp(filenamenode, (const xmlChar*)"param", (const xmlChar*)folder.append((char*)bla).c_str());
- }
- }
-
-
- return;
+
+ if (node == NULL) {
+ return;
+ }
+
+ bool user = false;
+ bool host = false;
+
+ const int MAX_LENGTH = 200;
+ char hostname[MAX_LENGTH];
+ uid_t id;
+ passwd *pwd;
+
+ string strline;
+ xmlNodePtr cur = node->children;
+ xmlNodePtr usernode = NULL;
+ xmlNodePtr hostnamenode = NULL;
+ xmlNodePtr compnamenode = NULL;
+ xmlNodePtr filenamenode = NULL;
+ xmlNodePtr firstchild = node->children;
+
+ // just use some standard Linux functions here ...
+ id = geteuid(); // gets effective user id
+ pwd = getpwuid(id); // gets passwd struct (including username)
+ gethostname(hostname, MAX_LENGTH - 1); // gets hostname
+
+ // Get <username> node and add "username#param" attribute
+ while (cur != NULL) {
+ if (!xmlStrcmp(cur->name, (const xmlChar *) "username")) {
+ user = true;
+ usernode = cur;
+ break;
+ }
+ cur = cur->next;
+ }
+ if (!user) {
+ usernode = xmlNewNode(NULL, (const xmlChar*) "username");
+ if (usernode != NULL) {
+ xmlNewProp(usernode, (const xmlChar*) "param",
+ (const xmlChar*) pwd->pw_name);
+ xmlAddChild(node, usernode);
+ } else {
+ cerr << "<username> node could not be created!" << endl;
+ }
+ } else {
+ // set param attribute in <username>
+ xmlSetProp(usernode, (const xmlChar*) "param",
+ (const xmlChar*) pwd->pw_name);
+ }
+
+ cur = node->children;
+
+ // Get <hostname> node and add "hostname#param" attribute
+ while (cur != NULL) {
+ if (!xmlStrcmp(cur->name, (const xmlChar *) "hostname")) {
+ host = true;
+ hostnamenode = cur;
+ break;
+ }
+ cur = cur->next;
+ }
+ if (!host) {
+ hostnamenode = xmlNewNode(NULL, (const xmlChar*) "hostname");
+ if (hostnamenode != NULL) {
+ xmlNewProp(hostnamenode, (const xmlChar*) "param",
+ (const xmlChar*) hostname);
+ xmlAddChild(node, hostnamenode);
+ } else {
+ cerr << "<hostname> node could not be created!" << endl;
+ }
+ } else {
+ // add param value to existant hostname-node
+ xmlSetProp(hostnamenode, (const xmlChar*) "param", (xmlChar*) hostname);
+ }
+
+ // We need to add computername-node as the first node
+ compnamenode = xmlNewNode(NULL, (const xmlChar*) "computername");
+ if (compnamenode != NULL) {
+ xmlNewProp(compnamenode, (const xmlChar*) "param",
+ (const xmlChar*) hostname);
+ // Add this node to the beginning of available children
+ // -> that is because bootpgm only looks in the first 500 chars
+ if (firstchild != NULL) {
+ xmlAddPrevSibling(firstchild, compnamenode);
+ }
+ //xmlFreeNode(compnamenode);
+ } else {
+ cerr << "<computername> node could not be created!" << endl;
+ }
+
+ // We need to add the filename to the xml
+ if (dat->xml_name.empty())
+ return;
+ bfs::path path(dat->xml_name);
+ std::string folder = path.branch_path().string();
+ folder.append("/");
+
+ if (folder.empty())
+ return;
+ cur = node->children;
+
+ // Get <hostname> node and add "hostname#param" attribute
+ while (cur != NULL) {
+ if (!xmlStrcmp(cur->name, (const xmlChar *) "image_name")) {
+ filenamenode = cur;
+ break;
+ }
+ cur = cur->next;
+ }
+ if (!filenamenode) {
+ cerr << "There is no node called 'image_name'. " << endl;
+ } else {
+ // add param value to existant hostname-node
+ xmlChar* bla = xmlGetProp(filenamenode, (const xmlChar*) "param");
+ if (!bla) {
+ cerr << "Could not read Attribute 'param' in 'image_name' node."
+ << endl;
+ return;
+ } else {
+ xmlSetProp(filenamenode, (const xmlChar*) "param",
+ (const xmlChar*) folder.append((char*) bla).c_str());
+ }
+ }
+
+ return;
+}
+
+
+
+
+/**
+ * read specific xml files for a group from
+ *
+ * /etc/opt/openslx/vmchooser-[groupname].xml
+ *
+ * and add group specific informations like shared_folders,
+ * printers and scanners
+ *
+ * @param dat - this is the DataEntry struct pointer
+ * @param group - this is the group name to get informations from
+ */
+void readGroupXml(DataEntry* dat, string group) {
+
+ xmlNodePtr envnode = 0,
+ tnode = 0, tnode2=0, tnode3=0, // temporary nodes
+ shared=0, // Node for shared folders
+ printer=0, // Node for printers
+ scanner=0; // Node for scanners
+
+ xmlChar* xmlenv = 0;
+ string t;
+
+ // these variables are for xpath queries
+ xmlXPathObjectPtr xpp;
+ int size = 0;
+
+ xmlDocPtr doc = xmlReadFile(
+ (string("/etc/opt/openslx/vmchooser-")+group+".xml").c_str(),
+ NULL,
+ XML_PARSE_RECOVER|XML_PARSE_NOERROR
+ );
+
+ if(! doc ) return;
+
+
+ envnode = xmlFirstElementChild(doc->children);
+ if(envnode == 0) {
+ return;
+ }
+
+ do {
+ xmlenv = xmlGetProp(envnode, (const xmlChar*)"param");
+ if(xmlStrlen(xmlenv) == 0
+ || string((const char*)envnode->name) != "environment")
+ {
+ continue;
+ }
+ if(group == (const char*)xmlenv) {
+ tnode = xmlFirstElementChild(envnode);
+ do {
+ if(tnode->type != XML_ELEMENT_NODE) continue;
+ t = (const char*)tnode->name;
+
+ // Here we are looking for shared_folders,printers and scanners
+ // respectively
+ if(t == "shared_folders") {
+ // There could be many shared folders in there
+ shared = xmlFirstElementChild(tnode);
+ do {
+ if(shared->type != XML_ELEMENT_NODE) continue;
+ xpp = evalXPath(dat->xml, "/settings/eintrag/shared_folders");
+ size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
+
+ if(size == 0) {
+ // shared_folders node not found - add it
+ tnode2 = xmlNewNode(NULL, (const xmlChar*) "shared_folders");
+ xmlAddChild(tnode2, shared);
+ xpp = evalXPath(dat->xml, "/settings/eintrag");
+ size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
+
+ for (int i= 0; i < size; i++) {
+ tnode3 = xpp->nodesetval->nodeTab[i];
+ if (tnode3->type == XML_ELEMENT_NODE ) {
+ xmlAddChild(tnode3, tnode2);
+ } else {
+ continue;
+ }
+ }
+ }
+ else
+ {
+ // found shared_folders node - add children
+ for (int i= 0; i < size; i++) {
+ tnode2 = xpp->nodesetval->nodeTab[i];
+ if (tnode2->type == XML_ELEMENT_NODE ) {
+ xmlAddChild(tnode2, shared);
+ } else {
+ continue;
+ }
+ }
+ }
+ } while ( (shared = shared->next) );
+ }
+ else if(t == "printers") {
+ // There could be many printer nodes in there
+ printer = xmlFirstElementChild(tnode);
+ do {
+ if(printer->type != XML_ELEMENT_NODE) continue;
+ xpp = evalXPath(dat->xml, "/settings/eintrag/printers");
+ size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
+
+ if(size == 0) {
+ // shared_folders node not found - add it
+ tnode2 = xmlNewNode(NULL, (const xmlChar*) "printers");
+ xmlAddChild(tnode2, printer);
+ xpp = evalXPath(dat->xml, "/settings/eintrag");
+ size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
+
+ for (int i= 0; i < size; i++) {
+ tnode3 = xpp->nodesetval->nodeTab[i];
+ if (tnode3->type == XML_ELEMENT_NODE ) {
+ xmlAddChild(tnode3, tnode2);
+ } else {
+ continue;
+ }
+ }
+ }
+ else
+ {
+ // found shared_folders node - add children
+ for (int i= 0; i < size; i++) {
+ tnode2 = xpp->nodesetval->nodeTab[i];
+ if (tnode2->type == XML_ELEMENT_NODE ) {
+ xmlAddChild(tnode2, printer);
+ } else {
+ continue;
+ }
+ }
+ }
+ } while ( (printer = printer->next) );
+ }
+ else if(t == "scanners") {
+ // There could be many printer nodes in there
+ scanner = xmlFirstElementChild(tnode);
+ do {
+ if(scanner->type != XML_ELEMENT_NODE) continue;
+ xpp = evalXPath(dat->xml, "/settings/eintrag/scanners");
+ size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
+
+ if(size == 0) {
+ // scanners node not found - add it
+ tnode2 = xmlNewNode(NULL, (const xmlChar*) "scanners");
+ xmlAddChild(tnode2, scanner);
+ xpp = evalXPath(dat->xml, "/settings/eintrag");
+ size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
+
+ for (int i= 0; i < size; i++) {
+ tnode3 = xpp->nodesetval->nodeTab[i];
+ if (tnode3->type == XML_ELEMENT_NODE ) {
+ xmlAddChild(tnode3, tnode2);
+ } else {
+ continue;
+ }
+ }
+ }
+ else
+ {
+ // found scanners node - add children
+ for (int i= 0; i < size; i++) {
+ tnode2 = xpp->nodesetval->nodeTab[i];
+ if (tnode2->type == XML_ELEMENT_NODE ) {
+ xmlAddChild(tnode2, scanner);
+ } else {
+ continue;
+ }
+ }
+ }
+ } while ( (scanner = scanner->next) );
+ }
+ }
+ while ( (tnode = tnode->next) );
+ }
+ }
+ while ( (envnode = envnode->next) );
+
+
}
diff --git a/vmchooser/addPrinters.cxx b/vmchooser/addPrinters.cxx
index f69ba56..05f7cad 100644
--- a/vmchooser/addPrinters.cxx
+++ b/vmchooser/addPrinters.cxx
@@ -7,45 +7,49 @@
#include <vector>
#include <queue>
+#include <boost/filesystem.hpp>
+
+namespace bfs=boost::filesystem;
+
/**
* function addPrinters(xmlNode* node, char* script)
* ----------------------------------------------------------
* runs content of script (absolute path of a script-file)
* -> this expects the script to print out printer information
* in the following format
- *
+ *
* printserver\tprinter\tprinter description
- *
+ *
* all other output has to be directed to /dev/null
- *
+ *
* then this function add some printer-nodes to the xml-file
- * in "settings/eintrag/printers"
+ * in "settings/eintrag/printers"
* (which will be created also if needed.)
* in the following form: <br/>
- *
+ *
* &lt;printer name=&quot;printer&quot; path=&quot;//printserver/printer&quot; &gt;
* Printerdescription
* &lt;/printer&gt;
*/
bool addPrinters(xmlNode* node, char* script) {
-
+
if(node == NULL) {
return false;
}
-
+
bool printer = false;
vector<string> info_printer;
-
+
const int MAX_LENGTH = 300;
char line[MAX_LENGTH];
char delims[] = "\t";
string strline;
FILE* inp = 0;
-
+
unsigned int tindex = 0;
xmlNodePtr cur = node->children;
xmlNodePtr printernode = NULL;
-
+
// Get <printers> node
while(cur != NULL) {
if (!xmlStrcmp(cur->name, (const xmlChar *)"printers")){
@@ -64,28 +68,29 @@ bool addPrinters(xmlNode* node, char* script) {
cerr << "No <printers> node created" << endl;
}
}
-
+
// Parse input of printer-Skript (called by "char* script")
// and write into <printer> nodes
+ if( bfs::is_regular_file(bfs::path(script)) )
if( (inp = popen(script, "r" )) ) {
while(fgets(line, MAX_LENGTH, inp ) != NULL) {
strline = string(line);
if(strline.length() > 3) {
-
+
queue<unsigned int> temp;
temp.push( strline.find_first_of( delims , 0) );
-
+
while( temp.back() != string::npos ) {
temp.push( strline.find_first_of( delims, temp.back()+1 ) );
}
-
+
unsigned int t_front;
string tstr = string("");
while( tindex != string::npos ) {
-
+
// build printer-info element
t_front = temp.front();
-
+
if(tindex == 0) {
tstr = strline.substr(0, t_front);
}
@@ -103,8 +108,8 @@ bool addPrinters(xmlNode* node, char* script) {
tindex = t_front;
temp.pop();
}
-
- // Construct <printer> nodes
+
+ // Construct <printer> nodes
xmlNodePtr pNode = xmlNewNode(NULL, (const xmlChar*) "printer");
xmlNewProp(pNode, (const xmlChar*) "name", (const xmlChar*) info_printer.at(1).c_str());
xmlNewProp ( pNode, (const xmlChar*) "path", (const xmlChar*)
@@ -113,20 +118,18 @@ bool addPrinters(xmlNode* node, char* script) {
if(info_printer.size() > 2) {
xmlAddChild( pNode, xmlNewText( (const xmlChar*) info_printer.at(2).c_str() ) );
}
-
+
if(pNode != NULL) {
xmlAddChild( printernode, pNode);
}
-
+
info_printer.clear();
tindex = 0;
}
}
pclose(inp);
- return true;
}
- fprintf(stderr, "Couldn't run \"%s\" script!", script);
- return false;
+ return true;
}
diff --git a/vmchooser/addScanners.cxx b/vmchooser/addScanners.cxx
index 3d3eb7f..0dc75bd 100644
--- a/vmchooser/addScanners.cxx
+++ b/vmchooser/addScanners.cxx
@@ -7,45 +7,49 @@
#include <vector>
#include <queue>
+#include <boost/filesystem.hpp>
+
+namespace bfs=boost::filesystem;
+
/**
* function addScanners(xmlNode* node, char* script)
* ----------------------------------------------------------
* runs content of script (absolute path of a script-file)
* -> this expects the script to print out scanner information
* in the following format
- *
+ *
* scanserver\tscanner\tscanner description
- *
+ *
* all other output has to be directed to /dev/null
- *
+ *
* then this function add some scanner-nodes to the xml-file
- * in "settings/eintrag/scanners"
+ * in "settings/eintrag/scanners"
* (which will be created also if needed.)
* in the following form: <br/>
- *
+ *
* &lt;scanner name=&quot;scanner&quot; path=&quot;//scanserver/scanner&quot; &gt;
* Scannerdescription
* &lt;/scanner&gt;
*/
bool addScanners(xmlNode* node, char* script) {
-
+
if(node == NULL) {
return false;
}
-
+
bool scanner = false;
vector<string> info_scanner;
-
+
const int MAX_LENGTH = 300;
char line[MAX_LENGTH];
char delims[] = "\t";
string strline;
FILE* inp = 0;
-
+
unsigned int tindex = 0;
xmlNodePtr cur = node->children;
xmlNodePtr scannernode = NULL;
-
+
// Get <scanners> node
while(cur != NULL) {
if (!xmlStrcmp(cur->name, (const xmlChar *)"scanners")){
@@ -64,28 +68,29 @@ bool addScanners(xmlNode* node, char* script) {
cerr << "No <scanners> node created" << endl;
}
}
-
+
// Parse input of scanner-Skript (called by "char* script")
// and write into <scanner> nodes
+ if( bfs::is_regular_file(bfs::path(script)) )
if( (inp = popen(script, "r" )) ) {
while(fgets(line, MAX_LENGTH, inp ) != NULL) {
strline = string(line);
if(strline.length() > 3) {
-
+
queue<unsigned int> temp;
temp.push( strline.find_first_of( delims , 0) );
-
+
while( temp.back() != string::npos ) {
temp.push( strline.find_first_of( delims, temp.back()+1 ) );
}
-
+
unsigned int t_front;
string tstr = string("");
while( tindex != string::npos ) {
-
+
// build scanner-info element
t_front = temp.front();
-
+
if(tindex == 0) {
tstr = strline.substr(0, t_front);
}
@@ -103,8 +108,8 @@ bool addScanners(xmlNode* node, char* script) {
tindex = t_front;
temp.pop();
}
-
- // Construct <scanner> nodes
+
+ // Construct <scanner> nodes
xmlNodePtr pNode = xmlNewNode(NULL, (const xmlChar*) "scanner");
xmlNewProp(pNode, (const xmlChar*) "name", (const xmlChar*) info_scanner.at(1).c_str());
xmlNewProp ( pNode, (const xmlChar*) "path", (const xmlChar*)
@@ -113,19 +118,18 @@ bool addScanners(xmlNode* node, char* script) {
if(info_scanner.size() > 2) {
xmlAddChild( pNode, xmlNewText( (const xmlChar*) info_scanner.at(2).c_str() ) );
}
-
+
if(pNode != NULL) {
xmlAddChild( scannernode, pNode);
}
-
+
info_scanner.clear();
tindex = 0;
}
}
pclose(inp);
- return true;
}
- fprintf(stderr, "Couldn't run \"%s\" script!", script);
- return false;
+
+ return true;
}
diff --git a/vmchooser/inc/functions.h b/vmchooser/inc/functions.h
index 9a10329..91dfdd1 100644
--- a/vmchooser/inc/functions.h
+++ b/vmchooser/inc/functions.h
@@ -2,6 +2,7 @@
#define _FUNCTIONS_H_
#include <fltk/Widget.h>
+#include <libxml/xpath.h>
#include <glob.h>
@@ -25,9 +26,11 @@ char* getFolderName();
/* Reads output from a skript (2. argument) */
/* Adds the elements into xmlNode "printers" (1. argument) */
-bool addPrinters(xmlNode* node, char* script);
-bool addScanners(xmlNode* node, char* script);
+bool addPrinters(xmlNode* node, char* script);
+bool addScanners(xmlNode* node, char* script);
void addInfo(xmlNode* node, DataEntry* dat); /* This is defined in addPrinters.cxx */
+void readGroupXml(DataEntry* dat, string group);
+xmlXPathObjectPtr evalXPath(xmlDocPtr doc, const char* path);
/* Write configuration xml */
string writeConfXml(DataEntry& dat);
diff --git a/vmchooser/main.cxx b/vmchooser/main.cxx
index 09d382c..8e8b2ea 100644
--- a/vmchooser/main.cxx
+++ b/vmchooser/main.cxx
@@ -62,7 +62,8 @@ int main(int argc, char** argv) {
return 0;
}
- /** XML - PATH
+ /**
+ * XML - PATH
*
* 1. read from stage3.conf
* 2. option -p
@@ -80,8 +81,7 @@ int main(int argc, char** argv) {
ifs.getline(buf, n);
s = buf;
if(s.substr(0,17) == "vmchooser_xmlpath") {
- xmlpath = new char[strlen(buf)];
- xmlpath = (char*)s.substr(19,s.length()-20).append("/").c_str();
+ xmlpath = (char*)strdup(s.substr(19,s.length()-20).append("/").c_str());
}
}
@@ -103,7 +103,7 @@ int main(int argc, char** argv) {
/* VERSION */
if(opt->getFlag('v') || opt->getFlag("version")) {
// just print out version information - helps testing
- cout << "virtual machine chooser 0.0.9"<< endl;
+ cout << "virtual machine chooser 0.0.10"<< endl;
delete opt;
return 0;
@@ -185,6 +185,7 @@ int main(int argc, char** argv) {
win.unfold_entries(lin_entries, vm_entries);
win.show(); // argc,argv
win.border(false);
+ free(xmlpath);
bool retval = run();
diff --git a/vmchooser/readXmlDir.cxx b/vmchooser/readXmlDir.cxx
index b275ea6..70b3d0c 100644
--- a/vmchooser/readXmlDir.cxx
+++ b/vmchooser/readXmlDir.cxx
@@ -13,51 +13,71 @@
#include <libxml/xpath.h>
#include <boost/regex.hpp>
+#include <boost/filesystem.hpp>
#include <cstring>
#include <vector>
#include <iostream>
+#include <fstream>
#include "inc/constants.h"
#include "inc/DataEntry.h"
#include "inc/functions.h"
+namespace bfs=boost::filesystem;
#ifdef LIBXML_TREE_ENABLED
-
+string env;
vector<string> xmlVec;
-char* getAttribute(xmlDoc *doc, char* name)
-{
- xmlNode* temp;
+xmlXPathObjectPtr evalXPath(xmlDocPtr doc, const char* path) {
xmlXPathContextPtr xp = xmlXPathNewContext(doc);
- string bla = string("/settings/eintrag/")+ string(name)+ string("/@param");
+ string bla = string(path);
if(xp == NULL) {
fprintf(stderr,"Error: unable to create new XPath context\n");
xmlFreeDoc(doc);
return NULL;
}
- xmlXPathObjectPtr xpp = xmlXPathEvalExpression((const xmlChar*)bla.c_str(), xp);
- if(xpp == NULL) {
+ xmlXPathObjectPtr result = xmlXPathEvalExpression((const xmlChar*)bla.c_str(), xp);
+ if(result == NULL) {
fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", bla.c_str());
- xmlXPathFreeContext(xp);
- xmlFreeDoc(doc);
+ xmlXPathFreeContext(xp);
+ xmlFreeDoc(doc);
return NULL;
}
+ xmlXPathFreeContext(xp);
+ return result;
+}
+
+char* getAttribute(xmlDoc *doc, char* name)
+{
+ xmlNode* temp;
+ string path = string("/settings/eintrag/")+ string(name)+ string("/@param");
+ char* cpath = strdup(path.c_str());
//print_xpath_nodes(xpp->nodesetval, stdout);
+
+ // Get Attribute via XPath
+ xmlXPathObjectPtr xpp = evalXPath(
+ doc,
+ (const char*)cpath
+ );
+ free(cpath);
+
int size;
size = (xpp->nodesetval) ? xpp->nodesetval->nodeNr: 0;
- for (int i= 0; i < size; i++) {
- temp = xpp->nodesetval->nodeTab[i];
- if (temp->type == XML_ATTRIBUTE_NODE ) {
- return (char*) temp->children->content;
- } else {
- continue;
- }
- }
- return NULL;
+ for (int i= 0; i < size; i++) {
+ temp = xpp->nodesetval->nodeTab[i];
+ if (temp->type == XML_ATTRIBUTE_NODE ) {
+
+ return (char*) temp->children->content;
+ } else {
+ continue;
+ }
+ }
+
+ return NULL;
}
char* getNodeValue(xmlDoc *doc, char* name)
@@ -73,8 +93,8 @@ char* getNodeValue(xmlDoc *doc, char* name)
xmlXPathObjectPtr xpp = xmlXPathEvalExpression((const xmlChar*)bla.c_str(), xp);
if(xpp == NULL) {
fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", bla.c_str());
- xmlXPathFreeContext(xp);
- xmlFreeDoc(doc);
+ xmlXPathFreeContext(xp);
+ xmlFreeDoc(doc);
return NULL;
}
@@ -98,11 +118,11 @@ DataEntry* get_entry(xmlDoc * doc)
{
char *tempc = NULL;
DataEntry* de = new DataEntry();
-
+
tempc = getAttribute(doc,(char *)"short_description");
if (tempc != NULL ) {
de->short_description = tempc;
- //printf("%s\n",de->short_description.c_str());
+
// replace a substring
std::string dest_string, dest1_string;
boost::regex re("\n|\r");
@@ -161,7 +181,7 @@ DataEntry* get_entry(xmlDoc * doc)
de->network = tempc;
}
tempc = NULL;
-
+
tempc = getAttribute(doc,(char *) "virtualmachine");
if (tempc != NULL ) {
@@ -174,11 +194,10 @@ DataEntry* get_entry(xmlDoc * doc)
}
}
else {
-
-
- /* TODO: DEFAULTS TO VMWARE HERE */
+
+ // Defaults to vmware - if the attribute is unknown
de->imgtype = VMWARE;
-
+
}
tempc = NULL;
@@ -191,7 +210,7 @@ DataEntry* get_entry(xmlDoc * doc)
}
}
tempc = NULL;
-
+
tempc = getAttribute(doc,(char *) "locked");
if (tempc != NULL ) {
de->locked = (strstr(tempc,"true")!= NULL?true:false);
@@ -221,14 +240,20 @@ DataEntry* get_entry(xmlDoc * doc)
de->priority = 5;
}
tempc = NULL;
-
+
de->xml = doc;
return de;
}
-
+/**
+ * The main function of this file:
+ *
+ * - calls xmlfilter.sh to glob a folder for xmls
+ * -> if no xmlfilter.sh is available, it globs for available xmls
+ * - reads all xml files and creates for each its own DataEntry-struct
+ */
DataEntry** readXmlDir(char* path)
{
const int MAX_LENGTH = 256;
@@ -241,17 +266,69 @@ DataEntry** readXmlDir(char* path)
return NULL;
}
- if( (inp = popen(string(fpath).append("/")
- .append(filterscript).append(" ")
- .append(path).c_str(), "r" )) ) {
- while(fgets(line, MAX_LENGTH, inp ) != NULL) {
- xmlVec.push_back(string(line).substr(0,strlen(line)-1) );
- }
- pclose(inp);
+ bfs::path filter(string(fpath).append("/").append(filterscript));
+
+
+ if(bfs::is_regular_file(filter)) {
+ if( (inp = popen(string(fpath).append("/")
+ .append(filterscript).append(" ")
+ .append(path).c_str(), "r" )) && bfs::is_regular_file(filter) ) {
+ while(fgets(line, MAX_LENGTH, inp ) != NULL) {
+ xmlVec.push_back(string(line).substr(0,strlen(line)-1) );
+ }
+ pclose(inp);
+ }
+ }
+ else
+ {
+ ifstream conffile("/etc/opt/openslx/vmchooser-stage3.conf");
+ if(conffile) {
+ int n = 255;
+ char buf[n];
+ string s = "";
+ while(!conffile.eof()) {
+ conffile.getline(buf, n);
+ s = buf;
+ if(s.substr(0,13) == "vmchooser_env") {
+ env = s.substr(15,s.length()-16);
+ }
+ }
+ }
+
+ glob_t globbuf;
+ glob(string(path).append("*.xml").c_str(), NULL, NULL, &globbuf);
+
+ xmlDocPtr tdoc = 0;
+ char* tstr = 0;
+
+ for(int c=0; c<globbuf.gl_pathc; c++) {
+ tdoc = xmlReadFile(globbuf.gl_pathv[c],NULL,XML_PARSE_RECOVER|XML_PARSE_NOERROR);
+
+ if(!tdoc) {
+ cerr << "Error opening xml file " << globbuf.gl_pathv[c] << "!" << endl;
+ return 0;
+ }
+
+ tstr = getAttribute(tdoc, (char*)"pools");
+
+ if(tstr == 0) {
+ xmlFreeDoc(tdoc);
+ continue;
+ }
+
+ if(env == tstr) {
+ xmlVec.push_back(string(globbuf.gl_pathv[c]) );
+ }
+
+ xmlFreeDoc(tdoc);
+ tdoc = 0; tstr = 0;
+ }
+
}
+
free(fpath);
- xmlDoc *doc = '\0';
+ xmlDoc *doc = 0;
int c = 0;
string::size_type loc;
@@ -267,7 +344,7 @@ DataEntry** readXmlDir(char* path)
// FOUND Vorlage
continue;
}
-
+
struct stat m;
stat(xmlVec[i].c_str(), &m);
@@ -278,14 +355,14 @@ DataEntry** readXmlDir(char* path)
continue;
}
- doc = xmlReadFile(xmlVec[i].c_str(), NULL, XML_PARSE_RECOVER);
+ doc = xmlReadFile(xmlVec[i].c_str(), NULL, XML_PARSE_RECOVER|XML_PARSE_NOERROR);
if (doc == NULL) {
fprintf(stderr, "error: could not parse file %s\n", xmlVec[i].c_str());
continue;
}
result[c] = get_entry(doc);
- if (result[c] != '\0') {
+ if (result[c] != 0) {
result[c]->xml_name = xmlVec[i];
c++;
}
diff --git a/vmchooser/runImage.cxx b/vmchooser/runImage.cxx
index 21ac50d..17d7190 100644
--- a/vmchooser/runImage.cxx
+++ b/vmchooser/runImage.cxx
@@ -18,6 +18,7 @@
/* define MAX_LENGTH for string in getFolderName */
const int MAX_LENGTH = 200;
extern SWindow* mainwin;
+extern string env;
/** *************************************************************
* void runImage runs a (virtual machine) image using fork()
@@ -26,18 +27,18 @@ extern SWindow* mainwin;
void runImage(fltk::Widget*, void* p)
{
string confxml;
-
+
/* printf("runImage called\n"); */
if ( p == NULL ) {
return;
}
-
+
DataEntry& dat = *((DataEntry*) p);
-
+
if(dat.imgtype == VMWARE || dat.imgtype == VBOX ) {
confxml = writeConfXml(dat);
}
-
+
pid_t pid;
// in case you want to wait hours on your thread
//int status;
@@ -45,10 +46,10 @@ void runImage(fltk::Widget*, void* p)
strncpy(arg1, (char*) string("'\n\nStarte Image: ")
.append(dat.short_description)
.append("\n'").c_str(),MAX_LENGTH);
- char* argv[] = { (char*) "/opt/openslx/plugin-repo/vmchooser/mesgdisp",
+ char* argv[] = { (char*) "/opt/openslx/plugin-repo/vmchooser/mesgdisp",
arg1, NULL };
- printf("%s", arg1);
+ //printf("%s", arg1);
pid = fork();
switch( pid ) {
@@ -60,7 +61,7 @@ void runImage(fltk::Widget*, void* p)
mainwin->destroy();
fltk::wait();
if(dat.imgtype == VMWARE || dat.imgtype == VBOX) {
- cout << "calling " << argv[1] << endl;
+ //cout << "calling " << argv[1] << endl;
execvp(argv[0], argv);
}
break;
@@ -80,7 +81,7 @@ void runImage(fltk::Widget*, void* p)
}
/**
- * Helper-function for runImage(Widget, void)
+ * Helper-function for runImage(Widget, void)
* - runs the chosen virtualizer image
**/
void runImage(DataEntry& dat, string confxml)
@@ -92,7 +93,7 @@ void runImage(DataEntry& dat, string confxml)
char* arg[] = { (char *) "/var/X11R6/bin/run-virt.sh",
(char*)confxml.c_str(),
NULL };
-
+
execvp("/var/X11R6/bin/run-virt.sh", arg);
}
@@ -103,7 +104,7 @@ void runImage(DataEntry& dat, string confxml)
* Helper-Function: Get folder name
*/
char* getFolderName() {
-
+
/* Var for the folder name */
char* pname = (char*) malloc(MAX_LENGTH);
int result;
@@ -133,15 +134,15 @@ string writeConfXml(DataEntry& dat) {
string pname = "/etc/opt/openslx/";
xmlNodePtr cur = 0;
xmlNodePtr root = 0;
-
+
string pskript = pname +"/printer.sh";
-
+
cur = xmlDocGetRootElement(dat.xml);
if(cur == NULL) {
printf("Empty XML Document %s!", dat.xml_name.c_str());
return dat.xml_name.c_str();
}
-
+
// xmlNode "eintrag"
root = cur->children;
while(xmlStrcmp(root->name, (const xmlChar*)"eintrag") != 0) {
@@ -151,17 +152,21 @@ string writeConfXml(DataEntry& dat) {
fprintf(stderr, "%s is not a valid xml file!", dat.xml_name.c_str());
return dat.xml_name.c_str();
}
-
+
// add "printers" and "scanners" - XML-Nodes
addPrinters(root, (char*)pskript.c_str());
-
+
pskript = pname + "/scanner.sh";
addScanners(root, (char*)pskript.c_str());
// add hostname and username information
addInfo(root, &dat);
-
-
+
+
+ // read the group configuration XML
+ readGroupXml(&dat, env);
+
+
srand(time(NULL));
string xmlfile;
ostringstream i;