Salome HOME
[bos #32518][EDF] (2022-T3)
[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 #include "CommunicationProtocolRSH.hxx"
35 #include "CommunicationProtocolSH.hxx"
36 #include "CommunicationProtocolSSH.hxx"
37 #include "CommunicationProtocolRsync.hxx"
38 #include "APIInternalFailureException.hxx"
39 #include "RunTimeException.hxx"
40 #include "Log.hxx"
41 #include "Utils.hxx"
42
43 using namespace std;
44
45 namespace Batch {
46
47   CommunicationProtocol::CommunicationProtocol(CommunicationProtocolType type)
48   : _type(type)
49   {
50   }
51
52   CommunicationProtocol::~CommunicationProtocol()
53   {
54   }
55
56   const CommunicationProtocol & CommunicationProtocol::getInstance(CommunicationProtocolType protocolType)
57   {
58     if (protocolType == SH) {
59       static CommunicationProtocolSH instanceSH;
60       return instanceSH;
61     } else if (protocolType == RSH) {
62       static CommunicationProtocolRSH instanceRSH;
63       return instanceRSH;
64     } else if (protocolType == SSH) {
65       static CommunicationProtocolSSH instanceSSH;
66       return instanceSSH;
67     } else if (protocolType == RSYNC) {
68       static CommunicationProtocolRsync instanceRsync;
69       return instanceRsync;
70     } else
71       throw APIInternalFailureException("Unknown communication protocol.");
72   }
73
74   string CommunicationProtocol::getExecCommand(const string & subCommand,
75                                                const string & host,
76                                                const string & user) const
77   {
78     return commandStringFromArgs(getExecCommandArgs(subCommand, host, user));
79   }
80
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
87   {
88     string command = commandStringFromArgs(getCopyCommandArgs(sourcePath, sourceHost, sourceUser,
89                                                               destinationPath, destinationHost,
90                                                               destinationUser));
91     LOG(command);
92     int status = system(command.c_str());
93     return status;
94   }
95
96   string CommunicationProtocol::getRemoveSubCommand(const string & path) const
97   {
98 #ifdef WIN32
99     return string("del /s ") + path;
100 #else
101     return string("rm ") + path;
102 #endif
103   }
104
105   string CommunicationProtocol::getRemoveDirectorySubCommand(const string & path) const
106   {
107 #ifdef WIN32
108     return string("rd /s /q ") + path;
109 #else
110     return string("rm -fR ") + path;
111 #endif
112   }
113
114   string CommunicationProtocol::getMakeDirectorySubCommand(const string & path) const
115   {
116 #ifdef WIN32
117     return string("md ") + path;
118 #else
119     return string("mkdir -p ") + path;
120 #endif
121   }
122
123   int CommunicationProtocol::removeFile(const std::string & path,
124                                         const std::string & host,
125                                         const std::string & user) const
126   {
127     string command = getExecCommand(getRemoveSubCommand(path), host, user);
128     LOG(command);
129     int status = system(command.c_str());
130     return status;
131   }
132
133   int CommunicationProtocol::removeDirectory(const std::string & path,
134                                         const std::string & host,
135                                         const std::string & user) const
136   {
137     string command = getExecCommand(getRemoveDirectorySubCommand(path), host, user);
138     LOG(command);
139     int status = system(command.c_str());
140     return status;
141   }
142
143   int CommunicationProtocol::makeDirectory(const std::string & path,
144                                            const std::string & host,
145                                            const std::string & user) const
146   {
147     string command = getExecCommand(getMakeDirectorySubCommand(path), host, user);
148     LOG(command);
149     int status = system(command.c_str());
150     return status;
151   }
152
153   string CommunicationProtocol::commandStringFromArgs(const vector<string> & commandArgs) const
154   {
155     string commandStr;
156
157     // On Windows we surround the whole command with quotes to avoid problems when
158     // we have several quoted arguments.
159 #ifdef WIN32
160     commandStr += "\"";
161 #endif
162
163     for (unsigned int i=0 ; i<commandArgs.size() ; i++) {
164       if (i != 0) commandStr += " ";
165
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] + "\"";
171       } else {
172         commandStr += commandArgs[i];
173       }
174     }
175
176 #ifdef WIN32
177     commandStr += "\"";
178 #endif
179
180     return commandStr;
181   }
182
183   CommunicationProtocolType CommunicationProtocol::getType() const
184   {
185     return _type;
186   }
187
188 }