Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Utility.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. bool
  33. Utility::FileExists(const std::string File) {
  34. if (Verbose()) {
  35. cout << "Check whether " << File << " exists...";
  36. }
  37. bool Exists;
  38. std::ifstream Input;
  39. errno = 0;
  40. Input.open(File.c_str());
  41. if (ENOENT == errno) {
  42. Exists = false;
  43. } else {
  44. Exists = true;
  45. }
  46. Input.close();
  47. OutputVerboseEnd();
  48. return Exists;
  49. }
  50. void
  51. Utility::Copy(std::string From, std::string To) {
  52. if (Verbose()) {
  53. cout << "Copy " << From << " to " << To << "...";
  54. }
  55. if (!Explain()) {
  56. std::ifstream Input;
  57. Input.open(From.c_str());
  58. if (!Input) {
  59. std::string Temp;
  60. Temp = "Error opening the file " + From;
  61. Temp += " to read from: ";
  62. Temp += strerror(errno);
  63. throw std::runtime_error(Temp);
  64. }
  65. std::ofstream Output;
  66. Output.open(To.c_str(), std::ios::trunc);
  67. if (!Output) {
  68. std::string Temp;
  69. Temp = "Error opening the file " + To;
  70. Temp += " to copy to: ";
  71. Temp += strerror(errno);
  72. throw std::runtime_error(Temp);
  73. }
  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 copying " + From;
  81. Temp += " to " + To;
  82. Temp += ": ";
  83. Temp += strerror(errno);
  84. throw std::runtime_error(Temp);
  85. }
  86. Input.close();
  87. if (!Input) {
  88. std::string Temp;
  89. Temp = "Error closing the file " + From;
  90. Temp += ": ";
  91. Temp += strerror(errno);
  92. throw std::runtime_error(Temp);
  93. }
  94. Output.close();
  95. if (!Output) {
  96. std::string Temp;
  97. Temp = "Error closing the file " + To;
  98. Temp += ": ";
  99. Temp += strerror(errno);
  100. throw std::runtime_error(Temp);
  101. }
  102. }
  103. OutputVerboseEnd();
  104. }
  105. void
  106. Utility::SetOwnerGroup(std::string &File) {
  107. struct passwd *SNFPasswd;
  108. uid_t SNFUid;
  109. struct group *SNFGroup;
  110. gid_t SNFGid;
  111. if (Verbose()) {
  112. cout << "Set owner:group of " << File << " to "
  113. << SNFUserName << ":" << SNFGroupName << "...";
  114. }
  115. if (!Explain()) {
  116. SNFPasswd = getpwnam(SNFUserName.c_str());
  117. if (SNFPasswd == 0) {
  118. string Temp;
  119. Temp = "Error getting info for Sniffer user " + SNFUserName;
  120. Temp += ": ";
  121. Temp += strerror(errno);
  122. throw runtime_error(Temp);
  123. }
  124. SNFUid = SNFPasswd->pw_uid;
  125. SNFGid = SNFPasswd->pw_gid;
  126. if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
  127. string Temp;
  128. Temp = "Error changing group and owner of file " + File;
  129. Temp += " to " + SNFUserName + ":" + SNFGroupName;
  130. Temp += ": ";
  131. Temp += strerror(errno);
  132. throw runtime_error(Temp);
  133. }
  134. }
  135. OutputVerboseEnd();
  136. }
  137. void
  138. Utility::SetMode(std::string &File, mode_t mode) {
  139. if (Verbose()) {
  140. cout << "Set mode of " << File << " to "
  141. << std::oct << mode << "...";
  142. }
  143. if (!Explain()) {
  144. if (chmod(File.c_str(), mode) != 0) {
  145. ostringstream Temp;
  146. Temp << "Error changing permissions of file " << File
  147. << " to " << mode << ": " << strerror(errno);
  148. throw runtime_error(Temp.str());
  149. }
  150. }
  151. OutputVerboseEnd();
  152. }
  153. void
  154. Utility::MkDir(std::string &Dir) {
  155. if (Verbose()) {
  156. cout << "Create directory " << Dir << "...";
  157. }
  158. if (!Explain()) {
  159. if (mkdir(Dir.c_str(), 0) != 0) {
  160. ostringstream Temp;
  161. Temp << "Error creating directory " << Dir << ": " << strerror(errno);
  162. throw runtime_error(Temp.str());
  163. }
  164. }
  165. OutputVerboseEnd();
  166. }
  167. bool
  168. Utility::CheckForString(std::string Line, std::string SearchString) {
  169. string::size_type Indx;
  170. Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
  171. if (string::npos != Indx) {
  172. Line = Line.substr(Indx);
  173. }
  174. if (Line.substr(0, SearchString.length()) == SearchString) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. void
  180. Utility::SetVerbose(bool Mode) {
  181. VerboseRequested = Mode;
  182. }
  183. bool
  184. Utility::Verbose() {
  185. return (VerboseRequested || ExplainRequested);
  186. }
  187. void
  188. Utility::SetExplain(bool Mode) {
  189. ExplainRequested = Mode;
  190. }
  191. bool
  192. Utility::Explain() {
  193. return ExplainRequested;
  194. }
  195. void
  196. Utility::SetHelp(bool Mode) {
  197. HelpRequested = Mode;
  198. }
  199. bool
  200. Utility::Help() {
  201. return HelpRequested;
  202. }
  203. void
  204. Utility::OutputVerboseEnd() {
  205. if (Verbose() && !Explain()) {
  206. cout << "done.\n";
  207. } else if (Explain()) {
  208. cout << "\n";
  209. }
  210. }