[vlc-commits] [Git][videolan/vlc][master] 6 commits: medialibrary: fix FsFactory::createFile method
Hugo Beauzée-Luyssen
gitlab at videolan.org
Sun May 2 10:44:13 UTC 2021
Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC
Commits:
e7fd452c by Alaric Senat at 2021-05-02T10:31:06+00:00
medialibrary: fix FsFactory::createFile method
The createFile method in the FsFactory was using the mrl incorrectly by
creating a temporary directory with the full mrl of the file instead of
truncating the file name.
- - - - -
c35d7f02 by Alaric Senat at 2021-05-02T10:31:06+00:00
medialibrary: fs/file: reformat ctor
Previously, input_files's slaves where just ignored while creating ml
files. These changes take into account these files, create them in the
ml hierarchy and link them with their relatives.
- - - - -
edfcc459 by Alaric Senat at 2021-05-02T10:31:06+00:00
medialibrary: add input_slaves to the ml hierarchy
Previously, input_files's slaves where just ignored while creating ml
files. These changes take into account these files, create them in the
ml hierarchy and link them with their relatives.
- - - - -
80864e99 by Alaric Senat at 2021-05-02T10:31:06+00:00
medialibrary: expose new ml_file_t values
The medialibrary has files sizes and last modification dates in its
database. This patch exposes theses two previously ignored values.
- - - - -
2af0f654 by Alaric Senat at 2021-05-02T10:31:06+00:00
medialibrary: fix `directory.cpp" formatting
- - - - -
76107933 by Alaric Senat at 2021-05-02T10:31:06+00:00
medialibrary: change misleading error messages
Switch to a more generic error message.
Theses errors message can be triggered by local filesystem errors or any
timeouts. Assuming all the errors will come from network is incorrect
and potentially misleading.
- - - - -
9 changed files:
- include/vlc_media_library.h
- modules/misc/medialibrary/entities.cpp
- modules/misc/medialibrary/fs/directory.cpp
- modules/misc/medialibrary/fs/directory.h
- modules/misc/medialibrary/fs/file.cpp
- modules/misc/medialibrary/fs/file.h
- modules/misc/medialibrary/fs/fs.cpp
- modules/misc/medialibrary/fs/util.cpp
- modules/misc/medialibrary/fs/util.h
Changes:
=====================================
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;
=====================================
modules/misc/medialibrary/entities.cpp
=====================================
@@ -296,6 +296,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
=====================================
modules/misc/medialibrary/fs/directory.cpp
=====================================
@@ -26,16 +26,20 @@
#include "file.h"
#include "util.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>
+
+#include <algorithm>
+#include <assert.h>
#include <medialibrary/filesystem/Errors.h>
+#include <sys/stat.h>
+#include <system_error>
+#include <vector>
using InputItemPtr = vlc_shared_data_ptr_type(input_item_t,
input_item_Hold,
@@ -61,7 +65,7 @@ SDDirectory::mrl() const
const std::vector<std::shared_ptr<IFile>> &
SDDirectory::files() const
{
- if (!m_read_done)
+ if ( !m_read_done )
read();
return m_files;
}
@@ -69,7 +73,7 @@ SDDirectory::files() const
const std::vector<std::shared_ptr<IDirectory>> &
SDDirectory::dirs() const
{
- if (!m_read_done)
+ if ( !m_read_done )
read();
return m_dirs;
}
@@ -77,8 +81,8 @@ SDDirectory::dirs() const
std::shared_ptr<IDevice>
SDDirectory::device() const
{
- if (!m_device)
- m_device = m_fs.createDeviceFromMrl(mrl());
+ if ( !m_device )
+ m_device = m_fs.createDeviceFromMrl( mrl() );
return m_device;
}
@@ -135,13 +139,13 @@ static void onParserSubtreeAdded( input_item_t *, input_item_node_t *subtree,
{
auto req = static_cast<vlc::medialibrary::metadata_request*>( data );
- for (int i = 0; i < subtree->i_children; ++i)
+ for ( int i = 0; i < subtree->i_children; ++i )
{
- input_item_node_t *child = subtree->pp_children[i];
+ input_item_node_t* child = subtree->pp_children[i];
/* this class assumes we always receive a flat list */
- assert(child->i_children == 0);
- input_item_t *media = child->p_item;
- req->children->emplace_back( media );
+ assert( child->i_children == 0 );
+ input_item_t* media = child->p_item;
+ req->children->emplace_back( media );
}
}
@@ -165,21 +169,20 @@ static bool request_metadata_sync( libvlc_int_t *libvlc, input_item_t *media,
onParserSubtreeAdded,
};
- auto inputParser = vlc::wrap_cptr(
- input_item_Parse( media, VLC_OBJECT( libvlc ), &cbs, &req ),
- &input_item_parser_id_Release );
+ auto inputParser = vlc::wrap_cptr( input_item_Parse( media, VLC_OBJECT( libvlc ), &cbs, &req ),
+ &input_item_parser_id_Release );
- if ( inputParser== nullptr )
+ if ( inputParser == nullptr )
return false;
vlc::threads::mutex_locker lock( req.lock );
while ( req.probe == false )
{
auto res = req.cond.timedwait( req.lock, deadline );
- if (res != 0 )
+ if ( res != 0 )
{
- throw medialibrary::fs::errors::System( ETIMEDOUT,
- "Failed to browse network directory: Network is too slow" );
+ throw medialibrary::fs::errors::System(
+ ETIMEDOUT, "Failed to browse directory: Operation timed out" );
}
}
return req.success;
@@ -188,33 +191,79 @@ static bool request_metadata_sync( libvlc_int_t *libvlc, input_item_t *media,
void
SDDirectory::read() const
{
- auto media = vlc::wrap_cptr( input_item_New(m_mrl.c_str(), m_mrl.c_str()),
- &input_item_Release );
- if (!media)
+ auto media =
+ vlc::wrap_cptr( input_item_New( m_mrl.c_str(), m_mrl.c_str() ), &input_item_Release );
+ if ( !media )
throw std::bad_alloc();
std::vector<InputItemPtr> children;
input_item_AddOption( media.get(), "show-hiddenfiles", VLC_INPUT_OPTION_TRUSTED );
input_item_AddOption( media.get(), "ignore-filetypes=''", VLC_INPUT_OPTION_TRUSTED );
- auto status = request_metadata_sync( m_fs.libvlc(), media.get(), &children);
+ input_item_AddOption( media.get(), "sub-autodetect-fuzzy=2", VLC_INPUT_OPTION_TRUSTED );
+ auto status = request_metadata_sync( m_fs.libvlc(), media.get(), &children );
if ( status == false )
- throw medialibrary::fs::errors::System( EIO,
- "Failed to browse network directory: Unknown error" );
+ throw medialibrary::fs::errors::System(
+ EIO, "Failed to browse directory: Unknown error" );
- for (const InputItemPtr &m : children)
+ for ( const InputItemPtr& m : children )
{
- const char *mrl = m.get()->psz_uri;
+ const char* mrl = m.get()->psz_uri;
enum input_item_type_e type = m->i_type;
- if (type == ITEM_TYPE_DIRECTORY)
- m_dirs.push_back(std::make_shared<SDDirectory>(mrl, m_fs));
- else if (type == ITEM_TYPE_FILE)
- m_files.push_back(std::make_shared<SDFile>(mrl));
+ if ( type == ITEM_TYPE_DIRECTORY )
+ {
+ m_dirs.push_back( std::make_shared<SDDirectory>( mrl, m_fs ) );
+ }
+ else if ( type == ITEM_TYPE_FILE )
+ {
+ addFile( mrl, IFile::LinkedFileType::None, {} );
+ for ( auto i = 0; i < m->i_slaves; ++i )
+ {
+ const auto* slave = m->pp_slaves[i];
+ const auto linked_type = slave->i_type == SLAVE_TYPE_AUDIO
+ ? IFile::LinkedFileType::SoundTrack
+ : IFile::LinkedFileType::Subtitles;
+
+ addFile( slave->psz_uri, linked_type, mrl );
+ }
+ }
}
m_read_done = true;
}
+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 ), fileSize, lastModificationDate ) );
+ }
+ else
+ {
+ m_files.push_back( std::make_shared<SDFile>(
+ std::move( mrl ), fType, std::move( linkedFile ), fileSize, lastModificationDate ) );
+ }
+}
} /* namespace medialibrary */
} /* namespace vlc */
=====================================
modules/misc/medialibrary/fs/directory.h
=====================================
@@ -44,6 +44,7 @@ public:
private:
void read() const;
+ void addFile( std::string mrl, fs::IFile::LinkedFileType, std::string linkedWith ) const;
std::string m_mrl;
SDFileSystemFactory &m_fs;
=====================================
modules/misc/medialibrary/fs/file.cpp
=====================================
@@ -28,10 +28,28 @@
namespace vlc {
namespace medialibrary {
-SDFile::SDFile(const std::string &mrl)
- : m_mrl(mrl)
- , m_name(utils::fileName(mrl))
- , m_extension(utils::extension(mrl))
+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.find( "file://" ) != 0 )
+ , m_size( size )
+ , m_lastModificationTime( lastModificationDate )
+{
+}
+SDFile::SDFile( const std::string mrl,
+ const LinkedFileType fType,
+ 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.find( "file://" ) != 0 )
+ , m_size( size )
+ , m_lastModificationTime( lastModificationDate )
{
}
@@ -56,18 +74,24 @@ SDFile::extension() const
time_t
SDFile::lastModificationDate() const
{
- return 0;
+ return m_lastModificationTime;
+}
+
+bool
+SDFile::isNetwork() const
+{
+ return m_isNetwork;
}
int64_t
SDFile::size() const
{
- return 0;
+ return m_size;
}
IFile::LinkedFileType SDFile::linkedType() const
{
- return IFile::LinkedFileType::None;
+ return m_linkedType;
}
const std::string &SDFile::linkedWith() const
=====================================
modules/misc/medialibrary/fs/file.h
=====================================
@@ -31,22 +31,28 @@ using namespace ::medialibrary::fs;
class SDFile : public IFile
{
public:
- explicit SDFile(const std::string &mrl);
+ 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;
const std::string& name() const override;
const std::string& extension() const override;
- time_t lastModificationDate() const override;
- int64_t size() const override;
- inline bool isNetwork() const override { return true; }
+ const std::string& linkedWith() const override;
LinkedFileType linkedType() const override;
- const std::string &linkedWith() const override;
+ bool isNetwork() const override;
+ int64_t size() const override;
+ time_t lastModificationDate() const override;
private:
std::string m_mrl;
std::string m_name;
std::string m_extension;
std::string m_linkedFile;
+ LinkedFileType m_linkedType = LinkedFileType::None;
+ bool m_isNetwork;
+ int64_t m_size = 0;
+ time_t m_lastModificationTime = 0;
};
} /* namespace medialibrary */
=====================================
modules/misc/medialibrary/fs/fs.cpp
=====================================
@@ -60,7 +60,7 @@ SDFileSystemFactory::createDirectory(const std::string &mrl)
std::shared_ptr<fs::IFile>
SDFileSystemFactory::createFile(const std::string& mrl)
{
- auto dir = createDirectory(mrl);
+ auto dir = createDirectory(utils::directory(mrl));
assert(dir != nullptr);
return dir->file(mrl);
}
=====================================
modules/misc/medialibrary/fs/util.cpp
=====================================
@@ -52,6 +52,14 @@ fileName(const std::string &filePath)
return filePath.substr(pos + 1);
}
+std::string
+directory(const std::string &filePath)
+{
+ auto pos = filePath.find_last_of(DIR_SEPARATORS);
+ if (pos == std::string::npos)
+ return filePath;
+ return filePath.substr(0, pos);
+}
} /* namespace utils */
} /* namespace medialibrary */
} /* namespace vlc */
=====================================
modules/misc/medialibrary/fs/util.h
=====================================
@@ -29,6 +29,7 @@ namespace vlc {
std::string fileName(const std::string& filePath);
std::string extension(const std::string& fileName);
+std::string directory(const std::string& fileName);
} /* namespace utils */
} /* namespace medialibrary */
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/3707b647ea48cf3850737cb49e69212d9c04b9ab...761079335c9554cfb0a4baf6fee44025a1aa639a
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/3707b647ea48cf3850737cb49e69212d9c04b9ab...761079335c9554cfb0a4baf6fee44025a1aa639a
You're receiving this email because of your account on code.videolan.org.
More information about the vlc-commits
mailing list