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

MediaLibrary: Properly handle devices updates

parent b9e157ca
......@@ -275,6 +275,8 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& thu
return false;
}
}
for ( auto& fsFactory : m_fsFactories )
refreshDevices( *fsFactory );
startDiscoverer();
startParser();
m_initialized = true;
......@@ -780,6 +782,30 @@ void MediaLibrary::setLogger( ILogger* logger )
Log::SetLogger( logger );
}
void MediaLibrary::refreshDevices( factory::IFileSystem& fsFactory )
{
// Don't refuse to process devices when none seem to be present, it might be a valid case
// if the user only discovered removable storages, and we would still need to mark those
// as "not present"
if ( fsFactory.refreshDevices() == false )
LOG_WARN( "No device detected." );
auto devices = Device::fetchAll( this );
for ( auto& d : devices )
{
auto deviceFs = fsFactory.createDevice( d->uuid() );
auto fsDevicePresent = deviceFs != nullptr && deviceFs->isPresent();
if ( d->isPresent() != fsDevicePresent )
{
LOG_INFO( "Device ", d->uuid(), " changed presence state: ", d->isPresent(), " -> ", fsDevicePresent );
d->setPresent( fsDevicePresent );
}
else
{
LOG_INFO( "Device ", d->uuid(), " unchanged" );
}
}
}
bool MediaLibrary::onDevicePlugged( const std::string& uuid, const std::string& mountpoint )
{
auto currentDevice = Device::fromUuid( this, uuid );
......@@ -789,7 +815,7 @@ bool MediaLibrary::onDevicePlugged( const std::string& uuid, const std::string&
{
if ( fsFactory->isMrlSupported( "file://" ) )
{
fsFactory->refreshDevices();
refreshDevices( *fsFactory );
break;
}
}
......@@ -803,7 +829,7 @@ void MediaLibrary::onDeviceUnplugged( const std::string& uuid )
{
if ( fsFactory->isMrlSupported( "file://" ) )
{
fsFactory->refreshDevices();
refreshDevices( *fsFactory );
break;
}
}
......
......@@ -150,6 +150,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
bool updateDatabaseModel( unsigned int previousVersion );
bool createAllTables();
void registerEntityHooks();
void refreshDevices(factory::IFileSystem& fsFactory);
static bool validateSearchPattern( const std::string& pattern );
protected:
......
......@@ -88,12 +88,6 @@ bool FsDiscoverer::discover( const std::string &entryPoint )
bool FsDiscoverer::reload()
{
LOG_INFO( "Reloading all folders" );
// Start by checking if previously known devices have been plugged/unplugged
if ( checkDevices() == false )
{
LOG_ERROR( "Refusing to reloading files with no storage device" );
return false;
}
auto rootFolders = Folder::fetchRootFolders( m_ml );
for ( const auto& f : rootFolders )
{
......@@ -114,12 +108,6 @@ bool FsDiscoverer::reload( const std::string& entryPoint )
if ( m_fsFactory->isMrlSupported( entryPoint ) == false )
return false;
LOG_INFO( "Reloading folder ", entryPoint );
// Start by checking if previously known devices have been plugged/unplugged
if ( checkDevices() == false )
{
LOG_ERROR( "Refusing to reloading files with no storage device" );
return false;
}
auto folder = Folder::fromMrl( m_ml, entryPoint );
if ( folder == nullptr )
{
......@@ -136,28 +124,6 @@ bool FsDiscoverer::reload( const std::string& entryPoint )
return true;
}
bool FsDiscoverer::checkDevices()
{
if ( m_fsFactory->refreshDevices() == false )
return false;
auto devices = Device::fetchAll( m_ml );
for ( auto& d : devices )
{
auto deviceFs = m_fsFactory->createDevice( d->uuid() );
auto fsDevicePresent = deviceFs != nullptr && deviceFs->isPresent();
if ( d->isPresent() != fsDevicePresent )
{
LOG_INFO( "Device ", d->uuid(), " changed presence state: ", d->isPresent(), " -> ", fsDevicePresent );
d->setPresent( fsDevicePresent );
}
else
{
LOG_INFO( "Device ", d->uuid(), " unchanged" );
}
}
return true;
}
void FsDiscoverer::checkFolder( fs::IDirectory& currentFolderFs, Folder& currentFolder, bool newFolder ) const
{
try
......
......@@ -51,7 +51,6 @@ private:
void checkFiles(fs::IDirectory& parentFolderFs, Folder& parentFolder ) const;
static bool hasDotNoMediaFile( const fs::IDirectory& directory );
bool addFolder( fs::IDirectory& folder, Folder* parentFolder ) const;
bool checkDevices();
private:
MediaLibrary* m_ml;
......
......@@ -92,7 +92,9 @@ TEST_F( DeviceEntity, Create )
ASSERT_NE( nullptr, d );
ASSERT_EQ( "dummy", d->uuid() );
ASSERT_TRUE( d->isRemovable() );
ASSERT_TRUE( d->isPresent() );
// Since there is no FsFactory to testify that this device is still plugged, the reload
// will mark it as "absent"
ASSERT_FALSE( d->isPresent() );
}
TEST_F( DeviceEntity, SetPresent )
......
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