dm_ftp Module

Module for file transfer via FTP(S).

Upload a local file to an FTP server:

character(len=*), parameter :: HOST        = '192.168.0.100'
character(len=*), parameter :: LOCAL_FILE  = '/tmp/observ.csv'
character(len=*), parameter :: REMOTE_FILE = 'test/observ.csv'

integer               :: rc
type(ftp_server_type) :: server

rc = dm_ftp_init()
call dm_ftp_server_set(server, host=HOST, create_missing=.true.)
rc = dm_ftp_upload(server, LOCAL_FILE, REMOTE_FILE)
call dm_ftp_shutdown()

If remote directory test/ does not exist, it will be created. The remote path is relative to the default directory of the FTP server. Absolute paths have to start with //.

Download remote file test/observ.csv instead:

call dm_ftp_server_set(server, host=HOST)
if (dm_file_exists(LOCAL_FILE)) call dm_file_delete(LOCAL_FILE)
rc = dm_ftp_download(server, REMOTE_FILE, LOCAL_FILE)

An existing local file has to be deleted first, or dm_ftp_download() returns error E_EXIST.


Uses

  • module~~dm_ftp~~UsesGraph module~dm_ftp dm_ftp curl curl module~dm_ftp->curl iso_c_binding iso_c_binding module~dm_ftp->iso_c_binding module~dm_error dm_error module~dm_ftp->module~dm_error module~dm_kind dm_kind module~dm_ftp->module~dm_kind module~dm_util dm_util module~dm_ftp->module~dm_util module~dm_version dm_version module~dm_ftp->module~dm_version module~dm_error->module~dm_kind module~dm_ascii dm_ascii module~dm_error->module~dm_ascii iso_fortran_env iso_fortran_env module~dm_kind->iso_fortran_env module~dm_util->module~dm_error module~dm_util->module~dm_kind

Used by

  • module~~dm_ftp~~UsedByGraph module~dm_ftp dm_ftp module~dmpack dmpack module~dmpack->module~dm_ftp

Variables

Type Visibility Attributes Name Initial
character(len=*), public, parameter :: FTP_USER_AGENT = 'DMPACK '//DM_VERSION_STRING

User agent of FTP client.

integer, public, parameter :: FTP_HOST_LEN = 256

Max. host length.

integer, public, parameter :: FTP_USERNAME_LEN = 32

Max. user name length.

integer, public, parameter :: FTP_PASSWORD_LEN = 32

Max. password length.

integer, public, parameter :: FTP_URL_LEN = 2048

Max. URL length.


Derived Types

type, public ::  ftp_server_type

FTP server type.

Components

Type Visibility Attributes Name Initial
character(len=FTP_HOST_LEN), public :: host = ' '

IP address or FQDN of FTP server.

integer, public :: port = 0

Control port (0 for default port 21).

character(len=FTP_USERNAME_LEN), public :: username = ' '

User name (empty for none).

character(len=FTP_PASSWORD_LEN), public :: password = ' '

Password (empty for none).

integer, public :: accept_timeout = 5

Accept timeout [sec].

integer, public :: connect_timeout = 30

Connection timeout [sec].

integer, public :: timeout = 30

Response timeout [sec].

logical, public :: active = .false.

Active mode.

logical, public :: create_missing = .false.

Create missing directories.

logical, public :: tls = .false.

Use Transport-Layer Security (FTPS).

logical, public :: verify_tls = .false.

Verify SSL certificate.


Functions

public function dm_ftp_download(server, remote_file, local_file, error_message, error_curl, debug) result(rc)

Downloads remote file from FTP server.

Read more…

Arguments

Type IntentOptional Attributes Name
type(ftp_server_type), intent(inout) :: server

FTP server type.

character(len=*), intent(in) :: remote_file

Path of remote file.

character(len=*), intent(in) :: local_file

Path of file to upload.

character(len=:), intent(out), optional, allocatable :: error_message

Error message.

integer, intent(out), optional :: error_curl

cURL error code.

logical, intent(in), optional :: debug

Output debug messages.

Return Value integer

public function dm_ftp_error(error_curl) result(rc)

Converts cURL easy stack error code to DMPACK error code.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: error_curl

cURL easy error code.

Return Value integer

public function dm_ftp_error_message(error_curl) result(message)

Return message associated with given cURL error code as allocatable character string.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: error_curl

cURL error code.

Return Value character(len=:), allocatable

Error message.

public function dm_ftp_init() result(rc)

Initialises FTP backend. The function returns E_FTP on error.

