libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
sftp.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2#pragma once
3
4#include <sshpp/config.hpp>
5
8#include <sshpp/export.hpp>
9#include <sshpp/fwd.hpp>
10#include <sshpp/result.hpp>
13#include <sshpp/sftp/file.hpp>
14#include <sshpp/types.hpp>
15
16#include <filesystem>
17#include <vector>
18
19namespace sshpp::sftp {
20
21enum class OpenMode : unsigned {
22 read = 1u << 0,
23 write = 1u << 1,
25 create = 1u << 2,
26 truncate = 1u << 3,
27 append = 1u << 4,
28 exclusive = 1u << 5,
29};
30SSHPP_API inline OpenMode operator|(OpenMode a, OpenMode b) noexcept {
31 return static_cast<OpenMode>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
32}
33SSHPP_API inline bool has_flag(OpenMode v, OpenMode flag) noexcept {
34 return (static_cast<unsigned>(v) & static_cast<unsigned>(flag)) != 0;
35}
36
37struct Limits {
38 std::uint64_t max_packet_length = 0;
39 std::uint64_t max_read_length = 0;
40 std::uint64_t max_write_length = 0;
41 std::uint64_t max_open_handles = 0;
42};
43
45class SSHPP_API Sftp {
46public:
47 Sftp() = default;
48 explicit Sftp(Session& session);
49 ~Sftp();
50 Sftp(Sftp&&) noexcept;
51 Sftp& operator=(Sftp&&) noexcept;
52 Sftp(const Sftp&) = delete;
53
54 Result<void> try_init();
55 explicit operator bool() const noexcept { return native_ != nullptr; }
56 native_sftp native_handle() const noexcept { return native_; }
57
58 int protocol_version() const noexcept;
59 Result<Limits> try_limits() const;
60
61 // ---- files -----------------------------------------------------------
62 Result<File> try_open(const RemotePath&, OpenMode,
63 std::filesystem::perms create_perms = std::filesystem::perms::owner_read |
64 std::filesystem::perms::owner_write);
65
66 // ---- metadata ---------------------------------------------------------
67 Result<Attributes> try_stat(const RemotePath&) const;
68 Result<Attributes> try_lstat(const RemotePath&) const;
69 Result<bool> try_exists(const RemotePath&) const;
70 Result<void> try_chmod(const RemotePath&, std::filesystem::perms);
71 Result<void> try_chown(const RemotePath&, std::uint32_t uid, std::uint32_t gid);
72
73 // ---- namespace ----------------------------------------------------------
74 Result<void> try_mkdir(const RemotePath&,
75 std::filesystem::perms = std::filesystem::perms::owner_all);
76 Result<void> try_mkdir_p(const RemotePath&,
77 std::filesystem::perms = std::filesystem::perms::owner_all);
78 Result<void> try_rmdir(const RemotePath&);
79 Result<void> try_remove(const RemotePath&);
80 Result<void> try_rename(const RemotePath& from, const RemotePath& to);
81 Result<void> try_symlink(const RemotePath& target, const RemotePath& link);
82 Result<RemotePath> try_readlink(const RemotePath&) const;
83 Result<RemotePath> try_canonicalize(const RemotePath&) const;
84
85 // ---- directories ----------------------------------------------------------
86 Result<Directory> try_open_directory(const RemotePath&) const;
87 Result<std::vector<Attributes>> try_list(const RemotePath&) const;
88
89private:
90 native_sftp native_ = nullptr;
91 detail::SessionCorePtr core_;
92};
93
95SSHPP_API Result<DirectoryRange> entries(Sftp&, const RemotePath&);
96
97} // namespace sshpp::sftp
98
99#if SSHPP_HEADER_ONLY
100#include <sshpp/detail/sftp.ipp>
101#endif
Definition types.hpp:76
Definition result.hpp:16
Definition session.hpp:41
Range adapter: for (const auto& e : sftp::DirectoryRange{std::move(dir)}).
Definition directory.hpp:95
An open SFTP directory handle. See docs/design/06 §6.4.
Definition directory.hpp:21
A single open SFTP file handle. See docs/design/06 §6.3.
Definition file.hpp:21
The SFTP subsystem session. See docs/design/06 §6.2.
Definition sftp.hpp:45
native_sftp native_handle() const noexcept
Definition sftp.hpp:56
Definition algorithms.ipp:10
OpenMode
Definition sftp.hpp:21
SSHPP_API bool has_flag(OpenMode v, OpenMode flag) noexcept
Definition sftp.hpp:33
SSHPP_API OpenMode operator|(OpenMode a, OpenMode b) noexcept
Definition sftp.hpp:30
::sftp_session_struct * native_sftp
Definition native_fwd.hpp:31
Definition error.hpp:110
Value-type copy of libssh's sftp_attributes. See docs/design/06 §6.1.
Definition attributes.hpp:19
Definition sftp.hpp:37
std::uint64_t max_packet_length
Definition sftp.hpp:38
std::uint64_t max_open_handles
Definition sftp.hpp:41
std::uint64_t max_write_length
Definition sftp.hpp:40
std::uint64_t max_read_length
Definition sftp.hpp:39