Folder.cpp 11.9 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 "File.h"
28
#include "Folder.h"
29
#include "Device.h"
30
#include "Media.h"
31

32
#include "database/SqliteTools.h"
33 34
#include "medialibrary/filesystem/IDirectory.h"
#include "medialibrary/filesystem/IDevice.h"
35
#include "medialibrary/filesystem/IFileSystemFactory.h"
36 37 38
#include "utils/Filename.h"

#include <unordered_map>
39

40 41 42
namespace medialibrary
{

43 44 45
const std::string Folder::Table::Name = "Folder";
const std::string Folder::Table::PrimaryKeyColumn = "id_folder";
int64_t Folder::* const Folder::Table::PrimaryKey = &Folder::m_id;
46

47 48
Folder::Folder( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
49 50 51 52 53
    , m_id( row.load<decltype(m_id)>( 0 ) )
    , m_path( row.load<decltype(m_path)>( 1 ) )
    , m_parent( row.load<decltype(m_parent)>( 2 ) )
    , m_isBlacklisted( row.load<decltype(m_isBlacklisted)>( 3 ) )
    , m_deviceId( row.load<decltype(m_deviceId)>( 4 ) )
54
    , m_isRemovable( row.load<decltype(m_isRemovable)>( 5 ) )
55 56 57
{
}

58
Folder::Folder(MediaLibraryPtr ml, const std::string& path, int64_t parent, int64_t deviceId, bool isRemovable )
59 60
    : m_ml( ml )
    , m_id( 0 )
61
    , m_path( path )
62
    , m_parent( parent )
63
    , m_isBlacklisted( false )
64
    , m_deviceId( deviceId )
65
    , m_isRemovable( isRemovable )
66 67 68
{
}

69
void Folder::createTable( sqlite::Connection* connection)
70
{
71 72 73 74 75
    const std::string reqs[] = {
        #include "database/tables/Folder_v14.sql"
    };
    for ( const auto& req : reqs )
        sqlite::Tools::executeRequest( connection, req );
76 77 78 79
}

void Folder::createTriggers( sqlite::Connection* connection )
{
80 81 82 83 84
    const std::string reqs[] = {
        #include "database/tables/Folder_triggers_v14.sql"
    };
    for ( const auto& req : reqs )
        sqlite::Tools::executeRequest( connection, req );
85 86
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87
std::shared_ptr<Folder> Folder::create( MediaLibraryPtr ml, const std::string& mrl,
88
                                        int64_t parentId, Device& device, fs::IDevice& deviceFs )
89
{
90 91
    std::string path;
    if ( device.isRemovable() == true )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
92
        path = utils::file::removePath( mrl, deviceFs.mountpoint() );
93
    else
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
94
        path = mrl;
95
    auto self = std::make_shared<Folder>( ml, path, parentId, device.id(), device.isRemovable() );
96
    static const std::string req = "INSERT INTO " + Folder::Table::Name +
97
            "(path, parent_id, device_id, is_removable) VALUES(?, ?, ?, ?)";
98
    if ( insert( ml, self, req, path, sqlite::ForeignKey( parentId ), device.id(), device.isRemovable() ) == false )
99
        return nullptr;
100 101 102 103 104
    if ( device.isRemovable() == true )
    {
        self->m_deviceMountpoint = deviceFs.mountpoint();
        self->m_fullPath = self->m_deviceMountpoint.get() + path;
    }
105 106 107
    return self;
}

108 109 110 111 112 113
void Folder::excludeEntryFolder( MediaLibraryPtr ml, int64_t folderId )
{
    std::string req = "INSERT INTO ExcludedEntryFolder(folder_id) VALUES(?)";
    sqlite::Tools::executeRequest( ml->getConn(), req, folderId );
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
114
bool Folder::blacklist( MediaLibraryPtr ml, const std::string& mrl )
115
{
116
    // Ensure we delete the existing folder if any & blacklist the folder in an "atomic" way
117 118
    return sqlite::Tools::withRetries( 3, [ml, &mrl]() {
        auto t = ml->getConn()->newTransaction();
119

120 121 122 123 124 125 126 127 128 129 130 131
        auto f = fromMrl( ml, mrl, BannedType::Any );
        if ( f != nullptr )
        {
            // No need to blacklist a folder twice
            if ( f->m_isBlacklisted == true )
                return true;
            // Let the foreign key destroy everything beneath this folder
            destroy( ml, f->id() );
        }
        auto fsFactory = ml->fsFactoryForMrl( mrl );
        if ( fsFactory == nullptr )
            return false;
132 133 134 135 136 137 138 139 140 141
        std::shared_ptr<fs::IDirectory> folderFs;
        try
        {
            folderFs = fsFactory->createDirectory( mrl );
        }
        catch ( std::system_error& ex )
        {
            LOG_ERROR( "Failed to instantiate a directory to ban folder: ", ex.what() );
            return false;
        }
142 143 144 145 146 147 148 149 150 151 152 153 154 155
        auto deviceFs = folderFs->device();
        if ( deviceFs == nullptr )
        {
            LOG_ERROR( "Can't find device associated with mrl ", mrl );
            return false;
        }
        auto device = Device::fromUuid( ml, deviceFs->uuid() );
        if ( device == nullptr )
            device = Device::create( ml, deviceFs->uuid(), utils::file::scheme( mrl ), deviceFs->isRemovable() );
        std::string path;
        if ( deviceFs->isRemovable() == true )
            path = utils::file::removePath( mrl, deviceFs->mountpoint() );
        else
            path = mrl;
156
        static const std::string req = "INSERT INTO " + Folder::Table::Name +
157 158 159 160 161
                "(path, parent_id, is_blacklisted, device_id, is_removable) VALUES(?, ?, ?, ?, ?)";
        auto res = sqlite::Tools::executeInsert( ml->getConn(), req, path, nullptr, true, device->id(), deviceFs->isRemovable() ) != 0;
        t->commit();
        return res;
    });
162 163
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
164
std::shared_ptr<Folder> Folder::fromMrl( MediaLibraryPtr ml, const std::string& mrl )
165
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
166
    return fromMrl( ml, mrl, BannedType::No );
167 168
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
169
std::shared_ptr<Folder> Folder::blacklistedFolder( MediaLibraryPtr ml, const std::string& mrl )
170
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
171
    return fromMrl( ml, mrl, BannedType::Yes );
172 173
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
174
std::shared_ptr<Folder> Folder::fromMrl( MediaLibraryPtr ml, const std::string& mrl, BannedType bannedType )
175
{
176 177
    if ( mrl.empty() == true )
        return nullptr;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
178
    auto fsFactory = ml->fsFactoryForMrl( mrl );
179 180
    if ( fsFactory == nullptr )
        return nullptr;
181 182 183 184 185 186 187 188 189 190 191
    std::shared_ptr<fs::IDirectory> folderFs;
    try
    {
        folderFs = fsFactory->createDirectory( mrl );
    }
    catch ( const std::system_error& ex )
    {
        LOG_ERROR( "Failed to instanciate a folder for mrl: ", mrl, ": ", ex.what() );
        return nullptr;
    }

192 193 194
    auto deviceFs = folderFs->device();
    if ( deviceFs == nullptr )
    {
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
195
        LOG_ERROR( "Failed to get device containing an existing folder: ", folderFs->mrl() );
196 197
        return nullptr;
    }
198 199
    if ( deviceFs->isRemovable() == false )
    {
200
        std::string req = "SELECT * FROM " + Folder::Table::Name + " WHERE path = ? AND is_removable = 0";
201
        if ( bannedType == BannedType::Any )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
202
            return fetch( ml, req, folderFs->mrl() );
203
        req += " AND is_blacklisted = ?";
204
        return fetch( ml, req, folderFs->mrl(), bannedType == BannedType::Yes ? true : false );
205 206
    }

207
    auto device = Device::fromUuid( ml, deviceFs->uuid() );
208 209 210
    // We are trying to find a folder. If we don't know the device it's on, we don't know the folder.
    if ( device == nullptr )
        return nullptr;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
211
    auto path = utils::file::removePath( folderFs->mrl(), deviceFs->mountpoint() );
212
    std::string req = "SELECT * FROM " + Folder::Table::Name + " WHERE path = ? AND device_id = ?";
213 214 215 216 217 218 219 220 221 222
    std::shared_ptr<Folder> folder;
    if ( bannedType == BannedType::Any )
    {
        folder = fetch( ml, req, path, device->id() );
    }
    else
    {
        req += " AND is_blacklisted = ?";
        folder = fetch( ml, req, path, device->id(), bannedType == BannedType::Yes ? true : false );
    }
223 224 225
    if ( folder == nullptr )
        return nullptr;
    folder->m_deviceMountpoint = deviceFs->mountpoint();
226
    folder->m_fullPath = folder->m_deviceMountpoint.get() + path;
227
    return folder;
228 229
}

230
int64_t Folder::id() const
231 232 233 234
{
    return m_id;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
235
const std::string& Folder::mrl() const
236
{
237 238 239
    if ( m_isRemovable == false )
        return m_path;

240 241 242 243
    auto lock = m_deviceMountpoint.lock();
    if ( m_deviceMountpoint.isCached() == true )
        return m_fullPath;

244 245 246 247 248 249 250 251 252 253 254
    // We can't compute the full path of a folder if it's removable and the device isn't present.
    // When there's no device, we don't know the mountpoint, therefor we don't know the full path
    // Calling isPresent will ensure we have the device representation cached locally
    if ( isPresent() == false )
    {
        assert( !"Device isn't present" );
        m_fullPath = "";
        return m_fullPath;
    }

    auto fsFactory = m_ml->fsFactoryForMrl( m_device.get()->scheme() );
255
    assert( fsFactory != nullptr );
256
    auto deviceFs = fsFactory->createDevice( m_device.get()->uuid() );
257 258 259 260 261 262 263 264
    // In case the device lister hasn't been updated accordingly, we might think
    // a device still is present while it's not.
    if( deviceFs == nullptr )
    {
        assert( !"File system Device representation couldn't be found" );
        m_fullPath = "";
        return m_fullPath;
    }
265 266
    m_deviceMountpoint = deviceFs->mountpoint();
    m_fullPath = m_deviceMountpoint.get() + m_path;
267
    return m_fullPath;
268 269
}

270 271 272 273 274 275 276 277 278
const std::string& Folder::rawMrl() const
{
    return m_path;
}

void Folder::setMrl( std::string mrl )
{
    if ( m_path == mrl )
        return;
279
    static const std::string req = "UPDATE " + Folder::Table::Name + " SET "
280 281 282 283 284 285 286 287 288 289
            "path = ? WHERE id_folder = ?";
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, mrl, m_id ) == false )
        return;
    // We shouldn't use this if any full path/mrl has been cached.
    // This is meant for migration only, so there is no need to have cached this
    // information so far.
    assert( m_isRemovable == false || m_fullPath.empty() == true );
    m_path = std::move( mrl );
}

290
std::vector<std::shared_ptr<File>> Folder::files()
291
{
292
    static const std::string req = "SELECT * FROM " + File::Table::Name +
293
        " WHERE folder_id = ?";
294
    return File::fetchAll<File>( m_ml, req, m_id );
295
}
296

297
std::vector<std::shared_ptr<Folder>> Folder::folders()
298
{
299 300 301
    static const std::string req = "SELECT * FROM " + Folder::Table::Name + " f "
            " LEFT JOIN " + Device::Table::Name + " d ON d.id_device = f.device_id"
            " WHERE parent_id = ? AND is_blacklisted = 0 AND d.is_present != 0";
302
    return DatabaseHelpers::fetchAll<Folder>( m_ml, req, m_id );
303 304
}

305
std::shared_ptr<Folder> Folder::parent()
306
{
307
    return fetch( m_ml, m_parent );
308
}
309

310
int64_t Folder::deviceId() const
311
{
312
    return m_deviceId;
313
}
314 315 316

bool Folder::isPresent() const
{
317 318 319
    auto deviceLock = m_device.lock();
    if ( m_device.isCached() == false )
        m_device = Device::fetch( m_ml, m_deviceId );
320 321
    // There must be a device containing the folder, since we never create a folder
    // without a device
322
    assert( m_device.get() != nullptr );
323 324 325
    // However, handle potential sporadic errors gracefully
    if( m_device.get() == nullptr )
        return false;
326
    return m_device.get()->isPresent();
327
}
328

329 330 331 332 333
bool Folder::isBanned() const
{
    return m_isBlacklisted;
}

334 335 336 337 338
bool Folder::isRootFolder() const
{
    return m_parent == 0;
}

339
std::vector<std::shared_ptr<Folder>> Folder::fetchRootFolders( MediaLibraryPtr ml )
340
{
341
    static const std::string req = "SELECT * FROM " + Folder::Table::Name + " f "
342
            " LEFT JOIN ExcludedEntryFolder"
343 344
            " ON f.id_folder = ExcludedEntryFolder.folder_id"
            " LEFT JOIN " + Device::Table::Name + " d ON d.id_device = f.device_id"
345
            " WHERE ExcludedEntryFolder.folder_id IS NULL AND"
346
            " parent_id IS NULL AND is_blacklisted = 0 AND d.is_present != 0";
347
    return DatabaseHelpers::fetchAll<Folder>( ml, req );
348
}
349 350

}