Folder.cpp 8.44 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
#include "Folder.h"
24
#include "Device.h"
25
#include "Media.h"
26

27
#include "database/SqliteTools.h"
28
#include "filesystem/IDirectory.h"
29
30
31
32
#include "filesystem/IDevice.h"
#include "utils/Filename.h"

#include <unordered_map>
33
34
35
36

namespace policy
{
    const std::string FolderTable::Name = "Folder";
37
    const std::string FolderTable::PrimaryKeyColumn = "id_folder";
38
39
40
    unsigned int Folder::* const FolderTable::PrimaryKey = &Folder::m_id;
}

41
42
43
std::shared_ptr<factory::IFileSystem> Folder::FsFactory;

Folder::Folder( DBConnection dbConnection, sqlite::Row& row )
44
45
    : m_dbConection( dbConnection )
{
46
47
48
    row >> m_id
        >> m_path
        >> m_parent
49
        >> m_isBlacklisted
50
        >> m_deviceId
51
52
        >> m_isPresent
        >> m_isRemovable;
53
54
}

55
Folder::Folder( const std::string& path, unsigned int parent, unsigned int deviceId, bool isRemovable )
56
57
    : m_id( 0 )
    , m_path( path )
58
    , m_parent( parent )
59
    , m_isBlacklisted( false )
60
    , m_deviceId( deviceId )
61
    , m_isPresent( true )
62
    , m_isRemovable( isRemovable )
63
64
65
66
67
{
}

bool Folder::createTable(DBConnection connection)
{
68
69
70
    std::string req = "CREATE TABLE IF NOT EXISTS " + policy::FolderTable::Name +
            "("
            "id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
71
            "path TEXT,"
72
            "parent_id UNSIGNED INTEGER,"
73
            "is_blacklisted INTEGER,"
74
            "device_id UNSIGNED INTEGER,"
75
            "is_present BOOLEAN NOT NULL DEFAULT 1,"
76
            "is_removable BOOLEAN NOT NULL,"
77
            "FOREIGN KEY (parent_id) REFERENCES " + policy::FolderTable::Name +
78
79
            "(id_folder) ON DELETE CASCADE,"
            "FOREIGN KEY (device_id) REFERENCES " + policy::DeviceTable::Name +
80
81
            "(id_device) ON DELETE CASCADE,"
            "UNIQUE(path, device_id) ON CONFLICT FAIL"
82
            ")";
83
84
85
86
87
88
89
    std::string triggerReq = "CREATE TRIGGER IF NOT EXISTS is_device_present AFTER UPDATE OF is_present ON "
            + policy::DeviceTable::Name +
            " BEGIN"
            " UPDATE " + policy::FolderTable::Name + " SET is_present = new.is_present WHERE device_id = new.id_device;"
            " END";
    return sqlite::Tools::executeRequest( connection, req ) &&
            sqlite::Tools::executeRequest( connection, triggerReq );
90
91
}

