You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. // Utility.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file implements the common functionality for the configuration
  7. // utilities.
  8. #include <cerrno>
  9. #include <cstring>
  10. #include <unistd.h>
  11. #include <pwd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <stdexcept>
  15. #include <sstream>
  16. #include <iostream>
  17. #include <fstream>
  18. #include <vector>
  19. #include "Utility.hpp"
  20. using namespace std;
  21. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. const std::string Utility::DirectorySeparator("/");
  25. /// SNF user name.
  26. const string SNFUserName = "snfuser";
  27. /// SNF group name.
  28. const string SNFGroupName = "snfuser";
  29. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  31. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. Utility::Utility() {
  33. }
  34. bool
  35. Utility::FileExists(const std::string File) {
  36. if (Verbose()) {
  37. cout << "Check whether " << File << " exists...";
  38. }
  39. bool Exists;
  40. std::ifstream Input;
  41. errno = 0;
  42. Input.open(File.c_str());
  43. if (ENOENT == errno) {
  44. Exists = false;
  45. } else {
  46. Exists = true;
  47. }
  48. Input.close();
  49. OutputVerboseEnd();
  50. return Exists;
  51. }
  52. void
  53. Utility::Copy(std::string From, std::string To) {
  54. if (Verbose()) {
  55. cout << "Copy " << From << " to " << To << "...";
  56. }
  57. if (!Explain()) {
  58. std::ifstream Input;
  59. Input.open(From.c_str());
  60. if (!Input) {
  61. std::string Temp;
  62. Temp = "Error opening the file " + From;
  63. Temp += " to read from: ";
  64. Temp += strerror(errno);
  65. throw std::runtime_error(Temp);
  66. }
  67. std::ofstream Output;
  68. Output.open(To.c_str(), std::ios::trunc);
  69. if (!Output) {
  70. std::string Temp;
  71. Temp = "Error opening the file " + To;
  72. Temp += " to copy to: ";
  73. Temp += strerror(errno);
  74. throw std::runtime_error(Temp);
  75. }
  76. if (!Input.eof()) { // Copy if there are characters.
  77. // Copying an empty file causes
  78. Output << Input.rdbuf(); // failbit to be set.
  79. }
  80. if (Output.bad() || Output.fail()) {
  81. std::string Temp;
  82. Temp = "Error copying " + From;
  83. Temp += " to " + To;
  84. Temp += ": ";
  85. Temp += strerror(errno);
  86. throw std::runtime_error(Temp);
  87. }
  88. Input.close();
  89. if (!Input) {
  90. std::string Temp;
  91. Temp = "Error closing the file " + From;
  92. Temp += ": ";
  93. Temp += strerror(errno);
  94. throw std::runtime_error(Temp);
  95. }
  96. Output.close();
  97. if (!Output) {
  98. std::string Temp;
  99. Temp = "Error closing the file " + To;
  100. Temp += ": ";
  101. Temp += strerror(errno);
  102. throw std::runtime_error(Temp);
  103. }
  104. }
  105. OutputVerboseEnd();
  106. }
  107. void
  108. Utility::SetOwnerGroup(std::string File) {
  109. struct passwd *SNFPasswd;
  110. uid_t SNFUid;
  111. struct group *SNFGroup;
  112. gid_t SNFGid;
  113. if (Verbose()) {
  114. cout << "Set owner:group of " << File << " to "
  115. << SNFUserName << ":" << SNFGroupName << "...";
  116. }
  117. if (!Explain()) {
  118. SNFPasswd = getpwnam(SNFUserName.c_str());
  119. if (SNFPasswd == 0) {
  120. string Temp;
  121. Temp = "Error getting info for Sniffer user " + SNFUserName;
  122. Temp += ": ";
  123. Temp += strerror(errno);
  124. throw runtime_error(Temp);
  125. }
  126. SNFUid = SNFPasswd->pw_uid;
  127. SNFGid = SNFPasswd->pw_gid;
  128. if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
  129. string Temp;
  130. Temp = "Error changing group and owner of file " + File;
  131. Temp += " to " + SNFUserName + ":" + SNFGroupName;
  132. Temp += ": ";
  133. Temp += strerror(errno);
  134. throw runtime_error(Temp);
  135. }
  136. }
  137. OutputVerboseEnd();
  138. }
  139. void
  140. Utility::SetMode(std::string File, mode_t mode) {
  141. if (Verbose()) {
  142. cout << "Set mode of " << File << " to "
  143. << std::oct << mode << "...";
  144. }
  145. if (!Explain()) {
  146. if (chmod(File.c_str(), mode) != 0) {
  147. ostringstream Temp;
  148. Temp << "Error changing permissions of file " << File
  149. << " to " << mode << ": " << strerror(errno);
  150. throw runtime_error(Temp.str());
  151. }
  152. }
  153. OutputVerboseEnd();
  154. }
  155. void
  156. Utility::MkDir(std::string &Dir) {
  157. if (Verbose()) {
  158. cout << "Create directory " << Dir << "...";
  159. }
  160. if (!Explain()) {
  161. if (mkdir(Dir.c_str(), 0) != 0) {
  162. ostringstream Temp;
  163. Temp << "Error creating directory " << Dir << ": " << strerror(errno);
  164. throw runtime_error(Temp.str());
  165. }
  166. }
  167. OutputVerboseEnd();
  168. }
  169. void
  170. Utility::ReplaceXmlAttribute(std::string *Content, std::string ElementName, std::string AttributeName, std::string AttributeValue) {
  171. std::string::size_type ElementContentBegin; // Index of start of the element content.
  172. std::string::size_type ElementContentEnd; // One past the end of the element content.
  173. ElementContentBegin = Content->find("<" + ElementName); // Get indices of element content.
  174. ElementContentEnd = Content->find("</" + ElementName);
  175. if (std::string::npos == ElementContentBegin) {
  176. std::string Temp;
  177. Temp = "Unable to find the start of element '" + ElementName;
  178. Temp += "'.";
  179. throw std::runtime_error(Temp);
  180. }
  181. if (std::string::npos != Content->find("<" + ElementName, ElementContentBegin + 1)) {
  182. std::string Temp;
  183. Temp = "Found two elements named '" + ElementName;
  184. Temp += "'; there must be only one.";
  185. throw std::runtime_error(Temp);
  186. }
  187. if (std::string::npos == ElementContentEnd) {
  188. ElementContentEnd = Content->find("/>", ElementContentBegin);
  189. }
  190. if (std::string::npos == ElementContentEnd){
  191. std::string Temp;
  192. Temp = "Unable to find the end of element '" + ElementName;
  193. Temp += "'.";
  194. throw std::runtime_error(Temp);
  195. }
  196. ElementContentBegin += ElementName.length() + 1; // Skip element name.
  197. std::string::size_type ProvisionalAttributeNameBegin = ElementContentBegin;
  198. std::string::size_type AttributeNameBegin ;
  199. bool FoundAttribute = false;
  200. std::string PrevChar;
  201. while (ProvisionalAttributeNameBegin < ElementContentEnd) { // Find start of attribute name.
  202. ProvisionalAttributeNameBegin = Content->find(AttributeName, ProvisionalAttributeNameBegin);
  203. if ( (ProvisionalAttributeNameBegin == std::string::npos) || (ProvisionalAttributeNameBegin >= ElementContentEnd) ) {
  204. break;
  205. }
  206. PrevChar = Content->at(ProvisionalAttributeNameBegin - 1);
  207. if (std::string::npos == PrevChar.find_first_not_of(" \t\r\n")) { // Check for whitespace before the attribute.
  208. if (FoundAttribute) {
  209. std::string Temp;
  210. Temp = "Found two attributes named '" + AttributeName;
  211. Temp += "' in element '" + ElementName;
  212. Temp += "'; there must be only one.";
  213. throw std::runtime_error(Temp);
  214. }
  215. FoundAttribute = true;
  216. AttributeNameBegin = ProvisionalAttributeNameBegin;
  217. }
  218. ProvisionalAttributeNameBegin = ProvisionalAttributeNameBegin + AttributeName.length(); // Skip.
  219. }
  220. if (!FoundAttribute) {
  221. std::string Temp;
  222. Temp = "Unable to find the attribute '" + AttributeName;
  223. Temp += "' in element '" + ElementName;
  224. Temp += "'.";
  225. throw std::runtime_error(Temp);
  226. }
  227. std::string::size_type EqualIndex; // Index of "=".
  228. std::string::size_type DelimiterIndex; // Index of delimiter of attribute value.
  229. std::string DelimiterValue; // Attribute value delimiter value.
  230. std::string::size_type AttributeValueBegin; // Index of start of attribute value.
  231. std::string::size_type AttributeValueEnd; // One past the end of the attribute value.
  232. EqualIndex = // Get index of first delimiter.
  233. Content->find_first_not_of(" \t\r\n", AttributeNameBegin + AttributeName.length());
  234. if ( (EqualIndex == std::string::npos) || (EqualIndex >= ElementContentEnd) ) {
  235. std::string Temp;
  236. Temp = "Unable to find \"=\" after '" + AttributeName;
  237. Temp += "' in element '" + ElementName;
  238. Temp += "'.";
  239. throw std::runtime_error(Temp);
  240. }
  241. DelimiterIndex = // Get index of first delimiter.
  242. Content->find_first_not_of(" \t\r\n", EqualIndex + 1);
  243. if ( (DelimiterIndex == std::string::npos) || (DelimiterIndex >= ElementContentEnd) ) {
  244. std::string Temp;
  245. Temp = "Unable to find start of value of attribute '" + AttributeName;
  246. Temp += "' in element '" + ElementName;
  247. Temp += "'.";
  248. throw std::runtime_error(Temp);
  249. }
  250. DelimiterValue = Content->at(DelimiterIndex);
  251. AttributeValueBegin = DelimiterIndex + 1;
  252. AttributeValueEnd = Content->find(DelimiterValue, AttributeValueBegin);
  253. if ( (AttributeValueEnd == std::string::npos) || (AttributeValueEnd >= ElementContentEnd) ) {
  254. std::string Temp;
  255. Temp = "Unable to find the end of the value of '" + AttributeName;
  256. Temp += "' in element '" + ElementName;
  257. Temp += "'.";
  258. throw std::runtime_error(Temp);
  259. }
  260. Content->replace(AttributeValueBegin,
  261. AttributeValueEnd - AttributeValueBegin,
  262. "");
  263. Content->insert(AttributeValueBegin, AttributeValue);
  264. }
  265. bool
  266. Utility::CheckForString(std::string Line, std::string SearchString) {
  267. string::size_type Indx;
  268. Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
  269. if (string::npos != Indx) {
  270. Line = Line.substr(Indx);
  271. }
  272. if (Line.substr(0, SearchString.length()) == SearchString) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. std::string
  278. Utility::Trim(std::string String) {
  279. std::string Whitespace(" \n\r\t");
  280. std::string::size_type End = String.find_last_not_of(Whitespace);
  281. if (End == std::string::npos) {
  282. return std::string();
  283. }
  284. std::string::size_type Start = String.find_first_not_of(Whitespace);
  285. if (Start == std::string::npos) {
  286. Start = 0;
  287. }
  288. return String.substr(Start, (End - Start) + 1);
  289. }
  290. void
  291. Utility::SetVerbose(bool Mode) {
  292. VerboseRequested = Mode;
  293. }
  294. bool
  295. Utility::Verbose() {
  296. return (VerboseRequested || ExplainRequested);
  297. }
  298. void
  299. Utility::SetExplain(bool Mode) {
  300. ExplainRequested = Mode;
  301. }
  302. bool
  303. Utility::Explain() {
  304. return ExplainRequested;
  305. }
  306. void
  307. Utility::SetHelp(bool Mode) {
  308. HelpRequested = Mode;
  309. }
  310. bool
  311. Utility::Help() {
  312. return HelpRequested;
  313. }
  314. void
  315. Utility::OutputVerboseEnd() {
  316. if (Verbose() && !Explain()) {
  317. cout << "done.\n";
  318. } else if (Explain()) {
  319. cout << "\n";
  320. }
  321. }