Skip to content

Commit 9ad8fb0

Browse files
committed
nbd.Server: Enable to override device not found behavior.
1 parent 45787c3 commit 9ad8fb0

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

nbd/src/elle/nbd/Server.cc

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,19 @@ namespace elle
389389
auto& device =
390390
[&] () -> Device&
391391
{
392-
if (this->_devices.empty())
393-
elle::err("no devices available");
394-
if (data == elle::ConstWeakBuffer(""))
392+
if (data == elle::ConstWeakBuffer("") && !this->_devices.empty())
395393
return this->_devices.front().get();
394+
auto res = boost::range::find_if(
395+
this->_devices,
396+
[&] (Device const& d)
397+
{ return data == d.name(); });
398+
if (res != this->_devices.end())
399+
return (*res).get();
396400
else
397401
{
398-
auto res = boost::range::find_if(
399-
this->_devices,
400-
[&] (Device const& d)
401-
{ return data == d.name(); });
402-
if (res != this->_devices.end())
403-
return (*res).get();
404-
elle::err("no such device: {}", data);
402+
auto& res = this->_device_not_found(data.string());
403+
this->add(res);
404+
return res;
405405
}
406406
}();
407407
ELLE_TRACE_SCOPE("export volume {}", device.name());
@@ -484,6 +484,12 @@ namespace elle
484484
}
485485
}
486486

487+
Server::Device&
488+
Server::_device_not_found(std::string name)
489+
{
490+
elle::err("no such device: {}", name);
491+
}
492+
487493
void
488494
Server::add(Device& d)
489495
{

nbd/src/elle/nbd/Server.hh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,26 @@ namespace elle
9090
/// it to memory for instance could speed up things. Takin no action,
9191
/// which is the default implementation, is correct.
9292
///
93-
/// @param offset Index of the first byte to cache. @param length
94-
/// Number of bytes to cache.
93+
/// @param offset Index of the first byte to cache.
94+
/// @param length Number of bytes to cache.
9595
virtual
9696
void
9797
cache(uint64_t offset, uint32_t length);
9898
};
9999
/// Register a device to be exposed by the server.
100100
void
101101
add(Device& d);
102+
/// Called when a requested device is not found.
103+
///
104+
/// Subclasses may return a dynamically allocated device or throw an
105+
/// exception to refuse doing so, which is the default behavior.
106+
///
107+
/// @param name The name of the device that was requested and not found.
108+
/// @return The device for that name.
109+
/// @throw elle::Error Signifies the device does not exist.
110+
virtual
111+
Device&
112+
_device_not_found(std::string name);
102113
ELLE_ATTRIBUTE_R(std::vector<std::reference_wrapper<Device>>, devices);
103114

104115
/*-------.

0 commit comments

Comments
 (0)