blob: 5903990f428c14d5d4ff25d8a7992beaf0df681b [file] [log] [blame] [edit]
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fidl/fuchsia.io/cpp/wire_types.h>
#include <lib/driver2/inspect.h>
#include <lib/fidl/llcpp/channel.h>
#include <lib/sys/component/cpp/outgoing_directory.h>
#include <memory>
#include <fbl/ref_ptr.h>
#include <src/lib/storage/vfs/cpp/pseudo_dir.h>
#include <src/lib/storage/vfs/cpp/synchronous_vfs.h>
#include <src/lib/storage/vfs/cpp/vmo_file.h>
namespace driver {
zx::status<ExposedInspector> ExposedInspector::Create(
async_dispatcher_t* dispatcher, const inspect::Inspector& inspector,
component::OutgoingDirectory& outgoing_directory) {
if (!inspector) {
return zx::error(ZX_ERR_NO_MEMORY);
}
zx::vmo vmo = inspector.DuplicateVmo();
uint64_t vmo_size;
zx_status_t status = vmo.get_size(&vmo_size);
if (status != ZX_OK) {
return zx::error(status);
}
auto vfs = std::make_unique<fs::SynchronousVfs>(dispatcher);
auto diagnostics_dir = fbl::MakeRefCounted<fs::PseudoDir>();
auto vmo_file = fbl::MakeRefCounted<fs::VmoFile>(std::move(vmo), vmo_size);
status = diagnostics_dir->AddEntry("root.inspect", std::move(vmo_file));
if (status != ZX_OK) {
return zx::error(status);
}
auto endpoints = fidl::CreateEndpoints<fuchsia_io::Directory>();
status =
vfs->ServeDirectory(diagnostics_dir, std::move(endpoints->server), fs::Rights::ReadWrite());
if (status != ZX_OK) {
return zx::error(status);
}
auto result = outgoing_directory.AddDirectory(std::move(endpoints->client), "diagnostics");
if (result.is_error()) {
return zx::error(result.status_value());
}
return zx::ok(ExposedInspector(std::move(vfs)));
}
ExposedInspector::ExposedInspector(std::unique_ptr<fs::SynchronousVfs> vfs)
: vfs_(std::move(vfs)) {}
} // namespace driver