Commit e9d58742 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

fs: Rework external factories device notifications

An external IFileSystemFactory is now responsible for calling the
mounted/unmounted callbacks with the device that changed. This is part
of the effort to handle multiple mountpoints for a given device
parent d4d7c812
......@@ -43,20 +43,24 @@ namespace fs
virtual ~IFileSystemFactoryCb() = default;
/**
* @brief onDevicePlugged Shall be invoked when a device gets plugged
* @param uuid The device UUID
* @param device The mounted device
* @param newMountpoint The device mountpoint that was just mounted
*
* When this callback returns, the medialibrary will assume that the
* corresponding fs::IDevice::isPresent method will return true.
*/
virtual void onDevicePlugged( const std::string& uuid ) = 0;
virtual void onDeviceMounted( const fs::IDevice& device,
const std::string& newMountpoint ) = 0;
/**
* @brief onDeviceUnplugged Shall be invoked when a device gets unplugged
* @param uuid The device UUID
* @param device The unmounted device
* @param unmountedMountpoint The device mountpoint that was just unmounted
*
* When this callback returns, the medialibrary will assume that the
* corresponding fs::IDevice::isPresent method will return false.
*/
virtual void onDeviceUnplugged( const std::string& uuid ) = 0;
virtual void onDeviceUnmounted( const fs::IDevice& device,
const std::string& unmountedMountpoint ) = 0;
};
class IFileSystemFactory
......
......@@ -1592,25 +1592,37 @@ MediaLibrary::FsFactoryCb::FsFactoryCb(MediaLibrary* ml)
{
}
void MediaLibrary::FsFactoryCb::onDevicePlugged( const std::string& uuid )
void MediaLibrary::FsFactoryCb::onDeviceChanged( const fs::IDevice& deviceFs ) const
{
onDeviceChanged( uuid, true );
auto device = Device::fromUuid( m_ml, deviceFs.uuid() );
// The device might be new and unused so far, we will create it when we need to
if ( device == nullptr )
return;
assert( device->isRemovable() == true );
if ( device->isPresent() != deviceFs.isPresent() )
{
LOG_INFO( "Device ", deviceFs.uuid(), " changed presence state: ",
device->isPresent() ? "1" : "0", " -> ",
deviceFs.isPresent() ? "1" : "0" );
device->setPresent( deviceFs.isPresent() );
}
}
void MediaLibrary::FsFactoryCb::onDeviceUnplugged( const std::string& uuid )
void MediaLibrary::FsFactoryCb::onDeviceMounted( const fs::IDevice& deviceFs,
const std::string& newMountpoint )
{
onDeviceChanged( uuid, false );
LOG_INFO( "Device ", deviceFs.uuid(), " mountpoint ", newMountpoint,
" was mounted" );
onDeviceChanged( deviceFs );
}
void MediaLibrary::FsFactoryCb::onDeviceChanged( const std::string& uuid, bool isPresent )
void MediaLibrary::FsFactoryCb::onDeviceUnmounted( const fs::IDevice& deviceFs,
const std::string& unmountedMountpoint )
{
auto device = Device::fromUuid( m_ml, uuid );
if ( device == nullptr )
return;
assert( device->isRemovable() == true );
if ( device->isPresent() == isPresent )
return;
device->setPresent( isPresent );
LOG_INFO( "Device ", deviceFs.uuid(), " mountpoint ", unmountedMountpoint,
" was unmounted" );
onDeviceChanged( deviceFs );
}
}
......@@ -243,9 +243,11 @@ class MediaLibrary : public IMediaLibrary
public:
FsFactoryCb( MediaLibrary* ml );
private:
virtual void onDevicePlugged( const std::string& uuid ) override;
virtual void onDeviceUnplugged( const std::string& uuid ) override;
void onDeviceChanged( const std::string& uuid, bool isPresent );
virtual void onDeviceMounted( const fs::IDevice& deviceFs,
const std::string& newMountpoint ) override;
virtual void onDeviceUnmounted( const fs::IDevice& deviceFs,
const std::string& removedMountpoint ) override;
void onDeviceChanged( const fs::IDevice& deviceFs ) const;
private:
MediaLibrary* m_ml;
};
......
......@@ -144,23 +144,32 @@ void NetworkFileSystemFactory::onDeviceAdded( VLC::MediaPtr media )
}
m_deviceCond.notify_one();
LOG_INFO( "Adding new network device: name: ", name, " - mrl: ", mrl );
m_cb->onDevicePlugged( name );
m_cb->onDeviceMounted( *(*m_devices.rbegin()).device, mrl );
}
void NetworkFileSystemFactory::onDeviceRemoved( VLC::MediaPtr media )
{
const auto& mrl = media->mrl();
std::shared_ptr<fs::NetworkDevice> device;
{
std::lock_guard<compat::Mutex> lock( m_devicesLock );
m_devices.erase( std::remove_if( begin( m_devices ), end( m_devices ), [&mrl]( const Device& d ) {
auto it = std::find_if( begin( m_devices ), end( m_devices ), [&mrl]( const Device& d ) {
return d.mrl == mrl;
}), end( m_devices ) );
});
if ( it != end( m_devices ) )
{
device =(*it).device;
m_devices.erase( it );
}
}
if ( device == nullptr )
{
assert( !"Unknown network device was removed" );
return;
}
const auto name = utils::file::stripScheme( mrl );
LOG_INFO( "Device ", mrl, " was removed" );
m_cb->onDeviceUnplugged( name );
m_cb->onDeviceUnmounted( *device, mrl );
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment