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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. Output << Input.rdbuf();
  122. if (Output.bad()) { // Copying an empty file causes
  123. std::string Temp; // failbit to be set.
  124. Temp = "Error copying " + From;
  125. Temp += " to " + To;
  126. Temp += ": ";
  127. Temp += strerror(errno);
  128. throw std::runtime_error(Temp);
  129. }
  130. Input.close();
  131. if (!Input) {
  132. std::string Temp;
  133. Temp = "Error closing the file " + From;
  134. Temp += ": ";
  135. Temp += strerror(errno);
  136. throw std::runtime_error(Temp);
  137. }
  138. Output.close();
  139. if (!Output) {
  140. std::string Temp;
  141. Temp = "Error closing the file " + To;
  142. Temp += ": ";
  143. Temp += strerror(errno);
  144. throw std::runtime_error(Temp);
  145. }
  146. }
  147. OutputVerboseEnd();
  148. }
  149. void
  150. Utility::SetOwnerGroup(std::string File) {
  151. struct passwd *SNFPasswd;
  152. uid_t SNFUid;
  153. struct group *SNFGroup;
  154. gid_t SNFGid;
  155. if (Verbose()) {
  156. cout << "Set owner:group of " << File << " to "
  157. << SNFUserName << ":" << SNFGroupName << "...";
  158. }
  159. if (!Explain()) {
  160. errno = 0;
  161. SNFPasswd = getpwnam(SNFUserName.c_str());
  162. if (SNFPasswd == 0) {
  163. string Temp;
  164. Temp = "Error getting info for Sniffer user " + SNFUserName;
  165. Temp += ": ";
  166. Temp += ((errno == 0) ? "No such user; create the user and try again." : strerror(errno));
  167. throw runtime_error(Temp);
  168. }
  169. SNFUid = SNFPasswd->pw_uid;
  170. SNFGid = SNFPasswd->pw_gid;
  171. if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
  172. string Temp;
  173. Temp = "Error changing group and owner of file " + File;
  174. Temp += " to " + SNFUserName + ":" + SNFGroupName;
  175. Temp += ": ";
  176. Temp += strerror(errno);
  177. throw runtime_error(Temp);
  178. }
  179. }
  180. OutputVerboseEnd();
  181. }
  182. void
  183. Utility::SetMode(std::string File, mode_t mode) {
  184. if (Verbose()) {
  185. cout << "Set mode of " << File << " to "
  186. << std::oct << mode << "...";
  187. }
  188. if (!Explain()) {
  189. if (chmod(File.c_str(), mode) != 0) {
  190. ostringstream Temp;
  191. Temp << "Error changing permissions of file " << File
  192. << " to " << mode << ": " << strerror(errno);
  193. throw runtime_error(Temp.str());
  194. }
  195. }
  196. OutputVerboseEnd();
  197. }
  198. void
  199. Utility::MkDir(std::string &Dir) {
  200. if (Verbose()) {
  201. cout << "Create directory " << Dir << "...";
  202. }
  203. if (!Explain()) {
  204. if (mkdir(Dir.c_str(), 0) != 0) {
  205. ostringstream Temp;
  206. Temp << "Error creating directory " << Dir << ": " << strerror(errno);
  207. throw runtime_error(Temp.str());
  208. }
  209. }
  210. OutputVerboseEnd();
  211. }
  212. void
  213. Utility::ReplaceXmlAttribute(std::string *Content, std::string ElementName, std::string AttributeName, std::string AttributeValue) {
  214. bool FoundElement;
  215. FoundElement = false;
  216. std::string::size_type ElementContentBegin; // Index of start of the element content.
  217. std::string::size_type ElementContentEnd; // One past the end of the element content.
  218. ElementContentBegin = 0;
  219. while (!FoundElement) {
  220. ElementContentBegin = Content->find("<" + ElementName, ElementContentBegin); // Get indices of element content.
  221. ElementContentEnd = Content->find("</" + ElementName, ElementContentBegin);
  222. if (std::string::npos == ElementContentBegin) {
  223. std::string Temp;
  224. Temp = "Unable to find the start of element '" + ElementName;
  225. Temp += "'.";
  226. throw std::runtime_error(Temp);
  227. }
  228. // Check whether the element is in a comment.
  229. std::string::size_type PrevCommentBegin;
  230. std::string::size_type PrevCommentEnd;
  231. PrevCommentBegin = Content->rfind("<!--", ElementContentBegin);
  232. PrevCommentEnd = Content->rfind("-->", ElementContentBegin);
  233. if ( (PrevCommentBegin == std::string::npos) ||
  234. ( (PrevCommentEnd < ElementContentBegin) &&
  235. (PrevCommentEnd > PrevCommentBegin) ) ) {
  236. FoundElement = true;
  237. break; // Not in comment; continue processing.
  238. }
  239. ElementContentBegin++; // In comment; continue search.
  240. }
  241. if (std::string::npos != Content->find("<" + ElementName, ElementContentBegin + 1)) {
  242. std::string Temp;
  243. Temp = "Found two elements named '" + ElementName;
  244. Temp += "'; there must be only one.";
  245. throw std::runtime_error(Temp);
  246. }
  247. if (std::string::npos == ElementContentEnd) {
  248. ElementContentEnd = Content->find("/>", ElementContentBegin);
  249. }
  250. if (std::string::npos == ElementContentEnd){
  251. std::string Temp;
  252. Temp = "Unable to find the end of element '" + ElementName;
  253. Temp += "'.";
  254. throw std::runtime_error(Temp);
  255. }
  256. ElementContentBegin += ElementName.length() + 1; // Skip element name.
  257. std::string::size_type ProvisionalAttributeNameBegin = ElementContentBegin;
  258. std::string::size_type AttributeNameBegin ;
  259. bool FoundAttribute = false;
  260. std::string PrevChar;
  261. while (ProvisionalAttributeNameBegin < ElementContentEnd) { // Find start of attribute name.
  262. ProvisionalAttributeNameBegin = Content->find(AttributeName, ProvisionalAttributeNameBegin);
  263. if ( (ProvisionalAttributeNameBegin == std::string::npos) || (ProvisionalAttributeNameBegin >= ElementContentEnd) ) {
  264. break;
  265. }
  266. PrevChar = Content->at(ProvisionalAttributeNameBegin - 1);
  267. if (std::string::npos == PrevChar.find_first_not_of(" \t\r\n")) { // Check for whitespace before the attribute.
  268. if (FoundAttribute) {
  269. std::string Temp;
  270. Temp = "Found two attributes named '" + AttributeName;
  271. Temp += "' in element '" + ElementName;
  272. Temp += "'; there must be only one.";
  273. throw std::runtime_error(Temp);
  274. }
  275. FoundAttribute = true;
  276. AttributeNameBegin = ProvisionalAttributeNameBegin;
  277. }
  278. ProvisionalAttributeNameBegin = ProvisionalAttributeNameBegin + AttributeName.length(); // Skip.
  279. }
  280. if (!FoundAttribute) {
  281. std::string Temp;
  282. Temp = "Unable to find the attribute '" + AttributeName;
  283. Temp += "' in element '" + ElementName;
  284. Temp += "'.";
  285. throw std::runtime_error(Temp);
  286. }
  287. std::string::size_type EqualIndex; // Index of "=".
  288. std::string::size_type DelimiterIndex; // Index of delimiter of attribute value.
  289. std::string DelimiterValue; // Attribute value delimiter value.
  290. std::string::size_type AttributeValueBegin; // Index of start of attribute value.
  291. std::string::size_type AttributeValueEnd; // One past the end of the attribute value.
  292. EqualIndex = // Get index of first delimiter.
  293. Content->find_first_not_of(" \t\r\n", AttributeNameBegin + AttributeName.length());
  294. if ( (EqualIndex == std::string::npos) || (EqualIndex >= ElementContentEnd) ) {
  295. std::string Temp;
  296. Temp = "Unable to find \"=\" after '" + AttributeName;
  297. Temp += "' in element '" + ElementName;
  298. Temp += "'.";
  299. throw std::runtime_error(Temp);
  300. }
  301. DelimiterIndex = // Get index of first delimiter.
  302. Content->find_first_not_of(" \t\r\n", EqualIndex + 1);
  303. if ( (DelimiterIndex == std::string::npos) || (DelimiterIndex >= ElementContentEnd) ) {
  304. std::string Temp;
  305. Temp = "Unable to find start of value of attribute '" + AttributeName;
  306. Temp += "' in element '" + ElementName;
  307. Temp += "'.";
  308. throw std::runtime_error(Temp);
  309. }
  310. DelimiterValue = Content->at(DelimiterIndex);
  311. AttributeValueBegin = DelimiterIndex + 1;
  312. AttributeValueEnd = Content->find(DelimiterValue, AttributeValueBegin);
  313. if ( (AttributeValueEnd == std::string::npos) || (AttributeValueEnd >= ElementContentEnd) ) {
  314. std::string Temp;
  315. Temp = "Unable to find the end of the value of '" + AttributeName;
  316. Temp += "' in element '" + ElementName;
  317. Temp += "'.";
  318. throw std::runtime_error(Temp);
  319. }
  320. Content->replace(AttributeValueBegin,
  321. AttributeValueEnd - AttributeValueBegin,
  322. "");
  323. Content->insert(AttributeValueBegin, AttributeValue);
  324. }
  325. bool
  326. Utility::CheckForString(std::string Line, std::string SearchString) {
  327. string::size_type Indx;
  328. Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
  329. if (string::npos != Indx) {
  330. Line = Line.substr(Indx);
  331. }
  332. if (Line.substr(0, SearchString.length()) == SearchString) {
  333. return true;
  334. }
  335. return false;
  336. }
  337. std::string
  338. Utility::Trim(std::string String) {
  339. std::string Whitespace(" \n\r\t");
  340. std::string::size_type End = String.find_last_not_of(Whitespace);
  341. if (End == std::string::npos) {
  342. return std::string();
  343. }
  344. std::string::size_type Start = String.find_first_not_of(Whitespace);
  345. if (Start == std::string::npos) {
  346. Start = 0;
  347. }
  348. return String.substr(Start, (End - Start) + 1);
  349. }
  350. void
  351. Utility::SetDebug(bool Mode) {
  352. DebugRequested = Mode;
  353. }
  354. bool
  355. Utility::Debug() {
  356. return DebugRequested;
  357. }
  358. void
  359. Utility::SetVerbose(bool Mode) {
  360. VerboseRequested = Mode;
  361. }
  362. bool
  363. Utility::Verbose() {
  364. return (VerboseRequested || ExplainRequested);
  365. }
  366. void
  367. Utility::SetExplain(bool Mode) {
  368. ExplainRequested = Mode;
  369. }
  370. bool
  371. Utility::Explain() {
  372. return ExplainRequested;
  373. }
  374. void
  375. Utility::SetHelp(bool Mode) {
  376. HelpRequested = Mode;
  377. }
  378. bool
  379. Utility::Help() {
  380. return HelpRequested;
  381. }
  382. void
  383. Utility::OutputVerboseEnd() {
  384. if (Verbose() && !Explain()) {
  385. cout << "done.\n";
  386. } else if (Explain()) {
  387. cout << "\n";
  388. }
  389. }