Arguments

None

Return Value integer

public function dm_ftp_upload(server, local_file, remote_file, rename_file_to, error_message, error_curl, debug) result(rc)

Uploads local file to FTP server.

Read more…

Arguments

Type IntentOptional Attributes Name
type(ftp_server_type), intent(inout) :: server

FTP server type.

character(len=*), intent(in) :: local_file

Path of file to upload.

character(len=*), intent(in) :: remote_file

Path of remote file.

character(len=*), intent(in), optional :: rename_file_to

File name to rename to remote file to.

character(len=:), intent(out), optional, allocatable :: error_message

Error message.

integer, intent(out), optional :: error_curl

cURL error code.

logical, intent(in), optional :: debug

Output debug messages.

Return Value integer

public function dm_ftp_url(host, port, path, tls) result(url)

Returns allocatable string of FTP server URL in the form ftp[s]://host[:port]/path. An absolute path has to start with //. Uses the URL API of libcurl to create the URL. By default, Transport-Layer Security (FTPS) is disabled.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: host

FTP host.

integer, intent(in), optional :: port

FTP port (up to 5 digits).

character(len=*), intent(in), optional :: path

FTP file path.

logical, intent(in), optional :: tls

Enable Transport-Layer Security.

Return Value character(len=:), allocatable

URL of FTP server.

public function dm_ftp_discard_callback(ptr, sz, nmemb, data) result(n) bind(c))

C-interoperable discard download function for libcurl. Do not call this function directly.

Arguments

Type IntentOptional Attributes Name
type(c_ptr), intent(in), value :: ptr

C pointer to a chunk of memory.

integer(kind=c_size_t), intent(in), value :: sz

Always 1.

integer(kind=c_size_t), intent(in), value :: nmemb

Size of the memory chunk.

type(c_ptr), intent(in), value :: data

C pointer to argument passed by caller.

Return Value integer(kind=c_size_t)

Function return value.

public function dm_ftp_read_callback(ptr, sz, nmemb, data) result(n) bind(c))

C-interoperable upload callback function for libcurl. Do not call this function directly.

Arguments

Type IntentOptional Attributes Name
type(c_ptr), intent(in), value :: ptr

C pointer to a chunk of memory.

integer(kind=c_size_t), intent(in), value :: sz

Always 1.

integer(kind=c_size_t), intent(in), value :: nmemb

Size of the memory chunk.

type(c_ptr), intent(in), value :: data

C pointer to argument passed by caller.

Return Value integer(kind=c_size_t)

Function return value.

public function dm_ftp_write_callback(ptr, sz, nmemb, data) result(n) bind(c))

C-interoperable download callback function for libcurl. Do not call this function directly.

Arguments

Type IntentOptional Attributes Name
type(c_ptr), intent(in), value :: ptr

C pointer to a chunk of memory.

integer(kind=c_size_t), intent(in), value :: sz

Always 1.

integer(kind=c_size_t), intent(in), value :: nmemb

Size of the memory chunk.

type(c_ptr), intent(in), value :: data

C pointer to argument passed by caller.

Return Value integer(kind=c_size_t)

Function return value.


Subroutines

public subroutine dm_ftp_shutdown()

Cleans up FTP backend.

Arguments

None

public subroutine dm_ftp_server_out(server, unit)

Prints FTP connection type to standard output or given file unit.

Arguments

Type IntentOptional Attributes Name
type(ftp_server_type), intent(inout) :: server

FTP server type.

integer, intent(in), optional :: unit

File unit.

public subroutine dm_ftp_server_set(server, host, port, username, password, accept_timeout, connect_timeout, timeout, active, create_missing, tls, verify_tls)

Sets attributes of given server type.

Arguments

Type IntentOptional Attributes Name
type(ftp_server_type), intent(inout) :: server

FTP server type.

character(len=*), intent(in), optional :: host

Host.

integer, intent(in), optional :: port

Port (or 0 for default).

character(len=*), intent(in), optional :: username

User name.

character(len=*), intent(in), optional :: password

Password.

integer, intent(in), optional :: accept_timeout

Accept timeout [sec].

integer, intent(in), optional :: connect_timeout

Connection timeout [sec].

integer, intent(in), optional :: timeout

Response timeout [sec].

logical, intent(in), optional :: active

Enable active mode.

logical, intent(in), optional :: create_missing

Create missing directories.

logical, intent(in), optional :: tls

Enable Transport-Layer Security.

logical, intent(in), optional :: verify_tls

Verify SSL cert.