Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[daemon] let daemon choose mount target if not specified #3695

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class Utils : public Singleton<Utils>
virtual QString make_uuid(const std::optional<std::string>& seed = std::nullopt) const;
virtual void sleep_for(const std::chrono::milliseconds& ms) const;
virtual bool is_ipv4_valid(const std::string& ipv4) const;

virtual Path default_mount_target(const Path& source) const;
};
} // namespace multipass

Expand Down
7 changes: 5 additions & 2 deletions src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,10 @@ try // clang-format on
for (const auto& path_entry : request->target_paths())
{
const auto& name = path_entry.instance_name();
const auto target_path = QDir::cleanPath(QString::fromStdString(path_entry.target_path())).toStdString();
const auto q_target_path = path_entry.target_path().empty()
? MP_UTILS.default_mount_target(QString::fromStdString(request->source_path()))
: QDir::cleanPath(QString::fromStdString(path_entry.target_path()));
const auto target_path = q_target_path.toStdString();

auto it = operative_instances.find(name);
if (it == operative_instances.end())
Expand All @@ -1922,7 +1925,7 @@ try // clang-format on
}
auto& vm = it->second;

if (mp::utils::invalid_target_path(QString::fromStdString(target_path)))
if (mp::utils::invalid_target_path(q_target_path))
{
add_fmt_to(errors, "unable to mount to \"{}\"", target_path);
continue;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,11 @@ bool mp::Utils::is_ipv4_valid(const std::string& ipv4) const
return true;
}

mp::Path mp::Utils::default_mount_target(const Path& source) const
{
return source.isEmpty() ? "" : QDir{QDir::cleanPath(source)}.dirName().prepend("/home/ubuntu/");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we throw if this is empty? You probably want to do that in this function's client (daemon) and make it a pre-condition here that the directory isn't empty, perhaps with an assert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a purpose for returning an empty path if given an empty path, which is the GUI target preview. If the source field in the GUI is left empty, the target should also be empty. So this just simplifies that process. But I understand that taken in isolation, the better behavior for this method is to error if the source is empty.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, well that could be done in the GUI itself, no? Anyway, I am OK keeping it like this as long as the daemon itself handles the case.

}

auto mp::utils::find_bridge_with(const std::vector<mp::NetworkInterfaceInfo>& networks,
const std::string& target_network,
const std::string& bridge_type) -> std::optional<mp::NetworkInterfaceInfo>
Expand Down
1 change: 1 addition & 0 deletions tests/mock_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MockUtils : public Utils
MOCK_METHOD(QString, make_uuid, (const std::optional<std::string>&), (const, override));
MOCK_METHOD(void, sleep_for, (const std::chrono::milliseconds&), (const, override));
MOCK_METHOD(bool, is_ipv4_valid, (const std::string& ipv4), (const, override));
MOCK_METHOD(Path, default_mount_target, (const Path& source), (const, override));

MP_MOCK_SINGLETON_BOILERPLATE(MockUtils, Utils);
};
Expand Down
Loading