-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ct_ftp.erl
410 lines (350 loc) · 11.3 KB
/
ct_ftp.erl
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2003-2024. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(ct_ftp).
-moduledoc """
FTP client module (based on the FTP application).
FTP client module (based on the `ftp` application).
""".
%% API
-export([get/3,put/3, open/1,close/1, send/2,send/3,
recv/2,recv/3, cd/2, ls/2, type/2, delete/2]).
%% Callbacks
-export([init/3,handle_msg/2,reconnect/2,terminate/2]).
-include("ct_util.hrl").
-record(state,{ftp_pid,target_name}).
-define(DEFAULT_PORT,21).
-doc "For `target_name`, see module `m:ct`.".
-type connection() :: handle() | ct:target_name().
-doc "Handle for a specific FTP connection, see module `m:ct`.".
-type handle() :: ct:handle().
-export_type([connection/0, handle/0]).
%%%=================================================================
%%% API
-doc """
Opens an FTP connection and sends a file to the remote host.
`LocalFile` and `RemoteFile` must be absolute paths.
For `target_name`, see module `m:ct`.
If the target host is a "special" node, the FTP address must be specified in the
configuration file as follows:
```erlang
{node,[{ftp,IpAddr}]}.
```
If the target host is something else, for example, a UNIX host, the
configuration file must also include the username and password (both strings):
```erlang
{unix,[{ftp,IpAddr},
{username,Username},
{password,Password}]}.
```
See also `ct:require/2`.
""".
-spec put(KeyOrName, LocalFile, RemoteFile) -> 'ok' | {'error', Reason}
when KeyOrName :: atom(),
LocalFile :: file:filename(),
RemoteFile :: file:filename(),
Reason :: term().
put(KeyOrName,LocalFile,RemoteFile) ->
Fun = fun(Ftp) -> send(Ftp,LocalFile,RemoteFile) end,
open_and_do(KeyOrName,Fun).
-doc """
Opens an FTP connection and fetches a file from the remote host.
`RemoteFile` and `LocalFile` must be absolute paths.
The configuration file must be as for [`ct_ftp:put/3`](`put/3`).
For `target_name`, see module `m:ct`.
See also `ct:require/2`.
""".
-spec get(KeyOrName, RemoteFile, LocalFile) -> 'ok' | {'error', Reason}
when KeyOrName :: atom(),
RemoteFile :: file:filename(),
LocalFile :: file:filename(),
Reason :: term().
get(KeyOrName,RemoteFile,LocalFile) ->
Fun = fun(Ftp) -> recv(Ftp,RemoteFile,LocalFile) end,
open_and_do(KeyOrName,Fun).
-doc """
Opens an FTP connection to the specified node.
You can open a connection for a particular `Name` and use the same name as
reference for all following subsequent operations. If you want the connection to
be associated with `Handle` instead (if you, for example, need to open multiple
connections to a host), use `Key`, the configuration variable name, to specify
the target. A connection without an associated target name can only be closed
with the handle value.
For information on how to create a new `Name`, see `ct:require/2`.
For `target_name`, see module `m:ct`.
""".
-spec open(KeyOrName) -> {'ok', Handle} | {'error', Reason}
when KeyOrName :: atom(),
Handle :: handle(),
Reason :: term().
open(KeyOrName) ->
case ct_util:get_key_from_name(KeyOrName) of
{ok,node} ->
open(KeyOrName,"erlang","x");
_ ->
case ct:get_config(KeyOrName) of
undefined ->
log(heading(open,KeyOrName),"Failed: ~tp\n",
[{not_available,KeyOrName}]),
{error,{not_available,KeyOrName}};
_ ->
case ct:get_config({KeyOrName,username}) of
undefined ->
log(heading(open,KeyOrName),"Failed: ~tp\n",
[{not_available,{KeyOrName,username}}]),
{error,{not_available,{KeyOrName,username}}};
Username ->
case ct:get_config({KeyOrName,password}) of
undefined ->
log(heading(open,KeyOrName),"Failed: ~tp\n",
[{not_available,{KeyOrName,password}}]),
{error,{not_available,{KeyOrName,password}}};
Password ->
open(KeyOrName,Username,Password)
end
end
end
end.
open(KeyOrName,Username,Password) ->
log(heading(open,KeyOrName),"",[]),
case ct:get_config({KeyOrName,ftp}) of
undefined ->
log(heading(open,KeyOrName),"Failed: ~tp\n",
[{not_available,{KeyOrName,ftp}}]),
{error,{not_available,{KeyOrName,ftp}}};
Addr ->
ct_gen_conn:start(KeyOrName,full_addr(Addr),{Username,Password},?MODULE)
end.
-doc """
Sends a file over FTP.
The file gets the same name on the remote host.
See also [`ct_ftp:send/3`](`send/3`).
""".
-spec send(Connection, LocalFile) -> 'ok' | {'error', Reason}
when Connection :: connection(),
LocalFile :: file:filename(),
Reason :: term().
send(Connection,LocalFile) ->
send(Connection,LocalFile,filename:basename(LocalFile)).
-doc """
Sends a file over FTP.
The file is named `RemoteFile` on the remote host.
""".
-spec send(Connection, LocalFile, RemoteFile) -> 'ok' | {'error', Reason}
when Connection :: connection(),
LocalFile :: file:filename(),
RemoteFile :: file:filename(),
Reason :: term().
send(Connection,LocalFile,RemoteFile) ->
case get_handle(Connection) of
{ok,Pid} ->
call(Pid,{send,LocalFile,RemoteFile});
Error ->
Error
end.
-doc """
Fetches a file over FTP.
The file gets the same name on the local host.
See also [`ct_ftp:recv/3`](`recv/3`).
""".
-spec recv(Connection, RemoteFile) -> 'ok' | {'error', Reason}
when Connection :: connection(),
RemoteFile :: file:filename(),
Reason :: term().
recv(Connection,RemoteFile) ->
recv(Connection,RemoteFile,filename:basename(RemoteFile)).
-doc """
Fetches a file over FTP.
The file is named `LocalFile` on the local host.
""".
-spec recv(Connection, RemoteFile, LocalFile) -> 'ok' | {'error', Reason}
when Connection :: connection(),
RemoteFile :: file:filename(),
LocalFile :: file:filename(),
Reason :: term().
recv(Connection,RemoteFile,LocalFile) ->
case get_handle(Connection) of
{ok,Pid} ->
call(Pid,{recv,RemoteFile,LocalFile});
Error ->
Error
end.
-doc """
Changes directory on remote host.
""".
-spec cd(Connection, Dir) -> 'ok' | {'error', Reason}
when Connection :: connection(),
Dir :: file:filename(),
Reason :: term().
cd(Connection,Dir) ->
case get_handle(Connection) of
{ok,Pid} ->
call(Pid,{cd,Dir});
Error ->
Error
end.
-doc """
Lists directory `Dir`.
""".
-spec ls(Connection, Dir) -> {'ok', Listing} | {'error', Reason}
when Connection :: connection(),
Dir :: file:filename(),
Listing :: string(),
Reason :: term().
ls(Connection,Dir) ->
case get_handle(Connection) of
{ok,Pid} ->
call(Pid,{ls,Dir});
Error ->
Error
end.
-doc """
Changes the file transfer type.
""".
-spec type(Connection, Type) -> 'ok' | {'error', Reason}
when Connection :: connection(),
Type :: ascii | binary,
Reason :: term().
type(Connection,Type) ->
case get_handle(Connection) of
{ok,Pid} ->
call(Pid,{type,Type});
Error ->
Error
end.
-doc """
Deletes a file on remote host.
""".
-spec delete(Connection, File) -> 'ok' | {'error', Reason}
when Connection :: connection(),
File :: file:filename(),
Reason :: term().
delete(Connection,File) ->
case get_handle(Connection) of
{ok,Pid} ->
call(Pid,{delete,File});
Error ->
Error
end.
-doc """
Closes the FTP connection.
""".
-spec close(Connection) -> 'ok' | {'error', Reason}
when Connection :: connection(),
Reason :: term().
close(Connection) ->
case get_handle(Connection) of
{ok,Pid} ->
ct_gen_conn:stop(Pid);
Error ->
Error
end.
%%%=================================================================
%%% Callback functions
-doc false.
init(KeyOrName,{IP,Port},{Username,Password}) ->
case ftp_connect(IP,Port,Username,Password) of
{ok,FtpPid} ->
log(heading(init,KeyOrName),
"Opened ftp connection:\nIP: ~tp\nUsername: ~tp\nPassword: ~p\n",
[IP,Username,lists:duplicate(string:length(Password),$*)]),
{ok,FtpPid,#state{ftp_pid=FtpPid,target_name=KeyOrName}};
Error ->
Error
end.
ftp_connect(IP,Port,Username,Password) ->
_ = ftp:start(),
case ftp:open(IP,[{port,Port}]) of
{ok,FtpPid} ->
case ftp:user(FtpPid,Username,Password) of
ok ->
{ok,FtpPid};
{error,Reason} ->
{error,{user,Reason}}
end;
{error,Reason} ->
{error,{open,Reason}}
end.
-doc false.
handle_msg({send,LocalFile,RemoteFile},State) ->
log(heading(send,State#state.target_name),
"LocalFile: ~tp\nRemoteFile: ~tp\n",[LocalFile,RemoteFile]),
Result = ftp:send(State#state.ftp_pid,LocalFile,RemoteFile),
{Result,State};
handle_msg({recv,RemoteFile,LocalFile},State) ->
log(heading(recv,State#state.target_name),
"RemoteFile: ~tp\nLocalFile: ~tp\n",[RemoteFile,LocalFile]),
Result = ftp:recv(State#state.ftp_pid,RemoteFile,LocalFile),
{Result,State};
handle_msg({cd,Dir},State) ->
log(heading(cd,State#state.target_name),"Dir: ~tp\n",[Dir]),
Result = ftp:cd(State#state.ftp_pid,Dir),
{Result,State};
handle_msg({ls,Dir},State) ->
log(heading(ls,State#state.target_name),"Dir: ~tp\n",[Dir]),
Result = ftp:ls(State#state.ftp_pid,Dir),
{Result,State};
handle_msg({type,Type},State) ->
log(heading(type,State#state.target_name),"Type: ~tp\n",[Type]),
Result = ftp:type(State#state.ftp_pid,Type),
{Result,State};
handle_msg({delete,File},State) ->
log(heading(delete,State#state.target_name),"Delete file: ~tp\n",[File]),
Result = ftp:delete(State#state.ftp_pid,File),
{Result,State}.
-doc false.
reconnect(_Addr,_State) ->
{error,no_reconnection_of_ftp}.
-doc false.
terminate(FtpPid,State) ->
log(heading(terminate,State#state.target_name),
"Closing FTP connection.\nHandle: ~p\n",[FtpPid]),
ftp:close(FtpPid).
%%%=================================================================
%%% Internal function
get_handle(Pid) when is_pid(Pid) ->
{ok,Pid};
get_handle(Name) ->
case ct_util:get_connection(Name,?MODULE) of
{ok,{Pid,_}} ->
{ok,Pid};
{error,no_registered_connection} ->
open(Name);
Error ->
Error
end.
full_addr({Ip,Port}) ->
{Ip,Port};
full_addr(Ip) ->
{Ip,?DEFAULT_PORT}.
call(Pid,Msg) ->
ct_gen_conn:call(Pid,Msg).
heading(Function,Name) ->
io_lib:format("ct_ftp:~tw ~tp",[Function,Name]).
log(Heading,Str,Args) ->
ct_gen_conn:log(Heading,Str,Args).
open_and_do(Name,Fun) ->
case open(Name) of
{ok,Ftp} ->
R = Fun(Ftp),
close(Ftp),
R;
Error ->
Error
end.