blob: d4f5f513dadf57755fa4eec24b62c427be5c9559 [file] [log] [blame]
// 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 <lib/fdio/cpp/caller.h>
#include <lib/stdcompat/span.h>
#include <zircon/status.h>
#include <fbl/unique_fd.h>
#include "src/storage/fshost/block-device-manager.h"
#include "src/storage/fshost/filesystem-mounter.h"
namespace fshost {
enum WatcherType {
kWatcherTypeBlock = 0,
inline constexpr const char* kWatcherPaths[] = {
static_assert(std::size(kWatcherPaths) == WatcherType::kWatcherTypeMax,
"Each watcher type must have a path");
class Watcher {
zx_status_t ReinitWatcher();
static std::vector<Watcher> CreateWatchers();
using WatcherCallback = fit::function<bool(Watcher&, int, int, const char*)>;
using AddDeviceCallback =
fit::function<zx_status_t(BlockDeviceManager&, FilesystemMounter*, fbl::unique_fd)>;
// Parse watch events from |buf|, calling |callback| for each event.
// |callback| should return true if it receives an idle event and the block watcher is paused.
void ProcessWatchMessages(cpp20::span<uint8_t> buf, WatcherCallback callback);
zx_status_t AddDevice(BlockDeviceManager& manager, FilesystemMounter* mounter, fbl::unique_fd fd);
WatcherType type() const { return type_; }
bool ignore_existing() const { return ignore_existing_; }
zx::unowned_channel borrow_watcher() { return watcher_.borrow(); }
Watcher(WatcherType type, fdio_cpp::FdioCaller caller, AddDeviceCallback callback)
: type_(type), caller_(std::move(caller)), add_device_(std::move(callback)) {}
WatcherType type_;
fdio_cpp::FdioCaller caller_;
AddDeviceCallback add_device_;
zx::channel watcher_;
bool ignore_existing_ = false;
} // namespace fshost