libsshpp v0.1.7
Modern C++17 wrapper for libssh
Loading...
Searching...
No Matches
directory.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/result.hpp>
11#include <sshpp/types.hpp>
12
13#include <iterator>
14#include <optional>
15
16namespace sshpp::sftp {
17
18class Sftp;
19
21class SSHPP_API Directory {
22public:
23 Directory() = default;
24 ~Directory();
25 Directory(Directory&&) noexcept;
26 Directory& operator=(Directory&&) noexcept;
27 Directory(const Directory&) = delete;
28
29 explicit operator bool() const noexcept { return native_ != nullptr; }
30 const RemotePath& path() const noexcept { return path_; }
31
33 std::optional<Attributes> next();
34 bool eof() const noexcept;
35 const ErrorInfo& last_error() const noexcept { return last_error_; }
36
37private:
38 friend class Sftp;
40 : native_(n), sftp_(sftp), core_(std::move(core)), path_(std::move(path)) {}
41
42 native_sftp_dir native_ = nullptr;
43 native_sftp sftp_ = nullptr;
45 RemotePath path_;
46 ErrorInfo last_error_;
47};
48
50class SSHPP_API DirectoryIterator {
51public:
53 using difference_type = std::ptrdiff_t;
54 using pointer = const Attributes*;
55 using reference = const Attributes&;
56 using iterator_category = std::input_iterator_tag;
57
58 DirectoryIterator() = default;
59 explicit DirectoryIterator(Directory& dir, bool skip_dot_entries = true)
60 : dir_(&dir), skip_dot_(skip_dot_entries) {
61 advance();
62 }
63
64 const Attributes& operator*() const { return *current_; }
65 const Attributes* operator->() const { return &*current_; }
66
68 advance();
69 return *this;
70 }
71
72 bool operator==(const DirectoryIterator& other) const noexcept {
73 bool this_end = !current_.has_value();
74 bool other_end = !other.current_.has_value();
75 return this_end && other_end;
76 }
77 bool operator!=(const DirectoryIterator& other) const noexcept { return !(*this == other); }
78
79private:
80 void advance() {
81 if (dir_ == nullptr) { current_.reset(); return; }
82 for (;;) {
83 current_ = dir_->next();
84 if (!current_) return;
85 if (!skip_dot_ || (current_->name != "." && current_->name != "..")) return;
86 }
87 }
88
89 Directory* dir_ = nullptr;
90 bool skip_dot_ = true;
91 std::optional<Attributes> current_;
92};
93
95class SSHPP_API DirectoryRange {
96public:
97 explicit DirectoryRange(Directory dir) : dir_(std::move(dir)) {}
100
101private:
102 Directory dir_;
103};
104
105} // namespace sshpp::sftp
106
107#if SSHPP_HEADER_ONLY
109#endif
Definition types.hpp:76
InputIterator over a Directory. Skips "." and ".." by default.
Definition directory.hpp:50
bool operator!=(const DirectoryIterator &other) const noexcept
Definition directory.hpp:77
bool operator==(const DirectoryIterator &other) const noexcept
Definition directory.hpp:72
std::ptrdiff_t difference_type
Definition directory.hpp:53
std::input_iterator_tag iterator_category
Definition directory.hpp:56
DirectoryIterator & operator++()
Definition directory.hpp:67
const Attributes & operator*() const
Definition directory.hpp:64
DirectoryIterator(Directory &dir, bool skip_dot_entries=true)
Definition directory.hpp:59
const Attributes * operator->() const
Definition directory.hpp:65
Range adapter: for (const auto& e : sftp::DirectoryRange{std::move(dir)}).
Definition directory.hpp:95
DirectoryIterator begin()
Definition directory.hpp:98
DirectoryIterator end()
Definition directory.hpp:99
DirectoryRange(Directory dir)
Definition directory.hpp:97
An open SFTP directory handle. See docs/design/06 §6.4.
Definition directory.hpp:21
const RemotePath & path() const noexcept
Definition directory.hpp:30
The SFTP subsystem session. See docs/design/06 §6.2.
Definition sftp.hpp:45
std::shared_ptr< SessionCore > SessionCorePtr
Definition session_core.hpp:43
Definition algorithms.ipp:10
::sftp_session_struct * native_sftp
Definition native_fwd.hpp:31
::sftp_dir_struct * native_sftp_dir
Definition native_fwd.hpp:33
Definition error.hpp:110
std::filesystem::path path
Definition server_sftp_subsystem.ipp:38
DIR * dir
Definition server_sftp_subsystem.ipp:37
Definition error.hpp:119
Value-type copy of libssh's sftp_attributes. See docs/design/06 §6.1.
Definition attributes.hpp:19