// 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 #include #include #include #include #include #include #include #include #include #include #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"; } }