1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * CommunicationProtocol.cxx :
25 * Created on: 14 sept. 2009
26 * Author : Renaud BARATE - EDF R&D
31 #include <libbatch_config.h>
33 #include "CommunicationProtocol.hxx"
35 #include "CommunicationProtocolRSH.hxx"
38 #include "CommunicationProtocolSH.hxx"
41 #include "CommunicationProtocolSSH.hxx"
44 #include "CommunicationProtocolRsync.hxx"
46 #include "APIInternalFailureException.hxx"
47 #include "RunTimeException.hxx"
55 CommunicationProtocol::CommunicationProtocol(CommunicationProtocolType type)
60 CommunicationProtocol::~CommunicationProtocol()
64 const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
66 if (protocolType == SH) {
68 static CommunicationProtocolSH instanceSH;
71 throw RunTimeException("Can't use SH protocol (SH tools were "
72 "not found on the system at compile time).");
74 } else if (protocolType == RSH) {
76 static CommunicationProtocolRSH instanceRSH;
79 throw RunTimeException("Can't use RSH protocol (RSH tools were "
80 "not found on the system at compile time).");
82 } else if (protocolType == SSH) {
84 static CommunicationProtocolSSH instanceSSH;
87 throw RunTimeException("Can't use SSH protocol (SSH tools were "
88 "not found on the system at compile time).");
90 } else if (protocolType == RSYNC) {
92 static CommunicationProtocolRsync instanceRsync;
95 throw RunTimeException("Can't use RSYNC protocol (RSYNC tools were "
96 "not found on the system at compile time).");
99 throw APIInternalFailureException("Unknown communication protocol.");
102 string CommunicationProtocol::getExecCommand(const string & subCommand,
104 const string & user) const
106 return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
109 int CommunicationProtocol::copyFile(const std::string & sourcePath,
110 const std::string & sourceHost,
111 const std::string & sourceUser,
112 const std::string & destinationPath,
113 const std::string & destinationHost,
114 const std::string & destinationUser) const
116 string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
117 destinationPath, destinationHost,
120 int status = system(command.c_str());
124 string CommunicationProtocol::getRemoveSubCommand(const string & path) const
126 return string("rm ") + path;
129 string CommunicationProtocol::getRemoveDirectorySubCommand(const string & path) const
131 return string("rm -fR ") + path;
134 string CommunicationProtocol::getMakeDirectorySubCommand(const string & path) const
136 return string("mkdir -p ") + path;
139 int CommunicationProtocol::removeFile(const std::string & path,
140 const std::string & host,
141 const std::string & user) const
143 string command = getExecCommand(getRemoveSubCommand(path), host, user);
145 int status = system(command.c_str());
149 int CommunicationProtocol::removeDirectory(const std::string & path,
150 const std::string & host,
151 const std::string & user) const
153 string command = getExecCommand(getRemoveDirectorySubCommand(path), host, user);
155 int status = system(command.c_str());
159 int CommunicationProtocol::makeDirectory(const std::string & path,
160 const std::string & host,
161 const std::string & user) const
163 string command = getExecCommand(getMakeDirectorySubCommand(path), host, user);
165 int status = system(command.c_str());
169 string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
173 // On Windows we surround the whole command with quotes to avoid problems when
174 // we have several quoted arguments.
179 for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
180 if (i != 0) commandStr += " ";
182 // if the argument contains spaces, we surround it with simple quotes (Linux)
183 // or double quotes (Windows)
184 if (commandArgs[i].find(' ') != string::npos &&
185 !Utils::isOption(commandArgs[i])){
186 commandStr += string("\"") + commandArgs[i] + "\"";
188 commandStr += commandArgs[i];
199 CommunicationProtocolType CommunicationProtocol::getType() const