92
std::shared_ptr<Folder> Folder::create( DBConnection connection, const std::string& fullPath, unsigned int parentId, Device& device, fs::IDevice& deviceFs )
93
{
94
95
96
97
98
99
    std::string path;
    if ( device.isRemovable() == true )
        path = utils::file::removePath( fullPath, deviceFs.mountpoint() );
    else
        path = fullPath;
    auto self = std::make_shared<Folder>( path, parentId, device.id(), device.isRemovable() );
100
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
101
            "(path, parent_id, device_id, is_removable) VALUES(?, ?, ?, ?)";
102
    if ( insert( connection, self, req, path, sqlite::ForeignKey( parentId ), device.id(), device.isRemovable() ) == false )
103
        return nullptr;
104
    self->m_dbConection = connection;
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
bool Folder::blacklist( DBConnection connection, const std::string& fullPath )
114
{
115
116
117
118
119
120
    auto f = fromPath( connection, fullPath );
    if ( f != nullptr )
    {
        // Let the foreign key destroy everything beneath this folder
        destroy( connection, f->id() );
    }
121
    auto folderFs = FsFactory->createDirectory( fullPath );
122
123
    if ( folderFs == nullptr )
        return false;
124
125
126
127
    auto deviceFs = folderFs->device();
    auto device = Device::fromUuid( connection, deviceFs->uuid() );
    if ( device == nullptr )
        device = Device::create( connection, deviceFs->uuid(), deviceFs->isRemovable() );
128
129
130
131
132
    std::string path;
    if ( deviceFs->isRemovable() == true )
        path = utils::file::removePath( fullPath, deviceFs->mountpoint() );
    else
        path = fullPath;
133
    static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
134
            "(path, parent_id, is_blacklisted, device_id, is_removable) VALUES(?, ?, ?, ?, ?)";
135
    return sqlite::Tools::insert( connection, req, path, nullptr, true, device->id(), deviceFs->isRemovable() ) != 0;
136
137
}

138
139
140
141
142
void Folder::setFileSystemFactory( std::shared_ptr<factory::IFileSystem> fsFactory )
{
    FsFactory = fsFactory;
}

143
std::shared_ptr<Folder> Folder::fromPath( DBConnection conn, const std::string& fullPath )
144
{
145
146
147
148
149
150
151
152
153
    auto folderFs = FsFactory->createDirectory( fullPath );
    if ( folderFs == nullptr )
        return nullptr;
    auto deviceFs = folderFs->device();
    if ( deviceFs == nullptr )
    {
        LOG_ERROR( "Failed to get device containing an existing folder: ", fullPath );
        return nullptr;
    }
154
155
156
157
158
159
160
161
162
    if ( deviceFs->isRemovable() == false )
    {
        const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND is_removable = 0"
                " AND is_blacklisted IS NULL";
        return fetch( conn, req, fullPath );
    }
    const std::string req = "SELECT * FROM " + policy::FolderTable::Name + " WHERE path = ? AND device_id = ? "
            "AND is_blacklisted IS NULL";

163
164
165
166
167
168
169
170
171
    auto device = Device::fromUuid( conn, deviceFs->uuid() );
    // 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;
    auto path = utils::file::removePath( fullPath, deviceFs->mountpoint() );
    auto folder = fetch( conn, req, path, device->id() );
    if ( folder == nullptr )
        return nullptr;
    folder->m_deviceMountpoint = deviceFs->mountpoint();
172
    folder->m_fullPath = folder->m_deviceMountpoint.get() + path;
173
    return folder;
174
175
}

176
177
178
179
180
unsigned int Folder::id() const
{
    return m_id;
}

181
const std::string& Folder::path() const
182
{
183
184
185
    if ( m_isRemovable == false )
        return m_path;

186
187
188
189
190
191
192
193
    auto lock = m_deviceMountpoint.lock();
    if ( m_deviceMountpoint.isCached() == true )
        return m_fullPath;

    auto device = Device::fetch( m_dbConection, m_deviceId );
    auto deviceFs = FsFactory->createDevice( device->uuid() );
    m_deviceMountpoint = deviceFs->mountpoint();
    m_fullPath = m_deviceMountpoint.get() + m_path;
194
    return m_fullPath;
195
196
}

197
std::vector<MediaPtr> Folder::files()
198
{
199
    static const std::string req = "SELECT * FROM " + policy::MediaTable::Name +
200
        " WHERE folder_id = ?";
201
    return Media::fetchAll<IMedia>( m_dbConection, req, m_id );
202
}
203

204
std::vector<std::shared_ptr<Folder>> Folder::folders()
205
{
206
    return fetchAll( m_dbConection, m_id );
207
208
}

209
std::shared_ptr<Folder> Folder::parent()
210
{
211
    return fetch( m_dbConection, m_parent );
212
}
213

214
unsigned int Folder::deviceId() const
215
{
216
    return m_deviceId;
217
}
218
219
220
221
222

bool Folder::isPresent() const
{
    return m_isPresent;
}
223
224
225
226
227
228

std::vector<std::shared_ptr<Folder>> Folder::fetchAll( DBConnection dbConn, unsigned int parentFolderId )
{
    if ( parentFolderId == 0 )
    {
        static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
229
                + " WHERE parent_id IS NULL AND is_blacklisted is NULL AND is_present = 1";
230
        return DatabaseHelpers::fetchAll<Folder>( dbConn, req );
231
232
233
234
    }
    else
    {
        static const std::string req = "SELECT * FROM " + policy::FolderTable::Name
235
                + " WHERE parent_id = ? AND is_blacklisted is NULL AND is_present = 1";
236
        return DatabaseHelpers::fetchAll<Folder>( dbConn, req, parentFolderId );
237
238
    }
}