Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Utility.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. bool
  170. Utility::CheckForString(std::string Line, std::string SearchString) {
  171. string::size_type Indx;
  172. Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
  173. if (string::npos != Indx) {
  174. Line = Line.substr(Indx);
  175. }
  176. if (Line.substr(0, SearchString.length()) == SearchString) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. std::string
  182. Utility::Trim(std::string String) {
  183. std::string Whitespace(" \n\r\t");
  184. std::string::size_type End = String.find_last_not_of(Whitespace);
  185. if (End == std::string::npos) {
  186. return std::string();
  187. }
  188. std::string::size_type Start = String.find_first_not_of(Whitespace);
  189. if (Start == std::string::npos) {
  190. Start = 0;
  191. }
  192. return String.substr(Start, (End - Start) + 1);
  193. }
  194. void
  195. Utility::SetVerbose(bool Mode) {
  196. VerboseRequested = Mode;
  197. }
  198. bool
  199. Utility::Verbose() {
  200. return (VerboseRequested || ExplainRequested);
  201. }
  202. void
  203. Utility::SetExplain(bool Mode) {
  204. ExplainRequested = Mode;
  205. }
  206. bool
  207. Utility::Explain() {
  208. return ExplainRequested;
  209. }
  210. void
  211. Utility::SetHelp(bool Mode) {
  212. HelpRequested = Mode;
  213. }
  214. bool
  215. Utility::Help() {
  216. return HelpRequested;
  217. }
  218. void
  219. Utility::OutputVerboseEnd() {
  220. if (Verbose() && !Explain()) {
  221. cout << "done.\n";
  222. } else if (Explain()) {
  223. cout << "\n";
  224. }
  225. }