[vlc-devel] [PATCH 06/14] medialibrary: expose new ml_file_t values

Alaric Senat dev.asenat at posteo.net
Fri Mar 19 11:40:01 UTC 2021


The medialibrary has files sizes and last modification dates in its
database. This patch exposes theses two previously ignored values.
---
 include/vlc_media_library.h                |  2 ++
 modules/misc/medialibrary/entities.cpp     |  2 ++
 modules/misc/medialibrary/fs/directory.cpp | 28 +++++++++++++++++++---
 modules/misc/medialibrary/fs/file.cpp      | 22 ++++++++++-------
 modules/misc/medialibrary/fs/file.h        |  6 +++--
 5 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/include/vlc_media_library.h b/include/vlc_media_library.h
index 39342c7a88..07daa6eded 100644
--- a/include/vlc_media_library.h
+++ b/include/vlc_media_library.h
@@ -150,6 +150,8 @@ typedef struct vlc_ml_label_list_t
 typedef struct vlc_ml_file_t
 {
     char* psz_mrl;
+    int64_t i_size;
+    time_t i_last_modification_date;
     vlc_ml_file_type_t i_type;
     bool b_external;
     bool b_removable;
diff --git a/modules/misc/medialibrary/entities.cpp b/modules/misc/medialibrary/entities.cpp
index a42d1e0727..ad879e0fce 100644
--- a/modules/misc/medialibrary/entities.cpp
+++ b/modules/misc/medialibrary/entities.cpp
@@ -295,6 +295,8 @@ bool Convert( const medialibrary::IFile* input, vlc_ml_file_t& output )
             vlc_assert_unreachable();
     }
 
+    output.i_size = input->size();
+    output.i_last_modification_date = input->lastModificationDate();
     output.b_removable = input->isRemovable();
     output.b_present = true;
     try
diff --git a/modules/misc/medialibrary/fs/directory.cpp b/modules/misc/medialibrary/fs/directory.cpp
index 858553b273..10d0b18915 100644
--- a/modules/misc/medialibrary/fs/directory.cpp
+++ b/modules/misc/medialibrary/fs/directory.cpp
@@ -26,12 +26,15 @@
 #include "file.h"
 #include "util.h"
 
+#include <sys/stat.h>
 #include <algorithm>
 #include <assert.h>
 #include <vector>
 #include <system_error>
 #include <vlc_common.h>
+#include <vlc_url.h>
 #include <vlc_input_item.h>
+#include <vlc_fs.h>
 #include <vlc_input.h>
 #include <vlc_threads.h>
 #include <vlc_cxx_helpers.hpp>
@@ -224,14 +227,33 @@ SDDirectory::read() const
 void
 SDDirectory::addFile(std::string mrl, IFile::LinkedFileType fType, std::string linkedFile) const
 {
+    time_t lastModificationDate = 0;
+    int64_t fileSize = 0;
+
+    if ( m_fs.isNetworkFileSystem() == false )
+    {
+        const auto path = vlc::wrap_cptr( vlc_uri2path( mrl.c_str() ) );
+        struct stat stat;
+
+        if ( vlc_stat( path.get(), &stat ) != 0 )
+        {
+            if ( errno == EACCES )
+                return;
+            throw errors::System{ errno, "Failed to get file info" };
+        }
+        lastModificationDate = stat.st_mtime;
+        fileSize = stat.st_size;
+    }
+
     if ( fType == IFile::LinkedFileType::None )
     {
-        m_files.push_back( std::make_shared<SDFile>( std::move( mrl ) ) );
+        m_files.push_back(
+            std::make_shared<SDFile>( std::move( mrl ), fileSize, lastModificationDate ) );
     }
     else
     {
-        m_files.push_back(
-            std::make_shared<SDFile>( std::move( mrl ), fType, std::move( linkedFile ) ) );
+        m_files.push_back( std::make_shared<SDFile>(
+            std::move( mrl ), fType, std::move( linkedFile ), fileSize, lastModificationDate ) );
     }
 }
   } /* namespace medialibrary */
diff --git a/modules/misc/medialibrary/fs/file.cpp b/modules/misc/medialibrary/fs/file.cpp
index 6ae7400955..c6513f9a56 100644
--- a/modules/misc/medialibrary/fs/file.cpp
+++ b/modules/misc/medialibrary/fs/file.cpp
@@ -28,22 +28,28 @@
 namespace vlc {
   namespace medialibrary {
 
-SDFile::SDFile( const std::string mrl )
-      : m_mrl( std::move( mrl ) )
-      , m_name( utils::fileName( m_mrl ) )
-      , m_extension( utils::extension( m_mrl ) )
-      , m_isNetwork( m_mrl.rfind( "file://" ) != 0 )
+SDFile::SDFile( const std::string mrl, const int64_t size, const time_t lastModificationDate )
+    : m_mrl( std::move( mrl ) )
+    , m_name( utils::fileName( m_mrl ) )
+    , m_extension( utils::extension( m_mrl ) )
+    , m_isNetwork( m_mrl.rfind( "file://" ) != 0 )
+    , m_size( size )
+    , m_lastModificationTime( lastModificationDate )
 {
 }
 SDFile::SDFile( const std::string mrl,
                 const LinkedFileType fType,
-                const std::string linkedFile )
+                const std::string linkedFile,
+                const int64_t size,
+                const time_t lastModificationDate )
     : m_mrl( std::move( mrl ) )
     , m_name( utils::fileName( m_mrl ) )
     , m_extension( utils::extension( m_mrl ) )
     , m_linkedFile( std::move( linkedFile ) )
     , m_linkedType( fType )
     , m_isNetwork( m_mrl.rfind( "file://" ) != 0 )
+    , m_size( size )
+    , m_lastModificationTime( lastModificationDate )
 {
 }
 
@@ -68,7 +74,7 @@ SDFile::extension() const
 time_t
 SDFile::lastModificationDate() const
 {
-    return 0;
+    return m_lastModificationTime;
 }
 
 bool
@@ -80,7 +86,7 @@ SDFile::isNetwork() const
 int64_t
 SDFile::size() const
 {
-    return 0;
+    return m_size;
 }
 
 IFile::LinkedFileType SDFile::linkedType() const
diff --git a/modules/misc/medialibrary/fs/file.h b/modules/misc/medialibrary/fs/file.h
index 97886ddbc5..7e929c6b46 100644
--- a/modules/misc/medialibrary/fs/file.h
+++ b/modules/misc/medialibrary/fs/file.h
@@ -31,8 +31,8 @@ using namespace ::medialibrary::fs;
 class SDFile : public IFile
 {
 public:
-    explicit SDFile( std::string mrl );
-    SDFile( std::string mrl, LinkedFileType, std::string linkedFile );
+    SDFile( std::string mrl, int64_t, time_t );
+    SDFile( std::string mrl, LinkedFileType, std::string linkedFile, int64_t, time_t );
 
     virtual ~SDFile() = default;
     const std::string& mrl() const override;
@@ -51,6 +51,8 @@ private:
     std::string m_linkedFile;
     LinkedFileType m_linkedType = LinkedFileType::None;
     bool m_isNetwork;
+    int64_t m_size = 0;
+    time_t m_lastModificationTime = 0;
 };
 
   } /* namespace medialibrary */
-- 
2.29.2



More information about the vlc-devel mailing list