1 // Copyright (C) 2007-2021 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"
34 #include "CommunicationProtocolRSH.hxx"
35 #include "CommunicationProtocolSH.hxx"
36 #include "CommunicationProtocolSSH.hxx"
37 #include "CommunicationProtocolRsync.hxx"
38 #include "APIInternalFailureException.hxx"
39 #include "RunTimeException.hxx"
47 CommunicationProtocol::CommunicationProtocol(CommunicationProtocolType type)
52 CommunicationProtocol::~CommunicationProtocol()
56 const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
58 if (protocolType == SH) {
59 static CommunicationProtocolSH instanceSH;
61 } else if (protocolType == RSH) {
62 static CommunicationProtocolRSH instanceRSH;
64 } else if (protocolType == SSH) {
65 static CommunicationProtocolSSH instanceSSH;
67 } else if (protocolType == RSYNC) {
68 static CommunicationProtocolRsync instanceRsync;
71 throw APIInternalFailureException("Unknown communication protocol.");
74 string CommunicationProtocol::getExecCommand(const string & subCommand,
76 const string & user) const
78 return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
81 int CommunicationProtocol::copyFile(const std::string & sourcePath,
82 const std::string & sourceHost,
83 const std::string & sourceUser,
84 const std::string & destinationPath,
85 const std::string & destinationHost,
86 const std::string & destinationUser) const
88 string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
89 destinationPath, destinationHost,
92 int status = system(command.c_str());
96 string CommunicationProtocol::getRemoveSubCommand(const string & path) const
99 return string("del /s ") + path;
101 return string("rm ") + path;
105 string CommunicationProtocol::getRemoveDirectorySubCommand(const string & path) const
108 return string("rd /s /q ") + path;
110 return string("rm -fR ") + path;
114 string CommunicationProtocol::getMakeDirectorySubCommand(const string & path) const
117 return string("md ") + path;
119 return string("mkdir -p ") + path;
123 int CommunicationProtocol::removeFile(const std::string & path,
124 const std::string & host,
125 const std::string & user) const
127 string command = getExecCommand(getRemoveSubCommand(path), host, user);
129 int status = system(command.c_str());
133 int CommunicationProtocol::removeDirectory(const std::string & path,
134 const std::string & host,
135 const std::string & user) const
137 string command = getExecCommand(getRemoveDirectorySubCommand(path), host, user);
139 int status = system(command.c_str());
143 int CommunicationProtocol::makeDirectory(const std::string & path,
144 const std::string & host,
145 const std::string & user) const
147 string command = getExecCommand(getMakeDirectorySubCommand(path), host, user);
149 int status = system(command.c_str());
153 string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
157 // On Windows we surround the whole command with quotes to avoid problems when
158 // we have several quoted arguments.
163 for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
164 if (i != 0) commandStr += " ";
166 // if the argument contains spaces, we surround it with simple quotes (Linux)
167 // or double quotes (Windows)
168 if (commandArgs[i].find(' ') != string::npos &&
169 !Utils::isOption(commandArgs[i])){
170 commandStr += string("\"") + commandArgs[i] + "\"";
172 commandStr += commandArgs[i];
183 CommunicationProtocolType CommunicationProtocol::getType() const