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

FsDiscoverer: Handle .nomedia files

parent f3ba530a
......@@ -63,6 +63,8 @@ bool FsDiscoverer::discover( const std::string &entryPoint )
auto blist = blacklist();
if ( isBlacklisted( *fsDir, blist ) == true )
return false;
if ( hasDotNoMediaFile( *fsDir ) )
return false;
return addFolder( *fsDir, nullptr, blist );
}
......@@ -81,8 +83,10 @@ void FsDiscoverer::reload()
m_ml->deleteFolder( *f );
continue;
}
checkSubfolders( *folder, *f, blist );
checkFiles( *folder, *f );
if ( checkSubfolders( *folder, *f, blist ) == true )
{
checkFiles( *folder, *f );
}
}
}
......@@ -108,6 +112,14 @@ void FsDiscoverer::checkDevices()
bool FsDiscoverer::checkSubfolders( fs::IDirectory& parentFolderFs, Folder& parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const
{
// We already know of this folder, though it may now contain a .nomedia file.
// In this case, simply delete the folder.
if ( hasDotNoMediaFile( parentFolderFs ) )
{
LOG_INFO( "Deleting folder ", parentFolderFs.path(), " due to a .nomedia file" );
m_ml->deleteFolder( parentFolder );
return false;
}
// Load the folders we already know of:
LOG_INFO( "Checking for modifications in ", parentFolderFs.path() );
auto subFoldersInDB = Folder::fetchAll( m_dbConn, parentFolder.id() );
......@@ -129,6 +141,11 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory& parentFolderFs, Folder& pare
LOG_INFO( "Ignoring blacklisted folder: ", subFolderPath );
continue;
}
if ( hasDotNoMediaFile( *subFolder ) )
{
LOG_INFO( "Ignoring folder with a .nomedia file" );
continue;
}
LOG_INFO( "New folder detected: ", subFolderPath );
addFolder( *subFolder, &parentFolder, blacklist );
continue;
......@@ -137,8 +154,10 @@ bool FsDiscoverer::checkSubfolders( fs::IDirectory& parentFolderFs, Folder& pare
// In any case, check for modifications, as a change related to a mountpoint might
// not update the folder modification date.
// Also, relying on the modification date probably isn't portable
checkSubfolders( *subFolder, *folderInDb, blacklist );
checkFiles( *subFolder, *folderInDb );
if ( checkSubfolders( *subFolder, *folderInDb, blacklist ) == true )
{
checkFiles( *subFolder, *folderInDb );
}
subFoldersInDB.erase( it );
}
// Now all folders we had in DB but haven't seen from the FS must have been deleted.
......@@ -222,6 +241,15 @@ bool FsDiscoverer::isBlacklisted( const fs::IDirectory& directory, const std::ve
}) != end( blacklist );
}
bool FsDiscoverer::hasDotNoMediaFile( const fs::IDirectory& directory )
{
const auto& files = directory.files();
return std::find_if( begin( files ), end( files ), []( const std::string& filePath ){
constexpr unsigned int endLength = strlen( "/.nomedia" );
return filePath.compare( filePath.length() - endLength, endLength, "/.nomedia" ) == 0;
}) != end( files );
}
bool FsDiscoverer::addFolder( fs::IDirectory& folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder>>& blacklist ) const
{
auto deviceFs = folder.device();
......
......@@ -39,10 +39,15 @@ public:
virtual void reload() override;
private:
///
/// \brief checkSubfolders
/// \return true if files in this folder needs to be listed, false otherwise
///
bool checkSubfolders(fs::IDirectory& folder, Folder& parentFolder, const std::vector<std::shared_ptr<Folder>> blacklist ) const;
void checkFiles(fs::IDirectory& parentFolderFs, Folder& parentFolder ) const;
std::vector<std::shared_ptr<Folder>> blacklist() const;
bool isBlacklisted( const fs::IDirectory& directory, const std::vector<std::shared_ptr<Folder>>& blacklist ) const;
static bool hasDotNoMediaFile( const fs::IDirectory& directory );
bool addFolder(fs::IDirectory& folder, Folder* parentFolder, const std::vector<std::shared_ptr<Folder> >& blacklist ) const;
void checkDevices();
......
......@@ -297,3 +297,48 @@ TEST_F( Folders, BlacklistAfterDiscovery )
auto f2 = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( nullptr, f2 );
}
TEST_F( FoldersNoDiscover, NoMediaBeforeDiscovery )
{
auto newFolder = mock::FileSystemFactory::Root + "newfolder/";
fsMock->addFolder( newFolder );
fsMock->addFile( newFolder + "newfile.avi" );
fsMock->addFile( newFolder + ".nomedia" );
cbMock->prepareForWait( 1 );
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
ASSERT_TRUE( discovered );
auto files = ml->files();
// We add 3 files before, and the new one shouldn't be account for since there is a .nomedia file
ASSERT_EQ( 3u, files.size() );
}
TEST_F( Folders, InsertNoMedia )
{
auto files = ml->files();
ASSERT_EQ( 3u, files.size() );
fsMock->addFile( mock::FileSystemFactory::SubFolder + ".nomedia" );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 2u, files.size() );
}
TEST_F( Folders, InsertNoMediaInRoot )
{
fsMock->addFile( mock::FileSystemFactory::Root + ".nomedia" );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->waitForReload();
ASSERT_TRUE( reloaded );
auto files = ml->files();
ASSERT_EQ( 0u, files.size() );
}
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