|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- // Utility.cpp
- //
- // Copyright (C) 2011, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This file implements the common functionality for the configuration
- // utilities.
-
- #include <cerrno>
- #include <cstring>
- #include <unistd.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include <stdexcept>
- #include <sstream>
- #include <iostream>
- #include <fstream>
- #include <vector>
-
- #include "Utility.hpp"
-
- using namespace std;
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- const std::string Utility::DirectorySeparator("/");
-
- /// SNF user name.
- const string SNFUserName = "snfuser";
-
- /// SNF group name.
- const string SNFGroupName = "snfuser";
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- bool
- Utility::FileExists(const std::string File) {
-
- if (Verbose()) {
-
- cout << "Check whether " << File << " exists...";
-
- }
-
- bool Exists;
- std::ifstream Input;
-
- errno = 0;
- Input.open(File.c_str());
-
- if (ENOENT == errno) {
-
- Exists = false;
-
- } else {
-
- Exists = true;
-
- }
-
- Input.close();
-
- OutputVerboseEnd();
-
- return Exists;
-
- }
-
- void
- Utility::Copy(std::string From, std::string To) {
-
- if (Verbose()) {
-
- cout << "Copy " << From << " to " << To << "...";
-
- }
-
- if (!Explain()) {
-
- std::ifstream Input;
-
- Input.open(From.c_str());
- if (!Input) {
- std::string Temp;
-
- Temp = "Error opening the file " + From;
- Temp += " to read from: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- std::ofstream Output;
-
- Output.open(To.c_str(), std::ios::trunc);
- if (!Output) {
- std::string Temp;
-
- Temp = "Error opening the file " + To;
- Temp += " to copy to: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- if (!Input.eof()) { // Copy if there are characters.
- // Copying an empty file causes
- Output << Input.rdbuf(); // failbit to be set.
-
- }
-
- if (Output.bad() || Output.fail()) {
- std::string Temp;
-
- Temp = "Error copying " + From;
- Temp += " to " + To;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Input.close();
- if (!Input) {
- std::string Temp;
-
- Temp = "Error closing the file " + From;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Output.close();
- if (!Output) {
- std::string Temp;
-
- Temp = "Error closing the file " + To;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
-
- void
- Utility::SetOwnerGroup(std::string &File) {
-
- struct passwd *SNFPasswd;
- uid_t SNFUid;
- struct group *SNFGroup;
- gid_t SNFGid;
-
- if (Verbose()) {
-
- cout << "Set owner:group of " << File << " to "
- << SNFUserName << ":" << SNFGroupName << "...";
-
- }
-
- if (!Explain()) {
-
- SNFPasswd = getpwnam(SNFUserName.c_str());
- if (SNFPasswd == 0) {
- string Temp;
-
- Temp = "Error getting info for Sniffer user " + SNFUserName;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
- SNFUid = SNFPasswd->pw_uid;
- SNFGid = SNFPasswd->pw_gid;
-
- if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
- string Temp;
-
- Temp = "Error changing group and owner of file " + File;
- Temp += " to " + SNFUserName + ":" + SNFGroupName;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- Utility::SetMode(std::string &File, mode_t mode) {
-
- if (Verbose()) {
-
- cout << "Set mode of " << File << " to "
- << std::oct << mode << "...";
-
- }
-
- if (!Explain()) {
-
- if (chmod(File.c_str(), mode) != 0) {
- ostringstream Temp;
-
- Temp << "Error changing permissions of file " << File
- << " to " << mode << ": " << strerror(errno);
- throw runtime_error(Temp.str());
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- Utility::MkDir(std::string &Dir) {
-
- if (Verbose()) {
-
- cout << "Create directory " << Dir << "...";
-
- }
-
- if (!Explain()) {
-
- if (mkdir(Dir.c_str(), 0) != 0) {
- ostringstream Temp;
-
- Temp << "Error creating directory " << Dir << ": " << strerror(errno);
- throw runtime_error(Temp.str());
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- bool
- Utility::CheckForString(std::string Line, std::string SearchString) {
-
- string::size_type Indx;
-
- Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
- if (string::npos != Indx) {
-
- Line = Line.substr(Indx);
-
- }
-
- if (Line.substr(0, SearchString.length()) == SearchString) {
-
- return true;
-
- }
-
- return false;
-
- }
-
- void
- Utility::SetVerbose(bool Mode) {
-
- VerboseRequested = Mode;
-
- }
-
- bool
- Utility::Verbose() {
-
- return (VerboseRequested || ExplainRequested);
-
- }
-
- void
- Utility::SetExplain(bool Mode) {
-
- ExplainRequested = Mode;
-
- }
-
- bool
- Utility::Explain() {
-
- return ExplainRequested;
-
- }
-
- void
- Utility::SetHelp(bool Mode) {
-
- HelpRequested = Mode;
-
- }
-
- bool
- Utility::Help() {
-
- return HelpRequested;
-
- }
-
- void
- Utility::OutputVerboseEnd() {
-
- if (Verbose() && !Explain()) {
-
- cout << "done.\n";
-
- } else if (Explain()) {
-
- cout << "\n";
-
- }
-
- }
|