Skip to content

Commit

Permalink
Transform the project into a library
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskovalchuk authored Sep 15, 2023
1 parent d43d12e commit f9633ff
Show file tree
Hide file tree
Showing 99 changed files with 458 additions and 300 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Exclude pyftpdlib module from language statistics.
test/ftp/server/pyftpdlib/** linguist-vendored
test/server/pyftpdlib/** linguist-vendored
10 changes: 5 additions & 5 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ jobs:
cmake -S . -B build/release -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++-10
cmake --build build/release
- name: Test FTP
- name: Test libftp
env:
FTP_CLIENT_TEST_SERVER_PATH: ${{github.workspace}}/test/ftp/server/server.py
LIBFTP_TEST_SERVER_PATH: ${{github.workspace}}/test/server/server.py
working-directory: ${{github.workspace}}/build/release/test
run: ./ftp_test
run: ./ftp_tests

- name: Test cmdline
working-directory: ${{github.workspace}}/build/release/test
run: ./cmdline_test
working-directory: ${{github.workspace}}/build/release/app/cmdline/test
run: ./cmdline_tests
10 changes: 5 additions & 5 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ jobs:
cmake -S . -B build/release -DCMAKE_BUILD_TYPE=Release
cmake --build build/release
- name: Test FTP
- name: Test libftp
env:
FTP_CLIENT_TEST_SERVER_PATH: ${{github.workspace}}/test/ftp/server/server.py
LIBFTP_TEST_SERVER_PATH: ${{github.workspace}}/test/server/server.py
working-directory: ${{github.workspace}}/build/release/test
run: ./ftp_test
run: ./ftp_tests

- name: Test cmdline
working-directory: ${{github.workspace}}/build/release/test
run: ./cmdline_test
working-directory: ${{github.workspace}}/build/release/app/cmdline/test
run: ./cmdline_tests
10 changes: 5 additions & 5 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ jobs:
cmake -S . -B build -DBoost_USE_STATIC_LIBS=ON
cmake --build build --config Release
- name: Test FTP
- name: Test libftp
env:
FTP_CLIENT_TEST_SERVER_PATH: ${{github.workspace}}\test\ftp\server\server.py
LIBFTP_TEST_SERVER_PATH: ${{github.workspace}}\test\server\server.py
working-directory: ${{github.workspace}}\build\test\Release
run: .\ftp_test.exe
run: .\ftp_tests.exe

- name: Test cmdline
working-directory: ${{github.workspace}}\build\test\Release
run: .\cmdline_test.exe
working-directory: ${{github.workspace}}\build\app\cmdline\test\Release
run: .\cmdline_tests.exe
72 changes: 66 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
cmake_minimum_required(VERSION 3.14)
project(ftp_client VERSION 0.0.1 LANGUAGES CXX)
project(ftp
VERSION 0.0.1
DESCRIPTION "FTP client library"
LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" is_top_level)

add_subdirectory(src)
add_subdirectory(test)
option(LIBFTP_BUILD_TEST "Build tests" ${is_top_level})
option(LIBFTP_BUILD_EXAMPLE "Build examples" ${is_top_level})
option(LIBFTP_BUILD_CMDLINE_CLIENT "Build the command-line FTP client application" ${is_top_level})

find_package(Boost 1.67.0 REQUIRED)

set(sources
include/ftp/detail/ascii_istream.hpp
include/ftp/detail/ascii_ostream.hpp
include/ftp/detail/binary_istream.hpp
include/ftp/detail/binary_ostream.hpp
include/ftp/detail/control_connection.hpp
include/ftp/detail/data_connection.hpp
include/ftp/detail/utils.hpp
include/ftp/stream/input_stream.hpp
include/ftp/stream/istream_adapter.hpp
include/ftp/stream/ostream_adapter.hpp
include/ftp/stream/output_stream.hpp
include/ftp/client.hpp
include/ftp/ftp.hpp
include/ftp/ftp_exception.hpp
include/ftp/observer.hpp
include/ftp/replies.hpp
include/ftp/reply.hpp
include/ftp/transfer_callback.hpp
include/ftp/transfer_mode.hpp
include/ftp/transfer_type.hpp
src/ascii_istream.cpp
src/ascii_ostream.cpp
src/binary_istream.cpp
src/binary_ostream.cpp
src/client.cpp
src/control_connection.cpp
src/data_connection.cpp
src/istream_adapter.cpp
src/ostream_adapter.cpp
src/replies.cpp
src/reply.cpp
src/utils.cpp)

source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${sources})

add_library(ftp ${sources})
add_library(ftp::ftp ALIAS ftp)

target_link_libraries(ftp PUBLIC Boost::boost)
target_include_directories(ftp PUBLIC include)
target_compile_features(ftp PUBLIC cxx_std_17)

if (LIBFTP_BUILD_TEST)
enable_testing()
add_subdirectory(test)
endif()

if (LIBFTP_BUILD_EXAMPLES)
add_subdirectory(example)
endif()

if (LIBFTP_BUILD_CMDLINE_CLIENT)
add_subdirectory(app/cmdline)
endif()
134 changes: 75 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# ftp-client
# libftp

![C++](https://img.shields.io/badge/C++-17-blue)
[![C++](https://img.shields.io/badge/C++-17-blue)](https://en.cppreference.com/w/cpp/17)
[![License](https://img.shields.io/badge/License-MIT-blue)](LICENSE)
[![Actions Workflow Windows](https://github.com/deniskovalchuk/ftp-client/actions/workflows/windows.yml/badge.svg)](https://github.com/deniskovalchuk/ftp-client/actions/workflows/windows.yml)
[![Actions Workflow Linux](https://github.com/deniskovalchuk/ftp-client/actions/workflows/linux.yml/badge.svg)](https://github.com/deniskovalchuk/ftp-client/actions/workflows/linux.yml)
[![Actions Workflow macOS](https://github.com/deniskovalchuk/ftp-client/actions/workflows/macos.yml/badge.svg)](https://github.com/deniskovalchuk/ftp-client/actions/workflows/macos.yml)

| CI | [![Actions Workflow Windows](https://github.com/deniskovalchuk/ftp-client/actions/workflows/windows.yml/badge.svg)](https://github.com/deniskovalchuk/ftp-client/actions/workflows/windows.yml) | [![Actions Workflow Linux](https://github.com/deniskovalchuk/ftp-client/actions/workflows/linux.yml/badge.svg)](https://github.com/deniskovalchuk/ftp-client/actions/workflows/linux.yml) | [![Actions Workflow macOS](https://github.com/deniskovalchuk/ftp-client/actions/workflows/macos.yml/badge.svg)](https://github.com/deniskovalchuk/ftp-client/actions/workflows/macos.yml) |
|----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

A cross-platform FTP client based on Boost.Asio.

![Session Example](docs/session-example.png)
A cross-platform FTP client library based on [Boost.Asio](https://www.boost.org/doc/libs/1_83_0/doc/html/boost_asio.html).

## Table of contents

1. [Overview](#overview)
1. [Features](#features)
1. [Prerequisites](#prerequisites)
1. [Build and Run](#build-and-run)
1. [Command-Line Client](#command-line-client)
1. [Examples](#examples)
1. [Integration](#integration)
1. [Building](#building)
1. [References](#references)

## Overview
Expand Down Expand Up @@ -53,50 +51,100 @@ reverse conversion.
## Features

- Windows, Linux and macOS are supported.
- The main command set from [RFC 959](docs/RFC959.txt) is implemented.
- The main command set from [RFC 959](doc/RFC959.txt) is implemented.
- Supports active and passive transfer modes.
- Supports ASCII and binary transfer types.

## Prerequisites
## Examples

Download the `README.TXT` file from [ftp.freebsd.org](https://download.freebsd.org/) and output its contents to `stdout`:

```c++
#include <iostream>
#include <sstream>

#include <ftp/ftp.hpp>

int main(int argc, char *argv[])
{
ftp::client client;

client.connect("ftp.freebsd.org", 21, "anonymous");

std::ostringstream oss;

client.download_file(ftp::ostream_adapter(oss), "pub/FreeBSD/README.TXT");

std::cout << oss.str();

client.disconnect();

return 0;
}
```
See more examples in the [example](example) folder.
## Integration
This library can be integrated into a project via CMake's `FetchContent`, for example:
```cmake
cmake_minimum_required(VERSION 3.14)
project(application)
include(FetchContent)
FetchContent_Declare(
libftp
GIT_REPOSITORY https://github.com/deniskovalchuk/ftp-client.git
GIT_TAG master
)
FetchContent_MakeAvailable(libftp)
add_executable(application main.cpp)
target_link_libraries(application ftp::ftp)
```

## Building

### Prerequisites

- A C++17-compliant compiler
- CMake 3.14 or newer
- [Boost 1.67.0](https://www.boost.org/users/history/version_1_67_0.html) or newer
- Boost 1.67.0 or newer
- Python3 (only for tests)

## Build and Run

First of all you need to satisfy prerequisites listed above.

### Windows

Build and run tests:

```
scripts/windows/build.ps1 [-BuildType Debug|Release] [-RunTest]
util/windows/build.ps1 [-BuildType Debug|Release] [-RunTest]
```

Clean the builds:

```
scripts/windows/clean.ps1
util/windows/clean.ps1
```

### Linux/macOS

Build and run tests:

```
scripts/unix/build.sh [--debug | --release] [--test]
util/unix/build.sh [--debug | --release] [--test]
```

Clean the builds:

```
scripts/unix/clean.sh
util/unix/clean.sh
```

### Custom build and run tests
### Custom environment

Build:

```bash
mkdir -p build
Expand All @@ -105,47 +153,15 @@ cmake ..
cmake --build .
```

To run tests, set `FTP_CLIENT_TEST_SERVER_PATH` to the path to this [script](test/ftp/server/server.py).
To run tests, set the `LIBFTP_TEST_SERVER_PATH` environment variable to the path to the
[server.py](test/server/server.py) file:

```bash
export FTP_CLIENT_TEST_SERVER_PATH="/path/to/server.py"
export LIBFTP_TEST_SERVER_PATH="/path/to/server.py"
cd test
ctest -V
```

## Command-Line Client

The supplied command-line client supports the following commands:

1. open hostname [ port ] - connect to ftp server
1. user username - send new user information
1. cd remote-directory - change working directory on the server
1. cdup - change working directory on the server to parent directory
1. ls [ remote-directory ] - list contents of remote directory
1. pwd - print working directory on the server
1. mkdir directory-name - make directory on the server
1. rmdir directory-name - remove directory on the server
1. put local-file [ remote-file ] - upload file to the server
1. get remote-file [ local-file ] - download file from the server
1. rename from-remote-path to-remote-path - rename file/directory on the server
1. size remote-file - show size of remote file
1. del remote-file - delete file on the server
1. stat [ remote-file ] - show current status
1. syst - show remote system type
1. type - show current transfer type
1. binary - set binary transfer type
1. ascii - set ascii transfer type
1. mode - show current mode for data connection
1. active - set active mode for data connection
1. passive - set passive mode for data connection
1. noop - no operation
1. rhelp [ remote-command ] - get help from the server
1. logout - log out current user
1. close - close ftp connection
1. help - print local help information
1. exit - close ftp connection and exit

## References

- File Transfer Protocol – https://en.wikipedia.org/wiki/File_Transfer_Protocol
- [RFC 959](docs/RFC959.txt) File Transfer Protocol (FTP) Network Working Group J. Postel, J. Reynolds ISI October 1985
- [RFC 959](doc/RFC959.txt) File Transfer Protocol (FTP) Network Working Group J. Postel, J. Reynolds ISI October 1985
27 changes: 27 additions & 0 deletions app/cmdline/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.14)
project(cmdline
VERSION 0.0.1
DESCRIPTION "Command-line FTP client"
LANGUAGES CXX)

add_executable(cmdline
src/cmdline_exception.hpp
src/cmdline_interface.cpp
src/cmdline_interface.hpp
src/command.hpp
src/command_handler.cpp
src/command_handler.hpp
src/command_parser.cpp
src/command_parser.hpp
src/main.cpp
src/transfer_callback.cpp
src/transfer_callback.hpp
src/utils.cpp
src/utils.hpp)

target_link_libraries(cmdline PRIVATE ftp::ftp Boost::boost)
target_compile_features(cmdline PRIVATE cxx_std_17)

if (LIBFTP_BUILD_TEST)
add_subdirectory(test)
endif()
Loading

0 comments on commit f9633ff

Please sign in to comment.