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 ...@@ -275,6 +275,8 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& thu
return false; return false;
} }
} }
for ( auto& fsFactory : m_fsFactories )
refreshDevices( *fsFactory );
startDiscoverer(); startDiscoverer();
startParser(); startParser();
m_initialized = true; m_initialized = true;
...@@ -780,6 +782,30 @@ void MediaLibrary::setLogger( ILogger* logger ) ...@@ -780,6 +782,30 @@ void MediaLibrary::setLogger( ILogger* logger )
Log::SetLogger( 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 ) bool MediaLibrary::onDevicePlugged( const std::string& uuid, const std::string& mountpoint )
{ {
auto currentDevice = Device::fromUuid( this, uuid ); auto currentDevice = Device::fromUuid( this, uuid );
...@@ -789,7 +815,7 @@ bool MediaLibrary::onDevicePlugged( const std::string& uuid, const std::string& ...@@ -789,7 +815,7 @@ bool MediaLibrary::onDevicePlugged( const std::string& uuid, const std::string&
{ {
if ( fsFactory->isMrlSupported( "file://" ) ) if ( fsFactory->isMrlSupported( "file://" ) )
{ {
fsFactory->refreshDevices(); refreshDevices( *fsFactory );
break; break;
} }
} }
...@@ -803,7 +829,7 @@ void MediaLibrary::onDeviceUnplugged( const std::string& uuid ) ...@@ -803,7 +829,7 @@ void MediaLibrary::onDeviceUnplugged( const std::string& uuid )
{ {
if ( fsFactory->isMrlSupported( "file://" ) ) if ( fsFactory->isMrlSupported( "file://" ) )
{ {
fsFactory->refreshDevices(); refreshDevices( *fsFactory );
break; break;
} }
} }
......
...@@ -150,6 +150,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb ...@@ -150,6 +150,7 @@ class MediaLibrary : public IMediaLibrary, public IDeviceListerCb
bool updateDatabaseModel( unsigned int previousVersion ); bool updateDatabaseModel( unsigned int previousVersion );
bool createAllTables(); bool createAllTables();
void registerEntityHooks(); void registerEntityHooks();
void refreshDevices(factory::IFileSystem& fsFactory);
static bool validateSearchPattern( const std::string& pattern ); static bool validateSearchPattern( const std::string& pattern );
protected: protected:
......
...@@ -88,12 +88,6 @@ bool FsDiscoverer::discover( const std::string &entryPoint ) ...@@ -88,12 +88,6 @@ bool FsDiscoverer::discover( const std::string &entryPoint )
bool FsDiscoverer::reload() bool FsDiscoverer::reload()
{ {
LOG_INFO( "Reloading all folders" ); 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 ); auto rootFolders = Folder::fetchRootFolders( m_ml );
for ( const auto& f : rootFolders ) for ( const auto& f : rootFolders )
{ {
...@@ -114,12 +108,6 @@ bool FsDiscoverer::reload( const std::string& entryPoint ) ...@@ -114,12 +108,6 @@ bool FsDiscoverer::reload( const std::string& entryPoint )
if ( m_fsFactory->isMrlSupported( entryPoint ) == false ) if ( m_fsFactory->isMrlSupported( entryPoint ) == false )
return false; return false;
LOG_INFO( "Reloading folder ", entryPoint ); 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 ); auto folder = Folder::fromMrl( m_ml, entryPoint );
if ( folder == nullptr ) if ( folder == nullptr )
{ {
...@@ -136,28 +124,6 @@ bool FsDiscoverer::reload( const std::string& entryPoint ) ...@@ -136,28 +124,6 @@ bool FsDiscoverer::reload( const std::string& entryPoint )
return true; 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 void FsDiscoverer::checkFolder( fs::IDirectory& currentFolderFs, Folder& currentFolder, bool newFolder ) const
{ {
try try
......
...@@ -51,7 +51,6 @@ private: ...@@ -51,7 +51,6 @@ private:
void checkFiles(fs::IDirectory& parentFolderFs, Folder& parentFolder ) const; void checkFiles(fs::IDirectory& parentFolderFs, Folder& parentFolder ) const;
static bool hasDotNoMediaFile( const fs::IDirectory& directory ); static bool hasDotNoMediaFile( const fs::IDirectory& directory );
bool addFolder( fs::IDirectory& folder, Folder* parentFolder ) const; bool addFolder( fs::IDirectory& folder, Folder* parentFolder ) const;
bool checkDevices();
private: private:
MediaLibrary* m_ml; MediaLibrary* m_ml;
......
...@@ -92,7 +92,9 @@ TEST_F( DeviceEntity, Create ) ...@@ -92,7 +92,9 @@ TEST_F( DeviceEntity, Create )
ASSERT_NE( nullptr, d ); ASSERT_NE( nullptr, d );
ASSERT_EQ( "dummy", d->uuid() ); ASSERT_EQ( "dummy", d->uuid() );
ASSERT_TRUE( d->isRemovable() ); 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 ) 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