summaryrefslogtreecommitdiffstats
path: root/vmchooser/addInfo.cxx
blob: d470ebd010643b7c770c6598bb51f0d7a85929be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "inc/functions.h"

#include <iostream>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

#include "boost/filesystem.hpp"

namespace bfs=boost::filesystem;

/******************************************
 * Adds user info and hostname to xml
 *
 * usernode: <username param="user" />
 * hostnamenode: <hostname param="host" />
 * 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;
}