FileSystemFactory.cpp 4.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "factory/FileSystemFactory.h"
28 29
#include "medialibrary/filesystem/IDirectory.h"
#include "medialibrary/filesystem/IFile.h"
30
#include "logging/Logger.h"
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
31
#include "utils/Filename.h"
32 33
#include "utils/Directory.h"
#include "utils/Url.h"
34 35 36

#if defined(__linux__) || defined(__APPLE__)
# include "filesystem/unix/Directory.h"
37
# include "filesystem/unix/File.h"
38
# include "filesystem/unix/Device.h"
39 40 41
#elif defined(_WIN32)
# include "filesystem/win32/Directory.h"
# include "filesystem/win32/File.h"
42
# include "filesystem/win32/Device.h"
43 44 45 46
#else
# error No filesystem implementation for this architecture
#endif

47 48 49
#include "medialibrary/IDeviceLister.h"
#include "compat/Mutex.h"

50 51
#include <algorithm>

52 53 54
namespace medialibrary
{

55 56 57
namespace factory
{

58
FileSystemFactory::FileSystemFactory( DeviceListerPtr lister )
59
    : m_deviceLister( std::move( lister ) )
60 61 62
{
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
63
std::shared_ptr<fs::IDirectory> FileSystemFactory::createDirectory( const std::string& mrl )
64
{
65
    return std::make_shared<fs::Directory>( mrl, *this );
66 67
}

68 69
std::shared_ptr<fs::IDevice> FileSystemFactory::createDevice( const std::string& uuid )
{
70 71
    auto it = m_deviceCache.find( uuid );
    if ( it != end( m_deviceCache ) )
72 73 74 75
        return it->second;
    return nullptr;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
76
std::shared_ptr<fs::IDevice> FileSystemFactory::createDeviceFromMrl( const std::string& mrl )
77
{
78
    std::string canonicalMrl;
79 80
    try
    {
81 82
        auto canonicalPath = utils::fs::toAbsolute( utils::file::toLocalPath( mrl ) );
        canonicalMrl = utils::file::toMrl( canonicalPath );
83 84 85 86 87 88
    }
    catch ( const std::system_error& ex )
    {
        LOG_WARN( "Failed to canonicalize mountpoint ", mrl, ": ", ex.what() );
        return nullptr;
    }
89
    std::shared_ptr<fs::IDevice> res;
90
    std::string mountpoint;
91
    for ( const auto& p : m_deviceCache )
92
    {
93 94
        auto match = p.second->matchesMountpoint( canonicalMrl );
        if ( std::get<0>( match ) == true )
95
        {
96 97 98
            const auto& newMountpoint = std::get<1>( match );
            if ( res == nullptr || mountpoint.length() < newMountpoint.length() )
            {
99
                res = p.second;
100 101
                mountpoint = newMountpoint;
            }
102 103 104
        }
    }
    return res;
105 106
}

107
void FileSystemFactory::refreshDevices()
108
{
109 110 111
    LOG_INFO( "Refreshing devices from IDeviceLister" );
    auto devices = m_deviceLister->devices();

112
    for ( auto& devicePair : m_deviceCache )
113
    {
114 115 116 117
        auto it = std::find_if( begin( devices ), end( devices ),
                                [&devicePair]( decltype(devices)::value_type& deviceTuple ) {
                                    return std::get<0>( deviceTuple ) == devicePair.first;
        });
118
        if ( it != end( devices ) )
119
            devices.erase( it );
120
    }
121
    // And now insert all new devices, if any
122 123 124 125 126
    for ( const auto& d : devices )
    {
        const auto& uuid = std::get<0>( d );
        const auto& mountpoint = std::get<1>( d );
        const auto removable = std::get<2>( d );
127
        LOG_INFO( "Caching device ", uuid, " mounted on ", mountpoint, ". Removable: ", removable ? "true" : "false" );
128
        m_deviceCache.emplace( uuid, std::make_shared<fs::Device>( uuid, mountpoint, removable ) );
129
    }
130
    LOG_INFO( "Done refreshing devices from IDeviceLister" );
131 132
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
133
bool FileSystemFactory::isMrlSupported( const std::string& path ) const
134 135 136 137
{
    return path.compare( 0, 7, "file://" ) == 0;
}

138 139 140 141 142
bool FileSystemFactory::isNetworkFileSystem() const
{
    return false;
}

143 144 145 146 147 148
const std::string&FileSystemFactory::scheme() const
{
    static const std::string s = "file://";
    return s;
}

149
bool FileSystemFactory::start( fs::IFileSystemFactoryCb* )
150
{
151
    return true;
152 153 154 155 156 157
}

void FileSystemFactory::stop()
{
}

158
}
159

160
}