Skip to content

Commit

Permalink
Merge pull request #584 from elfmz/netrocks-scp
Browse files Browse the repository at this point in the history
Netrocks: SCP protocol support (touch #247)
  • Loading branch information
elfmz authored Sep 1, 2019
2 parents 70594ff + aeb9002 commit f228d9c
Show file tree
Hide file tree
Showing 30 changed files with 1,600 additions and 592 deletions.
4 changes: 3 additions & 1 deletion NetRocks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ src/Host/HostRemote.cpp
src/UI/DialogUtils.cpp
src/UI/Settings/SiteConnectionEditor.cpp
src/UI/Settings/ConfigureProtocolFile.cpp
src/UI/Settings/ConfigureProtocolSFTP.cpp
src/UI/Settings/ConfigureProtocolSFTPSCP.cpp
src/UI/Settings/ConfigureProtocolSMB.cpp
src/UI/Settings/ConfigureProtocolNFS.cpp
src/UI/Settings/ConfigureProtocolWebDAV.cpp
Expand Down Expand Up @@ -71,7 +71,9 @@ if (LIBSSH_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_SFTP")
add_executable (NetRocks-SFTP
${PROTOCOL_SOURCES}
src/Protocol/SSHConnection.cpp
src/Protocol/ProtocolSFTP.cpp
src/Protocol/ProtocolSCP.cpp
)
target_link_libraries(NetRocks-SFTP utils ${LIBSSH_LIBRARIES})
target_include_directories(NetRocks-SFTP PRIVATE src ${LIBSSH_INCLUDE_DIRS})
Expand Down
1 change: 1 addition & 0 deletions NetRocks/configs/plug/eng.lng
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
"&Background tasks"

"SFTP protocol options"
"SCP protocol options"
"Private &key file path:"
"Custom &subsystem request/exec:"
"Max &read block size, bytes:"
Expand Down
1 change: 1 addition & 0 deletions NetRocks/configs/plug/rus.lng
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
"&Фоновые задачи"

"Настройки протокола SFTP"
"Настройки протокола SCP"
"Путь к файлу приватного &ключа:"
"Запрос особой подсистемы:"
"Размер блока чтения, байт:"
Expand Down
2 changes: 1 addition & 1 deletion NetRocks/src/Host/HostLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ std::shared_ptr<IFileReader> HostLocal::FileGet(const std::string &path, unsigne
return std::make_shared<HostLocalFileIO>(path, resume_pos, O_RDONLY, 0 );
}

std::shared_ptr<IFileWriter> HostLocal::FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos) throw (std::runtime_error)
std::shared_ptr<IFileWriter> HostLocal::FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos) throw (std::runtime_error)
{
return std::make_shared<HostLocalFileIO>(path, resume_pos, (resume_pos == 0) ? O_CREAT | O_TRUNC | O_RDWR : O_RDWR, mode );
}
Expand Down
2 changes: 1 addition & 1 deletion NetRocks/src/Host/HostLocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ class HostLocal : public IHost

virtual std::shared_ptr<IDirectoryEnumer> DirectoryEnum(const std::string &path) throw (std::runtime_error);
virtual std::shared_ptr<IFileReader> FileGet(const std::string &path, unsigned long long resume_pos = 0) throw (std::runtime_error);
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos = 0) throw (std::runtime_error);
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos = 0) throw (std::runtime_error);

};
3 changes: 2 additions & 1 deletion NetRocks/src/Host/HostRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,14 @@ std::shared_ptr<IFileReader> HostRemote::FileGet(const std::string &path, unsign
return std::make_shared<HostRemoteFileIO>(shared_from_this(), false);
}

