1 // Copyright (C) 2007-2012 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.
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 * CommunicationProtocolRSH.cxx :
25 * Created on: 14 sept. 2009
26 * Author : Renaud BARATE - EDF R&D
33 #include <RunTimeException.hxx>
38 #include "CommunicationProtocolRSH.hxx"
44 vector<string> CommunicationProtocolRSH::getExecCommandArgs(const string & subCommand,
46 const string & user) const
50 cmd.push_back(RSH_COMMAND);
53 if (user.size() > 0) {
62 cmd.push_back(subCommand);
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
77 if (sourceHost.size() != 0) {
78 if (sourceUser.size() != 0) {
80 fullSource += sourceHost + "." + sourceUser + ":";
82 fullSource += sourceUser + "@" + sourceHost + ":";
85 fullSource += sourceHost + ":";
88 fullSource += sourcePath;
90 string fullDestination;
91 if (destinationHost.size() != 0) {
92 if (destinationUser.size() != 0) {
94 fullDestination += destinationHost + "." + destinationUser + ":";
96 fullDestination += destinationUser + "@" + destinationHost + ":";
99 fullDestination += destinationHost + ":";
102 fullDestination += destinationPath;
104 cmd.push_back(RCP_COMMAND);
106 cmd.push_back(fullSource);
107 cmd.push_back(fullDestination);
113 int CommunicationProtocolRSH::copyFile(const std::string & sourcePath,
114 const std::string & sourceHost,
115 const std::string & sourceUser,
116 const std::string & destinationPath,
117 const std::string & destinationHost,
118 const std::string & destinationUser) const
120 // On Windows, we can't use drive letters in the paths of rcp command because they
121 // are confused with host names. So we must first change the working directory and
122 // then copy the file using its path without the drive letter.
124 // Extract the drive letter from the source path
125 string sourcePathWithoutDrive;
126 char sourceDriveLetter = getDriveLetter(sourcePath, &sourcePathWithoutDrive);
127 // Error if we have a drive letter and it is a remote path
128 if (sourceDriveLetter != '\0' && sourceHost.size() != 0)
129 throw RunTimeException(string("Invalid path: ") + sourcePath + " for host " + sourceHost);
131 // Extract the drive letter from the destination path
132 string destinationPathWithoutDrive;
133 char destinationDriveLetter = getDriveLetter(destinationPath, &destinationPathWithoutDrive);
134 // Error if we have a drive letter and it is a remote path
135 if (destinationDriveLetter != '\0' && destinationHost.size() != 0)
136 throw RunTimeException(string("Invalid path: ") + destinationPath + " for host " + destinationHost);
138 // Error if we have two drive letters and they are different
139 if (sourceDriveLetter != '\0' && destinationDriveLetter != '\0' &&
140 sourceDriveLetter != destinationDriveLetter)
141 throw RunTimeException(string("Can't use RCP to copy files between different drives: ") +
142 sourcePath + (", ") + destinationPath);
144 // Now get the drive letter to use if there is one
145 char driveLetter = (sourceDriveLetter != '\0') ? sourceDriveLetter : destinationDriveLetter;
147 // Get the drive of the current working directory
149 _getcwd(cwd, _MAX_PATH);
150 char currentDrive = getDriveLetter(cwd);
152 // Change working directory if necessary
153 if (driveLetter != '\0' && driveLetter != currentDrive) {
155 newdir[0] = driveLetter;
158 cout << "Changing directory: " << newdir << endl;
162 int status = CommunicationProtocol::copyFile(sourcePathWithoutDrive, sourceHost, sourceUser,
163 destinationPathWithoutDrive, destinationHost, destinationUser);
165 // Go back to previous directory if necessary
166 if (driveLetter != '\0' && driveLetter != currentDrive) {
167 cout << "Changing directory: " << cwd << endl;
174 char CommunicationProtocolRSH::getDriveLetter(const string & path, string * pathWithoutDrive) const
176 if (path.find(':') != string::npos) {
177 // Error if the colon is not the second character
178 if (path.size() < 2 || path[1] != ':')
179 throw RunTimeException(string("Invalid path: ") + path);
181 if (pathWithoutDrive != NULL) *pathWithoutDrive = path.substr(2);
185 if (pathWithoutDrive != NULL) *pathWithoutDrive = path;