Salome HOME
Upgrade version to 2.4.3
[tools/libbatch.git] / src / Core / CommunicationProtocolRsync.cxx
1 // Copyright (C) 2007-2019  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  *  CommunicationProtocolRsync.cxx :
24  *
25  *  Author : EDF R&D
26  */
27
28 #include <libbatch_config.h>
29 #include "Utils.hxx"
30
31 #include "CommunicationProtocolRsync.hxx"
32
33 using namespace std;
34
35 namespace Batch {
36
37   CommunicationProtocolRsync::CommunicationProtocolRsync()
38   : CommunicationProtocolSSH()
39   {
40     _type = RSYNC;
41   }
42
43   vector<string> CommunicationProtocolRsync::getCopyCommandArgs(const string & sourcePath,
44                                                               const string & sourceHost,
45                                                               const string & sourceUser,
46                                                               const string & destinationPath,
47                                                               const string & destinationHost,
48                                                               const string & destinationUser) const
49   {
50     vector<string> cmd;
51
52     string fullSource;
53
54     if(Utils::isOption(sourcePath))
55       fullSource += sourcePath;
56     else
57     {
58       if (sourceHost.size() != 0) {
59         if (sourceUser.size() != 0) {
60           fullSource += sourceUser + "@";
61         }
62         fullSource += sourceHost + ":";
63       }
64 #ifndef WIN32
65       fullSource += "'";
66 #endif
67       fullSource += sourcePath;
68 #ifndef WIN32
69       fullSource += "'";
70 #endif
71     }
72
73     string fullDestination;
74     if (destinationHost.size() != 0) {
75       if (destinationUser.size() != 0) {
76         fullDestination += destinationUser + "@";
77       }
78       fullDestination += destinationHost + ":";
79     }
80 #ifndef WIN32
81     fullDestination += "'";
82 #endif
83     fullDestination += destinationPath;
84 #ifndef WIN32
85     fullDestination += "'";
86 #endif
87
88     // Option -p is used to keep the same permissions for the destination file
89     // (particularly useful to keep scripts executable when copying them)
90     cmd.push_back(RSYNC_COMMAND);
91     if(!Utils::isOption(sourcePath))
92     {
93       cmd.push_back("-p");
94       cmd.push_back("-r");
95       if(Utils::usesRsyncRelativePath(sourcePath))
96         cmd.push_back("-R");
97     }
98     cmd.push_back(fullSource);
99     cmd.push_back(fullDestination);
100
101     return cmd;
102   }
103
104 }