comparison foosdk/sdk/foobar2000/SDK/playlist_loader.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "foobar2000-sdk-pch.h"
2 #include "playlist_loader.h"
3 #include "link_resolver.h"
4 #include "archive.h"
5 #include "file_info_impl.h"
6 #include "input.h"
7 #include "advconfig.h"
8 #include <string>
9 #include <unordered_set>
10 #include <list>
11
12 constexpr unsigned allowRecurseBase = 2; // max. 2 archive levels - mitigate droste.zip stack overflow
13 static void process_path_internal(const char * p_path,const service_ptr_t<file> & p_reader,playlist_loader_callback::ptr callback, abort_callback & abort,playlist_loader_callback::t_entry_type type,const t_filestats & p_stats, unsigned allowRecurse );
14
15 bool playlist_loader::g_try_load_playlist(file::ptr fileHint,const char * p_path,playlist_loader_callback::ptr p_callback, abort_callback & p_abort) {
16 // Determine if this file is a playlist or not (which usually means that it's a media file)
17 pfc::string8 filepath;
18
19 filesystem::g_get_canonical_path(p_path,filepath);
20
21 pfc::string8 extension = filesystem::g_get_extension(filepath);
22
23 service_ptr_t<file> l_file = fileHint;
24
25 if (l_file.is_empty()) {
26 filesystem::ptr fs;
27 if (filesystem::g_get_interface(fs,filepath)) {
28 if (fs->supports_content_types()) {
29 try {
30 fs->open(l_file,filepath,filesystem::open_mode_read,p_abort);
31 } catch(exception_io const &) { return false; } // fall thru
32 }
33 }
34 }
35
36 service_enum_t<playlist_loader> e;
37
38 if (l_file.is_valid()) {
39
40 // Important: in case of remote HTTP files, use actual connected path for matching file extensions, following any redirects.
41 // At least one internet radio station has been known to present .pls links that are 302 redirects to real streams, so they don't parse as playlists.
42 {
43 file_metadata_http::ptr meta;
44 if (meta &= l_file->get_metadata_(p_abort)) {
45 pfc::string8 realPath;
46 meta->get_connected_path(realPath);
47 extension = filesystem::g_get_extension(realPath);
48 }
49 }
50
51 pfc::string8 content_type;
52 if (l_file->get_content_type(content_type)) {
53 for (auto l : e) {
54 if (l->is_our_content_type(content_type)) {
55 try {
56 TRACK_CODE("playlist_loader::open",l->open(filepath,l_file,p_callback, p_abort));
57 return true;
58 } catch(exception_io_unsupported_format const &) {
59 l_file->reopen(p_abort);
60 }
61 }
62 }
63 }
64 }
65
66 if (extension.length()>0) {
67 for (auto l : e) {
68 if (stricmp_utf8(l->get_extension(),extension) == 0) {
69 if (l_file.is_empty()) filesystem::g_open_read(l_file,filepath,p_abort);
70 try {
71 TRACK_CODE("playlist_loader::open",l->open(filepath,l_file,p_callback,p_abort));
72 return true;
73 } catch(exception_io_unsupported_format const &) {
74 l_file->reopen(p_abort);
75 }
76 }
77 }
78 }
79
80 return false;
81 }
82
83 void playlist_loader::g_load_playlist_filehint(file::ptr fileHint,const char * p_path,playlist_loader_callback::ptr p_callback, abort_callback & p_abort) {
84 if (!g_try_load_playlist(fileHint, p_path, p_callback, p_abort)) throw exception_io_unsupported_format();
85 }
86
87 void playlist_loader::g_load_playlist(const char * p_path,playlist_loader_callback::ptr callback, abort_callback & abort) {
88 g_load_playlist_filehint(NULL,p_path,callback,abort);
89 }
90 namespace {
91 class MIC_impl : public metadb_info_container_v2 {
92 public:
93 t_filestats2 const& stats2() override { return m_stats; }
94 file_info const& info() override { return m_info; }
95 t_filestats const& stats() override { return m_stats.as_legacy(); }
96 bool isInfoPartial() override { return false; }
97
98 file_info_impl m_info;
99 t_filestats2 m_stats;
100 };
101 }
102 static void index_tracks_helper(const char * p_path,const service_ptr_t<file> & p_reader,const t_filestats & p_stats,playlist_loader_callback::t_entry_type p_type,playlist_loader_callback::ptr p_callback, abort_callback & p_abort,bool & p_got_input)
103 {
104 TRACK_CALL_TEXT("index_tracks_helper");
105 if (p_reader.is_empty() && filesystem::g_is_remote_safe(p_path))
106 {
107 TRACK_CALL_TEXT("remote");
108 metadb_handle_ptr handle;
109 p_callback->handle_create(handle,make_playable_location(p_path,0));
110 p_got_input = true;
111 p_callback->on_entry(handle,p_type,p_stats,true);
112 } else {
113 TRACK_CALL_TEXT("hintable");
114 service_ptr_t<input_info_reader> instance;
115 try {
116 input_entry::g_open_for_info_read(instance,p_reader,p_path,p_abort);
117 } catch(exception_io_unsupported_format const &) {
118 // specifically bail
119 throw;
120 } catch(exception_io const &) {
121 // broken file or some other error, open() failed - show it anyway
122 metadb_handle_ptr handle;
123 p_callback->handle_create(handle, make_playable_location(p_path, 0));
124 p_callback->on_entry(handle, p_type, p_stats, true);
125 return;
126 }
127
128 const auto stats = instance->get_stats2_(p_path, stats2_all, p_abort);
129
130 t_uint32 subsong,subsong_count = instance->get_subsong_count();
131 bool bInfoGetError = false;
132 for(subsong=0;subsong<subsong_count;subsong++)
133 {
134 TRACK_CALL_TEXT("subsong-loop");
135 p_abort.check();
136 metadb_handle_ptr handle;
137 t_uint32 index = instance->get_subsong(subsong);
138 p_callback->handle_create(handle,make_playable_location(p_path,index));
139
140 p_got_input = true;
141 if (! bInfoGetError && p_callback->want_info(handle,p_type,stats.as_legacy(),true) )
142 {
143 auto mic = fb2k::service_new<MIC_impl>();
144 mic->m_stats = stats;
145 try {
146 TRACK_CODE("get_info",instance->get_info(index,mic->m_info,p_abort));
147 } catch(...) {
148 bInfoGetError = true;
149 }
150 if (! bInfoGetError ) {
151 playlist_loader_callback_v2::ptr v2;
152 if (v2 &= p_callback) {
153 v2->on_entry_info_v2(handle, p_type, mic, true);
154 } else {
155 p_callback->on_entry_info(handle, p_type, stats.as_legacy(), mic->m_info, true);
156 }
157
158 }
159 }
160 else
161 {
162 p_callback->on_entry(handle,p_type,stats.as_legacy(),true);
163 }
164 }
165 }
166 }
167
168 static void track_indexer__g_get_tracks_wrap(const char * p_path,const service_ptr_t<file> & p_reader,const t_filestats & p_stats,playlist_loader_callback::t_entry_type p_type,playlist_loader_callback::ptr p_callback, abort_callback & p_abort) {
169 bool got_input = false;
170 bool fail = false;
171 try {
172 index_tracks_helper(p_path,p_reader,p_stats,p_type,p_callback,p_abort, got_input);
173 } catch(exception_aborted const &) {
174 throw;
175 } catch(exception_io_unsupported_format const &) {
176 fail = true;
177 } catch(std::exception const & e) {
178 fail = true;
179 FB2K_console_formatter() << "could not enumerate tracks (" << e << ") on:\n" << file_path_display(p_path);
180 }
181 if (fail) {
182 if (!got_input && !p_abort.is_aborting()) {
183 if (p_type == playlist_loader_callback::entry_user_requested)
184 {
185 metadb_handle_ptr handle;
186 p_callback->handle_create(handle,make_playable_location(p_path,0));
187 p_callback->on_entry(handle,p_type,p_stats,true);
188 }
189 }
190 }
191 }
192
193 namespace {
194
195 static bool queryAddHidden() {
196 // {2F9F4956-363F-4045-9531-603B1BF39BA8}
197 static const GUID guid_cfg_addhidden =
198 { 0x2f9f4956, 0x363f, 0x4045,{ 0x95, 0x31, 0x60, 0x3b, 0x1b, 0xf3, 0x9b, 0xa8 } };
199
200 advconfig_entry_checkbox::ptr ptr;
201 if (advconfig_entry::g_find_t(ptr, guid_cfg_addhidden)) {
202 return ptr->get_state();
203 }
204 return false;
205 }
206
207 class directory_callback_myimpl : public directory_callback
208 {
209 public:
210 void main(const char* folder, abort_callback& abort) {
211 visit(folder);
212
213 abort.check();
214 const uint32_t flags = listMode::filesAndFolders | (queryAddHidden() ? listMode::hidden : 0);
215
216 auto workHere = [&] (folder_t const & f) {
217 filesystem_v2::ptr v2;
218 if (v2 &= f.m_fs) {
219 v2->list_directory_ex(f.m_folder.c_str(), *this, flags, abort);
220 } else {
221 f.m_fs->list_directory(f.m_folder.c_str(), *this, abort);
222 }
223 };
224
225 workHere( folder_t { folder, filesystem::get(folder) } );
226
227 for (;; ) {
228 abort.check();
229 auto iter = m_foldersPending.begin();
230 if ( iter == m_foldersPending.end() ) break;
231 auto f = std::move(*iter); m_foldersPending.erase(iter);
232
233 try {
234 workHere( f );
235 } catch (exception_io const & e) {
236 FB2K_console_formatter() << "Error walking directory (" << e << "): " << f.m_folder.c_str();
237 }
238 }
239 }
240
241 bool on_entry(filesystem * owner,abort_callback & p_abort,const char * url,bool is_subdirectory,const t_filestats & p_stats) {
242 p_abort.check();
243 if (!visit(url)) return true;
244
245 filesystem_v2::ptr v2;
246 v2 &= owner;
247
248 if ( is_subdirectory ) {
249 m_foldersPending.emplace_back( folder_t { url, owner } );
250 } else {
251 m_entries.emplace_back( entry_t { url, p_stats } );
252 }
253 return true;
254 }
255
256 struct entry_t {
257 std::string m_path;
258 t_filestats m_stats;
259 };
260 std::list<entry_t> m_entries;
261
262 bool visit(const char* path) {
263 return m_visited.insert( path ).second;
264 }
265 std::unordered_set<std::string> m_visited;
266
267 struct folder_t {
268 std::string m_folder;
269 filesystem::ptr m_fs;
270 };
271 std::list<folder_t> m_foldersPending;
272 };
273 }
274
275
276 static void process_path_internal(const char * p_path,const service_ptr_t<file> & p_reader,playlist_loader_callback::ptr callback, abort_callback & abort,playlist_loader_callback::t_entry_type type,const t_filestats & p_stats, unsigned allowRecurse)
277 {
278 if (allowRecurse == 0) return;
279 //p_path must be canonical
280
281 abort.check();
282
283 callback->on_progress(p_path);
284
285
286 {
287 if (p_reader.is_empty() && type != playlist_loader_callback::entry_directory_enumerated) {
288 try {
289 directory_callback_myimpl results;
290 results.main( p_path, abort );
291 for( auto & i : results.m_entries ) {
292 try {
293 process_path_internal(i.m_path.c_str(), 0, callback, abort, playlist_loader_callback::entry_directory_enumerated, i.m_stats, allowRecurse);
294 } catch (exception_aborted const &) {
295 throw;
296 } catch (std::exception const& e) {
297 FB2K_console_formatter() << "Error walking path (" << e << "): " << file_path_display(i.m_path.c_str());
298 } catch (...) {
299 FB2K_console_formatter() << "Error walking path (bad exception): " << file_path_display(i.m_path.c_str());
300 }
301 }
302 return; // successfully enumerated directory - go no further
303 } catch(exception_aborted const &) {
304 throw;
305 } catch (exception_io_not_directory const &) {
306 // disregard
307 } catch(exception_io_not_found const &) {
308 // disregard
309 } catch (std::exception const& e) {
310 FB2K_console_formatter() << "Error walking directory (" << e << "): " << p_path;
311 } catch (...) {
312 FB2K_console_formatter() << "Error walking directory (bad exception): " << p_path;
313 }
314 }
315
316 if (allowRecurse > 1) {
317 for (auto f : filesystem::enumerate()) {
318 abort.check();
319 service_ptr_t<archive> arch;
320 if ((arch &= f) && arch->is_our_archive(p_path)) {
321 if (p_reader.is_valid()) p_reader->reopen(abort);
322
323 try {
324 archive::list_func_t archive_results = [callback, &abort, allowRecurse](const char* p_path, const t_filestats& p_stats, file::ptr p_reader) {
325 process_path_internal(p_path,p_reader,callback,abort,playlist_loader_callback::entry_directory_enumerated,p_stats,allowRecurse - 1);
326 };
327 TRACK_CODE("archive::archive_list",arch->archive_list(p_path,p_reader,archive_results,/*want readers*/true, abort));
328 return;
329 } catch(exception_aborted const &) {throw;}
330 catch(...) {
331 // Something failed hard
332 // Is is_our_archive() meaningful?
333 archive_v2::ptr arch2;
334 if (arch2 &= arch) {
335 // If yes, show errors
336 throw;
337 }
338 // Outdated archive implementation, preserve legacy behavior (try to open as non-archive)
339 }
340 }
341 }
342 }
343 }
344
345
346
347 {
348 service_ptr_t<link_resolver> ptr;
349 if (link_resolver::g_find(ptr,p_path))
350 {
351 if (p_reader.is_valid()) p_reader->reopen(abort);
352
353 pfc::string8 temp;
354 try {
355 TRACK_CODE("link_resolver::resolve",ptr->resolve(p_reader,p_path,temp,abort));
356
357 track_indexer__g_get_tracks_wrap(temp,0,filestats_invalid,playlist_loader_callback::entry_from_playlist,callback, abort);
358 return;//success
359 } catch(exception_aborted const &) {throw;}
360 catch(...) {}
361 }
362 }
363
364 if (callback->is_path_wanted(p_path,type)) {
365 track_indexer__g_get_tracks_wrap(p_path,p_reader,p_stats,type,callback, abort);
366 }
367 }
368
369 namespace {
370 class plcallback_simple : public playlist_loader_callback {
371 public:
372 void on_progress(const char* p_path) override { (void)p_path; }
373
374 void on_entry(const metadb_handle_ptr& p_item, t_entry_type p_type, const t_filestats& p_stats, bool p_fresh) override {
375 (void)p_type; (void)p_stats; (void)p_fresh;
376 m_items += p_item;
377 }
378 bool want_info(const metadb_handle_ptr& p_item, t_entry_type p_type, const t_filestats& p_stats, bool p_fresh) override {
379 (void)p_type; (void)p_stats; (void)p_fresh;
380 return p_item->should_reload(p_stats, p_fresh);
381 }
382
383 void on_entry_info(const metadb_handle_ptr& p_item, t_entry_type p_type, const t_filestats& p_stats, const file_info& p_info, bool p_fresh) override {
384 (void)p_type;
385 m_items += p_item;
386 m_hints->add_hint(p_item, p_info, p_stats, p_fresh);
387 }
388
389 void handle_create(metadb_handle_ptr& p_out, const playable_location& p_location) override {
390 m_metadb->handle_create(p_out, p_location);
391 }
392
393 bool is_path_wanted(const char* path, t_entry_type type) override {
394 (void)path; (void)type;
395 return true;
396 }
397
398 bool want_browse_info(const metadb_handle_ptr& p_item, t_entry_type p_type, t_filetimestamp ts) override {
399 (void)p_item; (void)p_type; (void)ts;
400 return true;
401 }
402 void on_browse_info(const metadb_handle_ptr& p_item, t_entry_type p_type, const file_info& info, t_filetimestamp ts) override {
403 (void)p_type;
404 metadb_hint_list_v2::ptr v2;
405 if (v2 &= m_hints) v2->add_hint_browse(p_item, info, ts);
406 }
407
408 ~plcallback_simple() {
409 m_hints->on_done();
410 }
411 metadb_handle_list m_items;
412 private:
413 const metadb_hint_list::ptr m_hints = metadb_hint_list::create();
414 const metadb::ptr m_metadb = metadb::get();
415 };
416 }
417 void playlist_loader::g_path_to_handles_simple(const char* p_path, pfc::list_base_t<metadb_handle_ptr>& p_out, abort_callback& p_abort) {
418 auto cb = fb2k::service_new<plcallback_simple>();
419 g_process_path(p_path, cb, p_abort);
420 p_out = cb->m_items;
421 }
422 void playlist_loader::g_process_path(const char * p_filename,playlist_loader_callback::ptr callback, abort_callback & abort,playlist_loader_callback::t_entry_type type)
423 {
424 TRACK_CALL_TEXT("playlist_loader::g_process_path");
425
426 auto filename = file_path_canonical(p_filename);
427
428 process_path_internal(filename,0,callback,abort, type,filestats_invalid, allowRecurseBase);
429 }
430
431 void playlist_loader::g_save_playlist(const char * p_filename,const pfc::list_base_const_t<metadb_handle_ptr> & data,abort_callback & p_abort)
432 {
433 TRACK_CALL_TEXT("playlist_loader::g_save_playlist");
434 pfc::string8 filename;
435 filesystem::g_get_canonical_path(p_filename,filename);
436 try {
437 service_ptr_t<file> r;
438 filesystem::g_open(r,filename,filesystem::open_mode_write_new,p_abort);
439
440 auto ext = pfc::string_extension(filename);
441
442 service_enum_t<playlist_loader> e;
443 service_ptr_t<playlist_loader> l;
444 if (e.first(l)) do {
445 if (l->can_write() && !stricmp_utf8(ext,l->get_extension())) {
446 try {
447 TRACK_CODE("playlist_loader::write",l->write(filename,r,data,p_abort));
448 return;
449 } catch(exception_io_data const &) {}
450 }
451 } while(e.next(l));
452 throw exception_io_data();
453 } catch(...) {
454 try {filesystem::g_remove(filename,p_abort);} catch(...) {}
455 throw;
456 }
457 }
458
459
460 bool playlist_loader::g_process_path_ex(const char * filename,playlist_loader_callback::ptr callback, abort_callback & abort,playlist_loader_callback::t_entry_type type)
461 {
462 if (g_try_load_playlist(NULL, filename, callback, abort)) return true;
463 //not a playlist format
464 g_process_path(filename,callback,abort,type);
465 return false;
466 }