FsDiscoverer.cpp 5.57 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 52 53
    return true;
}

54
void FsDiscoverer::reload()
55 56 57 58 59
{
    //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";
60
    auto rootFolders = sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConn, req );
61 62 63 64 65
    for ( const auto f : rootFolders )
    {
        auto folder = m_fsFactory->createDirectory( f->path() );
        if ( folder->lastModificationDate() == f->lastModificationDate() )
            continue;
66
        checkSubfolders( folder.get(), f );
67 68 69 70
        f->setLastModificationDate( folder->lastModificationDate() );
    }
}

71
bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, FolderPtr parentFolder )
72 73 74 75 76 77 78 79
{
    // 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.
80

81 82 83
    // 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
84
    LOG_INFO( "Checking for modifications in ", folder->path() );
85
    auto subFoldersInDB = sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConn, req, parentFolder->id() );
86 87
    for ( const auto& subFolderPath : folder->dirs() )
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
88 89
        auto subFolder = m_fsFactory->createDirectory( subFolderPath );

90 91 92 93 94
        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 ) )
95
        {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
96 97 98 99 100
            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 );
101
            continue;
102
        }
103
        if ( subFolder->lastModificationDate() == (*it)->lastModificationDate() )
104
        {
105 106 107 108
            // 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 );
109 110
            continue;
        }
111
        // This folder was modified, let's recurse
112 113
        checkSubfolders( subFolder.get(), *it );
        checkFiles( subFolder.get(), *it );
114 115 116 117 118 119
        (*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 )
    {
120 121
        LOG_INFO( "Folder ", f->path(), " not found in FS, deleting it" );
        m_ml->deleteFolder( f );
122 123 124
    }
    return true;
}
125

126
void FsDiscoverer::checkFiles( fs::IDirectory* folder, FolderPtr parentFolder )
127 128 129
{
    static const std::string req = "SELECT * FROM " + policy::FileTable::Name
            + " WHERE folder_id = ?";
130
    auto files = sqlite::Tools::fetchAll<File, IFile>( m_dbConn, req, parentFolder->id() );
131 132 133 134 135 136 137
    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 ) )
        {
138
            m_ml->addFile( filePath, parentFolder );
139
            continue;
140 141 142
        }
        auto file = m_fsFactory->createFile( filePath );
        if ( file->lastModificationDate() == (*it)->lastModificationDate() )
143
        {
144 145 146
            // Unchanged file
            files.erase( it );
            continue;
147
        }
148 149
        m_ml->deleteFile( filePath );
        m_ml->addFile( filePath, parentFolder );
150 151 152 153
        files.erase( it );
    }
    for ( auto file : files )
    {
154
        m_ml->deleteFile( file );
155 156
    }
}