Commit ca861e5b authored by Steven Murray's avatar Steven Murray
Browse files

Moved readFileIntoList() and parseFileList() from utils to TpcpCommand

parent 024e8834
......@@ -52,6 +52,7 @@
#include <ctype.h>
#include <exception>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <list>
......@@ -171,7 +172,7 @@ int castor::tape::tpcp::TpcpCommand::main(const int argc, char **argv) throw() {
try {
executeCommand();
} catch(castor::exception::Exception &ex) {
displayErrorMsgCleanUpAndExit(ex.getMessage().str().c_str());
displayErrorMsgCleanUpAndExit(ex.getMessage().str());
} catch(std::exception &se) {
displayErrorMsgCleanUpAndExit(se.what());
} catch(...) {
......@@ -203,7 +204,7 @@ int castor::tape::tpcp::TpcpCommand::determineCommandLineReturnCode()
// displayErrorMsgCleanUpAndExit
//------------------------------------------------------------------------------
void castor::tape::tpcp::TpcpCommand::displayErrorMsgCleanUpAndExit(
const char *msg) throw() {
const std::string &msg) throw() {
// Display error message
{
......@@ -287,8 +288,7 @@ void castor::tape::tpcp::TpcpCommand::executeCommand() {
if(m_cmdLine.fileListSet) {
// Parse the "filelist" file into the list of file names to be
// processed
utils::parseFileList(m_cmdLine.fileListFilename.c_str(),
m_filenames);
parseFileList(m_cmdLine.fileListFilename, m_filenames);
} else {
if(!m_cmdLine.nodataSet) {
// Copy the command-line argument file-names into the list of file-names
......@@ -1112,3 +1112,58 @@ void castor::tape::tpcp::TpcpCommand::localStat(const char *const path,
throw ex;
}
}
//------------------------------------------------------------------------------
// parseFileList
//------------------------------------------------------------------------------
void castor::tape::tpcp::TpcpCommand::parseFileList(const std::string &filename,
std::list<std::string> &list) {
readFileIntoList(filename, list);
std::list<std::string>::iterator itor=list.begin();
while(itor!=list.end()) {
std::string &line = *itor;
// Left and right trim the line
line = castor::utils::trimString(line);
// Remove the line if it is an empty string or if it starts with the shell
// comment character '#'
if(line.empty() || (line.size() > 0 && line[0] == '#')) {
itor = list.erase(itor);
} else {
itor++;
}
}
}
//------------------------------------------------------------------------------
// readFileIntoList
//------------------------------------------------------------------------------
void castor::tape::tpcp::TpcpCommand::readFileIntoList(
const std::string &filename, std::list<std::string> &lines) {
std::ifstream file(filename.c_str());
if(!file) {
castor::exception::Exception ex(ECANCELED);
ex.getMessage() << "Failed to open file: Filename='" << filename << "'";
throw ex;
}
std::string line;
while(!file.eof()) {
line.clear();
std::getline(file, line, '\n');
if(!line.empty() || !file.eof()) {
lines.push_back(line);
}
}
}
......@@ -386,41 +386,43 @@ protected:
private:
/**
* Displays the specified error message, cleans up (at least deletes the
* volume-request id if there is one) and then calls exit(1) indicating an
* error.
* The SIGINT signal handler.
*/
void displayErrorMsgCleanUpAndExit(
const char *msg) throw();
static void sigintHandler(int signal);
/**
* Executes the main code of the command.
*
* The specification of this method intentionally does not have a throw()
* clause so that any type of exception can be thrown.
* The SIGINT action handler structure to be used with sigaction.
*/
void executeCommand();
struct sigaction m_sigintAction;
/**
* Determines and returns the numeric code that should be returned by this
* command-line tool.
* The current working directory where tpcp command is run.
*/
int determineCommandLineReturnCode() const throw();
char m_cwd[CA_MAXPATHLEN+1];
/**
* Deletes the specified VDQM volume request.
* Structure used to describe a tape session error.
*/
void deleteVdqmVolumeRequest();
struct TapeSessionError {
int errorCode;
std::string errorCodeString;
std::string errorMessage;
TapeSessionError(): errorCode(0), errorCodeString(""),
errorMessage("Success") {
}
};
/**
* The SIGINT signal handler.
* If the tape server reported a tape session error then this member variable
* shall contain a decription of that error.
*/
static void sigintHandler(int signal);
TapeSessionError m_tapeSessionErrorReportedByTapeServer;
/**
* The SIGINT action handler structure to be used with sigaction.
* True if the tape server reported a tape session error.
*/
struct sigaction m_sigintAction;
bool m_tapeServerReportedATapeSessionError;
/**
* This method prefixes the local hostname onto the beginning of local
......@@ -432,38 +434,64 @@ private:
void translateFilenamesIntoRemoteFilenames();
/**
* The current working directory where tpcp command is run.
* Displays the specified error message, cleans up (at least deletes the
* volume-request id if there is one) and then calls exit(1) indicating an
* error.
*/
char m_cwd[CA_MAXPATHLEN+1];
void displayErrorMsgCleanUpAndExit(const std::string &msg) throw();
/**
* Structure used to describe a tape session error.
* Executes the main code of the command.
*
* The specification of this method intentionally does not have a throw()
* clause so that any type of exception can be thrown.
*/
struct TapeSessionError {
int errorCode;
std::string errorCodeString;
std::string errorMessage;
void executeCommand();
TapeSessionError(): errorCode(0), errorCodeString(""),
errorMessage("Success") {
}
};
/**
* Determines and returns the numeric code that should be returned by this
* command-line tool.
*/
int determineCommandLineReturnCode() const throw();
/**
* If the tape server reported a tape session error then this member variable
* shall contain a decription of that error.
* Deletes the specified VDQM volume request.
*/
TapeSessionError m_tapeSessionErrorReportedByTapeServer;
void deleteVdqmVolumeRequest();
/**
* True if the tape server reported a tape session error.
* Appends to the specified list the filenames from the "filelist" file with
* the specified filename.
*
* This method:
* <ul>
* <li>Trims leading and trailing white space from each line
* <li>Ignores blank lines with or without white space.
* <li>Ignores comment lines, i.e. those starting with a '#' after their
* leading and trailing white space has been trimmed.
* </ul>
*
* @param filename The filename of the "filelist" file.
* @param list The list to which the filenames will be appended.
*/
bool m_tapeServerReportedATapeSessionError;
void parseFileList(const std::string &filename, std::list<std::string> &list);
/**
* Appends each line of the specified file to the specified list of lines.
* The new-line characters are extracted from the file, but they are not
* stored in the lines appended to the list.
*
* An empty line, with or without a delimiting '\n' character will be appended
* to the list od lines as an empty string.
*
* @param filename The filename of the file to be read.
* @param lines The list to which each line of the file will be appended.
*/
void readFileIntoList(const std::string &filename,
std::list<std::string> &lines);
}; // class TpcpCommand
} // namespace tpcp
} // namespace tape
} // namespace castor
......@@ -91,61 +91,6 @@ const char *castor::tape::utils::volumeModeToString(
}
}
//------------------------------------------------------------------------------
// readFileIntoList
//------------------------------------------------------------------------------
void castor::tape::utils::readFileIntoList(const char *const filename,
std::list<std::string> &lines) {
std::ifstream file(filename);
if(!file) {
castor::exception::Exception ex(ECANCELED);
ex.getMessage() << "Failed to open file: Filename='" << filename << "'";
throw ex;
}
std::string line;
while(!file.eof()) {
line.clear();
std::getline(file, line, '\n');
if(!line.empty() || !file.eof()) {
lines.push_back(line);
}
}
}
//------------------------------------------------------------------------------
// parseFileList
//------------------------------------------------------------------------------
void castor::tape::utils::parseFileList(const char *filename,
std::list<std::string> &list) {
readFileIntoList(filename, list);
std::list<std::string>::iterator itor=list.begin();
while(itor!=list.end()) {
std::string &line = *itor;
// Left and right trim the line
line = castor::utils::trimString(line);
// Remove the line if it is an empty string or if it starts with the shell
// comment character '#'
if(line.empty() || (line.size() > 0 && line[0] == '#')) {
itor = list.erase(itor);
} else {
itor++;
}
}
}
//------------------------------------------------------------------------------
// getPortFromConfig
//------------------------------------------------------------------------------
......
......@@ -69,37 +69,6 @@ const char *volumeClientTypeToString(const tapegateway::ClientType mode)
*/
const char *volumeModeToString(const tapegateway::VolumeMode mode) throw();
/**
* Appends each line of the specified file to the specified list of lines.
* The new-line characters are extracted from the file, but they are not
* stored in the lines appended to the list.
*
* An empty line, with or without a delimiting '\n' character will be appended
* to the list od lines as an empty string.
*
* @param filename The filename of the file to be read.
* @param lines The list to which each line of the file will be appended.
*/
void readFileIntoList(const char *const filename,
std::list<std::string> &lines) ;
/**
* Appends to the specified list the filenames from the "filelist" file with
* the specified filename.
*
* This method:
* <ul>
* <li>Trims leading and trailing white space from each line
* <li>Ignores blank lines with or without white space.
* <li>Ignores comment lines, i.e. those starting with a '#' after their
* leading and trailing white space has been trimmed.
* </ul>
*
* @param filename The filename of the "filelist" file.
* @param list The list to which the filenames will be appended.
*/
void parseFileList(const char *filename, std::list<std::string> &list);
/**
* Gets and returns the specified port number using getconfent or returns the
* specified default if getconfent cannot find it.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment