Skip to content

Commit a1a9ef2

Browse files
committed
[cli] Unify remote entry checks
Route existence and directory checks after wildcard expansion through one helper so a missing entry is the only condition treated as a non-match. Permission and connection failures now propagate from the trailing-slash filter too. Add a unit test for an inaccessible trailing-slash candidate.
1 parent 5543380 commit a1a9ef2

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

‎src/ssh/sftp_client.cpp‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ bool wildcard_matches(const std::string_view pattern,
8686

8787
namespace multipass
8888
{
89+
namespace
90+
{
91+
enum class RemoteEntryType
92+
{
93+
missing,
94+
non_directory,
95+
directory,
96+
};
97+
98+
RemoteEntryType remote_entry_type(sftp_session sftp, const std::string& path)
99+
{
100+
if (auto attr = mp_sftp_stat(sftp, path.c_str()))
101+
return attr->type == SSH_FILEXFER_TYPE_DIRECTORY ? RemoteEntryType::directory
102+
: RemoteEntryType::non_directory;
103+
104+
// A missing entry simply stops matching; other failures, such as permission problems,
105+
// must not silently truncate the transfer.
106+
if (MP_LIBSSH.sftp_get_error(sftp) != SSH_FX_NO_SUCH_FILE)
107+
throw SFTPError{"cannot access remote path '{}': {}",
108+
path,
109+
MP_LIBSSH.ssh_get_error(sftp->session)};
110+
111+
return RemoteEntryType::missing;
112+
}
113+
} // namespace
114+
89115
namespace mpl = logging;
90116

91117
SFTPSessionUPtr make_sftp_session(ssh_session session)
@@ -151,17 +177,7 @@ std::vector<fs::path> SFTPClient::expand_remote_path(const fs::path& path)
151177
if (expanded_pattern)
152178
{
153179
std::erase_if(candidates, [this](const auto& candidate) {
154-
if (mp_sftp_stat(sftp.get(), candidate.c_str()))
155-
return false;
156-
157-
// A candidate that no longer exists simply stops matching. Other failures,
158-
// such as permission problems, must not silently truncate the transfer.
159-
if (MP_LIBSSH.sftp_get_error(sftp.get()) != SSH_FX_NO_SUCH_FILE)
160-
throw SFTPError{"cannot access remote path '{}': {}",
161-
candidate,
162-
MP_LIBSSH.ssh_get_error(sftp->session)};
163-
164-
return true;
180+
return remote_entry_type(sftp.get(), candidate) == RemoteEntryType::missing;
165181
});
166182
if (candidates.empty())
167183
throw SFTPError{"no matches found: {}", path};
@@ -220,8 +236,7 @@ std::vector<fs::path> SFTPClient::expand_remote_path(const fs::path& path)
220236
if (remote_path.ends_with('/'))
221237
{
222238
std::erase_if(candidates, [this](const auto& candidate) {
223-
const auto attr = mp_sftp_stat(sftp.get(), candidate.c_str());
224-
return !attr || attr->type != SSH_FILEXFER_TYPE_DIRECTORY;
239+
return remote_entry_type(sftp.get(), candidate) != RemoteEntryType::directory;
225240
});
226241

227242
if (candidates.empty())

‎tests/unit/test_sftp_client.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,31 @@ TEST_F(SFTPClient, throwsWhenTrailingSlashPatternMatchesNoDirectories)
528528
mpt::match_what(StrEq("no matches found: dir/*/")));
529529
}
530530

531+
TEST_F(SFTPClient, throwsWhenTrailingSlashPatternCannotAccessCandidate)
532+
{
533+
REPLACE_SFTP_INIT();
534+
535+
std::vector<sftp_attributes> entries{
536+
get_dummy_sftp_attr(SSH_FILEXFER_TYPE_DIRECTORY, "subdir"),
537+
nullptr,
538+
};
539+
REPLACE(sftp_opendir, [](auto, auto path) { return get_dummy_sftp_dir(path); });
540+
auto read_dir = [&, index = 0](auto...) mutable { return entries[index++]; };
541+
REPLACE(sftp_readdir, read_dir);
542+
REPLACE(sftp_dir_eof, [](auto...) { return true; });
543+
REPLACE(sftp_stat, [](auto...) { return nullptr; });
544+
REPLACE(sftp_get_error, [](auto...) { return SSH_FX_PERMISSION_DENIED; });
545+
auto err = "SFTP server: Permission denied";
546+
REPLACE(ssh_get_error, [&](auto...) { return err; });
547+
548+
auto sftp_client = make_sftp_client();
549+
550+
MP_EXPECT_THROW_THAT(
551+
sftp_client.expand_remote_path("dir/*/"),
552+
mp::SFTPError,
553+
mpt::match_what(StrEq(fmt::format("cannot access remote path 'dir/subdir': {}", err))));
554+
}
555+
531556
TEST_F(SFTPClient, throwsWhenRemoteWildcardHasNoMatches)
532557
{
533558
REPLACE_SFTP_INIT();

0 commit comments

Comments
 (0)