std::shared_ptr<IFileWriter> HostRemote::FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos) throw (std::runtime_error)
std::shared_ptr<IFileWriter> HostRemote::FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos) throw (std::runtime_error)
{
CheckReady();

SendCommand(IPC_FILE_PUT);
SendString(path);
SendPOD(mode);
SendPOD(size_hint);
SendPOD(resume_pos);
RecvReply(IPC_FILE_PUT);

Expand Down
2 changes: 1 addition & 1 deletion NetRocks/src/Host/HostRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class HostRemote : protected IPCRecver, protected IPCSender, public std::enable_

virtual std::shared_ptr<IDirectoryEnumer> DirectoryEnum(const std::string &path) throw (std::runtime_error);
virtual std::shared_ptr<IFileReader> FileGet(const std::string &path, unsigned long long resume_pos = 0) throw (std::runtime_error);
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos = 0) throw (std::runtime_error);
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos = 0) throw (std::runtime_error);

virtual void ExecuteCommand(const std::string &working_dir, const std::string &command_line, const std::string &fifo) throw (std::runtime_error);
};
11 changes: 6 additions & 5 deletions NetRocks/src/Host/HostRemoteBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HostRemoteBroker : protected IPCEndpoint
std::string str1, str2, str3;
timespec ts1, ts2;
FileInformation file_info;
unsigned long long ull;
unsigned long long ull1, ull2;
mode_t mode;
bool b;
} _args;
Expand Down Expand Up @@ -88,8 +88,8 @@ class HostRemoteBroker : protected IPCEndpoint
void OnFileGet()
{
RecvString(_args.str1);
RecvPOD(_args.ull);
std::shared_ptr<IFileReader> reader = _protocol->FileGet(_args.str1, _args.ull);
RecvPOD(_args.ull1);
std::shared_ptr<IFileReader> reader = _protocol->FileGet(_args.str1, _args.ull1);
SendCommand(IPC_FILE_GET);

for (;;) {
Expand Down Expand Up @@ -123,8 +123,9 @@ class HostRemoteBroker : protected IPCEndpoint
{
RecvString(_args.str1);
RecvPOD(_args.mode);
RecvPOD(_args.ull);
std::shared_ptr<IFileWriter> writer = _protocol->FilePut(_args.str1, _args.mode, _args.ull);
RecvPOD(_args.ull1);
RecvPOD(_args.ull2);
std::shared_ptr<IFileWriter> writer = _protocol->FilePut(_args.str1, _args.mode, _args.ull1, _args.ull2);
SendCommand(IPC_FILE_PUT);
// Trick to improve IO parallelization: instead of sending status reply on operation,
// send preliminary OK and if error will occur - do error reply on next operation.
Expand Down
52 changes: 3 additions & 49 deletions NetRocks/src/Op/OpExecute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,7 @@ SHAREDSYMBOL int OpExecute_Shell(int argc, char *argv[])
// fprintf(stderr, "OpExecute_Shell: LEAVE\n");
g_fd_ctl = -1;


int status = -1;
FDScope fd_status(open((fifo + ".status").c_str(), O_RDONLY));
if (fd_status.Valid()) {
ReadAll(fd_status, &status, sizeof(status));
}

return status;
return ExecCommandFIFO::sReadStatus(fifo);
}


Expand All @@ -178,54 +171,15 @@ OpExecute::OpExecute(std::shared_ptr<IHost> &host, const std::string &dir, const
_dir(dir),
_command(command)
{
char sz[64] = {};
snprintf(sz, sizeof(sz) - 1, "NetRocks/fifo/%lx", (unsigned long)getpid());

_fifo = InMyTemp(sz);

CleanupFIFO();

if (mkfifo((_fifo + ".ctl").c_str(), 0700) == -1 ) {
throw std::runtime_error("mkfifo ctl");
}
if (mkfifo((_fifo + ".in").c_str(), 0700) == -1 ) {
CleanupFIFO();
throw std::runtime_error("mkfifo in");
}
if (mkfifo((_fifo + ".out").c_str(), 0700) == -1) {
CleanupFIFO();
throw std::runtime_error("mkfifo out");
}
if (mkfifo((_fifo + ".err").c_str(), 0700) == -1) {
CleanupFIFO();
throw std::runtime_error("mkfifo err");
}
}

OpExecute::~OpExecute()
{
CleanupFIFO();
}

void OpExecute::CleanupFIFO()
{
{
FDScope fd_ctl(open((_fifo + ".ctl").c_str(), O_WRONLY | O_NONBLOCK));
FDScope fd_in(open((_fifo + ".in").c_str(), O_WRONLY | O_NONBLOCK));
FDScope fd_out(open((_fifo + ".out").c_str(), O_RDONLY | O_NONBLOCK));
FDScope fd_err(open((_fifo + ".err").c_str(), O_RDONLY | O_NONBLOCK));
}

unlink((_fifo + ".ctl").c_str());
unlink((_fifo + ".in").c_str());
unlink((_fifo + ".out").c_str());
unlink((_fifo + ".err").c_str());
unlink((_fifo + ".status").c_str());
}

void OpExecute::Do()
{
_host->ExecuteCommand(_dir, _command, _fifo);
G.info.FSF->ExecuteLibrary(G.plugin_path.c_str(), L"OpExecute_Shell", StrMB2Wide(_fifo).c_str(), EF_NOCMDPRINT);
_host->ExecuteCommand(_dir, _command, _fifo.FileName());
G.info.FSF->ExecuteLibrary(G.plugin_path.c_str(), L"OpExecute_Shell", StrMB2Wide(_fifo.FileName()).c_str(), EF_NOCMDPRINT);
}

3 changes: 2 additions & 1 deletion NetRocks/src/Op/OpExecute.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#include <string>
#include <memory>
#include "../Host/Host.h"
#include "Utils/ExecCommandFIFO.hpp"

class OpExecute
{
std::shared_ptr<IHost> _host;
std::string _dir;
std::string _command;
std::string _fifo;
ExecCommandFIFO _fifo;

void CleanupFIFO();

Expand Down
2 changes: 1 addition & 1 deletion NetRocks/src/Op/OpXfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ bool OpXfer::FileCopyLoop(const std::string &path_src, const std::string &path_d
indicted = _base_host.get();
std::shared_ptr<IFileReader> reader = _base_host->FileGet(path_src, file_complete);
indicted = _dst_host.get();
std::shared_ptr<IFileWriter> writer = _dst_host->FilePut(path_dst, info.mode | EXTRA_NEEDED_MODE, file_complete);
std::shared_ptr<IFileWriter> writer = _dst_host->FilePut(path_dst, info.mode | EXTRA_NEEDED_MODE, info.size, file_complete);
if (!_io_buf.Size())
throw std::runtime_error("No buffer - no file");

Expand Down
77 changes: 77 additions & 0 deletions NetRocks/src/Op/Utils/ExecCommandFIFO.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once
#include <ScopeHelpers.h>
#include <string>
#include <fcntl.h>

class ExecCommandFIFO
{
std::string _fifo;

void CleanupFIFO()
{
{
FDScope fd_ctl(open((_fifo + ".ctl").c_str(), O_WRONLY | O_NONBLOCK));
FDScope fd_in(open((_fifo + ".in").c_str(), O_WRONLY | O_NONBLOCK));
FDScope fd_out(open((_fifo + ".out").c_str(), O_RDONLY | O_NONBLOCK));
FDScope fd_err(open((_fifo + ".err").c_str(), O_RDONLY | O_NONBLOCK));
}

unlink((_fifo + ".ctl").c_str());
unlink((_fifo + ".in").c_str());
unlink((_fifo + ".out").c_str());
unlink((_fifo + ".err").c_str());
unlink((_fifo + ".status").c_str());
}

public:
ExecCommandFIFO() throw (std::runtime_error)
{
char sz[64] = {};
snprintf(sz, sizeof(sz) - 1, "NetRocks/fifo/%lx", (unsigned long)getpid());

_fifo = InMyTemp(sz);

CleanupFIFO();

if (mkfifo((_fifo + ".ctl").c_str(), 0700) == -1 ) {
throw std::runtime_error("mkfifo ctl");
}
if (mkfifo((_fifo + ".in").c_str(), 0700) == -1 ) {
CleanupFIFO();
throw std::runtime_error("mkfifo in");
}
if (mkfifo((_fifo + ".out").c_str(), 0700) == -1) {
CleanupFIFO();
throw std::runtime_error("mkfifo out");
}
if (mkfifo((_fifo + ".err").c_str(), 0700) == -1) {
CleanupFIFO();
throw std::runtime_error("mkfifo err");
}
}

virtual ~ExecCommandFIFO()
{
CleanupFIFO();
}

inline const std::string &FileName() const { return _fifo; }


static int sReadStatus(const std::string &fifo)
{
int status = -1;
FDScope fd_status(open((fifo + ".status").c_str(), O_RDONLY));
if (fd_status.Valid()) {
ReadAll(fd_status, &status, sizeof(status));
}

return status;
}

int ReadStatus()
{
return sReadStatus(_fifo);
}

};
2 changes: 2 additions & 0 deletions NetRocks/src/Protocol/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Protocol.h"

void ConfigureProtocolSFTP(std::string &options);
void ConfigureProtocolSCP(std::string &options);
void ConfigureProtocolSMB(std::string &options);
void ConfigureProtocolNFS(std::string &options);
void ConfigureProtocolWebDAV(std::string &options);
Expand All @@ -11,6 +12,7 @@ void ConfigureProtocolFile(std::string &options);
static ProtocolInfo s_protocols[] = {
#ifdef HAVE_SFTP
{ "sftp", "NetRocks-SFTP", 22, true, true, ConfigureProtocolSFTP},
{ "scp", "NetRocks-SFTP", 22, true, true, ConfigureProtocolSCP},
#endif
#ifdef HAVE_SMB
{ "smb", "NetRocks-SMB", -1, false, true, ConfigureProtocolSMB},
Expand Down
2 changes: 1 addition & 1 deletion NetRocks/src/Protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct IProtocol

virtual std::shared_ptr<IDirectoryEnumer> DirectoryEnum(const std::string &path) throw (std::runtime_error) = 0;
virtual std::shared_ptr<IFileReader> FileGet(const std::string &path, unsigned long long resume_pos = 0) throw (std::runtime_error) = 0;
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos = 0) throw (std::runtime_error) = 0;
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos = 0) throw (std::runtime_error) = 0;

virtual void ExecuteCommand(const std::string &working_dir, const std::string &command_line, const std::string &fifo) throw (std::runtime_error)
{ throw ProtocolUnsupportedError(""); }
Expand Down
2 changes: 1 addition & 1 deletion NetRocks/src/Protocol/ProtocolNFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ std::shared_ptr<IFileReader> ProtocolNFS::FileGet(const std::string &path, unsig
return std::make_shared<NFSFileIO>(_nfs, MountedRootedPath(path), O_RDONLY, 0, resume_pos);
}

std::shared_ptr<IFileWriter> ProtocolNFS::FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos) throw (std::runtime_error)
std::shared_ptr<IFileWriter> ProtocolNFS::FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos) throw (std::runtime_error)
{
return std::make_shared<NFSFileIO>(_nfs, MountedRootedPath(path), O_WRONLY | O_CREAT | (resume_pos ? 0 : O_TRUNC), mode, resume_pos);
}
2 changes: 1 addition & 1 deletion NetRocks/src/Protocol/ProtocolNFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ class ProtocolNFS : public IProtocol

virtual std::shared_ptr<IDirectoryEnumer> DirectoryEnum(const std::string &path) throw (std::runtime_error);
virtual std::shared_ptr<IFileReader> FileGet(const std::string &path, unsigned long long resume_pos = 0) throw (std::runtime_error);
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long resume_pos = 0) throw (std::runtime_error);
virtual std::shared_ptr<IFileWriter> FilePut(const std::string &path, mode_t mode, unsigned long long size_hint, unsigned long long resume_pos = 0) throw (std::runtime_error);
};
Loading

0 comments on commit f228d9c

Please sign in to comment.