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

Folders: Add a lastModificationDate field

parent f6e552e5
......@@ -13,5 +13,6 @@ public:
// This will only returns the files in this immediate folder
virtual std::vector<FilePtr> files() = 0;
virtual std::vector<FolderPtr> folders() = 0;
virtual unsigned int lastModificationDate() = 0;
virtual FolderPtr parent() = 0;
};
......@@ -27,11 +27,13 @@ Folder::Folder( DBConnection dbConnection, sqlite3_stmt* stmt )
m_id = sqlite::Traits<unsigned int>::Load( stmt, 0 );
m_path = sqlite::Traits<std::string>::Load( stmt, 1 );
m_parent = sqlite::Traits<unsigned int>::Load( stmt, 2 );
m_lastModificationDate = sqlite::Traits<unsigned int>::Load( stmt, 3 );
}
Folder::Folder( const std::string& path, unsigned int parent )
Folder::Folder( const std::string& path, unsigned int parent, unsigned int lastModificationDate )
: m_path( path )
, m_parent( parent )
, m_lastModificationDate( lastModificationDate )
{
}
......@@ -42,18 +44,19 @@ bool Folder::createTable(DBConnection connection)
"id_folder INTEGER PRIMARY KEY AUTOINCREMENT,"
"path TEXT UNIQUE ON CONFLICT FAIL,"
"id_parent UNSIGNED INTEGER,"
"last_modification_date UNSIGNED INTEGER,"
"FOREIGN KEY (id_parent) REFERENCES " + policy::FolderTable::Name +
"(id_folder) ON DELETE CASCADE"
")";
return sqlite::Tools::executeRequest( connection, req );
}
FolderPtr Folder::create(DBConnection connection, const std::string& path, unsigned int parent )
FolderPtr Folder::create(DBConnection connection, const std::string& path, unsigned int parent, unsigned int lastModifDate )
{
auto self = std::make_shared<Folder>( path, parent );
auto self = std::make_shared<Folder>( path, parent, lastModifDate );
static const std::string req = "INSERT INTO " + policy::FolderTable::Name +
"(path, id_parent) VALUES(?, ?)";
if ( _Cache::insert( connection, self, req, path, sqlite::ForeignKey( parent ) ) == false )
"(path, id_parent, last_modification_date) VALUES(?, ?, ?)";
if ( _Cache::insert( connection, self, req, path, sqlite::ForeignKey( parent ), lastModifDate ) == false )
return nullptr;
self->m_dbConection = connection;
return self;
......@@ -90,3 +93,8 @@ FolderPtr Folder::parent()
" WHERE id_folder = ?";
return sqlite::Tools::fetchOne<Folder>( m_dbConection, req, m_parent );
}
unsigned int Folder::lastModificationDate()
{
return m_lastModificationDate;
}
......@@ -31,16 +31,17 @@ class Folder : public IFolder, public Cache<Folder, IFolder, policy::FolderTable
public:
Folder(DBConnection dbConnection, sqlite3_stmt* stmt);
Folder(const std::string& path , unsigned int parent);
Folder(const std::string& path , unsigned int parent, unsigned int lastModificationDate );
static bool createTable( DBConnection connection );
static FolderPtr create(DBConnection connection, const std::string& path , unsigned int parent);
static FolderPtr create(DBConnection connection, const std::string& path , unsigned int parent, unsigned int lastModifDate);
virtual unsigned int id() const override;
virtual const std::string& path() override;
virtual std::vector<FilePtr> files() override;
virtual std::vector<FolderPtr> folders() override;
virtual FolderPtr parent() override;
virtual unsigned int lastModificationDate() override;
private:
DBConnection m_dbConection;
......@@ -48,6 +49,7 @@ private:
unsigned int m_id;
std::string m_path;
unsigned int m_parent;
unsigned int m_lastModificationDate;
friend _Cache;
friend struct policy::FolderTable;
......
......@@ -129,7 +129,7 @@ FolderPtr MediaLibrary::addFolder( const std::string& path )
continue;
}
auto folder = Folder::create( m_dbConnection, dir->path(), currentFolder.second );
auto folder = Folder::create( m_dbConnection, dir->path(), currentFolder.second, dir->lastModificationDate() );
if ( folder == nullptr && root == nullptr )
return nullptr;
if ( root == nullptr )
......
......@@ -15,5 +15,6 @@ namespace fs
virtual const std::string& path() const = 0;
virtual const std::vector<std::string>& files() const = 0;
virtual const std::vector<std::string>& dirs() const = 0;
virtual unsigned int lastModificationDate() const = 0;
};
}
......@@ -15,6 +15,9 @@ namespace fs
Directory::Directory( const std::string& path )
: m_path( toAbsolute( path ) )
{
struct stat s;
lstat( path.c_str(), &s );
m_lastModificationDate = s.st_mtim.tv_sec;
read();
}
......@@ -33,6 +36,11 @@ const std::vector<std::string>&Directory::dirs() const
return m_dirs;
}
unsigned int Directory::lastModificationDate() const
{
return m_lastModificationDate;
}
std::string Directory::toAbsolute(const std::string& path)
{
auto abs = std::unique_ptr<char[]>( new char[PATH_MAX] );
......
......@@ -13,6 +13,7 @@ public:
virtual const std::string& path() const override;
virtual const std::vector<std::string>& files() const override;
virtual const std::vector<std::string>& dirs() const override;
virtual unsigned int lastModificationDate() const override;
private:
static std::string toAbsolute( const std::string& path );
......@@ -24,6 +25,7 @@ private:
const std::string m_path;
std::vector<std::string> m_files;
std::vector<std::string> m_dirs;
unsigned int m_lastModificationDate;
};
}
......@@ -13,8 +13,9 @@ namespace mock
class Directory : public fs::IDirectory
{
public:
Directory( const std::string& path, const std::vector<std::string>& files, const std::vector<std::string>& dirs )
Directory( const std::string& path, const std::vector<std::string>& files, const std::vector<std::string>& dirs, unsigned int lastModif )
: m_path( path )
, m_lastModificationDate( lastModif )
{
for ( auto &f : files )
{
......@@ -41,10 +42,16 @@ public:
return m_dirs;
}
virtual unsigned int lastModificationDate() const override
{
return m_lastModificationDate;
}
private:
std::string m_path;
std::vector<std::string> m_files;
std::vector<std::string> m_dirs;
unsigned int m_lastModificationDate;
};
......@@ -69,7 +76,8 @@ struct FileSystemFactory : public factory::IFileSystem
std::vector<std::string>
{
"folder/"
}
},
123
});
}
else if ( path == SubFolder )
......@@ -81,7 +89,8 @@ struct FileSystemFactory : public factory::IFileSystem
{
"subfile.mp4"
},
std::vector<std::string>{}
std::vector<std::string>{},
456
});
}
throw std::runtime_error("Invalid path");
......@@ -225,3 +234,18 @@ TEST_F( Folders, ListFolders )
file = subFiles[0];
ASSERT_EQ( std::string{ mock::FileSystemFactory::SubFolder } + "subfile.mp4", file->mrl() );
}
TEST_F( Folders, LastModificationDate )
{
auto f = ml->addFolder( "." );
ASSERT_EQ( 123u, f->lastModificationDate() );
auto subFolders = f->folders();
ASSERT_EQ( 456u, subFolders[0]->lastModificationDate() );
SetUp();
f = ml->folder( f->path() );
ASSERT_EQ( 123u, f->lastModificationDate() );
subFolders = f->folders();
ASSERT_EQ( 456u, subFolders[0]->lastModificationDate() );
}
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