Folder.cpp 11.7 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
namespace policy
{
    const std::string FolderTable::Name = "Folder";
46
    const std::string FolderTable::PrimaryKeyColumn = "id_folder";
47
    int64_t Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
48 49
}

50 51
Folder::Folder( MediaLibraryPtr ml, sqlite::Row& row )
    : m_ml( ml )
52
{
53
    bool dummy;
54 55 56
    row >> m_id
        >> m_path
        >> m_parent
57
        >> m_isBlacklisted
58
        >> m_deviceId
59
        >> dummy
60
        >> m_isRemovable;
61 62
}

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

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

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

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

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

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

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

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

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

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

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

235
int64_t Folder::id() const
236 237 238 239
{
    return m_id;
}

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

245 246 247 248
    auto lock = m_deviceMountpoint.lock();
    if ( m_deviceMountpoint.isCached() == true )
        return m_fullPath;

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

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
const std::string& Folder::rawMrl() const
{
    return m_path;
}

void Folder::setMrl( std::string mrl )
{
    if ( m_path == mrl )
        return;
    static const std::string req = "UPDATE " + policy::FolderTable::Name + " SET "
            "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 );
}

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

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

309
std::shared_ptr<Folder> Folder::parent()
310
{
311
    return fetch( m_ml, m_parent );
312
}
313

314
int64_t Folder::deviceId() const
315
{
316
    return m_deviceId;
317
}
318 319 320

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

333 334 335 336 337
bool Folder::isBanned() const
{
    return m_isBlacklisted;
}

338 339 340 341 342
bool Folder::isRootFolder() const
{
    return m_parent == 0;
}

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

}