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

3
#include <algorithm>
4
#include "factory/FileSystem.h"
5 6
#include "File.h"
#include "Folder.h"
7
#include <queue>
8
#include <iostream>
9

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

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

    {
27
        auto f = Folder::fetch( m_dbConn, entryPoint );
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        // 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;
    }
43
    auto f = Folder::create( m_dbConn, fsDir.get(), 0 );
44 45
    if ( f == nullptr )
        return false;
46
    checkSubfolders( fsDir.get(), f );
47 48 49
    return true;
}

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

67
bool FsDiscoverer::checkSubfolders( fs::IDirectory* folder, FolderPtr parentFolder )
68 69 70 71 72 73 74 75
{
    // 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.
76

77 78 79
    // Load the folders we already know of:
    static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
            + " WHERE id_parent = ?";
80
    auto subFoldersInDB = sqlite::Tools::fetchAll<Folder, IFolder>( m_dbConn, req, parentFolder->id() );
81 82 83 84 85 86 87
    for ( const auto& subFolderPath : folder->dirs() )
    {
        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 ) )
88
        {
89 90 91 92
            //FIXME: In order to add the new folder, we need to use the same discoverer.
            // This probably means we need to store which discoverer was used to add which file
            // and store discoverers as a map instead of a vector
            continue;
93
        }
94 95
        auto subFolder = m_fsFactory->createDirectory( subFolderPath );
        if ( subFolder->lastModificationDate() == (*it)->lastModificationDate() )
96
        {
97 98 99 100
            // 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 );
101 102
            continue;
        }
103
        // This folder was modified, let's recurse
104 105
        checkSubfolders( subFolder.get(), *it );
        checkFiles( subFolder.get(), *it );
106 107 108 109 110 111
        (*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 )
    {
112 113
        LOG_INFO( "Folder ", f->path(), " not found in FS, deleting it" );
        m_ml->deleteFolder( f );
114 115 116
    }
    return true;
}
117

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