Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / Core / CommunicationProtocol.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  *  CommunicationProtocol.cxx :
24  *
25  *  Created on: 14 sept. 2009
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <stdlib.h>
30
31 #include <libbatch_config.h>
32
33 #include "CommunicationProtocol.hxx"
34 #ifdef HAS_RSH
35  #include "CommunicationProtocolRSH.hxx"
36 #endif
37 #ifdef HAS_SH
38  #include "CommunicationProtocolSH.hxx"
39 #endif
40 #ifdef HAS_SSH
41  #include "CommunicationProtocolSSH.hxx"
42 #endif
43 #ifdef HAS_RSYNC
44  #include "CommunicationProtocolRsync.hxx"
45 #endif
46 #include "APIInternalFailureException.hxx"
47 #include "RunTimeException.hxx"
48 #include "Log.hxx"
49 #include "Utils.hxx"
50
51 using namespace std;
52
53 namespace Batch {
54
55   CommunicationProtocol::CommunicationProtocol(CommunicationProtocolType type)
56   : _type(type)
57   {
58   }
59
60   CommunicationProtocol::~CommunicationProtocol()
61   {
62   }
63
64   const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
65   {
66     if (protocolType == SH) {
67 #ifdef HAS_SH
68       static CommunicationProtocolSH instanceSH;
69       return instanceSH;
70 #else
71       throw RunTimeException("Can't use SH protocol (SH tools were "
72                              "not found on the system at compile time).");
73 #endif
74     } else if (protocolType == RSH) {
75 #ifdef HAS_RSH
76       static CommunicationProtocolRSH instanceRSH;
77       return instanceRSH;
78 #else
79       throw RunTimeException("Can't use RSH protocol (RSH tools were "
80                              "not found on the system at compile time).");
81 #endif
82     } else if (protocolType == SSH) {
83 #ifdef HAS_SSH
84       static CommunicationProtocolSSH instanceSSH;
85       return instanceSSH;
86 #else
87       throw RunTimeException("Can't use SSH protocol (SSH tools were "
88                              "not found on the system at compile time).");
89 #endif
90     } else if (protocolType == RSYNC) {
91 #ifdef HAS_RSYNC
92       static CommunicationProtocolRsync instanceRsync;
93       return instanceRsync;
94 #else
95       throw RunTimeException("Can't use RSYNC protocol (RSYNC tools were "
96                              "not found on the system at compile time).");
97 #endif
98     } else
99       throw APIInternalFailureException("Unknown communication protocol.");
100   }
101
102   string CommunicationProtocol::getExecCommand(const string & subCommand,
103                                                const string & host,
104                                                const string & user) const
105   {
106     return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
107   }
108
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
115   {
116     string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
117                                                               destinationPath, destinationHost,
118                                                               destinationUser));
119     LOG(command);
120     int status = system(command.c_str());
121     return status;
122   }
123
124   string CommunicationProtocol::getRemoveSubCommand(const string & path) const
125   {
126 #ifdef WIN32
127     return string("del /s ") + path;
128 #else
129     return string("rm ") + path;
130 #endif
131   }
132
133   string CommunicationProtocol::getRemoveDirectorySubCommand(const string & path) const
134   {
135 #ifdef WIN32
136     return string("rd /s /q ") + path;
137 #else
138     return string("rm -fR ") + path;
139 #endif
140   }
141
142   string CommunicationProtocol::getMakeDirectorySubCommand(const string & path) const
143   {
144 #ifdef WIN32
145     return string("md ") + path;
146 #else
147     return string("mkdir -p ") + path;
148 #endif
149   }
150
151   int CommunicationProtocol::removeFile(const std::string & path,
152                                         const std::string & host,
153                                         const std::string & user) const
154   {
155     string command = getExecCommand(getRemoveSubCommand(path), host, user);
156     LOG(command);
157     int status = system(command.c_str());
158     return status;
159   }
160
161   int CommunicationProtocol::removeDirectory(const std::string & path,
162                                         const std::string & host,
163                                         const std::string & user) const
164   {
165     string command = getExecCommand(getRemoveDirectorySubCommand(path), host, user);
166     LOG(command);
167     int status = system(command.c_str());
168     return status;
169   }
170
171   int CommunicationProtocol::makeDirectory(const std::string & path,
172                                            const std::string & host,
173                                            const std::string & user) const
174   {
175     string command = getExecCommand(getMakeDirectorySubCommand(path), host, user);
176     LOG(command);
177     int status = system(command.c_str());
178     return status;
179   }
180
181   string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
182   {
183     string commandStr;
184
185     // On Windows we surround the whole command with quotes to avoid problems when
186     // we have several quoted arguments.
187 #ifdef WIN32
188     commandStr += "\"";
189 #endif
190
191     for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
192       if (i != 0) commandStr += " ";
193
194       // if the argument contains spaces, we surround it with simple quotes (Linux)
195       // or double quotes (Windows)
196       if (commandArgs[i].find(' ') != string::npos &&
197           !Utils::isOption(commandArgs[i])){
198         commandStr += string("\"") + commandArgs[i] + "\"";
199       } else {
200         commandStr += commandArgs[i];
201       }
202     }
203
204 #ifdef WIN32
205     commandStr += "\"";
206 #endif
207
208     return commandStr;
209   }
210
211   CommunicationProtocolType CommunicationProtocol::getType() const
212   {
213     return _type;
214   }
215
216 }