]> SALOME platform Git repositories - tools/libbatch.git/blob - src/Core/Batch_CommunicationProtocolRSH.cxx
Salome HOME
Refactored submission based on SH, RSH and SSH by grouping related commands in Commun...
[tools/libbatch.git] / src / Core / Batch_CommunicationProtocolRSH.cxx
1 //  Copyright (C) 2007-2009  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.
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  *  Batch_CommunicationProtocolRSH.cxx :
24  *
25  *  Created on: 14 sept. 2009
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #ifdef WIN32
30 #include <direct.h>
31 #include <iostream>
32
33 #include <Batch_RunTimeException.hxx>
34 #endif
35
36 #include <Batch_config.h>
37
38 #include "Batch_CommunicationProtocolRSH.hxx"
39
40 using namespace std;
41
42 namespace Batch {
43
44   vector<string> CommunicationProtocolRSH::getExecCommandArgs(const string & subCommand,
45                                                               const string & host,
46                                                               const string & user) const
47   {
48     vector<string> cmd;
49
50     cmd.push_back(RSH_COMMAND);
51     cmd.push_back(host);
52
53     if (user.size() > 0) {
54       cmd.push_back("-l");
55       cmd.push_back(user);
56     }
57
58 #ifdef WIN32
59     cmd.push_back("-n");
60 #endif
61
62     cmd.push_back(subCommand);
63
64     return cmd;
65   }
66
67   vector<string> CommunicationProtocolRSH::getCopyCommandArgs(const string & sourcePath,
68                                                               const string & sourceHost,
69                                                               const string & sourceUser,
70                                                               const string & destinationPath,
71                                                               const string & destinationHost,
72                                                               const string & destinationUser) const
73   {
74     vector<string> cmd;
75
76     string fullSource;
77     if (sourceHost.size() != 0) {
78       if (sourceUser.size() != 0) {
79 #ifdef WIN32
80         fullSource += sourceHost + "." + sourceUser + ":";
81 #else
82         fullSource += sourceUser + "@" + sourceHost + ":";
83 #endif
84       } else {
85         fullSource += sourceHost + ":";
86       }
87     }
88     fullSource += sourcePath;
89
90     string fullDestination;
91     if (destinationHost.size() != 0) {
92       if (destinationUser.size() != 0) {
93 #ifdef WIN32
94         fullDestination += destinationHost + "." + destinationUser + ":";
95 #else
96         fullDestination += destinationUser + "@" + destinationHost + ":";
97 #endif
98       } else {
99         fullDestination += destinationHost + ":";
100       }
101     }
102     fullDestination += destinationPath;
103
104     cmd.push_back(RCP_COMMAND);
105     cmd.push_back(fullSource);
106     cmd.push_back(fullDestination);
107
108     return cmd;
109   }
110
111 #ifdef WIN32
112   int CommunicationProtocolRSH::copyFile(const std::string & sourcePath,
113                                          const std::string & sourceHost,
114                                          const std::string & sourceUser,
115                                          const std::string & destinationPath,
116                                          const std::string & destinationHost,
117                                          const std::string & destinationUser) const
118   {
119     // On Windows, we can't use drive letters in the paths of rcp command because they
120     // are confused with host names. So we must first change the working directory and
121     // then copy the file using its path without the drive letter.
122
123     // Extract the drive letter from the source path
124     string sourcePathWithoutDrive;
125     char sourceDriveLetter = getDriveLetter(sourcePath, &sourcePathWithoutDrive);
126     // Error if we have a drive letter and it is a remote path
127     if (sourceDriveLetter != '\0' && sourceHost.size() != 0)
128       throw RunTimeException(string("Invalid path: ") + sourcePath + " for host " + sourceHost);
129
130     // Extract the drive letter from the destination path
131     string destinationPathWithoutDrive;
132     char destinationDriveLetter = getDriveLetter(destinationPath, &destinationPathWithoutDrive);
133     // Error if we have a drive letter and it is a remote path
134     if (destinationDriveLetter != '\0' && destinationHost.size() != 0)
135       throw RunTimeException(string("Invalid path: ") + destinationPath + " for host " + destinationHost);
136
137     // Error if we have two drive letters and they are different
138     if (sourceDriveLetter != '\0' && destinationDriveLetter != '\0' &&
139         sourceDriveLetter != destinationDriveLetter)
140       throw RunTimeException(string("Can't use RCP to copy files between different drives: ") +
141                              sourcePath + (", ") + destinationPath);
142
143     // Now get the drive letter to use if there is one
144     char driveLetter = (sourceDriveLetter != '\0') ? sourceDriveLetter : destinationDriveLetter;
145
146     // Get the drive of the current working directory
147     char cwd[_MAX_PATH];
148     _getcwd(cwd, _MAX_PATH);
149     char currentDrive = getDriveLetter(cwd);
150
151     // Change working directory if necessary
152     if (driveLetter != '\0' && driveLetter != currentDrive) {
153       char newdir[3];
154       newdir[0] = driveLetter;
155       newdir[1] = ':';
156       newdir[2] = '\0';
157       cout << "Changing directory: " << newdir << endl;
158       _chdir(newdir);
159     }
160
161     int status = CommunicationProtocol::copyFile(sourcePathWithoutDrive, sourceHost, sourceUser,
162                                                  destinationPathWithoutDrive, destinationHost, destinationUser);
163
164     // Go back to previous directory if necessary
165     if (driveLetter != '\0' && driveLetter != currentDrive) {
166       cout << "Changing directory: " << cwd << endl;
167       _chdir(cwd);
168     }
169
170     return status;
171   }
172
173   char CommunicationProtocolRSH::getDriveLetter(const string & path, string * pathWithoutDrive) const
174   {
175     if (path.find(':') != string::npos) {
176       // Error if the colon is not the second character
177       if (path.size() < 2 || path[1] != ':')
178         throw RunTimeException(string("Invalid path: ") + path);
179       else {
180         if (pathWithoutDrive != NULL) *pathWithoutDrive = path.substr(2);
181         return path[0];
182       }
183     } else {
184       if (pathWithoutDrive != NULL) *pathWithoutDrive = path;
185       return '\0';
186     }
187   }
188
189 #endif
190
191 }