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

FsDiscoverer: Simplify device changes detection

This also allows us to reintroduce the last modification date changes
parent 81496b37
......@@ -74,7 +74,7 @@ void Device::setPresent(bool value)
m_isPresent = value;
}
std::shared_ptr<Device> Device::create(DBConnection dbConnection, const std::string& uuid, bool isRemovable )
std::shared_ptr<Device> Device::create( DBConnection dbConnection, const std::string& uuid, bool isRemovable )
{
static const std::string req = "INSERT INTO " + policy::DeviceTable::Name
+ "(uuid, is_removable, is_present) VALUES(?, ?, ?)";
......
......@@ -155,3 +155,8 @@ unsigned int Folder::deviceId() const
{
return m_deviceId;
}
bool Folder::isPresent() const
{
return m_isPresent;
}
......@@ -66,6 +66,7 @@ public:
virtual unsigned int lastModificationDate() override;
bool setLastModificationDate(unsigned int lastModificationDate);
unsigned int deviceId() const;
bool isPresent() const;
private:
DBConnection m_dbConection;
......
......@@ -74,6 +74,8 @@ bool FsDiscoverer::discover( const std::string &entryPoint )
void FsDiscoverer::reload()
{
// Start by checking if previously known devices have been plugged/unplugged
checkDevices();
//FIXME: This should probably be in a sql transaction
static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
+ " WHERE id_parent IS NULL";
......@@ -82,21 +84,33 @@ void FsDiscoverer::reload()
for ( const auto& f : rootFolders )
{
auto folder = m_fsFactory->createDirectory( f->path() );
// Don't assume a folder that is a mountpoint will get its modification date updated on all platforms
// So first, we check if the device containing this folder is available:
auto deviceFs = folder->device();
if ( deviceFs == nullptr )
{
auto device = Device::fetch( m_dbConn, f->deviceId() );
device->setPresent( false );
if ( folder->lastModificationDate() == f->lastModificationDate() )
continue;
}
checkSubfolders( folder.get(), f.get(), blist );
checkFiles( folder.get(), f.get() );
f->setLastModificationDate( folder->lastModificationDate() );
}
}
void FsDiscoverer::checkDevices()
{
auto devices = Device::fetchAll( m_dbConn );
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" );
}
}
}
bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const
{
// From here we can have:
......@@ -133,19 +147,14 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, Folder* parentFolder
continue;
}
auto folderInDb = *it;
auto deviceFs = subFolder->device();
// If the device supposed to contain this folder is not present anymore, flag it as removed
if ( deviceFs == nullptr || deviceFs->isPresent() == false )
// If the folder isn't present, don't check it for modifications
if ( folderInDb->isPresent() == false )
{
subFoldersInDB.erase( it );
continue;
}
if ( subFolder->lastModificationDate() == folderInDb->lastModificationDate() )
{
auto device = Device::fetch( m_dbConn, folderInDb->deviceId() );
if ( device == nullptr )
{
LOG_ERROR( "Failed to fetch device containing folder ", folderInDb->path() );
continue;
}
LOG_INFO( "Device containing ", folderInDb->path(), " is not present anymore" );
device->setPresent( false );
// Don't let this folder be deleted after the main loop.
subFoldersInDB.erase( it );
continue;
}
......@@ -237,3 +246,4 @@ bool FsDiscoverer::addFolder( fs::IDirectory* folder, Folder* parentFolder, cons
f->setLastModificationDate( folder->lastModificationDate() );
return true;
}
......@@ -44,6 +44,7 @@ private:
std::vector<std::shared_ptr<Folder>> blacklist() const;
bool isBlacklisted( const std::string& path, const std::vector<std::shared_ptr<Folder>>& blacklist ) const;
bool addFolder(fs::IDirectory* folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder> >& blacklist ) const;
void checkDevices();
private:
MediaLibrary* m_ml;
......
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