Skip to content

Commit

Permalink
Added SIZE command (#29)
Browse files Browse the repository at this point in the history
- Added Size Command
- Fixed MinGW compile issue
- Improved Readme.md

Co-authored-by: Bjarne Juul Pasgaard <[email protected]>
  • Loading branch information
FlorianReimold and Bjarne Juul Pasgaard authored Jan 10, 2022
1 parent 51c7fec commit 5509f23
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[![Windows](https://github.com/continental/fineftp-server/actions/workflows/build-windows.yml/badge.svg)](https://github.com/continental/fineftp-server/actions/workflows/build-windows.yml) [![Ubuntu](https://github.com/continental/fineftp-server/actions/workflows/build-ubuntu.yml/badge.svg)](https://github.com/continental/fineftp-server/actions/workflows/build-ubuntu.yml) [![macOS](https://github.com/continental/fineftp-server/actions/workflows/build-macos.yml/badge.svg)](https://github.com/continental/fineftp-server/actions/workflows/build-macos.yml)

# fineFTP Server

FineFTP is a minimal FTP server library for Windows and Unix flavors. The project is CMake based and only depends on asio, which is integrated as git submodule. No boost is required.

You can easily embed this library into your own project in order to create an embedded FTP Server. It was developed and tested on Windows 10 (Visual Studio 2015 / 2019) and Ubuntu 16.04 - 21.10 (gcc 5.4.0 - 11.2.0).
You can easily embed this library into your own project in order to create an embedded FTP Server. It was developed and tested on Windows 10 (Visual Studio 2015 / 2019, MinGW) and Ubuntu 16.04 - 21.10 (gcc 5.4.0 - 11.2.0).

## Features

Expand All @@ -14,7 +15,7 @@ You can easily embed this library into your own project in order to create an em
- User authentication (and anonymous user without authentication)
- Individual local home path for each user
- Access control on a per-user-basis
- UTF8 support
- UTF8 support (On Windows MSVC only)

*fineFTP does not support any kind of encryption. You should only use fineFTP in trusted networks.*

Expand Down
2 changes: 1 addition & 1 deletion fineftp-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.5.1)

project(server VERSION 1.2.0)
project(server VERSION 1.3.0)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

Expand Down
2 changes: 1 addition & 1 deletion fineftp-server/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <windows.h>
#include <win_str_convert.h>

#else // WIN32
Expand Down
50 changes: 49 additions & 1 deletion fineftp-server/src/ftp_session.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ namespace fineftp
// Modern FTP Commands
{ "FEAT", std::bind(&FtpSession::handleFtpCommandFEAT, this, std::placeholders::_1) },
{ "OPTS", std::bind(&FtpSession::handleFtpCommandOPTS, this, std::placeholders::_1) },
{ "SIZE", std::bind(&FtpSession::handleFtpCommandSIZE, this, std::placeholders::_1) },
};

auto command_it = command_map.find(ftp_command);
Expand Down Expand Up @@ -485,6 +486,52 @@ namespace fineftp
return;
}

void FtpSession::handleFtpCommandSIZE(const std::string& param)
{
if (!logged_in_user_)
{
sendFtpMessage(FtpReplyCode::NOT_LOGGED_IN, "Not logged in");
return;
}
if (static_cast<int>(logged_in_user_->permissions_ & Permission::FileRead) == 0)
{
sendFtpMessage(FtpReplyCode::ACTION_NOT_TAKEN, "Permission denied");
return;
}

std::string local_path = toLocalPath(param);

std::ios::openmode open_mode =
std::ios::ate | (data_type_binary_ ? (std::ios::in | std::ios::binary) : (std::ios::in));
#if defined(WIN32) && !defined(__GNUG__)
std::ifstream file(StrConvert::Utf8ToWide(local_path), open_mode);
#else
std::ifstream file(local_path, open_mode);
#endif

if (!file.good())
{
sendFtpMessage(FtpReplyCode::ACTION_ABORTED_LOCAL_ERROR, "Error opening file for size retrieval");
return;
}

// RFC 3659 actually states that the returned size should depend on the STRU, MODE, and TYPE and that
// the returned size should be exact. We don't comply with this here. The size returned is the
// size for TYPE=I.
auto file_size = file.tellg();
if (std::fstream::pos_type(-1) == file_size)
{
sendFtpMessage(FtpReplyCode::ACTION_ABORTED_LOCAL_ERROR, "Error getting file size");
return;
}

// Form reply string
std::stringstream rep;
rep << file_size;

sendFtpMessage(FtpReplyCode::FILE_STATUS, rep.str());
}

