foobar2000 SDK  2015-08-03
filesystem.h
Go to the documentation of this file.
1 class file_info;
2 
4 
5 namespace foobar2000_io
6 {
14  const t_filetimestamp filetimestamp_invalid = 0;
16  static const t_filesize filesize_invalid = (t_filesize)(~0);
17 
18  static const t_filetimestamp filetimestamp_1second_increment = 10000000;
19 
21  PFC_DECLARE_EXCEPTION(exception_io, pfc::exception,"I/O error");
23  PFC_DECLARE_EXCEPTION(exception_io_not_found, exception_io,"Object not found");
25  PFC_DECLARE_EXCEPTION(exception_io_denied, exception_io,"Access denied");
27  PFC_DECLARE_EXCEPTION(exception_io_denied_readonly, exception_io_denied,"File is read-only");
29  PFC_DECLARE_EXCEPTION(exception_io_data, exception_io,"Unsupported format or corrupted file");
31  PFC_DECLARE_EXCEPTION(exception_io_data_truncation, exception_io_data,"Unsupported format or corrupted file");
33  PFC_DECLARE_EXCEPTION(exception_io_unsupported_format, exception_io_data,"Unsupported file format");
35  PFC_DECLARE_EXCEPTION(exception_io_object_is_remote, exception_io,"This operation is not supported on remote objects");
37  PFC_DECLARE_EXCEPTION(exception_io_sharing_violation, exception_io,"File is already in use");
39  PFC_DECLARE_EXCEPTION(exception_io_device_full, exception_io,"Device full");
41  PFC_DECLARE_EXCEPTION(exception_io_seek_out_of_range, exception_io,"Seek offset out of range");
43  PFC_DECLARE_EXCEPTION(exception_io_object_not_seekable, exception_io,"Object is not seekable");
45  PFC_DECLARE_EXCEPTION(exception_io_no_length, exception_io,"Length of object is unknown");
47  PFC_DECLARE_EXCEPTION(exception_io_no_handler_for_path, exception_io,"Invalid path");
49  PFC_DECLARE_EXCEPTION(exception_io_already_exists, exception_io,"Object already exists");
51  PFC_DECLARE_EXCEPTION(exception_io_no_data, exception_io,"The process receiving or sending data has terminated");
53  PFC_DECLARE_EXCEPTION(exception_io_network_not_reachable,exception_io,"Network not reachable");
55  PFC_DECLARE_EXCEPTION(exception_io_write_protected, exception_io_denied,"The media is write protected");
57  PFC_DECLARE_EXCEPTION(exception_io_file_corrupted, exception_io,"The file is corrupted");
59  PFC_DECLARE_EXCEPTION(exception_io_disk_change, exception_io,"Disc not available");
61  PFC_DECLARE_EXCEPTION(exception_io_directory_not_empty, exception_io,"Directory not empty");
63  PFC_DECLARE_EXCEPTION( exception_io_net, exception_io, "Connection error");
65  PFC_DECLARE_EXCEPTION( exception_io_dns, exception_io_net, "DNS error");
66 
67 
69  struct t_filestats {
71  t_filesize m_size;
73  t_filetimestamp m_timestamp;
74 
75  inline bool operator==(const t_filestats & param) const {return m_size == param.m_size && m_timestamp == param.m_timestamp;}
76  inline bool operator!=(const t_filestats & param) const {return m_size != param.m_size || m_timestamp != param.m_timestamp;}
77  };
78 
80  static const t_filestats filestats_invalid = {filesize_invalid,filetimestamp_invalid};
81 
82 #ifdef _WIN32
83  PFC_NORETURN void exception_io_from_win32(DWORD p_code);
84 #define WIN32_IO_OP(X) {SetLastError(NO_ERROR); if (!(X)) exception_io_from_win32(GetLastError());}
85 
86  // SPECIAL WORKAROUND: throw "file is read-only" rather than "access denied" where appropriate
87  PFC_NORETURN void win32_file_write_failure(DWORD p_code, const char * path);
88 #endif
89 
92  class NOVTABLE stream_reader {
93  public:
99  virtual t_size read(void * p_buffer,t_size p_bytes,abort_callback & p_abort) = 0;
104  virtual void read_object(void * p_buffer,t_size p_bytes,abort_callback & p_abort);
109  virtual t_filesize skip(t_filesize p_bytes,abort_callback & p_abort);
113  virtual void skip_object(t_filesize p_bytes,abort_callback & p_abort);
114 
118  template<typename T> inline void read_object_t(T& p_object,abort_callback & p_abort) {pfc::assert_raw_type<T>(); read_object(&p_object,sizeof(p_object),p_abort);}
122  template<typename T> inline void read_lendian_t(T& p_object,abort_callback & p_abort) {read_object_t(p_object,p_abort); byte_order::order_le_to_native_t(p_object);}
126  template<typename T> inline void read_bendian_t(T& p_object,abort_callback & p_abort) {read_object_t(p_object,p_abort); byte_order::order_be_to_native_t(p_object);}
127 
129  void read_string(pfc::string_base & p_out,abort_callback & p_abort);
131  void read_string_raw(pfc::string_base & p_out,abort_callback & p_abort);
133  pfc::string read_string(abort_callback & p_abort);
134 
136  void read_string_ex(pfc::string_base & p_out,t_size p_bytes,abort_callback & p_abort);
138  pfc::string read_string_ex(t_size p_len,abort_callback & p_abort);
139 
140  void read_string_nullterm( pfc::string_base & out, abort_callback & abort );
141 
142  t_filesize skip_till_eof(abort_callback & abort);
143 
144  template<typename t_outArray>
145  void read_till_eof(t_outArray & out, abort_callback & abort) {
146  pfc::assert_raw_type<typename t_outArray::t_item>();
147  const t_size itemWidth = sizeof(typename t_outArray::t_item);
148  out.set_size(pfc::max_t<t_size>(1,256 / itemWidth)); t_size done = 0;
149  for(;;) {
150  t_size delta = out.get_size() - done;
151  t_size delta2 = read(out.get_ptr() + done, delta * itemWidth, abort ) / itemWidth;
152  done += delta2;
153  if (delta2 != delta) break;
154  out.set_size(out.get_size() << 1);
155  }
156  out.set_size(done);
157  }
158  protected:
161  };
162 
163 
166  class NOVTABLE stream_writer {
167  public:
172  virtual void write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) = 0;
173 
175  inline void write_object(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {write(p_buffer,p_bytes,p_abort);}
176 
180  template<typename T> inline void write_object_t(const T & p_object, abort_callback & p_abort) {pfc::assert_raw_type<T>(); write_object(&p_object,sizeof(p_object),p_abort);}
184  template<typename T> inline void write_lendian_t(const T & p_object, abort_callback & p_abort) {T temp = p_object; byte_order::order_native_to_le_t(temp); write_object_t(temp,p_abort);}
188  template<typename T> inline void write_bendian_t(const T & p_object, abort_callback & p_abort) {T temp = p_object; byte_order::order_native_to_be_t(temp); write_object_t(temp,p_abort);}
189 
191  void write_string(const char * p_string,abort_callback & p_abort);
192  void write_string(const char * p_string,t_size p_len,abort_callback & p_abort);
193 
194  template<typename T>
195  void write_string(const T& val,abort_callback & p_abort) {write_string(pfc::stringToPtr(val),p_abort);}
196 
198  void write_string_raw(const char * p_string,abort_callback & p_abort);
199 
200  void write_string_nullterm( const char * p_string, abort_callback & p_abort) {this->write( p_string, strlen(p_string)+1, p_abort); }
201  protected:
204  };
205 
208  class NOVTABLE file : public service_base, public stream_reader, public stream_writer {
209  public:
210 
212  enum t_seek_mode {
214  seek_from_beginning = 0,
216  seek_from_current = 1,
218  seek_from_eof = 2,
219  };
220 
224  virtual t_filesize get_size(abort_callback & p_abort) = 0;
225 
226 
230  virtual t_filesize get_position(abort_callback & p_abort) = 0;
231 
234  virtual void resize(t_filesize p_size,abort_callback & p_abort) = 0;
235 
239  virtual void seek(t_filesize p_position,abort_callback & p_abort) = 0;
240 
242  void seek_probe(t_filesize p_position, abort_callback & p_abort);
243 
248  virtual void seek_ex(t_sfilesize p_position,t_seek_mode p_mode,abort_callback & p_abort);
249 
251  virtual bool can_seek() = 0;
252 
255  virtual bool get_content_type(pfc::string_base & p_out) = 0;
256 
258  virtual bool is_in_memory() {return false;}
259 
262  virtual void on_idle(abort_callback & p_abort) {}
263 
267  virtual t_filetimestamp get_timestamp(abort_callback & p_abort) {return filetimestamp_invalid;}
268 
271  virtual void reopen(abort_callback & p_abort) = 0;
272 
274  virtual bool is_remote() = 0;
275 
277  t_filestats get_stats(abort_callback & p_abort);
278 
280  bool is_eof(abort_callback & p_abort);
281 
283  void truncate(t_filesize p_position,abort_callback & p_abort);
284 
286  void set_eof(abort_callback & p_abort) {resize(get_position(p_abort),p_abort);}
287 
288 
290  t_filesize get_size_ex(abort_callback & p_abort);
291 
293  t_filesize get_remaining(abort_callback & p_abort);
294 
296  void probe_remaining(t_filesize bytes, abort_callback & p_abort);
297 
299  void ensure_seekable();
300 
302  void ensure_local();
303 
306  static t_filesize g_transfer(stream_reader * src,stream_writer * dst,t_filesize bytes,abort_callback & p_abort);
308  static void g_transfer_object(stream_reader * src,stream_writer * dst,t_filesize bytes,abort_callback & p_abort);
310  static void g_transfer_file(const service_ptr_t<file> & p_from,const service_ptr_t<file> & p_to,abort_callback & p_abort);
311 
313  static t_filesize g_transfer(service_ptr_t<file> p_src,service_ptr_t<file> p_dst,t_filesize p_bytes,abort_callback & p_abort);
315  static void g_transfer_object(service_ptr_t<file> p_src,service_ptr_t<file> p_dst,t_filesize p_bytes,abort_callback & p_abort);
316 
317 
318  t_filesize skip(t_filesize p_bytes,abort_callback & p_abort);
319  t_filesize skip_seek(t_filesize p_bytes,abort_callback & p_abort);
320 
321  FB2K_MAKE_SERVICE_INTERFACE(file,service_base);
322  };
323 
325 
327  class file_dynamicinfo : public file {
328  FB2K_MAKE_SERVICE_INTERFACE(file_dynamicinfo,file);
329  public:
331  virtual bool get_static_info(class file_info & p_out) = 0;
333  virtual bool is_dynamic_info_enabled()=0;
335  virtual bool get_dynamic_info(class file_info & p_out) = 0;
336  };
337 
339  class file_cached : public file {
340  FB2K_MAKE_SERVICE_INTERFACE(file_cached, file);
341  public:
342  virtual size_t get_cache_block_size() = 0;
343  virtual void suggest_grow_cache(size_t suggestSize) = 0;
344 
345  static file::ptr g_create(service_ptr_t<file> p_base,abort_callback & p_abort, t_size blockSize);
346  static void g_create(service_ptr_t<file> & p_out,service_ptr_t<file> p_base,abort_callback & p_abort, t_size blockSize);
347 
348  static void g_decodeInitCache(file::ptr & theFile, abort_callback & abort, size_t blockSize);
349  };
350 
352  template<typename t_base> class file_readonly_t : public t_base {
353  public:
354  void resize(t_filesize p_size,abort_callback & p_abort) {throw exception_io_denied();}
355  void write(const void * p_buffer,t_size p_bytes,abort_callback & p_abort) {throw exception_io_denied();}
356  };
358 
359  class file_streamstub : public file_readonly {
360  public:
361  t_size read(void * p_buffer,t_size p_bytes,abort_callback & p_abort) {return 0;}
362  t_filesize get_size(abort_callback & p_abort) {return filesize_invalid;}
363  t_filesize get_position(abort_callback & p_abort) {return 0;}
364  bool get_content_type(pfc::string_base & p_out) {return false;}
365  bool is_remote() {return true;}
367  void seek(t_filesize p_position,abort_callback & p_abort) {throw exception_io_object_not_seekable();}
368  bool can_seek() {return false;}
369  };
370 
371  class filesystem;
372 
373  class NOVTABLE directory_callback {
374  public:
376  virtual bool on_entry(filesystem * p_owner,abort_callback & p_abort,const char * p_url,bool p_is_subdirectory,const t_filestats & p_stats)=0;
377  };
378 
379 
383  class NOVTABLE filesystem : public service_base {
384  FB2K_MAKE_SERVICE_INTERFACE_ENTRYPOINT(filesystem);
385  public:
387  enum t_open_mode {
394  };
395 
396  virtual bool get_canonical_path(const char * p_path,pfc::string_base & p_out)=0;
397  virtual bool is_our_path(const char * p_path)=0;
398  virtual bool get_display_path(const char * p_path,pfc::string_base & p_out)=0;
399 
400  virtual void open(service_ptr_t<file> & p_out,const char * p_path, t_open_mode p_mode,abort_callback & p_abort)=0;
401  virtual void remove(const char * p_path,abort_callback & p_abort)=0;
402  virtual void move(const char * p_src,const char * p_dst,abort_callback & p_abort)=0;
404  virtual bool is_remote(const char * p_src) = 0;
405 
407  virtual void get_stats(const char * p_path,t_filestats & p_stats,bool & p_is_writeable,abort_callback & p_abort) = 0;
408 
409  virtual bool relative_path_create(const char * file_path,const char * playlist_path,pfc::string_base & out) {return false;}
410  virtual bool relative_path_parse(const char * relative_path,const char * playlist_path,pfc::string_base & out) {return false;}
411 
413  virtual void create_directory(const char * p_path,abort_callback & p_abort) = 0;
414 
415  virtual void list_directory(const char * p_path,directory_callback & p_out,abort_callback & p_abort)=0;
416 
419  virtual bool supports_content_types() = 0;
420 
421  static void g_get_canonical_path(const char * path,pfc::string_base & out);
422  static void g_get_display_path(const char * path,pfc::string_base & out);
425  static bool g_get_native_path( const char * path, pfc::string_base & out);
426 
427  static bool g_get_interface(service_ptr_t<filesystem> & p_out,const char * path);//path is AFTER get_canonical_path
428  static filesystem::ptr g_get_interface(const char * path);// throws exception_io_no_handler_for_path on failure
429  static bool g_is_remote(const char * p_path);//path is AFTER get_canonical_path
430  static bool g_is_recognized_and_remote(const char * p_path);//path is AFTER get_canonical_path
431  static bool g_is_remote_safe(const char * p_path) {return g_is_recognized_and_remote(p_path);}
432  static bool g_is_remote_or_unrecognized(const char * p_path);
433  static bool g_is_recognized_path(const char * p_path);
434 
436  static void g_open(service_ptr_t<file> & p_out,const char * p_path,t_open_mode p_mode,abort_callback & p_abort);
438  static void g_open_timeout(service_ptr_t<file> & p_out,const char * p_path,t_open_mode p_mode,double p_timeout,abort_callback & p_abort);
439  static void g_open_write_new(service_ptr_t<file> & p_out,const char * p_path,abort_callback & p_abort);
440  static void g_open_read(service_ptr_t<file> & p_out,const char * path,abort_callback & p_abort) {return g_open(p_out,path,open_mode_read,p_abort);}
441  static void g_open_precache(service_ptr_t<file> & p_out,const char * path,abort_callback & p_abort);//open only for precaching data (eg. will fail on http etc)
442  static bool g_exists(const char * p_path,abort_callback & p_abort);
443  static bool g_exists_writeable(const char * p_path,abort_callback & p_abort);
445  static void g_remove(const char * p_path,abort_callback & p_abort);
447  static void g_remove_timeout(const char * p_path,double p_timeout,abort_callback & p_abort);
449  static void g_move(const char * p_src,const char * p_dst,abort_callback & p_abort);
451  static void g_move_timeout(const char * p_src,const char * p_dst,double p_timeout,abort_callback & p_abort);
452 
453  static void g_link(const char * p_src,const char * p_dst,abort_callback & p_abort);
454  static void g_link_timeout(const char * p_src,const char * p_dst,double p_timeout,abort_callback & p_abort);
455 
456  static void g_copy(const char * p_src,const char * p_dst,abort_callback & p_abort);//needs canonical path
457  static void g_copy_timeout(const char * p_src,const char * p_dst,double p_timeout,abort_callback & p_abort);//needs canonical path
458  static void g_copy_directory(const char * p_src,const char * p_dst,abort_callback & p_abort);//needs canonical path
459  static void g_get_stats(const char * p_path,t_filestats & p_stats,bool & p_is_writeable,abort_callback & p_abort);
460  static bool g_relative_path_create(const char * p_file_path,const char * p_playlist_path,pfc::string_base & out);
461  static bool g_relative_path_parse(const char * p_relative_path,const char * p_playlist_path,pfc::string_base & out);
462 
463  static void g_create_directory(const char * p_path,abort_callback & p_abort);
464 
466  static FILE * streamio_open(const char * p_path,const char * p_flags);
467 
468  static void g_open_temp(service_ptr_t<file> & p_out,abort_callback & p_abort);
469  static void g_open_tempmem(service_ptr_t<file> & p_out,abort_callback & p_abort);
470  static file::ptr g_open_tempmem();
471 
472  static void g_list_directory(const char * p_path,directory_callback & p_out,abort_callback & p_abort);// path must be canonical
473 
474  static bool g_is_valid_directory(const char * path,abort_callback & p_abort);
475  static bool g_is_empty_directory(const char * path,abort_callback & p_abort);
476 
477  void remove_object_recur(const char * path, abort_callback & abort);
478  void remove_directory_content(const char * path, abort_callback & abort);
479  static void g_remove_object_recur(const char * path, abort_callback & abort);
480  static void g_remove_object_recur_timeout(const char * path, double timeout, abort_callback & abort);
481 
482  // Presumes both source and destination belong to this filesystem.
483  void copy_directory(const char * p_src, const char * p_dst, abort_callback & p_abort);
484  };
485 
487  {
488  struct t_entry
489  {
492  t_entry(const char * p_path, const t_filestats & p_stats) : m_path(p_path), m_stats(p_stats) {}
493  };
494 
495 
497  bool m_recur;
498 
499  static int sortfunc(const pfc::rcptr_t<const t_entry> & p1, const pfc::rcptr_t<const t_entry> & p2) {return pfc::io::path::compare(p1->m_path,p2->m_path);}
500  public:
501  bool on_entry(filesystem * owner,abort_callback & p_abort,const char * url,bool is_subdirectory,const t_filestats & p_stats);
502 
503  directory_callback_impl(bool p_recur) : m_recur(p_recur) {}
504  t_size get_count() {return m_data.get_count();}
505  const char * operator[](t_size n) const {return m_data[n]->m_path;}
506  const char * get_item(t_size n) const {return m_data[n]->m_path;}
507  const t_filestats & get_item_stats(t_size n) const {return m_data[n]->m_stats;}
508  void sort() {m_data.sort_t(sortfunc);}
509  };
510 
511  class archive;
512 
513  class NOVTABLE archive_callback : public abort_callback {
514  public:
515  virtual bool on_entry(archive * owner,const char * url,const t_filestats & p_stats,const service_ptr_t<file> & p_reader) = 0;
516  };
517 
519  class NOVTABLE archive : public filesystem {
520  public:
521  virtual void archive_list(const char * p_path,const service_ptr_t<file> & p_reader,archive_callback & p_callback,bool p_want_readers) = 0;
522 
523  FB2K_MAKE_SERVICE_INTERFACE(archive,filesystem);
524  };
525 
527  class NOVTABLE archive_impl : public archive {
528  private:
529  //do not override these
530  bool get_canonical_path(const char * path,pfc::string_base & out);
531  bool is_our_path(const char * path);
532  bool get_display_path(const char * path,pfc::string_base & out);
533  void remove(const char * path,abort_callback & p_abort);
534  void move(const char * src,const char * dst,abort_callback & p_abort);
535  bool is_remote(const char * src);
536  bool relative_path_create(const char * file_path,const char * playlist_path,pfc::string_base & out);
537  bool relative_path_parse(const char * relative_path,const char * playlist_path,pfc::string_base & out);
538  void open(service_ptr_t<file> & p_out,const char * path, t_open_mode mode,abort_callback & p_abort);
539  void create_directory(const char * path,abort_callback &);
540  void list_directory(const char * p_path,directory_callback & p_out,abort_callback & p_abort);
541  void get_stats(const char * p_path,t_filestats & p_stats,bool & p_is_writeable,abort_callback & p_abort);
542  protected:
543  //override these
544  virtual const char * get_archive_type()=0;//eg. "zip", must be lowercase
545  virtual t_filestats get_stats_in_archive(const char * p_archive,const char * p_file,abort_callback & p_abort) = 0;
546  virtual void open_archive(service_ptr_t<file> & p_out,const char * archive,const char * file, abort_callback & p_abort)=0;//opens for reading
547  public:
548  //override these
549  virtual void archive_list(const char * path,const service_ptr_t<file> & p_reader,archive_callback & p_out,bool p_want_readers)=0;
550 
551 
552  static bool g_is_unpack_path(const char * path);
553  static bool g_parse_unpack_path(const char * path,pfc::string_base & archive,pfc::string_base & file);
554  static bool g_parse_unpack_path_ex(const char * path,pfc::string_base & archive,pfc::string_base & file, pfc::string_base & type);
555  static void g_make_unpack_path(pfc::string_base & path,const char * archive,const char * file,const char * type);
556  void make_unpack_path(pfc::string_base & path,const char * archive,const char * file);
557 
558 
559  };
560 
561  template<typename T>
563 
564 
565  t_filetimestamp filetimestamp_from_system_timer();
566 
567 #ifdef _WIN32
568  inline t_filetimestamp import_filetimestamp(FILETIME ft) {
569  return *reinterpret_cast<t_filetimestamp*>(&ft);
570  }
571 #endif
572 
573  void generate_temp_location_for_file(pfc::string_base & p_out, const char * p_origpath,const char * p_extension,const char * p_magic);
574 
575 
576  inline file_ptr fileOpen(const char * p_path,filesystem::t_open_mode p_mode,abort_callback & p_abort,double p_timeout) {
577  file_ptr temp; filesystem::g_open_timeout(temp,p_path,p_mode,p_timeout,p_abort); PFC_ASSERT(temp.is_valid()); return temp;
578  }
579 
580  inline file_ptr fileOpenReadExisting(const char * p_path,abort_callback & p_abort,double p_timeout = 0) {
581  return fileOpen(p_path,filesystem::open_mode_read,p_abort,p_timeout);
582  }
583  inline file_ptr fileOpenWriteExisting(const char * p_path,abort_callback & p_abort,double p_timeout = 0) {
584  return fileOpen(p_path,filesystem::open_mode_write_existing,p_abort,p_timeout);
585  }
586  inline file_ptr fileOpenWriteNew(const char * p_path,abort_callback & p_abort,double p_timeout = 0) {
587  return fileOpen(p_path,filesystem::open_mode_write_new,p_abort,p_timeout);
588  }
589 
590  template<typename t_list>
592  public:
593  directory_callback_retrieveList(t_list & p_list,bool p_getFiles,bool p_getSubDirectories) : m_list(p_list), m_getFiles(p_getFiles), m_getSubDirectories(p_getSubDirectories) {}
594  bool on_entry(filesystem * p_owner,abort_callback & p_abort,const char * p_url,bool p_is_subdirectory,const t_filestats & p_stats) {
595  p_abort.check();
596  if (p_is_subdirectory ? m_getSubDirectories : m_getFiles) {
597  m_list.add_item(p_url);
598  }
599  return true;
600  }
601  private:
603  const bool m_getFiles;
604  t_list & m_list;
605  };
606  template<typename t_list>
608  public:
609  directory_callback_retrieveListEx(t_list & p_files, t_list & p_directories) : m_files(p_files), m_directories(p_directories) {}
610  bool on_entry(filesystem * p_owner,abort_callback & p_abort,const char * p_url,bool p_is_subdirectory,const t_filestats & p_stats) {
611  p_abort.check();
612  if (p_is_subdirectory) m_directories += p_url;
613  else m_files += p_url;
614  return true;
615  }
616  private:
617  t_list & m_files;
618  t_list & m_directories;
619  };
620  template<typename t_list> class directory_callback_retrieveListRecur : public directory_callback {
621  public:
622  directory_callback_retrieveListRecur(t_list & p_list) : m_list(p_list) {}
623  bool on_entry(filesystem * owner,abort_callback & p_abort,const char * path, bool isSubdir, const t_filestats&) {
624  if (isSubdir) {
625  try { owner->list_directory(path,*this,p_abort); } catch(exception_io) {}
626  } else {
627  m_list.add_item(path);
628  }
629  return true;
630  }
631  private:
632  t_list & m_list;
633  };
634 
635  template<typename t_list>
636  static void listFiles(const char * p_path,t_list & p_out,abort_callback & p_abort) {
637  directory_callback_retrieveList<t_list> callback(p_out,true,false);
638  filesystem::g_list_directory(p_path,callback,p_abort);
639  }
640  template<typename t_list>
641  static void listDirectories(const char * p_path,t_list & p_out,abort_callback & p_abort) {
642  directory_callback_retrieveList<t_list> callback(p_out,false,true);
643  filesystem::g_list_directory(p_path,callback,p_abort);
644  }
645  template<typename t_list>
646  static void listFilesAndDirectories(const char * p_path,t_list & p_files,t_list & p_directories,abort_callback & p_abort) {
647  directory_callback_retrieveListEx<t_list> callback(p_files,p_directories);
648  filesystem::g_list_directory(p_path,callback,p_abort);
649  }
650  template<typename t_list>
651  static void listFilesRecur(const char * p_path,t_list & p_out,abort_callback & p_abort) {
653  filesystem::g_list_directory(p_path,callback,p_abort);
654  }
655 
656  bool extract_native_path(const char * p_fspath,pfc::string_base & p_native);
657  bool _extract_native_path_ptr(const char * & p_fspath);
658  bool is_native_filesystem( const char * p_fspath );
659  bool extract_native_path_ex(const char * p_fspath, pfc::string_base & p_native);//prepends \\?\ where needed
660 
661  template<typename T>
662  pfc::string getPathDisplay(const T& source) {
665  return temp.toString();
666  }
667  template<typename T>
668  pfc::string getPathCanonical(const T& source) {
671  return temp.toString();
672  }
673 
674 
675  bool matchContentType(const char * fullString, const char * ourType);
676  bool matchProtocol(const char * fullString, const char * protocolName);
677  void substituteProtocol(pfc::string_base & out, const char * fullString, const char * protocolName);
678 
679  void purgeOldFiles(const char * directory, t_filetimestamp period, abort_callback & abort);
680 }
681 
682 using namespace foobar2000_io;
683 
684 #include "filesystem_helper.h"
void read(const service_ptr_t< file > &p_file, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
void write_string_nullterm(const char *p_string, abort_callback &p_abort)
Definition: filesystem.h:200
t_filesize get_size(HANDLE p_handle)
bool matchContentType(const char *fullString, const char *ourType)
service_ptr_t< file > file_ptr
Definition: filesystem.h:324
static void listFilesRecur(const char *p_path, t_list &p_out, abort_callback &p_abort)
Definition: filesystem.h:651
t_seek_mode
Seeking mode constants. Note: these are purposedly defined to same values as standard C SEEK_* consta...
Definition: filesystem.h:212
virtual void on_idle(abort_callback &p_abort)
Optional, called by owner thread before sleeping.
Definition: filesystem.h:262
t_int64 t_sfilesize
Type used for file size related variables when a signed value is needed.
Definition: filesystem.h:10
t_filesize m_size
Size of the file.
Definition: filesystem.h:71
void set_eof(abort_callback &p_abort)
Truncates the file at current read/write cursor position.
Definition: filesystem.h:286
const char * stringToPtr(T const &val)
Definition: string_base.h:1020
t_filetimestamp filetimestamp_from_system_timer()
Definition: filesystem.cpp:684
A class providing abstraction for an open file object, with reading/writing/seeking methods...
Definition: filesystem.h:208
static const t_filetimestamp filetimestamp_1second_increment
Definition: filesystem.h:18
PFC_NORETURN void win32_file_write_failure(DWORD p_code, const char *path)
Definition: filesystem.cpp:765
Opens an existing file for writing; if the file does not exist, the operation will fail...
Definition: filesystem.h:391
This class is used to signal underlying worker code whether user has decided to abort a potentially t...
void seek(HANDLE p_handle, t_sfilesize p_position, file::t_seek_mode p_mode)
bool get_content_type(pfc::string_base &p_out)
Definition: filesystem.h:364
virtual bool relative_path_create(const char *file_path, const char *playlist_path, pfc::string_base &out)
Definition: filesystem.h:409
pfc::string_simple m_path
Definition: filesystem.h:490
uint64_t t_uint64
Definition: int_types.h:3
static bool g_is_remote_safe(const char *p_path)
Definition: filesystem.h:431
static void g_get_canonical_path(const char *path, pfc::string_base &out)
Definition: filesystem.cpp:74
const char * get_item(t_size n) const
Definition: filesystem.h:506
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
bool extract_native_path_ex(const char *p_fspath, pfc::string_base &p_native)
Definition: filesystem.cpp:954
bool operator==(const t_filestats &param) const
Definition: filesystem.h:75
int compare(t1 const &p1, t2 const &p2)
Definition: pathUtils.h:29
t_filesize get_position(abort_callback &p_abort)
Definition: filesystem.h:363
pfc::string getPathDisplay(const T &source)
Definition: filesystem.h:662
bool _extract_native_path_ptr(const char *&p_fspath)
Definition: filesystem.cpp:942
void read_bendian_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream; corrects byte orde...
Definition: filesystem.h:126
t_size read(void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Definition: filesystem.h:361
const char * operator[](t_size n) const
Definition: filesystem.h:505
void order_native_to_le_t(T &param)
static int sortfunc(const pfc::rcptr_t< const t_entry > &p1, const pfc::rcptr_t< const t_entry > &p2)
Definition: filesystem.h:499
static const t_filesize filesize_invalid
Invalid/unknown file size constant. Also see: t_filesize.
Definition: filesystem.h:16
void check() const
Checks if user has requested the operation to be aborted, and throws exception_aborted if so...
static void listDirectories(const char *p_path, t_list &p_out, abort_callback &p_abort)
Definition: filesystem.h:641
bool is_native_filesystem(const char *p_fspath)
Definition: filesystem.cpp:938
static void g_open_read(service_ptr_t< file > &p_out, const char *path, abort_callback &p_abort)
Definition: filesystem.h:440
static void g_open_timeout(service_ptr_t< file > &p_out, const char *p_path, t_open_mode p_mode, double p_timeout, abort_callback &p_abort)
Attempts to open file at specified path; if the operation fails with sharing violation error...
Definition: filesystem.cpp:143
void order_be_to_native_t(T &param)
Extension for cached file access - allows callers to know that they&#39;re dealing with a cache layer...
Definition: filesystem.h:339
PFC_DECLARE_EXCEPTION(exception_aborted, pfc::exception,"User abort")
Extension for shoutcast dynamic metadata handling.
Definition: filesystem.h:327
directory_callback_retrieveList(t_list &p_list, bool p_getFiles, bool p_getSubDirectories)
Definition: filesystem.h:593
static const t_filestats filestats_invalid
Invalid/unknown file stats constant. See: t_filestats.
Definition: filesystem.h:80
t_open_mode
Enumeration specifying how to open a file. See: filesystem::open(), filesystem::g_open().
Definition: filesystem.h:387
file_readonly_t< file > file_readonly
Definition: filesystem.h:357
file_ptr fileOpenWriteNew(const char *p_path, abort_callback &p_abort, double p_timeout=0)
Definition: filesystem.h:586
bool is_valid() const
Definition: service.h:119
virtual bool is_in_memory()
Hint, returns whether the file is already fully buffered into memory.
Definition: filesystem.h:258
Generic interface to read data from a nonseekable stream. Also see: stream_writer, file. Error handling: all methods may throw exception_io or one of derivatives on failure; exception_aborted when abort_callback is signaled.
Definition: filesystem.h:92
pfc::list_t< pfc::rcptr_t< t_entry > > m_data
Definition: filesystem.h:496
Interface for archive reader services. When implementing, derive from archive_impl rather than from d...
Definition: filesystem.h:519
static void g_list_directory(const char *p_path, directory_callback &p_out, abort_callback &p_abort)
Definition: filesystem.cpp:210
const t_filetimestamp filetimestamp_invalid
Invalid/unknown file timestamp constant. Also see: t_filetimestamp.
Definition: filesystem.h:14
Contains various I/O related structures and interfaces.
Definition: filetimetools.h:1
Main interface class for information about some playable object.
Definition: file_info.h:73
void read_lendian_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream; corrects byte orde...
Definition: filesystem.h:122
t_filesize get_size(abort_callback &p_abort)
Definition: filesystem.h:362
void order_le_to_native_t(T &param)
string_simple_t< char > string_simple
Definition: string_base.h:826
const t_filestats & get_item_stats(t_size n) const
Definition: filesystem.h:507
size_t t_size
Definition: int_types.h:48
string8_fastalloc string_formatter
Definition: string_base.h:615
bool on_entry(filesystem *p_owner, abort_callback &p_abort, const char *p_url, bool p_is_subdirectory, const t_filestats &p_stats)
Definition: filesystem.h:594
t_entry(const char *p_path, const t_filestats &p_stats)
Definition: filesystem.h:492
bool matchProtocol(const char *fullString, const char *protocolName)
void seek(t_filesize p_position, abort_callback &p_abort)
Definition: filesystem.h:367
Generic interface to write data to a nonseekable stream. Also see: stream_reader, file...
Definition: filesystem.h:166
Base class for all service classes. Provides interfaces for reference counter and querying for differ...
Definition: service.h:333
t_filetimestamp m_timestamp
Time of last file modification.
Definition: filesystem.h:73
bool extract_native_path(const char *p_fspath, pfc::string_base &p_native)
Definition: filesystem.cpp:948
Opens an existing file for reading; if the file does not exist, the operation will fail...
Definition: filesystem.h:389
void write(const void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Definition: filesystem.h:355
void purgeOldFiles(const char *directory, t_filetimestamp period, abort_callback &abort)
Root class for archive implementations. Derive from this instead of from archive directly.
Definition: filesystem.h:527
bool on_entry(filesystem *owner, abort_callback &p_abort, const char *path, bool isSubdir, const t_filestats &)
Definition: filesystem.h:623
void write(const service_ptr_t< file > &p_file, abort_callback &p_abort, const char *p_string, bool is_utf8)
void generate_temp_location_for_file(pfc::string_base &p_out, const char *p_origpath, const char *p_extension, const char *p_magic)
Definition: filesystem.cpp:892
void read_till_eof(t_outArray &out, abort_callback &abort)
Definition: filesystem.h:145
virtual bool relative_path_parse(const char *relative_path, const char *playlist_path, pfc::string_base &out)
Definition: filesystem.h:410
void substituteProtocol(pfc::string_base &out, const char *fullString, const char *protocolName)
void resize(t_filesize p_size, abort_callback &p_abort)
Definition: filesystem.h:354
Entrypoint service for all filesystem operations. Implementation: standard implementations for local ...
Definition: filesystem.h:383
void write_lendian_t(const T &p_object, abort_callback &p_abort)
Helper template. Writes single raw object to the stream; corrects byte order assuming stream uses lit...
Definition: filesystem.h:184
t_uint64 t_filetimestamp
Type used for file timestamp related variables. 64-bit value representing the number of 100-nanosecon...
Definition: filesystem.h:12
virtual void list_directory(const char *p_path, directory_callback &p_out, abort_callback &p_abort)=0
bool on_entry(filesystem *p_owner, abort_callback &p_abort, const char *p_url, bool p_is_subdirectory, const t_filestats &p_stats)
Definition: filesystem.h:610
bool operator!=(const t_filestats &param) const
Definition: filesystem.h:76
void write_bendian_t(const T &p_object, abort_callback &p_abort)
Helper template. Writes single raw object to the stream; corrects byte order assuming stream uses big...
Definition: filesystem.h:188
static void g_get_display_path(const char *path, pfc::string_base &out)
Definition: filesystem.cpp:87
std::exception exception
Definition: primitives.h:193
static void listFilesAndDirectories(const char *p_path, t_list &p_files, t_list &p_directories, abort_callback &p_abort)
Definition: filesystem.h:646
Definition: filesystem.h:488
t_filetimestamp import_filetimestamp(FILETIME ft)
Definition: filesystem.h:568
t_uint64 t_filesize
Type used for file size related variables.
Definition: filesystem.h:8
void write_object_t(const T &p_object, abort_callback &p_abort)
Helper template. Writes single raw object to the stream.
Definition: filesystem.h:180
file_ptr fileOpenReadExisting(const char *p_path, abort_callback &p_abort, double p_timeout=0)
Definition: filesystem.h:580
Implementation helper - contains dummy implementations of methods that modify the file...
Definition: filesystem.h:352
New EXPERIMENTAL string class, allowing efficient copies and returning from functions. Does not implement the string_base interface so you still need string8 in many cases. Safe to pass between DLLs, but since a reference is used, objects possibly created by other DLLs must be released before owning DLLs are unloaded.
Definition: stringNew.h:19
virtual t_filetimestamp get_timestamp(abort_callback &p_abort)
Retrieves last modification time of the file.
Definition: filesystem.h:267
void write_string(const T &val, abort_callback &p_abort)
Definition: filesystem.h:195
Stores file stats (size and timestamp).
Definition: filesystem.h:69
void write_object(const void *p_buffer, t_size p_bytes, abort_callback &p_abort)
Helper. Same as write(), provided for consistency.
Definition: filesystem.h:175
void read_object_t(T &p_object, abort_callback &p_abort)
Helper template built around read_object. Reads single raw object from the stream.
Definition: filesystem.h:118
static void listFiles(const char *p_path, t_list &p_out, abort_callback &p_abort)
Definition: filesystem.h:636
void reopen(abort_callback &)
Definition: filesystem.h:366
t_filestats m_stats
Definition: filesystem.h:491
pfc::string getPathCanonical(const T &source)
Definition: filesystem.h:668
file_ptr fileOpen(const char *p_path, filesystem::t_open_mode p_mode, abort_callback &p_abort, double p_timeout)
Definition: filesystem.h:576
Opens a new file for writing; if the file exists, its contents will be wiped.
Definition: filesystem.h:393
int64_t t_int64
Definition: int_types.h:2
void order_native_to_be_t(T &param)
directory_callback_retrieveListEx(t_list &p_files, t_list &p_directories)
Definition: filesystem.h:609
file_ptr fileOpenWriteExisting(const char *p_path, abort_callback &p_abort, double p_timeout=0)
Definition: filesystem.h:583