FsDiscoverer.cpp 5.82 KB
Newer Older
1 2
#include "FsDiscoverer.h"

3
#include <algorithm>
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
4 5
#include <queue>

6
#include "factory/FileSystem.h"
7 8
#include "File.h"
#include "Folder.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
9
#include "logging/Logger.h"
10

11 12 13
FsDiscoverer::FsDiscoverer( std::shared_ptr<factory::IFileSystem> fsFactory, IMediaLibrary* ml, DBConnection dbConn )
    : m_ml( ml )
    , m_dbConn( dbConn )
14 15 16 17 18 19 20
{
    if ( fsFactory != nullptr )
        m_fsFactory = fsFactory;
    else
        m_fsFactory.reset( new factory::FileSystemDefaultFactory );
}

21
bool FsDiscoverer::discover( const std::string &entryPoint )
22
{
23 24 25
    // Assume :// denotes a scheme that isn't a file path, and refuse to discover it.
    if ( entryPoint.find( "://" ) != std::string::npos )
        return false;
26 27

    {
28
        auto f = Folder::fetch( m_dbConn, entryPoint );
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        // If the folder exists, we assume it is up to date
        if ( f != nullptr )
            return true;
    }
    // Otherwise, create a directory, and check it for modifications
    std::unique_ptr<fs::IDirectory> fsDir;
    try
    {
        fsDir = m_fsFactory->createDirectory( entryPoint );
    }
    catch (std::exception& ex)
    {
        LOG_ERROR("Failed to create an IDirectory for ", entryPoint, ": ", ex.what());
        return false;
    }
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
44 45 46
    // Force <0> as lastModificationDate, so this folder is detected as outdated
    // by the modification checking code
    auto f = Folder::create( m_dbConn, fsDir->path(), 0, fsDir->isRemovable(), 0 );
47 48
    if ( f == nullptr )
        return false;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
49
    checkFiles( fsDir.get(), f );
50
    checkSubfolders( fsDir.get(), f );
51
    f->setLastModificationDate( fsDir->lastModificationDate() );
52 53 54
    return true;
}

55
void FsDiscoverer::reload()
56 57 58 59 60
{
    //FIXME: This should probably be in a sql transaction
    //FIXME: This shouldn't be done for "removable"/network files
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
            + " WHERE id_parent IS NULL";
61
    auto rootFolders = Folder::fetchAll( m_dbConn, req );
62 63 64 65 66
    for ( const auto f : rootFolders )
    {
        auto folder = m_fsFactory->createDirectory( f->path() );
        if ( folder->lastModificationDate() == f->lastModificationDate() )
            continue;
67
        checkSubfolders( folder.get(), f );
68 69 70 71
        f->setLastModificationDate( folder->lastModificationDate() );
    }
}

72
bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, FolderPtr parentFolder )
73 74 75 76 77 78 79 80
{
    // From here we can have:
    // - New subfolder(s)
    // - Deleted subfolder(s)
    // - New file(s)
    // - Deleted file(s)
    // - Changed file(s)
    // ... in this folder, or in all the sub folders.
81

82 83 84
    // Load the folders we already know of:
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
            + " WHERE id_parent = ?";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
85
    LOG_INFO( "Checking for modifications in ", folder->path() );
86
    auto subFoldersInDB = Folder::fetchAll( m_dbConn, req, parentFolder->id() );
87 88
    for ( const auto& subFolderPath : folder->dirs() )
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
89 90
        auto subFolder = m_fsFactory->createDirectory( subFolderPath );

91 92 93 94 95
        auto it = std::find_if( begin( subFoldersInDB ), end( subFoldersInDB ), [subFolderPath](const std::shared_ptr<IFolder>& f) {
            return f->path() == subFolderPath;
        });
        // We don't know this folder, it's a new one
        if ( it == end( subFoldersInDB ) )
96
        {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
97 98 99 100 101
            LOG_INFO( "New folder detected: ", subFolderPath );
            // Force a scan by setting lastModificationDate to 0
            auto f = Folder::create( m_dbConn, subFolder->path(), 0, subFolder->isRemovable(), parentFolder->id() );
            checkFiles( subFolder.get(), f );
            checkSubfolders( subFolder.get(), f );
102
            f->setLastModificationDate( subFolder->lastModificationDate() );
103
            continue;
104
        }
105
        if ( subFolder->lastModificationDate() == (*it)->lastModificationDate() )
106
        {
107 108 109 110
            // Remove all folders that still exist in FS. That way, the list of folders that
            // will still be in subFoldersInDB when we're done is the list of folders that have
            // been deleted from the FS
            subFoldersInDB.erase( it );
111 112
            continue;
        }
113
        // This folder was modified, let's recurse
114 115
        checkSubfolders( subFolder.get(), *it );
        checkFiles( subFolder.get(), *it );
116 117 118 119 120 121
        (*it)->setLastModificationDate( subFolder->lastModificationDate() );
        subFoldersInDB.erase( it );
    }
    // Now all folders we had in DB but haven't seen from the FS must have been deleted.
    for ( auto f : subFoldersInDB )
    {
122 123
        LOG_INFO( "Folder ", f->path(), " not found in FS, deleting it" );
        m_ml->deleteFolder( f );
124 125 126
    }
    return true;
}
127

128
void FsDiscoverer::checkFiles( fs::IDirectory* folder, FolderPtr parentFolder )
129
{
130
    LOG_INFO( "Checking file in ", folder->path() );
131 132
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name
            + " WHERE folder_id = ?";
133
    auto files = File::fetchAll( m_dbConn, req, parentFolder->id() );
134 135 136 137 138 139 140
    for ( const auto& filePath : folder->files() )
    {        
        auto it = std::find_if( begin( files ), end( files ), [filePath](const std::shared_ptr<IFile>& f) {
            return f->mrl() == filePath;
        });
        if ( it == end( files ) )
        {
141
            m_ml->addFile( filePath, parentFolder );
142
            continue;
143 144 145
        }
        auto file = m_fsFactory->createFile( filePath );
        if ( file->lastModificationDate() == (*it)->lastModificationDate() )
146
        {
147 148 149
            // Unchanged file
            files.erase( it );
            continue;
150
        }
151
        LOG_INFO( "Forcing file refresh ", filePath );
152 153
        m_ml->deleteFile( filePath );
        m_ml->addFile( filePath, parentFolder );
154 155 156 157
        files.erase( it );
    }
    for ( auto file : files )
    {
158
        LOG_INFO( "File ", file->mrl(), " not found on filesystem, deleting it" );
159
        m_ml->deleteFile( file );
160 161
    }
}