Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. DebugRequested(false), VerboseRequested(false), ExplainRequested(false), HelpRequested(false)
  34. {
  35. }
  36. bool
  37. Utility::FileExists(const std::string File) {
  38. bool Exists;
  39. std::ifstream Input;
  40. errno = 0;
  41. Input.open(File.c_str());
  42. if (ENOENT == errno) {
  43. Exists = false;
  44. } else {
  45. Exists = true;
  46. }
  47. Input.close();
  48. return Exists;
  49. }
  50. std::string
  51. Utility::ReadLastPartOfFile(std::string File, long Size) {
  52. if (!FileExists(File)) {
  53. return "";
  54. }
  55. std::ifstream Input;
  56. Input.open(File.c_str());
  57. if (!Input) {
  58. std::string Temp;
  59. Temp = "Error opening the file " + File;
  60. Temp += " to read from: ";
  61. Temp += strerror(errno);
  62. throw std::runtime_error(Temp);
  63. }
  64. std::streampos FileSize;
  65. FileSize = Input.tellg();
  66. Input.seekg(0, ios_base::end);
  67. FileSize = Input.tellg() - FileSize;
  68. if (FileSize > Size) {
  69. Input.seekg(-Size, ios_base::end);
  70. } else {
  71. Input.seekg(0, ios_base::beg);
  72. }
  73. std::ostringstream Output;
  74. if (!Input.eof()) { // Copy if there are characters.
  75. // Copying an empty file causes
  76. Output << Input.rdbuf(); // failbit to be set.
  77. }
  78. if (Output.bad() || Output.fail()) {
  79. std::string Temp;
  80. Temp = "Error reading last part of file " + File;
  81. Temp += ". Content read: '";
  82. Input.close();
  83. Temp += Output.str();
  84. Temp += "'";
  85. throw std::runtime_error(Temp);
  86. }
  87. Input.close();
  88. if (!Input) {
  89. std::string Temp;
  90. Temp = "Error closing the file " + File;
  91. Temp += " after reading: ";
  92. Temp += strerror(errno);
  93. throw std::runtime_error(Temp);
  94. }
  95. return Output.str();
  96. }
  97. void
  98. Utility::Copy(std::string From, std::string To) {
  99. if (Verbose()) {
  100. cout << "Copy " << From << " to " << To << "...";
  101. }
  102. if (!Explain()) {
  103. std::ifstream Input;
  104. Input.open(From.c_str());
  105. if (!Input) {
  106. std::string Temp;
  107. Temp = "Error opening the file " + From;
  108. Temp += " to read from: ";
  109. Temp += strerror(errno);
  110. throw std::runtime_error(Temp);
  111. }
  112. std::ofstream Output;
  113. Output.open(To.c_str(), std::ios::trunc);
  114. if (!Output) {
  115. std::string Temp;
  116. Temp = "Error opening the file " + To;
  117. Temp += " to copy to: ";
  118. Temp += strerror(errno);
  119. throw std::runtime_error(Temp);
  120. }
  121. if (!Input.eof()) { // Copy if there are characters.
  122. // Copying an empty file causes
  123. Output << Input.rdbuf(); // failbit to be set.
  124. }
  125. if (Output.bad() || Output.fail()) {
  126. std::string Temp;
  127. Temp = "Error copying " + From;
  128. Temp += " to " + To;
  129. Temp += ": ";
  130. Temp += strerror(errno);
  131. throw std::runtime_error(Temp);
  132. }
  133. Input.close();
  134. if (!Input) {
  135. std::string Temp;
  136. Temp = "Error closing the file " + From;
  137. Temp += ": ";
  138. Temp += strerror(errno);
  139. throw std::runtime_error(Temp);
  140. }
  141. Output.close();
  142. if (!Output) {
  143. std::string Temp;
  144. Temp = "Error closing the file " + To;
  145. Temp += ": ";
  146. Temp += strerror(errno);
  147. throw std::runtime_error(Temp);
  148. }
  149. }
  150. OutputVerboseEnd();
  151. }
  152. void
  153. Utility::SetOwnerGroup(std::string File) {
  154. struct passwd *SNFPasswd;
  155. uid_t SNFUid;
  156. struct group *SNFGroup;
  157. gid_t SNFGid;
  158. if (Verbose()) {
  159. cout << "Set owner:group of " << File << " to "
  160. << SNFUserName << ":" << SNFGroupName << "...";
  161. }
  162. if (!Explain()) {
  163. errno = 0;
  164. SNFPasswd = getpwnam(SNFUserName.c_str());
  165. if (SNFPasswd == 0) {
  166. string Temp;
  167. Temp = "Error getting info for Sniffer user " + SNFUserName;
  168. Temp += ": ";
  169. Temp += ((errno == 0) ? "No such user; create the user and try again." : strerror(errno));
  170. throw runtime_error(Temp);
  171. }
  172. SNFUid = SNFPasswd->pw_uid;
  173. SNFGid = SNFPasswd->pw_gid;
  174. if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
  175. string Temp;
  176. Temp = "Error changing group and owner of file " + File;
  177. Temp += " to " + SNFUserName + ":" + SNFGroupName;
  178. Temp += ": ";
  179. Temp += strerror(errno);
  180. throw runtime_error(Temp);
  181. }
  182. }
  183. OutputVerboseEnd();
  184. }
  185. void
  186. Utility::SetMode(std::string File, mode_t mode) {
  187. if (Verbose()) {
  188. cout << "Set mode of " << File << " to "
  189. << std::oct << mode << "...";
  190. }
  191. if (!Explain()) {
  192. if (chmod(File.c_str(), mode) != 0) {
  193. ostringstream Temp;
  194. Temp << "Error changing permissions of file " << File
  195. << " to " << mode << ": " << strerror(errno);
  196. throw runtime_error(Temp.str());
  197. }
  198. }
  199. OutputVerboseEnd();
  200. }
  201. void
  202. Utility::MkDir(std::string &Dir) {
  203. if (Verbose()) {
  204. cout << "Create directory " << Dir << "...";
  205. }
  206. if (!Explain()) {
  207. if (mkdir(Dir.c_str(), 0) != 0) {
  208. ostringstream Temp;
  209. Temp << "Error creating directory " << Dir << ": " << strerror(errno);
  210. throw runtime_error(Temp.str());
  211. }
  212. }
  213. OutputVerboseEnd();
  214. }
  215. void
  216. Utility::ReplaceXmlAttribute(std::string *Content, std::string ElementName, std::string AttributeName, std::string AttributeValue) {
  217. std::string::size_type ElementContentBegin; // Index of start of the element content.
  218. std::string::size_type ElementContentEnd; // One past the end of the element content.
  219. ElementContentBegin = Content->find("<" + ElementName); // Get indices of element content.
  220. ElementContentEnd = Content->find("</" + ElementName);
  221. if (std::string::npos == ElementContentBegin) {
  222. std::string Temp;
  223. Temp = "Unable to find the start of element '" + ElementName;
  224. Temp += "'.";
  225. throw std::runtime_error(Temp);
  226. }
  227. if (std::string::npos != Content->find("<" + ElementName, ElementContentBegin + 1)) {
  228. std::string Temp;
  229. Temp = "Found two elements named '" + ElementName;
  230. Temp += "'; there must be only one.";
  231. throw std::runtime_error(Temp);
  232. }
  233. if (std::string::npos == ElementContentEnd) {
  234. ElementContentEnd = Content->find("/>", ElementContentBegin);
  235. }
  236. if (std::string::npos == ElementContentEnd){
  237. std::string Temp;
  238. Temp = "Unable to find the end of element '" + ElementName;
  239. Temp += "'.";
  240. throw std::runtime_error(Temp);
  241. }
  242. ElementContentBegin += ElementName.length() + 1; // Skip element name.
  243. std::string::size_type ProvisionalAttributeNameBegin = ElementContentBegin;
  244. std::string::size_type AttributeNameBegin ;
  245. bool FoundAttribute = false;
  246. std::string PrevChar;
  247. while (ProvisionalAttributeNameBegin < ElementContentEnd) { // Find start of attribute name.
  248. ProvisionalAttributeNameBegin = Content->find(AttributeName, ProvisionalAttributeNameBegin);
  249. if ( (ProvisionalAttributeNameBegin == std::string::npos) || (ProvisionalAttributeNameBegin >= ElementContentEnd) ) {
  250. break;
  251. }
  252. PrevChar = Content->at(ProvisionalAttributeNameBegin - 1);
  253. if (std::string::npos == PrevChar.find_first_not_of(" \t\r\n")) { // Check for whitespace before the attribute.
  254. if (FoundAttribute) {
  255. std::string Temp;
  256. Temp = "Found two attributes named '" + AttributeName;
  257. Temp += "' in element '" + ElementName;
  258. Temp += "'; there must be only one.";
  259. throw std::runtime_error(Temp);
  260. }
  261. FoundAttribute = true;
  262. AttributeNameBegin = ProvisionalAttributeNameBegin;
  263. }
  264. ProvisionalAttributeNameBegin = ProvisionalAttributeNameBegin + AttributeName.length(); // Skip.
  265. }
  266. if (!FoundAttribute) {
  267. std::string Temp;
  268. Temp = "Unable to find the attribute '" + AttributeName;
  269. Temp += "' in element '" + ElementName;
  270. Temp += "'.";
  271. throw std::runtime_error(Temp);
  272. }
  273. std::string::size_type EqualIndex; // Index of "=".
  274. std::string::size_type DelimiterIndex; // Index of delimiter of attribute value.
  275. std::string DelimiterValue; // Attribute value delimiter value.
  276. std::string::size_type AttributeValueBegin; // Index of start of attribute value.
  277. std::string::size_type AttributeValueEnd; // One past the end of the attribute value.
  278. EqualIndex = // Get index of first delimiter.
  279. Content->find_first_not_of(" \t\r\n", AttributeNameBegin + AttributeName.length());
  280. if ( (EqualIndex == std::string::npos) || (EqualIndex >= ElementContentEnd) ) {
  281. std::string Temp;
  282. Temp = "Unable to find \"=\" after '" + AttributeName;
  283. Temp += "' in element '" + ElementName;
  284. Temp += "'.";
  285. throw std::runtime_error(Temp);
  286. }
  287. DelimiterIndex = // Get index of first delimiter.
  288. Content->find_first_not_of(" \t\r\n", EqualIndex + 1);
  289. if ( (DelimiterIndex == std::string::npos) || (DelimiterIndex >= ElementContentEnd) ) {
  290. std::string Temp;
  291. Temp = "Unable to find start of value of attribute '" + AttributeName;
  292. Temp += "' in element '" + ElementName;
  293. Temp += "'.";
  294. throw std::runtime_error(Temp);
  295. }
  296. DelimiterValue = Content->at(DelimiterIndex);
  297. AttributeValueBegin = DelimiterIndex + 1;
  298. AttributeValueEnd = Content->find(DelimiterValue, AttributeValueBegin);
  299. if ( (AttributeValueEnd == std::string::npos) || (AttributeValueEnd >= ElementContentEnd) ) {
  300. std::string Temp;
  301. Temp = "Unable to find the end of the value of '" + AttributeName;
  302. Temp += "' in element '" + ElementName;
  303. Temp += "'.";
  304. throw std::runtime_error(Temp);
  305. }
  306. Content->replace(AttributeValueBegin,
  307. AttributeValueEnd - AttributeValueBegin,
  308. "");
  309. Content->insert(AttributeValueBegin, AttributeValue);
  310. }
  311. bool
  312. Utility::CheckForString(std::string Line, std::string SearchString) {
  313. string::size_type Indx;
  314. Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
  315. if (string::npos != Indx) {
  316. Line = Line.substr(Indx);
  317. }
  318. if (Line.substr(0, SearchString.length()) == SearchString) {
  319. return true;
  320. }
  321. return false;
  322. }
  323. std::string
  324. Utility::Trim(std::string String) {
  325. std::string Whitespace(" \n\r\t");
  326. std::string::size_type End = String.find_last_not_of(Whitespace);
  327. if (End == std::string::npos) {
  328. return std::string();
  329. }
  330. std::string::size_type Start = String.find_first_not_of(Whitespace);
  331. if (Start == std::string::npos) {
  332. Start = 0;
  333. }
  334. return String.substr(Start, (End - Start) + 1);
  335. }
  336. void
  337. Utility::SetDebug(bool Mode) {
  338. DebugRequested = Mode;
  339. }
  340. bool
  341. Utility::Debug() {
  342. return DebugRequested;
  343. }
  344. void
  345. Utility::SetVerbose(bool Mode) {
  346. VerboseRequested = Mode;
  347. }
  348. bool
  349. Utility::Verbose() {
  350. return (VerboseRequested || ExplainRequested);
  351. }
  352. void
  353. Utility::SetExplain(bool Mode) {
  354. ExplainRequested = Mode;
  355. }
  356. bool
  357. Utility::Explain() {
  358. return ExplainRequested;
  359. }
  360. void
  361. Utility::SetHelp(bool Mode) {
  362. HelpRequested = Mode;
  363. }
  364. bool
  365. Utility::Help() {
  366. return HelpRequested;
  367. }
  368. void
  369. Utility::OutputVerboseEnd() {
  370. if (Verbose() && !Explain()) {
  371. cout << "done.\n";
  372. } else if (Explain()) {
  373. cout << "\n";
  374. }
  375. }