libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
scp.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SCP is legacy; prefer sshpp::sftp for new code (SFTP is the OpenSSH default
4// since 9.0 and has no equivalent of CVE-2019-6111). This module exists for
5// servers/appliances that only speak SCP. See docs/design/06 ยง6.7.
6#pragma once
7
8#include <sshpp/config.hpp>
9
12#include <sshpp/export.hpp>
13#include <sshpp/fwd.hpp>
14#include <sshpp/result.hpp>
15#include <sshpp/types.hpp>
16
17#include <cstdint>
18#include <filesystem>
19#include <functional>
20#include <iosfwd>
21#include <optional>
22#include <string>
23
24namespace sshpp::scp {
25
28
29struct Request {
31 std::string name;
32 std::uint64_t size = 0;
33 std::filesystem::perms permissions{};
34 std::string warning;
35};
36
37struct Progress {
38 std::uint64_t bytes_done = 0;
39 std::optional<std::uint64_t> bytes_total;
40};
42using ProgressCallback = std::function<bool(const Progress&)>;
43
45class SSHPP_API Reader {
46public:
47 Reader() = default;
48 Reader(Session& session, const RemotePath& location, bool recursive);
49 ~Reader();
50 Reader(Reader&&) noexcept;
51 Reader& operator=(Reader&&) noexcept;
52 Reader(const Reader&) = delete;
53
54 explicit operator bool() const noexcept { return native_ != nullptr; }
55
56 Result<void> try_init();
57
60 Result<void> try_accept();
61 Result<void> try_deny(std::string_view reason);
63 Result<std::uint64_t> try_read_to(std::ostream&, ProgressCallback = {});
64
65private:
66 native_scp native_ = nullptr;
68};
69
71class SSHPP_API Writer {
72public:
73 Writer() = default;
74 Writer(Session& session, const RemotePath& destination_dir, bool recursive);
75 ~Writer();
76 Writer(Writer&&) noexcept;
77 Writer& operator=(Writer&&) noexcept;
78 Writer(const Writer&) = delete;
79
80 explicit operator bool() const noexcept { return native_ != nullptr; }
81
82 Result<void> try_init();
83
84 Result<void> try_push_file(std::string_view name, std::uint64_t size,
85 std::filesystem::perms = std::filesystem::perms::owner_read |
86 std::filesystem::perms::owner_write);
87 Result<void> try_push_directory(std::string_view name,
88 std::filesystem::perms = std::filesystem::perms::owner_all);
89 Result<void> try_leave_directory();
90 Result<void> try_write(ByteView);
91 Result<std::uint64_t> try_write_from(std::istream&, std::uint64_t size, ProgressCallback = {});
92
93private:
94 native_scp native_ = nullptr;
96};
97
100SSHPP_API Result<std::uint64_t> try_download(Session&, const RemotePath&,
101 const std::filesystem::path&, ProgressCallback = {});
102SSHPP_API Result<std::uint64_t> try_upload(Session&, const std::filesystem::path&,
103 const RemotePath&, ProgressCallback = {});
104
105} // namespace sshpp::scp
106
107#if SSHPP_HEADER_ONLY
108#include <sshpp/detail/scp.ipp>
109#endif
Input byte range. Implicitly constructible from std::string_view / std::vector<std::byte>.
Definition types.hpp:54
Output byte range (caller-owned, mutable buffer to write into).
Definition types.hpp:67
Definition types.hpp:76
Definition result.hpp:16
Definition session.hpp:41
Pull side of an SCP transfer: scp -f as seen by the client.
Definition scp.hpp:45
Push side of an SCP transfer: scp -t as seen by the client.
Definition scp.hpp:71
std::shared_ptr< SessionCore > SessionCorePtr
Definition session_core.hpp:43
Definition scp.ipp:25
SSHPP_INLINE Result< std::uint64_t > try_upload(Session &session, const std::filesystem::path &local, const RemotePath &remote, ProgressCallback progress)
Definition scp.ipp:312
RequestType
Definition scp.hpp:27
SSHPP_INLINE Result< std::uint64_t > try_download(Session &session, const RemotePath &remote, const std::filesystem::path &local, ProgressCallback progress)
Definition scp.ipp:269
Mode
Definition scp.hpp:26
std::function< bool(const Progress &)> ProgressCallback
Return false to abort the transfer (yields errc::cancelled).
Definition scp.hpp:42
::ssh_scp_struct * native_scp
Definition native_fwd.hpp:34
Definition scp.hpp:37
std::optional< std::uint64_t > bytes_total
Definition scp.hpp:39
std::uint64_t bytes_done
Definition scp.hpp:38
Definition scp.hpp:29
std::uint64_t size
Definition scp.hpp:32
std::string warning
Definition scp.hpp:34
RequestType type
Definition scp.hpp:30
std::string name
Definition scp.hpp:31
std::filesystem::perms permissions
Definition scp.hpp:33