void FtpSession::handleFtpCommandSTOR(const std::string& param)
{
if (!logged_in_user_)
Expand Down Expand Up @@ -1037,8 +1084,9 @@ namespace fineftp
std::stringstream ss;
ss << "211- Feature List:\r\n";
ss << " UTF8\r\n";
ss << " SIZE\r\n";
ss << " LANG EN\r\n";
ss << "211 end\r\n";
ss << "211 END\r\n";

sendRawFtpMessage(ss.str());
}
Expand Down
3 changes: 2 additions & 1 deletion fineftp-server/src/ftp_session.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace fineftp
struct IoFile
{
IoFile(const std::string& filename, std::ios::openmode mode)
#ifdef WIN32
#if defined(WIN32) && !defined(__GNUG__)
: file_stream_(StrConvert::Utf8ToWide(filename), mode)
#else
: file_stream_(filename, mode)
Expand Down Expand Up @@ -88,6 +88,7 @@ namespace fineftp

// Ftp service commands
void handleFtpCommandRETR(const std::string& param);
void handleFtpCommandSIZE(const std::string& param);
void handleFtpCommandSTOR(const std::string& param);
void handleFtpCommandSTOU(const std::string& param);
void handleFtpCommandAPPE(const std::string& param);
Expand Down
4 changes: 2 additions & 2 deletions fineftp-server/src/win_str_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <windows.h>
#endif // WIN32

#include <string>
Expand Down Expand Up @@ -56,4 +56,4 @@ namespace fineftp
}
#endif
}
}
}
24 changes: 24 additions & 0 deletions toolchain_mingw.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(CMAKE_SYSTEM_NAME Windows)

set(CMAKE_C_COMPILER /usr/bin/x86_64-w64-mingw32-gcc-posix)
set(CMAKE_CXX_COMPILER /usr/bin/x86_64-w64-mingw32-g++-posix)
set(CMAKE_AR /usr/bin/x86_64-w64-mingw32-ar CACHE FILEPATH "" FORCE)
set(CMAKE_C_COMPILER_AR /usr/bin/x86_64-w64-mingw32-gcc-ar CACHE FILEPATH "" FORCE)
set(CMAKE_CXX_COMPILER_AR /usr/bin/x86_64-w64-mingw32-gcc-ar CACHE FILEPATH "" FORCE)
set(CMAKE_LINKER /usr/bin/x86_64-w64-mingw32-ld CACHE FILEPATH "" FORCE)
set(CMAKE_NM /usr/bin/x86_64-w64-mingw32-nm CACHE FILEPATH "" FORCE)
set(CMAKE_OBJCOPY /usr/bin/x86_64-w64-mingw32-objcopy CACHE FILEPATH "" FORCE)
set(CMAKE_OBJDUMP /usr/bin/x86_64-w64-mingw32-objdump CACHE FILEPATH "" FORCE)
set(CMAKE_RANLIB /usr/bin/x86_64-w64-mingw32-ranlib CACHE FILEPATH "" FORCE)
set(CMAKE_C_COMPILER_RANLIB /usr/bin/x86_64-w64-mingw32-gcc-ranlib CACHE FILEPATH "" FORCE)
set(CMAKE_CXX_COMPILER_RANLIB /usr/bin/x86_64-w64-mingw32-gcc-ranlib CACHE FILEPATH "" FORCE)
set(CMAKE_STRIP /usr/bin/x86_64-w64-mingw32-strip CACHE FILEPATH "" FORCE)

set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32;${PROJECT_SOURCE_DIR})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_INSTALL_PREFIX ${CMAKE_FIND_ROOT_PATH}/usr CACHE FILEPATH "install path prefix")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-static")

0 comments on commit 5509f23

Please sign in to comment.