Folder.cpp 11.6 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
    bool dummy;
51 52 53
    row >> m_id
        >> m_path
        >> m_parent
54
        >> m_isBlacklisted
55
        >> m_deviceId
56
        >> dummy
57
        >> m_isRemovable;
58 59
}

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

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

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

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

110 111 112 113 114 115
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
116
bool Folder::blacklist( MediaLibraryPtr ml, const std::string& mrl )
117
{
118
    // Ensure we delete the existing folder if any & blacklist the folder in an "atomic" way
119 120
    return sqlite::Tools::withRetries( 3, [ml, &mrl]() {
        auto t = ml->getConn()->newTransaction();
121

122 123 124 125 126 127 128 129 130 131 132 133
        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;
134 135 136 137 138 139 140 141 142 143
        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;
        }
144 145 146 147 148 149 150 151 152 153 154 155 156 157
        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;
158
        static const std::string req = "INSERT INTO " + Folder::Table::Name +
159 160 161 162 163
                "(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;
    });
164 165
}

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

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
176
std::shared_ptr<Folder> Folder::fromMrl( MediaLibraryPtr ml, const std::string& mrl, BannedType bannedType )
177
{
178 179
    if ( mrl.empty() == true )
        return nullptr;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
180
    auto fsFactory = ml->fsFactoryForMrl( mrl );
181 182
    if ( fsFactory == nullptr )
        return nullptr;
183 184 185 186 187 188 189 190 191 192 193
    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;
    }

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

209
    auto device = Device::fromUuid( ml, deviceFs->uuid() );
210 211 212
    // 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
213
    auto path = utils::file::removePath( folderFs->mrl(), deviceFs->mountpoint() );
214
    std::string req = "SELECT * FROM " + Folder::Table::Name + " WHERE path = ? AND device_id = ?";
215 216 217 218 219 220 221 222 223 224
    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 );
    }
225 226 227
    if ( folder == nullptr )
        return nullptr;
    folder->m_deviceMountpoint = deviceFs->mountpoint();
228
    folder->m_fullPath = folder->m_deviceMountpoint.get() + path;
229
    return folder;
230 231
}

232
int64_t Folder::id() const
233 234 235 236
{
    return m_id;
}

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

242 243 244 245
    auto lock = m_deviceMountpoint.lock();
    if ( m_deviceMountpoint.isCached() == true )
        return m_fullPath;

246 247 248 249 250 251 252 253 254 255 256
    // 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() );
257
    assert( fsFactory != nullptr );
258
    auto deviceFs = fsFactory->createDevice( m_device.get()->uuid() );
259 260 261 262 263 264 265 266
    // 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;
    }
267 268
    m_deviceMountpoint = deviceFs->mountpoint();
    m_fullPath = m_deviceMountpoint.get() + m_path;
269
    return m_fullPath;
270 271
}

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

void Folder::setMrl( std::string mrl )
{
    if ( m_path == mrl )
        return;
281
    static const std::string req = "UPDATE " + Folder::Table::Name + " SET "
282 283 284 285 286 287 288 289 290 291
            "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 );
}

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

299
std::vector<std::shared_ptr<Folder>> Folder::folders()
300
{
301
    static const std::string req = "SELECT * FROM " + Folder::Table::Name
302
            + " WHERE parent_id = ? AND is_blacklisted = 0 AND is_present != 0";
303
    return DatabaseHelpers::fetchAll<Folder>( m_ml, req, m_id );
304 305
}

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

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

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

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

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

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

}