4 #include "SftpClient.h"
6 SftpClient::SftpClient()
7 : SpawningUploadClient()
12 SftpClient::~SftpClient()
14 if (OutputPipe() >= 0)
15 SendCommand("quit\n");
20 SftpClient::ChangeDir(const string
& dir
)
26 cmd
<< " " << dir
.c_str() << "\n";
27 SendCommand(cmd
.String());
28 if ((len
= ReadReply(&reply
)) < 0) {
29 fprintf(stderr
, _GetReadText(), len
);
32 fprintf(stderr
, _GetReplyText(), reply
.String());
33 if (reply
.FindFirst("sftp>") < 0)
41 SftpClient::ListDirContents(string
& listing
)
48 if ((len
= ReadReply(&reply
)) < 0) {
49 fprintf(stderr
, _GetReadText(), len
);
52 fprintf(stderr
, _GetReplyText(), reply
.String());
53 printf("%s", reply
.String());
54 if (reply
.FindFirst("sftp>") == 0)
62 SftpClient::PrintWorkingDir(string
& dir
)
72 SftpClient::Connect(const string
& server
, const string
& login
,
77 BString
host(server
.c_str());
79 if (host
.FindFirst(':'))
80 host
.MoveInto(port
, host
.FindFirst(':'), host
.Length());
83 cmd
<< "-oPort=" << port
<< " ";
85 cmd
<< "@" << host
.String();
86 printf("COMMAND: '%s'\n", cmd
.String());
87 SetCommandLine(cmd
.String());
88 rc
= SpawningUploadClient::Connect(server
, login
, passwd
);
92 if ((len
= ReadReply(&reply
)) < 0) {
93 fprintf(stderr
, _GetLongReadText(), len
);
96 fprintf(stderr
, _GetReplyText(), reply
.String());
97 if (reply
.FindFirst("Connecting to ") != 0)
100 if ((len
= ReadReply(&reply
)) < 0) {
101 fprintf(stderr
, _GetLongReadText(), len
);
104 fprintf(stderr
, _GetReplyText(), reply
.String());
105 if (reply
.FindFirst(/*[pP]*/"assword:") < 0)
108 write(OutputPipe(), passwd
.c_str(), strlen(passwd
.c_str()));
109 write(OutputPipe(), "\n", 1);
111 if ((len
= ReadReply(&reply
)) < 0) {
112 fprintf(stderr
, _GetLongReadText(), len
);
115 fprintf(stderr
, _GetReplyText(), reply
.String());
119 if ((len
= ReadReply(&reply
)) < 0) {
120 fprintf(stderr
, _GetLongReadText(), len
);
123 fprintf(stderr
, _GetReplyText(), reply
.String());
124 if (reply
.FindFirst("sftp>") < 0)
131 SftpClient::PutFile(const string
& local
, const string
& remote
, ftp_mode mode
)
137 cmd
<< " " << local
.c_str() << " " << remote
.c_str() << "\n";
138 SendCommand(cmd
.String());
141 if ((len
= ReadReply(&reply
)) < 0) {
142 fprintf(stderr
, _GetReadText(), len
);
145 fprintf(stderr
, _GetReplyText(), reply
.String());
146 if (reply
.FindFirst("Uploading") < 0)
149 if ((len
= ReadReply(&reply
)) < 0) {
150 fprintf(stderr
, _GetReadText(), len
);
153 fprintf(stderr
, _GetReplyText(), reply
.String());
154 if (reply
.FindFirst("sftp>") < 0)
163 SftpClient::GetFile(const string
& remote
, const string
& local
, ftp_mode mode
)
171 // Note: this only works for local remote moves, cross filesystem moves
174 SftpClient::MoveFile(const string
& oldPath
, const string
& newPath
)
179 // sftpd can't rename to an existing file...
181 cmd
<< " " << newPath
.c_str() << "\n";
182 fprintf(stderr
, "CMD: '%s'\n", cmd
.String());
183 SendCommand(cmd
.String());
186 if ((len
= ReadReply(&reply
)) < 0) {
187 fprintf(stderr
, _GetReadText(), len
);
190 fprintf(stderr
, _GetReplyText(), reply
.String());
191 // we don't care if it worked or not.
192 //if (reply.FindFirst("Removing") != 0 && reply.FindFirst("Couldn't") )
195 if ((len
= ReadReply(&reply
)) < 0) {
196 fprintf(stderr
, _GetReadText(), len
);
199 fprintf(stderr
, _GetReplyText(), reply
.String());
200 if (reply
.FindFirst("sftp>") < 0)
204 cmd
<< " " << oldPath
.c_str() << " " << newPath
.c_str() << "\n";
205 SendCommand(cmd
.String());
206 if ((len
= ReadReply(&reply
)) < 0) {
207 fprintf(stderr
, _GetReadText(), len
);
210 fprintf(stderr
, _GetReplyText(), reply
.String());
211 if (reply
.FindFirst("sftp>") < 0)
219 SftpClient::Chmod(const string
& path
, const string
& mod
)
224 BString
cmd("chmod");
225 cmd
<< " " << mod
.c_str() << " " << path
.c_str() << "\n";
226 SendCommand(cmd
.String());
229 if ((len
= ReadReply(&reply
)) < 0) {
230 fprintf(stderr
, _GetReadText(), len
);
233 fprintf(stderr
, _GetReplyText(), reply
.String());
234 if (reply
.FindFirst("Changing") < 0)
237 if ((len
= ReadReply(&reply
)) < 0) {
238 fprintf(stderr
, _GetReadText(), len
);
241 fprintf(stderr
, _GetReplyText(), reply
.String());
242 if (reply
.FindFirst("sftp>") < 0)
251 SftpClient::SetPassive(bool on
)
257 SftpClient::_GetLongReadText() const
259 return B_TRANSLATE("read: %ld\n");
264 SftpClient::_GetReadText() const
266 return B_TRANSLATE("read: %d\n");
271 SftpClient::_GetReplyText() const
273 return B_TRANSLATE("reply: '%s'\n");