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

Add a fs::Mountpoint class

parent 4869ad56
......@@ -29,6 +29,7 @@ namespace fs
{
class IDirectory;
class IFile;
class IMountpoint;
}
namespace factory
......
......@@ -29,6 +29,7 @@
namespace fs
{
class IFile;
class IMountpoint;
class IDirectory
{
......@@ -41,6 +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 bool isRemovable() const = 0;
};
}
/*****************************************************************************
* 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.
*****************************************************************************/
#pragma once
#include <string>
namespace fs
{
class IMountpoint
{
public:
virtual ~IMountpoint() = default;
virtual const std::string& uuid() const = 0;
virtual bool isPresent() const = 0;
virtual bool isRemovable() const = 0;
};
}
......@@ -72,6 +72,7 @@ list(APPEND SRC_LIST ${HEADERS_LIST}
filesystem/common/CommonFile.cpp
filesystem/${ARCH_FOLDER}/Directory.cpp
filesystem/${ARCH_FOLDER}/File.cpp
filesystem/${ARCH_FOLDER}/Mountpoint.cpp
discoverer/FsDiscoverer.cpp
discoverer/DiscovererWorker.cpp
......
......@@ -22,6 +22,7 @@
#include "Directory.h"
#include "Media.h"
#include "Mountpoint.h"
#include <cstring>
#include <cstdlib>
......@@ -71,6 +72,12 @@ unsigned int Directory::lastModificationDate() const
return m_lastModificationDate;
}
std::shared_ptr<IMountpoint> Directory::mountpoint() const
{
//FIXME: Cache this?
return Mountpoint::fromPath( m_path );
}
bool Directory::isRemovable() const
{
//FIXME
......
......@@ -36,6 +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 bool isRemovable() const override;
private:
......
/*****************************************************************************
* 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.
*****************************************************************************/
#include "Mountpoint.h"
#include <mntent.h>
#include <cstring>
namespace
{
// Allow private ctors to be used from make_shared
struct MountpointBuilder : fs::Mountpoint
{
MountpointBuilder( const std::string& path ) : Mountpoint( path ) {}
};
}
namespace fs
{
const Mountpoint::MountpointMap Mountpoint::Cache = Mountpoint::listMountpoints();
std::shared_ptr<IMountpoint> Mountpoint::unknownMountpoint = std::make_shared<UnknownMountpoint>();
Mountpoint::Mountpoint( const std::string& devicePath )
: m_device( devicePath )
, m_uuid( "fake uuid" )
{
}
const std::string& Mountpoint::uuid() const
{
return m_uuid;
}
bool Mountpoint::isPresent() const
{
return true;
}
bool Mountpoint::isRemovable() const
{
return false;
}
std::shared_ptr<IMountpoint> Mountpoint::fromPath( const std::string& path )
{
for ( const auto& p : Cache )
{
if ( path.find( p.first ) == 0 )
return p.second;
}
return unknownMountpoint;
}
Mountpoint::MountpointMap Mountpoint::listMountpoints()
{
MountpointMap res;
FILE* f = setmntent("/etc/mtab", "r");
if ( f == nullptr )
throw std::runtime_error( "Failed to read /etc/mtab" );
std::unique_ptr<FILE, int(*)(FILE*)>( f, &endmntent );
char buff[512];
mntent s;
while ( getmntent_r( f, &s, buff, sizeof(buff) ) != nullptr )
{
// Ugly work around for mountpoints we don't care
if ( strcmp( s.mnt_type, "proc" ) == 0
|| strcmp( s.mnt_type, "devtmpfs" ) == 0
|| strcmp( s.mnt_type, "devpts" ) == 0
|| strcmp( s.mnt_type, "sysfs" ) == 0
|| strcmp( s.mnt_type, "cgroup" ) == 0
|| strcmp( s.mnt_type, "debugfs" ) == 0
|| strcmp( s.mnt_type, "hugetlbfs" ) == 0
|| strcmp( s.mnt_type, "efivarfs" ) == 0
|| strcmp( s.mnt_type, "securityfs" ) == 0
|| strcmp( s.mnt_type, "mqueue" ) == 0
|| strcmp( s.mnt_type, "pstore" ) == 0
|| strcmp( s.mnt_type, "autofs" ) == 0
|| 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 );
}
return res;
}
}
/*****************************************************************************
* 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.
*****************************************************************************/
#pragma once
#include "filesystem/IMountpoint.h"
#include <memory>
#include <unordered_map>
namespace fs
{
class UnknownMountpoint : public IMountpoint
{
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>>;
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 );
protected:
Mountpoint( const std::string& devicePath );
private:
static MountpointMap listMountpoints();
static std::shared_ptr<IMountpoint> unknownMountpoint;
private:
static const MountpointMap Cache;
private:
std::string m_device;
std::string m_uuid;
};
}
......@@ -116,6 +116,11 @@ public:
return m_lastModificationDate;
}
virtual std::shared_ptr<fs::IMountpoint> mountpoint() const override
{
return nullptr;
}
void addFile( const std::string& fileName )
{
m_files.emplace_back( m_path + fileName );
......
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