Commit e5b274e8 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

s/Mountpoint/Device

parent a8f82d12
......@@ -29,7 +29,7 @@ namespace fs
{
class IDirectory;
class IFile;
class IMountpoint;
class IDevice;
}
namespace factory
......
......@@ -26,10 +26,10 @@
namespace fs
{
class IMountpoint
class IDevice
{
public:
virtual ~IMountpoint() = default;
virtual ~IDevice() = default;
virtual const std::string& uuid() const = 0;
virtual bool isPresent() const = 0;
virtual bool isRemovable() const = 0;
......
......@@ -29,7 +29,7 @@
namespace fs
{
class IFile;
class IMountpoint;
class IDevice;
class IDirectory
{
......@@ -42,7 +42,7 @@ namespace fs
/// Returns a list of absolute path to this folder subdirectories
virtual const std::vector<std::string>& dirs() = 0;
virtual unsigned int lastModificationDate() const = 0;
virtual std::shared_ptr<IMountpoint> mountpoint() const = 0;
virtual std::shared_ptr<IDevice> device() const = 0;
virtual bool isRemovable() const = 0;
};
}
......@@ -66,13 +66,13 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
Parser.cpp
Artist.cpp
Settings.cpp
Mountpoint.cpp
Device.cpp
factory/FileSystem.cpp
filesystem/common/CommonFile.cpp
filesystem/${ARCH_FOLDER}/Directory.cpp
filesystem/${ARCH_FOLDER}/File.cpp
filesystem/${ARCH_FOLDER}/Mountpoint.cpp
filesystem/${ARCH_FOLDER}/Device.cpp
discoverer/FsDiscoverer.cpp
discoverer/DiscovererWorker.cpp
......
......@@ -20,13 +20,13 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "Mountpoint.h"
#include "Device.h"
const std::string policy::MountpointTable::Name = "Mountpoint";
const std::string policy::MountpointTable::PrimaryKeyColumn = "id_mountpoint";
unsigned int Mountpoint::* const policy::MountpointTable::PrimaryKey = &Mountpoint::m_id;
const std::string policy::DeviceTable::Name = "Device";
const std::string policy::DeviceTable::PrimaryKeyColumn = "id_device";
unsigned int Device::* const policy::DeviceTable::PrimaryKey = &Device::m_id;
Mountpoint::Mountpoint( DBConnection dbConnection, sqlite::Row& row )
Device::Device( DBConnection dbConnection, sqlite::Row& row )
: m_dbConn( dbConnection )
{
row >> m_id
......@@ -37,58 +37,58 @@ Mountpoint::Mountpoint( DBConnection dbConnection, sqlite::Row& row )
//only be here for sqlite triggering purposes
}
Mountpoint::Mountpoint( const std::string& uuid, bool isRemovable )
Device::Device( const std::string& uuid, bool isRemovable )
: m_uuid( uuid )
, m_isRemovable( isRemovable )
// Assume we can't add an unmounted/absent mountpoint
// Assume we can't add an absent device
, m_isPresent( true )
{
}
unsigned int Mountpoint::id() const
unsigned int Device::id() const
{
return m_id;
}
const std::string&Mountpoint::uuid() const
const std::string&Device::uuid() const
{
return m_uuid;
}
bool Mountpoint::isRemovable() const
bool Device::isRemovable() const
{
return m_isRemovable;
}
bool Mountpoint::isPresent() const
bool Device::isPresent() const
{
return m_isPresent;
}
void Mountpoint::setPresent(bool value)
void Device::setPresent(bool value)
{
static const std::string req = "UPDATE " + policy::MountpointTable::Name +
static const std::string req = "UPDATE " + policy::DeviceTable::Name +
" SET is_present = ?";
if ( sqlite::Tools::executeUpdate( m_dbConn, req, value ) == false )
return;
m_isPresent = value;
}
std::shared_ptr<Mountpoint> Mountpoint::create(DBConnection dbConnection, const std::string& uuid, bool isRemovable )
std::shared_ptr<Device> Device::create(DBConnection dbConnection, const std::string& uuid, bool isRemovable )
{
static const std::string req = "INSERT INTO " + policy::MountpointTable::Name
static const std::string req = "INSERT INTO " + policy::DeviceTable::Name
+ "(uuid, is_removable, is_present) VALUES(?, ?, ?)";
auto self = std::make_shared<Mountpoint>( uuid, isRemovable );
auto self = std::make_shared<Device>( uuid, isRemovable );
if ( insert( dbConnection, self, req, uuid, isRemovable, self->isPresent() ) == false )
return nullptr;
self->m_dbConn = dbConnection;
return self;
}
bool Mountpoint::createTable(DBConnection connection)
bool Device::createTable(DBConnection connection)
{
std::string req = "CREATE TABLE IF NOT EXISTS " + policy::MountpointTable::Name + "("
"id_mountpoint INTEGER PRIMARY KEY AUTOINCREMENT,"
std::string req = "CREATE TABLE IF NOT EXISTS " + policy::DeviceTable::Name + "("
"id_device INTEGER PRIMARY KEY AUTOINCREMENT,"
"uuid TEXT UNIQUE ON CONFLICT FAIL,"
"is_removable BOOLEAN,"
"is_present BOOLEAN"
......@@ -96,9 +96,9 @@ bool Mountpoint::createTable(DBConnection connection)
return sqlite::Tools::executeRequest( connection, req );
}
std::shared_ptr<Mountpoint> Mountpoint::fromUuid( DBConnection dbConnection, const std::string& uuid )
std::shared_ptr<Device> Device::fromUuid( DBConnection dbConnection, const std::string& uuid )
{
static const std::string req = "SELECT * FROM " + policy::MountpointTable::Name +
static const std::string req = "SELECT * FROM " + policy::DeviceTable::Name +
" WHERE uuid = ?";
return fetch( dbConnection, req, uuid );
}
......
......@@ -25,32 +25,32 @@
#include "Types.h"
#include "database/DatabaseHelpers.h"
class Mountpoint;
class Device;
namespace policy
{
struct MountpointTable
struct DeviceTable
{
static const std::string Name;
static const std::string PrimaryKeyColumn;
static unsigned int Mountpoint::*const PrimaryKey;
static unsigned int Device::*const PrimaryKey;
};
}
class Mountpoint : public DatabaseHelpers<Mountpoint, policy::MountpointTable>
class Device : public DatabaseHelpers<Device, policy::DeviceTable>
{
public:
Mountpoint( const std::string& uuid, bool isRemovable );
Mountpoint( DBConnection dbConnection, sqlite::Row& row );
Device( const std::string& uuid, bool isRemovable );
Device( DBConnection dbConnection, sqlite::Row& row );
unsigned int id() const;
const std::string& uuid() const;
bool isRemovable() const;
bool isPresent() const;
void setPresent( bool value );
static std::shared_ptr<Mountpoint> create( DBConnection dbConnection, const std::string& uuid, bool isRemovable );
static std::shared_ptr<Device> create( DBConnection dbConnection, const std::string& uuid, bool isRemovable );
static bool createTable( DBConnection connection );
static std::shared_ptr<Mountpoint> fromUuid( DBConnection dbConnection, const std::string& uuid );
static std::shared_ptr<Device> fromUuid( DBConnection dbConnection, const std::string& uuid );
private:
DBConnection m_dbConn;
......@@ -62,5 +62,5 @@ private:
bool m_isRemovable;
bool m_isPresent;
friend struct policy::MountpointTable;
friend struct policy::DeviceTable;
};
......@@ -31,7 +31,7 @@
#include "AudioTrack.h"
#include "discoverer/DiscovererWorker.h"
#include "Media.h"
#include "Mountpoint.h"
#include "Device.h"
#include "Folder.h"
#include "MediaLibrary.h"
#include "IMetadataService.h"
......@@ -99,7 +99,7 @@ MediaLibrary::~MediaLibrary()
VideoTrack::clear();
AudioTrack::clear();
Artist::clear();
Mountpoint::clear();
Device::clear();
// Explicitely release the connection's TLS
if ( m_dbConnection != nullptr )
m_dbConnection->release();
......@@ -156,7 +156,7 @@ bool MediaLibrary::initialize( const std::string& dbPath, const std::string& sna
Artist::createTable( m_dbConnection.get() ) &&
Artist::createDefaultArtists( m_dbConnection.get() ) &&
Settings::createTable( m_dbConnection.get() ) &&
Mountpoint::createTable( m_dbConnection.get() ) ) == false )
Device::createTable( m_dbConnection.get() ) ) == false )
{
LOG_ERROR( "Failed to create database structure" );
return false;
......@@ -267,14 +267,14 @@ bool MediaLibrary::deleteFolder( FolderPtr folder )
return true;
}
std::shared_ptr<Mountpoint> MediaLibrary::mountpoint( const std::string& uuid )
std::shared_ptr<Device> MediaLibrary::device( const std::string& uuid )
{
return Mountpoint::fromUuid( m_dbConnection.get(), uuid );
return Device::fromUuid( m_dbConnection.get(), uuid );
}
std::shared_ptr<Mountpoint> MediaLibrary::addMountpoint( const std::string& uuid, bool isRemovable )
std::shared_ptr<Device> MediaLibrary::addDevice( const std::string& uuid, bool isRemovable )
{
return Mountpoint::create( m_dbConnection.get(), uuid, isRemovable );
return Device::create( m_dbConnection.get(), uuid, isRemovable );
}
LabelPtr MediaLibrary::createLabel( const std::string& label )
......
......@@ -38,7 +38,7 @@ class Artist;
class Media;
class Movie;
class Show;
class Mountpoint;
class Device;
class MediaLibrary : public IMediaLibrary
{
......@@ -58,8 +58,8 @@ class MediaLibrary : public IMediaLibrary
virtual FolderPtr folder( const std::string& path ) override;
bool deleteFolder( FolderPtr folder );
std::shared_ptr<Mountpoint> mountpoint( const std::string& uuid );
std::shared_ptr<Mountpoint> addMountpoint( const std::string& uuid, bool isRemovable );
std::shared_ptr<Device> device( const std::string& uuid );
std::shared_ptr<Device> addDevice( const std::string& uuid, bool isRemovable );
virtual LabelPtr createLabel( const std::string& label ) override;
virtual bool deleteLabel( LabelPtr label ) override;
......
......@@ -26,9 +26,9 @@
#include <queue>
#include "factory/FileSystem.h"
#include "filesystem/IMountpoint.h"
#include "filesystem/IDevice.h"
#include "Media.h"
#include "Mountpoint.h"
#include "Device.h"
#include "Folder.h"
#include "logging/Logger.h"
#include "MediaLibrary.h"
......@@ -74,14 +74,14 @@ bool FsDiscoverer::discover( const std::string &entryPoint )
auto f = Folder::create( m_dbConn, fsDir->path(), 0, fsDir->isRemovable(), 0 );
if ( f == nullptr )
return false;
// Since this is the "root" folder, we will always try to add its mountpoint to our mountpoint list.
// We can then check if the subfolders mountpoint are different
auto mountpointFs = fsDir->mountpoint();
auto mountpoint = Mountpoint::fromUuid( m_dbConn, mountpointFs->uuid() );
if ( mountpoint == nullptr )
// Since this is the "root" folder, we will always try to add the device it's located on
// to our devices list. We can then check if the subfolders containing device are different
auto deviceFs = fsDir->device();
auto device = Device::fromUuid( m_dbConn, deviceFs->uuid() );
if ( device == nullptr )
{
LOG_INFO( "Creating new mountpoint ", mountpointFs->uuid() );
mountpoint = Mountpoint::create( m_dbConn, mountpointFs->uuid(), mountpointFs->isRemovable() );
LOG_INFO( "Creating new device in DB ", deviceFs->uuid() );
device = Device::create( m_dbConn, deviceFs->uuid(), deviceFs->isRemovable() );
}
checkFiles( fsDir.get(), f );
checkSubfolders( fsDir.get(), f, blist );
......
......@@ -20,7 +20,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "Mountpoint.h"
#include "Device.h"
#include <mntent.h>
#include <cstring>
......@@ -28,52 +28,51 @@
namespace
{
// Allow private ctors to be used from make_shared
struct MountpointBuilder : fs::Mountpoint
struct DeviceBuilder : fs::Device
{
MountpointBuilder( const std::string& path ) : Mountpoint( path ) {}
DeviceBuilder( const std::string& path ) : Device( path ) {}
};
}
namespace fs
{
const Mountpoint::MountpointMap Mountpoint::Cache = Mountpoint::listMountpoints();
std::shared_ptr<IMountpoint> Mountpoint::unknownMountpoint = std::make_shared<UnknownMountpoint>();
const Device::DeviceMap Device::Cache = Device::listDevices();
Mountpoint::Mountpoint( const std::string& devicePath )
Device::Device( const std::string& devicePath )
: m_device( devicePath )
, m_uuid( "fake uuid" )
{
}
const std::string& Mountpoint::uuid() const
const std::string& Device::uuid() const
{
return m_uuid;
}
bool Mountpoint::isPresent() const
bool Device::isPresent() const
{
return true;
}
bool Mountpoint::isRemovable() const
bool Device::isRemovable() const
{
return false;
}
std::shared_ptr<IMountpoint> Mountpoint::fromPath( const std::string& path )
std::shared_ptr<IDevice> Device::fromPath( const std::string& path )
{
for ( const auto& p : Cache )
{
if ( path.find( p.first ) == 0 )
return p.second;
}
return unknownMountpoint;
return nullptr;
}
Mountpoint::MountpointMap Mountpoint::listMountpoints()
Device::DeviceMap Device::listDevices()
{
MountpointMap res;
DeviceMap res;
FILE* f = setmntent("/etc/mtab", "r");
if ( f == nullptr )
throw std::runtime_error( "Failed to read /etc/mtab" );
......@@ -98,7 +97,7 @@ Mountpoint::MountpointMap Mountpoint::listMountpoints()
|| strcmp( s.mnt_type, "binfmt_misc" ) == 0
|| strcmp( s.mnt_type, "tmpfs" ) == 0 )
continue;
res[s.mnt_dir] = std::make_shared<MountpointBuilder>( s.mnt_fsname );
res[s.mnt_dir] = std::make_shared<DeviceBuilder>( s.mnt_fsname );
}
return res;
}
......
......@@ -22,45 +22,33 @@
#pragma once
#include "filesystem/IMountpoint.h"
#include "filesystem/IDevice.h"
#include <memory>
#include <unordered_map>
namespace fs
{
class UnknownMountpoint : public IMountpoint
class Device : public IDevice
{
public:
UnknownMountpoint() : m_uuid( "unknown" ) {}
virtual const std::string& uuid() const override { return m_uuid; }
virtual bool isPresent() const override { return true; }
virtual bool isRemovable() const override { return false; }
private:
std::string m_uuid;
};
class Mountpoint : public IMountpoint
{
public:
using MountpointMap = std::unordered_map<std::string, std::shared_ptr<IMountpoint>>;
using DeviceMap = std::unordered_map<std::string, std::shared_ptr<IDevice>>;
virtual const std::string& uuid() const override;
virtual bool isPresent() const override;
virtual bool isRemovable() const override;
static std::shared_ptr<IMountpoint> fromPath( const std::string& path );
static std::shared_ptr<IDevice> fromPath( const std::string& path );
protected:
Mountpoint( const std::string& devicePath );
Device( const std::string& devicePath );
private:
static MountpointMap listMountpoints();
static std::shared_ptr<IMountpoint> unknownMountpoint;
static DeviceMap listDevices();
private:
static const MountpointMap Cache;
static const DeviceMap Cache;
private:
std::string m_device;
......
......@@ -22,7 +22,7 @@
#include "Directory.h"
#include "Media.h"
#include "Mountpoint.h"
#include "Device.h"
#include <cstring>
#include <cstdlib>
......@@ -72,10 +72,10 @@ unsigned int Directory::lastModificationDate() const
return m_lastModificationDate;
}
std::shared_ptr<IMountpoint> Directory::mountpoint() const
std::shared_ptr<IDevice> Directory::device() const
{
//FIXME: Cache this?
return Mountpoint::fromPath( m_path );
return Device::fromPath( m_path );
}
bool Directory::isRemovable() const
......
......@@ -36,7 +36,7 @@ public:
virtual const std::vector<std::string>& files() override;
virtual const std::vector<std::string>& dirs() override;
virtual unsigned int lastModificationDate() const override;
virtual std::shared_ptr<IMountpoint> mountpoint() const override;
virtual std::shared_ptr<IDevice> device() const override;
virtual bool isRemovable() const override;
private:
......
......@@ -28,7 +28,7 @@
#include "filesystem/IDirectory.h"
#include "filesystem/IFile.h"
#include "filesystem/IMountpoint.h"
#include "filesystem/IDevice.h"
#include "factory/IFileSystem.h"
#include "utils/Filename.h"
......@@ -86,10 +86,10 @@ public:
unsigned int m_lastModification;
};
class Mountpoint : public fs::IMountpoint
class Device : public fs::IDevice
{
public:
Mountpoint( const std::string& uuid ) : m_uuid( uuid ), m_present( true ), m_removable( false ) {}
Device( const std::string& uuid ) : m_uuid( uuid ), m_present( true ), m_removable( false ) {}
virtual const std::string& uuid() const override { return m_uuid; }
virtual bool isPresent() const override { return m_present; }
virtual bool isRemovable() const override { return m_removable; }
......@@ -106,12 +106,12 @@ private:
class Directory : public fs::IDirectory
{
public:
Directory( std::shared_ptr<mock::Directory> parent, const std::string& path, unsigned int lastModif, std::shared_ptr<fs::IMountpoint> mountpoint )
Directory( std::shared_ptr<mock::Directory> parent, const std::string& path, unsigned int lastModif, std::shared_ptr<fs::IDevice> device )
: m_path( path )
, m_parent( parent )
, m_lastModificationDate( lastModif )
, m_isRemovable( false )
, m_mountpoint( mountpoint )
, m_device( device )
{
}
......@@ -135,9 +135,9 @@ public:
return m_lastModificationDate;
}
virtual std::shared_ptr<fs::IMountpoint> mountpoint() const override
virtual std::shared_ptr<fs::IDevice> device() const override
{
return m_mountpoint;
return m_device;
}
void addFile( const std::string& fileName )
......@@ -201,7 +201,7 @@ private:
std::shared_ptr<mock::Directory> m_parent;
unsigned int m_lastModificationDate;
bool m_isRemovable;
std::shared_ptr<fs::IMountpoint> m_mountpoint;
std::shared_ptr<fs::IDevice> m_device;
};
......@@ -212,16 +212,16 @@ struct FileSystemFactory : public factory::IFileSystem
FileSystemFactory()
{
auto rootMountpoint = std::make_shared<Mountpoint>( "root" );
auto removableMountpoint = std::make_shared<Mountpoint>( "removable" );
removableMountpoint->setRemovable( true );
removableMountpoint->setPresent( true );
dirs[Root] = std::unique_ptr<mock::Directory>( new Directory{ nullptr, Root, 123, rootMountpoint } );
auto rootDevice = std::make_shared<Device>( "root" );
auto removableDevice = std::make_shared<Device>( "removable" );
removableDevice ->setRemovable( true );
removableDevice ->setPresent( true );
dirs[Root] = std::unique_ptr<mock::Directory>( new Directory{ nullptr, Root, 123, rootDevice } );
addFile( Root, "video.avi" );
addFile( Root, "audio.mp3" );
addFile( Root, "not_a_media.something" );
addFile( Root, "some_other_file.seaotter" );
addFolder( Root, "folder/", 456, removableMountpoint );
addFolder( Root, "folder/", 456, removableDevice );
addFile( SubFolder, "subfile.mp4" );
}
......@@ -231,11 +231,11 @@ struct FileSystemFactory : public factory::IFileSystem
files[path + fileName] = std::unique_ptr<mock::File>( new mock::File( path + fileName ) );
}
void addFolder( const std::string& parentPath, const std::string& path, unsigned int lastModif, std::shared_ptr<fs::IMountpoint> mountpoint )
void addFolder( const std::string& parentPath, const std::string& path, unsigned int lastModif, std::shared_ptr<fs::IDevice> device )
{
auto parent = dirs[parentPath];
parent->addFolder( path );
dirs[parentPath + path] = std::unique_ptr<mock::Directory>( new Directory( parent, parentPath + path, lastModif, mountpoint ) );
dirs[parentPath + path] = std::unique_ptr<mock::Directory>( new Directory( parent, parentPath + path, lastModif, device ) );
}
void removeFile( const std::string& path, const std::string& fileName )
......
......@@ -22,40 +22,40 @@
#include "Tests.h"
#include "Mountpoint.h"
#include "Device.h"
class Mountpoints : public Tests
class Devices : public Tests
{
};
TEST_F( Mountpoints, Create )
TEST_F( Devices, Create )
{
auto m = ml->addMountpoint( "dummy", true );
ASSERT_NE( nullptr, m );
ASSERT_EQ( "dummy", m->uuid() );
ASSERT_TRUE( m->isRemovable() );
ASSERT_TRUE( m->isPresent() );
auto d = ml->addDevice( "dummy", true );
ASSERT_NE( nullptr, d );
ASSERT_EQ( "dummy", d->uuid() );
ASSERT_TRUE( d->isRemovable() );
ASSERT_TRUE( d->isPresent() );
Reload();
m = ml->mountpoint( "dummy" );
ASSERT_NE( nullptr, m );
ASSERT_EQ( "dummy", m->uuid() );
ASSERT_TRUE( m->isRemovable() );
ASSERT_TRUE( m->isPresent() );
d = ml->device( "dummy" );
ASSERT_NE( nullptr, d );
ASSERT_EQ( "dummy", d->uuid() );
ASSERT_TRUE( d->isRemovable() );
ASSERT_TRUE( d->isPresent() );
}
TEST_F( Mountpoints, SetPresent )
TEST_F( Devices, SetPresent )
{
auto m = ml->addMountpoint( "dummy", true );
ASSERT_NE( nullptr, m );
ASSERT_TRUE( m->isPresent() );
auto d = ml->addDevice( "dummy", true );
ASSERT_NE( nullptr, d );
ASSERT_TRUE( d->isPresent() );
m->setPresent( false );
ASSERT_FALSE( m->isPresent() );
d->setPresent( false );
ASSERT_FALSE( d->isPresent() );
Reload();
m = ml->mountpoint( "dummy" );
ASSERT_FALSE( m->isPresent() );
d = ml->device( "dummy" );
ASSERT_FALSE( d->isPresent() );
}
......@@ -11,7 +11,7 @@ list(APPEND TEST_SRCS
unittest/FsUtilsTests.cpp
unittest/ArtistTests.cpp
unittest/AlbumTrackTests.cpp
unittest/MountpointTests.cpp
unittest/DeviceTests.cpp
mocks/FileSystem.h
mocks/DiscovererCbMock.h
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment