1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 | #include "gate/gatemain.h"
#include "gate/debugging.h"
#include "gate/console.hpp"
#include "gate/applications.hpp"
#include "gate/net/sshclients.hpp"
#include "gate/files.hpp"
#include "gate/lambdas.hpp"
#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
# include <vld.h>
#endif
namespace gate
{
namespace apps
{
using namespace gate::net;
static AppOptionDef options[] =
{<--- Shadowed declaration
AppOptionDef::createString("")
};
GATE_LAMBDA(printer, void, (File::Entry const* entry, void* param, bool* continueListing),
{
gate::Console << entry->getPath()
<< "\t" << entry->getName()
<< "\t" << entry->getSize()
<< "\t" << entry->getTimeModified().toString()
<< gate::strings::NewLine;
});
class GATE_API_LOCAL SFTPcliApp : public App
{
public:
virtual void run() override
{
static AppOptionDef help = AppOptionDef::createSwitch("help", "?", "show_help", "Shows help");
static AppOptionDef server = AppOptionDef::createString("server", "S", "127.0.0.1", "server_host_or_ip", "SSH server to be accessed");
static AppOptionDef port = AppOptionDef::createInt32("port", "P", 22, "ssh_port", "SSH server port to connect to");
static AppOptionDef user = AppOptionDef::createString("user", "U", "", "login_user", "User account for authentification");
static AppOptionDef pass = AppOptionDef::createString("password", "PW", "", "login_password", "Password of user account for authentification");
static AppOptionDef list = AppOptionDef::createString("list", "LS", "", "remote_dir_path", "Lists the contents of a directory");
static AppOptionDef upload = AppOptionDef::createString("upload", "UL", "", "local_input_file_path", "Uploads a local file to a remote path");
static AppOptionDef download = AppOptionDef::createString("download", "DL", "", "local_output_file_path", "Downloads a remote file to a local path");
static AppOptionDef targetfile = AppOptionDef::createString("target-path", "T", "", "Path to remote file to be accessed");
static AppOptionDef* options[] =
{<--- Shadow variable
&help,
&server,
&port,
&user,
&pass,
&list,
&upload,
&download,
&targetfile
};
size_t const optionsCount = sizeof(options) / sizeof(options[0]);
App::parseAppOptions(this->getArgs(), options, optionsCount);
if (help.getBool())
{
App::printAppOptions(options, optionsCount, Console);
return;
}
SshClientRef sshclient = SshClient::create(
server.getString(), port.getInt32(), user.getString(), pass.getString());
SshFtp sftp(sshclient);
if (list)
{
File::callback_t cb(printer);
sftp.listEntries(list.getString(), cb, NULL);
return;
}
if (upload && targetfile)
{
ControlStream source = File::open(upload.getString(), File::Open_Write);
SshFtpFile target(sftp, targetfile.getString());
char buffer[4096];
size_t received;
while ((received = source.read(buffer, sizeof(buffer))) > 0)
{
size_t done = 0;
while (done < received)
{
size_t written = target.write(&buffer[done], received - done);
done += written;
}
}
return;
}
if (download && targetfile)
{
SshFtpFile source(sftp, download.getString());
ControlStream target = File::open(targetfile.getString(), File::Open_Write);
char buffer[4096];
size_t received;
while ((received = source.read(buffer, sizeof(buffer))) > 0)
{
size_t done = 0;
while (done < received)
{
size_t written = target.write(&buffer[done], received - done);
done += written;
}
}
return;
}
ConError
<< "Missing arguments." << strings::NewLine
<< "Use '--help' to see all available options." << strings::NewLine;
GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Missing arguments", 0);
}
};
} // end of namespace apps
} // end of namespace gate
int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
gate::apps::SFTPcliApp app;
return gate::App::runApp(app, program, arguments, argcount, apphandle);
}
|