From 9d34fc394ce30a28ec0e43f2ad7172b8de8b3be6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 24 Jan 2014 16:18:50 +0100 Subject: Database*: move to db/ --- Makefile.am | 120 ++-- src/CommandLine.cxx | 6 +- src/DatabaseError.cxx | 24 - src/DatabaseError.hxx | 37 -- src/DatabaseGlue.cxx | 152 ----- src/DatabaseGlue.hxx | 62 -- src/DatabaseHelpers.cxx | 131 ---- src/DatabaseHelpers.hxx | 41 -- src/DatabaseListener.hxx | 38 -- src/DatabaseLock.cxx | 27 - src/DatabaseLock.hxx | 97 --- src/DatabasePlaylist.cxx | 53 -- src/DatabasePlaylist.hxx | 34 - src/DatabasePlugin.hxx | 159 ----- src/DatabasePrint.cxx | 250 -------- src/DatabasePrint.hxx | 54 -- src/DatabaseQueue.cxx | 57 -- src/DatabaseQueue.hxx | 31 - src/DatabaseRegistry.cxx | 47 -- src/DatabaseRegistry.hxx | 37 -- src/DatabaseSave.cxx | 154 ----- src/DatabaseSave.hxx | 35 -- src/DatabaseSelection.cxx | 37 -- src/DatabaseSelection.hxx | 51 -- src/DatabaseSimple.hxx | 74 --- src/DatabaseSong.cxx | 41 -- src/DatabaseSong.hxx | 38 -- src/DatabaseVisitor.hxx | 37 -- src/DetachedSong.cxx | 2 +- src/Directory.cxx | 300 --------- src/Directory.hxx | 248 -------- src/DirectorySave.cxx | 163 ----- src/DirectorySave.hxx | 35 -- src/Instance.hxx | 2 +- src/LightDirectory.hxx | 61 -- src/LightSong.cxx | 33 - src/LightSong.hxx | 92 --- src/Main.cxx | 8 +- src/Mapper.cxx | 6 +- src/PlaylistEdit.cxx | 2 +- src/PlaylistFile.cxx | 2 +- src/PlaylistPrint.cxx | 4 +- src/PlaylistUpdate.cxx | 6 +- src/PlaylistVector.cxx | 2 +- src/Song.cxx | 110 ---- src/Song.hxx | 112 ---- src/SongFilter.cxx | 4 +- src/SongPrint.cxx | 2 +- src/SongSave.cxx | 2 +- src/SongSort.cxx | 114 ---- src/SongSort.hxx | 28 - src/SongUpdate.cxx | 4 +- src/Stats.cxx | 8 +- src/command/CommandError.cxx | 2 +- src/command/DatabaseCommands.cxx | 8 +- src/command/OtherCommands.cxx | 4 +- src/command/PlayerCommands.cxx | 2 +- src/command/PlaylistCommands.cxx | 2 +- src/command/QueueCommands.cxx | 4 +- src/command/StickerCommands.cxx | 8 +- src/db/DatabaseError.cxx | 24 + src/db/DatabaseError.hxx | 37 ++ src/db/DatabaseGlue.cxx | 152 +++++ src/db/DatabaseGlue.hxx | 62 ++ src/db/DatabaseListener.hxx | 38 ++ src/db/DatabaseLock.cxx | 27 + src/db/DatabaseLock.hxx | 97 +++ src/db/DatabasePlaylist.cxx | 53 ++ src/db/DatabasePlaylist.hxx | 34 + src/db/DatabasePlugin.hxx | 159 +++++ src/db/DatabasePrint.cxx | 250 ++++++++ src/db/DatabasePrint.hxx | 54 ++ src/db/DatabaseQueue.cxx | 57 ++ src/db/DatabaseQueue.hxx | 31 + src/db/DatabaseSave.cxx | 154 +++++ src/db/DatabaseSave.hxx | 35 ++ src/db/DatabaseSimple.hxx | 74 +++ src/db/DatabaseSong.cxx | 41 ++ src/db/DatabaseSong.hxx | 38 ++ src/db/Directory.cxx | 300 +++++++++ src/db/Directory.hxx | 248 ++++++++ src/db/DirectorySave.cxx | 163 +++++ src/db/DirectorySave.hxx | 35 ++ src/db/Helpers.cxx | 131 ++++ src/db/Helpers.hxx | 41 ++ src/db/LazyDatabase.cxx | 103 ---- src/db/LazyDatabase.hxx | 69 --- src/db/LightDirectory.hxx | 61 ++ src/db/LightSong.cxx | 33 + src/db/LightSong.hxx | 92 +++ src/db/ProxyDatabasePlugin.cxx | 782 ----------------------- src/db/ProxyDatabasePlugin.hxx | 27 - src/db/Registry.cxx | 47 ++ src/db/Registry.hxx | 37 ++ src/db/Selection.cxx | 37 ++ src/db/Selection.hxx | 51 ++ src/db/SimpleDatabasePlugin.cxx | 336 ---------- src/db/SimpleDatabasePlugin.hxx | 106 ---- src/db/Song.cxx | 110 ++++ src/db/Song.hxx | 112 ++++ src/db/SongSort.cxx | 114 ++++ src/db/SongSort.hxx | 28 + src/db/UpnpDatabasePlugin.cxx | 785 ------------------------ src/db/UpnpDatabasePlugin.hxx | 27 - src/db/Visitor.hxx | 37 ++ src/db/plugins/LazyDatabase.cxx | 103 ++++ src/db/plugins/LazyDatabase.hxx | 69 +++ src/db/plugins/ProxyDatabasePlugin.cxx | 782 +++++++++++++++++++++++ src/db/plugins/ProxyDatabasePlugin.hxx | 27 + src/db/plugins/SimpleDatabasePlugin.cxx | 336 ++++++++++ src/db/plugins/SimpleDatabasePlugin.hxx | 106 ++++ src/db/plugins/UpnpDatabasePlugin.cxx | 785 ++++++++++++++++++++++++ src/db/plugins/UpnpDatabasePlugin.hxx | 27 + src/db/plugins/upnp/Action.hxx | 56 ++ src/db/plugins/upnp/ContentDirectoryService.cxx | 273 ++++++++ src/db/plugins/upnp/ContentDirectoryService.hxx | 128 ++++ src/db/plugins/upnp/Device.cxx | 134 ++++ src/db/plugins/upnp/Device.hxx | 88 +++ src/db/plugins/upnp/Directory.cxx | 262 ++++++++ src/db/plugins/upnp/Directory.hxx | 66 ++ src/db/plugins/upnp/Discovery.cxx | 318 ++++++++++ src/db/plugins/upnp/Discovery.hxx | 152 +++++ src/db/plugins/upnp/Domain.cxx | 23 + src/db/plugins/upnp/Domain.hxx | 27 + src/db/plugins/upnp/Object.cxx | 25 + src/db/plugins/upnp/Object.hxx | 85 +++ src/db/plugins/upnp/Tags.cxx | 33 + src/db/plugins/upnp/Tags.hxx | 28 + src/db/plugins/upnp/Util.cxx | 166 +++++ src/db/plugins/upnp/Util.hxx | 46 ++ src/db/plugins/upnp/WorkQueue.hxx | 206 +++++++ src/db/plugins/upnp/ixmlwrap.cxx | 44 ++ src/db/plugins/upnp/ixmlwrap.hxx | 35 ++ src/db/plugins/upnp/upnpplib.cxx | 75 +++ src/db/plugins/upnp/upnpplib.hxx | 70 +++ src/db/update/InotifyDomain.cxx | 23 + src/db/update/InotifyDomain.hxx | 25 + src/db/update/InotifyQueue.cxx | 89 +++ src/db/update/InotifyQueue.hxx | 41 ++ src/db/update/InotifySource.cxx | 114 ++++ src/db/update/InotifySource.hxx | 74 +++ src/db/update/InotifyUpdate.cxx | 339 ++++++++++ src/db/update/InotifyUpdate.hxx | 47 ++ src/db/update/UpdateArchive.cxx | 169 +++++ src/db/update/UpdateArchive.hxx | 50 ++ src/db/update/UpdateContainer.cxx | 136 ++++ src/db/update/UpdateContainer.hxx | 36 ++ src/db/update/UpdateDatabase.cxx | 104 ++++ src/db/update/UpdateDatabase.hxx | 50 ++ src/db/update/UpdateDomain.cxx | 23 + src/db/update/UpdateDomain.hxx | 25 + src/db/update/UpdateGlue.cxx | 181 ++++++ src/db/update/UpdateGlue.hxx | 43 ++ src/db/update/UpdateIO.cxx | 113 ++++ src/db/update/UpdateIO.hxx | 50 ++ src/db/update/UpdateInternal.hxx | 28 + src/db/update/UpdateQueue.cxx | 49 ++ src/db/update/UpdateQueue.hxx | 48 ++ src/db/update/UpdateRemove.cxx | 97 +++ src/db/update/UpdateRemove.hxx | 38 ++ src/db/update/UpdateSong.cxx | 113 ++++ src/db/update/UpdateSong.hxx | 34 + src/db/update/UpdateWalk.cxx | 484 +++++++++++++++ src/db/update/UpdateWalk.hxx | 37 ++ src/db/upnp/Action.hxx | 56 -- src/db/upnp/ContentDirectoryService.cxx | 273 -------- src/db/upnp/ContentDirectoryService.hxx | 128 ---- src/db/upnp/Device.cxx | 134 ---- src/db/upnp/Device.hxx | 88 --- src/db/upnp/Directory.cxx | 262 -------- src/db/upnp/Directory.hxx | 66 -- src/db/upnp/Discovery.cxx | 318 ---------- src/db/upnp/Discovery.hxx | 152 ----- src/db/upnp/Domain.cxx | 23 - src/db/upnp/Domain.hxx | 27 - src/db/upnp/Object.cxx | 25 - src/db/upnp/Object.hxx | 85 --- src/db/upnp/Tags.cxx | 33 - src/db/upnp/Tags.hxx | 28 - src/db/upnp/Util.cxx | 166 ----- src/db/upnp/Util.hxx | 46 -- src/db/upnp/WorkQueue.hxx | 206 ------- src/db/upnp/ixmlwrap.cxx | 44 -- src/db/upnp/ixmlwrap.hxx | 35 -- src/db/upnp/upnpplib.cxx | 75 --- src/db/upnp/upnpplib.hxx | 70 --- src/playlist/PlaylistSong.cxx | 2 +- src/queue/QueueSave.cxx | 2 +- src/sticker/SongSticker.cxx | 6 +- src/update/InotifyDomain.cxx | 23 - src/update/InotifyDomain.hxx | 25 - src/update/InotifyQueue.cxx | 89 --- src/update/InotifyQueue.hxx | 41 -- src/update/InotifySource.cxx | 114 ---- src/update/InotifySource.hxx | 74 --- src/update/InotifyUpdate.cxx | 339 ---------- src/update/InotifyUpdate.hxx | 47 -- src/update/UpdateArchive.cxx | 169 ----- src/update/UpdateArchive.hxx | 50 -- src/update/UpdateContainer.cxx | 136 ---- src/update/UpdateContainer.hxx | 36 -- src/update/UpdateDatabase.cxx | 104 ---- src/update/UpdateDatabase.hxx | 50 -- src/update/UpdateDomain.cxx | 23 - src/update/UpdateDomain.hxx | 25 - src/update/UpdateGlue.cxx | 181 ------ src/update/UpdateGlue.hxx | 43 -- src/update/UpdateIO.cxx | 113 ---- src/update/UpdateIO.hxx | 50 -- src/update/UpdateInternal.hxx | 28 - src/update/UpdateQueue.cxx | 49 -- src/update/UpdateQueue.hxx | 48 -- src/update/UpdateRemove.cxx | 97 --- src/update/UpdateRemove.hxx | 38 -- src/update/UpdateSong.cxx | 113 ---- src/update/UpdateSong.hxx | 34 - src/update/UpdateWalk.cxx | 484 --------------- src/update/UpdateWalk.hxx | 37 -- test/DumpDatabase.cxx | 12 +- test/run_inotify.cxx | 2 +- test/test_translate_song.cxx | 2 +- 221 files changed, 10447 insertions(+), 10447 deletions(-) delete mode 100644 src/DatabaseError.cxx delete mode 100644 src/DatabaseError.hxx delete mode 100644 src/DatabaseGlue.cxx delete mode 100644 src/DatabaseGlue.hxx delete mode 100644 src/DatabaseHelpers.cxx delete mode 100644 src/DatabaseHelpers.hxx delete mode 100644 src/DatabaseListener.hxx delete mode 100644 src/DatabaseLock.cxx delete mode 100644 src/DatabaseLock.hxx delete mode 100644 src/DatabasePlaylist.cxx delete mode 100644 src/DatabasePlaylist.hxx delete mode 100644 src/DatabasePlugin.hxx delete mode 100644 src/DatabasePrint.cxx delete mode 100644 src/DatabasePrint.hxx delete mode 100644 src/DatabaseQueue.cxx delete mode 100644 src/DatabaseQueue.hxx delete mode 100644 src/DatabaseRegistry.cxx delete mode 100644 src/DatabaseRegistry.hxx delete mode 100644 src/DatabaseSave.cxx delete mode 100644 src/DatabaseSave.hxx delete mode 100644 src/DatabaseSelection.cxx delete mode 100644 src/DatabaseSelection.hxx delete mode 100644 src/DatabaseSimple.hxx delete mode 100644 src/DatabaseSong.cxx delete mode 100644 src/DatabaseSong.hxx delete mode 100644 src/DatabaseVisitor.hxx delete mode 100644 src/Directory.cxx delete mode 100644 src/Directory.hxx delete mode 100644 src/DirectorySave.cxx delete mode 100644 src/DirectorySave.hxx delete mode 100644 src/LightDirectory.hxx delete mode 100644 src/LightSong.cxx delete mode 100644 src/LightSong.hxx delete mode 100644 src/Song.cxx delete mode 100644 src/Song.hxx delete mode 100644 src/SongSort.cxx delete mode 100644 src/SongSort.hxx create mode 100644 src/db/DatabaseError.cxx create mode 100644 src/db/DatabaseError.hxx create mode 100644 src/db/DatabaseGlue.cxx create mode 100644 src/db/DatabaseGlue.hxx create mode 100644 src/db/DatabaseListener.hxx create mode 100644 src/db/DatabaseLock.cxx create mode 100644 src/db/DatabaseLock.hxx create mode 100644 src/db/DatabasePlaylist.cxx create mode 100644 src/db/DatabasePlaylist.hxx create mode 100644 src/db/DatabasePlugin.hxx create mode 100644 src/db/DatabasePrint.cxx create mode 100644 src/db/DatabasePrint.hxx create mode 100644 src/db/DatabaseQueue.cxx create mode 100644 src/db/DatabaseQueue.hxx create mode 100644 src/db/DatabaseSave.cxx create mode 100644 src/db/DatabaseSave.hxx create mode 100644 src/db/DatabaseSimple.hxx create mode 100644 src/db/DatabaseSong.cxx create mode 100644 src/db/DatabaseSong.hxx create mode 100644 src/db/Directory.cxx create mode 100644 src/db/Directory.hxx create mode 100644 src/db/DirectorySave.cxx create mode 100644 src/db/DirectorySave.hxx create mode 100644 src/db/Helpers.cxx create mode 100644 src/db/Helpers.hxx delete mode 100644 src/db/LazyDatabase.cxx delete mode 100644 src/db/LazyDatabase.hxx create mode 100644 src/db/LightDirectory.hxx create mode 100644 src/db/LightSong.cxx create mode 100644 src/db/LightSong.hxx delete mode 100644 src/db/ProxyDatabasePlugin.cxx delete mode 100644 src/db/ProxyDatabasePlugin.hxx create mode 100644 src/db/Registry.cxx create mode 100644 src/db/Registry.hxx create mode 100644 src/db/Selection.cxx create mode 100644 src/db/Selection.hxx delete mode 100644 src/db/SimpleDatabasePlugin.cxx delete mode 100644 src/db/SimpleDatabasePlugin.hxx create mode 100644 src/db/Song.cxx create mode 100644 src/db/Song.hxx create mode 100644 src/db/SongSort.cxx create mode 100644 src/db/SongSort.hxx delete mode 100644 src/db/UpnpDatabasePlugin.cxx delete mode 100644 src/db/UpnpDatabasePlugin.hxx create mode 100644 src/db/Visitor.hxx create mode 100644 src/db/plugins/LazyDatabase.cxx create mode 100644 src/db/plugins/LazyDatabase.hxx create mode 100644 src/db/plugins/ProxyDatabasePlugin.cxx create mode 100644 src/db/plugins/ProxyDatabasePlugin.hxx create mode 100644 src/db/plugins/SimpleDatabasePlugin.cxx create mode 100644 src/db/plugins/SimpleDatabasePlugin.hxx create mode 100644 src/db/plugins/UpnpDatabasePlugin.cxx create mode 100644 src/db/plugins/UpnpDatabasePlugin.hxx create mode 100644 src/db/plugins/upnp/Action.hxx create mode 100644 src/db/plugins/upnp/ContentDirectoryService.cxx create mode 100644 src/db/plugins/upnp/ContentDirectoryService.hxx create mode 100644 src/db/plugins/upnp/Device.cxx create mode 100644 src/db/plugins/upnp/Device.hxx create mode 100644 src/db/plugins/upnp/Directory.cxx create mode 100644 src/db/plugins/upnp/Directory.hxx create mode 100644 src/db/plugins/upnp/Discovery.cxx create mode 100644 src/db/plugins/upnp/Discovery.hxx create mode 100644 src/db/plugins/upnp/Domain.cxx create mode 100644 src/db/plugins/upnp/Domain.hxx create mode 100644 src/db/plugins/upnp/Object.cxx create mode 100644 src/db/plugins/upnp/Object.hxx create mode 100644 src/db/plugins/upnp/Tags.cxx create mode 100644 src/db/plugins/upnp/Tags.hxx create mode 100644 src/db/plugins/upnp/Util.cxx create mode 100644 src/db/plugins/upnp/Util.hxx create mode 100644 src/db/plugins/upnp/WorkQueue.hxx create mode 100644 src/db/plugins/upnp/ixmlwrap.cxx create mode 100644 src/db/plugins/upnp/ixmlwrap.hxx create mode 100644 src/db/plugins/upnp/upnpplib.cxx create mode 100644 src/db/plugins/upnp/upnpplib.hxx create mode 100644 src/db/update/InotifyDomain.cxx create mode 100644 src/db/update/InotifyDomain.hxx create mode 100644 src/db/update/InotifyQueue.cxx create mode 100644 src/db/update/InotifyQueue.hxx create mode 100644 src/db/update/InotifySource.cxx create mode 100644 src/db/update/InotifySource.hxx create mode 100644 src/db/update/InotifyUpdate.cxx create mode 100644 src/db/update/InotifyUpdate.hxx create mode 100644 src/db/update/UpdateArchive.cxx create mode 100644 src/db/update/UpdateArchive.hxx create mode 100644 src/db/update/UpdateContainer.cxx create mode 100644 src/db/update/UpdateContainer.hxx create mode 100644 src/db/update/UpdateDatabase.cxx create mode 100644 src/db/update/UpdateDatabase.hxx create mode 100644 src/db/update/UpdateDomain.cxx create mode 100644 src/db/update/UpdateDomain.hxx create mode 100644 src/db/update/UpdateGlue.cxx create mode 100644 src/db/update/UpdateGlue.hxx create mode 100644 src/db/update/UpdateIO.cxx create mode 100644 src/db/update/UpdateIO.hxx create mode 100644 src/db/update/UpdateInternal.hxx create mode 100644 src/db/update/UpdateQueue.cxx create mode 100644 src/db/update/UpdateQueue.hxx create mode 100644 src/db/update/UpdateRemove.cxx create mode 100644 src/db/update/UpdateRemove.hxx create mode 100644 src/db/update/UpdateSong.cxx create mode 100644 src/db/update/UpdateSong.hxx create mode 100644 src/db/update/UpdateWalk.cxx create mode 100644 src/db/update/UpdateWalk.hxx delete mode 100644 src/db/upnp/Action.hxx delete mode 100644 src/db/upnp/ContentDirectoryService.cxx delete mode 100644 src/db/upnp/ContentDirectoryService.hxx delete mode 100644 src/db/upnp/Device.cxx delete mode 100644 src/db/upnp/Device.hxx delete mode 100644 src/db/upnp/Directory.cxx delete mode 100644 src/db/upnp/Directory.hxx delete mode 100644 src/db/upnp/Discovery.cxx delete mode 100644 src/db/upnp/Discovery.hxx delete mode 100644 src/db/upnp/Domain.cxx delete mode 100644 src/db/upnp/Domain.hxx delete mode 100644 src/db/upnp/Object.cxx delete mode 100644 src/db/upnp/Object.hxx delete mode 100644 src/db/upnp/Tags.cxx delete mode 100644 src/db/upnp/Tags.hxx delete mode 100644 src/db/upnp/Util.cxx delete mode 100644 src/db/upnp/Util.hxx delete mode 100644 src/db/upnp/WorkQueue.hxx delete mode 100644 src/db/upnp/ixmlwrap.cxx delete mode 100644 src/db/upnp/ixmlwrap.hxx delete mode 100644 src/db/upnp/upnpplib.cxx delete mode 100644 src/db/upnp/upnpplib.hxx delete mode 100644 src/update/InotifyDomain.cxx delete mode 100644 src/update/InotifyDomain.hxx delete mode 100644 src/update/InotifyQueue.cxx delete mode 100644 src/update/InotifyQueue.hxx delete mode 100644 src/update/InotifySource.cxx delete mode 100644 src/update/InotifySource.hxx delete mode 100644 src/update/InotifyUpdate.cxx delete mode 100644 src/update/InotifyUpdate.hxx delete mode 100644 src/update/UpdateArchive.cxx delete mode 100644 src/update/UpdateArchive.hxx delete mode 100644 src/update/UpdateContainer.cxx delete mode 100644 src/update/UpdateContainer.hxx delete mode 100644 src/update/UpdateDatabase.cxx delete mode 100644 src/update/UpdateDatabase.hxx delete mode 100644 src/update/UpdateDomain.cxx delete mode 100644 src/update/UpdateDomain.hxx delete mode 100644 src/update/UpdateGlue.cxx delete mode 100644 src/update/UpdateGlue.hxx delete mode 100644 src/update/UpdateIO.cxx delete mode 100644 src/update/UpdateIO.hxx delete mode 100644 src/update/UpdateInternal.hxx delete mode 100644 src/update/UpdateQueue.cxx delete mode 100644 src/update/UpdateQueue.hxx delete mode 100644 src/update/UpdateRemove.cxx delete mode 100644 src/update/UpdateRemove.hxx delete mode 100644 src/update/UpdateSong.cxx delete mode 100644 src/update/UpdateSong.hxx delete mode 100644 src/update/UpdateWalk.cxx delete mode 100644 src/update/UpdateWalk.hxx diff --git a/Makefile.am b/Makefile.am index 74909e27d..fe069ac79 100644 --- a/Makefile.am +++ b/Makefile.am @@ -102,36 +102,36 @@ src_mpd_SOURCES = \ src/decoder/DecoderPlugin.hxx \ src/decoder/DecoderInternal.cxx src/decoder/DecoderInternal.hxx \ src/decoder/DecoderPrint.cxx src/decoder/DecoderPrint.hxx \ - src/Directory.cxx src/Directory.hxx \ - src/DirectorySave.cxx src/DirectorySave.hxx \ - src/DatabaseSimple.hxx \ - src/DatabaseGlue.cxx src/DatabaseGlue.hxx \ - src/DatabaseSong.cxx src/DatabaseSong.hxx \ - src/DatabasePrint.cxx src/DatabasePrint.hxx \ - src/DatabaseQueue.cxx src/DatabaseQueue.hxx \ - src/DatabasePlaylist.cxx src/DatabasePlaylist.hxx \ - src/DatabaseError.cxx src/DatabaseError.hxx \ - src/DatabaseLock.cxx src/DatabaseLock.hxx \ - src/DatabaseSave.cxx src/DatabaseSave.hxx \ - src/DatabasePlugin.hxx \ - src/DatabaseListener.hxx \ - src/DatabaseVisitor.hxx \ - src/DatabaseSelection.cxx src/DatabaseSelection.hxx \ + src/db/Directory.cxx src/db/Directory.hxx \ + src/db/DirectorySave.cxx src/db/DirectorySave.hxx \ + src/db/DatabaseSimple.hxx \ + src/db/DatabaseGlue.cxx src/db/DatabaseGlue.hxx \ + src/db/DatabaseSong.cxx src/db/DatabaseSong.hxx \ + src/db/DatabasePrint.cxx src/db/DatabasePrint.hxx \ + src/db/DatabaseQueue.cxx src/db/DatabaseQueue.hxx \ + src/db/DatabasePlaylist.cxx src/db/DatabasePlaylist.hxx \ + src/db/DatabaseError.cxx src/db/DatabaseError.hxx \ + src/db/DatabaseLock.cxx src/db/DatabaseLock.hxx \ + src/db/DatabaseSave.cxx src/db/DatabaseSave.hxx \ + src/db/DatabasePlugin.hxx \ + src/db/DatabaseListener.hxx \ + src/db/Visitor.hxx \ + src/db/Selection.cxx src/db/Selection.hxx \ src/ExcludeList.cxx src/ExcludeList.hxx \ src/FilterConfig.cxx src/FilterConfig.hxx \ src/FilterPlugin.cxx src/FilterPlugin.hxx \ src/FilterInternal.hxx \ src/FilterRegistry.cxx src/FilterRegistry.hxx \ - src/update/UpdateDomain.cxx src/update/UpdateDomain.hxx \ - src/update/UpdateGlue.cxx src/update/UpdateGlue.hxx \ - src/update/UpdateQueue.cxx src/update/UpdateQueue.hxx \ - src/update/UpdateIO.cxx src/update/UpdateIO.hxx \ - src/update/UpdateDatabase.cxx src/update/UpdateDatabase.hxx \ - src/update/UpdateWalk.cxx src/update/UpdateWalk.hxx \ - src/update/UpdateSong.cxx src/update/UpdateSong.hxx \ - src/update/UpdateContainer.cxx src/update/UpdateContainer.hxx \ - src/update/UpdateInternal.hxx \ - src/update/UpdateRemove.cxx src/update/UpdateRemove.hxx \ + src/db/update/UpdateDomain.cxx src/db/update/UpdateDomain.hxx \ + src/db/update/UpdateGlue.cxx src/db/update/UpdateGlue.hxx \ + src/db/update/UpdateQueue.cxx src/db/update/UpdateQueue.hxx \ + src/db/update/UpdateIO.cxx src/db/update/UpdateIO.hxx \ + src/db/update/UpdateDatabase.cxx src/db/update/UpdateDatabase.hxx \ + src/db/update/UpdateWalk.cxx src/db/update/UpdateWalk.hxx \ + src/db/update/UpdateSong.cxx src/db/update/UpdateSong.hxx \ + src/db/update/UpdateContainer.cxx src/db/update/UpdateContainer.hxx \ + src/db/update/UpdateInternal.hxx \ + src/db/update/UpdateRemove.cxx src/db/update/UpdateRemove.hxx \ src/client/Client.cxx src/client/Client.hxx \ src/client/ClientInternal.hxx \ src/client/ClientEvent.cxx \ @@ -196,13 +196,13 @@ src_mpd_SOURCES = \ src/ReplayGainConfig.cxx src/ReplayGainConfig.hxx \ src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx \ src/DetachedSong.cxx src/DetachedSong.hxx \ - src/LightSong.cxx src/LightSong.hxx \ - src/LightDirectory.hxx \ - src/Song.cxx src/Song.hxx \ + src/db/LightSong.cxx src/db/LightSong.hxx \ + src/db/LightDirectory.hxx \ + src/db/Song.cxx src/db/Song.hxx \ src/SongUpdate.cxx \ src/SongPrint.cxx src/SongPrint.hxx \ src/SongSave.cxx src/SongSave.hxx \ - src/SongSort.cxx src/SongSort.hxx \ + src/db/SongSort.cxx src/db/SongSort.hxx \ src/StateFile.cxx src/StateFile.hxx \ src/Stats.cxx src/Stats.hxx \ src/TagPrint.cxx src/TagPrint.hxx \ @@ -235,10 +235,10 @@ endif if ENABLE_INOTIFY src_mpd_SOURCES += \ - src/update/InotifyDomain.cxx src/update/InotifyDomain.hxx \ - src/update/InotifySource.cxx src/update/InotifySource.hxx \ - src/update/InotifyQueue.cxx src/update/InotifyQueue.hxx \ - src/update/InotifyUpdate.cxx src/update/InotifyUpdate.hxx + src/db/update/InotifyDomain.cxx src/db/update/InotifyDomain.hxx \ + src/db/update/InotifySource.cxx src/db/update/InotifySource.hxx \ + src/db/update/InotifyQueue.cxx src/db/update/InotifyQueue.hxx \ + src/db/update/InotifyUpdate.cxx src/db/update/InotifyUpdate.hxx endif if ENABLE_SQLITE @@ -400,14 +400,14 @@ libfs_a_SOURCES = \ # database plugins libdb_plugins_a_SOURCES = \ - src/DatabaseRegistry.cxx src/DatabaseRegistry.hxx \ - src/DatabaseHelpers.cxx src/DatabaseHelpers.hxx \ - src/db/LazyDatabase.cxx src/db/LazyDatabase.hxx \ - src/db/SimpleDatabasePlugin.cxx src/db/SimpleDatabasePlugin.hxx + src/db/Registry.cxx src/db/Registry.hxx \ + src/db/Helpers.cxx src/db/Helpers.hxx \ + src/db/plugins/LazyDatabase.cxx src/db/plugins/LazyDatabase.hxx \ + src/db/plugins/SimpleDatabasePlugin.cxx src/db/plugins/SimpleDatabasePlugin.hxx if HAVE_LIBMPDCLIENT libdb_plugins_a_SOURCES += \ - src/db/ProxyDatabasePlugin.cxx src/db/ProxyDatabasePlugin.hxx + src/db/plugins/ProxyDatabasePlugin.cxx src/db/plugins/ProxyDatabasePlugin.hxx endif DB_LIBS = \ @@ -416,19 +416,19 @@ DB_LIBS = \ if HAVE_LIBUPNP libdb_plugins_a_SOURCES += \ - src/db/UpnpDatabasePlugin.cxx src/db/UpnpDatabasePlugin.hxx \ - src/db/upnp/Tags.cxx src/db/upnp/Tags.hxx \ - src/db/upnp/ContentDirectoryService.cxx src/db/upnp/ContentDirectoryService.hxx \ - src/db/upnp/Device.cxx src/db/upnp/Device.hxx \ - src/db/upnp/Directory.cxx src/db/upnp/Directory.hxx \ - src/db/upnp/Discovery.cxx src/db/upnp/Discovery.hxx \ - src/db/upnp/Domain.cxx src/db/upnp/Domain.hxx \ - src/db/upnp/ixmlwrap.cxx src/db/upnp/ixmlwrap.hxx \ - src/db/upnp/upnpplib.cxx src/db/upnp/upnpplib.hxx \ - src/db/upnp/Util.cxx src/db/upnp/Util.hxx \ - src/db/upnp/Action.hxx \ - src/db/upnp/WorkQueue.hxx \ - src/db/upnp/Object.cxx src/db/upnp/Object.hxx + src/db/plugins/UpnpDatabasePlugin.cxx src/db/plugins/UpnpDatabasePlugin.hxx \ + src/db/plugins/upnp/Tags.cxx src/db/plugins/upnp/Tags.hxx \ + src/db/plugins/upnp/ContentDirectoryService.cxx src/db/plugins/upnp/ContentDirectoryService.hxx \ + src/db/plugins/upnp/Device.cxx src/db/plugins/upnp/Device.hxx \ + src/db/plugins/upnp/Directory.cxx src/db/plugins/upnp/Directory.hxx \ + src/db/plugins/upnp/Discovery.cxx src/db/plugins/upnp/Discovery.hxx \ + src/db/plugins/upnp/Domain.cxx src/db/plugins/upnp/Domain.hxx \ + src/db/plugins/upnp/ixmlwrap.cxx src/db/plugins/upnp/ixmlwrap.hxx \ + src/db/plugins/upnp/upnpplib.cxx src/db/plugins/upnp/upnpplib.hxx \ + src/db/plugins/upnp/Util.cxx src/db/plugins/upnp/Util.hxx \ + src/db/plugins/upnp/Action.hxx \ + src/db/plugins/upnp/WorkQueue.hxx \ + src/db/plugins/upnp/Object.cxx src/db/plugins/upnp/Object.hxx DB_LIBS += \ $(EXPAT_LIBS) \ $(UPNP_LIBS) @@ -441,7 +441,7 @@ if ENABLE_ARCHIVE noinst_LIBRARIES += libarchive.a src_mpd_SOURCES += \ - src/update/UpdateArchive.cxx src/update/UpdateArchive.hxx + src/db/update/UpdateArchive.cxx src/db/update/UpdateArchive.hxx libarchive_a_SOURCES = \ src/archive/ArchiveDomain.cxx src/archive/ArchiveDomain.hxx \ @@ -1244,13 +1244,13 @@ test_DumpDatabase_LDADD = \ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/protocol/Ack.cxx \ src/Log.cxx src/LogBackend.cxx \ - src/DatabaseError.cxx \ - src/DatabaseRegistry.cxx \ - src/DatabaseSelection.cxx \ - src/Directory.cxx src/DirectorySave.cxx \ + src/db/DatabaseError.cxx \ + src/db/Registry.cxx \ + src/db/Selection.cxx \ + src/db/Directory.cxx src/db/DirectorySave.cxx \ src/PlaylistVector.cxx src/PlaylistDatabase.cxx \ - src/DatabaseLock.cxx src/DatabaseSave.cxx \ - src/Song.cxx src/SongSave.cxx src/SongSort.cxx \ + src/db/DatabaseLock.cxx src/db/DatabaseSave.cxx \ + src/db/Song.cxx src/SongSave.cxx src/db/SongSort.cxx \ src/DetachedSong.cxx \ src/TagSave.cxx \ src/SongFilter.cxx @@ -1581,8 +1581,8 @@ noinst_PROGRAMS += test/run_inotify test_run_inotify_SOURCES = test/run_inotify.cxx \ test/ShutdownHandler.cxx test/ShutdownHandler.hxx \ src/Log.cxx src/LogBackend.cxx \ - src/update/InotifyDomain.cxx \ - src/update/InotifySource.cxx + src/db/update/InotifyDomain.cxx \ + src/db/update/InotifySource.cxx test_run_inotify_LDADD = \ libevent.a \ libsystem.a \ diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 2b386f5de..c5adc4153 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -23,8 +23,8 @@ #include "LogInit.hxx" #include "Log.hxx" #include "config/ConfigGlobal.hxx" -#include "DatabaseRegistry.hxx" -#include "DatabasePlugin.hxx" +#include "db/Registry.hxx" +#include "db/DatabasePlugin.hxx" #include "decoder/DecoderList.hxx" #include "decoder/DecoderPlugin.hxx" #include "output/OutputList.hxx" @@ -99,7 +99,7 @@ static void version(void) "This is free software; see the source for copying conditions. There is NO\n" "warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" "\n" - "Database plugins:"); + "db/Database plugins:"); for (auto i = database_plugins; *i != nullptr; ++i) printf(" %s", (*i)->name); diff --git a/src/DatabaseError.cxx b/src/DatabaseError.cxx deleted file mode 100644 index e0cbdd6a3..000000000 --- a/src/DatabaseError.cxx +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseError.hxx" -#include "util/Domain.hxx" - -const Domain db_domain("db"); diff --git a/src/DatabaseError.hxx b/src/DatabaseError.hxx deleted file mode 100644 index 1485a21b6..000000000 --- a/src/DatabaseError.hxx +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DB_ERROR_HXX -#define MPD_DB_ERROR_HXX - -class Domain; - -enum db_error { - /** - * The database is disabled, i.e. none is configured in this - * MPD instance. - */ - DB_DISABLED, - - DB_NOT_FOUND, -}; - -extern const Domain db_domain; - -#endif diff --git a/src/DatabaseGlue.cxx b/src/DatabaseGlue.cxx deleted file mode 100644 index ce53ec184..000000000 --- a/src/DatabaseGlue.cxx +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseGlue.hxx" -#include "DatabaseSimple.hxx" -#include "DatabaseRegistry.hxx" -#include "DatabaseError.hxx" -#include "Directory.hxx" -#include "util/Error.hxx" -#include "config/ConfigData.hxx" -#include "Stats.hxx" -#include "DatabasePlugin.hxx" -#include "db/SimpleDatabasePlugin.hxx" - -#include -#include - -static Database *db; -static bool db_is_open; -static bool is_simple; - -bool -DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, Error &error) -{ - assert(db == nullptr); - assert(!db_is_open); - - const char *plugin_name = - param.GetBlockValue("plugin", "simple"); - is_simple = strcmp(plugin_name, "simple") == 0; - - const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name); - if (plugin == nullptr) { - error.Format(db_domain, - "No such database plugin: %s", plugin_name); - return false; - } - - db = plugin->create(loop, listener, param, error); - return db != nullptr; -} - -void -DatabaseGlobalDeinit(void) -{ - if (db_is_open) - db->Close(); - - if (db != nullptr) - delete db; -} - -const Database * -GetDatabase() -{ - assert(db == nullptr || db_is_open); - - return db; -} - -const Database * -GetDatabase(Error &error) -{ - assert(db == nullptr || db_is_open); - - if (db == nullptr) - error.Set(db_domain, DB_DISABLED, "No database"); - - return db; -} - -bool -db_is_simple(void) -{ - assert(db == nullptr || db_is_open); - - return is_simple; -} - -Directory * -db_get_root(void) -{ - assert(db != nullptr); - assert(db_is_simple()); - - return ((SimpleDatabase *)db)->GetRoot(); -} - -Directory * -db_get_directory(const char *name) -{ - if (db == nullptr) - return nullptr; - - Directory *music_root = db_get_root(); - if (name == nullptr) - return music_root; - - return music_root->LookupDirectory(name); -} - -bool -db_save(Error &error) -{ - assert(db != nullptr); - assert(db_is_open); - assert(db_is_simple()); - - return ((SimpleDatabase *)db)->Save(error); -} - -bool -DatabaseGlobalOpen(Error &error) -{ - assert(db != nullptr); - assert(!db_is_open); - - if (!db->Open(error)) - return false; - - db_is_open = true; - - return true; -} - -bool -db_exists() -{ - assert(db != nullptr); - assert(db_is_open); - assert(db_is_simple()); - - return ((SimpleDatabase *)db)->GetUpdateStamp() > 0; -} diff --git a/src/DatabaseGlue.hxx b/src/DatabaseGlue.hxx deleted file mode 100644 index 78032edb2..000000000 --- a/src/DatabaseGlue.hxx +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_GLUE_HXX -#define MPD_DATABASE_GLUE_HXX - -#include "Compiler.h" - -struct config_param; -class EventLoop; -class DatabaseListener; -class Database; -class Error; - -/** - * Initialize the database library. - * - * @param param the database configuration block - */ -bool -DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, Error &error); - -void -DatabaseGlobalDeinit(void); - -bool -DatabaseGlobalOpen(Error &error); - -/** - * Returns the global #Database instance. May return nullptr if this MPD - * configuration has no database (no music_directory was configured). - */ -gcc_const -const Database * -GetDatabase(); - -/** - * Returns the global #Database instance. May return nullptr if this MPD - * configuration has no database (no music_directory was configured). - */ -gcc_pure -const Database * -GetDatabase(Error &error); - -#endif diff --git a/src/DatabaseHelpers.cxx b/src/DatabaseHelpers.cxx deleted file mode 100644 index 58e7aaa3b..000000000 --- a/src/DatabaseHelpers.cxx +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "DatabaseHelpers.hxx" -#include "DatabasePlugin.hxx" -#include "LightSong.hxx" -#include "tag/Tag.hxx" - -#include -#include - -#include - -struct StringLess { - gcc_pure - bool operator()(const char *a, const char *b) const { - return strcmp(a, b) < 0; - } -}; - -typedef std::set StringSet; - -static bool -CollectTags(StringSet &set, TagType tag_type, const LightSong &song) -{ - const Tag *tag = song.tag; - - bool found = false; - for (unsigned i = 0; i < tag->num_items; ++i) { - if (tag->items[i]->type == tag_type) { - set.insert(tag->items[i]->value); - found = true; - } - } - - if (!found) - set.insert(""); - - return true; -} - -bool -VisitUniqueTags(const Database &db, const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) -{ - StringSet set; - - using namespace std::placeholders; - const auto f = std::bind(CollectTags, std::ref(set), tag_type, _1); - if (!db.Visit(selection, f, error)) - return false; - - for (auto value : set) - if (!visit_string(value, error)) - return false; - - return true; -} - -static void -StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums, - const Tag &tag) -{ - if (tag.time > 0) - stats.total_duration += tag.time; - - for (unsigned i = 0; i < tag.num_items; ++i) { - const TagItem &item = *tag.items[i]; - - switch (item.type) { - case TAG_ARTIST: - artists.insert(item.value); - break; - - case TAG_ALBUM: - albums.insert(item.value); - break; - - default: - break; - } - } -} - -static bool -StatsVisitSong(DatabaseStats &stats, StringSet &artists, StringSet &albums, - const LightSong &song) -{ - ++stats.song_count; - - StatsVisitTag(stats, artists, albums, *song.tag); - - return true; -} - -bool -GetStats(const Database &db, const DatabaseSelection &selection, - DatabaseStats &stats, Error &error) -{ - stats.Clear(); - - StringSet artists, albums; - using namespace std::placeholders; - const auto f = std::bind(StatsVisitSong, - std::ref(stats), std::ref(artists), - std::ref(albums), _1); - if (!db.Visit(selection, f, error)) - return false; - - stats.artist_count = artists.size(); - stats.album_count = albums.size(); - return true; -} diff --git a/src/DatabaseHelpers.hxx b/src/DatabaseHelpers.hxx deleted file mode 100644 index 0880e4a56..000000000 --- a/src/DatabaseHelpers.hxx +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_MEMORY_DATABASE_PLUGIN_HXX -#define MPD_MEMORY_DATABASE_PLUGIN_HXX - -#include "DatabaseVisitor.hxx" -#include "tag/TagType.h" - -class Error; -class Database; -struct DatabaseSelection; -struct DatabaseStats; - -bool -VisitUniqueTags(const Database &db, const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error); - -bool -GetStats(const Database &db, const DatabaseSelection &selection, - DatabaseStats &stats, Error &error); - -#endif diff --git a/src/DatabaseListener.hxx b/src/DatabaseListener.hxx deleted file mode 100644 index 4da458866..000000000 --- a/src/DatabaseListener.hxx +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_CLIENT_HXX -#define MPD_DATABASE_CLIENT_HXX - -/** - * An object that listens to events from the #Database. - * - * @see #Instance - */ -class DatabaseListener { -public: - /** - * The database has been modified. This must be called in the - * thread that has created the #Database instance and that - * runs the #EventLoop. - */ - virtual void OnDatabaseModified() = 0; -}; - -#endif diff --git a/src/DatabaseLock.cxx b/src/DatabaseLock.cxx deleted file mode 100644 index c0b5e4844..000000000 --- a/src/DatabaseLock.cxx +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseLock.hxx" - -Mutex db_mutex; - -#ifndef NDEBUG -ThreadId db_mutex_holder; -#endif diff --git a/src/DatabaseLock.hxx b/src/DatabaseLock.hxx deleted file mode 100644 index 9d0b0c152..000000000 --- a/src/DatabaseLock.hxx +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** \file - * - * Support for locking data structures from the database, for safe - * multi-threading. - */ - -#ifndef MPD_DB_LOCK_HXX -#define MPD_DB_LOCK_HXX - -#include "check.h" -#include "thread/Mutex.hxx" -#include "Compiler.h" - -#include - -extern Mutex db_mutex; - -#ifndef NDEBUG - -#include "thread/Id.hxx" - -extern ThreadId db_mutex_holder; - -/** - * Does the current thread hold the database lock? - */ -gcc_pure -static inline bool -holding_db_lock(void) -{ - return db_mutex_holder.IsInside(); -} - -#endif - -/** - * Obtain the global database lock. This is needed before - * dereferencing a #song or #directory. It is not recursive. - */ -static inline void -db_lock(void) -{ - assert(!holding_db_lock()); - - db_mutex.lock(); - - assert(db_mutex_holder.IsNull()); -#ifndef NDEBUG - db_mutex_holder = ThreadId::GetCurrent(); -#endif -} - -/** - * Release the global database lock. - */ -static inline void -db_unlock(void) -{ - assert(holding_db_lock()); -#ifndef NDEBUG - db_mutex_holder = ThreadId::Null(); -#endif - - db_mutex.unlock(); -} - -class ScopeDatabaseLock { -public: - ScopeDatabaseLock() { - db_lock(); - } - - ~ScopeDatabaseLock() { - db_unlock(); - } -}; - -#endif diff --git a/src/DatabasePlaylist.cxx b/src/DatabasePlaylist.cxx deleted file mode 100644 index 58742ca64..000000000 --- a/src/DatabasePlaylist.cxx +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabasePlaylist.hxx" -#include "DatabaseSelection.hxx" -#include "PlaylistFile.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" -#include "DetachedSong.hxx" -#include "Mapper.hxx" - -#include - -static bool -AddSong(const char *playlist_path_utf8, - const LightSong &song, Error &error) -{ - return spl_append_song(playlist_path_utf8, map_song_detach(song), - error); -} - -bool -search_add_to_playlist(const char *uri, const char *playlist_path_utf8, - const SongFilter *filter, - Error &error) -{ - const Database *db = GetDatabase(error); - if (db == nullptr) - return false; - - const DatabaseSelection selection(uri, true, filter); - - using namespace std::placeholders; - const auto f = std::bind(AddSong, playlist_path_utf8, _1, _2); - return db->Visit(selection, f, error); -} diff --git a/src/DatabasePlaylist.hxx b/src/DatabasePlaylist.hxx deleted file mode 100644 index 1ee7584d3..000000000 --- a/src/DatabasePlaylist.hxx +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_PLAYLIST_HXX -#define MPD_DATABASE_PLAYLIST_HXX - -#include "Compiler.h" - -class SongFilter; -class Error; - -gcc_nonnull(1,2) -bool -search_add_to_playlist(const char *uri, const char *path_utf8, - const SongFilter *filter, - Error &error); - -#endif diff --git a/src/DatabasePlugin.hxx b/src/DatabasePlugin.hxx deleted file mode 100644 index 2ded7f736..000000000 --- a/src/DatabasePlugin.hxx +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/** \file - * - * This header declares the db_plugin class. It describes a - * plugin API for databases of song metadata. - */ - -#ifndef MPD_DATABASE_PLUGIN_HXX -#define MPD_DATABASE_PLUGIN_HXX - -#include "DatabaseVisitor.hxx" -#include "tag/TagType.h" -#include "Compiler.h" - -#include - -struct config_param; -struct DatabaseSelection; -struct db_visitor; -struct LightSong; -class Error; -class EventLoop; -class DatabaseListener; - -struct DatabaseStats { - /** - * Number of songs. - */ - unsigned song_count; - - /** - * Total duration of all songs (in seconds). - */ - unsigned long total_duration; - - /** - * Number of distinct artist names. - */ - unsigned artist_count; - - /** - * Number of distinct album names. - */ - unsigned album_count; - - void Clear() { - song_count = 0; - total_duration = 0; - artist_count = album_count = 0; - } -}; - -class Database { -public: - /** - * Free instance data. - */ - virtual ~Database() {} - - /** - * Open the database. Read it into memory if applicable. - */ - virtual bool Open(gcc_unused Error &error) { - return true; - } - - /** - * Close the database, free allocated memory. - */ - virtual void Close() {} - - /** - * Look up a song (including tag data) in the database. When - * you don't need this anymore, call ReturnSong(). - * - * @param uri_utf8 the URI of the song within the music - * directory (UTF-8) - */ - virtual const LightSong *GetSong(const char *uri_utf8, - Error &error) const = 0; - - /** - * Mark the song object as "unused". Call this on objects - * returned by GetSong(). - */ - virtual void ReturnSong(const LightSong *song) const = 0; - - /** - * Visit the selected entities. - */ - virtual bool Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const = 0; - - bool Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - Error &error) const { - return Visit(selection, visit_directory, visit_song, - VisitPlaylist(), error); - } - - bool Visit(const DatabaseSelection &selection, VisitSong visit_song, - Error &error) const { - return Visit(selection, VisitDirectory(), visit_song, error); - } - - /** - * Visit all unique tag values. - */ - virtual bool VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const = 0; - - virtual bool GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, - Error &error) const = 0; - - /** - * Returns the time stamp of the last database update. - * Returns 0 if that is not not known/available. - */ - gcc_pure - virtual time_t GetUpdateStamp() const = 0; -}; - -struct DatabasePlugin { - const char *name; - - /** - * Allocates and configures a database. - */ - Database *(*create)(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, - Error &error); -}; - -#endif diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx deleted file mode 100644 index b22141aef..000000000 --- a/src/DatabasePrint.cxx +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabasePrint.hxx" -#include "DatabaseSelection.hxx" -#include "SongFilter.hxx" -#include "SongPrint.hxx" -#include "TimePrint.hxx" -#include "client/Client.hxx" -#include "tag/Tag.hxx" -#include "LightSong.hxx" -#include "LightDirectory.hxx" -#include "PlaylistInfo.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" - -#include - -static bool -PrintDirectoryBrief(Client &client, const LightDirectory &directory) -{ - if (!directory.IsRoot()) - client_printf(client, "directory: %s\n", directory.GetPath()); - - return true; -} - -static bool -PrintDirectoryFull(Client &client, const LightDirectory &directory) -{ - if (!directory.IsRoot()) { - client_printf(client, "directory: %s\n", directory.GetPath()); - - if (directory.mtime > 0) - time_print(client, "Last-Modified", directory.mtime); - } - - return true; -} - -static void -print_playlist_in_directory(Client &client, - const char *directory, - const char *name_utf8) -{ - if (directory == nullptr) - client_printf(client, "playlist: %s\n", name_utf8); - else - client_printf(client, "playlist: %s/%s\n", - directory, name_utf8); -} - -static void -print_playlist_in_directory(Client &client, - const LightDirectory *directory, - const char *name_utf8) -{ - if (directory == nullptr || directory->IsRoot()) - client_printf(client, "playlist: %s\n", name_utf8); - else - client_printf(client, "playlist: %s/%s\n", - directory->GetPath(), name_utf8); -} - -static bool -PrintSongBrief(Client &client, const LightSong &song) -{ - song_print_uri(client, song); - - if (song.tag->has_playlist) - /* this song file has an embedded CUE sheet */ - print_playlist_in_directory(client, song.directory, song.uri); - - return true; -} - -static bool -PrintSongFull(Client &client, const LightSong &song) -{ - song_print_info(client, song); - - if (song.tag->has_playlist) - /* this song file has an embedded CUE sheet */ - print_playlist_in_directory(client, song.directory, song.uri); - - return true; -} - -static bool -PrintPlaylistBrief(Client &client, - const PlaylistInfo &playlist, - const LightDirectory &directory) -{ - print_playlist_in_directory(client, &directory, playlist.name.c_str()); - return true; -} - -static bool -PrintPlaylistFull(Client &client, - const PlaylistInfo &playlist, - const LightDirectory &directory) -{ - print_playlist_in_directory(client, &directory, playlist.name.c_str()); - - if (playlist.mtime > 0) - time_print(client, "Last-Modified", playlist.mtime); - - return true; -} - -bool -db_selection_print(Client &client, const DatabaseSelection &selection, - bool full, Error &error) -{ - const Database *db = GetDatabase(error); - if (db == nullptr) - return false; - - using namespace std::placeholders; - const auto d = selection.filter == nullptr - ? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief, - std::ref(client), _1) - : VisitDirectory(); - const auto s = std::bind(full ? PrintSongFull : PrintSongBrief, - std::ref(client), _1); - const auto p = selection.filter == nullptr - ? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief, - std::ref(client), _1, _2) - : VisitPlaylist(); - - return db->Visit(selection, d, s, p, error); -} - -struct SearchStats { - int numberOfSongs; - unsigned long playTime; -}; - -static void printSearchStats(Client &client, SearchStats *stats) -{ - client_printf(client, "songs: %i\n", stats->numberOfSongs); - client_printf(client, "playtime: %li\n", stats->playTime); -} - -static bool -stats_visitor_song(SearchStats &stats, const LightSong &song) -{ - stats.numberOfSongs++; - stats.playTime += song.GetDuration(); - - return true; -} - -bool -searchStatsForSongsIn(Client &client, const char *name, - const SongFilter *filter, - Error &error) -{ - const Database *db = GetDatabase(error); - if (db == nullptr) - return false; - - const DatabaseSelection selection(name, true, filter); - - SearchStats stats; - stats.numberOfSongs = 0; - stats.playTime = 0; - - using namespace std::placeholders; - const auto f = std::bind(stats_visitor_song, std::ref(stats), - _1); - if (!db->Visit(selection, f, error)) - return false; - - printSearchStats(client, &stats); - return true; -} - -bool -printAllIn(Client &client, const char *uri_utf8, Error &error) -{ - const DatabaseSelection selection(uri_utf8, true); - return db_selection_print(client, selection, false, error); -} - -bool -printInfoForAllIn(Client &client, const char *uri_utf8, - Error &error) -{ - const DatabaseSelection selection(uri_utf8, true); - return db_selection_print(client, selection, true, error); -} - -static bool -PrintSongURIVisitor(Client &client, const LightSong &song) -{ - song_print_uri(client, song); - - return true; -} - -static bool -PrintUniqueTag(Client &client, TagType tag_type, - const char *value) -{ - client_printf(client, "%s: %s\n", tag_item_names[tag_type], value); - return true; -} - -bool -listAllUniqueTags(Client &client, int type, - const SongFilter *filter, - Error &error) -{ - const Database *db = GetDatabase(error); - if (db == nullptr) - return false; - - const DatabaseSelection selection("", true, filter); - - if (type == LOCATE_TAG_FILE_TYPE) { - using namespace std::placeholders; - const auto f = std::bind(PrintSongURIVisitor, - std::ref(client), _1); - return db->Visit(selection, f, error); - } else { - using namespace std::placeholders; - const auto f = std::bind(PrintUniqueTag, std::ref(client), - (TagType)type, _1); - return db->VisitUniqueTags(selection, (TagType)type, - f, error); - } -} diff --git a/src/DatabasePrint.hxx b/src/DatabasePrint.hxx deleted file mode 100644 index 2007e256b..000000000 --- a/src/DatabasePrint.hxx +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DB_PRINT_H -#define MPD_DB_PRINT_H - -#include "Compiler.h" - -class SongFilter; -struct DatabaseSelection; -class Client; -class Error; - -bool -db_selection_print(Client &client, const DatabaseSelection &selection, - bool full, Error &error); - -gcc_nonnull(2) -bool -printAllIn(Client &client, const char *uri_utf8, Error &error); - -gcc_nonnull(2) -bool -printInfoForAllIn(Client &client, const char *uri_utf8, - Error &error); - -gcc_nonnull(2) -bool -searchStatsForSongsIn(Client &client, const char *name, - const SongFilter *filter, - Error &error); - -bool -listAllUniqueTags(Client &client, int type, - const SongFilter *filter, - Error &error); - -#endif diff --git a/src/DatabaseQueue.cxx b/src/DatabaseQueue.cxx deleted file mode 100644 index ee1dbd57c..000000000 --- a/src/DatabaseQueue.cxx +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseQueue.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" -#include "Partition.hxx" -#include "util/Error.hxx" -#include "DetachedSong.hxx" -#include "Mapper.hxx" - -#include - -static bool -AddToQueue(Partition &partition, const LightSong &song, Error &error) -{ - PlaylistResult result = - partition.playlist.AppendSong(partition.pc, - map_song_detach(song), - nullptr); - if (result != PlaylistResult::SUCCESS) { - error.Set(playlist_domain, int(result), "Playlist error"); - return false; - } - - return true; -} - -bool -AddFromDatabase(Partition &partition, const DatabaseSelection &selection, - Error &error) -{ - const Database *db = GetDatabase(error); - if (db == nullptr) - return false; - - using namespace std::placeholders; - const auto f = std::bind(AddToQueue, std::ref(partition), _1, _2); - return db->Visit(selection, f, error); -} diff --git a/src/DatabaseQueue.hxx b/src/DatabaseQueue.hxx deleted file mode 100644 index e653f973c..000000000 --- a/src/DatabaseQueue.hxx +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_QUEUE_HXX -#define MPD_DATABASE_QUEUE_HXX - -struct Partition; -struct DatabaseSelection; -class Error; - -bool -AddFromDatabase(Partition &partition, const DatabaseSelection &selection, - Error &error); - -#endif diff --git a/src/DatabaseRegistry.cxx b/src/DatabaseRegistry.cxx deleted file mode 100644 index b8a91343e..000000000 --- a/src/DatabaseRegistry.cxx +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseRegistry.hxx" -#include "db/SimpleDatabasePlugin.hxx" -#include "db/ProxyDatabasePlugin.hxx" -#include "db/UpnpDatabasePlugin.hxx" - -#include - -const DatabasePlugin *const database_plugins[] = { - &simple_db_plugin, -#ifdef HAVE_LIBMPDCLIENT - &proxy_db_plugin, -#endif -#ifdef HAVE_LIBUPNP - &upnp_db_plugin, -#endif - nullptr -}; - -const DatabasePlugin * -GetDatabasePluginByName(const char *name) -{ - for (auto i = database_plugins; *i != nullptr; ++i) - if (strcmp((*i)->name, name) == 0) - return *i; - - return nullptr; -} diff --git a/src/DatabaseRegistry.hxx b/src/DatabaseRegistry.hxx deleted file mode 100644 index 050842e21..000000000 --- a/src/DatabaseRegistry.hxx +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_REGISTRY_HXX -#define MPD_DATABASE_REGISTRY_HXX - -#include "Compiler.h" - -struct DatabasePlugin; - -/** - * nullptr terminated list of all database plugins which were enabled at - * compile time. - */ -extern const DatabasePlugin *const database_plugins[]; - -gcc_pure -const DatabasePlugin * -GetDatabasePluginByName(const char *name); - -#endif diff --git a/src/DatabaseSave.cxx b/src/DatabaseSave.cxx deleted file mode 100644 index e9c81442b..000000000 --- a/src/DatabaseSave.cxx +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseSave.hxx" -#include "DatabaseLock.hxx" -#include "DatabaseError.hxx" -#include "Directory.hxx" -#include "DirectorySave.hxx" -#include "fs/TextFile.hxx" -#include "tag/Tag.hxx" -#include "tag/TagSettings.h" -#include "fs/Charset.hxx" -#include "util/StringUtil.hxx" -#include "util/Error.hxx" -#include "Log.hxx" - -#include -#include - -#define DIRECTORY_INFO_BEGIN "info_begin" -#define DIRECTORY_INFO_END "info_end" -#define DB_FORMAT_PREFIX "format: " -#define DIRECTORY_MPD_VERSION "mpd_version: " -#define DIRECTORY_FS_CHARSET "fs_charset: " -#define DB_TAG_PREFIX "tag: " - -static constexpr unsigned DB_FORMAT = 1; - -void -db_save_internal(FILE *fp, const Directory &music_root) -{ - fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN); - fprintf(fp, DB_FORMAT_PREFIX "%u\n", DB_FORMAT); - fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION); - fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset()); - - for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) - if (!ignore_tag_items[i]) - fprintf(fp, DB_TAG_PREFIX "%s\n", tag_item_names[i]); - - fprintf(fp, "%s\n", DIRECTORY_INFO_END); - - directory_save(fp, music_root); -} - -bool -db_load_internal(TextFile &file, Directory &music_root, Error &error) -{ - char *line; - unsigned format = 0; - bool found_charset = false, found_version = false; - bool success; - bool tags[TAG_NUM_OF_ITEM_TYPES]; - - /* get initial info */ - line = file.ReadLine(); - if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) { - error.Set(db_domain, "Database corrupted"); - return false; - } - - memset(tags, false, sizeof(tags)); - - while ((line = file.ReadLine()) != nullptr && - strcmp(line, DIRECTORY_INFO_END) != 0) { - if (StringStartsWith(line, DB_FORMAT_PREFIX)) { - format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1); - } else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) { - if (found_version) { - error.Set(db_domain, "Duplicate version line"); - return false; - } - - found_version = true; - } else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) { - const char *new_charset; - - if (found_charset) { - error.Set(db_domain, "Duplicate charset line"); - return false; - } - - found_charset = true; - - new_charset = line + sizeof(DIRECTORY_FS_CHARSET) - 1; - const char *const old_charset = GetFSCharset(); - if (*old_charset != 0 - && strcmp(new_charset, old_charset) != 0) { - error.Format(db_domain, - "Existing database has charset " - "\"%s\" instead of \"%s\"; " - "discarding database file", - new_charset, old_charset); - return false; - } - } else if (StringStartsWith(line, DB_TAG_PREFIX)) { - const char *name = line + sizeof(DB_TAG_PREFIX) - 1; - TagType tag = tag_name_parse(name); - if (tag == TAG_NUM_OF_ITEM_TYPES) { - error.Format(db_domain, - "Unrecognized tag '%s', " - "discarding database file", - name); - return false; - } - - tags[tag] = true; - } else { - error.Format(db_domain, "Malformed line: %s", line); - return false; - } - } - - if (format != DB_FORMAT) { - error.Set(db_domain, - "Database format mismatch, " - "discarding database file"); - return false; - } - - for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) { - if (!ignore_tag_items[i] && !tags[i]) { - error.Set(db_domain, - "Tag list mismatch, " - "discarding database file"); - return false; - } - } - - LogDebug(db_domain, "reading DB"); - - db_lock(); - success = directory_load(file, music_root, error); - db_unlock(); - - return success; -} diff --git a/src/DatabaseSave.hxx b/src/DatabaseSave.hxx deleted file mode 100644 index 3bd3377ae..000000000 --- a/src/DatabaseSave.hxx +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_SAVE_HXX -#define MPD_DATABASE_SAVE_HXX - -#include - -struct Directory; -class TextFile; -class Error; - -void -db_save_internal(FILE *file, const Directory &root); - -bool -db_load_internal(TextFile &file, Directory &root, Error &error); - -#endif diff --git a/src/DatabaseSelection.cxx b/src/DatabaseSelection.cxx deleted file mode 100644 index 035321252..000000000 --- a/src/DatabaseSelection.cxx +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "DatabaseSelection.hxx" -#include "SongFilter.hxx" - -DatabaseSelection::DatabaseSelection(const char *_uri, bool _recursive, - const SongFilter *_filter) - :uri(_uri), recursive(_recursive), filter(_filter) -{ - /* optimization: if the caller didn't specify a base URI, pick - the one from SongFilter */ - if (uri.empty() && filter != nullptr) - uri = filter->GetBase(); -} - -bool -DatabaseSelection::Match(const LightSong &song) const -{ - return filter == nullptr || filter->Match(song); -} diff --git a/src/DatabaseSelection.hxx b/src/DatabaseSelection.hxx deleted file mode 100644 index a39ce7afe..000000000 --- a/src/DatabaseSelection.hxx +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_SELECTION_HXX -#define MPD_DATABASE_SELECTION_HXX - -#include "Compiler.h" - -#include - -class SongFilter; -struct LightSong; - -struct DatabaseSelection { - /** - * The base URI of the search (UTF-8). Must not begin or end - * with a slash. An empty string searches the whole database. - */ - std::string uri; - - /** - * Recursively search all sub directories? - */ - bool recursive; - - const SongFilter *filter; - - DatabaseSelection(const char *_uri, bool _recursive, - const SongFilter *_filter=nullptr); - - gcc_pure - bool Match(const LightSong &song) const; -}; - -#endif diff --git a/src/DatabaseSimple.hxx b/src/DatabaseSimple.hxx deleted file mode 100644 index b99b3bfa5..000000000 --- a/src/DatabaseSimple.hxx +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_SIMPLE_HXX -#define MPD_DATABASE_SIMPLE_HXX - -#include "Compiler.h" - -#include - -struct config_param; -struct Directory; -struct db_selection; -struct db_visitor; -class Error; - -/** - * Check whether the default #SimpleDatabasePlugin is used. This - * allows using db_get_root(), db_save(), db_get_mtime() and - * db_exists(). - */ -bool -db_is_simple(void); - -/** - * Returns the root directory object. Returns NULL if there is no - * configured music directory. - * - * May only be used if db_is_simple() returns true. - */ -gcc_pure -Directory * -db_get_root(void); - -/** - * Caller must lock the #db_mutex. - */ -gcc_nonnull(1) -gcc_pure -Directory * -db_get_directory(const char *name); - -/** - * May only be used if db_is_simple() returns true. - */ -bool -db_save(Error &error); - -/** - * Returns true if there is a valid database file on the disk. - * - * May only be used if db_is_simple() returns true. - */ -gcc_pure -bool -db_exists(); - -#endif diff --git a/src/DatabaseSong.cxx b/src/DatabaseSong.cxx deleted file mode 100644 index 592d38b85..000000000 --- a/src/DatabaseSong.cxx +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DatabaseSong.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" -#include "DetachedSong.hxx" -#include "Mapper.hxx" - -DetachedSong * -DatabaseDetachSong(const char *uri, Error &error) -{ - const Database *db = GetDatabase(error); - if (db == nullptr) - return nullptr; - - const LightSong *tmp = db->GetSong(uri, error); - if (tmp == nullptr) - return nullptr; - - DetachedSong *song = new DetachedSong(map_song_detach(*tmp)); - db->ReturnSong(tmp); - return song; -} diff --git a/src/DatabaseSong.hxx b/src/DatabaseSong.hxx deleted file mode 100644 index 0200af6b8..000000000 --- a/src/DatabaseSong.hxx +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_SONG_HXX -#define MPD_DATABASE_SONG_HXX - -#include "Compiler.h" - -class DetachedSong; -class Error; - -/** - * Look up a song in the database and convert it to a #DetachedSong - * instance. The caller is responsible for freeing it. - * - * @return nullptr on error - */ -gcc_malloc gcc_nonnull_all -DetachedSong * -DatabaseDetachSong(const char *uri, Error &error); - -#endif diff --git a/src/DatabaseVisitor.hxx b/src/DatabaseVisitor.hxx deleted file mode 100644 index 0ec29bf49..000000000 --- a/src/DatabaseVisitor.hxx +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DATABASE_VISITOR_HXX -#define MPD_DATABASE_VISITOR_HXX - -#include - -struct LightDirectory; -struct LightSong; -struct PlaylistInfo; -class Error; - -typedef std::function VisitDirectory; -typedef std::function VisitSong; -typedef std::function VisitPlaylist; - -typedef std::function VisitString; - -#endif diff --git a/src/DetachedSong.cxx b/src/DetachedSong.cxx index 8882d2863..2fff9b70f 100644 --- a/src/DetachedSong.cxx +++ b/src/DetachedSong.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "DetachedSong.hxx" -#include "LightSong.hxx" +#include "db/LightSong.hxx" #include "util/UriUtil.hxx" #include "fs/Traits.hxx" diff --git a/src/Directory.cxx b/src/Directory.cxx deleted file mode 100644 index 14cf88eb5..000000000 --- a/src/Directory.cxx +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "Directory.hxx" -#include "LightDirectory.hxx" -#include "SongFilter.hxx" -#include "PlaylistVector.hxx" -#include "DatabaseLock.hxx" -#include "SongSort.hxx" -#include "Song.hxx" -#include "LightSong.hxx" -#include "fs/Traits.hxx" -#include "util/Alloc.hxx" -#include "util/Error.hxx" - -extern "C" { -#include "util/list_sort.h" -} - -#include - -#include -#include -#include - -Directory::Directory(const char *_path_utf8, Directory *_parent) - :parent(_parent), - mtime(0), have_stat(false), - path(_path_utf8) -{ - INIT_LIST_HEAD(&children); - INIT_LIST_HEAD(&songs); -} - -Directory::~Directory() -{ - Song *song, *ns; - directory_for_each_song_safe(song, ns, *this) - song->Free(); - - Directory *child, *n; - directory_for_each_child_safe(child, n, *this) - delete child; -} - -void -Directory::Delete() -{ - assert(holding_db_lock()); - assert(parent != nullptr); - - list_del(&siblings); - delete this; -} - -const char * -Directory::GetName() const -{ - assert(!IsRoot()); - - return PathTraitsUTF8::GetBase(path.c_str()); -} - -Directory * -Directory::CreateChild(const char *name_utf8) -{ - assert(holding_db_lock()); - assert(name_utf8 != nullptr); - assert(*name_utf8 != 0); - - char *allocated; - const char *path_utf8; - if (IsRoot()) { - allocated = nullptr; - path_utf8 = name_utf8; - } else { - allocated = g_strconcat(GetPath(), - "/", name_utf8, nullptr); - path_utf8 = allocated; - } - - Directory *child = new Directory(path_utf8, this); - g_free(allocated); - - list_add_tail(&child->siblings, &children); - return child; -} - -const Directory * -Directory::FindChild(const char *name) const -{ - assert(holding_db_lock()); - - const Directory *child; - directory_for_each_child(child, *this) - if (strcmp(child->GetName(), name) == 0) - return child; - - return nullptr; -} - -void -Directory::PruneEmpty() -{ - assert(holding_db_lock()); - - Directory *child, *n; - directory_for_each_child_safe(child, n, *this) { - child->PruneEmpty(); - - if (child->IsEmpty()) - child->Delete(); - } -} - -Directory * -Directory::LookupDirectory(const char *uri) -{ - assert(holding_db_lock()); - assert(uri != nullptr); - - if (isRootDirectory(uri)) - return this; - - char *duplicated = xstrdup(uri), *name = duplicated; - - Directory *d = this; - while (1) { - char *slash = strchr(name, '/'); - if (slash == name) { - d = nullptr; - break; - } - - if (slash != nullptr) - *slash = '\0'; - - d = d->FindChild(name); - if (d == nullptr || slash == nullptr) - break; - - name = slash + 1; - } - - free(duplicated); - - return d; -} - -void -Directory::AddSong(Song *song) -{ - assert(holding_db_lock()); - assert(song != nullptr); - assert(song->parent == this); - - list_add_tail(&song->siblings, &songs); -} - -void -Directory::RemoveSong(Song *song) -{ - assert(holding_db_lock()); - assert(song != nullptr); - assert(song->parent == this); - - list_del(&song->siblings); -} - -const Song * -Directory::FindSong(const char *name_utf8) const -{ - assert(holding_db_lock()); - assert(name_utf8 != nullptr); - - Song *song; - directory_for_each_song(song, *this) { - assert(song->parent == this); - - if (strcmp(song->uri, name_utf8) == 0) - return song; - } - - return nullptr; -} - -Song * -Directory::LookupSong(const char *uri) -{ - char *duplicated, *base; - - assert(holding_db_lock()); - assert(uri != nullptr); - - duplicated = xstrdup(uri); - base = strrchr(duplicated, '/'); - - Directory *d = this; - if (base != nullptr) { - *base++ = 0; - d = d->LookupDirectory(duplicated); - if (d == nullptr) { - free(duplicated); - return nullptr; - } - } else - base = duplicated; - - Song *song = d->FindSong(base); - assert(song == nullptr || song->parent == d); - - free(duplicated); - return song; - -} - -static int -directory_cmp(gcc_unused void *priv, - struct list_head *_a, struct list_head *_b) -{ - const Directory *a = (const Directory *)_a; - const Directory *b = (const Directory *)_b; - return g_utf8_collate(a->path.c_str(), b->path.c_str()); -} - -void -Directory::Sort() -{ - assert(holding_db_lock()); - - list_sort(nullptr, &children, directory_cmp); - song_list_sort(&songs); - - Directory *child; - directory_for_each_child(child, *this) - child->Sort(); -} - -bool -Directory::Walk(bool recursive, const SongFilter *filter, - VisitDirectory visit_directory, VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const -{ - assert(!error.IsDefined()); - - if (visit_song) { - Song *song; - directory_for_each_song(song, *this) { - const LightSong song2 = song->Export(); - if ((filter == nullptr || filter->Match(song2)) && - !visit_song(song2, error)) - return false; - } - } - - if (visit_playlist) { - for (const PlaylistInfo &p : playlists) - if (!visit_playlist(p, Export(), error)) - return false; - } - - Directory *child; - directory_for_each_child(child, *this) { - if (visit_directory && - !visit_directory(child->Export(), error)) - return false; - - if (recursive && - !child->Walk(recursive, filter, - visit_directory, visit_song, visit_playlist, - error)) - return false; - } - - return true; -} - -LightDirectory -Directory::Export() const -{ - return LightDirectory(GetPath(), mtime); -} diff --git a/src/Directory.hxx b/src/Directory.hxx deleted file mode 100644 index c64a028ba..000000000 --- a/src/Directory.hxx +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DIRECTORY_HXX -#define MPD_DIRECTORY_HXX - -#include "check.h" -#include "util/list.h" -#include "Compiler.h" -#include "DatabaseVisitor.hxx" -#include "PlaylistVector.hxx" - -#include - -#include - -#define DEVICE_INARCHIVE (dev_t)(-1) -#define DEVICE_CONTAINER (dev_t)(-2) - -#define directory_for_each_child(pos, directory) \ - list_for_each_entry(pos, &(directory).children, siblings) - -#define directory_for_each_child_safe(pos, n, directory) \ - list_for_each_entry_safe(pos, n, &(directory).children, siblings) - -#define directory_for_each_song(pos, directory) \ - list_for_each_entry(pos, &(directory).songs, siblings) - -#define directory_for_each_song_safe(pos, n, directory) \ - list_for_each_entry_safe(pos, n, &(directory).songs, siblings) - -struct Song; -struct db_visitor; -class SongFilter; -class Error; - -struct Directory { - /** - * Pointers to the siblings of this directory within the - * parent directory. It is unused (undefined) in the root - * directory. - * - * This attribute is protected with the global #db_mutex. - * Read access in the update thread does not need protection. - */ - struct list_head siblings; - - /** - * A doubly linked list of child directories. - * - * This attribute is protected with the global #db_mutex. - * Read access in the update thread does not need protection. - */ - struct list_head children; - - /** - * A doubly linked list of songs within this directory. - * - * This attribute is protected with the global #db_mutex. - * Read access in the update thread does not need protection. - */ - struct list_head songs; - - PlaylistVector playlists; - - Directory *parent; - time_t mtime; - ino_t inode; - dev_t device; - bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */ - - std::string path; - -public: - Directory(const char *_path_utf8, Directory *_parent); - ~Directory(); - - /** - * Create a new root #Directory object. - */ - gcc_malloc - static Directory *NewRoot() { - return new Directory("", nullptr); - } - - /** - * Remove this #Directory object from its parent and free it. This - * must not be called with the root Directory. - * - * Caller must lock the #db_mutex. - */ - void Delete(); - - /** - * Create a new #Directory object as a child of the given one. - * - * Caller must lock the #db_mutex. - * - * @param name_utf8 the UTF-8 encoded name of the new sub directory - */ - gcc_malloc - Directory *CreateChild(const char *name_utf8); - - /** - * Caller must lock the #db_mutex. - */ - gcc_pure - const Directory *FindChild(const char *name) const; - - gcc_pure - Directory *FindChild(const char *name) { - const Directory *cthis = this; - return const_cast(cthis->FindChild(name)); - } - - /** - * Look up a sub directory, and create the object if it does not - * exist. - * - * Caller must lock the #db_mutex. - */ - Directory *MakeChild(const char *name_utf8) { - Directory *child = FindChild(name_utf8); - if (child == nullptr) - child = CreateChild(name_utf8); - return child; - } - - /** - * Looks up a directory by its relative URI. - * - * @param uri the relative URI - * @return the Directory, or nullptr if none was found - */ - gcc_pure - Directory *LookupDirectory(const char *uri); - - gcc_pure - bool IsEmpty() const { - return list_empty(&children) && - list_empty(&songs) && - playlists.empty(); - } - - gcc_pure - const char *GetPath() const { - return path.c_str(); - } - - /** - * Returns the base name of the directory. - */ - gcc_pure - const char *GetName() const; - - /** - * Is this the root directory of the music database? - */ - gcc_pure - bool IsRoot() const { - return parent == nullptr; - } - - /** - * Look up a song in this directory by its name. - * - * Caller must lock the #db_mutex. - */ - gcc_pure - const Song *FindSong(const char *name_utf8) const; - - gcc_pure - Song *FindSong(const char *name_utf8) { - const Directory *cthis = this; - return const_cast(cthis->FindSong(name_utf8)); - } - - /** - * Looks up a song by its relative URI. - * - * Caller must lock the #db_mutex. - * - * @param uri the relative URI - * @return the song, or nullptr if none was found - */ - gcc_pure - Song *LookupSong(const char *uri); - - /** - * Add a song object to this directory. Its "parent" attribute must - * be set already. - */ - void AddSong(Song *song); - - /** - * Remove a song object from this directory (which effectively - * invalidates the song object, because the "parent" attribute becomes - * stale), but does not free it. - */ - void RemoveSong(Song *song); - - /** - * Caller must lock the #db_mutex. - */ - void PruneEmpty(); - - /** - * Sort all directory entries recursively. - * - * Caller must lock the #db_mutex. - */ - void Sort(); - - /** - * Caller must lock #db_mutex. - */ - bool Walk(bool recursive, const SongFilter *match, - VisitDirectory visit_directory, VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const; - - gcc_pure - LightDirectory Export() const; -}; - -static inline bool -isRootDirectory(const char *name) -{ - return name[0] == 0 || (name[0] == '/' && name[1] == 0); -} - -#endif diff --git a/src/DirectorySave.cxx b/src/DirectorySave.cxx deleted file mode 100644 index 499f84734..000000000 --- a/src/DirectorySave.cxx +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "DirectorySave.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "SongSave.hxx" -#include "DetachedSong.hxx" -#include "PlaylistDatabase.hxx" -#include "fs/TextFile.hxx" -#include "util/StringUtil.hxx" -#include "util/NumberParser.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" - -#include - -#define DIRECTORY_DIR "directory: " -#define DIRECTORY_MTIME "mtime: " -#define DIRECTORY_BEGIN "begin: " -#define DIRECTORY_END "end: " - -static constexpr Domain directory_domain("directory"); - -void -directory_save(FILE *fp, const Directory &directory) -{ - if (!directory.IsRoot()) { - fprintf(fp, DIRECTORY_MTIME "%lu\n", - (unsigned long)directory.mtime); - - fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory.GetPath()); - } - - Directory *cur; - directory_for_each_child(cur, directory) { - fprintf(fp, DIRECTORY_DIR "%s\n", cur->GetName()); - - directory_save(fp, *cur); - - if (ferror(fp)) - return; - } - - Song *song; - directory_for_each_song(song, directory) - song_save(fp, *song); - - playlist_vector_save(fp, directory.playlists); - - if (!directory.IsRoot()) - fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath()); -} - -static Directory * -directory_load_subdir(TextFile &file, Directory &parent, const char *name, - Error &error) -{ - bool success; - - if (parent.FindChild(name) != nullptr) { - error.Format(directory_domain, - "Duplicate subdirectory '%s'", name); - return nullptr; - } - - Directory *directory = parent.CreateChild(name); - - const char *line = file.ReadLine(); - if (line == nullptr) { - error.Set(directory_domain, "Unexpected end of file"); - directory->Delete(); - return nullptr; - } - - if (StringStartsWith(line, DIRECTORY_MTIME)) { - directory->mtime = - ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1); - - line = file.ReadLine(); - if (line == nullptr) { - error.Set(directory_domain, "Unexpected end of file"); - directory->Delete(); - return nullptr; - } - } - - if (!StringStartsWith(line, DIRECTORY_BEGIN)) { - error.Format(directory_domain, "Malformed line: %s", line); - directory->Delete(); - return nullptr; - } - - success = directory_load(file, *directory, error); - if (!success) { - directory->Delete(); - return nullptr; - } - - return directory; -} - -bool -directory_load(TextFile &file, Directory &directory, Error &error) -{ - const char *line; - - while ((line = file.ReadLine()) != nullptr && - !StringStartsWith(line, DIRECTORY_END)) { - if (StringStartsWith(line, DIRECTORY_DIR)) { - Directory *subdir = - directory_load_subdir(file, directory, - line + sizeof(DIRECTORY_DIR) - 1, - error); - if (subdir == nullptr) - return false; - } else if (StringStartsWith(line, SONG_BEGIN)) { - const char *name = line + sizeof(SONG_BEGIN) - 1; - - if (directory.FindSong(name) != nullptr) { - error.Format(directory_domain, - "Duplicate song '%s'", name); - return false; - } - - DetachedSong *song = song_load(file, name, error); - if (song == nullptr) - return false; - - directory.AddSong(Song::NewFrom(std::move(*song), - directory)); - delete song; - } else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) { - const char *name = line + sizeof(PLAYLIST_META_BEGIN) - 1; - if (!playlist_metadata_load(file, directory.playlists, - name, error)) - return false; - } else { - error.Format(directory_domain, - "Malformed line: %s", line); - return false; - } - } - - return true; -} diff --git a/src/DirectorySave.hxx b/src/DirectorySave.hxx deleted file mode 100644 index 07e9e158b..000000000 --- a/src/DirectorySave.hxx +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_DIRECTORY_SAVE_HXX -#define MPD_DIRECTORY_SAVE_HXX - -#include - -struct Directory; -class TextFile; -class Error; - -void -directory_save(FILE *fp, const Directory &directory); - -bool -directory_load(TextFile &file, Directory &directory, Error &error); - -#endif diff --git a/src/Instance.hxx b/src/Instance.hxx index 71203ba48..ca7bb5197 100644 --- a/src/Instance.hxx +++ b/src/Instance.hxx @@ -21,7 +21,7 @@ #define MPD_INSTANCE_HXX #include "check.h" -#include "DatabaseListener.hxx" +#include "db/DatabaseListener.hxx" #include "Compiler.h" class ClientList; diff --git a/src/LightDirectory.hxx b/src/LightDirectory.hxx deleted file mode 100644 index d134151a4..000000000 --- a/src/LightDirectory.hxx +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_LIGHT_DIRECTORY_HXX -#define MPD_LIGHT_DIRECTORY_HXX - -#include "Compiler.h" - -#include - -#include - -struct Tag; - -/** - * A reference to a directory. Unlike the #Directory class, this one - * consists only of pointers. It is supposed to be as light as - * possible while still providing all the information MPD has about a - * directory. This class does not manage any memory, and the pointers - * become invalid quickly. Only to be used to pass around during - * well-defined situations. - */ -struct LightDirectory { - const char *uri; - - time_t mtime; - - constexpr LightDirectory(const char *_uri, time_t _mtime) - :uri(_uri), mtime(_mtime) {} - - static constexpr LightDirectory Root() { - return LightDirectory("", 0); - } - - bool IsRoot() const { - return *uri == 0; - } - - gcc_pure - const char *GetPath() const { - return uri; - } -}; - -#endif diff --git a/src/LightSong.cxx b/src/LightSong.cxx deleted file mode 100644 index af1e801f8..000000000 --- a/src/LightSong.cxx +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "LightSong.hxx" -#include "tag/Tag.hxx" - -double -LightSong::GetDuration() const -{ - if (end_ms > 0) - return (end_ms - start_ms) / 1000.0; - - if (tag->time <= 0) - return 0; - - return tag->time - start_ms / 1000.0; -} diff --git a/src/LightSong.hxx b/src/LightSong.hxx deleted file mode 100644 index c0cd47749..000000000 --- a/src/LightSong.hxx +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_LIGHT_SONG_HXX -#define MPD_LIGHT_SONG_HXX - -#include "Compiler.h" - -#include - -#include - -struct Tag; - -/** - * A reference to a song file. Unlike the other "Song" classes in the - * MPD code base, this one consists only of pointers. It is supposed - * to be as light as possible while still providing all the - * information MPD has about a song file. This class does not manage - * any memory, and the pointers become invalid quickly. Only to be - * used to pass around during well-defined situations. - */ -struct LightSong { - /** - * If this is not nullptr, then it denotes a prefix for the - * #uri. To build the full URI, join directory and uri with a - * slash. - */ - const char *directory; - - const char *uri; - - /** - * The "real" URI, the one to be used for opening the - * resource. If this attribute is empty, then #uri (and - * #directory) shall be used. - * - * This attribute is used for songs from the database which - * have a relative URI. - */ - std::string real_uri; - - /** - * Must not be nullptr. - */ - const Tag *tag; - - time_t mtime; - - /** - * Start of this sub-song within the file in milliseconds. - */ - unsigned start_ms; - - /** - * End of this sub-song within the file in milliseconds. - * Unused if zero. - */ - unsigned end_ms; - - gcc_pure - std::string GetURI() const { - if (directory == nullptr) - return std::string(uri); - - std::string result(directory); - result.push_back('/'); - result.append(uri); - return result; - } - - gcc_pure - double GetDuration() const; -}; - -#endif diff --git a/src/Main.cxx b/src/Main.cxx index 815f26f0a..e71814cff 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -23,13 +23,13 @@ #include "CommandLine.hxx" #include "PlaylistFile.hxx" #include "PlaylistGlobal.hxx" -#include "update/UpdateGlue.hxx" +#include "db/update/UpdateGlue.hxx" #include "MusicChunk.hxx" #include "StateFile.hxx" #include "PlayerThread.hxx" #include "Mapper.hxx" -#include "DatabaseGlue.hxx" -#include "DatabaseSimple.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabaseSimple.hxx" #include "Permission.hxx" #include "Listen.hxx" #include "client/Client.hxx" @@ -68,7 +68,7 @@ #include "Stats.hxx" #ifdef ENABLE_INOTIFY -#include "update/InotifyUpdate.hxx" +#include "db/update/InotifyUpdate.hxx" #endif #ifdef ENABLE_SQLITE diff --git a/src/Mapper.cxx b/src/Mapper.cxx index f29fd0646..ebcab91bf 100644 --- a/src/Mapper.cxx +++ b/src/Mapper.cxx @@ -23,10 +23,10 @@ #include "config.h" #include "Mapper.hxx" -#include "Directory.hxx" -#include "Song.hxx" #include "DetachedSong.hxx" -#include "LightSong.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "db/LightSong.hxx" #include "fs/AllocatedPath.hxx" #include "fs/Traits.hxx" #include "fs/Charset.hxx" diff --git a/src/PlaylistEdit.cxx b/src/PlaylistEdit.cxx index cbae02fef..87a64c5c9 100644 --- a/src/PlaylistEdit.cxx +++ b/src/PlaylistEdit.cxx @@ -32,7 +32,7 @@ #include "DetachedSong.hxx" #include "Mapper.hxx" #include "Idle.hxx" -#include "DatabaseSong.hxx" +#include "db/DatabaseSong.hxx" #include "Log.hxx" #include diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index befa45d94..0b0f8d32d 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -22,7 +22,7 @@ #include "PlaylistSave.hxx" #include "PlaylistInfo.hxx" #include "PlaylistVector.hxx" -#include "DatabaseSong.hxx" +#include "db/DatabaseSong.hxx" #include "DetachedSong.hxx" #include "Mapper.hxx" #include "fs/TextFile.hxx" diff --git a/src/PlaylistPrint.cxx b/src/PlaylistPrint.cxx index a0697f8cf..faf373be7 100644 --- a/src/PlaylistPrint.cxx +++ b/src/PlaylistPrint.cxx @@ -23,8 +23,8 @@ #include "Playlist.hxx" #include "queue/QueuePrint.hxx" #include "SongPrint.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabasePlugin.hxx" #include "client/Client.hxx" #include "input/InputStream.hxx" #include "DetachedSong.hxx" diff --git a/src/PlaylistUpdate.cxx b/src/PlaylistUpdate.cxx index 800ad49c9..114305960 100644 --- a/src/PlaylistUpdate.cxx +++ b/src/PlaylistUpdate.cxx @@ -19,9 +19,9 @@ #include "config.h" #include "Playlist.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" -#include "LightSong.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/LightSong.hxx" #include "DetachedSong.hxx" #include "tag/Tag.hxx" #include "Idle.hxx" diff --git a/src/PlaylistVector.cxx b/src/PlaylistVector.cxx index 094167e33..82a3519d9 100644 --- a/src/PlaylistVector.cxx +++ b/src/PlaylistVector.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "PlaylistVector.hxx" -#include "DatabaseLock.hxx" +#include "db/DatabaseLock.hxx" #include diff --git a/src/Song.cxx b/src/Song.cxx deleted file mode 100644 index 15924a40a..000000000 --- a/src/Song.cxx +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "Song.hxx" -#include "Directory.hxx" -#include "tag/Tag.hxx" -#include "util/VarSize.hxx" -#include "DetachedSong.hxx" -#include "LightSong.hxx" - -#include -#include -#include - -inline Song::Song(const char *_uri, size_t uri_length, Directory &_parent) - :parent(&_parent), mtime(0), start_ms(0), end_ms(0) -{ - memcpy(uri, _uri, uri_length + 1); -} - -inline Song::~Song() -{ -} - -static Song * -song_alloc(const char *uri, Directory &parent) -{ - size_t uri_length; - - assert(uri); - uri_length = strlen(uri); - assert(uri_length); - - return NewVarSize(sizeof(Song::uri), - uri_length + 1, - uri, uri_length, parent); -} - -Song * -Song::NewFrom(DetachedSong &&other, Directory &parent) -{ - Song *song = song_alloc(other.GetURI(), parent); - song->tag = std::move(other.WritableTag()); - song->mtime = other.GetLastModified(); - song->start_ms = other.GetStartMS(); - song->end_ms = other.GetEndMS(); - return song; -} - -Song * -Song::NewFile(const char *path, Directory &parent) -{ - return song_alloc(path, parent); -} - -void -Song::Free() -{ - DeleteVarSize(this); -} - -std::string -Song::GetURI() const -{ - assert(*uri); - - if (parent->IsRoot()) - return std::string(uri); - else { - const char *path = parent->GetPath(); - - std::string result; - result.reserve(strlen(path) + 1 + strlen(uri)); - result.assign(path); - result.push_back('/'); - result.append(uri); - return result; - } -} - -LightSong -Song::Export() const -{ - LightSong dest; - dest.directory = parent->IsRoot() - ? nullptr : parent->GetPath(); - dest.uri = uri; - dest.tag = &tag; - dest.mtime = mtime; - dest.start_ms = start_ms; - dest.end_ms = end_ms; - return dest; -} diff --git a/src/Song.hxx b/src/Song.hxx deleted file mode 100644 index 0b94fe6d0..000000000 --- a/src/Song.hxx +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_SONG_HXX -#define MPD_SONG_HXX - -#include "util/list.h" -#include "tag/Tag.hxx" -#include "Compiler.h" - -#include - -#include -#include - -struct LightSong; -struct Directory; -class DetachedSong; - -/** - * A song file inside the configured music directory. - */ -struct Song { - /** - * Pointers to the siblings of this directory within the - * parent directory. It is unused (undefined) if this song is - * not in the database. - * - * This attribute is protected with the global #db_mutex. - * Read access in the update thread does not need protection. - */ - struct list_head siblings; - - Tag tag; - - /** - * The #Directory that contains this song. May be nullptr if - * the current database plugin does not manage the parent - * directory this way. - */ - Directory *const parent; - - time_t mtime; - - /** - * Start of this sub-song within the file in milliseconds. - */ - unsigned start_ms; - - /** - * End of this sub-song within the file in milliseconds. - * Unused if zero. - */ - unsigned end_ms; - - /** - * The file name. If #parent is nullptr, then this is the URI - * relative to the music directory. - */ - char uri[sizeof(int)]; - - Song(const char *_uri, size_t uri_length, Directory &parent); - ~Song(); - - gcc_malloc - static Song *NewFrom(DetachedSong &&other, Directory &parent); - - /** allocate a new song with a local file name */ - gcc_malloc - static Song *NewFile(const char *path_utf8, Directory &parent); - - /** - * allocate a new song structure with a local file name and attempt to - * load its metadata. If all decoder plugin fail to read its meta - * data, nullptr is returned. - */ - gcc_malloc - static Song *LoadFile(const char *path_utf8, Directory &parent); - - void Free(); - - bool UpdateFile(); - bool UpdateFileInArchive(); - - /** - * Returns the URI of the song in UTF-8 encoding, including its - * location within the music directory. - */ - gcc_pure - std::string GetURI() const; - - gcc_pure - LightSong Export() const; -}; - -#endif diff --git a/src/SongFilter.cxx b/src/SongFilter.cxx index 594ac3abc..637150c37 100644 --- a/src/SongFilter.cxx +++ b/src/SongFilter.cxx @@ -19,8 +19,8 @@ #include "config.h" #include "SongFilter.hxx" -#include "Song.hxx" -#include "LightSong.hxx" +#include "db/Song.hxx" +#include "db/LightSong.hxx" #include "DetachedSong.hxx" #include "tag/Tag.hxx" #include "util/ASCII.hxx" diff --git a/src/SongPrint.cxx b/src/SongPrint.cxx index 18d732161..b0c9ed0a6 100644 --- a/src/SongPrint.cxx +++ b/src/SongPrint.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "SongPrint.hxx" -#include "LightSong.hxx" +#include "db/LightSong.hxx" #include "DetachedSong.hxx" #include "TimePrint.hxx" #include "TagPrint.hxx" diff --git a/src/SongSave.cxx b/src/SongSave.cxx index 790047bb6..d53e5bb62 100644 --- a/src/SongSave.cxx +++ b/src/SongSave.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "SongSave.hxx" -#include "Song.hxx" +#include "db/Song.hxx" #include "DetachedSong.hxx" #include "TagSave.hxx" #include "fs/TextFile.hxx" diff --git a/src/SongSort.cxx b/src/SongSort.cxx deleted file mode 100644 index dcea033b6..000000000 --- a/src/SongSort.cxx +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "SongSort.hxx" -#include "Song.hxx" -#include "tag/Tag.hxx" - -extern "C" { -#include "util/list_sort.h" -} - -#include - -#include - -static int -compare_utf8_string(const char *a, const char *b) -{ - if (a == nullptr) - return b == nullptr ? 0 : -1; - - if (b == nullptr) - return 1; - - return g_utf8_collate(a, b); -} - -/** - * Compare two string tag values, ignoring case. Either one may be - * nullptr. - */ -static int -compare_string_tag_item(const Tag &a, const Tag &b, - TagType type) -{ - return compare_utf8_string(a.GetValue(type), - b.GetValue(type)); -} - -/** - * Compare two tag values which should contain an integer value - * (e.g. disc or track number). Either one may be nullptr. - */ -static int -compare_number_string(const char *a, const char *b) -{ - long ai = a == nullptr ? 0 : strtol(a, nullptr, 10); - long bi = b == nullptr ? 0 : strtol(b, nullptr, 10); - - if (ai <= 0) - return bi <= 0 ? 0 : -1; - - if (bi <= 0) - return 1; - - return ai - bi; -} - -static int -compare_tag_item(const Tag &a, const Tag &b, TagType type) -{ - return compare_number_string(a.GetValue(type), - b.GetValue(type)); -} - -/* Only used for sorting/searchin a songvec, not general purpose compares */ -static int -song_cmp(gcc_unused void *priv, struct list_head *_a, struct list_head *_b) -{ - const Song *a = (const Song *)_a; - const Song *b = (const Song *)_b; - int ret; - - /* first sort by album */ - ret = compare_string_tag_item(a->tag, b->tag, TAG_ALBUM); - if (ret != 0) - return ret; - - /* then sort by disc */ - ret = compare_tag_item(a->tag, b->tag, TAG_DISC); - if (ret != 0) - return ret; - - /* then by track number */ - ret = compare_tag_item(a->tag, b->tag, TAG_TRACK); - if (ret != 0) - return ret; - - /* still no difference? compare file name */ - return g_utf8_collate(a->uri, b->uri); -} - -void -song_list_sort(struct list_head *songs) -{ - list_sort(nullptr, songs, song_cmp); -} diff --git a/src/SongSort.hxx b/src/SongSort.hxx deleted file mode 100644 index 28b903532..000000000 --- a/src/SongSort.hxx +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_SONG_SORT_HXX -#define MPD_SONG_SORT_HXX - -struct list_head; - -void -song_list_sort(list_head *songs); - -#endif diff --git a/src/SongUpdate.cxx b/src/SongUpdate.cxx index 571415ff1..0f3e9b172 100644 --- a/src/SongUpdate.cxx +++ b/src/SongUpdate.cxx @@ -18,10 +18,10 @@ */ #include "config.h" /* must be first for large file support */ -#include "Song.hxx" #include "DetachedSong.hxx" +#include "db/Song.hxx" +#include "db/Directory.hxx" #include "util/UriUtil.hxx" -#include "Directory.hxx" #include "Mapper.hxx" #include "fs/AllocatedPath.hxx" #include "fs/Traits.hxx" diff --git a/src/Stats.cxx b/src/Stats.cxx index 5b979b322..940a984da 100644 --- a/src/Stats.cxx +++ b/src/Stats.cxx @@ -21,10 +21,10 @@ #include "Stats.hxx" #include "PlayerControl.hxx" #include "client/Client.hxx" -#include "DatabaseSelection.hxx" -#include "DatabaseGlue.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseSimple.hxx" +#include "db/Selection.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/DatabaseSimple.hxx" #include "util/Error.hxx" #include "system/Clock.hxx" #include "Log.hxx" diff --git a/src/command/CommandError.cxx b/src/command/CommandError.cxx index cf3bf6851..73e363f24 100644 --- a/src/command/CommandError.cxx +++ b/src/command/CommandError.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "CommandError.hxx" -#include "DatabaseError.hxx" +#include "db/DatabaseError.hxx" #include "protocol/Result.hxx" #include "util/Error.hxx" #include "Log.hxx" diff --git a/src/command/DatabaseCommands.cxx b/src/command/DatabaseCommands.cxx index eaff1e3ec..2b871e565 100644 --- a/src/command/DatabaseCommands.cxx +++ b/src/command/DatabaseCommands.cxx @@ -19,10 +19,10 @@ #include "config.h" #include "DatabaseCommands.hxx" -#include "DatabaseQueue.hxx" -#include "DatabasePlaylist.hxx" -#include "DatabasePrint.hxx" -#include "DatabaseSelection.hxx" +#include "db/DatabaseQueue.hxx" +#include "db/DatabasePlaylist.hxx" +#include "db/DatabasePrint.hxx" +#include "db/Selection.hxx" #include "CommandError.hxx" #include "client/Client.hxx" #include "tag/Tag.hxx" diff --git a/src/command/OtherCommands.cxx b/src/command/OtherCommands.cxx index 4d61884c1..67d2aecf3 100644 --- a/src/command/OtherCommands.cxx +++ b/src/command/OtherCommands.cxx @@ -20,9 +20,9 @@ #include "config.h" #include "OtherCommands.hxx" #include "DatabaseCommands.hxx" +#include "db/update/UpdateGlue.hxx" #include "CommandError.hxx" -#include "update/UpdateGlue.hxx" -#include "Directory.hxx" +#include "db/Directory.hxx" #include "DetachedSong.hxx" #include "SongPrint.hxx" #include "TagPrint.hxx" diff --git a/src/command/PlayerCommands.cxx b/src/command/PlayerCommands.cxx index 759a37030..f703057cf 100644 --- a/src/command/PlayerCommands.cxx +++ b/src/command/PlayerCommands.cxx @@ -22,7 +22,7 @@ #include "CommandError.hxx" #include "Playlist.hxx" #include "PlaylistPrint.hxx" -#include "update/UpdateGlue.hxx" +#include "db/update/UpdateGlue.hxx" #include "client/Client.hxx" #include "Volume.hxx" #include "output/OutputAll.hxx" diff --git a/src/command/PlaylistCommands.cxx b/src/command/PlaylistCommands.cxx index 0441811c4..fbbb66757 100644 --- a/src/command/PlaylistCommands.cxx +++ b/src/command/PlaylistCommands.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "PlaylistCommands.hxx" -#include "DatabasePlaylist.hxx" +#include "db/DatabasePlaylist.hxx" #include "CommandError.hxx" #include "PlaylistPrint.hxx" #include "PlaylistSave.hxx" diff --git a/src/command/QueueCommands.cxx b/src/command/QueueCommands.cxx index e884c71c3..ed2b551c4 100644 --- a/src/command/QueueCommands.cxx +++ b/src/command/QueueCommands.cxx @@ -20,9 +20,9 @@ #include "config.h" #include "QueueCommands.hxx" #include "CommandError.hxx" -#include "DatabaseQueue.hxx" +#include "db/DatabaseQueue.hxx" #include "SongFilter.hxx" -#include "DatabaseSelection.hxx" +#include "db/Selection.hxx" #include "Playlist.hxx" #include "PlaylistPrint.hxx" #include "client/ClientFile.hxx" diff --git a/src/command/StickerCommands.cxx b/src/command/StickerCommands.cxx index cf1be076b..68a0d585f 100644 --- a/src/command/StickerCommands.cxx +++ b/src/command/StickerCommands.cxx @@ -20,10 +20,10 @@ #include "config.h" #include "StickerCommands.hxx" #include "SongPrint.hxx" -#include "DatabaseLock.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseGlue.hxx" -#include "DatabaseSimple.hxx" +#include "db/DatabaseLock.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/DatabaseGlue.hxx" +#include "db/DatabaseSimple.hxx" #include "sticker/SongSticker.hxx" #include "sticker/StickerPrint.hxx" #include "sticker/StickerDatabase.hxx" diff --git a/src/db/DatabaseError.cxx b/src/db/DatabaseError.cxx new file mode 100644 index 000000000..e0cbdd6a3 --- /dev/null +++ b/src/db/DatabaseError.cxx @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabaseError.hxx" +#include "util/Domain.hxx" + +const Domain db_domain("db"); diff --git a/src/db/DatabaseError.hxx b/src/db/DatabaseError.hxx new file mode 100644 index 000000000..1485a21b6 --- /dev/null +++ b/src/db/DatabaseError.hxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DB_ERROR_HXX +#define MPD_DB_ERROR_HXX + +class Domain; + +enum db_error { + /** + * The database is disabled, i.e. none is configured in this + * MPD instance. + */ + DB_DISABLED, + + DB_NOT_FOUND, +}; + +extern const Domain db_domain; + +#endif diff --git a/src/db/DatabaseGlue.cxx b/src/db/DatabaseGlue.cxx new file mode 100644 index 000000000..3734e156c --- /dev/null +++ b/src/db/DatabaseGlue.cxx @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabaseGlue.hxx" +#include "DatabaseSimple.hxx" +#include "Registry.hxx" +#include "DatabaseError.hxx" +#include "Directory.hxx" +#include "util/Error.hxx" +#include "config/ConfigData.hxx" +#include "Stats.hxx" +#include "DatabasePlugin.hxx" +#include "plugins/SimpleDatabasePlugin.hxx" + +#include +#include + +static Database *db; +static bool db_is_open; +static bool is_simple; + +bool +DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, Error &error) +{ + assert(db == nullptr); + assert(!db_is_open); + + const char *plugin_name = + param.GetBlockValue("plugin", "simple"); + is_simple = strcmp(plugin_name, "simple") == 0; + + const DatabasePlugin *plugin = GetDatabasePluginByName(plugin_name); + if (plugin == nullptr) { + error.Format(db_domain, + "No such database plugin: %s", plugin_name); + return false; + } + + db = plugin->create(loop, listener, param, error); + return db != nullptr; +} + +void +DatabaseGlobalDeinit(void) +{ + if (db_is_open) + db->Close(); + + if (db != nullptr) + delete db; +} + +const Database * +GetDatabase() +{ + assert(db == nullptr || db_is_open); + + return db; +} + +const Database * +GetDatabase(Error &error) +{ + assert(db == nullptr || db_is_open); + + if (db == nullptr) + error.Set(db_domain, DB_DISABLED, "No database"); + + return db; +} + +bool +db_is_simple(void) +{ + assert(db == nullptr || db_is_open); + + return is_simple; +} + +Directory * +db_get_root(void) +{ + assert(db != nullptr); + assert(db_is_simple()); + + return ((SimpleDatabase *)db)->GetRoot(); +} + +Directory * +db_get_directory(const char *name) +{ + if (db == nullptr) + return nullptr; + + Directory *music_root = db_get_root(); + if (name == nullptr) + return music_root; + + return music_root->LookupDirectory(name); +} + +bool +db_save(Error &error) +{ + assert(db != nullptr); + assert(db_is_open); + assert(db_is_simple()); + + return ((SimpleDatabase *)db)->Save(error); +} + +bool +DatabaseGlobalOpen(Error &error) +{ + assert(db != nullptr); + assert(!db_is_open); + + if (!db->Open(error)) + return false; + + db_is_open = true; + + return true; +} + +bool +db_exists() +{ + assert(db != nullptr); + assert(db_is_open); + assert(db_is_simple()); + + return ((SimpleDatabase *)db)->GetUpdateStamp() > 0; +} diff --git a/src/db/DatabaseGlue.hxx b/src/db/DatabaseGlue.hxx new file mode 100644 index 000000000..78032edb2 --- /dev/null +++ b/src/db/DatabaseGlue.hxx @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_GLUE_HXX +#define MPD_DATABASE_GLUE_HXX + +#include "Compiler.h" + +struct config_param; +class EventLoop; +class DatabaseListener; +class Database; +class Error; + +/** + * Initialize the database library. + * + * @param param the database configuration block + */ +bool +DatabaseGlobalInit(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, Error &error); + +void +DatabaseGlobalDeinit(void); + +bool +DatabaseGlobalOpen(Error &error); + +/** + * Returns the global #Database instance. May return nullptr if this MPD + * configuration has no database (no music_directory was configured). + */ +gcc_const +const Database * +GetDatabase(); + +/** + * Returns the global #Database instance. May return nullptr if this MPD + * configuration has no database (no music_directory was configured). + */ +gcc_pure +const Database * +GetDatabase(Error &error); + +#endif diff --git a/src/db/DatabaseListener.hxx b/src/db/DatabaseListener.hxx new file mode 100644 index 000000000..4da458866 --- /dev/null +++ b/src/db/DatabaseListener.hxx @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_CLIENT_HXX +#define MPD_DATABASE_CLIENT_HXX + +/** + * An object that listens to events from the #Database. + * + * @see #Instance + */ +class DatabaseListener { +public: + /** + * The database has been modified. This must be called in the + * thread that has created the #Database instance and that + * runs the #EventLoop. + */ + virtual void OnDatabaseModified() = 0; +}; + +#endif diff --git a/src/db/DatabaseLock.cxx b/src/db/DatabaseLock.cxx new file mode 100644 index 000000000..c0b5e4844 --- /dev/null +++ b/src/db/DatabaseLock.cxx @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabaseLock.hxx" + +Mutex db_mutex; + +#ifndef NDEBUG +ThreadId db_mutex_holder; +#endif diff --git a/src/db/DatabaseLock.hxx b/src/db/DatabaseLock.hxx new file mode 100644 index 000000000..9d0b0c152 --- /dev/null +++ b/src/db/DatabaseLock.hxx @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** \file + * + * Support for locking data structures from the database, for safe + * multi-threading. + */ + +#ifndef MPD_DB_LOCK_HXX +#define MPD_DB_LOCK_HXX + +#include "check.h" +#include "thread/Mutex.hxx" +#include "Compiler.h" + +#include + +extern Mutex db_mutex; + +#ifndef NDEBUG + +#include "thread/Id.hxx" + +extern ThreadId db_mutex_holder; + +/** + * Does the current thread hold the database lock? + */ +gcc_pure +static inline bool +holding_db_lock(void) +{ + return db_mutex_holder.IsInside(); +} + +#endif + +/** + * Obtain the global database lock. This is needed before + * dereferencing a #song or #directory. It is not recursive. + */ +static inline void +db_lock(void) +{ + assert(!holding_db_lock()); + + db_mutex.lock(); + + assert(db_mutex_holder.IsNull()); +#ifndef NDEBUG + db_mutex_holder = ThreadId::GetCurrent(); +#endif +} + +/** + * Release the global database lock. + */ +static inline void +db_unlock(void) +{ + assert(holding_db_lock()); +#ifndef NDEBUG + db_mutex_holder = ThreadId::Null(); +#endif + + db_mutex.unlock(); +} + +class ScopeDatabaseLock { +public: + ScopeDatabaseLock() { + db_lock(); + } + + ~ScopeDatabaseLock() { + db_unlock(); + } +}; + +#endif diff --git a/src/db/DatabasePlaylist.cxx b/src/db/DatabasePlaylist.cxx new file mode 100644 index 000000000..64b365d2a --- /dev/null +++ b/src/db/DatabasePlaylist.cxx @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabasePlaylist.hxx" +#include "Selection.hxx" +#include "PlaylistFile.hxx" +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" +#include "DetachedSong.hxx" +#include "Mapper.hxx" + +#include + +static bool +AddSong(const char *playlist_path_utf8, + const LightSong &song, Error &error) +{ + return spl_append_song(playlist_path_utf8, map_song_detach(song), + error); +} + +bool +search_add_to_playlist(const char *uri, const char *playlist_path_utf8, + const SongFilter *filter, + Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return false; + + const DatabaseSelection selection(uri, true, filter); + + using namespace std::placeholders; + const auto f = std::bind(AddSong, playlist_path_utf8, _1, _2); + return db->Visit(selection, f, error); +} diff --git a/src/db/DatabasePlaylist.hxx b/src/db/DatabasePlaylist.hxx new file mode 100644 index 000000000..1ee7584d3 --- /dev/null +++ b/src/db/DatabasePlaylist.hxx @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_PLAYLIST_HXX +#define MPD_DATABASE_PLAYLIST_HXX + +#include "Compiler.h" + +class SongFilter; +class Error; + +gcc_nonnull(1,2) +bool +search_add_to_playlist(const char *uri, const char *path_utf8, + const SongFilter *filter, + Error &error); + +#endif diff --git a/src/db/DatabasePlugin.hxx b/src/db/DatabasePlugin.hxx new file mode 100644 index 000000000..b0cb41502 --- /dev/null +++ b/src/db/DatabasePlugin.hxx @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** \file + * + * This header declares the db_plugin class. It describes a + * plugin API for databases of song metadata. + */ + +#ifndef MPD_DATABASE_PLUGIN_HXX +#define MPD_DATABASE_PLUGIN_HXX + +#include "Visitor.hxx" +#include "tag/TagType.h" +#include "Compiler.h" + +#include + +struct config_param; +struct DatabaseSelection; +struct db_visitor; +struct LightSong; +class Error; +class EventLoop; +class DatabaseListener; + +struct DatabaseStats { + /** + * Number of songs. + */ + unsigned song_count; + + /** + * Total duration of all songs (in seconds). + */ + unsigned long total_duration; + + /** + * Number of distinct artist names. + */ + unsigned artist_count; + + /** + * Number of distinct album names. + */ + unsigned album_count; + + void Clear() { + song_count = 0; + total_duration = 0; + artist_count = album_count = 0; + } +}; + +class Database { +public: + /** + * Free instance data. + */ + virtual ~Database() {} + + /** + * Open the database. Read it into memory if applicable. + */ + virtual bool Open(gcc_unused Error &error) { + return true; + } + + /** + * Close the database, free allocated memory. + */ + virtual void Close() {} + + /** + * Look up a song (including tag data) in the database. When + * you don't need this anymore, call ReturnSong(). + * + * @param uri_utf8 the URI of the song within the music + * directory (UTF-8) + */ + virtual const LightSong *GetSong(const char *uri_utf8, + Error &error) const = 0; + + /** + * Mark the song object as "unused". Call this on objects + * returned by GetSong(). + */ + virtual void ReturnSong(const LightSong *song) const = 0; + + /** + * Visit the selected entities. + */ + virtual bool Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const = 0; + + bool Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + Error &error) const { + return Visit(selection, visit_directory, visit_song, + VisitPlaylist(), error); + } + + bool Visit(const DatabaseSelection &selection, VisitSong visit_song, + Error &error) const { + return Visit(selection, VisitDirectory(), visit_song, error); + } + + /** + * Visit all unique tag values. + */ + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const = 0; + + virtual bool GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, + Error &error) const = 0; + + /** + * Returns the time stamp of the last database update. + * Returns 0 if that is not not known/available. + */ + gcc_pure + virtual time_t GetUpdateStamp() const = 0; +}; + +struct DatabasePlugin { + const char *name; + + /** + * Allocates and configures a database. + */ + Database *(*create)(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, + Error &error); +}; + +#endif diff --git a/src/db/DatabasePrint.cxx b/src/db/DatabasePrint.cxx new file mode 100644 index 000000000..9ed0b0826 --- /dev/null +++ b/src/db/DatabasePrint.cxx @@ -0,0 +1,250 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabasePrint.hxx" +#include "Selection.hxx" +#include "SongFilter.hxx" +#include "SongPrint.hxx" +#include "TimePrint.hxx" +#include "client/Client.hxx" +#include "tag/Tag.hxx" +#include "LightSong.hxx" +#include "LightDirectory.hxx" +#include "PlaylistInfo.hxx" +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" + +#include + +static bool +PrintDirectoryBrief(Client &client, const LightDirectory &directory) +{ + if (!directory.IsRoot()) + client_printf(client, "directory: %s\n", directory.GetPath()); + + return true; +} + +static bool +PrintDirectoryFull(Client &client, const LightDirectory &directory) +{ + if (!directory.IsRoot()) { + client_printf(client, "directory: %s\n", directory.GetPath()); + + if (directory.mtime > 0) + time_print(client, "Last-Modified", directory.mtime); + } + + return true; +} + +static void +print_playlist_in_directory(Client &client, + const char *directory, + const char *name_utf8) +{ + if (directory == nullptr) + client_printf(client, "playlist: %s\n", name_utf8); + else + client_printf(client, "playlist: %s/%s\n", + directory, name_utf8); +} + +static void +print_playlist_in_directory(Client &client, + const LightDirectory *directory, + const char *name_utf8) +{ + if (directory == nullptr || directory->IsRoot()) + client_printf(client, "playlist: %s\n", name_utf8); + else + client_printf(client, "playlist: %s/%s\n", + directory->GetPath(), name_utf8); +} + +static bool +PrintSongBrief(Client &client, const LightSong &song) +{ + song_print_uri(client, song); + + if (song.tag->has_playlist) + /* this song file has an embedded CUE sheet */ + print_playlist_in_directory(client, song.directory, song.uri); + + return true; +} + +static bool +PrintSongFull(Client &client, const LightSong &song) +{ + song_print_info(client, song); + + if (song.tag->has_playlist) + /* this song file has an embedded CUE sheet */ + print_playlist_in_directory(client, song.directory, song.uri); + + return true; +} + +static bool +PrintPlaylistBrief(Client &client, + const PlaylistInfo &playlist, + const LightDirectory &directory) +{ + print_playlist_in_directory(client, &directory, playlist.name.c_str()); + return true; +} + +static bool +PrintPlaylistFull(Client &client, + const PlaylistInfo &playlist, + const LightDirectory &directory) +{ + print_playlist_in_directory(client, &directory, playlist.name.c_str()); + + if (playlist.mtime > 0) + time_print(client, "Last-Modified", playlist.mtime); + + return true; +} + +bool +db_selection_print(Client &client, const DatabaseSelection &selection, + bool full, Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return false; + + using namespace std::placeholders; + const auto d = selection.filter == nullptr + ? std::bind(full ? PrintDirectoryFull : PrintDirectoryBrief, + std::ref(client), _1) + : VisitDirectory(); + const auto s = std::bind(full ? PrintSongFull : PrintSongBrief, + std::ref(client), _1); + const auto p = selection.filter == nullptr + ? std::bind(full ? PrintPlaylistFull : PrintPlaylistBrief, + std::ref(client), _1, _2) + : VisitPlaylist(); + + return db->Visit(selection, d, s, p, error); +} + +struct SearchStats { + int numberOfSongs; + unsigned long playTime; +}; + +static void printSearchStats(Client &client, SearchStats *stats) +{ + client_printf(client, "songs: %i\n", stats->numberOfSongs); + client_printf(client, "playtime: %li\n", stats->playTime); +} + +static bool +stats_visitor_song(SearchStats &stats, const LightSong &song) +{ + stats.numberOfSongs++; + stats.playTime += song.GetDuration(); + + return true; +} + +bool +searchStatsForSongsIn(Client &client, const char *name, + const SongFilter *filter, + Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return false; + + const DatabaseSelection selection(name, true, filter); + + SearchStats stats; + stats.numberOfSongs = 0; + stats.playTime = 0; + + using namespace std::placeholders; + const auto f = std::bind(stats_visitor_song, std::ref(stats), + _1); + if (!db->Visit(selection, f, error)) + return false; + + printSearchStats(client, &stats); + return true; +} + +bool +printAllIn(Client &client, const char *uri_utf8, Error &error) +{ + const DatabaseSelection selection(uri_utf8, true); + return db_selection_print(client, selection, false, error); +} + +bool +printInfoForAllIn(Client &client, const char *uri_utf8, + Error &error) +{ + const DatabaseSelection selection(uri_utf8, true); + return db_selection_print(client, selection, true, error); +} + +static bool +PrintSongURIVisitor(Client &client, const LightSong &song) +{ + song_print_uri(client, song); + + return true; +} + +static bool +PrintUniqueTag(Client &client, TagType tag_type, + const char *value) +{ + client_printf(client, "%s: %s\n", tag_item_names[tag_type], value); + return true; +} + +bool +listAllUniqueTags(Client &client, int type, + const SongFilter *filter, + Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return false; + + const DatabaseSelection selection("", true, filter); + + if (type == LOCATE_TAG_FILE_TYPE) { + using namespace std::placeholders; + const auto f = std::bind(PrintSongURIVisitor, + std::ref(client), _1); + return db->Visit(selection, f, error); + } else { + using namespace std::placeholders; + const auto f = std::bind(PrintUniqueTag, std::ref(client), + (TagType)type, _1); + return db->VisitUniqueTags(selection, (TagType)type, + f, error); + } +} diff --git a/src/db/DatabasePrint.hxx b/src/db/DatabasePrint.hxx new file mode 100644 index 000000000..2007e256b --- /dev/null +++ b/src/db/DatabasePrint.hxx @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DB_PRINT_H +#define MPD_DB_PRINT_H + +#include "Compiler.h" + +class SongFilter; +struct DatabaseSelection; +class Client; +class Error; + +bool +db_selection_print(Client &client, const DatabaseSelection &selection, + bool full, Error &error); + +gcc_nonnull(2) +bool +printAllIn(Client &client, const char *uri_utf8, Error &error); + +gcc_nonnull(2) +bool +printInfoForAllIn(Client &client, const char *uri_utf8, + Error &error); + +gcc_nonnull(2) +bool +searchStatsForSongsIn(Client &client, const char *name, + const SongFilter *filter, + Error &error); + +bool +listAllUniqueTags(Client &client, int type, + const SongFilter *filter, + Error &error); + +#endif diff --git a/src/db/DatabaseQueue.cxx b/src/db/DatabaseQueue.cxx new file mode 100644 index 000000000..ee1dbd57c --- /dev/null +++ b/src/db/DatabaseQueue.cxx @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabaseQueue.hxx" +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" +#include "Partition.hxx" +#include "util/Error.hxx" +#include "DetachedSong.hxx" +#include "Mapper.hxx" + +#include + +static bool +AddToQueue(Partition &partition, const LightSong &song, Error &error) +{ + PlaylistResult result = + partition.playlist.AppendSong(partition.pc, + map_song_detach(song), + nullptr); + if (result != PlaylistResult::SUCCESS) { + error.Set(playlist_domain, int(result), "Playlist error"); + return false; + } + + return true; +} + +bool +AddFromDatabase(Partition &partition, const DatabaseSelection &selection, + Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return false; + + using namespace std::placeholders; + const auto f = std::bind(AddToQueue, std::ref(partition), _1, _2); + return db->Visit(selection, f, error); +} diff --git a/src/db/DatabaseQueue.hxx b/src/db/DatabaseQueue.hxx new file mode 100644 index 000000000..e653f973c --- /dev/null +++ b/src/db/DatabaseQueue.hxx @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_QUEUE_HXX +#define MPD_DATABASE_QUEUE_HXX + +struct Partition; +struct DatabaseSelection; +class Error; + +bool +AddFromDatabase(Partition &partition, const DatabaseSelection &selection, + Error &error); + +#endif diff --git a/src/db/DatabaseSave.cxx b/src/db/DatabaseSave.cxx new file mode 100644 index 000000000..e9c81442b --- /dev/null +++ b/src/db/DatabaseSave.cxx @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabaseSave.hxx" +#include "DatabaseLock.hxx" +#include "DatabaseError.hxx" +#include "Directory.hxx" +#include "DirectorySave.hxx" +#include "fs/TextFile.hxx" +#include "tag/Tag.hxx" +#include "tag/TagSettings.h" +#include "fs/Charset.hxx" +#include "util/StringUtil.hxx" +#include "util/Error.hxx" +#include "Log.hxx" + +#include +#include + +#define DIRECTORY_INFO_BEGIN "info_begin" +#define DIRECTORY_INFO_END "info_end" +#define DB_FORMAT_PREFIX "format: " +#define DIRECTORY_MPD_VERSION "mpd_version: " +#define DIRECTORY_FS_CHARSET "fs_charset: " +#define DB_TAG_PREFIX "tag: " + +static constexpr unsigned DB_FORMAT = 1; + +void +db_save_internal(FILE *fp, const Directory &music_root) +{ + fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN); + fprintf(fp, DB_FORMAT_PREFIX "%u\n", DB_FORMAT); + fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION); + fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset()); + + for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) + if (!ignore_tag_items[i]) + fprintf(fp, DB_TAG_PREFIX "%s\n", tag_item_names[i]); + + fprintf(fp, "%s\n", DIRECTORY_INFO_END); + + directory_save(fp, music_root); +} + +bool +db_load_internal(TextFile &file, Directory &music_root, Error &error) +{ + char *line; + unsigned format = 0; + bool found_charset = false, found_version = false; + bool success; + bool tags[TAG_NUM_OF_ITEM_TYPES]; + + /* get initial info */ + line = file.ReadLine(); + if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) { + error.Set(db_domain, "Database corrupted"); + return false; + } + + memset(tags, false, sizeof(tags)); + + while ((line = file.ReadLine()) != nullptr && + strcmp(line, DIRECTORY_INFO_END) != 0) { + if (StringStartsWith(line, DB_FORMAT_PREFIX)) { + format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1); + } else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) { + if (found_version) { + error.Set(db_domain, "Duplicate version line"); + return false; + } + + found_version = true; + } else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) { + const char *new_charset; + + if (found_charset) { + error.Set(db_domain, "Duplicate charset line"); + return false; + } + + found_charset = true; + + new_charset = line + sizeof(DIRECTORY_FS_CHARSET) - 1; + const char *const old_charset = GetFSCharset(); + if (*old_charset != 0 + && strcmp(new_charset, old_charset) != 0) { + error.Format(db_domain, + "Existing database has charset " + "\"%s\" instead of \"%s\"; " + "discarding database file", + new_charset, old_charset); + return false; + } + } else if (StringStartsWith(line, DB_TAG_PREFIX)) { + const char *name = line + sizeof(DB_TAG_PREFIX) - 1; + TagType tag = tag_name_parse(name); + if (tag == TAG_NUM_OF_ITEM_TYPES) { + error.Format(db_domain, + "Unrecognized tag '%s', " + "discarding database file", + name); + return false; + } + + tags[tag] = true; + } else { + error.Format(db_domain, "Malformed line: %s", line); + return false; + } + } + + if (format != DB_FORMAT) { + error.Set(db_domain, + "Database format mismatch, " + "discarding database file"); + return false; + } + + for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) { + if (!ignore_tag_items[i] && !tags[i]) { + error.Set(db_domain, + "Tag list mismatch, " + "discarding database file"); + return false; + } + } + + LogDebug(db_domain, "reading DB"); + + db_lock(); + success = directory_load(file, music_root, error); + db_unlock(); + + return success; +} diff --git a/src/db/DatabaseSave.hxx b/src/db/DatabaseSave.hxx new file mode 100644 index 000000000..3bd3377ae --- /dev/null +++ b/src/db/DatabaseSave.hxx @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_SAVE_HXX +#define MPD_DATABASE_SAVE_HXX + +#include + +struct Directory; +class TextFile; +class Error; + +void +db_save_internal(FILE *file, const Directory &root); + +bool +db_load_internal(TextFile &file, Directory &root, Error &error); + +#endif diff --git a/src/db/DatabaseSimple.hxx b/src/db/DatabaseSimple.hxx new file mode 100644 index 000000000..b99b3bfa5 --- /dev/null +++ b/src/db/DatabaseSimple.hxx @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_SIMPLE_HXX +#define MPD_DATABASE_SIMPLE_HXX + +#include "Compiler.h" + +#include + +struct config_param; +struct Directory; +struct db_selection; +struct db_visitor; +class Error; + +/** + * Check whether the default #SimpleDatabasePlugin is used. This + * allows using db_get_root(), db_save(), db_get_mtime() and + * db_exists(). + */ +bool +db_is_simple(void); + +/** + * Returns the root directory object. Returns NULL if there is no + * configured music directory. + * + * May only be used if db_is_simple() returns true. + */ +gcc_pure +Directory * +db_get_root(void); + +/** + * Caller must lock the #db_mutex. + */ +gcc_nonnull(1) +gcc_pure +Directory * +db_get_directory(const char *name); + +/** + * May only be used if db_is_simple() returns true. + */ +bool +db_save(Error &error); + +/** + * Returns true if there is a valid database file on the disk. + * + * May only be used if db_is_simple() returns true. + */ +gcc_pure +bool +db_exists(); + +#endif diff --git a/src/db/DatabaseSong.cxx b/src/db/DatabaseSong.cxx new file mode 100644 index 000000000..592d38b85 --- /dev/null +++ b/src/db/DatabaseSong.cxx @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DatabaseSong.hxx" +#include "DatabaseGlue.hxx" +#include "DatabasePlugin.hxx" +#include "DetachedSong.hxx" +#include "Mapper.hxx" + +DetachedSong * +DatabaseDetachSong(const char *uri, Error &error) +{ + const Database *db = GetDatabase(error); + if (db == nullptr) + return nullptr; + + const LightSong *tmp = db->GetSong(uri, error); + if (tmp == nullptr) + return nullptr; + + DetachedSong *song = new DetachedSong(map_song_detach(*tmp)); + db->ReturnSong(tmp); + return song; +} diff --git a/src/db/DatabaseSong.hxx b/src/db/DatabaseSong.hxx new file mode 100644 index 000000000..0200af6b8 --- /dev/null +++ b/src/db/DatabaseSong.hxx @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_SONG_HXX +#define MPD_DATABASE_SONG_HXX + +#include "Compiler.h" + +class DetachedSong; +class Error; + +/** + * Look up a song in the database and convert it to a #DetachedSong + * instance. The caller is responsible for freeing it. + * + * @return nullptr on error + */ +gcc_malloc gcc_nonnull_all +DetachedSong * +DatabaseDetachSong(const char *uri, Error &error); + +#endif diff --git a/src/db/Directory.cxx b/src/db/Directory.cxx new file mode 100644 index 000000000..e74eabd19 --- /dev/null +++ b/src/db/Directory.cxx @@ -0,0 +1,300 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Directory.hxx" +#include "LightDirectory.hxx" +#include "SongFilter.hxx" +#include "PlaylistVector.hxx" +#include "db/DatabaseLock.hxx" +#include "SongSort.hxx" +#include "Song.hxx" +#include "LightSong.hxx" +#include "fs/Traits.hxx" +#include "util/Alloc.hxx" +#include "util/Error.hxx" + +extern "C" { +#include "util/list_sort.h" +} + +#include + +#include +#include +#include + +Directory::Directory(const char *_path_utf8, Directory *_parent) + :parent(_parent), + mtime(0), have_stat(false), + path(_path_utf8) +{ + INIT_LIST_HEAD(&children); + INIT_LIST_HEAD(&songs); +} + +Directory::~Directory() +{ + Song *song, *ns; + directory_for_each_song_safe(song, ns, *this) + song->Free(); + + Directory *child, *n; + directory_for_each_child_safe(child, n, *this) + delete child; +} + +void +Directory::Delete() +{ + assert(holding_db_lock()); + assert(parent != nullptr); + + list_del(&siblings); + delete this; +} + +const char * +Directory::GetName() const +{ + assert(!IsRoot()); + + return PathTraitsUTF8::GetBase(path.c_str()); +} + +Directory * +Directory::CreateChild(const char *name_utf8) +{ + assert(holding_db_lock()); + assert(name_utf8 != nullptr); + assert(*name_utf8 != 0); + + char *allocated; + const char *path_utf8; + if (IsRoot()) { + allocated = nullptr; + path_utf8 = name_utf8; + } else { + allocated = g_strconcat(GetPath(), + "/", name_utf8, nullptr); + path_utf8 = allocated; + } + + Directory *child = new Directory(path_utf8, this); + g_free(allocated); + + list_add_tail(&child->siblings, &children); + return child; +} + +const Directory * +Directory::FindChild(const char *name) const +{ + assert(holding_db_lock()); + + const Directory *child; + directory_for_each_child(child, *this) + if (strcmp(child->GetName(), name) == 0) + return child; + + return nullptr; +} + +void +Directory::PruneEmpty() +{ + assert(holding_db_lock()); + + Directory *child, *n; + directory_for_each_child_safe(child, n, *this) { + child->PruneEmpty(); + + if (child->IsEmpty()) + child->Delete(); + } +} + +Directory * +Directory::LookupDirectory(const char *uri) +{ + assert(holding_db_lock()); + assert(uri != nullptr); + + if (isRootDirectory(uri)) + return this; + + char *duplicated = xstrdup(uri), *name = duplicated; + + Directory *d = this; + while (1) { + char *slash = strchr(name, '/'); + if (slash == name) { + d = nullptr; + break; + } + + if (slash != nullptr) + *slash = '\0'; + + d = d->FindChild(name); + if (d == nullptr || slash == nullptr) + break; + + name = slash + 1; + } + + free(duplicated); + + return d; +} + +void +Directory::AddSong(Song *song) +{ + assert(holding_db_lock()); + assert(song != nullptr); + assert(song->parent == this); + + list_add_tail(&song->siblings, &songs); +} + +void +Directory::RemoveSong(Song *song) +{ + assert(holding_db_lock()); + assert(song != nullptr); + assert(song->parent == this); + + list_del(&song->siblings); +} + +const Song * +Directory::FindSong(const char *name_utf8) const +{ + assert(holding_db_lock()); + assert(name_utf8 != nullptr); + + Song *song; + directory_for_each_song(song, *this) { + assert(song->parent == this); + + if (strcmp(song->uri, name_utf8) == 0) + return song; + } + + return nullptr; +} + +Song * +Directory::LookupSong(const char *uri) +{ + char *duplicated, *base; + + assert(holding_db_lock()); + assert(uri != nullptr); + + duplicated = xstrdup(uri); + base = strrchr(duplicated, '/'); + + Directory *d = this; + if (base != nullptr) { + *base++ = 0; + d = d->LookupDirectory(duplicated); + if (d == nullptr) { + free(duplicated); + return nullptr; + } + } else + base = duplicated; + + Song *song = d->FindSong(base); + assert(song == nullptr || song->parent == d); + + free(duplicated); + return song; + +} + +static int +directory_cmp(gcc_unused void *priv, + struct list_head *_a, struct list_head *_b) +{ + const Directory *a = (const Directory *)_a; + const Directory *b = (const Directory *)_b; + return g_utf8_collate(a->path.c_str(), b->path.c_str()); +} + +void +Directory::Sort() +{ + assert(holding_db_lock()); + + list_sort(nullptr, &children, directory_cmp); + song_list_sort(&songs); + + Directory *child; + directory_for_each_child(child, *this) + child->Sort(); +} + +bool +Directory::Walk(bool recursive, const SongFilter *filter, + VisitDirectory visit_directory, VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const +{ + assert(!error.IsDefined()); + + if (visit_song) { + Song *song; + directory_for_each_song(song, *this) { + const LightSong song2 = song->Export(); + if ((filter == nullptr || filter->Match(song2)) && + !visit_song(song2, error)) + return false; + } + } + + if (visit_playlist) { + for (const PlaylistInfo &p : playlists) + if (!visit_playlist(p, Export(), error)) + return false; + } + + Directory *child; + directory_for_each_child(child, *this) { + if (visit_directory && + !visit_directory(child->Export(), error)) + return false; + + if (recursive && + !child->Walk(recursive, filter, + visit_directory, visit_song, visit_playlist, + error)) + return false; + } + + return true; +} + +LightDirectory +Directory::Export() const +{ + return LightDirectory(GetPath(), mtime); +} diff --git a/src/db/Directory.hxx b/src/db/Directory.hxx new file mode 100644 index 000000000..e114b27f4 --- /dev/null +++ b/src/db/Directory.hxx @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DIRECTORY_HXX +#define MPD_DIRECTORY_HXX + +#include "check.h" +#include "util/list.h" +#include "Compiler.h" +#include "db/Visitor.hxx" +#include "PlaylistVector.hxx" + +#include + +#include + +#define DEVICE_INARCHIVE (dev_t)(-1) +#define DEVICE_CONTAINER (dev_t)(-2) + +#define directory_for_each_child(pos, directory) \ + list_for_each_entry(pos, &(directory).children, siblings) + +#define directory_for_each_child_safe(pos, n, directory) \ + list_for_each_entry_safe(pos, n, &(directory).children, siblings) + +#define directory_for_each_song(pos, directory) \ + list_for_each_entry(pos, &(directory).songs, siblings) + +#define directory_for_each_song_safe(pos, n, directory) \ + list_for_each_entry_safe(pos, n, &(directory).songs, siblings) + +struct Song; +struct db_visitor; +class SongFilter; +class Error; + +struct Directory { + /** + * Pointers to the siblings of this directory within the + * parent directory. It is unused (undefined) in the root + * directory. + * + * This attribute is protected with the global #db_mutex. + * Read access in the update thread does not need protection. + */ + struct list_head siblings; + + /** + * A doubly linked list of child directories. + * + * This attribute is protected with the global #db_mutex. + * Read access in the update thread does not need protection. + */ + struct list_head children; + + /** + * A doubly linked list of songs within this directory. + * + * This attribute is protected with the global #db_mutex. + * Read access in the update thread does not need protection. + */ + struct list_head songs; + + PlaylistVector playlists; + + Directory *parent; + time_t mtime; + ino_t inode; + dev_t device; + bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */ + + std::string path; + +public: + Directory(const char *_path_utf8, Directory *_parent); + ~Directory(); + + /** + * Create a new root #Directory object. + */ + gcc_malloc + static Directory *NewRoot() { + return new Directory("", nullptr); + } + + /** + * Remove this #Directory object from its parent and free it. This + * must not be called with the root Directory. + * + * Caller must lock the #db_mutex. + */ + void Delete(); + + /** + * Create a new #Directory object as a child of the given one. + * + * Caller must lock the #db_mutex. + * + * @param name_utf8 the UTF-8 encoded name of the new sub directory + */ + gcc_malloc + Directory *CreateChild(const char *name_utf8); + + /** + * Caller must lock the #db_mutex. + */ + gcc_pure + const Directory *FindChild(const char *name) const; + + gcc_pure + Directory *FindChild(const char *name) { + const Directory *cthis = this; + return const_cast(cthis->FindChild(name)); + } + + /** + * Look up a sub directory, and create the object if it does not + * exist. + * + * Caller must lock the #db_mutex. + */ + Directory *MakeChild(const char *name_utf8) { + Directory *child = FindChild(name_utf8); + if (child == nullptr) + child = CreateChild(name_utf8); + return child; + } + + /** + * Looks up a directory by its relative URI. + * + * @param uri the relative URI + * @return the Directory, or nullptr if none was found + */ + gcc_pure + Directory *LookupDirectory(const char *uri); + + gcc_pure + bool IsEmpty() const { + return list_empty(&children) && + list_empty(&songs) && + playlists.empty(); + } + + gcc_pure + const char *GetPath() const { + return path.c_str(); + } + + /** + * Returns the base name of the directory. + */ + gcc_pure + const char *GetName() const; + + /** + * Is this the root directory of the music database? + */ + gcc_pure + bool IsRoot() const { + return parent == nullptr; + } + + /** + * Look up a song in this directory by its name. + * + * Caller must lock the #db_mutex. + */ + gcc_pure + const Song *FindSong(const char *name_utf8) const; + + gcc_pure + Song *FindSong(const char *name_utf8) { + const Directory *cthis = this; + return const_cast(cthis->FindSong(name_utf8)); + } + + /** + * Looks up a song by its relative URI. + * + * Caller must lock the #db_mutex. + * + * @param uri the relative URI + * @return the song, or nullptr if none was found + */ + gcc_pure + Song *LookupSong(const char *uri); + + /** + * Add a song object to this directory. Its "parent" attribute must + * be set already. + */ + void AddSong(Song *song); + + /** + * Remove a song object from this directory (which effectively + * invalidates the song object, because the "parent" attribute becomes + * stale), but does not free it. + */ + void RemoveSong(Song *song); + + /** + * Caller must lock the #db_mutex. + */ + void PruneEmpty(); + + /** + * Sort all directory entries recursively. + * + * Caller must lock the #db_mutex. + */ + void Sort(); + + /** + * Caller must lock #db_mutex. + */ + bool Walk(bool recursive, const SongFilter *match, + VisitDirectory visit_directory, VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const; + + gcc_pure + LightDirectory Export() const; +}; + +static inline bool +isRootDirectory(const char *name) +{ + return name[0] == 0 || (name[0] == '/' && name[1] == 0); +} + +#endif diff --git a/src/db/DirectorySave.cxx b/src/db/DirectorySave.cxx new file mode 100644 index 000000000..499f84734 --- /dev/null +++ b/src/db/DirectorySave.cxx @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "DirectorySave.hxx" +#include "Directory.hxx" +#include "Song.hxx" +#include "SongSave.hxx" +#include "DetachedSong.hxx" +#include "PlaylistDatabase.hxx" +#include "fs/TextFile.hxx" +#include "util/StringUtil.hxx" +#include "util/NumberParser.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" + +#include + +#define DIRECTORY_DIR "directory: " +#define DIRECTORY_MTIME "mtime: " +#define DIRECTORY_BEGIN "begin: " +#define DIRECTORY_END "end: " + +static constexpr Domain directory_domain("directory"); + +void +directory_save(FILE *fp, const Directory &directory) +{ + if (!directory.IsRoot()) { + fprintf(fp, DIRECTORY_MTIME "%lu\n", + (unsigned long)directory.mtime); + + fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory.GetPath()); + } + + Directory *cur; + directory_for_each_child(cur, directory) { + fprintf(fp, DIRECTORY_DIR "%s\n", cur->GetName()); + + directory_save(fp, *cur); + + if (ferror(fp)) + return; + } + + Song *song; + directory_for_each_song(song, directory) + song_save(fp, *song); + + playlist_vector_save(fp, directory.playlists); + + if (!directory.IsRoot()) + fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath()); +} + +static Directory * +directory_load_subdir(TextFile &file, Directory &parent, const char *name, + Error &error) +{ + bool success; + + if (parent.FindChild(name) != nullptr) { + error.Format(directory_domain, + "Duplicate subdirectory '%s'", name); + return nullptr; + } + + Directory *directory = parent.CreateChild(name); + + const char *line = file.ReadLine(); + if (line == nullptr) { + error.Set(directory_domain, "Unexpected end of file"); + directory->Delete(); + return nullptr; + } + + if (StringStartsWith(line, DIRECTORY_MTIME)) { + directory->mtime = + ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1); + + line = file.ReadLine(); + if (line == nullptr) { + error.Set(directory_domain, "Unexpected end of file"); + directory->Delete(); + return nullptr; + } + } + + if (!StringStartsWith(line, DIRECTORY_BEGIN)) { + error.Format(directory_domain, "Malformed line: %s", line); + directory->Delete(); + return nullptr; + } + + success = directory_load(file, *directory, error); + if (!success) { + directory->Delete(); + return nullptr; + } + + return directory; +} + +bool +directory_load(TextFile &file, Directory &directory, Error &error) +{ + const char *line; + + while ((line = file.ReadLine()) != nullptr && + !StringStartsWith(line, DIRECTORY_END)) { + if (StringStartsWith(line, DIRECTORY_DIR)) { + Directory *subdir = + directory_load_subdir(file, directory, + line + sizeof(DIRECTORY_DIR) - 1, + error); + if (subdir == nullptr) + return false; + } else if (StringStartsWith(line, SONG_BEGIN)) { + const char *name = line + sizeof(SONG_BEGIN) - 1; + + if (directory.FindSong(name) != nullptr) { + error.Format(directory_domain, + "Duplicate song '%s'", name); + return false; + } + + DetachedSong *song = song_load(file, name, error); + if (song == nullptr) + return false; + + directory.AddSong(Song::NewFrom(std::move(*song), + directory)); + delete song; + } else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) { + const char *name = line + sizeof(PLAYLIST_META_BEGIN) - 1; + if (!playlist_metadata_load(file, directory.playlists, + name, error)) + return false; + } else { + error.Format(directory_domain, + "Malformed line: %s", line); + return false; + } + } + + return true; +} diff --git a/src/db/DirectorySave.hxx b/src/db/DirectorySave.hxx new file mode 100644 index 000000000..07e9e158b --- /dev/null +++ b/src/db/DirectorySave.hxx @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DIRECTORY_SAVE_HXX +#define MPD_DIRECTORY_SAVE_HXX + +#include + +struct Directory; +class TextFile; +class Error; + +void +directory_save(FILE *fp, const Directory &directory); + +bool +directory_load(TextFile &file, Directory &directory, Error &error); + +#endif diff --git a/src/db/Helpers.cxx b/src/db/Helpers.cxx new file mode 100644 index 000000000..579b83e15 --- /dev/null +++ b/src/db/Helpers.cxx @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Helpers.hxx" +#include "DatabasePlugin.hxx" +#include "LightSong.hxx" +#include "tag/Tag.hxx" + +#include +#include + +#include + +struct StringLess { + gcc_pure + bool operator()(const char *a, const char *b) const { + return strcmp(a, b) < 0; + } +}; + +typedef std::set StringSet; + +static bool +CollectTags(StringSet &set, TagType tag_type, const LightSong &song) +{ + const Tag *tag = song.tag; + + bool found = false; + for (unsigned i = 0; i < tag->num_items; ++i) { + if (tag->items[i]->type == tag_type) { + set.insert(tag->items[i]->value); + found = true; + } + } + + if (!found) + set.insert(""); + + return true; +} + +bool +VisitUniqueTags(const Database &db, const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) +{ + StringSet set; + + using namespace std::placeholders; + const auto f = std::bind(CollectTags, std::ref(set), tag_type, _1); + if (!db.Visit(selection, f, error)) + return false; + + for (auto value : set) + if (!visit_string(value, error)) + return false; + + return true; +} + +static void +StatsVisitTag(DatabaseStats &stats, StringSet &artists, StringSet &albums, + const Tag &tag) +{ + if (tag.time > 0) + stats.total_duration += tag.time; + + for (unsigned i = 0; i < tag.num_items; ++i) { + const TagItem &item = *tag.items[i]; + + switch (item.type) { + case TAG_ARTIST: + artists.insert(item.value); + break; + + case TAG_ALBUM: + albums.insert(item.value); + break; + + default: + break; + } + } +} + +static bool +StatsVisitSong(DatabaseStats &stats, StringSet &artists, StringSet &albums, + const LightSong &song) +{ + ++stats.song_count; + + StatsVisitTag(stats, artists, albums, *song.tag); + + return true; +} + +bool +GetStats(const Database &db, const DatabaseSelection &selection, + DatabaseStats &stats, Error &error) +{ + stats.Clear(); + + StringSet artists, albums; + using namespace std::placeholders; + const auto f = std::bind(StatsVisitSong, + std::ref(stats), std::ref(artists), + std::ref(albums), _1); + if (!db.Visit(selection, f, error)) + return false; + + stats.artist_count = artists.size(); + stats.album_count = albums.size(); + return true; +} diff --git a/src/db/Helpers.hxx b/src/db/Helpers.hxx new file mode 100644 index 000000000..24db260c0 --- /dev/null +++ b/src/db/Helpers.hxx @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_MEMORY_DATABASE_PLUGIN_HXX +#define MPD_MEMORY_DATABASE_PLUGIN_HXX + +#include "Visitor.hxx" +#include "tag/TagType.h" + +class Error; +class Database; +struct DatabaseSelection; +struct DatabaseStats; + +bool +VisitUniqueTags(const Database &db, const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error); + +bool +GetStats(const Database &db, const DatabaseSelection &selection, + DatabaseStats &stats, Error &error); + +#endif diff --git a/src/db/LazyDatabase.cxx b/src/db/LazyDatabase.cxx deleted file mode 100644 index 6a01ffb82..000000000 --- a/src/db/LazyDatabase.cxx +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "LazyDatabase.hxx" - -#include - -LazyDatabase::~LazyDatabase() -{ - assert(!open); - - delete db; -} - -bool -LazyDatabase::EnsureOpen(Error &error) const -{ - if (open) - return true; - - if (!db->Open(error)) - return false; - - open = true; - return true; -} - -void -LazyDatabase::Close() -{ - if (open) { - open = false; - db->Close(); - } -} - -const LightSong * -LazyDatabase::GetSong(const char *uri, Error &error) const -{ - return EnsureOpen(error) - ? db->GetSong(uri, error) - : nullptr; -} - -void -LazyDatabase::ReturnSong(const LightSong *song) const -{ - assert(open); - - db->ReturnSong(song); -} - -bool -LazyDatabase::Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const -{ - return EnsureOpen(error) && - db->Visit(selection, visit_directory, visit_song, - visit_playlist, error); -} - -bool -LazyDatabase::VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const -{ - return EnsureOpen(error) && - db->VisitUniqueTags(selection, tag_type, visit_string, error); -} - -bool -LazyDatabase::GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, Error &error) const -{ - return EnsureOpen(error) && db->GetStats(selection, stats, error); -} - -time_t -LazyDatabase::GetUpdateStamp() const -{ - return open ? db->GetUpdateStamp() : 0; -} diff --git a/src/db/LazyDatabase.hxx b/src/db/LazyDatabase.hxx deleted file mode 100644 index f718ecb3f..000000000 --- a/src/db/LazyDatabase.hxx +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_LAZY_DATABASE_PLUGIN_HXX -#define MPD_LAZY_DATABASE_PLUGIN_HXX - -#include "DatabasePlugin.hxx" - -/** - * A wrapper for a #Database object that gets opened on the first - * database access. This works around daemonization problems with - * some plugins. - */ -class LazyDatabase final : public Database { - Database *const db; - - mutable bool open; - -public: - gcc_nonnull_all - LazyDatabase(Database *_db) - :db(_db), open(false) {} - - virtual ~LazyDatabase(); - - virtual void Close() override; - - virtual const LightSong *GetSong(const char *uri_utf8, - Error &error) const override; - virtual void ReturnSong(const LightSong *song) const; - - virtual bool Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const override; - - virtual bool VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const override; - - virtual bool GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, - Error &error) const override; - - virtual time_t GetUpdateStamp() const override; - -private: - bool EnsureOpen(Error &error) const; -}; - -#endif diff --git a/src/db/LightDirectory.hxx b/src/db/LightDirectory.hxx new file mode 100644 index 000000000..d134151a4 --- /dev/null +++ b/src/db/LightDirectory.hxx @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_LIGHT_DIRECTORY_HXX +#define MPD_LIGHT_DIRECTORY_HXX + +#include "Compiler.h" + +#include + +#include + +struct Tag; + +/** + * A reference to a directory. Unlike the #Directory class, this one + * consists only of pointers. It is supposed to be as light as + * possible while still providing all the information MPD has about a + * directory. This class does not manage any memory, and the pointers + * become invalid quickly. Only to be used to pass around during + * well-defined situations. + */ +struct LightDirectory { + const char *uri; + + time_t mtime; + + constexpr LightDirectory(const char *_uri, time_t _mtime) + :uri(_uri), mtime(_mtime) {} + + static constexpr LightDirectory Root() { + return LightDirectory("", 0); + } + + bool IsRoot() const { + return *uri == 0; + } + + gcc_pure + const char *GetPath() const { + return uri; + } +}; + +#endif diff --git a/src/db/LightSong.cxx b/src/db/LightSong.cxx new file mode 100644 index 000000000..af1e801f8 --- /dev/null +++ b/src/db/LightSong.cxx @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "LightSong.hxx" +#include "tag/Tag.hxx" + +double +LightSong::GetDuration() const +{ + if (end_ms > 0) + return (end_ms - start_ms) / 1000.0; + + if (tag->time <= 0) + return 0; + + return tag->time - start_ms / 1000.0; +} diff --git a/src/db/LightSong.hxx b/src/db/LightSong.hxx new file mode 100644 index 000000000..c0cd47749 --- /dev/null +++ b/src/db/LightSong.hxx @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_LIGHT_SONG_HXX +#define MPD_LIGHT_SONG_HXX + +#include "Compiler.h" + +#include + +#include + +struct Tag; + +/** + * A reference to a song file. Unlike the other "Song" classes in the + * MPD code base, this one consists only of pointers. It is supposed + * to be as light as possible while still providing all the + * information MPD has about a song file. This class does not manage + * any memory, and the pointers become invalid quickly. Only to be + * used to pass around during well-defined situations. + */ +struct LightSong { + /** + * If this is not nullptr, then it denotes a prefix for the + * #uri. To build the full URI, join directory and uri with a + * slash. + */ + const char *directory; + + const char *uri; + + /** + * The "real" URI, the one to be used for opening the + * resource. If this attribute is empty, then #uri (and + * #directory) shall be used. + * + * This attribute is used for songs from the database which + * have a relative URI. + */ + std::string real_uri; + + /** + * Must not be nullptr. + */ + const Tag *tag; + + time_t mtime; + + /** + * Start of this sub-song within the file in milliseconds. + */ + unsigned start_ms; + + /** + * End of this sub-song within the file in milliseconds. + * Unused if zero. + */ + unsigned end_ms; + + gcc_pure + std::string GetURI() const { + if (directory == nullptr) + return std::string(uri); + + std::string result(directory); + result.push_back('/'); + result.append(uri); + return result; + } + + gcc_pure + double GetDuration() const; +}; + +#endif diff --git a/src/db/ProxyDatabasePlugin.cxx b/src/db/ProxyDatabasePlugin.cxx deleted file mode 100644 index 7253ba0d0..000000000 --- a/src/db/ProxyDatabasePlugin.cxx +++ /dev/null @@ -1,782 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "ProxyDatabasePlugin.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseListener.hxx" -#include "DatabaseSelection.hxx" -#include "DatabaseError.hxx" -#include "PlaylistInfo.hxx" -#include "LightDirectory.hxx" -#include "LightSong.hxx" -#include "SongFilter.hxx" -#include "Compiler.h" -#include "config/ConfigData.hxx" -#include "tag/TagBuilder.hxx" -#include "tag/Tag.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" -#include "protocol/Ack.hxx" -#include "Main.hxx" -#include "event/SocketMonitor.hxx" -#include "event/IdleMonitor.hxx" -#include "Log.hxx" - -#include -#include - -#include -#include -#include - -class ProxySong : public LightSong { - Tag tag2; - -public: - explicit ProxySong(const mpd_song *song); -}; - -class AllocatedProxySong : public ProxySong { - mpd_song *const song; - -public: - explicit AllocatedProxySong(mpd_song *_song) - :ProxySong(_song), song(_song) {} - - ~AllocatedProxySong() { - mpd_song_free(song); - } -}; - -class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor { - DatabaseListener &listener; - - std::string host; - unsigned port; - - struct mpd_connection *connection; - - /* this is mutable because GetStats() must be "const" */ - mutable time_t update_stamp; - - /** - * The libmpdclient idle mask that was removed from the other - * MPD. This will be handled by the next OnIdle() call. - */ - unsigned idle_received; - - /** - * Is the #connection currently "idle"? That is, did we send - * the "idle" command to it? - */ - bool is_idle; - -public: - ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener) - :SocketMonitor(_loop), IdleMonitor(_loop), - listener(_listener) {} - - static Database *Create(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, - Error &error); - - virtual bool Open(Error &error) override; - virtual void Close() override; - virtual const LightSong *GetSong(const char *uri_utf8, - Error &error) const override; - virtual void ReturnSong(const LightSong *song) const; - - virtual bool Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const override; - - virtual bool VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const override; - - virtual bool GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, - Error &error) const override; - - virtual time_t GetUpdateStamp() const override { - return update_stamp; - } - -private: - bool Configure(const config_param ¶m, Error &error); - - bool Connect(Error &error); - bool CheckConnection(Error &error); - bool EnsureConnected(Error &error); - - void Disconnect(); - - /* virtual methods from SocketMonitor */ - virtual bool OnSocketReady(unsigned flags) override; - - /* virtual methods from IdleMonitor */ - virtual void OnIdle() override; -}; - -static constexpr Domain libmpdclient_domain("libmpdclient"); - -static constexpr struct { - TagType d; - enum mpd_tag_type s; -} tag_table[] = { - { TAG_ARTIST, MPD_TAG_ARTIST }, - { TAG_ALBUM, MPD_TAG_ALBUM }, - { TAG_ALBUM_ARTIST, MPD_TAG_ALBUM_ARTIST }, - { TAG_TITLE, MPD_TAG_TITLE }, - { TAG_TRACK, MPD_TAG_TRACK }, - { TAG_NAME, MPD_TAG_NAME }, - { TAG_GENRE, MPD_TAG_GENRE }, - { TAG_DATE, MPD_TAG_DATE }, - { TAG_COMPOSER, MPD_TAG_COMPOSER }, - { TAG_PERFORMER, MPD_TAG_PERFORMER }, - { TAG_COMMENT, MPD_TAG_COMMENT }, - { TAG_DISC, MPD_TAG_DISC }, - { TAG_MUSICBRAINZ_ARTISTID, MPD_TAG_MUSICBRAINZ_ARTISTID }, - { TAG_MUSICBRAINZ_ALBUMID, MPD_TAG_MUSICBRAINZ_ALBUMID }, - { TAG_MUSICBRAINZ_ALBUMARTISTID, - MPD_TAG_MUSICBRAINZ_ALBUMARTISTID }, - { TAG_MUSICBRAINZ_TRACKID, MPD_TAG_MUSICBRAINZ_TRACKID }, - { TAG_NUM_OF_ITEM_TYPES, MPD_TAG_COUNT } -}; - -static void -Copy(TagBuilder &tag, TagType d_tag, - const struct mpd_song *song, enum mpd_tag_type s_tag) -{ - - for (unsigned i = 0;; ++i) { - const char *value = mpd_song_get_tag(song, s_tag, i); - if (value == nullptr) - break; - - tag.AddItem(d_tag, value); - } -} - -ProxySong::ProxySong(const mpd_song *song) -{ - directory = nullptr; - uri = mpd_song_get_uri(song); - tag = &tag2; - mtime = mpd_song_get_last_modified(song); - start_ms = mpd_song_get_start(song) * 1000; - end_ms = mpd_song_get_end(song) * 1000; - - TagBuilder tag_builder; - tag_builder.SetTime(mpd_song_get_duration(song)); - - for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i) - Copy(tag_builder, i->d, song, i->s); - - tag_builder.Commit(tag2); -} - -gcc_const -static enum mpd_tag_type -Convert(TagType tag_type) -{ - for (auto i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i) - if (i->d == tag_type) - return i->s; - - return MPD_TAG_COUNT; -} - -static bool -CheckError(struct mpd_connection *connection, Error &error) -{ - const auto code = mpd_connection_get_error(connection); - if (code == MPD_ERROR_SUCCESS) - return true; - - if (code == MPD_ERROR_SERVER) { - /* libmpdclient's "enum mpd_server_error" is the same - as our "enum ack" */ - const auto server_error = - mpd_connection_get_server_error(connection); - error.Set(ack_domain, (int)server_error, - mpd_connection_get_error_message(connection)); - } else { - error.Set(libmpdclient_domain, (int)code, - mpd_connection_get_error_message(connection)); - } - - mpd_connection_clear_error(connection); - return false; -} - -static bool -SendConstraints(mpd_connection *connection, const SongFilter::Item &item) -{ - switch (item.GetTag()) { - mpd_tag_type tag; - -#if LIBMPDCLIENT_CHECK_VERSION(2,9,0) - case LOCATE_TAG_BASE_TYPE: - if (mpd_connection_cmp_server_version(connection, 0, 18, 0) < 0) - /* requires MPD 0.18 */ - return true; - - return mpd_search_add_base_constraint(connection, - MPD_OPERATOR_DEFAULT, - item.GetValue().c_str()); -#endif - - case LOCATE_TAG_FILE_TYPE: - return mpd_search_add_uri_constraint(connection, - MPD_OPERATOR_DEFAULT, - item.GetValue().c_str()); - - case LOCATE_TAG_ANY_TYPE: - return mpd_search_add_any_tag_constraint(connection, - MPD_OPERATOR_DEFAULT, - item.GetValue().c_str()); - - default: - tag = Convert(TagType(item.GetTag())); - if (tag == MPD_TAG_COUNT) - return true; - - return mpd_search_add_tag_constraint(connection, - MPD_OPERATOR_DEFAULT, - tag, - item.GetValue().c_str()); - } -} - -static bool -SendConstraints(mpd_connection *connection, const SongFilter &filter) -{ - for (const auto &i : filter.GetItems()) - if (!SendConstraints(connection, i)) - return false; - - return true; -} - -static bool -SendConstraints(mpd_connection *connection, const DatabaseSelection &selection) -{ -#if LIBMPDCLIENT_CHECK_VERSION(2,9,0) - if (!selection.uri.empty() && - mpd_connection_cmp_server_version(connection, 0, 18, 0) >= 0) { - /* requires MPD 0.18 */ - if (!mpd_search_add_base_constraint(connection, - MPD_OPERATOR_DEFAULT, - selection.uri.c_str())) - return false; - } -#endif - - if (selection.filter != nullptr && - !SendConstraints(connection, *selection.filter)) - return false; - - return true; -} - -Database * -ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, Error &error) -{ - ProxyDatabase *db = new ProxyDatabase(loop, listener); - if (!db->Configure(param, error)) { - delete db; - db = nullptr; - } - - return db; -} - -bool -ProxyDatabase::Configure(const config_param ¶m, gcc_unused Error &error) -{ - host = param.GetBlockValue("host", ""); - port = param.GetBlockValue("port", 0u); - - return true; -} - -bool -ProxyDatabase::Open(Error &error) -{ - if (!Connect(error)) - return false; - - update_stamp = 0; - - return true; -} - -void -ProxyDatabase::Close() -{ - if (connection != nullptr) - Disconnect(); -} - -bool -ProxyDatabase::Connect(Error &error) -{ - const char *_host = host.empty() ? nullptr : host.c_str(); - connection = mpd_connection_new(_host, port, 0); - if (connection == nullptr) { - error.Set(libmpdclient_domain, (int)MPD_ERROR_OOM, - "Out of memory"); - return false; - } - - idle_received = unsigned(-1); - is_idle = false; - - SocketMonitor::Open(mpd_async_get_fd(mpd_connection_get_async(connection))); - IdleMonitor::Schedule(); - - if (!CheckError(connection, error)) { - if (connection != nullptr) - Disconnect(); - - return false; - } - - return true; -} - -bool -ProxyDatabase::CheckConnection(Error &error) -{ - assert(connection != nullptr); - - if (!mpd_connection_clear_error(connection)) { - Disconnect(); - return Connect(error); - } - - if (is_idle) { - unsigned idle = mpd_run_noidle(connection); - if (idle == 0 && !CheckError(connection, error)) { - Disconnect(); - return false; - } - - idle_received |= idle; - is_idle = false; - IdleMonitor::Schedule(); - } - - return true; -} - -bool -ProxyDatabase::EnsureConnected(Error &error) -{ - return connection != nullptr - ? CheckConnection(error) - : Connect(error); -} - -void -ProxyDatabase::Disconnect() -{ - assert(connection != nullptr); - - IdleMonitor::Cancel(); - SocketMonitor::Steal(); - - mpd_connection_free(connection); - connection = nullptr; -} - -bool -ProxyDatabase::OnSocketReady(gcc_unused unsigned flags) -{ - assert(connection != nullptr); - - if (!is_idle) { - // TODO: can this happen? - IdleMonitor::Schedule(); - return false; - } - - unsigned idle = (unsigned)mpd_recv_idle(connection, false); - if (idle == 0) { - Error error; - if (!CheckError(connection, error)) { - LogError(error); - Disconnect(); - return false; - } - } - - /* let OnIdle() handle this */ - idle_received |= idle; - is_idle = false; - IdleMonitor::Schedule(); - return false; -} - -void -ProxyDatabase::OnIdle() -{ - assert(connection != nullptr); - - /* handle previous idle events */ - - if (idle_received & MPD_IDLE_DATABASE) - listener.OnDatabaseModified(); - - idle_received = 0; - - /* send a new idle command to the other MPD */ - - if (is_idle) - // TODO: can this happen? - return; - - if (!mpd_send_idle_mask(connection, MPD_IDLE_DATABASE)) { - Error error; - if (!CheckError(connection, error)) - LogError(error); - - SocketMonitor::Steal(); - mpd_connection_free(connection); - connection = nullptr; - return; - } - - is_idle = true; - SocketMonitor::ScheduleRead(); -} - -const LightSong * -ProxyDatabase::GetSong(const char *uri, Error &error) const -{ - // TODO: eliminate the const_cast - if (!const_cast(this)->EnsureConnected(error)) - return nullptr; - - if (!mpd_send_list_meta(connection, uri)) { - CheckError(connection, error); - return nullptr; - } - - struct mpd_song *song = mpd_recv_song(connection); - if (!mpd_response_finish(connection) && - !CheckError(connection, error)) { - if (song != nullptr) - mpd_song_free(song); - return nullptr; - } - - if (song == nullptr) { - error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri); - return nullptr; - } - - return new AllocatedProxySong(song); -} - -void -ProxyDatabase::ReturnSong(const LightSong *_song) const -{ - assert(_song != nullptr); - - AllocatedProxySong *song = (AllocatedProxySong *) - const_cast(_song); - delete song; -} - -static bool -Visit(struct mpd_connection *connection, const char *uri, - bool recursive, const SongFilter *filter, - VisitDirectory visit_directory, VisitSong visit_song, - VisitPlaylist visit_playlist, Error &error); - -static bool -Visit(struct mpd_connection *connection, - bool recursive, const SongFilter *filter, - const struct mpd_directory *directory, - VisitDirectory visit_directory, VisitSong visit_song, - VisitPlaylist visit_playlist, Error &error) -{ - const char *path = mpd_directory_get_path(directory); -#if LIBMPDCLIENT_CHECK_VERSION(2,9,0) - time_t mtime = mpd_directory_get_last_modified(directory); -#else - time_t mtime = 0; -#endif - - if (visit_directory && - !visit_directory(LightDirectory(path, mtime), error)) - return false; - - if (recursive && - !Visit(connection, path, recursive, filter, - visit_directory, visit_song, visit_playlist, error)) - return false; - - return true; -} - -gcc_pure -static bool -Match(const SongFilter *filter, const LightSong &song) -{ - return filter == nullptr || filter->Match(song); -} - -static bool -Visit(const SongFilter *filter, - const mpd_song *_song, - VisitSong visit_song, Error &error) -{ - if (!visit_song) - return true; - - const ProxySong song(_song); - return !Match(filter, song) || visit_song(song, error); -} - -static bool -Visit(const struct mpd_playlist *playlist, - VisitPlaylist visit_playlist, Error &error) -{ - if (!visit_playlist) - return true; - - PlaylistInfo p(mpd_playlist_get_path(playlist), - mpd_playlist_get_last_modified(playlist)); - - return visit_playlist(p, LightDirectory::Root(), error); -} - -class ProxyEntity { - struct mpd_entity *entity; - -public: - explicit ProxyEntity(struct mpd_entity *_entity) - :entity(_entity) {} - - ProxyEntity(const ProxyEntity &other) = delete; - - ProxyEntity(ProxyEntity &&other) - :entity(other.entity) { - other.entity = nullptr; - } - - ~ProxyEntity() { - if (entity != nullptr) - mpd_entity_free(entity); - } - - ProxyEntity &operator=(const ProxyEntity &other) = delete; - - operator const struct mpd_entity *() const { - return entity; - } -}; - -static std::list -ReceiveEntities(struct mpd_connection *connection) -{ - std::list entities; - struct mpd_entity *entity; - while ((entity = mpd_recv_entity(connection)) != nullptr) - entities.push_back(ProxyEntity(entity)); - - mpd_response_finish(connection); - return entities; -} - -static bool -Visit(struct mpd_connection *connection, const char *uri, - bool recursive, const SongFilter *filter, - VisitDirectory visit_directory, VisitSong visit_song, - VisitPlaylist visit_playlist, Error &error) -{ - if (!mpd_send_list_meta(connection, uri)) - return CheckError(connection, error); - - std::list entities(ReceiveEntities(connection)); - if (!CheckError(connection, error)) - return false; - - for (const auto &entity : entities) { - switch (mpd_entity_get_type(entity)) { - case MPD_ENTITY_TYPE_UNKNOWN: - break; - - case MPD_ENTITY_TYPE_DIRECTORY: - if (!Visit(connection, recursive, filter, - mpd_entity_get_directory(entity), - visit_directory, visit_song, visit_playlist, - error)) - return false; - break; - - case MPD_ENTITY_TYPE_SONG: - if (!Visit(filter, - mpd_entity_get_song(entity), visit_song, - error)) - return false; - break; - - case MPD_ENTITY_TYPE_PLAYLIST: - if (!Visit(mpd_entity_get_playlist(entity), - visit_playlist, error)) - return false; - break; - } - } - - return CheckError(connection, error); -} - -static bool -SearchSongs(struct mpd_connection *connection, - const DatabaseSelection &selection, - VisitSong visit_song, - Error &error) -{ - assert(selection.recursive); - assert(visit_song); - - const bool exact = selection.filter == nullptr || - !selection.filter->HasFoldCase(); - - if (!mpd_search_db_songs(connection, exact) || - !SendConstraints(connection, selection) || - !mpd_search_commit(connection)) - return CheckError(connection, error); - - bool result = true; - struct mpd_song *song; - while (result && (song = mpd_recv_song(connection)) != nullptr) { - AllocatedProxySong song2(song); - - result = !Match(selection.filter, song2) || - visit_song(song2, error); - } - - mpd_response_finish(connection); - return result && CheckError(connection, error); -} - -bool -ProxyDatabase::Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const -{ - // TODO: eliminate the const_cast - if (!const_cast(this)->EnsureConnected(error)) - return nullptr; - - if (!visit_directory && !visit_playlist && selection.recursive) - /* this optimized code path can only be used under - certain conditions */ - return ::SearchSongs(connection, selection, visit_song, error); - - /* fall back to recursive walk (slow!) */ - return ::Visit(connection, selection.uri.c_str(), - selection.recursive, selection.filter, - visit_directory, visit_song, visit_playlist, - error); -} - -bool -ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const -{ - // TODO: eliminate the const_cast - if (!const_cast(this)->EnsureConnected(error)) - return nullptr; - - enum mpd_tag_type tag_type2 = Convert(tag_type); - if (tag_type2 == MPD_TAG_COUNT) { - error.Set(libmpdclient_domain, "Unsupported tag"); - return false; - } - - if (!mpd_search_db_tags(connection, tag_type2)) - return CheckError(connection, error); - - if (!SendConstraints(connection, selection)) - return CheckError(connection, error); - - if (!mpd_search_commit(connection)) - return CheckError(connection, error); - - bool result = true; - - struct mpd_pair *pair; - while (result && - (pair = mpd_recv_pair_tag(connection, tag_type2)) != nullptr) { - result = visit_string(pair->value, error); - mpd_return_pair(connection, pair); - } - - return mpd_response_finish(connection) && - CheckError(connection, error) && - result; -} - -bool -ProxyDatabase::GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, Error &error) const -{ - // TODO: match - (void)selection; - - // TODO: eliminate the const_cast - if (!const_cast(this)->EnsureConnected(error)) - return nullptr; - - struct mpd_stats *stats2 = - mpd_run_stats(connection); - if (stats2 == nullptr) - return CheckError(connection, error); - - update_stamp = (time_t)mpd_stats_get_db_update_time(stats2); - - stats.song_count = mpd_stats_get_number_of_songs(stats2); - stats.total_duration = mpd_stats_get_db_play_time(stats2); - stats.artist_count = mpd_stats_get_number_of_artists(stats2); - stats.album_count = mpd_stats_get_number_of_albums(stats2); - mpd_stats_free(stats2); - - return true; -} - -const DatabasePlugin proxy_db_plugin = { - "proxy", - ProxyDatabase::Create, -}; diff --git a/src/db/ProxyDatabasePlugin.hxx b/src/db/ProxyDatabasePlugin.hxx deleted file mode 100644 index 699d374b5..000000000 --- a/src/db/ProxyDatabasePlugin.hxx +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_PROXY_DATABASE_PLUGIN_HXX -#define MPD_PROXY_DATABASE_PLUGIN_HXX - -struct DatabasePlugin; - -extern const DatabasePlugin proxy_db_plugin; - -#endif diff --git a/src/db/Registry.cxx b/src/db/Registry.cxx new file mode 100644 index 000000000..295d3cf2a --- /dev/null +++ b/src/db/Registry.cxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Registry.hxx" +#include "plugins/SimpleDatabasePlugin.hxx" +#include "plugins/ProxyDatabasePlugin.hxx" +#include "plugins/UpnpDatabasePlugin.hxx" + +#include + +const DatabasePlugin *const database_plugins[] = { + &simple_db_plugin, +#ifdef HAVE_LIBMPDCLIENT + &proxy_db_plugin, +#endif +#ifdef HAVE_LIBUPNP + &upnp_db_plugin, +#endif + nullptr +}; + +const DatabasePlugin * +GetDatabasePluginByName(const char *name) +{ + for (auto i = database_plugins; *i != nullptr; ++i) + if (strcmp((*i)->name, name) == 0) + return *i; + + return nullptr; +} diff --git a/src/db/Registry.hxx b/src/db/Registry.hxx new file mode 100644 index 000000000..050842e21 --- /dev/null +++ b/src/db/Registry.hxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_REGISTRY_HXX +#define MPD_DATABASE_REGISTRY_HXX + +#include "Compiler.h" + +struct DatabasePlugin; + +/** + * nullptr terminated list of all database plugins which were enabled at + * compile time. + */ +extern const DatabasePlugin *const database_plugins[]; + +gcc_pure +const DatabasePlugin * +GetDatabasePluginByName(const char *name); + +#endif diff --git a/src/db/Selection.cxx b/src/db/Selection.cxx new file mode 100644 index 000000000..96382eed7 --- /dev/null +++ b/src/db/Selection.cxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Selection.hxx" +#include "SongFilter.hxx" + +DatabaseSelection::DatabaseSelection(const char *_uri, bool _recursive, + const SongFilter *_filter) + :uri(_uri), recursive(_recursive), filter(_filter) +{ + /* optimization: if the caller didn't specify a base URI, pick + the one from SongFilter */ + if (uri.empty() && filter != nullptr) + uri = filter->GetBase(); +} + +bool +DatabaseSelection::Match(const LightSong &song) const +{ + return filter == nullptr || filter->Match(song); +} diff --git a/src/db/Selection.hxx b/src/db/Selection.hxx new file mode 100644 index 000000000..a39ce7afe --- /dev/null +++ b/src/db/Selection.hxx @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_SELECTION_HXX +#define MPD_DATABASE_SELECTION_HXX + +#include "Compiler.h" + +#include + +class SongFilter; +struct LightSong; + +struct DatabaseSelection { + /** + * The base URI of the search (UTF-8). Must not begin or end + * with a slash. An empty string searches the whole database. + */ + std::string uri; + + /** + * Recursively search all sub directories? + */ + bool recursive; + + const SongFilter *filter; + + DatabaseSelection(const char *_uri, bool _recursive, + const SongFilter *_filter=nullptr); + + gcc_pure + bool Match(const LightSong &song) const; +}; + +#endif diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx deleted file mode 100644 index 73e080b41..000000000 --- a/src/db/SimpleDatabasePlugin.cxx +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "SimpleDatabasePlugin.hxx" -#include "DatabaseSelection.hxx" -#include "DatabaseHelpers.hxx" -#include "LightDirectory.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "SongFilter.hxx" -#include "DatabaseSave.hxx" -#include "DatabaseLock.hxx" -#include "DatabaseError.hxx" -#include "fs/TextFile.hxx" -#include "config/ConfigData.hxx" -#include "fs/FileSystem.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" -#include "Log.hxx" - -#include - -static constexpr Domain simple_db_domain("simple_db"); - -Database * -SimpleDatabase::Create(gcc_unused EventLoop &loop, - gcc_unused DatabaseListener &listener, - const config_param ¶m, Error &error) -{ - SimpleDatabase *db = new SimpleDatabase(); - if (!db->Configure(param, error)) { - delete db; - db = nullptr; - } - - return db; -} - -bool -SimpleDatabase::Configure(const config_param ¶m, Error &error) -{ - path = param.GetBlockPath("path", error); - if (path.IsNull()) { - if (!error.IsDefined()) - error.Set(simple_db_domain, - "No \"path\" parameter specified"); - return false; - } - - path_utf8 = path.ToUTF8(); - - return true; -} - -bool -SimpleDatabase::Check(Error &error) const -{ - assert(!path.IsNull()); - - /* Check if the file exists */ - if (!CheckAccess(path)) { - /* If the file doesn't exist, we can't check if we can write - * it, so we are going to try to get the directory path, and - * see if we can write a file in that */ - const auto dirPath = path.GetDirectoryName(); - - /* Check that the parent part of the path is a directory */ - struct stat st; - if (!StatFile(dirPath, st)) { - error.FormatErrno("Couldn't stat parent directory of db file " - "\"%s\"", - path_utf8.c_str()); - return false; - } - - if (!S_ISDIR(st.st_mode)) { - error.Format(simple_db_domain, - "Couldn't create db file \"%s\" because the " - "parent path is not a directory", - path_utf8.c_str()); - return false; - } - -#ifndef WIN32 - /* Check if we can write to the directory */ - if (!CheckAccess(dirPath, X_OK | W_OK)) { - const int e = errno; - const std::string dirPath_utf8 = dirPath.ToUTF8(); - error.FormatErrno(e, "Can't create db file in \"%s\"", - dirPath_utf8.c_str()); - return false; - } -#endif - return true; - } - - /* Path exists, now check if it's a regular file */ - struct stat st; - if (!StatFile(path, st)) { - error.FormatErrno("Couldn't stat db file \"%s\"", - path_utf8.c_str()); - return false; - } - - if (!S_ISREG(st.st_mode)) { - error.Format(simple_db_domain, - "db file \"%s\" is not a regular file", - path_utf8.c_str()); - return false; - } - -#ifndef WIN32 - /* And check that we can write to it */ - if (!CheckAccess(path, R_OK | W_OK)) { - error.FormatErrno("Can't open db file \"%s\" for reading/writing", - path_utf8.c_str()); - return false; - } -#endif - - return true; -} - -bool -SimpleDatabase::Load(Error &error) -{ - assert(!path.IsNull()); - assert(root != nullptr); - - TextFile file(path); - if (file.HasFailed()) { - error.FormatErrno("Failed to open database file \"%s\"", - path_utf8.c_str()); - return false; - } - - if (!db_load_internal(file, *root, error)) - return false; - - struct stat st; - if (StatFile(path, st)) - mtime = st.st_mtime; - - return true; -} - -bool -SimpleDatabase::Open(Error &error) -{ - root = Directory::NewRoot(); - mtime = 0; - -#ifndef NDEBUG - borrowed_song_count = 0; -#endif - - if (!Load(error)) { - delete root; - - LogError(error); - error.Clear(); - - if (!Check(error)) - return false; - - root = Directory::NewRoot(); - } - - return true; -} - -void -SimpleDatabase::Close() -{ - assert(root != nullptr); - assert(borrowed_song_count == 0); - - delete root; -} - -const LightSong * -SimpleDatabase::GetSong(const char *uri, Error &error) const -{ - assert(root != nullptr); - assert(borrowed_song_count == 0); - - db_lock(); - const Song *song = root->LookupSong(uri); - db_unlock(); - if (song == nullptr) { - error.Format(db_domain, DB_NOT_FOUND, - "No such song: %s", uri); - return nullptr; - } - - light_song = song->Export(); - -#ifndef NDEBUG - ++borrowed_song_count; -#endif - - return &light_song; -} - -void -SimpleDatabase::ReturnSong(gcc_unused const LightSong *song) const -{ - assert(song == &light_song); - -#ifndef NDEBUG - assert(borrowed_song_count > 0); - --borrowed_song_count; -#endif -} - -gcc_pure -const Directory * -SimpleDatabase::LookupDirectory(const char *uri) const -{ - assert(root != nullptr); - assert(uri != nullptr); - - ScopeDatabaseLock protect; - return root->LookupDirectory(uri); -} - -bool -SimpleDatabase::Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const -{ - ScopeDatabaseLock protect; - - const Directory *directory = root->LookupDirectory(selection.uri.c_str()); - if (directory == nullptr) { - if (visit_song) { - Song *song = root->LookupSong(selection.uri.c_str()); - if (song != nullptr) { - const LightSong song2 = song->Export(); - return !selection.Match(song2) || - visit_song(song2, error); - } - } - - error.Set(db_domain, DB_NOT_FOUND, "No such directory"); - return false; - } - - if (selection.recursive && visit_directory && - !visit_directory(directory->Export(), error)) - return false; - - return directory->Walk(selection.recursive, selection.filter, - visit_directory, visit_song, visit_playlist, - error); -} - -bool -SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const -{ - return ::VisitUniqueTags(*this, selection, tag_type, visit_string, - error); -} - -bool -SimpleDatabase::GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, Error &error) const -{ - return ::GetStats(*this, selection, stats, error); -} - -bool -SimpleDatabase::Save(Error &error) -{ - db_lock(); - - LogDebug(simple_db_domain, "removing empty directories from DB"); - root->PruneEmpty(); - - LogDebug(simple_db_domain, "sorting DB"); - root->Sort(); - - db_unlock(); - - LogDebug(simple_db_domain, "writing DB"); - - FILE *fp = FOpen(path, FOpenMode::WriteText); - if (!fp) { - error.FormatErrno("unable to write to db file \"%s\"", - path_utf8.c_str()); - return false; - } - - db_save_internal(fp, *root); - - if (ferror(fp)) { - error.SetErrno("Failed to write to database file"); - fclose(fp); - return false; - } - - fclose(fp); - - struct stat st; - if (StatFile(path, st)) - mtime = st.st_mtime; - - return true; -} - -const DatabasePlugin simple_db_plugin = { - "simple", - SimpleDatabase::Create, -}; diff --git a/src/db/SimpleDatabasePlugin.hxx b/src/db/SimpleDatabasePlugin.hxx deleted file mode 100644 index 509b91e4e..000000000 --- a/src/db/SimpleDatabasePlugin.hxx +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_SIMPLE_DATABASE_PLUGIN_HXX -#define MPD_SIMPLE_DATABASE_PLUGIN_HXX - -#include "DatabasePlugin.hxx" -#include "fs/AllocatedPath.hxx" -#include "LightSong.hxx" -#include "Compiler.h" - -#include - -struct Directory; - -class SimpleDatabase : public Database { - AllocatedPath path; - std::string path_utf8; - - Directory *root; - - time_t mtime; - - /** - * A buffer for GetSong(). - */ - mutable LightSong light_song; - -#ifndef NDEBUG - mutable unsigned borrowed_song_count; -#endif - - SimpleDatabase() - :path(AllocatedPath::Null()) {} - -public: - gcc_pure - Directory *GetRoot() { - assert(root != NULL); - - return root; - } - - bool Save(Error &error); - - static Database *Create(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, - Error &error); - - virtual bool Open(Error &error) override; - virtual void Close() override; - - virtual const LightSong *GetSong(const char *uri_utf8, - Error &error) const override; - virtual void ReturnSong(const LightSong *song) const; - - virtual bool Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const override; - - virtual bool VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const override; - - virtual bool GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, - Error &error) const override; - - virtual time_t GetUpdateStamp() const override { - return mtime; - } - -protected: - bool Configure(const config_param ¶m, Error &error); - - gcc_pure - bool Check(Error &error) const; - - bool Load(Error &error); - - gcc_pure - const Directory *LookupDirectory(const char *uri) const; -}; - -extern const DatabasePlugin simple_db_plugin; - -#endif diff --git a/src/db/Song.cxx b/src/db/Song.cxx new file mode 100644 index 000000000..15924a40a --- /dev/null +++ b/src/db/Song.cxx @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Song.hxx" +#include "Directory.hxx" +#include "tag/Tag.hxx" +#include "util/VarSize.hxx" +#include "DetachedSong.hxx" +#include "LightSong.hxx" + +#include +#include +#include + +inline Song::Song(const char *_uri, size_t uri_length, Directory &_parent) + :parent(&_parent), mtime(0), start_ms(0), end_ms(0) +{ + memcpy(uri, _uri, uri_length + 1); +} + +inline Song::~Song() +{ +} + +static Song * +song_alloc(const char *uri, Directory &parent) +{ + size_t uri_length; + + assert(uri); + uri_length = strlen(uri); + assert(uri_length); + + return NewVarSize(sizeof(Song::uri), + uri_length + 1, + uri, uri_length, parent); +} + +Song * +Song::NewFrom(DetachedSong &&other, Directory &parent) +{ + Song *song = song_alloc(other.GetURI(), parent); + song->tag = std::move(other.WritableTag()); + song->mtime = other.GetLastModified(); + song->start_ms = other.GetStartMS(); + song->end_ms = other.GetEndMS(); + return song; +} + +Song * +Song::NewFile(const char *path, Directory &parent) +{ + return song_alloc(path, parent); +} + +void +Song::Free() +{ + DeleteVarSize(this); +} + +std::string +Song::GetURI() const +{ + assert(*uri); + + if (parent->IsRoot()) + return std::string(uri); + else { + const char *path = parent->GetPath(); + + std::string result; + result.reserve(strlen(path) + 1 + strlen(uri)); + result.assign(path); + result.push_back('/'); + result.append(uri); + return result; + } +} + +LightSong +Song::Export() const +{ + LightSong dest; + dest.directory = parent->IsRoot() + ? nullptr : parent->GetPath(); + dest.uri = uri; + dest.tag = &tag; + dest.mtime = mtime; + dest.start_ms = start_ms; + dest.end_ms = end_ms; + return dest; +} diff --git a/src/db/Song.hxx b/src/db/Song.hxx new file mode 100644 index 000000000..0b94fe6d0 --- /dev/null +++ b/src/db/Song.hxx @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_SONG_HXX +#define MPD_SONG_HXX + +#include "util/list.h" +#include "tag/Tag.hxx" +#include "Compiler.h" + +#include + +#include +#include + +struct LightSong; +struct Directory; +class DetachedSong; + +/** + * A song file inside the configured music directory. + */ +struct Song { + /** + * Pointers to the siblings of this directory within the + * parent directory. It is unused (undefined) if this song is + * not in the database. + * + * This attribute is protected with the global #db_mutex. + * Read access in the update thread does not need protection. + */ + struct list_head siblings; + + Tag tag; + + /** + * The #Directory that contains this song. May be nullptr if + * the current database plugin does not manage the parent + * directory this way. + */ + Directory *const parent; + + time_t mtime; + + /** + * Start of this sub-song within the file in milliseconds. + */ + unsigned start_ms; + + /** + * End of this sub-song within the file in milliseconds. + * Unused if zero. + */ + unsigned end_ms; + + /** + * The file name. If #parent is nullptr, then this is the URI + * relative to the music directory. + */ + char uri[sizeof(int)]; + + Song(const char *_uri, size_t uri_length, Directory &parent); + ~Song(); + + gcc_malloc + static Song *NewFrom(DetachedSong &&other, Directory &parent); + + /** allocate a new song with a local file name */ + gcc_malloc + static Song *NewFile(const char *path_utf8, Directory &parent); + + /** + * allocate a new song structure with a local file name and attempt to + * load its metadata. If all decoder plugin fail to read its meta + * data, nullptr is returned. + */ + gcc_malloc + static Song *LoadFile(const char *path_utf8, Directory &parent); + + void Free(); + + bool UpdateFile(); + bool UpdateFileInArchive(); + + /** + * Returns the URI of the song in UTF-8 encoding, including its + * location within the music directory. + */ + gcc_pure + std::string GetURI() const; + + gcc_pure + LightSong Export() const; +}; + +#endif diff --git a/src/db/SongSort.cxx b/src/db/SongSort.cxx new file mode 100644 index 000000000..dcea033b6 --- /dev/null +++ b/src/db/SongSort.cxx @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "SongSort.hxx" +#include "Song.hxx" +#include "tag/Tag.hxx" + +extern "C" { +#include "util/list_sort.h" +} + +#include + +#include + +static int +compare_utf8_string(const char *a, const char *b) +{ + if (a == nullptr) + return b == nullptr ? 0 : -1; + + if (b == nullptr) + return 1; + + return g_utf8_collate(a, b); +} + +/** + * Compare two string tag values, ignoring case. Either one may be + * nullptr. + */ +static int +compare_string_tag_item(const Tag &a, const Tag &b, + TagType type) +{ + return compare_utf8_string(a.GetValue(type), + b.GetValue(type)); +} + +/** + * Compare two tag values which should contain an integer value + * (e.g. disc or track number). Either one may be nullptr. + */ +static int +compare_number_string(const char *a, const char *b) +{ + long ai = a == nullptr ? 0 : strtol(a, nullptr, 10); + long bi = b == nullptr ? 0 : strtol(b, nullptr, 10); + + if (ai <= 0) + return bi <= 0 ? 0 : -1; + + if (bi <= 0) + return 1; + + return ai - bi; +} + +static int +compare_tag_item(const Tag &a, const Tag &b, TagType type) +{ + return compare_number_string(a.GetValue(type), + b.GetValue(type)); +} + +/* Only used for sorting/searchin a songvec, not general purpose compares */ +static int +song_cmp(gcc_unused void *priv, struct list_head *_a, struct list_head *_b) +{ + const Song *a = (const Song *)_a; + const Song *b = (const Song *)_b; + int ret; + + /* first sort by album */ + ret = compare_string_tag_item(a->tag, b->tag, TAG_ALBUM); + if (ret != 0) + return ret; + + /* then sort by disc */ + ret = compare_tag_item(a->tag, b->tag, TAG_DISC); + if (ret != 0) + return ret; + + /* then by track number */ + ret = compare_tag_item(a->tag, b->tag, TAG_TRACK); + if (ret != 0) + return ret; + + /* still no difference? compare file name */ + return g_utf8_collate(a->uri, b->uri); +} + +void +song_list_sort(struct list_head *songs) +{ + list_sort(nullptr, songs, song_cmp); +} diff --git a/src/db/SongSort.hxx b/src/db/SongSort.hxx new file mode 100644 index 000000000..28b903532 --- /dev/null +++ b/src/db/SongSort.hxx @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_SONG_SORT_HXX +#define MPD_SONG_SORT_HXX + +struct list_head; + +void +song_list_sort(list_head *songs); + +#endif diff --git a/src/db/UpnpDatabasePlugin.cxx b/src/db/UpnpDatabasePlugin.cxx deleted file mode 100644 index 46084061f..000000000 --- a/src/db/UpnpDatabasePlugin.cxx +++ /dev/null @@ -1,785 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "UpnpDatabasePlugin.hxx" -#include "upnp/Domain.hxx" -#include "upnp/upnpplib.hxx" -#include "upnp/Discovery.hxx" -#include "upnp/ContentDirectoryService.hxx" -#include "upnp/Directory.hxx" -#include "upnp/Tags.hxx" -#include "upnp/Util.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseSelection.hxx" -#include "DatabaseError.hxx" -#include "LightDirectory.hxx" -#include "LightSong.hxx" -#include "config/ConfigData.hxx" -#include "tag/TagBuilder.hxx" -#include "tag/TagTable.hxx" -#include "util/Error.hxx" -#include "util/Domain.hxx" -#include "fs/Traits.hxx" -#include "Log.hxx" -#include "SongFilter.hxx" - -#include -#include -#include - -#include -#include - -static const char *const rootid = "0"; - -class UpnpSong : public LightSong { - std::string uri2, real_uri2; - - Tag tag2; - -public: - UpnpSong(UPnPDirObject &&object, std::string &&_uri) - :uri2(std::move(_uri)), - real_uri2(std::move(object.url)), - tag2(std::move(object.tag)) { - directory = nullptr; - uri = uri2.c_str(); - real_uri = real_uri2.c_str(); - tag = &tag2; - mtime = 0; - start_ms = end_ms = 0; - } -}; - -class UpnpDatabase : public Database { - LibUPnP *m_lib; - UPnPDeviceDirectory *m_superdir; - -public: - static Database *Create(EventLoop &loop, DatabaseListener &listener, - const config_param ¶m, - Error &error); - - virtual bool Open(Error &error) override; - virtual void Close() override; - virtual const LightSong *GetSong(const char *uri_utf8, - Error &error) const override; - virtual void ReturnSong(const LightSong *song) const; - - virtual bool Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const override; - - virtual bool VisitUniqueTags(const DatabaseSelection &selection, - TagType tag_type, - VisitString visit_string, - Error &error) const override; - - virtual bool GetStats(const DatabaseSelection &selection, - DatabaseStats &stats, - Error &error) const override; - virtual time_t GetUpdateStamp() const {return 0;} - -protected: - bool Configure(const config_param ¶m, Error &error); - -private: - bool VisitServer(const ContentDirectoryService &server, - const std::list &vpath, - const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const; - - /** - * Run an UPnP search according to MPD parameters, and - * visit_song the results. - */ - bool SearchSongs(const ContentDirectoryService &server, - const char *objid, - const DatabaseSelection &selection, - VisitSong visit_song, - Error &error) const; - - bool SearchSongs(const ContentDirectoryService &server, - const char *objid, - const DatabaseSelection &selection, - UPnPDirContent& dirbuf, - Error &error) const; - - bool Namei(const ContentDirectoryService &server, - const std::list &vpath, - UPnPDirObject &dirent, - Error &error) const; - - /** - * Take server and objid, return metadata. - */ - bool ReadNode(const ContentDirectoryService &server, - const char *objid, UPnPDirObject& dirent, - Error &error) const; - - /** - * Get the path for an object Id. This works much like pwd, - * except easier cause our inodes have a parent id. Not used - * any more actually (see comments in SearchSongs). - */ - bool BuildPath(const ContentDirectoryService &server, - const UPnPDirObject& dirent, std::string &idpath, - Error &error) const; -}; - -Database * -UpnpDatabase::Create(gcc_unused EventLoop &loop, - gcc_unused DatabaseListener &listener, - const config_param ¶m, Error &error) -{ - UpnpDatabase *db = new UpnpDatabase(); - if (!db->Configure(param, error)) { - delete db; - return nullptr; - } - - /* libupnp loses its ability to receive multicast messages - apparently due to daemonization; using the LazyDatabase - wrapper works around this problem */ - return db; -} - -inline bool -UpnpDatabase::Configure(const config_param &, Error &) -{ - return true; -} - -bool -UpnpDatabase::Open(Error &error) -{ - m_lib = new LibUPnP(); - if (!m_lib->ok()) { - error.Set(m_lib->GetInitError()); - delete m_lib; - return false; - } - - m_superdir = new UPnPDeviceDirectory(m_lib); - if (!m_superdir->Start(error)) { - delete m_superdir; - delete m_lib; - return false; - } - - // Wait for device answers. This should be consistent with the value set - // in the lib (currently 2) - sleep(2); - return true; -} - -void -UpnpDatabase::Close() -{ - delete m_superdir; - delete m_lib; -} - -void -UpnpDatabase::ReturnSong(const LightSong *_song) const -{ - assert(_song != nullptr); - - UpnpSong *song = (UpnpSong *)const_cast(_song); - delete song; -} - -// Get song info by path. We can receive either the id path, or the titles -// one -const LightSong * -UpnpDatabase::GetSong(const char *uri, Error &error) const -{ - auto vpath = stringToTokens(uri, "/", true); - if (vpath.size() < 2) { - error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri); - return nullptr; - } - - ContentDirectoryService server; - if (!m_superdir->getServer(vpath.front().c_str(), server, error)) - return nullptr; - - vpath.pop_front(); - - UPnPDirObject dirent; - if (vpath.front() != rootid) { - if (!Namei(server, vpath, dirent, error)) - return nullptr; - } else { - if (!ReadNode(server, vpath.back().c_str(), dirent, - error)) - return nullptr; - } - - return new UpnpSong(std::move(dirent), uri); -} - -/** - * Double-quote a string, adding internal backslash escaping. - */ -static void -dquote(std::string &out, const char *in) -{ - out.push_back('"'); - - for (; *in != 0; ++in) { - switch(*in) { - case '\\': - case '"': - out.push_back('\\'); - break; - } - - out.push_back(*in); - } - - out.push_back('"'); -} - -// Run an UPnP search, according to MPD parameters. Return results as -// UPnP items -bool -UpnpDatabase::SearchSongs(const ContentDirectoryService &server, - const char *objid, - const DatabaseSelection &selection, - UPnPDirContent &dirbuf, - Error &error) const -{ - const SongFilter *filter = selection.filter; - if (selection.filter == nullptr) - return true; - - std::list searchcaps; - if (!server.getSearchCapabilities(m_lib->getclh(), searchcaps, error)) - return false; - - if (searchcaps.empty()) - return true; - - std::string cond; - for (const auto &item : filter->GetItems()) { - switch (auto tag = item.GetTag()) { - case LOCATE_TAG_ANY_TYPE: - { - if (!cond.empty()) { - cond += " and "; - } - cond += '('; - bool first(true); - for (const auto& cap : searchcaps) { - if (first) - first = false; - else - cond += " or "; - cond += cap; - if (item.GetFoldCase()) { - cond += " contains "; - } else { - cond += " = "; - } - dquote(cond, item.GetValue().c_str()); - } - cond += ')'; - } - break; - - default: - /* Unhandled conditions like - LOCATE_TAG_BASE_TYPE or - LOCATE_TAG_FILE_TYPE won't have a - corresponding upnp prop, so they will be - skipped */ - if (tag == TAG_ALBUM_ARTIST) - tag = TAG_ARTIST; - - // TODO: support LOCATE_TAG_ANY_TYPE etc. - const char *name = tag_table_lookup(upnp_tags, - TagType(tag)); - if (name == nullptr) - continue; - - if (!cond.empty()) { - cond += " and "; - } - cond += name; - - /* FoldCase doubles up as contains/equal - switch. UpNP search is supposed to be - case-insensitive, but at least some servers - have the same convention as mpd (e.g.: - minidlna) */ - if (item.GetFoldCase()) { - cond += " contains "; - } else { - cond += " = "; - } - dquote(cond, item.GetValue().c_str()); - } - } - - return server.search(m_lib->getclh(), - objid, cond.c_str(), dirbuf, - error); -} - -static bool -visitSong(const UPnPDirObject &meta, const char *path, - const DatabaseSelection &selection, - VisitSong visit_song, Error& error) -{ - if (!visit_song) - return true; - - LightSong song; - song.directory = nullptr; - song.uri = path; - song.real_uri = meta.url.c_str(); - song.tag = &meta.tag; - song.mtime = 0; - song.start_ms = song.end_ms = 0; - - return !selection.Match(song) || visit_song(song, error); -} - -/** - * Build synthetic path based on object id for search results. The use - * of "rootid" is arbitrary, any name that is not likely to be a top - * directory name would fit. - */ -static std::string -songPath(const std::string &servername, - const std::string &objid) -{ - return servername + "/" + rootid + "/" + objid; -} - -bool -UpnpDatabase::SearchSongs(const ContentDirectoryService &server, - const char *objid, - const DatabaseSelection &selection, - VisitSong visit_song, - Error &error) const -{ - UPnPDirContent dirbuf; - if (!visit_song) - return true; - if (!SearchSongs(server, objid, selection, dirbuf, error)) - return false; - - for (auto &dirent : dirbuf.objects) { - if (dirent.type != UPnPDirObject::Type::ITEM || - dirent.item_class != UPnPDirObject::ItemClass::MUSIC) - continue; - - // We get song ids as the result of the UPnP search. But our - // client expects paths (e.g. we get 1$4$3788 from minidlna, - // but we need to translate to /Music/All_Music/Satisfaction). - // We can do this in two ways: - // - Rebuild a normal path using BuildPath() which is a kind of pwd - // - Build a bogus path based on the song id. - // The first method is nice because the returned paths are pretty, but - // it has two big problems: - // - The song paths are ambiguous: e.g. minidlna returns all search - // results as being from the "All Music" directory, which can - // contain several songs with the same title (but different objids) - // - The performance of BuildPath() is atrocious on very big - // directories, even causing timeouts in clients. And of - // course, 'All Music' is very big. - // So we return synthetic and ugly paths based on the object id, - // which we later have to detect. - const std::string path = songPath(server.getFriendlyName(), - dirent.m_id); - if (!visitSong(std::move(dirent), path.c_str(), - selection, visit_song, - error)) - return false; - } - - return true; -} - -bool -UpnpDatabase::ReadNode(const ContentDirectoryService &server, - const char *objid, UPnPDirObject &dirent, - Error &error) const -{ - UPnPDirContent dirbuf; - if (!server.getMetadata(m_lib->getclh(), objid, dirbuf, error)) - return false; - - if (dirbuf.objects.size() == 1) { - dirent = std::move(dirbuf.objects.front()); - } else { - error.Format(upnp_domain, "Bad resource"); - return false; - } - - return true; -} - -bool -UpnpDatabase::BuildPath(const ContentDirectoryService &server, - const UPnPDirObject& idirent, - std::string &path, - Error &error) const -{ - const char *pid = idirent.m_id.c_str(); - path.clear(); - UPnPDirObject dirent; - while (strcmp(pid, rootid) != 0) { - if (!ReadNode(server, pid, dirent, error)) - return false; - pid = dirent.m_pid.c_str(); - - if (path.empty()) - path = dirent.name; - else - path = PathTraitsUTF8::Build(dirent.name.c_str(), - path.c_str()); - } - - path = PathTraitsUTF8::Build(server.getFriendlyName(), - path.c_str()); - return true; -} - -// Take server and internal title pathname and return objid and metadata. -bool -UpnpDatabase::Namei(const ContentDirectoryService &server, - const std::list &vpath, - UPnPDirObject &odirent, - Error &error) const -{ - if (vpath.empty()) { - // looking for root info - if (!ReadNode(server, rootid, odirent, error)) - return false; - - return true; - } - - const UpnpClient_Handle handle = m_lib->getclh(); - - std::string objid(rootid); - - // Walk the path elements, read each directory and try to find the next one - for (auto i = vpath.begin(), last = std::prev(vpath.end());; ++i) { - UPnPDirContent dirbuf; - if (!server.readDir(handle, objid.c_str(), dirbuf, error)) - return false; - - // Look for the name in the sub-container list - UPnPDirObject *child = dirbuf.FindObject(i->c_str()); - if (child == nullptr) { - error.Format(db_domain, DB_NOT_FOUND, - "No such object"); - return false; - } - - if (i == last) { - odirent = std::move(*child); - return true; - } - - if (child->type != UPnPDirObject::Type::CONTAINER) { - error.Format(db_domain, DB_NOT_FOUND, - "Not a container"); - return false; - } - - objid = std::move(child->m_id); - } -} - -static bool -VisitItem(const UPnPDirObject &object, const char *uri, - const DatabaseSelection &selection, - VisitSong visit_song, VisitPlaylist visit_playlist, - Error &error) -{ - assert(object.type == UPnPDirObject::Type::ITEM); - - switch (object.item_class) { - case UPnPDirObject::ItemClass::MUSIC: - return !visit_song || - visitSong(object, uri, - selection, visit_song, error); - - case UPnPDirObject::ItemClass::PLAYLIST: - if (visit_playlist) { - /* Note: I've yet to see a - playlist item (playlists - seem to be usually handled - as containers, so I'll - decide what to do when I - see one... */ - } - - return true; - - case UPnPDirObject::ItemClass::UNKNOWN: - return true; - } - - assert(false); - gcc_unreachable(); -} - -static bool -VisitObject(const UPnPDirObject &object, const char *uri, - const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) -{ - switch (object.type) { - case UPnPDirObject::Type::UNKNOWN: - assert(false); - gcc_unreachable(); - - case UPnPDirObject::Type::CONTAINER: - return !visit_directory || - visit_directory(LightDirectory(uri, 0), error); - - case UPnPDirObject::Type::ITEM: - return VisitItem(object, uri, selection, - visit_song, visit_playlist, - error); - } - - assert(false); - gcc_unreachable(); -} - -// vpath is a parsed and writeable version of selection.uri. There is -// really just one path parameter. -bool -UpnpDatabase::VisitServer(const ContentDirectoryService &server, - const std::list &vpath, - const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const -{ - /* If the path begins with rootid, we know that this is a - song, not a directory (because that's how we set things - up). Just visit it. Note that the choice of rootid is - arbitrary, any value not likely to be the name of a top - directory would be ok. */ - /* !Note: this *can't* be handled by Namei further down, - because the path is not valid for traversal. Besides, it's - just faster to access the target node directly */ - if (!vpath.empty() && vpath.front() == rootid) { - switch (vpath.size()) { - case 1: - return true; - - case 2: - break; - - default: - error.Format(db_domain, DB_NOT_FOUND, - "Not found"); - return false; - } - - if (visit_song) { - UPnPDirObject dirent; - if (!ReadNode(server, vpath.back().c_str(), dirent, - error)) - return false; - - if (dirent.type != UPnPDirObject::Type::ITEM || - dirent.item_class != UPnPDirObject::ItemClass::MUSIC) { - error.Format(db_domain, DB_NOT_FOUND, - "Not found"); - return false; - } - - std::string path = songPath(server.getFriendlyName(), - dirent.m_id); - if (!visitSong(std::move(dirent), path.c_str(), - selection, - visit_song, error)) - return false; - } - return true; - } - - // Translate the target path into an object id and the associated metadata. - UPnPDirObject tdirent; - if (!Namei(server, vpath, tdirent, error)) - return false; - - /* If recursive is set, this is a search... No use sending it - if the filter is empty. In this case, we implement limited - recursion (1-deep) here, which will handle the "add dir" - case. */ - if (selection.recursive && selection.filter) - return SearchSongs(server, tdirent.m_id.c_str(), selection, - visit_song, error); - - const char *const base_uri = selection.uri.empty() - ? server.getFriendlyName() - : selection.uri.c_str(); - - if (tdirent.type == UPnPDirObject::Type::ITEM) { - return VisitItem(tdirent, base_uri, - selection, - visit_song, visit_playlist, - error); - } - - /* Target was a a container. Visit it. We could read slices - and loop here, but it's not useful as mpd will only return - data to the client when we're done anyway. */ - UPnPDirContent dirbuf; - if (!server.readDir(m_lib->getclh(), tdirent.m_id.c_str(), dirbuf, - error)) - return false; - - for (auto &dirent : dirbuf.objects) { - const std::string uri = PathTraitsUTF8::Build(base_uri, - dirent.name.c_str()); - if (!VisitObject(dirent, uri.c_str(), - selection, - visit_directory, - visit_song, visit_playlist, - error)) - return false; - } - - return true; -} - -// Deal with the possibly multiple servers, call VisitServer if needed. -bool -UpnpDatabase::Visit(const DatabaseSelection &selection, - VisitDirectory visit_directory, - VisitSong visit_song, - VisitPlaylist visit_playlist, - Error &error) const -{ - auto vpath = stringToTokens(selection.uri, "/", true); - if (vpath.empty()) { - std::vector servers; - if (!m_superdir->getDirServices(servers, error)) - return false; - - for (const auto &server : servers) { - if (visit_directory) { - const LightDirectory d(server.getFriendlyName(), 0); - if (!visit_directory(d, error)) - return false; - } - - if (selection.recursive && - !VisitServer(server, vpath, selection, - visit_directory, visit_song, visit_playlist, - error)) - return false; - } - - return true; - } - - // We do have a path: the first element selects the server - std::string servername(std::move(vpath.front())); - vpath.pop_front(); - - ContentDirectoryService server; - if (!m_superdir->getServer(servername.c_str(), server, error)) - return false; - - return VisitServer(server, vpath, selection, - visit_directory, visit_song, visit_playlist, error); -} - -bool -UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection, - TagType tag, - VisitString visit_string, - Error &error) const -{ - if (!visit_string) - return true; - - std::vector servers; - if (!m_superdir->getDirServices(servers, error)) - return false; - - std::set values; - for (auto& server : servers) { - UPnPDirContent dirbuf; - if (!SearchSongs(server, rootid, selection, dirbuf, error)) - return false; - - for (const auto &dirent : dirbuf.objects) { - if (dirent.type != UPnPDirObject::Type::ITEM || - dirent.item_class != UPnPDirObject::ItemClass::MUSIC) - continue; - - const char *value = dirent.tag.GetValue(tag); - if (value != nullptr) { -#if defined(__clang__) || GCC_CHECK_VERSION(4,8) - values.emplace(value); -#else - values.insert(value); -#endif - } - } - } - - for (const auto& value : values) - if (!visit_string(value.c_str(), error)) - return false; - - return true; -} - -bool -UpnpDatabase::GetStats(const DatabaseSelection &, - DatabaseStats &stats, Error &) const -{ - /* Note: this gets called before the daemonizing so we can't - reallyopen this would be a problem if we had real stats */ - stats.song_count = 0; - stats.total_duration = 0; - stats.artist_count = 0; - stats.album_count = 0; - return true; -} - -const DatabasePlugin upnp_db_plugin = { - "upnp", - UpnpDatabase::Create, -}; diff --git a/src/db/UpnpDatabasePlugin.hxx b/src/db/UpnpDatabasePlugin.hxx deleted file mode 100644 index 0228405cd..000000000 --- a/src/db/UpnpDatabasePlugin.hxx +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_DATABASE_PLUGIN_HXX -#define MPD_UPNP_DATABASE_PLUGIN_HXX - -struct DatabasePlugin; - -extern const DatabasePlugin upnp_db_plugin; - -#endif diff --git a/src/db/Visitor.hxx b/src/db/Visitor.hxx new file mode 100644 index 000000000..0ec29bf49 --- /dev/null +++ b/src/db/Visitor.hxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_DATABASE_VISITOR_HXX +#define MPD_DATABASE_VISITOR_HXX + +#include + +struct LightDirectory; +struct LightSong; +struct PlaylistInfo; +class Error; + +typedef std::function VisitDirectory; +typedef std::function VisitSong; +typedef std::function VisitPlaylist; + +typedef std::function VisitString; + +#endif diff --git a/src/db/plugins/LazyDatabase.cxx b/src/db/plugins/LazyDatabase.cxx new file mode 100644 index 000000000..6a01ffb82 --- /dev/null +++ b/src/db/plugins/LazyDatabase.cxx @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "LazyDatabase.hxx" + +#include + +LazyDatabase::~LazyDatabase() +{ + assert(!open); + + delete db; +} + +bool +LazyDatabase::EnsureOpen(Error &error) const +{ + if (open) + return true; + + if (!db->Open(error)) + return false; + + open = true; + return true; +} + +void +LazyDatabase::Close() +{ + if (open) { + open = false; + db->Close(); + } +} + +const LightSong * +LazyDatabase::GetSong(const char *uri, Error &error) const +{ + return EnsureOpen(error) + ? db->GetSong(uri, error) + : nullptr; +} + +void +LazyDatabase::ReturnSong(const LightSong *song) const +{ + assert(open); + + db->ReturnSong(song); +} + +bool +LazyDatabase::Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const +{ + return EnsureOpen(error) && + db->Visit(selection, visit_directory, visit_song, + visit_playlist, error); +} + +bool +LazyDatabase::VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const +{ + return EnsureOpen(error) && + db->VisitUniqueTags(selection, tag_type, visit_string, error); +} + +bool +LazyDatabase::GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, Error &error) const +{ + return EnsureOpen(error) && db->GetStats(selection, stats, error); +} + +time_t +LazyDatabase::GetUpdateStamp() const +{ + return open ? db->GetUpdateStamp() : 0; +} diff --git a/src/db/plugins/LazyDatabase.hxx b/src/db/plugins/LazyDatabase.hxx new file mode 100644 index 000000000..336b8558f --- /dev/null +++ b/src/db/plugins/LazyDatabase.hxx @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_LAZY_DATABASE_PLUGIN_HXX +#define MPD_LAZY_DATABASE_PLUGIN_HXX + +#include "db/DatabasePlugin.hxx" + +/** + * A wrapper for a #Database object that gets opened on the first + * database access. This works around daemonization problems with + * some plugins. + */ +class LazyDatabase final : public Database { + Database *const db; + + mutable bool open; + +public: + gcc_nonnull_all + LazyDatabase(Database *_db) + :db(_db), open(false) {} + + virtual ~LazyDatabase(); + + virtual void Close() override; + + virtual const LightSong *GetSong(const char *uri_utf8, + Error &error) const override; + virtual void ReturnSong(const LightSong *song) const; + + virtual bool Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const override; + + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const override; + + virtual bool GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, + Error &error) const override; + + virtual time_t GetUpdateStamp() const override; + +private: + bool EnsureOpen(Error &error) const; +}; + +#endif diff --git a/src/db/plugins/ProxyDatabasePlugin.cxx b/src/db/plugins/ProxyDatabasePlugin.cxx new file mode 100644 index 000000000..daa963c7d --- /dev/null +++ b/src/db/plugins/ProxyDatabasePlugin.cxx @@ -0,0 +1,782 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "ProxyDatabasePlugin.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/DatabaseListener.hxx" +#include "db/Selection.hxx" +#include "db/DatabaseError.hxx" +#include "PlaylistInfo.hxx" +#include "db/LightDirectory.hxx" +#include "db/LightSong.hxx" +#include "SongFilter.hxx" +#include "Compiler.h" +#include "config/ConfigData.hxx" +#include "tag/TagBuilder.hxx" +#include "tag/Tag.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" +#include "protocol/Ack.hxx" +#include "Main.hxx" +#include "event/SocketMonitor.hxx" +#include "event/IdleMonitor.hxx" +#include "Log.hxx" + +#include +#include + +#include +#include +#include + +class ProxySong : public LightSong { + Tag tag2; + +public: + explicit ProxySong(const mpd_song *song); +}; + +class AllocatedProxySong : public ProxySong { + mpd_song *const song; + +public: + explicit AllocatedProxySong(mpd_song *_song) + :ProxySong(_song), song(_song) {} + + ~AllocatedProxySong() { + mpd_song_free(song); + } +}; + +class ProxyDatabase final : public Database, SocketMonitor, IdleMonitor { + DatabaseListener &listener; + + std::string host; + unsigned port; + + struct mpd_connection *connection; + + /* this is mutable because GetStats() must be "const" */ + mutable time_t update_stamp; + + /** + * The libmpdclient idle mask that was removed from the other + * MPD. This will be handled by the next OnIdle() call. + */ + unsigned idle_received; + + /** + * Is the #connection currently "idle"? That is, did we send + * the "idle" command to it? + */ + bool is_idle; + +public: + ProxyDatabase(EventLoop &_loop, DatabaseListener &_listener) + :SocketMonitor(_loop), IdleMonitor(_loop), + listener(_listener) {} + + static Database *Create(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, + Error &error); + + virtual bool Open(Error &error) override; + virtual void Close() override; + virtual const LightSong *GetSong(const char *uri_utf8, + Error &error) const override; + virtual void ReturnSong(const LightSong *song) const; + + virtual bool Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const override; + + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const override; + + virtual bool GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, + Error &error) const override; + + virtual time_t GetUpdateStamp() const override { + return update_stamp; + } + +private: + bool Configure(const config_param ¶m, Error &error); + + bool Connect(Error &error); + bool CheckConnection(Error &error); + bool EnsureConnected(Error &error); + + void Disconnect(); + + /* virtual methods from SocketMonitor */ + virtual bool OnSocketReady(unsigned flags) override; + + /* virtual methods from IdleMonitor */ + virtual void OnIdle() override; +}; + +static constexpr Domain libmpdclient_domain("libmpdclient"); + +static constexpr struct { + TagType d; + enum mpd_tag_type s; +} tag_table[] = { + { TAG_ARTIST, MPD_TAG_ARTIST }, + { TAG_ALBUM, MPD_TAG_ALBUM }, + { TAG_ALBUM_ARTIST, MPD_TAG_ALBUM_ARTIST }, + { TAG_TITLE, MPD_TAG_TITLE }, + { TAG_TRACK, MPD_TAG_TRACK }, + { TAG_NAME, MPD_TAG_NAME }, + { TAG_GENRE, MPD_TAG_GENRE }, + { TAG_DATE, MPD_TAG_DATE }, + { TAG_COMPOSER, MPD_TAG_COMPOSER }, + { TAG_PERFORMER, MPD_TAG_PERFORMER }, + { TAG_COMMENT, MPD_TAG_COMMENT }, + { TAG_DISC, MPD_TAG_DISC }, + { TAG_MUSICBRAINZ_ARTISTID, MPD_TAG_MUSICBRAINZ_ARTISTID }, + { TAG_MUSICBRAINZ_ALBUMID, MPD_TAG_MUSICBRAINZ_ALBUMID }, + { TAG_MUSICBRAINZ_ALBUMARTISTID, + MPD_TAG_MUSICBRAINZ_ALBUMARTISTID }, + { TAG_MUSICBRAINZ_TRACKID, MPD_TAG_MUSICBRAINZ_TRACKID }, + { TAG_NUM_OF_ITEM_TYPES, MPD_TAG_COUNT } +}; + +static void +Copy(TagBuilder &tag, TagType d_tag, + const struct mpd_song *song, enum mpd_tag_type s_tag) +{ + + for (unsigned i = 0;; ++i) { + const char *value = mpd_song_get_tag(song, s_tag, i); + if (value == nullptr) + break; + + tag.AddItem(d_tag, value); + } +} + +ProxySong::ProxySong(const mpd_song *song) +{ + directory = nullptr; + uri = mpd_song_get_uri(song); + tag = &tag2; + mtime = mpd_song_get_last_modified(song); + start_ms = mpd_song_get_start(song) * 1000; + end_ms = mpd_song_get_end(song) * 1000; + + TagBuilder tag_builder; + tag_builder.SetTime(mpd_song_get_duration(song)); + + for (const auto *i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i) + Copy(tag_builder, i->d, song, i->s); + + tag_builder.Commit(tag2); +} + +gcc_const +static enum mpd_tag_type +Convert(TagType tag_type) +{ + for (auto i = &tag_table[0]; i->d != TAG_NUM_OF_ITEM_TYPES; ++i) + if (i->d == tag_type) + return i->s; + + return MPD_TAG_COUNT; +} + +static bool +CheckError(struct mpd_connection *connection, Error &error) +{ + const auto code = mpd_connection_get_error(connection); + if (code == MPD_ERROR_SUCCESS) + return true; + + if (code == MPD_ERROR_SERVER) { + /* libmpdclient's "enum mpd_server_error" is the same + as our "enum ack" */ + const auto server_error = + mpd_connection_get_server_error(connection); + error.Set(ack_domain, (int)server_error, + mpd_connection_get_error_message(connection)); + } else { + error.Set(libmpdclient_domain, (int)code, + mpd_connection_get_error_message(connection)); + } + + mpd_connection_clear_error(connection); + return false; +} + +static bool +SendConstraints(mpd_connection *connection, const SongFilter::Item &item) +{ + switch (item.GetTag()) { + mpd_tag_type tag; + +#if LIBMPDCLIENT_CHECK_VERSION(2,9,0) + case LOCATE_TAG_BASE_TYPE: + if (mpd_connection_cmp_server_version(connection, 0, 18, 0) < 0) + /* requires MPD 0.18 */ + return true; + + return mpd_search_add_base_constraint(connection, + MPD_OPERATOR_DEFAULT, + item.GetValue().c_str()); +#endif + + case LOCATE_TAG_FILE_TYPE: + return mpd_search_add_uri_constraint(connection, + MPD_OPERATOR_DEFAULT, + item.GetValue().c_str()); + + case LOCATE_TAG_ANY_TYPE: + return mpd_search_add_any_tag_constraint(connection, + MPD_OPERATOR_DEFAULT, + item.GetValue().c_str()); + + default: + tag = Convert(TagType(item.GetTag())); + if (tag == MPD_TAG_COUNT) + return true; + + return mpd_search_add_tag_constraint(connection, + MPD_OPERATOR_DEFAULT, + tag, + item.GetValue().c_str()); + } +} + +static bool +SendConstraints(mpd_connection *connection, const SongFilter &filter) +{ + for (const auto &i : filter.GetItems()) + if (!SendConstraints(connection, i)) + return false; + + return true; +} + +static bool +SendConstraints(mpd_connection *connection, const DatabaseSelection &selection) +{ +#if LIBMPDCLIENT_CHECK_VERSION(2,9,0) + if (!selection.uri.empty() && + mpd_connection_cmp_server_version(connection, 0, 18, 0) >= 0) { + /* requires MPD 0.18 */ + if (!mpd_search_add_base_constraint(connection, + MPD_OPERATOR_DEFAULT, + selection.uri.c_str())) + return false; + } +#endif + + if (selection.filter != nullptr && + !SendConstraints(connection, *selection.filter)) + return false; + + return true; +} + +Database * +ProxyDatabase::Create(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, Error &error) +{ + ProxyDatabase *db = new ProxyDatabase(loop, listener); + if (!db->Configure(param, error)) { + delete db; + db = nullptr; + } + + return db; +} + +bool +ProxyDatabase::Configure(const config_param ¶m, gcc_unused Error &error) +{ + host = param.GetBlockValue("host", ""); + port = param.GetBlockValue("port", 0u); + + return true; +} + +bool +ProxyDatabase::Open(Error &error) +{ + if (!Connect(error)) + return false; + + update_stamp = 0; + + return true; +} + +void +ProxyDatabase::Close() +{ + if (connection != nullptr) + Disconnect(); +} + +bool +ProxyDatabase::Connect(Error &error) +{ + const char *_host = host.empty() ? nullptr : host.c_str(); + connection = mpd_connection_new(_host, port, 0); + if (connection == nullptr) { + error.Set(libmpdclient_domain, (int)MPD_ERROR_OOM, + "Out of memory"); + return false; + } + + idle_received = unsigned(-1); + is_idle = false; + + SocketMonitor::Open(mpd_async_get_fd(mpd_connection_get_async(connection))); + IdleMonitor::Schedule(); + + if (!CheckError(connection, error)) { + if (connection != nullptr) + Disconnect(); + + return false; + } + + return true; +} + +bool +ProxyDatabase::CheckConnection(Error &error) +{ + assert(connection != nullptr); + + if (!mpd_connection_clear_error(connection)) { + Disconnect(); + return Connect(error); + } + + if (is_idle) { + unsigned idle = mpd_run_noidle(connection); + if (idle == 0 && !CheckError(connection, error)) { + Disconnect(); + return false; + } + + idle_received |= idle; + is_idle = false; + IdleMonitor::Schedule(); + } + + return true; +} + +bool +ProxyDatabase::EnsureConnected(Error &error) +{ + return connection != nullptr + ? CheckConnection(error) + : Connect(error); +} + +void +ProxyDatabase::Disconnect() +{ + assert(connection != nullptr); + + IdleMonitor::Cancel(); + SocketMonitor::Steal(); + + mpd_connection_free(connection); + connection = nullptr; +} + +bool +ProxyDatabase::OnSocketReady(gcc_unused unsigned flags) +{ + assert(connection != nullptr); + + if (!is_idle) { + // TODO: can this happen? + IdleMonitor::Schedule(); + return false; + } + + unsigned idle = (unsigned)mpd_recv_idle(connection, false); + if (idle == 0) { + Error error; + if (!CheckError(connection, error)) { + LogError(error); + Disconnect(); + return false; + } + } + + /* let OnIdle() handle this */ + idle_received |= idle; + is_idle = false; + IdleMonitor::Schedule(); + return false; +} + +void +ProxyDatabase::OnIdle() +{ + assert(connection != nullptr); + + /* handle previous idle events */ + + if (idle_received & MPD_IDLE_DATABASE) + listener.OnDatabaseModified(); + + idle_received = 0; + + /* send a new idle command to the other MPD */ + + if (is_idle) + // TODO: can this happen? + return; + + if (!mpd_send_idle_mask(connection, MPD_IDLE_DATABASE)) { + Error error; + if (!CheckError(connection, error)) + LogError(error); + + SocketMonitor::Steal(); + mpd_connection_free(connection); + connection = nullptr; + return; + } + + is_idle = true; + SocketMonitor::ScheduleRead(); +} + +const LightSong * +ProxyDatabase::GetSong(const char *uri, Error &error) const +{ + // TODO: eliminate the const_cast + if (!const_cast(this)->EnsureConnected(error)) + return nullptr; + + if (!mpd_send_list_meta(connection, uri)) { + CheckError(connection, error); + return nullptr; + } + + struct mpd_song *song = mpd_recv_song(connection); + if (!mpd_response_finish(connection) && + !CheckError(connection, error)) { + if (song != nullptr) + mpd_song_free(song); + return nullptr; + } + + if (song == nullptr) { + error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri); + return nullptr; + } + + return new AllocatedProxySong(song); +} + +void +ProxyDatabase::ReturnSong(const LightSong *_song) const +{ + assert(_song != nullptr); + + AllocatedProxySong *song = (AllocatedProxySong *) + const_cast(_song); + delete song; +} + +static bool +Visit(struct mpd_connection *connection, const char *uri, + bool recursive, const SongFilter *filter, + VisitDirectory visit_directory, VisitSong visit_song, + VisitPlaylist visit_playlist, Error &error); + +static bool +Visit(struct mpd_connection *connection, + bool recursive, const SongFilter *filter, + const struct mpd_directory *directory, + VisitDirectory visit_directory, VisitSong visit_song, + VisitPlaylist visit_playlist, Error &error) +{ + const char *path = mpd_directory_get_path(directory); +#if LIBMPDCLIENT_CHECK_VERSION(2,9,0) + time_t mtime = mpd_directory_get_last_modified(directory); +#else + time_t mtime = 0; +#endif + + if (visit_directory && + !visit_directory(LightDirectory(path, mtime), error)) + return false; + + if (recursive && + !Visit(connection, path, recursive, filter, + visit_directory, visit_song, visit_playlist, error)) + return false; + + return true; +} + +gcc_pure +static bool +Match(const SongFilter *filter, const LightSong &song) +{ + return filter == nullptr || filter->Match(song); +} + +static bool +Visit(const SongFilter *filter, + const mpd_song *_song, + VisitSong visit_song, Error &error) +{ + if (!visit_song) + return true; + + const ProxySong song(_song); + return !Match(filter, song) || visit_song(song, error); +} + +static bool +Visit(const struct mpd_playlist *playlist, + VisitPlaylist visit_playlist, Error &error) +{ + if (!visit_playlist) + return true; + + PlaylistInfo p(mpd_playlist_get_path(playlist), + mpd_playlist_get_last_modified(playlist)); + + return visit_playlist(p, LightDirectory::Root(), error); +} + +class ProxyEntity { + struct mpd_entity *entity; + +public: + explicit ProxyEntity(struct mpd_entity *_entity) + :entity(_entity) {} + + ProxyEntity(const ProxyEntity &other) = delete; + + ProxyEntity(ProxyEntity &&other) + :entity(other.entity) { + other.entity = nullptr; + } + + ~ProxyEntity() { + if (entity != nullptr) + mpd_entity_free(entity); + } + + ProxyEntity &operator=(const ProxyEntity &other) = delete; + + operator const struct mpd_entity *() const { + return entity; + } +}; + +static std::list +ReceiveEntities(struct mpd_connection *connection) +{ + std::list entities; + struct mpd_entity *entity; + while ((entity = mpd_recv_entity(connection)) != nullptr) + entities.push_back(ProxyEntity(entity)); + + mpd_response_finish(connection); + return entities; +} + +static bool +Visit(struct mpd_connection *connection, const char *uri, + bool recursive, const SongFilter *filter, + VisitDirectory visit_directory, VisitSong visit_song, + VisitPlaylist visit_playlist, Error &error) +{ + if (!mpd_send_list_meta(connection, uri)) + return CheckError(connection, error); + + std::list entities(ReceiveEntities(connection)); + if (!CheckError(connection, error)) + return false; + + for (const auto &entity : entities) { + switch (mpd_entity_get_type(entity)) { + case MPD_ENTITY_TYPE_UNKNOWN: + break; + + case MPD_ENTITY_TYPE_DIRECTORY: + if (!Visit(connection, recursive, filter, + mpd_entity_get_directory(entity), + visit_directory, visit_song, visit_playlist, + error)) + return false; + break; + + case MPD_ENTITY_TYPE_SONG: + if (!Visit(filter, + mpd_entity_get_song(entity), visit_song, + error)) + return false; + break; + + case MPD_ENTITY_TYPE_PLAYLIST: + if (!Visit(mpd_entity_get_playlist(entity), + visit_playlist, error)) + return false; + break; + } + } + + return CheckError(connection, error); +} + +static bool +SearchSongs(struct mpd_connection *connection, + const DatabaseSelection &selection, + VisitSong visit_song, + Error &error) +{ + assert(selection.recursive); + assert(visit_song); + + const bool exact = selection.filter == nullptr || + !selection.filter->HasFoldCase(); + + if (!mpd_search_db_songs(connection, exact) || + !SendConstraints(connection, selection) || + !mpd_search_commit(connection)) + return CheckError(connection, error); + + bool result = true; + struct mpd_song *song; + while (result && (song = mpd_recv_song(connection)) != nullptr) { + AllocatedProxySong song2(song); + + result = !Match(selection.filter, song2) || + visit_song(song2, error); + } + + mpd_response_finish(connection); + return result && CheckError(connection, error); +} + +bool +ProxyDatabase::Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const +{ + // TODO: eliminate the const_cast + if (!const_cast(this)->EnsureConnected(error)) + return nullptr; + + if (!visit_directory && !visit_playlist && selection.recursive) + /* this optimized code path can only be used under + certain conditions */ + return ::SearchSongs(connection, selection, visit_song, error); + + /* fall back to recursive walk (slow!) */ + return ::Visit(connection, selection.uri.c_str(), + selection.recursive, selection.filter, + visit_directory, visit_song, visit_playlist, + error); +} + +bool +ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const +{ + // TODO: eliminate the const_cast + if (!const_cast(this)->EnsureConnected(error)) + return nullptr; + + enum mpd_tag_type tag_type2 = Convert(tag_type); + if (tag_type2 == MPD_TAG_COUNT) { + error.Set(libmpdclient_domain, "Unsupported tag"); + return false; + } + + if (!mpd_search_db_tags(connection, tag_type2)) + return CheckError(connection, error); + + if (!SendConstraints(connection, selection)) + return CheckError(connection, error); + + if (!mpd_search_commit(connection)) + return CheckError(connection, error); + + bool result = true; + + struct mpd_pair *pair; + while (result && + (pair = mpd_recv_pair_tag(connection, tag_type2)) != nullptr) { + result = visit_string(pair->value, error); + mpd_return_pair(connection, pair); + } + + return mpd_response_finish(connection) && + CheckError(connection, error) && + result; +} + +bool +ProxyDatabase::GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, Error &error) const +{ + // TODO: match + (void)selection; + + // TODO: eliminate the const_cast + if (!const_cast(this)->EnsureConnected(error)) + return nullptr; + + struct mpd_stats *stats2 = + mpd_run_stats(connection); + if (stats2 == nullptr) + return CheckError(connection, error); + + update_stamp = (time_t)mpd_stats_get_db_update_time(stats2); + + stats.song_count = mpd_stats_get_number_of_songs(stats2); + stats.total_duration = mpd_stats_get_db_play_time(stats2); + stats.artist_count = mpd_stats_get_number_of_artists(stats2); + stats.album_count = mpd_stats_get_number_of_albums(stats2); + mpd_stats_free(stats2); + + return true; +} + +const DatabasePlugin proxy_db_plugin = { + "proxy", + ProxyDatabase::Create, +}; diff --git a/src/db/plugins/ProxyDatabasePlugin.hxx b/src/db/plugins/ProxyDatabasePlugin.hxx new file mode 100644 index 000000000..699d374b5 --- /dev/null +++ b/src/db/plugins/ProxyDatabasePlugin.hxx @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_PROXY_DATABASE_PLUGIN_HXX +#define MPD_PROXY_DATABASE_PLUGIN_HXX + +struct DatabasePlugin; + +extern const DatabasePlugin proxy_db_plugin; + +#endif diff --git a/src/db/plugins/SimpleDatabasePlugin.cxx b/src/db/plugins/SimpleDatabasePlugin.cxx new file mode 100644 index 000000000..55e08b6d7 --- /dev/null +++ b/src/db/plugins/SimpleDatabasePlugin.cxx @@ -0,0 +1,336 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "SimpleDatabasePlugin.hxx" +#include "db/Selection.hxx" +#include "db/Helpers.hxx" +#include "db/LightDirectory.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "SongFilter.hxx" +#include "db/DatabaseSave.hxx" +#include "db/DatabaseLock.hxx" +#include "db/DatabaseError.hxx" +#include "fs/TextFile.hxx" +#include "config/ConfigData.hxx" +#include "fs/FileSystem.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" +#include "Log.hxx" + +#include + +static constexpr Domain simple_db_domain("simple_db"); + +Database * +SimpleDatabase::Create(gcc_unused EventLoop &loop, + gcc_unused DatabaseListener &listener, + const config_param ¶m, Error &error) +{ + SimpleDatabase *db = new SimpleDatabase(); + if (!db->Configure(param, error)) { + delete db; + db = nullptr; + } + + return db; +} + +bool +SimpleDatabase::Configure(const config_param ¶m, Error &error) +{ + path = param.GetBlockPath("path", error); + if (path.IsNull()) { + if (!error.IsDefined()) + error.Set(simple_db_domain, + "No \"path\" parameter specified"); + return false; + } + + path_utf8 = path.ToUTF8(); + + return true; +} + +bool +SimpleDatabase::Check(Error &error) const +{ + assert(!path.IsNull()); + + /* Check if the file exists */ + if (!CheckAccess(path)) { + /* If the file doesn't exist, we can't check if we can write + * it, so we are going to try to get the directory path, and + * see if we can write a file in that */ + const auto dirPath = path.GetDirectoryName(); + + /* Check that the parent part of the path is a directory */ + struct stat st; + if (!StatFile(dirPath, st)) { + error.FormatErrno("Couldn't stat parent directory of db file " + "\"%s\"", + path_utf8.c_str()); + return false; + } + + if (!S_ISDIR(st.st_mode)) { + error.Format(simple_db_domain, + "Couldn't create db file \"%s\" because the " + "parent path is not a directory", + path_utf8.c_str()); + return false; + } + +#ifndef WIN32 + /* Check if we can write to the directory */ + if (!CheckAccess(dirPath, X_OK | W_OK)) { + const int e = errno; + const std::string dirPath_utf8 = dirPath.ToUTF8(); + error.FormatErrno(e, "Can't create db file in \"%s\"", + dirPath_utf8.c_str()); + return false; + } +#endif + return true; + } + + /* Path exists, now check if it's a regular file */ + struct stat st; + if (!StatFile(path, st)) { + error.FormatErrno("Couldn't stat db file \"%s\"", + path_utf8.c_str()); + return false; + } + + if (!S_ISREG(st.st_mode)) { + error.Format(simple_db_domain, + "db file \"%s\" is not a regular file", + path_utf8.c_str()); + return false; + } + +#ifndef WIN32 + /* And check that we can write to it */ + if (!CheckAccess(path, R_OK | W_OK)) { + error.FormatErrno("Can't open db file \"%s\" for reading/writing", + path_utf8.c_str()); + return false; + } +#endif + + return true; +} + +bool +SimpleDatabase::Load(Error &error) +{ + assert(!path.IsNull()); + assert(root != nullptr); + + TextFile file(path); + if (file.HasFailed()) { + error.FormatErrno("Failed to open database file \"%s\"", + path_utf8.c_str()); + return false; + } + + if (!db_load_internal(file, *root, error)) + return false; + + struct stat st; + if (StatFile(path, st)) + mtime = st.st_mtime; + + return true; +} + +bool +SimpleDatabase::Open(Error &error) +{ + root = Directory::NewRoot(); + mtime = 0; + +#ifndef NDEBUG + borrowed_song_count = 0; +#endif + + if (!Load(error)) { + delete root; + + LogError(error); + error.Clear(); + + if (!Check(error)) + return false; + + root = Directory::NewRoot(); + } + + return true; +} + +void +SimpleDatabase::Close() +{ + assert(root != nullptr); + assert(borrowed_song_count == 0); + + delete root; +} + +const LightSong * +SimpleDatabase::GetSong(const char *uri, Error &error) const +{ + assert(root != nullptr); + assert(borrowed_song_count == 0); + + db_lock(); + const Song *song = root->LookupSong(uri); + db_unlock(); + if (song == nullptr) { + error.Format(db_domain, DB_NOT_FOUND, + "No such song: %s", uri); + return nullptr; + } + + light_song = song->Export(); + +#ifndef NDEBUG + ++borrowed_song_count; +#endif + + return &light_song; +} + +void +SimpleDatabase::ReturnSong(gcc_unused const LightSong *song) const +{ + assert(song == &light_song); + +#ifndef NDEBUG + assert(borrowed_song_count > 0); + --borrowed_song_count; +#endif +} + +gcc_pure +const Directory * +SimpleDatabase::LookupDirectory(const char *uri) const +{ + assert(root != nullptr); + assert(uri != nullptr); + + ScopeDatabaseLock protect; + return root->LookupDirectory(uri); +} + +bool +SimpleDatabase::Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const +{ + ScopeDatabaseLock protect; + + const Directory *directory = root->LookupDirectory(selection.uri.c_str()); + if (directory == nullptr) { + if (visit_song) { + Song *song = root->LookupSong(selection.uri.c_str()); + if (song != nullptr) { + const LightSong song2 = song->Export(); + return !selection.Match(song2) || + visit_song(song2, error); + } + } + + error.Set(db_domain, DB_NOT_FOUND, "No such directory"); + return false; + } + + if (selection.recursive && visit_directory && + !visit_directory(directory->Export(), error)) + return false; + + return directory->Walk(selection.recursive, selection.filter, + visit_directory, visit_song, visit_playlist, + error); +} + +bool +SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const +{ + return ::VisitUniqueTags(*this, selection, tag_type, visit_string, + error); +} + +bool +SimpleDatabase::GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, Error &error) const +{ + return ::GetStats(*this, selection, stats, error); +} + +bool +SimpleDatabase::Save(Error &error) +{ + db_lock(); + + LogDebug(simple_db_domain, "removing empty directories from DB"); + root->PruneEmpty(); + + LogDebug(simple_db_domain, "sorting DB"); + root->Sort(); + + db_unlock(); + + LogDebug(simple_db_domain, "writing DB"); + + FILE *fp = FOpen(path, FOpenMode::WriteText); + if (!fp) { + error.FormatErrno("unable to write to db file \"%s\"", + path_utf8.c_str()); + return false; + } + + db_save_internal(fp, *root); + + if (ferror(fp)) { + error.SetErrno("Failed to write to database file"); + fclose(fp); + return false; + } + + fclose(fp); + + struct stat st; + if (StatFile(path, st)) + mtime = st.st_mtime; + + return true; +} + +const DatabasePlugin simple_db_plugin = { + "simple", + SimpleDatabase::Create, +}; diff --git a/src/db/plugins/SimpleDatabasePlugin.hxx b/src/db/plugins/SimpleDatabasePlugin.hxx new file mode 100644 index 000000000..137a60884 --- /dev/null +++ b/src/db/plugins/SimpleDatabasePlugin.hxx @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_SIMPLE_DATABASE_PLUGIN_HXX +#define MPD_SIMPLE_DATABASE_PLUGIN_HXX + +#include "db/DatabasePlugin.hxx" +#include "fs/AllocatedPath.hxx" +#include "db/LightSong.hxx" +#include "Compiler.h" + +#include + +struct Directory; + +class SimpleDatabase : public Database { + AllocatedPath path; + std::string path_utf8; + + Directory *root; + + time_t mtime; + + /** + * A buffer for GetSong(). + */ + mutable LightSong light_song; + +#ifndef NDEBUG + mutable unsigned borrowed_song_count; +#endif + + SimpleDatabase() + :path(AllocatedPath::Null()) {} + +public: + gcc_pure + Directory *GetRoot() { + assert(root != NULL); + + return root; + } + + bool Save(Error &error); + + static Database *Create(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, + Error &error); + + virtual bool Open(Error &error) override; + virtual void Close() override; + + virtual const LightSong *GetSong(const char *uri_utf8, + Error &error) const override; + virtual void ReturnSong(const LightSong *song) const; + + virtual bool Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const override; + + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const override; + + virtual bool GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, + Error &error) const override; + + virtual time_t GetUpdateStamp() const override { + return mtime; + } + +protected: + bool Configure(const config_param ¶m, Error &error); + + gcc_pure + bool Check(Error &error) const; + + bool Load(Error &error); + + gcc_pure + const Directory *LookupDirectory(const char *uri) const; +}; + +extern const DatabasePlugin simple_db_plugin; + +#endif diff --git a/src/db/plugins/UpnpDatabasePlugin.cxx b/src/db/plugins/UpnpDatabasePlugin.cxx new file mode 100644 index 000000000..10575fc94 --- /dev/null +++ b/src/db/plugins/UpnpDatabasePlugin.cxx @@ -0,0 +1,785 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "UpnpDatabasePlugin.hxx" +#include "upnp/Domain.hxx" +#include "upnp/upnpplib.hxx" +#include "upnp/Discovery.hxx" +#include "upnp/ContentDirectoryService.hxx" +#include "upnp/Directory.hxx" +#include "upnp/Tags.hxx" +#include "upnp/Util.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/Selection.hxx" +#include "db/DatabaseError.hxx" +#include "db/LightDirectory.hxx" +#include "db/LightSong.hxx" +#include "config/ConfigData.hxx" +#include "tag/TagBuilder.hxx" +#include "tag/TagTable.hxx" +#include "util/Error.hxx" +#include "util/Domain.hxx" +#include "fs/Traits.hxx" +#include "Log.hxx" +#include "SongFilter.hxx" + +#include +#include +#include + +#include +#include + +static const char *const rootid = "0"; + +class UpnpSong : public LightSong { + std::string uri2, real_uri2; + + Tag tag2; + +public: + UpnpSong(UPnPDirObject &&object, std::string &&_uri) + :uri2(std::move(_uri)), + real_uri2(std::move(object.url)), + tag2(std::move(object.tag)) { + directory = nullptr; + uri = uri2.c_str(); + real_uri = real_uri2.c_str(); + tag = &tag2; + mtime = 0; + start_ms = end_ms = 0; + } +}; + +class UpnpDatabase : public Database { + LibUPnP *m_lib; + UPnPDeviceDirectory *m_superdir; + +public: + static Database *Create(EventLoop &loop, DatabaseListener &listener, + const config_param ¶m, + Error &error); + + virtual bool Open(Error &error) override; + virtual void Close() override; + virtual const LightSong *GetSong(const char *uri_utf8, + Error &error) const override; + virtual void ReturnSong(const LightSong *song) const; + + virtual bool Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const override; + + virtual bool VisitUniqueTags(const DatabaseSelection &selection, + TagType tag_type, + VisitString visit_string, + Error &error) const override; + + virtual bool GetStats(const DatabaseSelection &selection, + DatabaseStats &stats, + Error &error) const override; + virtual time_t GetUpdateStamp() const {return 0;} + +protected: + bool Configure(const config_param ¶m, Error &error); + +private: + bool VisitServer(const ContentDirectoryService &server, + const std::list &vpath, + const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const; + + /** + * Run an UPnP search according to MPD parameters, and + * visit_song the results. + */ + bool SearchSongs(const ContentDirectoryService &server, + const char *objid, + const DatabaseSelection &selection, + VisitSong visit_song, + Error &error) const; + + bool SearchSongs(const ContentDirectoryService &server, + const char *objid, + const DatabaseSelection &selection, + UPnPDirContent& dirbuf, + Error &error) const; + + bool Namei(const ContentDirectoryService &server, + const std::list &vpath, + UPnPDirObject &dirent, + Error &error) const; + + /** + * Take server and objid, return metadata. + */ + bool ReadNode(const ContentDirectoryService &server, + const char *objid, UPnPDirObject& dirent, + Error &error) const; + + /** + * Get the path for an object Id. This works much like pwd, + * except easier cause our inodes have a parent id. Not used + * any more actually (see comments in SearchSongs). + */ + bool BuildPath(const ContentDirectoryService &server, + const UPnPDirObject& dirent, std::string &idpath, + Error &error) const; +}; + +Database * +UpnpDatabase::Create(gcc_unused EventLoop &loop, + gcc_unused DatabaseListener &listener, + const config_param ¶m, Error &error) +{ + UpnpDatabase *db = new UpnpDatabase(); + if (!db->Configure(param, error)) { + delete db; + return nullptr; + } + + /* libupnp loses its ability to receive multicast messages + apparently due to daemonization; using the LazyDatabase + wrapper works around this problem */ + return db; +} + +inline bool +UpnpDatabase::Configure(const config_param &, Error &) +{ + return true; +} + +bool +UpnpDatabase::Open(Error &error) +{ + m_lib = new LibUPnP(); + if (!m_lib->ok()) { + error.Set(m_lib->GetInitError()); + delete m_lib; + return false; + } + + m_superdir = new UPnPDeviceDirectory(m_lib); + if (!m_superdir->Start(error)) { + delete m_superdir; + delete m_lib; + return false; + } + + // Wait for device answers. This should be consistent with the value set + // in the lib (currently 2) + sleep(2); + return true; +} + +void +UpnpDatabase::Close() +{ + delete m_superdir; + delete m_lib; +} + +void +UpnpDatabase::ReturnSong(const LightSong *_song) const +{ + assert(_song != nullptr); + + UpnpSong *song = (UpnpSong *)const_cast(_song); + delete song; +} + +// Get song info by path. We can receive either the id path, or the titles +// one +const LightSong * +UpnpDatabase::GetSong(const char *uri, Error &error) const +{ + auto vpath = stringToTokens(uri, "/", true); + if (vpath.size() < 2) { + error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri); + return nullptr; + } + + ContentDirectoryService server; + if (!m_superdir->getServer(vpath.front().c_str(), server, error)) + return nullptr; + + vpath.pop_front(); + + UPnPDirObject dirent; + if (vpath.front() != rootid) { + if (!Namei(server, vpath, dirent, error)) + return nullptr; + } else { + if (!ReadNode(server, vpath.back().c_str(), dirent, + error)) + return nullptr; + } + + return new UpnpSong(std::move(dirent), uri); +} + +/** + * Double-quote a string, adding internal backslash escaping. + */ +static void +dquote(std::string &out, const char *in) +{ + out.push_back('"'); + + for (; *in != 0; ++in) { + switch(*in) { + case '\\': + case '"': + out.push_back('\\'); + break; + } + + out.push_back(*in); + } + + out.push_back('"'); +} + +// Run an UPnP search, according to MPD parameters. Return results as +// UPnP items +bool +UpnpDatabase::SearchSongs(const ContentDirectoryService &server, + const char *objid, + const DatabaseSelection &selection, + UPnPDirContent &dirbuf, + Error &error) const +{ + const SongFilter *filter = selection.filter; + if (selection.filter == nullptr) + return true; + + std::list searchcaps; + if (!server.getSearchCapabilities(m_lib->getclh(), searchcaps, error)) + return false; + + if (searchcaps.empty()) + return true; + + std::string cond; + for (const auto &item : filter->GetItems()) { + switch (auto tag = item.GetTag()) { + case LOCATE_TAG_ANY_TYPE: + { + if (!cond.empty()) { + cond += " and "; + } + cond += '('; + bool first(true); + for (const auto& cap : searchcaps) { + if (first) + first = false; + else + cond += " or "; + cond += cap; + if (item.GetFoldCase()) { + cond += " contains "; + } else { + cond += " = "; + } + dquote(cond, item.GetValue().c_str()); + } + cond += ')'; + } + break; + + default: + /* Unhandled conditions like + LOCATE_TAG_BASE_TYPE or + LOCATE_TAG_FILE_TYPE won't have a + corresponding upnp prop, so they will be + skipped */ + if (tag == TAG_ALBUM_ARTIST) + tag = TAG_ARTIST; + + // TODO: support LOCATE_TAG_ANY_TYPE etc. + const char *name = tag_table_lookup(upnp_tags, + TagType(tag)); + if (name == nullptr) + continue; + + if (!cond.empty()) { + cond += " and "; + } + cond += name; + + /* FoldCase doubles up as contains/equal + switch. UpNP search is supposed to be + case-insensitive, but at least some servers + have the same convention as mpd (e.g.: + minidlna) */ + if (item.GetFoldCase()) { + cond += " contains "; + } else { + cond += " = "; + } + dquote(cond, item.GetValue().c_str()); + } + } + + return server.search(m_lib->getclh(), + objid, cond.c_str(), dirbuf, + error); +} + +static bool +visitSong(const UPnPDirObject &meta, const char *path, + const DatabaseSelection &selection, + VisitSong visit_song, Error& error) +{ + if (!visit_song) + return true; + + LightSong song; + song.directory = nullptr; + song.uri = path; + song.real_uri = meta.url.c_str(); + song.tag = &meta.tag; + song.mtime = 0; + song.start_ms = song.end_ms = 0; + + return !selection.Match(song) || visit_song(song, error); +} + +/** + * Build synthetic path based on object id for search results. The use + * of "rootid" is arbitrary, any name that is not likely to be a top + * directory name would fit. + */ +static std::string +songPath(const std::string &servername, + const std::string &objid) +{ + return servername + "/" + rootid + "/" + objid; +} + +bool +UpnpDatabase::SearchSongs(const ContentDirectoryService &server, + const char *objid, + const DatabaseSelection &selection, + VisitSong visit_song, + Error &error) const +{ + UPnPDirContent dirbuf; + if (!visit_song) + return true; + if (!SearchSongs(server, objid, selection, dirbuf, error)) + return false; + + for (auto &dirent : dirbuf.objects) { + if (dirent.type != UPnPDirObject::Type::ITEM || + dirent.item_class != UPnPDirObject::ItemClass::MUSIC) + continue; + + // We get song ids as the result of the UPnP search. But our + // client expects paths (e.g. we get 1$4$3788 from minidlna, + // but we need to translate to /Music/All_Music/Satisfaction). + // We can do this in two ways: + // - Rebuild a normal path using BuildPath() which is a kind of pwd + // - Build a bogus path based on the song id. + // The first method is nice because the returned paths are pretty, but + // it has two big problems: + // - The song paths are ambiguous: e.g. minidlna returns all search + // results as being from the "All Music" directory, which can + // contain several songs with the same title (but different objids) + // - The performance of BuildPath() is atrocious on very big + // directories, even causing timeouts in clients. And of + // course, 'All Music' is very big. + // So we return synthetic and ugly paths based on the object id, + // which we later have to detect. + const std::string path = songPath(server.getFriendlyName(), + dirent.m_id); + if (!visitSong(std::move(dirent), path.c_str(), + selection, visit_song, + error)) + return false; + } + + return true; +} + +bool +UpnpDatabase::ReadNode(const ContentDirectoryService &server, + const char *objid, UPnPDirObject &dirent, + Error &error) const +{ + UPnPDirContent dirbuf; + if (!server.getMetadata(m_lib->getclh(), objid, dirbuf, error)) + return false; + + if (dirbuf.objects.size() == 1) { + dirent = std::move(dirbuf.objects.front()); + } else { + error.Format(upnp_domain, "Bad resource"); + return false; + } + + return true; +} + +bool +UpnpDatabase::BuildPath(const ContentDirectoryService &server, + const UPnPDirObject& idirent, + std::string &path, + Error &error) const +{ + const char *pid = idirent.m_id.c_str(); + path.clear(); + UPnPDirObject dirent; + while (strcmp(pid, rootid) != 0) { + if (!ReadNode(server, pid, dirent, error)) + return false; + pid = dirent.m_pid.c_str(); + + if (path.empty()) + path = dirent.name; + else + path = PathTraitsUTF8::Build(dirent.name.c_str(), + path.c_str()); + } + + path = PathTraitsUTF8::Build(server.getFriendlyName(), + path.c_str()); + return true; +} + +// Take server and internal title pathname and return objid and metadata. +bool +UpnpDatabase::Namei(const ContentDirectoryService &server, + const std::list &vpath, + UPnPDirObject &odirent, + Error &error) const +{ + if (vpath.empty()) { + // looking for root info + if (!ReadNode(server, rootid, odirent, error)) + return false; + + return true; + } + + const UpnpClient_Handle handle = m_lib->getclh(); + + std::string objid(rootid); + + // Walk the path elements, read each directory and try to find the next one + for (auto i = vpath.begin(), last = std::prev(vpath.end());; ++i) { + UPnPDirContent dirbuf; + if (!server.readDir(handle, objid.c_str(), dirbuf, error)) + return false; + + // Look for the name in the sub-container list + UPnPDirObject *child = dirbuf.FindObject(i->c_str()); + if (child == nullptr) { + error.Format(db_domain, DB_NOT_FOUND, + "No such object"); + return false; + } + + if (i == last) { + odirent = std::move(*child); + return true; + } + + if (child->type != UPnPDirObject::Type::CONTAINER) { + error.Format(db_domain, DB_NOT_FOUND, + "Not a container"); + return false; + } + + objid = std::move(child->m_id); + } +} + +static bool +VisitItem(const UPnPDirObject &object, const char *uri, + const DatabaseSelection &selection, + VisitSong visit_song, VisitPlaylist visit_playlist, + Error &error) +{ + assert(object.type == UPnPDirObject::Type::ITEM); + + switch (object.item_class) { + case UPnPDirObject::ItemClass::MUSIC: + return !visit_song || + visitSong(object, uri, + selection, visit_song, error); + + case UPnPDirObject::ItemClass::PLAYLIST: + if (visit_playlist) { + /* Note: I've yet to see a + playlist item (playlists + seem to be usually handled + as containers, so I'll + decide what to do when I + see one... */ + } + + return true; + + case UPnPDirObject::ItemClass::UNKNOWN: + return true; + } + + assert(false); + gcc_unreachable(); +} + +static bool +VisitObject(const UPnPDirObject &object, const char *uri, + const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) +{ + switch (object.type) { + case UPnPDirObject::Type::UNKNOWN: + assert(false); + gcc_unreachable(); + + case UPnPDirObject::Type::CONTAINER: + return !visit_directory || + visit_directory(LightDirectory(uri, 0), error); + + case UPnPDirObject::Type::ITEM: + return VisitItem(object, uri, selection, + visit_song, visit_playlist, + error); + } + + assert(false); + gcc_unreachable(); +} + +// vpath is a parsed and writeable version of selection.uri. There is +// really just one path parameter. +bool +UpnpDatabase::VisitServer(const ContentDirectoryService &server, + const std::list &vpath, + const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const +{ + /* If the path begins with rootid, we know that this is a + song, not a directory (because that's how we set things + up). Just visit it. Note that the choice of rootid is + arbitrary, any value not likely to be the name of a top + directory would be ok. */ + /* !Note: this *can't* be handled by Namei further down, + because the path is not valid for traversal. Besides, it's + just faster to access the target node directly */ + if (!vpath.empty() && vpath.front() == rootid) { + switch (vpath.size()) { + case 1: + return true; + + case 2: + break; + + default: + error.Format(db_domain, DB_NOT_FOUND, + "Not found"); + return false; + } + + if (visit_song) { + UPnPDirObject dirent; + if (!ReadNode(server, vpath.back().c_str(), dirent, + error)) + return false; + + if (dirent.type != UPnPDirObject::Type::ITEM || + dirent.item_class != UPnPDirObject::ItemClass::MUSIC) { + error.Format(db_domain, DB_NOT_FOUND, + "Not found"); + return false; + } + + std::string path = songPath(server.getFriendlyName(), + dirent.m_id); + if (!visitSong(std::move(dirent), path.c_str(), + selection, + visit_song, error)) + return false; + } + return true; + } + + // Translate the target path into an object id and the associated metadata. + UPnPDirObject tdirent; + if (!Namei(server, vpath, tdirent, error)) + return false; + + /* If recursive is set, this is a search... No use sending it + if the filter is empty. In this case, we implement limited + recursion (1-deep) here, which will handle the "add dir" + case. */ + if (selection.recursive && selection.filter) + return SearchSongs(server, tdirent.m_id.c_str(), selection, + visit_song, error); + + const char *const base_uri = selection.uri.empty() + ? server.getFriendlyName() + : selection.uri.c_str(); + + if (tdirent.type == UPnPDirObject::Type::ITEM) { + return VisitItem(tdirent, base_uri, + selection, + visit_song, visit_playlist, + error); + } + + /* Target was a a container. Visit it. We could read slices + and loop here, but it's not useful as mpd will only return + data to the client when we're done anyway. */ + UPnPDirContent dirbuf; + if (!server.readDir(m_lib->getclh(), tdirent.m_id.c_str(), dirbuf, + error)) + return false; + + for (auto &dirent : dirbuf.objects) { + const std::string uri = PathTraitsUTF8::Build(base_uri, + dirent.name.c_str()); + if (!VisitObject(dirent, uri.c_str(), + selection, + visit_directory, + visit_song, visit_playlist, + error)) + return false; + } + + return true; +} + +// Deal with the possibly multiple servers, call VisitServer if needed. +bool +UpnpDatabase::Visit(const DatabaseSelection &selection, + VisitDirectory visit_directory, + VisitSong visit_song, + VisitPlaylist visit_playlist, + Error &error) const +{ + auto vpath = stringToTokens(selection.uri, "/", true); + if (vpath.empty()) { + std::vector servers; + if (!m_superdir->getDirServices(servers, error)) + return false; + + for (const auto &server : servers) { + if (visit_directory) { + const LightDirectory d(server.getFriendlyName(), 0); + if (!visit_directory(d, error)) + return false; + } + + if (selection.recursive && + !VisitServer(server, vpath, selection, + visit_directory, visit_song, visit_playlist, + error)) + return false; + } + + return true; + } + + // We do have a path: the first element selects the server + std::string servername(std::move(vpath.front())); + vpath.pop_front(); + + ContentDirectoryService server; + if (!m_superdir->getServer(servername.c_str(), server, error)) + return false; + + return VisitServer(server, vpath, selection, + visit_directory, visit_song, visit_playlist, error); +} + +bool +UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection, + TagType tag, + VisitString visit_string, + Error &error) const +{ + if (!visit_string) + return true; + + std::vector servers; + if (!m_superdir->getDirServices(servers, error)) + return false; + + std::set values; + for (auto& server : servers) { + UPnPDirContent dirbuf; + if (!SearchSongs(server, rootid, selection, dirbuf, error)) + return false; + + for (const auto &dirent : dirbuf.objects) { + if (dirent.type != UPnPDirObject::Type::ITEM || + dirent.item_class != UPnPDirObject::ItemClass::MUSIC) + continue; + + const char *value = dirent.tag.GetValue(tag); + if (value != nullptr) { +#if defined(__clang__) || GCC_CHECK_VERSION(4,8) + values.emplace(value); +#else + values.insert(value); +#endif + } + } + } + + for (const auto& value : values) + if (!visit_string(value.c_str(), error)) + return false; + + return true; +} + +bool +UpnpDatabase::GetStats(const DatabaseSelection &, + DatabaseStats &stats, Error &) const +{ + /* Note: this gets called before the daemonizing so we can't + reallyopen this would be a problem if we had real stats */ + stats.song_count = 0; + stats.total_duration = 0; + stats.artist_count = 0; + stats.album_count = 0; + return true; +} + +const DatabasePlugin upnp_db_plugin = { + "upnp", + UpnpDatabase::Create, +}; diff --git a/src/db/plugins/UpnpDatabasePlugin.hxx b/src/db/plugins/UpnpDatabasePlugin.hxx new file mode 100644 index 000000000..0228405cd --- /dev/null +++ b/src/db/plugins/UpnpDatabasePlugin.hxx @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_DATABASE_PLUGIN_HXX +#define MPD_UPNP_DATABASE_PLUGIN_HXX + +struct DatabasePlugin; + +extern const DatabasePlugin upnp_db_plugin; + +#endif diff --git a/src/db/plugins/upnp/Action.hxx b/src/db/plugins/upnp/Action.hxx new file mode 100644 index 000000000..28c88be92 --- /dev/null +++ b/src/db/plugins/upnp/Action.hxx @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_ACTION_HXX +#define MPD_UPNP_ACTION_HXX + +#include "Compiler.h" + +#include + +static inline constexpr unsigned +CountNameValuePairs() +{ + return 0; +} + +template +static inline constexpr unsigned +CountNameValuePairs(gcc_unused const char *name, gcc_unused const char *value, + Args... args) +{ + return 1 + CountNameValuePairs(args...); +} + +/** + * A wrapper for UpnpMakeAction() that counts the number of name/value + * pairs and adds the nullptr sentinel. + */ +template +static inline IXML_Document * +MakeActionHelper(const char *action_name, const char *service_type, + Args... args) +{ + const unsigned n = CountNameValuePairs(args...); + return UpnpMakeAction(action_name, service_type, n, + args..., + nullptr, nullptr); +} + +#endif diff --git a/src/db/plugins/upnp/ContentDirectoryService.cxx b/src/db/plugins/upnp/ContentDirectoryService.cxx new file mode 100644 index 000000000..35445e09d --- /dev/null +++ b/src/db/plugins/upnp/ContentDirectoryService.cxx @@ -0,0 +1,273 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "ContentDirectoryService.hxx" +#include "Domain.hxx" +#include "Device.hxx" +#include "ixmlwrap.hxx" +#include "Directory.hxx" +#include "Util.hxx" +#include "Action.hxx" +#include "util/NumberParser.hxx" +#include "util/Error.hxx" + +#include +#include + +ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device, + const UPnPService &service) + :m_actionURL(caturl(device.URLBase, service.controlURL)), + m_serviceType(service.serviceType), + m_deviceId(device.UDN), + m_friendlyName(device.friendlyName), + m_manufacturer(device.manufacturer), + m_modelName(device.modelName), + m_rdreqcnt(200) +{ + if (!m_modelName.compare("MediaTomb")) { + // Readdir by 200 entries is good for most, but MediaTomb likes + // them really big. Actually 1000 is better but I don't dare + m_rdreqcnt = 500; + } +} + +ContentDirectoryService::~ContentDirectoryService() +{ + /* this destructor exists here just so it won't get inlined */ +} + +static bool +ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response, Error &error) +{ + const char *p = ixmlwrap::getFirstElementValue(response, "Result"); + if (p == nullptr) + p = ""; + + return dirbuf.parse(p, error); +} + +inline bool +ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl, + const char *objectId, unsigned offset, + unsigned count, UPnPDirContent &dirbuf, + unsigned &didreadp, unsigned &totalp, + Error &error) const +{ + // Create request + char ofbuf[100], cntbuf[100]; + sprintf(ofbuf, "%u", offset); + sprintf(cntbuf, "%u", count); + // Some devices require an empty SortCriteria, else bad params + IXML_Document *request = + MakeActionHelper("Browse", m_serviceType.c_str(), + "ObjectID", objectId, + "BrowseFlag", "BrowseDirectChildren", + "Filter", "*", + "SortCriteria", "", + "StartingIndex", ofbuf, + "RequestedCount", cntbuf); + if (request == nullptr) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned"); + didreadp = value != nullptr + ? ParseUnsigned(value) + : 0; + + value = ixmlwrap::getFirstElementValue(response, "TotalMatches"); + if (value != nullptr) + totalp = ParseUnsigned(value); + + bool success = ReadResultTag(dirbuf, response, error); + ixmlDocument_free(response); + return success; +} + +bool +ContentDirectoryService::readDir(UpnpClient_Handle handle, + const char *objectId, + UPnPDirContent &dirbuf, + Error &error) const +{ + unsigned offset = 0, total = -1, count; + + do { + if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf, + count, total, error)) + return false; + + offset += count; + } while (count > 0 && offset < total); + + return true; +} + +bool +ContentDirectoryService::search(UpnpClient_Handle hdl, + const char *objectId, + const char *ss, + UPnPDirContent &dirbuf, + Error &error) const +{ + unsigned offset = 0, total = -1, count; + + do { + char ofbuf[100]; + sprintf(ofbuf, "%d", offset); + + IXML_Document *request = + MakeActionHelper("Search", m_serviceType.c_str(), + "ContainerID", objectId, + "SearchCriteria", ss, + "Filter", "*", + "SortCriteria", "", + "StartingIndex", ofbuf, + "RequestedCount", "0"); // Setting a value here gets twonky into fits + if (request == 0) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + auto code = UpnpSendAction(hdl, m_actionURL.c_str(), + m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + const char *value = + ixmlwrap::getFirstElementValue(response, "NumberReturned"); + count = value != nullptr + ? ParseUnsigned(value) + : 0; + + offset += count; + + value = ixmlwrap::getFirstElementValue(response, "TotalMatches"); + if (value != nullptr) + total = ParseUnsigned(value); + + bool success = ReadResultTag(dirbuf, response, error); + ixmlDocument_free(response); + if (!success) + return false; + } while (count > 0 && offset < total); + + return true; +} + +bool +ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl, + std::list &result, + Error &error) const +{ + assert(result.empty()); + + IXML_Document *request = + UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(), + 0, + nullptr, nullptr); + if (request == 0) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + auto code = UpnpSendAction(hdl, m_actionURL.c_str(), + m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + const char *s = ixmlwrap::getFirstElementValue(response, "SearchCaps"); + if (s == nullptr || *s == 0) { + ixmlDocument_free(response); + return true; + } + + bool success = true; + if (!csvToStrings(s, result)) { + error.Set(upnp_domain, "Bad response"); + success = false; + } + + ixmlDocument_free(response); + return success; +} + +bool +ContentDirectoryService::getMetadata(UpnpClient_Handle hdl, + const char *objectId, + UPnPDirContent &dirbuf, + Error &error) const +{ + // Create request + IXML_Document *request = + MakeActionHelper("Browse", m_serviceType.c_str(), + "ObjectID", objectId, + "BrowseFlag", "BrowseMetadata", + "Filter", "*", + "SortCriteria", "", + "StartingIndex", "0", + "RequestedCount", "1"); + if (request == nullptr) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + auto code = UpnpSendAction(hdl, m_actionURL.c_str(), + m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + bool success = ReadResultTag(dirbuf, response, error); + ixmlDocument_free(response); + return success; +} diff --git a/src/db/plugins/upnp/ContentDirectoryService.hxx b/src/db/plugins/upnp/ContentDirectoryService.hxx new file mode 100644 index 000000000..24be5dfbf --- /dev/null +++ b/src/db/plugins/upnp/ContentDirectoryService.hxx @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _UPNPDIR_HXX_INCLUDED_ +#define _UPNPDIR_HXX_INCLUDED_ + +#include + +#include +#include + +class Error; +class UPnPDevice; +struct UPnPService; +class UPnPDirContent; + +/** + * Content Directory Service class. + * + * This stores identity data from a directory service + * and the device it belongs to, and has methods to query + * the directory, using libupnp for handling the UPnP protocols. + * + * Note: m_rdreqcnt: number of entries requested per directory read. + * 0 means all entries. The device can still return less entries than + * requested, depending on its own limits. In general it's not optimal + * becauses it triggers issues, and is sometimes actually slower, e.g. on + * a D-Link NAS 327 + * + * The value chosen may affect by the UpnpSetMaxContentLength + * (2000*1024) done during initialization, but this should be ample + */ +class ContentDirectoryService { + std::string m_actionURL; + std::string m_serviceType; + std::string m_deviceId; + std::string m_friendlyName; + std::string m_manufacturer; + std::string m_modelName; + + int m_rdreqcnt; // Slice size to use when reading + +public: + /** + * Construct by copying data from device and service objects. + * + * The discovery service does this: use getDirServices() + */ + ContentDirectoryService(const UPnPDevice &device, + const UPnPService &service); + + /** An empty one */ + ContentDirectoryService() = default; + + ~ContentDirectoryService(); + + /** Read a container's children list into dirbuf. + * + * @param objectId the UPnP object Id for the container. Root has Id "0" + * @param[out] dirbuf stores the entries we read. + */ + bool readDir(UpnpClient_Handle handle, + const char *objectId, UPnPDirContent &dirbuf, + Error &error) const; + + bool readDirSlice(UpnpClient_Handle handle, + const char *objectId, unsigned offset, + unsigned count, UPnPDirContent& dirbuf, + unsigned &didread, unsigned &total, + Error &error) const; + + /** Search the content directory service. + * + * @param objectId the UPnP object Id under which the search + * should be done. Not all servers actually support this below + * root. Root has Id "0" + * @param searchstring an UPnP searchcriteria string. Check the + * UPnP document: UPnP-av-ContentDirectory-v1-Service-20020625.pdf + * section 2.5.5. Maybe we'll provide an easier way some day... + * @param[out] dirbuf stores the entries we read. + */ + bool search(UpnpClient_Handle handle, + const char *objectId, const char *searchstring, + UPnPDirContent &dirbuf, + Error &error) const; + + /** Read metadata for a given node. + * + * @param objectId the UPnP object Id. Root has Id "0" + * @param[out] dirbuf stores the entries we read. At most one entry will be + * returned. + */ + bool getMetadata(UpnpClient_Handle handle, + const char *objectId, UPnPDirContent &dirbuf, + Error &error) const; + + /** Retrieve search capabilities + * + * @param[out] result an empty vector: no search, or a single '*' element: + * any tag can be used in a search, or a list of usable tag names. + */ + bool getSearchCapabilities(UpnpClient_Handle handle, + std::list &result, + Error &error) const; + + /** Retrieve the "friendly name" for this server, useful for display. */ + const char *getFriendlyName() const { + return m_friendlyName.c_str(); + } +}; + +#endif /* _UPNPDIR_HXX_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Device.cxx b/src/db/plugins/upnp/Device.cxx new file mode 100644 index 000000000..7bec1cccd --- /dev/null +++ b/src/db/plugins/upnp/Device.cxx @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Device.hxx" +#include "Util.hxx" +#include "Expat.hxx" +#include "util/Error.hxx" + +#include + +#include + +UPnPDevice::~UPnPDevice() +{ + /* this destructor exists here just so it won't get inlined */ +} + +/** + * An XML parser which constructs an UPnP device object from the + * device descriptor. + */ +class UPnPDeviceParser final : public CommonExpatParser { + UPnPDevice &m_device; + + std::string *value; + + UPnPService m_tservice; + +public: + UPnPDeviceParser(UPnPDevice& device) + :m_device(device), + value(nullptr) {} + +protected: + virtual void StartElement(const XML_Char *name, const XML_Char **) { + value = nullptr; + + switch (name[0]) { + case 'c': + if (strcmp(name, "controlURL") == 0) + value = &m_tservice.controlURL; + break; + case 'd': + if (strcmp(name, "deviceType") == 0) + value = &m_device.deviceType; + break; + case 'f': + if (strcmp(name, "friendlyName") == 0) + value = &m_device.friendlyName; + break; + case 'm': + if (strcmp(name, "manufacturer") == 0) + value = &m_device.manufacturer; + else if (strcmp(name, "modelName") == 0) + value = &m_device.modelName; + break; + case 's': + if (strcmp(name, "serviceType") == 0) + value = &m_tservice.serviceType; + break; + case 'U': + if (strcmp(name, "UDN") == 0) + value = &m_device.UDN; + else if (strcmp(name, "URLBase") == 0) + value = &m_device.URLBase; + break; + } + } + + virtual void EndElement(const XML_Char *name) { + if (value != nullptr) { + trimstring(*value); + value = nullptr; + } else if (!strcmp(name, "service")) { + m_device.services.emplace_back(std::move(m_tservice)); + m_tservice.clear(); + } + } + + virtual void CharacterData(const XML_Char *s, int len) { + if (value != nullptr) + value->append(s, len); + } +}; + +bool +UPnPDevice::Parse(const std::string &url, const char *description, + Error &error) +{ + { + UPnPDeviceParser mparser(*this); + if (!mparser.Parse(description, strlen(description), + true, error)) + return false; + } + + if (URLBase.empty()) { + // The standard says that if the URLBase value is empty, we should use + // the url the description was retrieved from. However this is + // sometimes something like http://host/desc.xml, sometimes something + // like http://host/ + + if (url.size() < 8) { + // ??? + URLBase = url; + } else { + auto hostslash = url.find_first_of("/", 7); + if (hostslash == std::string::npos || hostslash == url.size()-1) { + URLBase = url; + } else { + URLBase = path_getfather(url); + } + } + } + + return true; +} diff --git a/src/db/plugins/upnp/Device.hxx b/src/db/plugins/upnp/Device.hxx new file mode 100644 index 000000000..dd7ecac2d --- /dev/null +++ b/src/db/plugins/upnp/Device.hxx @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _UPNPDEV_HXX_INCLUDED_ +#define _UPNPDEV_HXX_INCLUDED_ + +#include +#include + +class Error; + +/** + * UPnP Description phase: interpreting the device description which we + * downloaded from the URL obtained by the discovery phase. + */ + +/** + * Data holder for a UPnP service, parsed from the XML description + * downloaded after discovery yielded its URL. + */ +struct UPnPService { + // e.g. urn:schemas-upnp-org:service:ConnectionManager:1 + std::string serviceType; + std::string controlURL; // e.g.: /upnp/control/cm + + void clear() + { + serviceType.clear(); + controlURL.clear(); + } +}; + +/** + * Data holder for a UPnP device, parsed from the XML description obtained + * during discovery. + * A device may include several services. To be of interest to us, + * one of them must be a ContentDirectory. + */ +class UPnPDevice { +public: + // e.g. urn:schemas-upnp-org:device:MediaServer:1 + std::string deviceType; + // e.g. MediaTomb + std::string friendlyName; + // Unique device number. This should match the deviceID in the + // discovery message. e.g. uuid:a7bdcd12-e6c1-4c7e-b588-3bbc959eda8d + std::string UDN; + // Base for all relative URLs. e.g. http://192.168.4.4:49152/ + std::string URLBase; + // Manufacturer: e.g. D-Link, PacketVideo ("manufacturer") + std::string manufacturer; + // Model name: e.g. MediaTomb, DNS-327L ("modelName") + std::string modelName; + // Services provided by this device. + std::vector services; + + UPnPDevice() = default; + UPnPDevice(const UPnPDevice &) = delete; + UPnPDevice(UPnPDevice &&) = default; + UPnPDevice &operator=(UPnPDevice &&) = default; + + ~UPnPDevice(); + + /** Build device from xml description downloaded from discovery + * @param url where the description came from + * @param description the xml device description + */ + bool Parse(const std::string &url, const char *description, + Error &error); +}; + +#endif /* _UPNPDEV_HXX_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Directory.cxx b/src/db/plugins/upnp/Directory.cxx new file mode 100644 index 000000000..adb8b213a --- /dev/null +++ b/src/db/plugins/upnp/Directory.cxx @@ -0,0 +1,262 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Directory.hxx" +#include "Util.hxx" +#include "Expat.hxx" +#include "Tags.hxx" +#include "tag/TagBuilder.hxx" +#include "tag/TagTable.hxx" +#include "util/NumberParser.hxx" + +#include +#include + +#include + +UPnPDirContent::~UPnPDirContent() +{ + /* this destructor exists here just so it won't get inlined */ +} + +gcc_pure gcc_nonnull_all +static bool +CompareStringLiteral(const char *literal, const char *value, size_t length) +{ + return length == strlen(literal) && + memcmp(literal, value, length) == 0; +} + +gcc_pure +static UPnPDirObject::ItemClass +ParseItemClass(const char *name, size_t length) +{ + if (CompareStringLiteral("object.item.audioItem.musicTrack", + name, length)) + return UPnPDirObject::ItemClass::MUSIC; + else if (CompareStringLiteral("object.item.playlistItem", + name, length)) + return UPnPDirObject::ItemClass::PLAYLIST; + else + return UPnPDirObject::ItemClass::UNKNOWN; +} + +gcc_pure +static int +ParseDuration(const char *duration) +{ + char *endptr; + + unsigned result = ParseUnsigned(duration, &endptr); + if (endptr == duration || *endptr != ':') + return 0; + + result *= 60; + duration = endptr + 1; + result += ParseUnsigned(duration, &endptr); + if (endptr == duration || *endptr != ':') + return 0; + + result *= 60; + duration = endptr + 1; + result += ParseUnsigned(duration, &endptr); + if (endptr == duration || *endptr != 0) + return 0; + + return result; +} + +/** + * Transform titles to turn '/' into '_' to make them acceptable path + * elements. There is a very slight risk of collision in doing + * this. Twonky returns directory names (titles) like 'Artist/Album'. + */ +gcc_pure +static std::string +titleToPathElt(std::string &&s) +{ + std::replace(s.begin(), s.end(), '/', '_'); + return s; +} + +/** + * An XML parser which builds directory contents from DIDL lite input. + */ +class UPnPDirParser final : public CommonExpatParser { + UPnPDirContent &m_dir; + + enum { + NONE, + RES, + CLASS, + } state; + + /** + * If not equal to #TAG_NUM_OF_ITEM_TYPES, then we're + * currently reading an element containing a tag value. The + * value is being constructed in #value. + */ + TagType tag_type; + + /** + * The text inside the current element. + */ + std::string value; + + UPnPDirObject m_tobj; + TagBuilder tag; + +public: + UPnPDirParser(UPnPDirContent& dir) + :m_dir(dir), + state(NONE), + tag_type(TAG_NUM_OF_ITEM_TYPES) + { + } + +protected: + virtual void StartElement(const XML_Char *name, const XML_Char **attrs) + { + if (m_tobj.type != UPnPDirObject::Type::UNKNOWN && + tag_type == TAG_NUM_OF_ITEM_TYPES) { + tag_type = tag_table_lookup(upnp_tags, name); + if (tag_type != TAG_NUM_OF_ITEM_TYPES) + return; + } else { + assert(tag_type == TAG_NUM_OF_ITEM_TYPES); + } + + switch (name[0]) { + case 'c': + if (!strcmp(name, "container")) { + m_tobj.clear(); + m_tobj.type = UPnPDirObject::Type::CONTAINER; + + const char *id = GetAttribute(attrs, "id"); + if (id != nullptr) + m_tobj.m_id = id; + + const char *pid = GetAttribute(attrs, "parentID"); + if (pid != nullptr) + m_tobj.m_pid = pid; + } + break; + + case 'i': + if (!strcmp(name, "item")) { + m_tobj.clear(); + m_tobj.type = UPnPDirObject::Type::ITEM; + + const char *id = GetAttribute(attrs, "id"); + if (id != nullptr) + m_tobj.m_id = id; + + const char *pid = GetAttribute(attrs, "parentID"); + if (pid != nullptr) + m_tobj.m_pid = pid; + } + break; + + case 'r': + if (!strcmp(name, "res")) { + // + + const char *duration = + GetAttribute(attrs, "duration"); + if (duration != nullptr) + tag.SetTime(ParseDuration(duration)); + + state = RES; + } + + break; + + case 'u': + if (strcmp(name, "upnp:class") == 0) + state = CLASS; + } + } + + bool checkobjok() { + if (m_tobj.m_id.empty() || m_tobj.m_pid.empty() || + m_tobj.name.empty() || + (m_tobj.type == UPnPDirObject::Type::ITEM && + m_tobj.item_class == UPnPDirObject::ItemClass::UNKNOWN)) + return false; + + return true; + } + + virtual void EndElement(const XML_Char *name) + { + if (tag_type != TAG_NUM_OF_ITEM_TYPES) { + assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN); + + tag.AddItem(tag_type, value.c_str()); + + if (tag_type == TAG_TITLE) + m_tobj.name = titleToPathElt(std::move(value)); + + value.clear(); + tag_type = TAG_NUM_OF_ITEM_TYPES; + return; + } + + if ((!strcmp(name, "container") || !strcmp(name, "item")) && + checkobjok()) { + tag.Commit(m_tobj.tag); + m_dir.objects.emplace_back(std::move(m_tobj)); + } + + state = NONE; + } + + virtual void CharacterData(const XML_Char *s, int len) + { + if (tag_type != TAG_NUM_OF_ITEM_TYPES) { + assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN); + + value.append(s, len); + return; + } + + switch (state) { + case NONE: + break; + + case RES: + m_tobj.url.assign(s, len); + break; + + case CLASS: + m_tobj.item_class = ParseItemClass(s, len); + break; + } + } +}; + +bool +UPnPDirContent::parse(const char *input, Error &error) +{ + UPnPDirParser parser(*this); + return parser.Parse(input, strlen(input), true, error); +} diff --git a/src/db/plugins/upnp/Directory.hxx b/src/db/plugins/upnp/Directory.hxx new file mode 100644 index 000000000..433979900 --- /dev/null +++ b/src/db/plugins/upnp/Directory.hxx @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_DIRECTORY_HXX +#define MPD_UPNP_DIRECTORY_HXX + +#include "Object.hxx" +#include "Compiler.h" + +#include +#include + +class Error; + +/** + * Image of a MediaServer Directory Service container (directory), + * possibly containing items and subordinate containers. + */ +class UPnPDirContent { +public: + std::vector objects; + + ~UPnPDirContent(); + + gcc_pure + UPnPDirObject *FindObject(const char *name) { + for (auto &o : objects) + if (o.name == name) + return &o; + + return nullptr; + } + + /** + * Parse from DIDL-Lite XML data. + * + * Normally only used by ContentDirectoryService::readDir() + * This is cumulative: in general, the XML data is obtained in + * several documents corresponding to (offset,count) slices of the + * directory (container). parse() can be called repeatedly with + * the successive XML documents and will accumulate entries in the item + * and container vectors. This makes more sense if the different + * chunks are from the same container, but given that UPnP Ids are + * actually global, nothing really bad will happen if you mix + * up... + */ + bool parse(const char *didltext, Error &error); +}; + +#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Discovery.cxx b/src/db/plugins/upnp/Discovery.cxx new file mode 100644 index 000000000..5203dba83 --- /dev/null +++ b/src/db/plugins/upnp/Discovery.cxx @@ -0,0 +1,318 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Discovery.hxx" +#include "Domain.hxx" +#include "ContentDirectoryService.hxx" +#include "upnpplib.hxx" +#include "system/Clock.hxx" +#include "Log.hxx" + +#include + +#include + +// The service type string we are looking for. +static constexpr char ContentDirectorySType[] = "urn:schemas-upnp-org:service:ContentDirectory:1"; + +// We don't include a version in comparisons, as we are satisfied with +// version 1 +gcc_pure +static bool +isCDService(const char *st) +{ + constexpr size_t sz = sizeof(ContentDirectorySType) - 3; + return memcmp(ContentDirectorySType, st, sz) == 0; +} + +// The type of device we're asking for in search +static constexpr char MediaServerDType[] = "urn:schemas-upnp-org:device:MediaServer:1"; + +gcc_pure +static bool +isMSDevice(const char *st) +{ + constexpr size_t sz = sizeof(MediaServerDType) - 3; + return memcmp(MediaServerDType, st, sz) == 0; +} + +inline void +UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d) +{ + const ScopeLock protect(mutex); + + for (auto &i : directories) { + if (i.id == d.id) { + i = std::move(d); + return; + } + } + + directories.emplace_back(std::move(d)); +} + +inline void +UPnPDeviceDirectory::LockRemove(const std::string &id) +{ + const ScopeLock protect(mutex); + + for (auto i = directories.begin(), end = directories.end(); + i != end; ++i) { + if (i->id == id) { + directories.erase(i); + break; + } + } +} + +inline void +UPnPDeviceDirectory::discoExplorer() +{ + for (;;) { + DiscoveredTask *tsk = 0; + if (!discoveredQueue.take(tsk)) { + discoveredQueue.workerExit(); + return; + } + + // Device signals its existence and well-being. Perform the + // UPnP "description" phase by downloading and decoding the + // description document. + char *buf; + // LINE_SIZE is defined by libupnp's upnp.h... + char contentType[LINE_SIZE]; + int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType); + if (code != UPNP_E_SUCCESS) { + continue; + } + + // Update or insert the device + ContentDirectoryDescriptor d(std::move(tsk->deviceId), + MonotonicClockS(), tsk->expires); + + { + Error error2; + bool success = d.Parse(tsk->url, buf, error2); + free(buf); + if (!success) { + delete tsk; + LogError(error2); + continue; + } + } + + LockAdd(std::move(d)); + delete tsk; + } +} + +void * +UPnPDeviceDirectory::discoExplorer(void *ctx) +{ + UPnPDeviceDirectory &directory = *(UPnPDeviceDirectory *)ctx; + directory.discoExplorer(); + return (void*)1; +} + +inline int +UPnPDeviceDirectory::OnAlive(Upnp_Discovery *disco) +{ + if (isMSDevice(disco->DeviceType) || + isCDService(disco->ServiceType)) { + DiscoveredTask *tp = new DiscoveredTask(disco); + if (discoveredQueue.put(tp)) + return UPNP_E_FINISH; + } + + return UPNP_E_SUCCESS; +} + +inline int +UPnPDeviceDirectory::OnByeBye(Upnp_Discovery *disco) +{ + if (isMSDevice(disco->DeviceType) || + isCDService(disco->ServiceType)) { + // Device signals it is going off. + LockRemove(disco->DeviceId); + } + + return UPNP_E_SUCCESS; +} + +// This gets called for all libupnp asynchronous events, in a libupnp +// thread context. +// Example: ContentDirectories appearing and disappearing from the network +// We queue a task for our worker thread(s) +inline int +UPnPDeviceDirectory::cluCallBack(Upnp_EventType et, void *evp) +{ + switch (et) { + case UPNP_DISCOVERY_SEARCH_RESULT: + case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE: + { + Upnp_Discovery *disco = (Upnp_Discovery *)evp; + return OnAlive(disco); + } + + case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE: + { + Upnp_Discovery *disco = (Upnp_Discovery *)evp; + return OnByeBye(disco); + } + + default: + // Ignore other events for now + break; + } + + return UPNP_E_SUCCESS; +} + +bool +UPnPDeviceDirectory::expireDevices(Error &error) +{ + const ScopeLock protect(mutex); + const unsigned now = MonotonicClockS(); + bool didsomething = false; + + for (auto it = directories.begin(); + it != directories.end();) { + if (now > it->expires) { + it = directories.erase(it); + didsomething = true; + } else { + it++; + } + } + + if (didsomething) + return search(error); + + return true; +} + +UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib) + :lib(_lib), + discoveredQueue("DiscoveredQueue"), + m_searchTimeout(2), m_lastSearch(0) +{ +} + +UPnPDeviceDirectory::~UPnPDeviceDirectory() +{ + /* this destructor exists here just so it won't get inlined */ +} + +bool +UPnPDeviceDirectory::Start(Error &error) +{ + if (!discoveredQueue.start(1, discoExplorer, this)) { + error.Set(upnp_domain, "Discover work queue start failed"); + return false; + } + + lib->SetHandler([this](Upnp_EventType type, void *event){ + cluCallBack(type, event); + }); + + return search(error); +} + +bool +UPnPDeviceDirectory::search(Error &error) +{ + const unsigned now = MonotonicClockS(); + if (now - m_lastSearch < 10) + return true; + m_lastSearch = now; + + // We search both for device and service just in case. + int code = UpnpSearchAsync(lib->getclh(), m_searchTimeout, + ContentDirectorySType, lib); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSearchAsync() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + code = UpnpSearchAsync(lib->getclh(), m_searchTimeout, + MediaServerDType, lib); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSearchAsync() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + return true; +} + +bool +UPnPDeviceDirectory::getDirServices(std::vector &out, + Error &error) +{ + // Has locking, do it before our own lock + if (!expireDevices(error)) + return false; + + const ScopeLock protect(mutex); + + for (auto dit = directories.begin(); + dit != directories.end(); dit++) { + for (const auto &service : dit->device.services) { + if (isCDService(service.serviceType.c_str())) { + out.emplace_back(dit->device, service); + } + } + } + + return true; +} + +bool +UPnPDeviceDirectory::getServer(const char *friendlyName, + ContentDirectoryService &server, + Error &error) +{ + // Has locking, do it before our own lock + if (!expireDevices(error)) + return false; + + const ScopeLock protect(mutex); + + for (const auto &i : directories) { + const auto &device = i.device; + + if (device.friendlyName != friendlyName) + continue; + + for (const auto &service : device.services) { + if (isCDService(service.serviceType.c_str())) { + server = ContentDirectoryService(device, + service); + return true; + } + } + } + + error.Set(upnp_domain, "Server not found"); + return false; +} diff --git a/src/db/plugins/upnp/Discovery.hxx b/src/db/plugins/upnp/Discovery.hxx new file mode 100644 index 000000000..4c64fe420 --- /dev/null +++ b/src/db/plugins/upnp/Discovery.hxx @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _UPNPPDISC_H_X_INCLUDED_ +#define _UPNPPDISC_H_X_INCLUDED_ + +#include "Device.hxx" +#include "WorkQueue.hxx" +#include "thread/Mutex.hxx" +#include "util/Error.hxx" + +#include + +#include +#include +#include + +class LibUPnP; +class ContentDirectoryService; + +/** + * Manage UPnP discovery and maintain a directory of active devices. Singleton. + * + * We are only interested in MediaServers with a ContentDirectory service + * for now, but this could be made more general, by removing the filtering. + */ +class UPnPDeviceDirectory { + /** + * Each appropriate discovery event (executing in a libupnp thread + * context) queues the following task object for processing by the + * discovery thread. + */ + struct DiscoveredTask { + std::string url; + std::string deviceId; + unsigned expires; // Seconds valid + + DiscoveredTask(const Upnp_Discovery *disco) + :url(disco->Location), + deviceId(disco->DeviceId), + expires(disco->Expires) {} + }; + + /** + * Descriptor for one device having a Content Directory + * service found on the network. + */ + class ContentDirectoryDescriptor { + public: + std::string id; + + UPnPDevice device; + + /** + * The MonotonicClockS() time stamp when this device + * expires. + */ + unsigned expires; + + ContentDirectoryDescriptor() = default; + + ContentDirectoryDescriptor(std::string &&_id, + unsigned last, int exp) + :id(std::move(_id)), expires(last + exp + 20) {} + + bool Parse(const std::string &url, const char *description, + Error &_error) { + return device.Parse(url, description, _error); + } + }; + + LibUPnP *const lib; + + Mutex mutex; + std::list directories; + WorkQueue discoveredQueue; + + /** + * The UPnP device search timeout, which should actually be + * called delay because it's the base of a random delay that + * the devices apply to avoid responding all at the same time. + */ + int m_searchTimeout; + + /** + * The MonotonicClockS() time stamp of the last search. + */ + unsigned m_lastSearch; + +public: + UPnPDeviceDirectory(LibUPnP *_lib); + ~UPnPDeviceDirectory(); + + UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete; + UPnPDeviceDirectory& operator=(const UPnPDeviceDirectory &) = delete; + + bool Start(Error &error); + + /** Retrieve the directory services currently seen on the network */ + bool getDirServices(std::vector &, Error &); + + /** + * Get server by friendly name. + */ + bool getServer(const char *friendlyName, + ContentDirectoryService &server, + Error &error); + +private: + bool search(Error &error); + + /** + * Look at the devices and get rid of those which have not + * been seen for too long. We do this when listing the top + * directory. + */ + bool expireDevices(Error &error); + + void LockAdd(ContentDirectoryDescriptor &&d); + void LockRemove(const std::string &id); + + /** + * Worker routine for the discovery queue. Get messages about + * devices appearing and disappearing, and update the + * directory pool accordingly. + */ + static void *discoExplorer(void *); + void discoExplorer(); + + int OnAlive(Upnp_Discovery *disco); + int OnByeBye(Upnp_Discovery *disco); + int cluCallBack(Upnp_EventType et, void *evp); +}; + + +#endif /* _UPNPPDISC_H_X_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Domain.cxx b/src/db/plugins/upnp/Domain.cxx new file mode 100644 index 000000000..010d4c7c2 --- /dev/null +++ b/src/db/plugins/upnp/Domain.cxx @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Domain.hxx" +#include "util/Domain.hxx" + +const Domain upnp_domain("upnp"); diff --git a/src/db/plugins/upnp/Domain.hxx b/src/db/plugins/upnp/Domain.hxx new file mode 100644 index 000000000..ec01ef735 --- /dev/null +++ b/src/db/plugins/upnp/Domain.hxx @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_DOMAIN_HXX +#define MPD_UPNP_DOMAIN_HXX + +class Domain; + +extern const Domain upnp_domain; + +#endif diff --git a/src/db/plugins/upnp/Object.cxx b/src/db/plugins/upnp/Object.cxx new file mode 100644 index 000000000..703fb0be4 --- /dev/null +++ b/src/db/plugins/upnp/Object.cxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Object.hxx" + +UPnPDirObject::~UPnPDirObject() +{ + /* this destructor exists here just so it won't get inlined */ +} diff --git a/src/db/plugins/upnp/Object.hxx b/src/db/plugins/upnp/Object.hxx new file mode 100644 index 000000000..16a66c774 --- /dev/null +++ b/src/db/plugins/upnp/Object.hxx @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_OBJECT_HXX +#define MPD_UPNP_OBJECT_HXX + +#include "tag/Tag.hxx" + +#include + +/** + * UpnP Media Server directory entry, converted from XML data. + * + * This is a dumb data holder class, a struct with helpers. + */ +class UPnPDirObject { +public: + enum class Type { + UNKNOWN, + ITEM, + CONTAINER, + }; + + // There are actually several kinds of containers: + // object.container.storageFolder, object.container.person, + // object.container.playlistContainer etc., but they all seem to + // behave the same as far as we're concerned. Otoh, musicTrack + // items are special to us, and so should playlists, but I've not + // seen one of the latter yet (servers seem to use containers for + // playlists). + enum class ItemClass { + UNKNOWN, + MUSIC, + PLAYLIST, + }; + + std::string m_id; // ObjectId + std::string m_pid; // Parent ObjectId + std::string url; + + /** + * A copy of "dc:title" sanitized as a file name. + */ + std::string name; + + Type type; + ItemClass item_class; + + Tag tag; + + UPnPDirObject() = default; + UPnPDirObject(UPnPDirObject &&) = default; + + ~UPnPDirObject(); + + UPnPDirObject &operator=(UPnPDirObject &&) = default; + + void clear() + { + m_id.clear(); + m_pid.clear(); + url.clear(); + type = Type::UNKNOWN; + item_class = ItemClass::UNKNOWN; + tag.Clear(); + } +}; + +#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Tags.cxx b/src/db/plugins/upnp/Tags.cxx new file mode 100644 index 000000000..fd65df4d0 --- /dev/null +++ b/src/db/plugins/upnp/Tags.cxx @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Tags.hxx" +#include "tag/TagTable.hxx" + +const struct tag_table upnp_tags[] = { + { "upnp:artist", TAG_ARTIST }, + { "upnp:album", TAG_ALBUM }, + { "upnp:originalTrackNumber", TAG_TRACK }, + { "upnp:genre", TAG_GENRE }, + { "dc:title", TAG_TITLE }, + + /* sentinel */ + { nullptr, TAG_NUM_OF_ITEM_TYPES } +}; diff --git a/src/db/plugins/upnp/Tags.hxx b/src/db/plugins/upnp/Tags.hxx new file mode 100644 index 000000000..ec6d18478 --- /dev/null +++ b/src/db/plugins/upnp/Tags.hxx @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_TAGS_HXX +#define MPD_UPNP_TAGS_HXX + +/** + * Map UPnP property names to MPD tags. + */ +extern const struct tag_table upnp_tags[]; + +#endif diff --git a/src/db/plugins/upnp/Util.cxx b/src/db/plugins/upnp/Util.cxx new file mode 100644 index 000000000..cf34a47d3 --- /dev/null +++ b/src/db/plugins/upnp/Util.cxx @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Util.hxx" + +#include + +#include + +/** Get rid of white space at both ends */ +void +trimstring(std::string &s, const char *ws) +{ + auto pos = s.find_first_not_of(ws); + if (pos == std::string::npos) { + s.clear(); + return; + } + s.replace(0, pos, std::string()); + + pos = s.find_last_not_of(ws); + if (pos != std::string::npos && pos != s.length()-1) + s.replace(pos + 1, std::string::npos, std::string()); +} + +std::string +caturl(const std::string &s1, const std::string &s2) +{ + if (s2.front() == '/') { + /* absolute path: replace the whole URI path in s1 */ + + auto i = s1.find("://"); + if (i == s1.npos) + /* no scheme: override s1 completely */ + return s2; + + /* find the first slash after the host part */ + i = s1.find('/', i + 3); + if (i == s1.npos) + /* there's no URI path - simply append s2 */ + i = s1.length(); + + return s1.substr(0, i) + s2; + } + + std::string out(s1); + if (out.back() != '/') + out.push_back('/'); + + out += s2; + return out; +} + +static void +path_catslash(std::string &s) +{ + if (s.empty() || s.back() != '/') + s += '/'; +} + +std::string +path_getfather(const std::string &s) +{ + std::string father = s; + + // ?? + if (father.empty()) + return "./"; + + if (father.back() == '/') { + // Input ends with /. Strip it, handle special case for root + if (father.length() == 1) + return father; + father.erase(father.length()-1); + } + + auto slp = father.rfind('/'); + if (slp == std::string::npos) + return "./"; + + father.erase(slp); + path_catslash(father); + return father; +} + +std::list +stringToTokens(const std::string &str, + const char *delims, bool skipinit) +{ + std::list tokens; + + std::string::size_type startPos = 0; + + // Skip initial delims, return empty if this eats all. + if (skipinit && + (startPos = str.find_first_not_of(delims, 0)) == std::string::npos) + return tokens; + + while (startPos < str.size()) { + // Find next delimiter or end of string (end of token) + auto pos = str.find_first_of(delims, startPos); + + // Add token to the vector and adjust start + if (pos == std::string::npos) { + tokens.emplace_back(str, startPos); + break; + } else if (pos == startPos) { + // Dont' push empty tokens after first + if (tokens.empty()) + tokens.emplace_back(); + startPos = ++pos; + } else { + tokens.emplace_back(str, startPos, pos - startPos); + startPos = ++pos; + } + } + + return tokens; +} + +template +bool +csvToStrings(const char *s, T &tokens) +{ + assert(tokens.empty()); + + std::string current; + + while (true) { + char ch = *s++; + if (ch == 0) { + tokens.emplace_back(std::move(current)); + return true; + } + + if (ch == '\\') { + ch = *s++; + if (ch == 0) + return false; + } else if (ch == ',') { + tokens.emplace_back(std::move(current)); + current.clear(); + continue; + } + + current.push_back(ch); + } +} + +template bool csvToStrings>(const char *, std::list &); diff --git a/src/db/plugins/upnp/Util.hxx b/src/db/plugins/upnp/Util.hxx new file mode 100644 index 000000000..58e382faa --- /dev/null +++ b/src/db/plugins/upnp/Util.hxx @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_UTIL_HXX +#define MPD_UPNP_UTIL_HXX + +#include "Compiler.h" + +#include +#include + +std::string +caturl(const std::string& s1, const std::string& s2); + +void +trimstring(std::string &s, const char *ws = " \t\n"); + +std::string +path_getfather(const std::string &s); + +gcc_pure +std::list +stringToTokens(const std::string &str, + const char *delims = "/", bool skipinit = true); + +template +bool +csvToStrings(const char *s, T &tokens); + +#endif /* _UPNPP_H_X_INCLUDED_ */ diff --git a/src/db/plugins/upnp/WorkQueue.hxx b/src/db/plugins/upnp/WorkQueue.hxx new file mode 100644 index 000000000..fe8ce53f9 --- /dev/null +++ b/src/db/plugins/upnp/WorkQueue.hxx @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _WORKQUEUE_H_INCLUDED_ +#define _WORKQUEUE_H_INCLUDED_ + +#include "thread/Mutex.hxx" +#include "thread/Cond.hxx" + +#include +#include + +#include +#include + +#define LOGINFO(X) +#define LOGERR(X) + +/** + * A WorkQueue manages the synchronisation around a queue of work items, + * where a number of client threads queue tasks and a number of worker + * threads take and execute them. The goal is to introduce some level + * of parallelism between the successive steps of a previously single + * threaded pipeline. For example data extraction / data preparation / index + * update, but this could have other uses. + * + * There is no individual task status return. In case of fatal error, + * the client or worker sets an end condition on the queue. A second + * queue could conceivably be used for returning individual task + * status. + */ +template +class WorkQueue { + // Configuration + const std::string name; + + // Status + // Worker threads having called exit + unsigned n_workers_exited; + bool ok; + + unsigned n_threads; + pthread_t *threads; + + // Synchronization + std::queue queue; + Cond client_cond; + Cond worker_cond; + Mutex mutex; + +public: + /** Create a WorkQueue + * @param name for message printing + * @param hi number of tasks on queue before clients blocks. Default 0 + * meaning no limit. hi == -1 means that the queue is disabled. + * @param lo minimum count of tasks before worker starts. Default 1. + */ + WorkQueue(const char *_name) + :name(_name), + n_workers_exited(0), + ok(false), + n_threads(0), threads(nullptr) + { + } + + ~WorkQueue() { + setTerminateAndWait(); + } + + /** Start the worker threads. + * + * @param nworkers number of threads copies to start. + * @param start_routine thread function. It should loop + * taking (QueueWorker::take()) and executing tasks. + * @param arg initial parameter to thread function. + * @return true if ok. + */ + bool start(unsigned nworkers, void *(*workproc)(void *), void *arg) + { + const ScopeLock protect(mutex); + + assert(nworkers > 0); + assert(!ok); + assert(n_threads == 0); + assert(threads == nullptr); + + n_threads = nworkers; + threads = new pthread_t[n_threads]; + + for (unsigned i = 0; i < nworkers; i++) { + int err; + if ((err = pthread_create(&threads[i], 0, workproc, arg))) { + LOGERR(("WorkQueue:%s: pthread_create failed, err %d\n", + name.c_str(), err)); + return false; + } + } + + ok = true; + return true; + } + + /** Add item to work queue, called from client. + * + * Sleeps if there are already too many. + */ + template + bool put(U &&u) + { + const ScopeLock protect(mutex); + + queue.emplace(std::forward(u)); + + // Just wake one worker, there is only one new task. + worker_cond.signal(); + + return true; + } + + + /** Tell the workers to exit, and wait for them. + */ + void setTerminateAndWait() + { + const ScopeLock protect(mutex); + + // Wait for all worker threads to have called workerExit() + ok = false; + while (n_workers_exited < n_threads) { + worker_cond.broadcast(); + client_cond.wait(mutex); + } + + // Perform the thread joins and compute overall status + // Workers return (void*)1 if ok + for (unsigned i = 0; i < n_threads; ++i) { + void *status; + pthread_join(threads[i], &status); + } + + delete[] threads; + threads = nullptr; + n_threads = 0; + + // Reset to start state. + n_workers_exited = 0; + } + + /** Take task from queue. Called from worker. + * + * Sleeps if there are not enough. Signal if we go to sleep on empty + * queue: client may be waiting for our going idle. + */ + bool take(T &tp) + { + const ScopeLock protect(mutex); + + if (!ok) + return false; + + while (queue.empty()) { + worker_cond.wait(mutex); + if (!ok) + return false; + } + + tp = std::move(queue.front()); + queue.pop(); + return true; + } + + /** Advertise exit and abort queue. Called from worker + * + * This would happen after an unrecoverable error, or when + * the queue is terminated by the client. Workers never exit normally, + * except when the queue is shut down (at which point ok is set to + * false by the shutdown code anyway). The thread must return/exit + * immediately after calling this. + */ + void workerExit() + { + const ScopeLock protect(mutex); + + n_workers_exited++; + ok = false; + client_cond.broadcast(); + } +}; + +#endif /* _WORKQUEUE_H_INCLUDED_ */ diff --git a/src/db/plugins/upnp/ixmlwrap.cxx b/src/db/plugins/upnp/ixmlwrap.cxx new file mode 100644 index 000000000..6a2829cf9 --- /dev/null +++ b/src/db/plugins/upnp/ixmlwrap.cxx @@ -0,0 +1,44 @@ +/* Copyright (C) 2013 J.F.Dockes + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "ixmlwrap.hxx" + +namespace ixmlwrap { + +const char * +getFirstElementValue(IXML_Document *doc, const char *name) +{ + const char *ret = nullptr; + IXML_NodeList *nodes = + ixmlDocument_getElementsByTagName(doc, name); + + if (nodes) { + IXML_Node *first = ixmlNodeList_item(nodes, 0); + if (first) { + IXML_Node *dnode = ixmlNode_getFirstChild(first); + if (dnode) { + ret = ixmlNode_getNodeValue(dnode); + } + } + + ixmlNodeList_free(nodes); + } + + return ret; +} + +} diff --git a/src/db/plugins/upnp/ixmlwrap.hxx b/src/db/plugins/upnp/ixmlwrap.hxx new file mode 100644 index 000000000..0d519a323 --- /dev/null +++ b/src/db/plugins/upnp/ixmlwrap.hxx @@ -0,0 +1,35 @@ +/* Copyright (C) 2013 J.F.Dockes + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +#ifndef _IXMLWRAP_H_INCLUDED_ +#define _IXMLWRAP_H_INCLUDED_ + +#include + +#include + +namespace ixmlwrap { + /** + * Retrieve the text content for the first element of given + * name. Returns nullptr if the element does not + * contain a text node + */ + const char *getFirstElementValue(IXML_Document *doc, + const char *name); + +}; + +#endif /* _IXMLWRAP_H_INCLUDED_ */ diff --git a/src/db/plugins/upnp/upnpplib.cxx b/src/db/plugins/upnp/upnpplib.cxx new file mode 100644 index 000000000..27b4e0564 --- /dev/null +++ b/src/db/plugins/upnp/upnpplib.cxx @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "upnpplib.hxx" +#include "Domain.hxx" +#include "Log.hxx" + +#include +#include + +static LibUPnP *theLib; + +LibUPnP::LibUPnP() +{ + auto code = UpnpInit(0, 0); + if (code != UPNP_E_SUCCESS) { + init_error.Format(upnp_domain, code, + "UpnpInit() failed: %s", + UpnpGetErrorMessage(code)); + return; + } + + UpnpSetMaxContentLength(2000*1024); + + code = UpnpRegisterClient(o_callback, (void *)this, &m_clh); + if (code != UPNP_E_SUCCESS) { + init_error.Format(upnp_domain, code, + "UpnpRegisterClient() failed: %s", + UpnpGetErrorMessage(code)); + return; + } + + // Servers sometimes make error (e.g.: minidlna returns bad utf-8) + ixmlRelaxParser(1); +} + +int +LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie) +{ + LibUPnP *ulib = (LibUPnP *)cookie; + if (ulib == nullptr) { + // Because the asyncsearch calls uses a null cookie. + ulib = theLib; + } + + if (ulib->handler) + ulib->handler(et, evp); + + return UPNP_E_SUCCESS; +} + +LibUPnP::~LibUPnP() +{ + int error = UpnpFinish(); + if (error != UPNP_E_SUCCESS) + FormatError(upnp_domain, "UpnpFinish() failed: %s", + UpnpGetErrorMessage(error)); +} diff --git a/src/db/plugins/upnp/upnpplib.hxx b/src/db/plugins/upnp/upnpplib.hxx new file mode 100644 index 000000000..6759aa16d --- /dev/null +++ b/src/db/plugins/upnp/upnpplib.hxx @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _LIBUPNP_H_X_INCLUDED_ +#define _LIBUPNP_H_X_INCLUDED_ + +#include "util/Error.hxx" + +#include + +#include + +/** Our link to libupnp. Initialize and keep the handle around */ +class LibUPnP { + typedef std::function Handler; + + Error init_error; + UpnpClient_Handle m_clh; + + Handler handler; + + static int o_callback(Upnp_EventType, void *, void *); + +public: + LibUPnP(); + + LibUPnP(const LibUPnP &) = delete; + LibUPnP &operator=(const LibUPnP &) = delete; + + ~LibUPnP(); + + /** Check state after initialization */ + bool ok() const + { + return !init_error.IsDefined(); + } + + /** Retrieve init error if state not ok */ + const Error &GetInitError() const { + return init_error; + } + + template + void SetHandler(T &&_handler) { + handler = std::forward(_handler); + } + + UpnpClient_Handle getclh() + { + return m_clh; + } +}; + +#endif /* _LIBUPNP.H_X_INCLUDED_ */ diff --git a/src/db/update/InotifyDomain.cxx b/src/db/update/InotifyDomain.cxx new file mode 100644 index 000000000..4a3ab2d79 --- /dev/null +++ b/src/db/update/InotifyDomain.cxx @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "InotifyDomain.hxx" +#include "util/Domain.hxx" + +const Domain inotify_domain("inotify"); diff --git a/src/db/update/InotifyDomain.hxx b/src/db/update/InotifyDomain.hxx new file mode 100644 index 000000000..ad6202361 --- /dev/null +++ b/src/db/update/InotifyDomain.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_DOMAIN_HXX +#define MPD_INOTIFY_DOMAIN_HXX + +extern const class Domain inotify_domain; + +#endif diff --git a/src/db/update/InotifyQueue.cxx b/src/db/update/InotifyQueue.cxx new file mode 100644 index 000000000..f4bccf7ae --- /dev/null +++ b/src/db/update/InotifyQueue.cxx @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "InotifyQueue.hxx" +#include "InotifyDomain.hxx" +#include "UpdateGlue.hxx" +#include "Log.hxx" + +#include + +/** + * Wait this long after the last change before calling + * update_enqueue(). This increases the probability that updates can + * be bundled. + */ +static constexpr unsigned INOTIFY_UPDATE_DELAY_S = 5; + +void +InotifyQueue::OnTimeout() +{ + unsigned id; + + while (!queue.empty()) { + const char *uri_utf8 = queue.front().c_str(); + + id = update_enqueue(uri_utf8, false); + if (id == 0) { + /* retry later */ + ScheduleSeconds(INOTIFY_UPDATE_DELAY_S); + return; + } + + FormatDebug(inotify_domain, "updating '%s' job=%u", + uri_utf8, id); + + queue.pop_front(); + } +} + +static bool +path_in(const char *path, const char *possible_parent) +{ + size_t length = strlen(possible_parent); + + return path[0] == 0 || + (memcmp(possible_parent, path, length) == 0 && + (path[length] == 0 || path[length] == '/')); +} + +void +InotifyQueue::Enqueue(const char *uri_utf8) +{ + ScheduleSeconds(INOTIFY_UPDATE_DELAY_S); + + for (auto i = queue.begin(), end = queue.end(); i != end;) { + const char *current_uri = i->c_str(); + + if (path_in(uri_utf8, current_uri)) + /* already enqueued */ + return; + + if (path_in(current_uri, uri_utf8)) + /* existing path is a sub-path of the new + path; we can dequeue the existing path and + update the new path instead */ + i = queue.erase(i); + else + ++i; + } + + queue.emplace_back(uri_utf8); +} diff --git a/src/db/update/InotifyQueue.hxx b/src/db/update/InotifyQueue.hxx new file mode 100644 index 000000000..99e2635b1 --- /dev/null +++ b/src/db/update/InotifyQueue.hxx @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_QUEUE_HXX +#define MPD_INOTIFY_QUEUE_HXX + +#include "event/TimeoutMonitor.hxx" +#include "Compiler.h" + +#include +#include + +class InotifyQueue final : private TimeoutMonitor { + std::list queue; + +public: + InotifyQueue(EventLoop &_loop):TimeoutMonitor(_loop) {} + + void Enqueue(const char *uri_utf8); + +private: + virtual void OnTimeout() override; +}; + +#endif diff --git a/src/db/update/InotifySource.cxx b/src/db/update/InotifySource.cxx new file mode 100644 index 000000000..c2783690e --- /dev/null +++ b/src/db/update/InotifySource.cxx @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "InotifySource.hxx" +#include "InotifyDomain.hxx" +#include "util/Error.hxx" +#include "system/fd_util.h" +#include "system/FatalError.hxx" +#include "Log.hxx" + +#include +#include +#include + +bool +InotifySource::OnSocketReady(gcc_unused unsigned flags) +{ + const auto dest = buffer.Write(); + if (dest.IsEmpty()) + FatalError("buffer full"); + + ssize_t nbytes = read(Get(), dest.data, dest.size); + if (nbytes < 0) + FatalSystemError("Failed to read from inotify"); + if (nbytes == 0) + FatalError("end of file from inotify"); + + buffer.Append(nbytes); + + while (true) { + const char *name; + + auto range = buffer.Read(); + const struct inotify_event *event = + (const struct inotify_event *) + range.data; + if (range.size < sizeof(*event) || + range.size < sizeof(*event) + event->len) + break; + + if (event->len > 0 && event->name[event->len - 1] == 0) + name = event->name; + else + name = nullptr; + + callback(event->wd, event->mask, name, callback_ctx); + buffer.Consume(sizeof(*event) + event->len); + } + + return true; +} + +inline +InotifySource::InotifySource(EventLoop &_loop, + mpd_inotify_callback_t _callback, void *_ctx, + int _fd) + :SocketMonitor(_fd, _loop), + callback(_callback), callback_ctx(_ctx) +{ + ScheduleRead(); + +} + +InotifySource * +InotifySource::Create(EventLoop &loop, + mpd_inotify_callback_t callback, void *callback_ctx, + Error &error) +{ + int fd = inotify_init_cloexec(); + if (fd < 0) { + error.SetErrno("inotify_init() has failed"); + return nullptr; + } + + return new InotifySource(loop, callback, callback_ctx, fd); +} + +int +InotifySource::Add(const char *path_fs, unsigned mask, Error &error) +{ + int wd = inotify_add_watch(Get(), path_fs, mask); + if (wd < 0) + error.SetErrno("inotify_add_watch() has failed"); + + return wd; +} + +void +InotifySource::Remove(unsigned wd) +{ + int ret = inotify_rm_watch(Get(), wd); + if (ret < 0 && errno != EINVAL) + LogErrno(inotify_domain, "inotify_rm_watch() has failed"); + + /* EINVAL may happen here when the file has been deleted; the + kernel seems to auto-unregister deleted files */ +} diff --git a/src/db/update/InotifySource.hxx b/src/db/update/InotifySource.hxx new file mode 100644 index 000000000..77c11093c --- /dev/null +++ b/src/db/update/InotifySource.hxx @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_SOURCE_HXX +#define MPD_INOTIFY_SOURCE_HXX + +#include "event/SocketMonitor.hxx" +#include "util/FifoBuffer.hxx" + +class Error; + +typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask, + const char *name, void *ctx); + +class InotifySource final : private SocketMonitor { + mpd_inotify_callback_t callback; + void *callback_ctx; + + FifoBuffer buffer; + + InotifySource(EventLoop &_loop, + mpd_inotify_callback_t callback, void *ctx, int fd); + +public: + ~InotifySource() { + Close(); + } + + /** + * Creates a new inotify source and registers it in the GLib main + * loop. + * + * @param a callback invoked for events received from the kernel + */ + static InotifySource *Create(EventLoop &_loop, + mpd_inotify_callback_t callback, + void *ctx, + Error &error); + + /** + * Adds a path to the notify list. + * + * @return a watch descriptor or -1 on error + */ + int Add(const char *path_fs, unsigned mask, Error &error); + + /** + * Removes a path from the notify list. + * + * @param wd the watch descriptor returned by mpd_inotify_source_add() + */ + void Remove(unsigned wd); + +private: + virtual bool OnSocketReady(unsigned flags) override; +}; + +#endif diff --git a/src/db/update/InotifyUpdate.cxx b/src/db/update/InotifyUpdate.cxx new file mode 100644 index 000000000..7515990d7 --- /dev/null +++ b/src/db/update/InotifyUpdate.cxx @@ -0,0 +1,339 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "InotifyUpdate.hxx" +#include "InotifySource.hxx" +#include "InotifyQueue.hxx" +#include "InotifyDomain.hxx" +#include "Mapper.hxx" +#include "Main.hxx" +#include "fs/AllocatedPath.hxx" +#include "fs/FileSystem.hxx" +#include "util/Error.hxx" +#include "Log.hxx" + +#include +#include +#include + +#include +#include +#include +#include +#include + +static constexpr unsigned IN_MASK = +#ifdef IN_ONLYDIR + IN_ONLYDIR| +#endif + IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF + |IN_MOVE|IN_MOVE_SELF; + +struct WatchDirectory { + WatchDirectory *parent; + + AllocatedPath name; + + int descriptor; + + std::forward_list children; + + template + WatchDirectory(WatchDirectory *_parent, N &&_name, + int _descriptor) + :parent(_parent), name(std::forward(_name)), + descriptor(_descriptor) {} + + WatchDirectory(const WatchDirectory &) = delete; + WatchDirectory &operator=(const WatchDirectory &) = delete; +}; + +static InotifySource *inotify_source; +static InotifyQueue *inotify_queue; + +static unsigned inotify_max_depth; +static WatchDirectory *inotify_root; +static std::map inotify_directories; + +static void +tree_add_watch_directory(WatchDirectory *directory) +{ + inotify_directories.insert(std::make_pair(directory->descriptor, + directory)); +} + +static void +tree_remove_watch_directory(WatchDirectory *directory) +{ + auto i = inotify_directories.find(directory->descriptor); + assert(i != inotify_directories.end()); + inotify_directories.erase(i); +} + +static WatchDirectory * +tree_find_watch_directory(int wd) +{ + auto i = inotify_directories.find(wd); + if (i == inotify_directories.end()) + return nullptr; + + return i->second; +} + +static void +disable_watch_directory(WatchDirectory &directory) +{ + tree_remove_watch_directory(&directory); + + for (WatchDirectory &child : directory.children) + disable_watch_directory(child); + + inotify_source->Remove(directory.descriptor); +} + +static void +remove_watch_directory(WatchDirectory *directory) +{ + assert(directory != nullptr); + + if (directory->parent == nullptr) { + LogWarning(inotify_domain, + "music directory was removed - " + "cannot continue to watch it"); + return; + } + + disable_watch_directory(*directory); + + /* remove it from the parent, which effectively deletes it */ + directory->parent->children.remove_if([directory](const WatchDirectory &child){ + return &child == directory; + }); +} + +static AllocatedPath +watch_directory_get_uri_fs(const WatchDirectory *directory) +{ + if (directory->parent == nullptr) + return AllocatedPath::Null(); + + const auto uri = watch_directory_get_uri_fs(directory->parent); + if (uri.IsNull()) + return directory->name; + + return AllocatedPath::Build(uri, directory->name); +} + +/* we don't look at "." / ".." nor files with newlines in their name */ +static bool skip_path(const char *path) +{ + return (path[0] == '.' && path[1] == 0) || + (path[0] == '.' && path[1] == '.' && path[2] == 0) || + strchr(path, '\n') != nullptr; +} + +static void +recursive_watch_subdirectories(WatchDirectory *directory, + const AllocatedPath &path_fs, unsigned depth) +{ + Error error; + DIR *dir; + struct dirent *ent; + + assert(directory != nullptr); + assert(depth <= inotify_max_depth); + assert(!path_fs.IsNull()); + + ++depth; + + if (depth > inotify_max_depth) + return; + + dir = opendir(path_fs.c_str()); + if (dir == nullptr) { + FormatErrno(inotify_domain, + "Failed to open directory %s", path_fs.c_str()); + return; + } + + while ((ent = readdir(dir))) { + struct stat st; + int ret; + + if (skip_path(ent->d_name)) + continue; + + const auto child_path_fs = + AllocatedPath::Build(path_fs, ent->d_name); + ret = StatFile(child_path_fs, st); + if (ret < 0) { + FormatErrno(inotify_domain, + "Failed to stat %s", + child_path_fs.c_str()); + continue; + } + + if (!S_ISDIR(st.st_mode)) + continue; + + ret = inotify_source->Add(child_path_fs.c_str(), IN_MASK, + error); + if (ret < 0) { + FormatError(error, + "Failed to register %s", + child_path_fs.c_str()); + error.Clear(); + continue; + } + + WatchDirectory *child = tree_find_watch_directory(ret); + if (child != nullptr) + /* already being watched */ + continue; + + directory->children.emplace_front(directory, + AllocatedPath::FromFS(ent->d_name), + ret); + child = &directory->children.front(); + + tree_add_watch_directory(child); + + recursive_watch_subdirectories(child, child_path_fs, depth); + } + + closedir(dir); +} + +gcc_pure +static unsigned +watch_directory_depth(const WatchDirectory *d) +{ + assert(d != nullptr); + + unsigned depth = 0; + while ((d = d->parent) != nullptr) + ++depth; + + return depth; +} + +static void +mpd_inotify_callback(int wd, unsigned mask, + gcc_unused const char *name, gcc_unused void *ctx) +{ + WatchDirectory *directory; + + /*FormatDebug(inotify_domain, "wd=%d mask=0x%x name='%s'", wd, mask, name);*/ + + directory = tree_find_watch_directory(wd); + if (directory == nullptr) + return; + + const auto uri_fs = watch_directory_get_uri_fs(directory); + + if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) { + remove_watch_directory(directory); + return; + } + + if ((mask & (IN_ATTRIB|IN_CREATE|IN_MOVE)) != 0 && + (mask & IN_ISDIR) != 0) { + /* a sub directory was changed: register those in + inotify */ + const auto &root = mapper_get_music_directory_fs(); + + const auto path_fs = uri_fs.IsNull() + ? root + : AllocatedPath::Build(root, uri_fs.c_str()); + + recursive_watch_subdirectories(directory, path_fs, + watch_directory_depth(directory)); + } + + if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 || + /* at the maximum depth, we watch out for newly created + directories */ + (watch_directory_depth(directory) == inotify_max_depth && + (mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) { + /* a file was changed, or a directory was + moved/deleted: queue a database update */ + + if (!uri_fs.IsNull()) { + const std::string uri_utf8 = uri_fs.ToUTF8(); + if (!uri_utf8.empty()) + inotify_queue->Enqueue(uri_utf8.c_str()); + } + else + inotify_queue->Enqueue(""); + } +} + +void +mpd_inotify_init(unsigned max_depth) +{ + LogDebug(inotify_domain, "initializing inotify"); + + const auto &path = mapper_get_music_directory_fs(); + if (path.IsNull()) { + LogDebug(inotify_domain, "no music directory configured"); + return; + } + + Error error; + inotify_source = InotifySource::Create(*main_loop, + mpd_inotify_callback, nullptr, + error); + if (inotify_source == nullptr) { + LogError(error); + return; + } + + inotify_max_depth = max_depth; + + int descriptor = inotify_source->Add(path.c_str(), IN_MASK, error); + if (descriptor < 0) { + LogError(error); + delete inotify_source; + inotify_source = nullptr; + return; + } + + inotify_root = new WatchDirectory(nullptr, path, descriptor); + + tree_add_watch_directory(inotify_root); + + recursive_watch_subdirectories(inotify_root, path, 0); + + inotify_queue = new InotifyQueue(*main_loop); + + LogDebug(inotify_domain, "watching music directory"); +} + +void +mpd_inotify_finish(void) +{ + if (inotify_source == nullptr) + return; + + delete inotify_queue; + delete inotify_source; + delete inotify_root; + inotify_directories.clear(); +} diff --git a/src/db/update/InotifyUpdate.hxx b/src/db/update/InotifyUpdate.hxx new file mode 100644 index 000000000..2d7d4e3b4 --- /dev/null +++ b/src/db/update/InotifyUpdate.hxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_INOTIFY_UPDATE_HXX +#define MPD_INOTIFY_UPDATE_HXX + +#include "check.h" + +#ifdef HAVE_INOTIFY_INIT + +void +mpd_inotify_init(unsigned max_depth); + +void +mpd_inotify_finish(void); + +#else /* !HAVE_INOTIFY_INIT */ + +static inline void +mpd_inotify_init(gcc_unused unsigned max_depth) +{ +} + +static inline void +mpd_inotify_finish(void) +{ +} + +#endif /* !HAVE_INOTIFY_INIT */ + +#endif diff --git a/src/db/update/UpdateArchive.cxx b/src/db/update/UpdateArchive.cxx new file mode 100644 index 000000000..5e733202d --- /dev/null +++ b/src/db/update/UpdateArchive.cxx @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateArchive.hxx" +#include "UpdateInternal.hxx" +#include "UpdateDomain.hxx" +#include "db/DatabaseLock.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "Mapper.hxx" +#include "fs/AllocatedPath.hxx" +#include "archive/ArchiveList.hxx" +#include "archive/ArchivePlugin.hxx" +#include "archive/ArchiveFile.hxx" +#include "archive/ArchiveVisitor.hxx" +#include "util/Error.hxx" +#include "Log.hxx" + +#include + +#include + +static void +update_archive_tree(Directory &directory, const char *name) +{ + const char *tmp = strchr(name, '/'); + if (tmp) { + const std::string child_name(name, tmp); + //add dir is not there already + db_lock(); + Directory *subdir = + directory.MakeChild(child_name.c_str()); + subdir->device = DEVICE_INARCHIVE; + db_unlock(); + + //create directories first + update_archive_tree(*subdir, tmp+1); + } else { + if (strlen(name) == 0) { + LogWarning(update_domain, + "archive returned directory only"); + return; + } + + //add file + db_lock(); + Song *song = directory.FindSong(name); + db_unlock(); + if (song == nullptr) { + song = Song::LoadFile(name, directory); + if (song != nullptr) { + db_lock(); + directory.AddSong(song); + db_unlock(); + + modified = true; + FormatDefault(update_domain, "added %s/%s", + directory.GetPath(), name); + } + } + } +} + +/** + * Updates the file listing from an archive file. + * + * @param parent the parent directory the archive file resides in + * @param name the UTF-8 encoded base name of the archive file + * @param st stat() information on the archive file + * @param plugin the archive plugin which fits this archive type + */ +static void +update_archive_file2(Directory &parent, const char *name, + const struct stat *st, + const struct archive_plugin *plugin) +{ + db_lock(); + Directory *directory = parent.FindChild(name); + db_unlock(); + + if (directory != nullptr && directory->mtime == st->st_mtime && + !walk_discard) + /* MPD has already scanned the archive, and it hasn't + changed since - don't consider updating it */ + return; + + const auto path_fs = map_directory_child_fs(parent, name); + + /* open archive */ + Error error; + ArchiveFile *file = archive_file_open(plugin, path_fs.c_str(), error); + if (file == nullptr) { + LogError(error); + return; + } + + FormatDebug(update_domain, "archive %s opened", path_fs.c_str()); + + if (directory == nullptr) { + FormatDebug(update_domain, + "creating archive directory: %s", name); + db_lock(); + directory = parent.CreateChild(name); + /* mark this directory as archive (we use device for + this) */ + directory->device = DEVICE_INARCHIVE; + db_unlock(); + } + + directory->mtime = st->st_mtime; + + class UpdateArchiveVisitor final : public ArchiveVisitor { + Directory *directory; + + public: + UpdateArchiveVisitor(Directory *_directory) + :directory(_directory) {} + + virtual void VisitArchiveEntry(const char *path_utf8) override { + FormatDebug(update_domain, + "adding archive file: %s", path_utf8); + update_archive_tree(*directory, path_utf8); + } + }; + + UpdateArchiveVisitor visitor(directory); + file->Visit(visitor); + file->Close(); +} + +bool +update_archive_file(Directory &directory, + const char *name, const char *suffix, + const struct stat *st) +{ +#ifdef ENABLE_ARCHIVE + const struct archive_plugin *plugin = + archive_plugin_from_suffix(suffix); + if (plugin == nullptr) + return false; + + update_archive_file2(directory, name, st, plugin); + return true; +#else + (void)directory; + (void)name; + (void)suffix; + (void)st; + + return false; +#endif +} diff --git a/src/db/update/UpdateArchive.hxx b/src/db/update/UpdateArchive.hxx new file mode 100644 index 000000000..1fc9af349 --- /dev/null +++ b/src/db/update/UpdateArchive.hxx @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_ARCHIVE_HXX +#define MPD_UPDATE_ARCHIVE_HXX + +#include "check.h" +#include "Compiler.h" + +#include + +struct Directory; + +#ifdef ENABLE_ARCHIVE + +bool +update_archive_file(Directory &directory, + const char *name, const char *suffix, + const struct stat *st); + +#else + +static inline bool +update_archive_file(gcc_unused Directory &directory, + gcc_unused const char *name, + gcc_unused const char *suffix, + gcc_unused const struct stat *st) +{ + return false; +} + +#endif + +#endif diff --git a/src/db/update/UpdateContainer.cxx b/src/db/update/UpdateContainer.cxx new file mode 100644 index 000000000..c03d88748 --- /dev/null +++ b/src/db/update/UpdateContainer.cxx @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateContainer.hxx" +#include "UpdateInternal.hxx" +#include "UpdateDatabase.hxx" +#include "UpdateDomain.hxx" +#include "db/DatabaseLock.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "decoder/DecoderPlugin.hxx" +#include "decoder/DecoderList.hxx" +#include "Mapper.hxx" +#include "fs/AllocatedPath.hxx" +#include "tag/TagHandler.hxx" +#include "tag/TagBuilder.hxx" +#include "Log.hxx" + +#include + +/** + * Create the specified directory object if it does not exist already + * or if the #stat object indicates that it has been modified since + * the last update. Returns nullptr when it exists already and is + * unmodified. + * + * The caller must lock the database. + */ +static Directory * +make_directory_if_modified(Directory &parent, const char *name, + const struct stat *st) +{ + Directory *directory = parent.FindChild(name); + + // directory exists already + if (directory != nullptr) { + if (directory->mtime == st->st_mtime && !walk_discard) { + /* not modified */ + return nullptr; + } + + delete_directory(directory); + modified = true; + } + + directory = parent.MakeChild(name); + directory->mtime = st->st_mtime; + return directory; +} + +static bool +SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix) +{ + return plugin.container_scan != nullptr && + plugin.SupportsSuffix(suffix); +} + +bool +update_container_file(Directory &directory, + const char *name, + const struct stat *st, + const char *suffix) +{ + const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){ + return SupportsContainerSuffix(plugin, suffix); + }); + if (_plugin == nullptr) + return false; + const DecoderPlugin &plugin = *_plugin; + + db_lock(); + Directory *contdir = make_directory_if_modified(directory, name, st); + if (contdir == nullptr) { + /* not modified */ + db_unlock(); + return true; + } + + contdir->device = DEVICE_CONTAINER; + db_unlock(); + + const auto pathname = map_directory_child_fs(directory, name); + + char *vtrack; + unsigned int tnum = 0; + TagBuilder tag_builder; + while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) { + Song *song = Song::NewFile(vtrack, *contdir); + + // shouldn't be necessary but it's there.. + song->mtime = st->st_mtime; + + const auto child_path_fs = + map_directory_child_fs(*contdir, vtrack); + + plugin.ScanFile(child_path_fs.c_str(), + add_tag_handler, &tag_builder); + + tag_builder.Commit(song->tag); + + db_lock(); + contdir->AddSong(song); + db_unlock(); + + modified = true; + + FormatDefault(update_domain, "added %s/%s", + directory.GetPath(), vtrack); + g_free(vtrack); + } + + if (tnum == 1) { + db_lock(); + delete_directory(contdir); + db_unlock(); + return false; + } else + return true; +} diff --git a/src/db/update/UpdateContainer.hxx b/src/db/update/UpdateContainer.hxx new file mode 100644 index 000000000..8125f71ee --- /dev/null +++ b/src/db/update/UpdateContainer.hxx @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_CONTAINER_HXX +#define MPD_UPDATE_CONTAINER_HXX + +#include "check.h" + +#include + +struct Directory; +struct DecoderPlugin; + +bool +update_container_file(Directory &directory, + const char *name, + const struct stat *st, + const char *suffix); + +#endif diff --git a/src/db/update/UpdateDatabase.cxx b/src/db/update/UpdateDatabase.cxx new file mode 100644 index 000000000..8ef0b6d82 --- /dev/null +++ b/src/db/update/UpdateDatabase.cxx @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateDatabase.hxx" +#include "UpdateRemove.hxx" +#include "PlaylistVector.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "db/DatabaseLock.hxx" + +#include +#include + +void +delete_song(Directory &dir, Song *del) +{ + assert(del->parent == &dir); + + /* first, prevent traversers in main task from getting this */ + dir.RemoveSong(del); + + db_unlock(); /* temporary unlock, because update_remove_song() blocks */ + + /* now take it out of the playlist (in the main_task) */ + update_remove_song(del); + + /* finally, all possible references gone, free it */ + del->Free(); + + db_lock(); +} + +/** + * Recursively remove all sub directories and songs from a directory, + * leaving an empty directory. + * + * Caller must lock the #db_mutex. + */ +static void +clear_directory(Directory &directory) +{ + Directory *child, *n; + directory_for_each_child_safe(child, n, directory) + delete_directory(child); + + Song *song, *ns; + directory_for_each_song_safe(song, ns, directory) { + assert(song->parent == &directory); + delete_song(directory, song); + } +} + +void +delete_directory(Directory *directory) +{ + assert(directory->parent != nullptr); + + clear_directory(*directory); + + directory->Delete(); +} + +bool +delete_name_in(Directory &parent, const char *name) +{ + bool modified = false; + + db_lock(); + Directory *directory = parent.FindChild(name); + + if (directory != nullptr) { + delete_directory(directory); + modified = true; + } + + Song *song = parent.FindSong(name); + if (song != nullptr) { + delete_song(parent, song); + modified = true; + } + + parent.playlists.erase(name); + + db_unlock(); + + return modified; +} diff --git a/src/db/update/UpdateDatabase.hxx b/src/db/update/UpdateDatabase.hxx new file mode 100644 index 000000000..bd7c395f2 --- /dev/null +++ b/src/db/update/UpdateDatabase.hxx @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_DATABASE_HXX +#define MPD_UPDATE_DATABASE_HXX + +#include "check.h" + +struct Directory; +struct Song; + +/** + * Caller must lock the #db_mutex. + */ +void +delete_song(Directory &parent, Song *song); + +/** + * Recursively free a directory and all its contents. + * + * Caller must lock the #db_mutex. + */ +void +delete_directory(Directory *directory); + +/** + * Caller must NOT lock the #db_mutex. + * + * @return true if the database was modified + */ +bool +delete_name_in(Directory &parent, const char *name); + +#endif diff --git a/src/db/update/UpdateDomain.cxx b/src/db/update/UpdateDomain.cxx new file mode 100644 index 000000000..80ad4fd22 --- /dev/null +++ b/src/db/update/UpdateDomain.cxx @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "UpdateDomain.hxx" +#include "util/Domain.hxx" + +const Domain update_domain("update"); diff --git a/src/db/update/UpdateDomain.hxx b/src/db/update/UpdateDomain.hxx new file mode 100644 index 000000000..a6e994390 --- /dev/null +++ b/src/db/update/UpdateDomain.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_DOMAIN_HXX +#define MPD_UPDATE_DOMAIN_HXX + +extern const class Domain update_domain; + +#endif diff --git a/src/db/update/UpdateGlue.cxx b/src/db/update/UpdateGlue.cxx new file mode 100644 index 000000000..d18747ba1 --- /dev/null +++ b/src/db/update/UpdateGlue.cxx @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "UpdateGlue.hxx" +#include "UpdateQueue.hxx" +#include "UpdateWalk.hxx" +#include "UpdateRemove.hxx" +#include "UpdateDomain.hxx" +#include "Mapper.hxx" +#include "db/DatabaseSimple.hxx" +#include "Idle.hxx" +#include "GlobalEvents.hxx" +#include "util/Error.hxx" +#include "Log.hxx" +#include "Main.hxx" +#include "Instance.hxx" +#include "system/FatalError.hxx" +#include "thread/Id.hxx" +#include "thread/Thread.hxx" +#include "thread/Util.hxx" + +#include + +static enum update_progress { + UPDATE_PROGRESS_IDLE = 0, + UPDATE_PROGRESS_RUNNING = 1, + UPDATE_PROGRESS_DONE = 2 +} progress; + +static bool modified; + +static Thread update_thread; + +static const unsigned update_task_id_max = 1 << 15; + +static unsigned update_task_id; + +static UpdateQueueItem next; + +unsigned +isUpdatingDB(void) +{ + return next.id; +} + +static void +update_task(gcc_unused void *ctx) +{ + if (!next.path_utf8.empty()) + FormatDebug(update_domain, "starting: %s", + next.path_utf8.c_str()); + else + LogDebug(update_domain, "starting"); + + SetThreadIdlePriority(); + + modified = update_walk(next.path_utf8.c_str(), next.discard); + + if (modified || !db_exists()) { + Error error; + if (!db_save(error)) + LogError(error, "Failed to save database"); + } + + if (!next.path_utf8.empty()) + FormatDebug(update_domain, "finished: %s", + next.path_utf8.c_str()); + else + LogDebug(update_domain, "finished"); + + progress = UPDATE_PROGRESS_DONE; + GlobalEvents::Emit(GlobalEvents::UPDATE); +} + +static void +spawn_update_task(UpdateQueueItem &&i) +{ + assert(main_thread.IsInside()); + + progress = UPDATE_PROGRESS_RUNNING; + modified = false; + + next = std::move(i); + + Error error; + if (!update_thread.Start(update_task, nullptr, error)) + FatalError(error); + + FormatDebug(update_domain, + "spawned thread for update job id %i", next.id); +} + +static unsigned +generate_update_id() +{ + unsigned id = update_task_id + 1; + if (id > update_task_id_max) + id = 1; + return id; +} + +unsigned +update_enqueue(const char *path, bool discard) +{ + assert(main_thread.IsInside()); + + if (!db_is_simple() || !mapper_has_music_directory()) + return 0; + + if (progress != UPDATE_PROGRESS_IDLE) { + const unsigned id = generate_update_id(); + if (!update_queue_push(path, discard, id)) + return 0; + + update_task_id = id; + return id; + } + + const unsigned id = update_task_id = generate_update_id(); + spawn_update_task(UpdateQueueItem(path, discard, id)); + + idle_add(IDLE_UPDATE); + + return id; +} + +/** + * Called in the main thread after the database update is finished. + */ +static void update_finished_event(void) +{ + assert(progress == UPDATE_PROGRESS_DONE); + assert(next.IsDefined()); + + update_thread.Join(); + next = UpdateQueueItem(); + + idle_add(IDLE_UPDATE); + + if (modified) + /* send "idle" events */ + instance->DatabaseModified(); + + auto i = update_queue_shift(); + if (i.IsDefined()) { + /* schedule the next path */ + spawn_update_task(std::move(i)); + } else { + progress = UPDATE_PROGRESS_IDLE; + } +} + +void update_global_init(void) +{ + GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event); + + update_remove_global_init(); + update_walk_global_init(); +} + +void update_global_finish(void) +{ + update_walk_global_finish(); +} diff --git a/src/db/update/UpdateGlue.hxx b/src/db/update/UpdateGlue.hxx new file mode 100644 index 000000000..6e247414e --- /dev/null +++ b/src/db/update/UpdateGlue.hxx @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_GLUE_HXX +#define MPD_UPDATE_GLUE_HXX + +#include "Compiler.h" + +void update_global_init(void); + +void update_global_finish(void); + +unsigned +isUpdatingDB(void); + +/** + * Add this path to the database update queue. + * + * @param path a path to update; if an empty string, + * the whole music directory is updated + * @return the job id, or 0 on error + */ +gcc_nonnull_all +unsigned +update_enqueue(const char *path, bool discard); + +#endif diff --git a/src/db/update/UpdateIO.cxx b/src/db/update/UpdateIO.cxx new file mode 100644 index 000000000..f91caf359 --- /dev/null +++ b/src/db/update/UpdateIO.cxx @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateIO.hxx" +#include "UpdateDomain.hxx" +#include "db/Directory.hxx" +#include "Mapper.hxx" +#include "fs/AllocatedPath.hxx" +#include "fs/FileSystem.hxx" +#include "Log.hxx" + +#include +#include + +int +stat_directory(const Directory &directory, struct stat *st) +{ + const auto path_fs = map_directory_fs(directory); + if (path_fs.IsNull()) + return -1; + + if (!StatFile(path_fs, *st)) { + int error = errno; + const std::string path_utf8 = path_fs.ToUTF8(); + FormatErrno(update_domain, error, + "Failed to stat %s", path_utf8.c_str()); + return -1; + } + + return 0; +} + +int +stat_directory_child(const Directory &parent, const char *name, + struct stat *st) +{ + const auto path_fs = map_directory_child_fs(parent, name); + if (path_fs.IsNull()) + return -1; + + if (!StatFile(path_fs, *st)) { + int error = errno; + const std::string path_utf8 = path_fs.ToUTF8(); + FormatErrno(update_domain, error, + "Failed to stat %s", path_utf8.c_str()); + return -1; + } + + return 0; +} + +bool +directory_exists(const Directory &directory) +{ + const auto path_fs = map_directory_fs(directory); + if (path_fs.IsNull()) + /* invalid path: cannot exist */ + return false; + + return directory.device == DEVICE_INARCHIVE || + directory.device == DEVICE_CONTAINER + ? FileExists(path_fs) + : DirectoryExists(path_fs); +} + +bool +directory_child_is_regular(const Directory &directory, + const char *name_utf8) +{ + const auto path_fs = map_directory_child_fs(directory, name_utf8); + if (path_fs.IsNull()) + return false; + + return FileExists(path_fs); +} + +bool +directory_child_access(const Directory &directory, + const char *name, int mode) +{ +#ifdef WIN32 + /* CheckAccess() is useless on WIN32 */ + (void)directory; + (void)name; + (void)mode; + return true; +#else + const auto path = map_directory_child_fs(directory, name); + if (path.IsNull()) + /* something went wrong, but that isn't a permission + problem */ + return true; + + return CheckAccess(path, mode) || errno != EACCES; +#endif +} diff --git a/src/db/update/UpdateIO.hxx b/src/db/update/UpdateIO.hxx new file mode 100644 index 000000000..819879422 --- /dev/null +++ b/src/db/update/UpdateIO.hxx @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_IO_HXX +#define MPD_UPDATE_IO_HXX + +#include "check.h" + +#include + +struct Directory; + +int +stat_directory(const Directory &directory, struct stat *st); + +int +stat_directory_child(const Directory &parent, const char *name, + struct stat *st); + +bool +directory_exists(const Directory &directory); + +bool +directory_child_is_regular(const Directory &directory, + const char *name_utf8); + +/** + * Checks if the given permissions on the mapped file are given. + */ +bool +directory_child_access(const Directory &directory, + const char *name, int mode); + +#endif diff --git a/src/db/update/UpdateInternal.hxx b/src/db/update/UpdateInternal.hxx new file mode 100644 index 000000000..2e373bd06 --- /dev/null +++ b/src/db/update/UpdateInternal.hxx @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_INTERNAL_H +#define MPD_UPDATE_INTERNAL_H + +#include "check.h" + +extern bool walk_discard; +extern bool modified; + +#endif diff --git a/src/db/update/UpdateQueue.cxx b/src/db/update/UpdateQueue.cxx new file mode 100644 index 000000000..a6002f854 --- /dev/null +++ b/src/db/update/UpdateQueue.cxx @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "UpdateQueue.hxx" + +#include +#include + +static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32; + +static std::queue> update_queue; + +bool +update_queue_push(const char *path, bool discard, unsigned id) +{ + if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE) + return false; + + update_queue.emplace(path, discard, id); + return true; +} + +UpdateQueueItem +update_queue_shift() +{ + if (update_queue.empty()) + return UpdateQueueItem(); + + auto i = std::move(update_queue.front()); + update_queue.pop(); + return i; +} diff --git a/src/db/update/UpdateQueue.hxx b/src/db/update/UpdateQueue.hxx new file mode 100644 index 000000000..e4228f5ed --- /dev/null +++ b/src/db/update/UpdateQueue.hxx @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_QUEUE_HXX +#define MPD_UPDATE_QUEUE_HXX + +#include "check.h" + +#include + +struct UpdateQueueItem { + std::string path_utf8; + unsigned id; + bool discard; + + UpdateQueueItem():id(0) {} + UpdateQueueItem(const char *_path, bool _discard, + unsigned _id) + :path_utf8(_path), id(_id), discard(_discard) {} + + bool IsDefined() const { + return id != 0; + } +}; + +bool +update_queue_push(const char *path, bool discard, unsigned id); + +UpdateQueueItem +update_queue_shift(); + +#endif diff --git a/src/db/update/UpdateRemove.cxx b/src/db/update/UpdateRemove.cxx new file mode 100644 index 000000000..c57758aef --- /dev/null +++ b/src/db/update/UpdateRemove.cxx @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateRemove.hxx" +#include "UpdateDomain.hxx" +#include "GlobalEvents.hxx" +#include "thread/Mutex.hxx" +#include "thread/Cond.hxx" +#include "db/Song.hxx" +#include "db/LightSong.hxx" +#include "Main.hxx" +#include "Instance.hxx" +#include "Log.hxx" + +#ifdef ENABLE_SQLITE +#include "sticker/StickerDatabase.hxx" +#include "sticker/SongSticker.hxx" +#endif + +#include + +static const Song *removed_song; + +static Mutex remove_mutex; +static Cond remove_cond; + +/** + * Safely remove a song from the database. This must be done in the + * main task, to be sure that there is no pointer left to it. + */ +static void +song_remove_event(void) +{ + assert(removed_song != nullptr); + + { + const auto uri = removed_song->GetURI(); + FormatDefault(update_domain, "removing %s", uri.c_str()); + } + +#ifdef ENABLE_SQLITE + /* if the song has a sticker, remove it */ + if (sticker_enabled()) + sticker_song_delete(removed_song->Export()); +#endif + + { + const auto uri = removed_song->GetURI(); + instance->DeleteSong(uri.c_str()); + } + + /* clear "removed_song" and send signal to update thread */ + remove_mutex.lock(); + removed_song = nullptr; + remove_cond.signal(); + remove_mutex.unlock(); +} + +void +update_remove_global_init(void) +{ + GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event); +} + +void +update_remove_song(const Song *song) +{ + assert(removed_song == nullptr); + + removed_song = song; + + GlobalEvents::Emit(GlobalEvents::DELETE); + + remove_mutex.lock(); + + while (removed_song != nullptr) + remove_cond.wait(remove_mutex); + + remove_mutex.unlock(); +} diff --git a/src/db/update/UpdateRemove.hxx b/src/db/update/UpdateRemove.hxx new file mode 100644 index 000000000..d54e3aa80 --- /dev/null +++ b/src/db/update/UpdateRemove.hxx @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_REMOVE_HXX +#define MPD_UPDATE_REMOVE_HXX + +#include "check.h" + +struct Song; + +void +update_remove_global_init(void); + +/** + * Sends a signal to the main thread which will in turn remove the + * song: from the sticker database and from the playlist. This + * serialized access is implemented to avoid excessive locking. + */ +void +update_remove_song(const Song *song); + +#endif diff --git a/src/db/update/UpdateSong.cxx b/src/db/update/UpdateSong.cxx new file mode 100644 index 000000000..ac2d01cd2 --- /dev/null +++ b/src/db/update/UpdateSong.cxx @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateSong.hxx" +#include "UpdateInternal.hxx" +#include "UpdateIO.hxx" +#include "UpdateDatabase.hxx" +#include "UpdateContainer.hxx" +#include "UpdateDomain.hxx" +#include "db/DatabaseLock.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "decoder/DecoderList.hxx" +#include "Log.hxx" + +#include + +static void +update_song_file2(Directory &directory, + const char *name, const struct stat *st, + const char *suffix) +{ + db_lock(); + Song *song = directory.FindSong(name); + db_unlock(); + + if (!directory_child_access(directory, name, R_OK)) { + FormatError(update_domain, + "no read permissions on %s/%s", + directory.GetPath(), name); + if (song != nullptr) { + db_lock(); + delete_song(directory, song); + db_unlock(); + } + + return; + } + + if (!(song != nullptr && st->st_mtime == song->mtime && + !walk_discard) && + update_container_file(directory, name, st, suffix)) { + if (song != nullptr) { + db_lock(); + delete_song(directory, song); + db_unlock(); + } + + return; + } + + if (song == nullptr) { + FormatDebug(update_domain, "reading %s/%s", + directory.GetPath(), name); + song = Song::LoadFile(name, directory); + if (song == nullptr) { + FormatDebug(update_domain, + "ignoring unrecognized file %s/%s", + directory.GetPath(), name); + return; + } + + db_lock(); + directory.AddSong(song); + db_unlock(); + + modified = true; + FormatDefault(update_domain, "added %s/%s", + directory.GetPath(), name); + } else if (st->st_mtime != song->mtime || walk_discard) { + FormatDefault(update_domain, "updating %s/%s", + directory.GetPath(), name); + if (!song->UpdateFile()) { + FormatDebug(update_domain, + "deleting unrecognized file %s/%s", + directory.GetPath(), name); + db_lock(); + delete_song(directory, song); + db_unlock(); + } + + modified = true; + } +} + +bool +update_song_file(Directory &directory, + const char *name, const char *suffix, + const struct stat *st) +{ + if (!decoder_plugins_supports_suffix(suffix)) + return false; + + update_song_file2(directory, name, st, suffix); + return true; +} diff --git a/src/db/update/UpdateSong.hxx b/src/db/update/UpdateSong.hxx new file mode 100644 index 000000000..5feb01928 --- /dev/null +++ b/src/db/update/UpdateSong.hxx @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_SONG_HXX +#define MPD_UPDATE_SONG_HXX + +#include "check.h" + +#include + +struct Directory; + +bool +update_song_file(Directory &directory, + const char *name, const char *suffix, + const struct stat *st); + +#endif diff --git a/src/db/update/UpdateWalk.cxx b/src/db/update/UpdateWalk.cxx new file mode 100644 index 000000000..c5a9936e9 --- /dev/null +++ b/src/db/update/UpdateWalk.cxx @@ -0,0 +1,484 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" /* must be first for large file support */ +#include "UpdateWalk.hxx" +#include "UpdateIO.hxx" +#include "UpdateDatabase.hxx" +#include "UpdateSong.hxx" +#include "UpdateArchive.hxx" +#include "UpdateDomain.hxx" +#include "db/DatabaseLock.hxx" +#include "db/DatabaseSimple.hxx" +#include "db/Directory.hxx" +#include "db/Song.hxx" +#include "PlaylistVector.hxx" +#include "playlist/PlaylistRegistry.hxx" +#include "Mapper.hxx" +#include "ExcludeList.hxx" +#include "config/ConfigGlobal.hxx" +#include "config/ConfigOption.hxx" +#include "fs/AllocatedPath.hxx" +#include "fs/Traits.hxx" +#include "fs/FileSystem.hxx" +#include "fs/DirectoryReader.hxx" +#include "util/Alloc.hxx" +#include "util/UriUtil.hxx" +#include "Log.hxx" + +#include +#include +#include +#include +#include + +bool walk_discard; +bool modified; + +#ifndef WIN32 + +static constexpr bool DEFAULT_FOLLOW_INSIDE_SYMLINKS = true; +static constexpr bool DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true; + +static bool follow_inside_symlinks; +static bool follow_outside_symlinks; + +#endif + +void +update_walk_global_init(void) +{ +#ifndef WIN32 + follow_inside_symlinks = + config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS, + DEFAULT_FOLLOW_INSIDE_SYMLINKS); + + follow_outside_symlinks = + config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS, + DEFAULT_FOLLOW_OUTSIDE_SYMLINKS); +#endif +} + +void +update_walk_global_finish(void) +{ +} + +static void +directory_set_stat(Directory &dir, const struct stat *st) +{ + dir.inode = st->st_ino; + dir.device = st->st_dev; + dir.have_stat = true; +} + +static void +remove_excluded_from_directory(Directory &directory, + const ExcludeList &exclude_list) +{ + db_lock(); + + Directory *child, *n; + directory_for_each_child_safe(child, n, directory) { + const auto name_fs = AllocatedPath::FromUTF8(child->GetName()); + + if (name_fs.IsNull() || exclude_list.Check(name_fs)) { + delete_directory(child); + modified = true; + } + } + + Song *song, *ns; + directory_for_each_song_safe(song, ns, directory) { + assert(song->parent == &directory); + + const auto name_fs = AllocatedPath::FromUTF8(song->uri); + if (name_fs.IsNull() || exclude_list.Check(name_fs)) { + delete_song(directory, song); + modified = true; + } + } + + db_unlock(); +} + +static void +purge_deleted_from_directory(Directory &directory) +{ + Directory *child, *n; + directory_for_each_child_safe(child, n, directory) { + if (directory_exists(*child)) + continue; + + db_lock(); + delete_directory(child); + db_unlock(); + + modified = true; + } + + Song *song, *ns; + directory_for_each_song_safe(song, ns, directory) { + const auto path = map_song_fs(*song); + if (path.IsNull() || !FileExists(path)) { + db_lock(); + delete_song(directory, song); + db_unlock(); + + modified = true; + } + } + + for (auto i = directory.playlists.begin(), + end = directory.playlists.end(); + i != end;) { + if (!directory_child_is_regular(directory, i->name.c_str())) { + db_lock(); + i = directory.playlists.erase(i); + db_unlock(); + } else + ++i; + } +} + +#ifndef WIN32 +static int +update_directory_stat(Directory &directory) +{ + struct stat st; + if (stat_directory(directory, &st) < 0) + return -1; + + directory_set_stat(directory, &st); + return 0; +} +#endif + +static int +find_inode_ancestor(Directory *parent, ino_t inode, dev_t device) +{ +#ifndef WIN32 + while (parent) { + if (!parent->have_stat && update_directory_stat(*parent) < 0) + return -1; + + if (parent->inode == inode && parent->device == device) { + LogDebug(update_domain, "recursive directory found"); + return 1; + } + + parent = parent->parent; + } +#else + (void)parent; + (void)inode; + (void)device; +#endif + + return 0; +} + +static bool +update_playlist_file2(Directory &directory, + const char *name, const char *suffix, + const struct stat *st) +{ + if (!playlist_suffix_supported(suffix)) + return false; + + PlaylistInfo pi(name, st->st_mtime); + + db_lock(); + if (directory.playlists.UpdateOrInsert(std::move(pi))) + modified = true; + db_unlock(); + return true; +} + +static bool +update_regular_file(Directory &directory, + const char *name, const struct stat *st) +{ + const char *suffix = uri_get_suffix(name); + if (suffix == nullptr) + return false; + + return update_song_file(directory, name, suffix, st) || + update_archive_file(directory, name, suffix, st) || + update_playlist_file2(directory, name, suffix, st); +} + +static bool +update_directory(Directory &directory, const struct stat *st); + +static void +update_directory_child(Directory &directory, + const char *name, const struct stat *st) +{ + assert(strchr(name, '/') == nullptr); + + if (S_ISREG(st->st_mode)) { + update_regular_file(directory, name, st); + } else if (S_ISDIR(st->st_mode)) { + if (find_inode_ancestor(&directory, st->st_ino, st->st_dev)) + return; + + db_lock(); + Directory *subdir = directory.MakeChild(name); + db_unlock(); + + assert(&directory == subdir->parent); + + if (!update_directory(*subdir, st)) { + db_lock(); + delete_directory(subdir); + db_unlock(); + } + } else { + FormatDebug(update_domain, + "%s is not a directory, archive or music", name); + } +} + +/* we don't look at "." / ".." nor files with newlines in their name */ +gcc_pure +static bool skip_path(Path path_fs) +{ + const char *path = path_fs.c_str(); + return (path[0] == '.' && path[1] == 0) || + (path[0] == '.' && path[1] == '.' && path[2] == 0) || + strchr(path, '\n') != nullptr; +} + +gcc_pure +static bool +skip_symlink(const Directory *directory, const char *utf8_name) +{ +#ifndef WIN32 + const auto path_fs = map_directory_child_fs(*directory, utf8_name); + if (path_fs.IsNull()) + return true; + + const auto target = ReadLink(path_fs); + if (target.IsNull()) + /* don't skip if this is not a symlink */ + return errno != EINVAL; + + if (!follow_inside_symlinks && !follow_outside_symlinks) { + /* ignore all symlinks */ + return true; + } else if (follow_inside_symlinks && follow_outside_symlinks) { + /* consider all symlinks */ + return false; + } + + const char *target_str = target.c_str(); + + if (PathTraitsFS::IsAbsolute(target_str)) { + /* if the symlink points to an absolute path, see if + that path is inside the music directory */ + const char *relative = map_to_relative_path(target_str); + return relative > target_str + ? !follow_inside_symlinks + : !follow_outside_symlinks; + } + + const char *p = target_str; + while (*p == '.') { + if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) { + /* "../" moves to parent directory */ + directory = directory->parent; + if (directory == nullptr) { + /* we have moved outside the music + directory - skip this symlink + if such symlinks are not allowed */ + return !follow_outside_symlinks; + } + p += 3; + } else if (PathTraitsFS::IsSeparator(p[1])) + /* eliminate "./" */ + p += 2; + else + break; + } + + /* we are still in the music directory, so this symlink points + to a song which is already in the database - skip according + to the follow_inside_symlinks param*/ + return !follow_inside_symlinks; +#else + /* no symlink checking on WIN32 */ + + (void)directory; + (void)utf8_name; + + return false; +#endif +} + +static bool +update_directory(Directory &directory, const struct stat *st) +{ + assert(S_ISDIR(st->st_mode)); + + directory_set_stat(directory, st); + + const auto path_fs = map_directory_fs(directory); + if (path_fs.IsNull()) + return false; + + DirectoryReader reader(path_fs); + if (reader.HasFailed()) { + int error = errno; + const auto path_utf8 = path_fs.ToUTF8(); + FormatErrno(update_domain, error, + "Failed to open directory %s", + path_utf8.c_str()); + return false; + } + + ExcludeList exclude_list; + exclude_list.LoadFile(AllocatedPath::Build(path_fs, ".mpdignore")); + + if (!exclude_list.IsEmpty()) + remove_excluded_from_directory(directory, exclude_list); + + purge_deleted_from_directory(directory); + + while (reader.ReadEntry()) { + std::string utf8; + struct stat st2; + + const auto entry = reader.GetEntry(); + + if (skip_path(entry) || exclude_list.Check(entry)) + continue; + + utf8 = entry.ToUTF8(); + if (utf8.empty()) + continue; + + if (skip_symlink(&directory, utf8.c_str())) { + modified |= delete_name_in(directory, utf8.c_str()); + continue; + } + + if (stat_directory_child(directory, utf8.c_str(), &st2) == 0) + update_directory_child(directory, utf8.c_str(), &st2); + else + modified |= delete_name_in(directory, utf8.c_str()); + } + + directory.mtime = st->st_mtime; + + return true; +} + +static Directory * +directory_make_child_checked(Directory &parent, const char *name_utf8) +{ + db_lock(); + Directory *directory = parent.FindChild(name_utf8); + db_unlock(); + + if (directory != nullptr) + return directory; + + struct stat st; + if (stat_directory_child(parent, name_utf8, &st) < 0 || + find_inode_ancestor(&parent, st.st_ino, st.st_dev)) + return nullptr; + + if (skip_symlink(&parent, name_utf8)) + return nullptr; + + /* if we're adding directory paths, make sure to delete filenames + with potentially the same name */ + db_lock(); + Song *conflicting = parent.FindSong(name_utf8); + if (conflicting) + delete_song(parent, conflicting); + + directory = parent.CreateChild(name_utf8); + db_unlock(); + + directory_set_stat(*directory, &st); + return directory; +} + +static Directory * +directory_make_uri_parent_checked(const char *uri) +{ + Directory *directory = db_get_root(); + char *duplicated = xstrdup(uri); + char *name_utf8 = duplicated, *slash; + + while ((slash = strchr(name_utf8, '/')) != nullptr) { + *slash = 0; + + if (*name_utf8 == 0) + continue; + + directory = directory_make_child_checked(*directory, + name_utf8); + if (directory == nullptr) + break; + + name_utf8 = slash + 1; + } + + free(duplicated); + return directory; +} + +static void +update_uri(const char *uri) +{ + Directory *parent = directory_make_uri_parent_checked(uri); + if (parent == nullptr) + return; + + const char *name = PathTraitsUTF8::GetBase(uri); + + struct stat st; + if (!skip_symlink(parent, name) && + stat_directory_child(*parent, name, &st) == 0) + update_directory_child(*parent, name, &st); + else + modified |= delete_name_in(*parent, name); +} + +bool +update_walk(const char *path, bool discard) +{ + walk_discard = discard; + modified = false; + + if (path != nullptr && !isRootDirectory(path)) { + update_uri(path); + } else { + Directory *directory = db_get_root(); + struct stat st; + + if (stat_directory(*directory, &st) == 0) + update_directory(*directory, &st); + } + + return modified; +} diff --git a/src/db/update/UpdateWalk.hxx b/src/db/update/UpdateWalk.hxx new file mode 100644 index 000000000..e908829e3 --- /dev/null +++ b/src/db/update/UpdateWalk.hxx @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPDATE_WALK_HXX +#define MPD_UPDATE_WALK_HXX + +#include "check.h" + +void +update_walk_global_init(void); + +void +update_walk_global_finish(void); + +/** + * Returns true if the database was modified. + */ +bool +update_walk(const char *path, bool discard); + +#endif diff --git a/src/db/upnp/Action.hxx b/src/db/upnp/Action.hxx deleted file mode 100644 index 28c88be92..000000000 --- a/src/db/upnp/Action.hxx +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_ACTION_HXX -#define MPD_UPNP_ACTION_HXX - -#include "Compiler.h" - -#include - -static inline constexpr unsigned -CountNameValuePairs() -{ - return 0; -} - -template -static inline constexpr unsigned -CountNameValuePairs(gcc_unused const char *name, gcc_unused const char *value, - Args... args) -{ - return 1 + CountNameValuePairs(args...); -} - -/** - * A wrapper for UpnpMakeAction() that counts the number of name/value - * pairs and adds the nullptr sentinel. - */ -template -static inline IXML_Document * -MakeActionHelper(const char *action_name, const char *service_type, - Args... args) -{ - const unsigned n = CountNameValuePairs(args...); - return UpnpMakeAction(action_name, service_type, n, - args..., - nullptr, nullptr); -} - -#endif diff --git a/src/db/upnp/ContentDirectoryService.cxx b/src/db/upnp/ContentDirectoryService.cxx deleted file mode 100644 index 35445e09d..000000000 --- a/src/db/upnp/ContentDirectoryService.cxx +++ /dev/null @@ -1,273 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "ContentDirectoryService.hxx" -#include "Domain.hxx" -#include "Device.hxx" -#include "ixmlwrap.hxx" -#include "Directory.hxx" -#include "Util.hxx" -#include "Action.hxx" -#include "util/NumberParser.hxx" -#include "util/Error.hxx" - -#include -#include - -ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device, - const UPnPService &service) - :m_actionURL(caturl(device.URLBase, service.controlURL)), - m_serviceType(service.serviceType), - m_deviceId(device.UDN), - m_friendlyName(device.friendlyName), - m_manufacturer(device.manufacturer), - m_modelName(device.modelName), - m_rdreqcnt(200) -{ - if (!m_modelName.compare("MediaTomb")) { - // Readdir by 200 entries is good for most, but MediaTomb likes - // them really big. Actually 1000 is better but I don't dare - m_rdreqcnt = 500; - } -} - -ContentDirectoryService::~ContentDirectoryService() -{ - /* this destructor exists here just so it won't get inlined */ -} - -static bool -ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response, Error &error) -{ - const char *p = ixmlwrap::getFirstElementValue(response, "Result"); - if (p == nullptr) - p = ""; - - return dirbuf.parse(p, error); -} - -inline bool -ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl, - const char *objectId, unsigned offset, - unsigned count, UPnPDirContent &dirbuf, - unsigned &didreadp, unsigned &totalp, - Error &error) const -{ - // Create request - char ofbuf[100], cntbuf[100]; - sprintf(ofbuf, "%u", offset); - sprintf(cntbuf, "%u", count); - // Some devices require an empty SortCriteria, else bad params - IXML_Document *request = - MakeActionHelper("Browse", m_serviceType.c_str(), - "ObjectID", objectId, - "BrowseFlag", "BrowseDirectChildren", - "Filter", "*", - "SortCriteria", "", - "StartingIndex", ofbuf, - "RequestedCount", cntbuf); - if (request == nullptr) { - error.Set(upnp_domain, "UpnpMakeAction() failed"); - return false; - } - - IXML_Document *response; - int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(), - 0 /*devUDN*/, request, &response); - ixmlDocument_free(request); - if (code != UPNP_E_SUCCESS) { - error.Format(upnp_domain, code, - "UpnpSendAction() failed: %s", - UpnpGetErrorMessage(code)); - return false; - } - - const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned"); - didreadp = value != nullptr - ? ParseUnsigned(value) - : 0; - - value = ixmlwrap::getFirstElementValue(response, "TotalMatches"); - if (value != nullptr) - totalp = ParseUnsigned(value); - - bool success = ReadResultTag(dirbuf, response, error); - ixmlDocument_free(response); - return success; -} - -bool -ContentDirectoryService::readDir(UpnpClient_Handle handle, - const char *objectId, - UPnPDirContent &dirbuf, - Error &error) const -{ - unsigned offset = 0, total = -1, count; - - do { - if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf, - count, total, error)) - return false; - - offset += count; - } while (count > 0 && offset < total); - - return true; -} - -bool -ContentDirectoryService::search(UpnpClient_Handle hdl, - const char *objectId, - const char *ss, - UPnPDirContent &dirbuf, - Error &error) const -{ - unsigned offset = 0, total = -1, count; - - do { - char ofbuf[100]; - sprintf(ofbuf, "%d", offset); - - IXML_Document *request = - MakeActionHelper("Search", m_serviceType.c_str(), - "ContainerID", objectId, - "SearchCriteria", ss, - "Filter", "*", - "SortCriteria", "", - "StartingIndex", ofbuf, - "RequestedCount", "0"); // Setting a value here gets twonky into fits - if (request == 0) { - error.Set(upnp_domain, "UpnpMakeAction() failed"); - return false; - } - - IXML_Document *response; - auto code = UpnpSendAction(hdl, m_actionURL.c_str(), - m_serviceType.c_str(), - 0 /*devUDN*/, request, &response); - ixmlDocument_free(request); - if (code != UPNP_E_SUCCESS) { - error.Format(upnp_domain, code, - "UpnpSendAction() failed: %s", - UpnpGetErrorMessage(code)); - return false; - } - - const char *value = - ixmlwrap::getFirstElementValue(response, "NumberReturned"); - count = value != nullptr - ? ParseUnsigned(value) - : 0; - - offset += count; - - value = ixmlwrap::getFirstElementValue(response, "TotalMatches"); - if (value != nullptr) - total = ParseUnsigned(value); - - bool success = ReadResultTag(dirbuf, response, error); - ixmlDocument_free(response); - if (!success) - return false; - } while (count > 0 && offset < total); - - return true; -} - -bool -ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl, - std::list &result, - Error &error) const -{ - assert(result.empty()); - - IXML_Document *request = - UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(), - 0, - nullptr, nullptr); - if (request == 0) { - error.Set(upnp_domain, "UpnpMakeAction() failed"); - return false; - } - - IXML_Document *response; - auto code = UpnpSendAction(hdl, m_actionURL.c_str(), - m_serviceType.c_str(), - 0 /*devUDN*/, request, &response); - ixmlDocument_free(request); - if (code != UPNP_E_SUCCESS) { - error.Format(upnp_domain, code, - "UpnpSendAction() failed: %s", - UpnpGetErrorMessage(code)); - return false; - } - - const char *s = ixmlwrap::getFirstElementValue(response, "SearchCaps"); - if (s == nullptr || *s == 0) { - ixmlDocument_free(response); - return true; - } - - bool success = true; - if (!csvToStrings(s, result)) { - error.Set(upnp_domain, "Bad response"); - success = false; - } - - ixmlDocument_free(response); - return success; -} - -bool -ContentDirectoryService::getMetadata(UpnpClient_Handle hdl, - const char *objectId, - UPnPDirContent &dirbuf, - Error &error) const -{ - // Create request - IXML_Document *request = - MakeActionHelper("Browse", m_serviceType.c_str(), - "ObjectID", objectId, - "BrowseFlag", "BrowseMetadata", - "Filter", "*", - "SortCriteria", "", - "StartingIndex", "0", - "RequestedCount", "1"); - if (request == nullptr) { - error.Set(upnp_domain, "UpnpMakeAction() failed"); - return false; - } - - IXML_Document *response; - auto code = UpnpSendAction(hdl, m_actionURL.c_str(), - m_serviceType.c_str(), - 0 /*devUDN*/, request, &response); - ixmlDocument_free(request); - if (code != UPNP_E_SUCCESS) { - error.Format(upnp_domain, code, - "UpnpSendAction() failed: %s", - UpnpGetErrorMessage(code)); - return false; - } - - bool success = ReadResultTag(dirbuf, response, error); - ixmlDocument_free(response); - return success; -} diff --git a/src/db/upnp/ContentDirectoryService.hxx b/src/db/upnp/ContentDirectoryService.hxx deleted file mode 100644 index 24be5dfbf..000000000 --- a/src/db/upnp/ContentDirectoryService.hxx +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef _UPNPDIR_HXX_INCLUDED_ -#define _UPNPDIR_HXX_INCLUDED_ - -#include - -#include -#include - -class Error; -class UPnPDevice; -struct UPnPService; -class UPnPDirContent; - -/** - * Content Directory Service class. - * - * This stores identity data from a directory service - * and the device it belongs to, and has methods to query - * the directory, using libupnp for handling the UPnP protocols. - * - * Note: m_rdreqcnt: number of entries requested per directory read. - * 0 means all entries. The device can still return less entries than - * requested, depending on its own limits. In general it's not optimal - * becauses it triggers issues, and is sometimes actually slower, e.g. on - * a D-Link NAS 327 - * - * The value chosen may affect by the UpnpSetMaxContentLength - * (2000*1024) done during initialization, but this should be ample - */ -class ContentDirectoryService { - std::string m_actionURL; - std::string m_serviceType; - std::string m_deviceId; - std::string m_friendlyName; - std::string m_manufacturer; - std::string m_modelName; - - int m_rdreqcnt; // Slice size to use when reading - -public: - /** - * Construct by copying data from device and service objects. - * - * The discovery service does this: use getDirServices() - */ - ContentDirectoryService(const UPnPDevice &device, - const UPnPService &service); - - /** An empty one */ - ContentDirectoryService() = default; - - ~ContentDirectoryService(); - - /** Read a container's children list into dirbuf. - * - * @param objectId the UPnP object Id for the container. Root has Id "0" - * @param[out] dirbuf stores the entries we read. - */ - bool readDir(UpnpClient_Handle handle, - const char *objectId, UPnPDirContent &dirbuf, - Error &error) const; - - bool readDirSlice(UpnpClient_Handle handle, - const char *objectId, unsigned offset, - unsigned count, UPnPDirContent& dirbuf, - unsigned &didread, unsigned &total, - Error &error) const; - - /** Search the content directory service. - * - * @param objectId the UPnP object Id under which the search - * should be done. Not all servers actually support this below - * root. Root has Id "0" - * @param searchstring an UPnP searchcriteria string. Check the - * UPnP document: UPnP-av-ContentDirectory-v1-Service-20020625.pdf - * section 2.5.5. Maybe we'll provide an easier way some day... - * @param[out] dirbuf stores the entries we read. - */ - bool search(UpnpClient_Handle handle, - const char *objectId, const char *searchstring, - UPnPDirContent &dirbuf, - Error &error) const; - - /** Read metadata for a given node. - * - * @param objectId the UPnP object Id. Root has Id "0" - * @param[out] dirbuf stores the entries we read. At most one entry will be - * returned. - */ - bool getMetadata(UpnpClient_Handle handle, - const char *objectId, UPnPDirContent &dirbuf, - Error &error) const; - - /** Retrieve search capabilities - * - * @param[out] result an empty vector: no search, or a single '*' element: - * any tag can be used in a search, or a list of usable tag names. - */ - bool getSearchCapabilities(UpnpClient_Handle handle, - std::list &result, - Error &error) const; - - /** Retrieve the "friendly name" for this server, useful for display. */ - const char *getFriendlyName() const { - return m_friendlyName.c_str(); - } -}; - -#endif /* _UPNPDIR_HXX_INCLUDED_ */ diff --git a/src/db/upnp/Device.cxx b/src/db/upnp/Device.cxx deleted file mode 100644 index 7bec1cccd..000000000 --- a/src/db/upnp/Device.cxx +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "Device.hxx" -#include "Util.hxx" -#include "Expat.hxx" -#include "util/Error.hxx" - -#include - -#include - -UPnPDevice::~UPnPDevice() -{ - /* this destructor exists here just so it won't get inlined */ -} - -/** - * An XML parser which constructs an UPnP device object from the - * device descriptor. - */ -class UPnPDeviceParser final : public CommonExpatParser { - UPnPDevice &m_device; - - std::string *value; - - UPnPService m_tservice; - -public: - UPnPDeviceParser(UPnPDevice& device) - :m_device(device), - value(nullptr) {} - -protected: - virtual void StartElement(const XML_Char *name, const XML_Char **) { - value = nullptr; - - switch (name[0]) { - case 'c': - if (strcmp(name, "controlURL") == 0) - value = &m_tservice.controlURL; - break; - case 'd': - if (strcmp(name, "deviceType") == 0) - value = &m_device.deviceType; - break; - case 'f': - if (strcmp(name, "friendlyName") == 0) - value = &m_device.friendlyName; - break; - case 'm': - if (strcmp(name, "manufacturer") == 0) - value = &m_device.manufacturer; - else if (strcmp(name, "modelName") == 0) - value = &m_device.modelName; - break; - case 's': - if (strcmp(name, "serviceType") == 0) - value = &m_tservice.serviceType; - break; - case 'U': - if (strcmp(name, "UDN") == 0) - value = &m_device.UDN; - else if (strcmp(name, "URLBase") == 0) - value = &m_device.URLBase; - break; - } - } - - virtual void EndElement(const XML_Char *name) { - if (value != nullptr) { - trimstring(*value); - value = nullptr; - } else if (!strcmp(name, "service")) { - m_device.services.emplace_back(std::move(m_tservice)); - m_tservice.clear(); - } - } - - virtual void CharacterData(const XML_Char *s, int len) { - if (value != nullptr) - value->append(s, len); - } -}; - -bool -UPnPDevice::Parse(const std::string &url, const char *description, - Error &error) -{ - { - UPnPDeviceParser mparser(*this); - if (!mparser.Parse(description, strlen(description), - true, error)) - return false; - } - - if (URLBase.empty()) { - // The standard says that if the URLBase value is empty, we should use - // the url the description was retrieved from. However this is - // sometimes something like http://host/desc.xml, sometimes something - // like http://host/ - - if (url.size() < 8) { - // ??? - URLBase = url; - } else { - auto hostslash = url.find_first_of("/", 7); - if (hostslash == std::string::npos || hostslash == url.size()-1) { - URLBase = url; - } else { - URLBase = path_getfather(url); - } - } - } - - return true; -} diff --git a/src/db/upnp/Device.hxx b/src/db/upnp/Device.hxx deleted file mode 100644 index dd7ecac2d..000000000 --- a/src/db/upnp/Device.hxx +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef _UPNPDEV_HXX_INCLUDED_ -#define _UPNPDEV_HXX_INCLUDED_ - -#include -#include - -class Error; - -/** - * UPnP Description phase: interpreting the device description which we - * downloaded from the URL obtained by the discovery phase. - */ - -/** - * Data holder for a UPnP service, parsed from the XML description - * downloaded after discovery yielded its URL. - */ -struct UPnPService { - // e.g. urn:schemas-upnp-org:service:ConnectionManager:1 - std::string serviceType; - std::string controlURL; // e.g.: /upnp/control/cm - - void clear() - { - serviceType.clear(); - controlURL.clear(); - } -}; - -/** - * Data holder for a UPnP device, parsed from the XML description obtained - * during discovery. - * A device may include several services. To be of interest to us, - * one of them must be a ContentDirectory. - */ -class UPnPDevice { -public: - // e.g. urn:schemas-upnp-org:device:MediaServer:1 - std::string deviceType; - // e.g. MediaTomb - std::string friendlyName; - // Unique device number. This should match the deviceID in the - // discovery message. e.g. uuid:a7bdcd12-e6c1-4c7e-b588-3bbc959eda8d - std::string UDN; - // Base for all relative URLs. e.g. http://192.168.4.4:49152/ - std::string URLBase; - // Manufacturer: e.g. D-Link, PacketVideo ("manufacturer") - std::string manufacturer; - // Model name: e.g. MediaTomb, DNS-327L ("modelName") - std::string modelName; - // Services provided by this device. - std::vector services; - - UPnPDevice() = default; - UPnPDevice(const UPnPDevice &) = delete; - UPnPDevice(UPnPDevice &&) = default; - UPnPDevice &operator=(UPnPDevice &&) = default; - - ~UPnPDevice(); - - /** Build device from xml description downloaded from discovery - * @param url where the description came from - * @param description the xml device description - */ - bool Parse(const std::string &url, const char *description, - Error &error); -}; - -#endif /* _UPNPDEV_HXX_INCLUDED_ */ diff --git a/src/db/upnp/Directory.cxx b/src/db/upnp/Directory.cxx deleted file mode 100644 index adb8b213a..000000000 --- a/src/db/upnp/Directory.cxx +++ /dev/null @@ -1,262 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "Directory.hxx" -#include "Util.hxx" -#include "Expat.hxx" -#include "Tags.hxx" -#include "tag/TagBuilder.hxx" -#include "tag/TagTable.hxx" -#include "util/NumberParser.hxx" - -#include -#include - -#include - -UPnPDirContent::~UPnPDirContent() -{ - /* this destructor exists here just so it won't get inlined */ -} - -gcc_pure gcc_nonnull_all -static bool -CompareStringLiteral(const char *literal, const char *value, size_t length) -{ - return length == strlen(literal) && - memcmp(literal, value, length) == 0; -} - -gcc_pure -static UPnPDirObject::ItemClass -ParseItemClass(const char *name, size_t length) -{ - if (CompareStringLiteral("object.item.audioItem.musicTrack", - name, length)) - return UPnPDirObject::ItemClass::MUSIC; - else if (CompareStringLiteral("object.item.playlistItem", - name, length)) - return UPnPDirObject::ItemClass::PLAYLIST; - else - return UPnPDirObject::ItemClass::UNKNOWN; -} - -gcc_pure -static int -ParseDuration(const char *duration) -{ - char *endptr; - - unsigned result = ParseUnsigned(duration, &endptr); - if (endptr == duration || *endptr != ':') - return 0; - - result *= 60; - duration = endptr + 1; - result += ParseUnsigned(duration, &endptr); - if (endptr == duration || *endptr != ':') - return 0; - - result *= 60; - duration = endptr + 1; - result += ParseUnsigned(duration, &endptr); - if (endptr == duration || *endptr != 0) - return 0; - - return result; -} - -/** - * Transform titles to turn '/' into '_' to make them acceptable path - * elements. There is a very slight risk of collision in doing - * this. Twonky returns directory names (titles) like 'Artist/Album'. - */ -gcc_pure -static std::string -titleToPathElt(std::string &&s) -{ - std::replace(s.begin(), s.end(), '/', '_'); - return s; -} - -/** - * An XML parser which builds directory contents from DIDL lite input. - */ -class UPnPDirParser final : public CommonExpatParser { - UPnPDirContent &m_dir; - - enum { - NONE, - RES, - CLASS, - } state; - - /** - * If not equal to #TAG_NUM_OF_ITEM_TYPES, then we're - * currently reading an element containing a tag value. The - * value is being constructed in #value. - */ - TagType tag_type; - - /** - * The text inside the current element. - */ - std::string value; - - UPnPDirObject m_tobj; - TagBuilder tag; - -public: - UPnPDirParser(UPnPDirContent& dir) - :m_dir(dir), - state(NONE), - tag_type(TAG_NUM_OF_ITEM_TYPES) - { - } - -protected: - virtual void StartElement(const XML_Char *name, const XML_Char **attrs) - { - if (m_tobj.type != UPnPDirObject::Type::UNKNOWN && - tag_type == TAG_NUM_OF_ITEM_TYPES) { - tag_type = tag_table_lookup(upnp_tags, name); - if (tag_type != TAG_NUM_OF_ITEM_TYPES) - return; - } else { - assert(tag_type == TAG_NUM_OF_ITEM_TYPES); - } - - switch (name[0]) { - case 'c': - if (!strcmp(name, "container")) { - m_tobj.clear(); - m_tobj.type = UPnPDirObject::Type::CONTAINER; - - const char *id = GetAttribute(attrs, "id"); - if (id != nullptr) - m_tobj.m_id = id; - - const char *pid = GetAttribute(attrs, "parentID"); - if (pid != nullptr) - m_tobj.m_pid = pid; - } - break; - - case 'i': - if (!strcmp(name, "item")) { - m_tobj.clear(); - m_tobj.type = UPnPDirObject::Type::ITEM; - - const char *id = GetAttribute(attrs, "id"); - if (id != nullptr) - m_tobj.m_id = id; - - const char *pid = GetAttribute(attrs, "parentID"); - if (pid != nullptr) - m_tobj.m_pid = pid; - } - break; - - case 'r': - if (!strcmp(name, "res")) { - // - - const char *duration = - GetAttribute(attrs, "duration"); - if (duration != nullptr) - tag.SetTime(ParseDuration(duration)); - - state = RES; - } - - break; - - case 'u': - if (strcmp(name, "upnp:class") == 0) - state = CLASS; - } - } - - bool checkobjok() { - if (m_tobj.m_id.empty() || m_tobj.m_pid.empty() || - m_tobj.name.empty() || - (m_tobj.type == UPnPDirObject::Type::ITEM && - m_tobj.item_class == UPnPDirObject::ItemClass::UNKNOWN)) - return false; - - return true; - } - - virtual void EndElement(const XML_Char *name) - { - if (tag_type != TAG_NUM_OF_ITEM_TYPES) { - assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN); - - tag.AddItem(tag_type, value.c_str()); - - if (tag_type == TAG_TITLE) - m_tobj.name = titleToPathElt(std::move(value)); - - value.clear(); - tag_type = TAG_NUM_OF_ITEM_TYPES; - return; - } - - if ((!strcmp(name, "container") || !strcmp(name, "item")) && - checkobjok()) { - tag.Commit(m_tobj.tag); - m_dir.objects.emplace_back(std::move(m_tobj)); - } - - state = NONE; - } - - virtual void CharacterData(const XML_Char *s, int len) - { - if (tag_type != TAG_NUM_OF_ITEM_TYPES) { - assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN); - - value.append(s, len); - return; - } - - switch (state) { - case NONE: - break; - - case RES: - m_tobj.url.assign(s, len); - break; - - case CLASS: - m_tobj.item_class = ParseItemClass(s, len); - break; - } - } -}; - -bool -UPnPDirContent::parse(const char *input, Error &error) -{ - UPnPDirParser parser(*this); - return parser.Parse(input, strlen(input), true, error); -} diff --git a/src/db/upnp/Directory.hxx b/src/db/upnp/Directory.hxx deleted file mode 100644 index 433979900..000000000 --- a/src/db/upnp/Directory.hxx +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_DIRECTORY_HXX -#define MPD_UPNP_DIRECTORY_HXX - -#include "Object.hxx" -#include "Compiler.h" - -#include -#include - -class Error; - -/** - * Image of a MediaServer Directory Service container (directory), - * possibly containing items and subordinate containers. - */ -class UPnPDirContent { -public: - std::vector objects; - - ~UPnPDirContent(); - - gcc_pure - UPnPDirObject *FindObject(const char *name) { - for (auto &o : objects) - if (o.name == name) - return &o; - - return nullptr; - } - - /** - * Parse from DIDL-Lite XML data. - * - * Normally only used by ContentDirectoryService::readDir() - * This is cumulative: in general, the XML data is obtained in - * several documents corresponding to (offset,count) slices of the - * directory (container). parse() can be called repeatedly with - * the successive XML documents and will accumulate entries in the item - * and container vectors. This makes more sense if the different - * chunks are from the same container, but given that UPnP Ids are - * actually global, nothing really bad will happen if you mix - * up... - */ - bool parse(const char *didltext, Error &error); -}; - -#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ diff --git a/src/db/upnp/Discovery.cxx b/src/db/upnp/Discovery.cxx deleted file mode 100644 index 5203dba83..000000000 --- a/src/db/upnp/Discovery.cxx +++ /dev/null @@ -1,318 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "Discovery.hxx" -#include "Domain.hxx" -#include "ContentDirectoryService.hxx" -#include "upnpplib.hxx" -#include "system/Clock.hxx" -#include "Log.hxx" - -#include - -#include - -// The service type string we are looking for. -static constexpr char ContentDirectorySType[] = "urn:schemas-upnp-org:service:ContentDirectory:1"; - -// We don't include a version in comparisons, as we are satisfied with -// version 1 -gcc_pure -static bool -isCDService(const char *st) -{ - constexpr size_t sz = sizeof(ContentDirectorySType) - 3; - return memcmp(ContentDirectorySType, st, sz) == 0; -} - -// The type of device we're asking for in search -static constexpr char MediaServerDType[] = "urn:schemas-upnp-org:device:MediaServer:1"; - -gcc_pure -static bool -isMSDevice(const char *st) -{ - constexpr size_t sz = sizeof(MediaServerDType) - 3; - return memcmp(MediaServerDType, st, sz) == 0; -} - -inline void -UPnPDeviceDirectory::LockAdd(ContentDirectoryDescriptor &&d) -{ - const ScopeLock protect(mutex); - - for (auto &i : directories) { - if (i.id == d.id) { - i = std::move(d); - return; - } - } - - directories.emplace_back(std::move(d)); -} - -inline void -UPnPDeviceDirectory::LockRemove(const std::string &id) -{ - const ScopeLock protect(mutex); - - for (auto i = directories.begin(), end = directories.end(); - i != end; ++i) { - if (i->id == id) { - directories.erase(i); - break; - } - } -} - -inline void -UPnPDeviceDirectory::discoExplorer() -{ - for (;;) { - DiscoveredTask *tsk = 0; - if (!discoveredQueue.take(tsk)) { - discoveredQueue.workerExit(); - return; - } - - // Device signals its existence and well-being. Perform the - // UPnP "description" phase by downloading and decoding the - // description document. - char *buf; - // LINE_SIZE is defined by libupnp's upnp.h... - char contentType[LINE_SIZE]; - int code = UpnpDownloadUrlItem(tsk->url.c_str(), &buf, contentType); - if (code != UPNP_E_SUCCESS) { - continue; - } - - // Update or insert the device - ContentDirectoryDescriptor d(std::move(tsk->deviceId), - MonotonicClockS(), tsk->expires); - - { - Error error2; - bool success = d.Parse(tsk->url, buf, error2); - free(buf); - if (!success) { - delete tsk; - LogError(error2); - continue; - } - } - - LockAdd(std::move(d)); - delete tsk; - } -} - -void * -UPnPDeviceDirectory::discoExplorer(void *ctx) -{ - UPnPDeviceDirectory &directory = *(UPnPDeviceDirectory *)ctx; - directory.discoExplorer(); - return (void*)1; -} - -inline int -UPnPDeviceDirectory::OnAlive(Upnp_Discovery *disco) -{ - if (isMSDevice(disco->DeviceType) || - isCDService(disco->ServiceType)) { - DiscoveredTask *tp = new DiscoveredTask(disco); - if (discoveredQueue.put(tp)) - return UPNP_E_FINISH; - } - - return UPNP_E_SUCCESS; -} - -inline int -UPnPDeviceDirectory::OnByeBye(Upnp_Discovery *disco) -{ - if (isMSDevice(disco->DeviceType) || - isCDService(disco->ServiceType)) { - // Device signals it is going off. - LockRemove(disco->DeviceId); - } - - return UPNP_E_SUCCESS; -} - -// This gets called for all libupnp asynchronous events, in a libupnp -// thread context. -// Example: ContentDirectories appearing and disappearing from the network -// We queue a task for our worker thread(s) -inline int -UPnPDeviceDirectory::cluCallBack(Upnp_EventType et, void *evp) -{ - switch (et) { - case UPNP_DISCOVERY_SEARCH_RESULT: - case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE: - { - Upnp_Discovery *disco = (Upnp_Discovery *)evp; - return OnAlive(disco); - } - - case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE: - { - Upnp_Discovery *disco = (Upnp_Discovery *)evp; - return OnByeBye(disco); - } - - default: - // Ignore other events for now - break; - } - - return UPNP_E_SUCCESS; -} - -bool -UPnPDeviceDirectory::expireDevices(Error &error) -{ - const ScopeLock protect(mutex); - const unsigned now = MonotonicClockS(); - bool didsomething = false; - - for (auto it = directories.begin(); - it != directories.end();) { - if (now > it->expires) { - it = directories.erase(it); - didsomething = true; - } else { - it++; - } - } - - if (didsomething) - return search(error); - - return true; -} - -UPnPDeviceDirectory::UPnPDeviceDirectory(LibUPnP *_lib) - :lib(_lib), - discoveredQueue("DiscoveredQueue"), - m_searchTimeout(2), m_lastSearch(0) -{ -} - -UPnPDeviceDirectory::~UPnPDeviceDirectory() -{ - /* this destructor exists here just so it won't get inlined */ -} - -bool -UPnPDeviceDirectory::Start(Error &error) -{ - if (!discoveredQueue.start(1, discoExplorer, this)) { - error.Set(upnp_domain, "Discover work queue start failed"); - return false; - } - - lib->SetHandler([this](Upnp_EventType type, void *event){ - cluCallBack(type, event); - }); - - return search(error); -} - -bool -UPnPDeviceDirectory::search(Error &error) -{ - const unsigned now = MonotonicClockS(); - if (now - m_lastSearch < 10) - return true; - m_lastSearch = now; - - // We search both for device and service just in case. - int code = UpnpSearchAsync(lib->getclh(), m_searchTimeout, - ContentDirectorySType, lib); - if (code != UPNP_E_SUCCESS) { - error.Format(upnp_domain, code, - "UpnpSearchAsync() failed: %s", - UpnpGetErrorMessage(code)); - return false; - } - - code = UpnpSearchAsync(lib->getclh(), m_searchTimeout, - MediaServerDType, lib); - if (code != UPNP_E_SUCCESS) { - error.Format(upnp_domain, code, - "UpnpSearchAsync() failed: %s", - UpnpGetErrorMessage(code)); - return false; - } - - return true; -} - -bool -UPnPDeviceDirectory::getDirServices(std::vector &out, - Error &error) -{ - // Has locking, do it before our own lock - if (!expireDevices(error)) - return false; - - const ScopeLock protect(mutex); - - for (auto dit = directories.begin(); - dit != directories.end(); dit++) { - for (const auto &service : dit->device.services) { - if (isCDService(service.serviceType.c_str())) { - out.emplace_back(dit->device, service); - } - } - } - - return true; -} - -bool -UPnPDeviceDirectory::getServer(const char *friendlyName, - ContentDirectoryService &server, - Error &error) -{ - // Has locking, do it before our own lock - if (!expireDevices(error)) - return false; - - const ScopeLock protect(mutex); - - for (const auto &i : directories) { - const auto &device = i.device; - - if (device.friendlyName != friendlyName) - continue; - - for (const auto &service : device.services) { - if (isCDService(service.serviceType.c_str())) { - server = ContentDirectoryService(device, - service); - return true; - } - } - } - - error.Set(upnp_domain, "Server not found"); - return false; -} diff --git a/src/db/upnp/Discovery.hxx b/src/db/upnp/Discovery.hxx deleted file mode 100644 index 4c64fe420..000000000 --- a/src/db/upnp/Discovery.hxx +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef _UPNPPDISC_H_X_INCLUDED_ -#define _UPNPPDISC_H_X_INCLUDED_ - -#include "Device.hxx" -#include "WorkQueue.hxx" -#include "thread/Mutex.hxx" -#include "util/Error.hxx" - -#include - -#include -#include -#include - -class LibUPnP; -class ContentDirectoryService; - -/** - * Manage UPnP discovery and maintain a directory of active devices. Singleton. - * - * We are only interested in MediaServers with a ContentDirectory service - * for now, but this could be made more general, by removing the filtering. - */ -class UPnPDeviceDirectory { - /** - * Each appropriate discovery event (executing in a libupnp thread - * context) queues the following task object for processing by the - * discovery thread. - */ - struct DiscoveredTask { - std::string url; - std::string deviceId; - unsigned expires; // Seconds valid - - DiscoveredTask(const Upnp_Discovery *disco) - :url(disco->Location), - deviceId(disco->DeviceId), - expires(disco->Expires) {} - }; - - /** - * Descriptor for one device having a Content Directory - * service found on the network. - */ - class ContentDirectoryDescriptor { - public: - std::string id; - - UPnPDevice device; - - /** - * The MonotonicClockS() time stamp when this device - * expires. - */ - unsigned expires; - - ContentDirectoryDescriptor() = default; - - ContentDirectoryDescriptor(std::string &&_id, - unsigned last, int exp) - :id(std::move(_id)), expires(last + exp + 20) {} - - bool Parse(const std::string &url, const char *description, - Error &_error) { - return device.Parse(url, description, _error); - } - }; - - LibUPnP *const lib; - - Mutex mutex; - std::list directories; - WorkQueue discoveredQueue; - - /** - * The UPnP device search timeout, which should actually be - * called delay because it's the base of a random delay that - * the devices apply to avoid responding all at the same time. - */ - int m_searchTimeout; - - /** - * The MonotonicClockS() time stamp of the last search. - */ - unsigned m_lastSearch; - -public: - UPnPDeviceDirectory(LibUPnP *_lib); - ~UPnPDeviceDirectory(); - - UPnPDeviceDirectory(const UPnPDeviceDirectory &) = delete; - UPnPDeviceDirectory& operator=(const UPnPDeviceDirectory &) = delete; - - bool Start(Error &error); - - /** Retrieve the directory services currently seen on the network */ - bool getDirServices(std::vector &, Error &); - - /** - * Get server by friendly name. - */ - bool getServer(const char *friendlyName, - ContentDirectoryService &server, - Error &error); - -private: - bool search(Error &error); - - /** - * Look at the devices and get rid of those which have not - * been seen for too long. We do this when listing the top - * directory. - */ - bool expireDevices(Error &error); - - void LockAdd(ContentDirectoryDescriptor &&d); - void LockRemove(const std::string &id); - - /** - * Worker routine for the discovery queue. Get messages about - * devices appearing and disappearing, and update the - * directory pool accordingly. - */ - static void *discoExplorer(void *); - void discoExplorer(); - - int OnAlive(Upnp_Discovery *disco); - int OnByeBye(Upnp_Discovery *disco); - int cluCallBack(Upnp_EventType et, void *evp); -}; - - -#endif /* _UPNPPDISC_H_X_INCLUDED_ */ diff --git a/src/db/upnp/Domain.cxx b/src/db/upnp/Domain.cxx deleted file mode 100644 index 010d4c7c2..000000000 --- a/src/db/upnp/Domain.cxx +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "Domain.hxx" -#include "util/Domain.hxx" - -const Domain upnp_domain("upnp"); diff --git a/src/db/upnp/Domain.hxx b/src/db/upnp/Domain.hxx deleted file mode 100644 index ec01ef735..000000000 --- a/src/db/upnp/Domain.hxx +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_DOMAIN_HXX -#define MPD_UPNP_DOMAIN_HXX - -class Domain; - -extern const Domain upnp_domain; - -#endif diff --git a/src/db/upnp/Object.cxx b/src/db/upnp/Object.cxx deleted file mode 100644 index 703fb0be4..000000000 --- a/src/db/upnp/Object.cxx +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "Object.hxx" - -UPnPDirObject::~UPnPDirObject() -{ - /* this destructor exists here just so it won't get inlined */ -} diff --git a/src/db/upnp/Object.hxx b/src/db/upnp/Object.hxx deleted file mode 100644 index 16a66c774..000000000 --- a/src/db/upnp/Object.hxx +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_OBJECT_HXX -#define MPD_UPNP_OBJECT_HXX - -#include "tag/Tag.hxx" - -#include - -/** - * UpnP Media Server directory entry, converted from XML data. - * - * This is a dumb data holder class, a struct with helpers. - */ -class UPnPDirObject { -public: - enum class Type { - UNKNOWN, - ITEM, - CONTAINER, - }; - - // There are actually several kinds of containers: - // object.container.storageFolder, object.container.person, - // object.container.playlistContainer etc., but they all seem to - // behave the same as far as we're concerned. Otoh, musicTrack - // items are special to us, and so should playlists, but I've not - // seen one of the latter yet (servers seem to use containers for - // playlists). - enum class ItemClass { - UNKNOWN, - MUSIC, - PLAYLIST, - }; - - std::string m_id; // ObjectId - std::string m_pid; // Parent ObjectId - std::string url; - - /** - * A copy of "dc:title" sanitized as a file name. - */ - std::string name; - - Type type; - ItemClass item_class; - - Tag tag; - - UPnPDirObject() = default; - UPnPDirObject(UPnPDirObject &&) = default; - - ~UPnPDirObject(); - - UPnPDirObject &operator=(UPnPDirObject &&) = default; - - void clear() - { - m_id.clear(); - m_pid.clear(); - url.clear(); - type = Type::UNKNOWN; - item_class = ItemClass::UNKNOWN; - tag.Clear(); - } -}; - -#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ diff --git a/src/db/upnp/Tags.cxx b/src/db/upnp/Tags.cxx deleted file mode 100644 index fd65df4d0..000000000 --- a/src/db/upnp/Tags.cxx +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "Tags.hxx" -#include "tag/TagTable.hxx" - -const struct tag_table upnp_tags[] = { - { "upnp:artist", TAG_ARTIST }, - { "upnp:album", TAG_ALBUM }, - { "upnp:originalTrackNumber", TAG_TRACK }, - { "upnp:genre", TAG_GENRE }, - { "dc:title", TAG_TITLE }, - - /* sentinel */ - { nullptr, TAG_NUM_OF_ITEM_TYPES } -}; diff --git a/src/db/upnp/Tags.hxx b/src/db/upnp/Tags.hxx deleted file mode 100644 index ec6d18478..000000000 --- a/src/db/upnp/Tags.hxx +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_TAGS_HXX -#define MPD_UPNP_TAGS_HXX - -/** - * Map UPnP property names to MPD tags. - */ -extern const struct tag_table upnp_tags[]; - -#endif diff --git a/src/db/upnp/Util.cxx b/src/db/upnp/Util.cxx deleted file mode 100644 index cf34a47d3..000000000 --- a/src/db/upnp/Util.cxx +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "Util.hxx" - -#include - -#include - -/** Get rid of white space at both ends */ -void -trimstring(std::string &s, const char *ws) -{ - auto pos = s.find_first_not_of(ws); - if (pos == std::string::npos) { - s.clear(); - return; - } - s.replace(0, pos, std::string()); - - pos = s.find_last_not_of(ws); - if (pos != std::string::npos && pos != s.length()-1) - s.replace(pos + 1, std::string::npos, std::string()); -} - -std::string -caturl(const std::string &s1, const std::string &s2) -{ - if (s2.front() == '/') { - /* absolute path: replace the whole URI path in s1 */ - - auto i = s1.find("://"); - if (i == s1.npos) - /* no scheme: override s1 completely */ - return s2; - - /* find the first slash after the host part */ - i = s1.find('/', i + 3); - if (i == s1.npos) - /* there's no URI path - simply append s2 */ - i = s1.length(); - - return s1.substr(0, i) + s2; - } - - std::string out(s1); - if (out.back() != '/') - out.push_back('/'); - - out += s2; - return out; -} - -static void -path_catslash(std::string &s) -{ - if (s.empty() || s.back() != '/') - s += '/'; -} - -std::string -path_getfather(const std::string &s) -{ - std::string father = s; - - // ?? - if (father.empty()) - return "./"; - - if (father.back() == '/') { - // Input ends with /. Strip it, handle special case for root - if (father.length() == 1) - return father; - father.erase(father.length()-1); - } - - auto slp = father.rfind('/'); - if (slp == std::string::npos) - return "./"; - - father.erase(slp); - path_catslash(father); - return father; -} - -std::list -stringToTokens(const std::string &str, - const char *delims, bool skipinit) -{ - std::list tokens; - - std::string::size_type startPos = 0; - - // Skip initial delims, return empty if this eats all. - if (skipinit && - (startPos = str.find_first_not_of(delims, 0)) == std::string::npos) - return tokens; - - while (startPos < str.size()) { - // Find next delimiter or end of string (end of token) - auto pos = str.find_first_of(delims, startPos); - - // Add token to the vector and adjust start - if (pos == std::string::npos) { - tokens.emplace_back(str, startPos); - break; - } else if (pos == startPos) { - // Dont' push empty tokens after first - if (tokens.empty()) - tokens.emplace_back(); - startPos = ++pos; - } else { - tokens.emplace_back(str, startPos, pos - startPos); - startPos = ++pos; - } - } - - return tokens; -} - -template -bool -csvToStrings(const char *s, T &tokens) -{ - assert(tokens.empty()); - - std::string current; - - while (true) { - char ch = *s++; - if (ch == 0) { - tokens.emplace_back(std::move(current)); - return true; - } - - if (ch == '\\') { - ch = *s++; - if (ch == 0) - return false; - } else if (ch == ',') { - tokens.emplace_back(std::move(current)); - current.clear(); - continue; - } - - current.push_back(ch); - } -} - -template bool csvToStrings>(const char *, std::list &); diff --git a/src/db/upnp/Util.hxx b/src/db/upnp/Util.hxx deleted file mode 100644 index 58e382faa..000000000 --- a/src/db/upnp/Util.hxx +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPNP_UTIL_HXX -#define MPD_UPNP_UTIL_HXX - -#include "Compiler.h" - -#include -#include - -std::string -caturl(const std::string& s1, const std::string& s2); - -void -trimstring(std::string &s, const char *ws = " \t\n"); - -std::string -path_getfather(const std::string &s); - -gcc_pure -std::list -stringToTokens(const std::string &str, - const char *delims = "/", bool skipinit = true); - -template -bool -csvToStrings(const char *s, T &tokens); - -#endif /* _UPNPP_H_X_INCLUDED_ */ diff --git a/src/db/upnp/WorkQueue.hxx b/src/db/upnp/WorkQueue.hxx deleted file mode 100644 index fe8ce53f9..000000000 --- a/src/db/upnp/WorkQueue.hxx +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef _WORKQUEUE_H_INCLUDED_ -#define _WORKQUEUE_H_INCLUDED_ - -#include "thread/Mutex.hxx" -#include "thread/Cond.hxx" - -#include -#include - -#include -#include - -#define LOGINFO(X) -#define LOGERR(X) - -/** - * A WorkQueue manages the synchronisation around a queue of work items, - * where a number of client threads queue tasks and a number of worker - * threads take and execute them. The goal is to introduce some level - * of parallelism between the successive steps of a previously single - * threaded pipeline. For example data extraction / data preparation / index - * update, but this could have other uses. - * - * There is no individual task status return. In case of fatal error, - * the client or worker sets an end condition on the queue. A second - * queue could conceivably be used for returning individual task - * status. - */ -template -class WorkQueue { - // Configuration - const std::string name; - - // Status - // Worker threads having called exit - unsigned n_workers_exited; - bool ok; - - unsigned n_threads; - pthread_t *threads; - - // Synchronization - std::queue queue; - Cond client_cond; - Cond worker_cond; - Mutex mutex; - -public: - /** Create a WorkQueue - * @param name for message printing - * @param hi number of tasks on queue before clients blocks. Default 0 - * meaning no limit. hi == -1 means that the queue is disabled. - * @param lo minimum count of tasks before worker starts. Default 1. - */ - WorkQueue(const char *_name) - :name(_name), - n_workers_exited(0), - ok(false), - n_threads(0), threads(nullptr) - { - } - - ~WorkQueue() { - setTerminateAndWait(); - } - - /** Start the worker threads. - * - * @param nworkers number of threads copies to start. - * @param start_routine thread function. It should loop - * taking (QueueWorker::take()) and executing tasks. - * @param arg initial parameter to thread function. - * @return true if ok. - */ - bool start(unsigned nworkers, void *(*workproc)(void *), void *arg) - { - const ScopeLock protect(mutex); - - assert(nworkers > 0); - assert(!ok); - assert(n_threads == 0); - assert(threads == nullptr); - - n_threads = nworkers; - threads = new pthread_t[n_threads]; - - for (unsigned i = 0; i < nworkers; i++) { - int err; - if ((err = pthread_create(&threads[i], 0, workproc, arg))) { - LOGERR(("WorkQueue:%s: pthread_create failed, err %d\n", - name.c_str(), err)); - return false; - } - } - - ok = true; - return true; - } - - /** Add item to work queue, called from client. - * - * Sleeps if there are already too many. - */ - template - bool put(U &&u) - { - const ScopeLock protect(mutex); - - queue.emplace(std::forward(u)); - - // Just wake one worker, there is only one new task. - worker_cond.signal(); - - return true; - } - - - /** Tell the workers to exit, and wait for them. - */ - void setTerminateAndWait() - { - const ScopeLock protect(mutex); - - // Wait for all worker threads to have called workerExit() - ok = false; - while (n_workers_exited < n_threads) { - worker_cond.broadcast(); - client_cond.wait(mutex); - } - - // Perform the thread joins and compute overall status - // Workers return (void*)1 if ok - for (unsigned i = 0; i < n_threads; ++i) { - void *status; - pthread_join(threads[i], &status); - } - - delete[] threads; - threads = nullptr; - n_threads = 0; - - // Reset to start state. - n_workers_exited = 0; - } - - /** Take task from queue. Called from worker. - * - * Sleeps if there are not enough. Signal if we go to sleep on empty - * queue: client may be waiting for our going idle. - */ - bool take(T &tp) - { - const ScopeLock protect(mutex); - - if (!ok) - return false; - - while (queue.empty()) { - worker_cond.wait(mutex); - if (!ok) - return false; - } - - tp = std::move(queue.front()); - queue.pop(); - return true; - } - - /** Advertise exit and abort queue. Called from worker - * - * This would happen after an unrecoverable error, or when - * the queue is terminated by the client. Workers never exit normally, - * except when the queue is shut down (at which point ok is set to - * false by the shutdown code anyway). The thread must return/exit - * immediately after calling this. - */ - void workerExit() - { - const ScopeLock protect(mutex); - - n_workers_exited++; - ok = false; - client_cond.broadcast(); - } -}; - -#endif /* _WORKQUEUE_H_INCLUDED_ */ diff --git a/src/db/upnp/ixmlwrap.cxx b/src/db/upnp/ixmlwrap.cxx deleted file mode 100644 index 6a2829cf9..000000000 --- a/src/db/upnp/ixmlwrap.cxx +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright (C) 2013 J.F.Dockes - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the - * Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include "ixmlwrap.hxx" - -namespace ixmlwrap { - -const char * -getFirstElementValue(IXML_Document *doc, const char *name) -{ - const char *ret = nullptr; - IXML_NodeList *nodes = - ixmlDocument_getElementsByTagName(doc, name); - - if (nodes) { - IXML_Node *first = ixmlNodeList_item(nodes, 0); - if (first) { - IXML_Node *dnode = ixmlNode_getFirstChild(first); - if (dnode) { - ret = ixmlNode_getNodeValue(dnode); - } - } - - ixmlNodeList_free(nodes); - } - - return ret; -} - -} diff --git a/src/db/upnp/ixmlwrap.hxx b/src/db/upnp/ixmlwrap.hxx deleted file mode 100644 index 0d519a323..000000000 --- a/src/db/upnp/ixmlwrap.hxx +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2013 J.F.Dockes - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the - * Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ -#ifndef _IXMLWRAP_H_INCLUDED_ -#define _IXMLWRAP_H_INCLUDED_ - -#include - -#include - -namespace ixmlwrap { - /** - * Retrieve the text content for the first element of given - * name. Returns nullptr if the element does not - * contain a text node - */ - const char *getFirstElementValue(IXML_Document *doc, - const char *name); - -}; - -#endif /* _IXMLWRAP_H_INCLUDED_ */ diff --git a/src/db/upnp/upnpplib.cxx b/src/db/upnp/upnpplib.cxx deleted file mode 100644 index 27b4e0564..000000000 --- a/src/db/upnp/upnpplib.cxx +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "upnpplib.hxx" -#include "Domain.hxx" -#include "Log.hxx" - -#include -#include - -static LibUPnP *theLib; - -LibUPnP::LibUPnP() -{ - auto code = UpnpInit(0, 0); - if (code != UPNP_E_SUCCESS) { - init_error.Format(upnp_domain, code, - "UpnpInit() failed: %s", - UpnpGetErrorMessage(code)); - return; - } - - UpnpSetMaxContentLength(2000*1024); - - code = UpnpRegisterClient(o_callback, (void *)this, &m_clh); - if (code != UPNP_E_SUCCESS) { - init_error.Format(upnp_domain, code, - "UpnpRegisterClient() failed: %s", - UpnpGetErrorMessage(code)); - return; - } - - // Servers sometimes make error (e.g.: minidlna returns bad utf-8) - ixmlRelaxParser(1); -} - -int -LibUPnP::o_callback(Upnp_EventType et, void* evp, void* cookie) -{ - LibUPnP *ulib = (LibUPnP *)cookie; - if (ulib == nullptr) { - // Because the asyncsearch calls uses a null cookie. - ulib = theLib; - } - - if (ulib->handler) - ulib->handler(et, evp); - - return UPNP_E_SUCCESS; -} - -LibUPnP::~LibUPnP() -{ - int error = UpnpFinish(); - if (error != UPNP_E_SUCCESS) - FormatError(upnp_domain, "UpnpFinish() failed: %s", - UpnpGetErrorMessage(error)); -} diff --git a/src/db/upnp/upnpplib.hxx b/src/db/upnp/upnpplib.hxx deleted file mode 100644 index 6759aa16d..000000000 --- a/src/db/upnp/upnpplib.hxx +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef _LIBUPNP_H_X_INCLUDED_ -#define _LIBUPNP_H_X_INCLUDED_ - -#include "util/Error.hxx" - -#include - -#include - -/** Our link to libupnp. Initialize and keep the handle around */ -class LibUPnP { - typedef std::function Handler; - - Error init_error; - UpnpClient_Handle m_clh; - - Handler handler; - - static int o_callback(Upnp_EventType, void *, void *); - -public: - LibUPnP(); - - LibUPnP(const LibUPnP &) = delete; - LibUPnP &operator=(const LibUPnP &) = delete; - - ~LibUPnP(); - - /** Check state after initialization */ - bool ok() const - { - return !init_error.IsDefined(); - } - - /** Retrieve init error if state not ok */ - const Error &GetInitError() const { - return init_error; - } - - template - void SetHandler(T &&_handler) { - handler = std::forward(_handler); - } - - UpnpClient_Handle getclh() - { - return m_clh; - } -}; - -#endif /* _LIBUPNP.H_X_INCLUDED_ */ diff --git a/src/playlist/PlaylistSong.cxx b/src/playlist/PlaylistSong.cxx index bcbdc30be..69f8762ab 100644 --- a/src/playlist/PlaylistSong.cxx +++ b/src/playlist/PlaylistSong.cxx @@ -20,7 +20,7 @@ #include "config.h" #include "PlaylistSong.hxx" #include "Mapper.hxx" -#include "DatabaseSong.hxx" +#include "db/DatabaseSong.hxx" #include "ls.hxx" #include "tag/Tag.hxx" #include "tag/TagBuilder.hxx" diff --git a/src/queue/QueueSave.cxx b/src/queue/QueueSave.cxx index 87de02c56..11c61aa9a 100644 --- a/src/queue/QueueSave.cxx +++ b/src/queue/QueueSave.cxx @@ -23,7 +23,7 @@ #include "PlaylistError.hxx" #include "DetachedSong.hxx" #include "SongSave.hxx" -#include "DatabaseSong.hxx" +#include "db/DatabaseSong.hxx" #include "fs/TextFile.hxx" #include "util/StringUtil.hxx" #include "util/UriUtil.hxx" diff --git a/src/sticker/SongSticker.cxx b/src/sticker/SongSticker.cxx index 55143d278..3431a1702 100644 --- a/src/sticker/SongSticker.cxx +++ b/src/sticker/SongSticker.cxx @@ -20,9 +20,9 @@ #include "config.h" #include "SongSticker.hxx" #include "StickerDatabase.hxx" -#include "LightSong.hxx" -#include "Song.hxx" -#include "Directory.hxx" +#include "db/LightSong.hxx" +#include "db/Song.hxx" +#include "db/Directory.hxx" #include diff --git a/src/update/InotifyDomain.cxx b/src/update/InotifyDomain.cxx deleted file mode 100644 index 4a3ab2d79..000000000 --- a/src/update/InotifyDomain.cxx +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "InotifyDomain.hxx" -#include "util/Domain.hxx" - -const Domain inotify_domain("inotify"); diff --git a/src/update/InotifyDomain.hxx b/src/update/InotifyDomain.hxx deleted file mode 100644 index ad6202361..000000000 --- a/src/update/InotifyDomain.hxx +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_DOMAIN_HXX -#define MPD_INOTIFY_DOMAIN_HXX - -extern const class Domain inotify_domain; - -#endif diff --git a/src/update/InotifyQueue.cxx b/src/update/InotifyQueue.cxx deleted file mode 100644 index f4bccf7ae..000000000 --- a/src/update/InotifyQueue.cxx +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "InotifyQueue.hxx" -#include "InotifyDomain.hxx" -#include "UpdateGlue.hxx" -#include "Log.hxx" - -#include - -/** - * Wait this long after the last change before calling - * update_enqueue(). This increases the probability that updates can - * be bundled. - */ -static constexpr unsigned INOTIFY_UPDATE_DELAY_S = 5; - -void -InotifyQueue::OnTimeout() -{ - unsigned id; - - while (!queue.empty()) { - const char *uri_utf8 = queue.front().c_str(); - - id = update_enqueue(uri_utf8, false); - if (id == 0) { - /* retry later */ - ScheduleSeconds(INOTIFY_UPDATE_DELAY_S); - return; - } - - FormatDebug(inotify_domain, "updating '%s' job=%u", - uri_utf8, id); - - queue.pop_front(); - } -} - -static bool -path_in(const char *path, const char *possible_parent) -{ - size_t length = strlen(possible_parent); - - return path[0] == 0 || - (memcmp(possible_parent, path, length) == 0 && - (path[length] == 0 || path[length] == '/')); -} - -void -InotifyQueue::Enqueue(const char *uri_utf8) -{ - ScheduleSeconds(INOTIFY_UPDATE_DELAY_S); - - for (auto i = queue.begin(), end = queue.end(); i != end;) { - const char *current_uri = i->c_str(); - - if (path_in(uri_utf8, current_uri)) - /* already enqueued */ - return; - - if (path_in(current_uri, uri_utf8)) - /* existing path is a sub-path of the new - path; we can dequeue the existing path and - update the new path instead */ - i = queue.erase(i); - else - ++i; - } - - queue.emplace_back(uri_utf8); -} diff --git a/src/update/InotifyQueue.hxx b/src/update/InotifyQueue.hxx deleted file mode 100644 index 99e2635b1..000000000 --- a/src/update/InotifyQueue.hxx +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_QUEUE_HXX -#define MPD_INOTIFY_QUEUE_HXX - -#include "event/TimeoutMonitor.hxx" -#include "Compiler.h" - -#include -#include - -class InotifyQueue final : private TimeoutMonitor { - std::list queue; - -public: - InotifyQueue(EventLoop &_loop):TimeoutMonitor(_loop) {} - - void Enqueue(const char *uri_utf8); - -private: - virtual void OnTimeout() override; -}; - -#endif diff --git a/src/update/InotifySource.cxx b/src/update/InotifySource.cxx deleted file mode 100644 index c2783690e..000000000 --- a/src/update/InotifySource.cxx +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "InotifySource.hxx" -#include "InotifyDomain.hxx" -#include "util/Error.hxx" -#include "system/fd_util.h" -#include "system/FatalError.hxx" -#include "Log.hxx" - -#include -#include -#include - -bool -InotifySource::OnSocketReady(gcc_unused unsigned flags) -{ - const auto dest = buffer.Write(); - if (dest.IsEmpty()) - FatalError("buffer full"); - - ssize_t nbytes = read(Get(), dest.data, dest.size); - if (nbytes < 0) - FatalSystemError("Failed to read from inotify"); - if (nbytes == 0) - FatalError("end of file from inotify"); - - buffer.Append(nbytes); - - while (true) { - const char *name; - - auto range = buffer.Read(); - const struct inotify_event *event = - (const struct inotify_event *) - range.data; - if (range.size < sizeof(*event) || - range.size < sizeof(*event) + event->len) - break; - - if (event->len > 0 && event->name[event->len - 1] == 0) - name = event->name; - else - name = nullptr; - - callback(event->wd, event->mask, name, callback_ctx); - buffer.Consume(sizeof(*event) + event->len); - } - - return true; -} - -inline -InotifySource::InotifySource(EventLoop &_loop, - mpd_inotify_callback_t _callback, void *_ctx, - int _fd) - :SocketMonitor(_fd, _loop), - callback(_callback), callback_ctx(_ctx) -{ - ScheduleRead(); - -} - -InotifySource * -InotifySource::Create(EventLoop &loop, - mpd_inotify_callback_t callback, void *callback_ctx, - Error &error) -{ - int fd = inotify_init_cloexec(); - if (fd < 0) { - error.SetErrno("inotify_init() has failed"); - return nullptr; - } - - return new InotifySource(loop, callback, callback_ctx, fd); -} - -int -InotifySource::Add(const char *path_fs, unsigned mask, Error &error) -{ - int wd = inotify_add_watch(Get(), path_fs, mask); - if (wd < 0) - error.SetErrno("inotify_add_watch() has failed"); - - return wd; -} - -void -InotifySource::Remove(unsigned wd) -{ - int ret = inotify_rm_watch(Get(), wd); - if (ret < 0 && errno != EINVAL) - LogErrno(inotify_domain, "inotify_rm_watch() has failed"); - - /* EINVAL may happen here when the file has been deleted; the - kernel seems to auto-unregister deleted files */ -} diff --git a/src/update/InotifySource.hxx b/src/update/InotifySource.hxx deleted file mode 100644 index 77c11093c..000000000 --- a/src/update/InotifySource.hxx +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_SOURCE_HXX -#define MPD_INOTIFY_SOURCE_HXX - -#include "event/SocketMonitor.hxx" -#include "util/FifoBuffer.hxx" - -class Error; - -typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask, - const char *name, void *ctx); - -class InotifySource final : private SocketMonitor { - mpd_inotify_callback_t callback; - void *callback_ctx; - - FifoBuffer buffer; - - InotifySource(EventLoop &_loop, - mpd_inotify_callback_t callback, void *ctx, int fd); - -public: - ~InotifySource() { - Close(); - } - - /** - * Creates a new inotify source and registers it in the GLib main - * loop. - * - * @param a callback invoked for events received from the kernel - */ - static InotifySource *Create(EventLoop &_loop, - mpd_inotify_callback_t callback, - void *ctx, - Error &error); - - /** - * Adds a path to the notify list. - * - * @return a watch descriptor or -1 on error - */ - int Add(const char *path_fs, unsigned mask, Error &error); - - /** - * Removes a path from the notify list. - * - * @param wd the watch descriptor returned by mpd_inotify_source_add() - */ - void Remove(unsigned wd); - -private: - virtual bool OnSocketReady(unsigned flags) override; -}; - -#endif diff --git a/src/update/InotifyUpdate.cxx b/src/update/InotifyUpdate.cxx deleted file mode 100644 index 7515990d7..000000000 --- a/src/update/InotifyUpdate.cxx +++ /dev/null @@ -1,339 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "InotifyUpdate.hxx" -#include "InotifySource.hxx" -#include "InotifyQueue.hxx" -#include "InotifyDomain.hxx" -#include "Mapper.hxx" -#include "Main.hxx" -#include "fs/AllocatedPath.hxx" -#include "fs/FileSystem.hxx" -#include "util/Error.hxx" -#include "Log.hxx" - -#include -#include -#include - -#include -#include -#include -#include -#include - -static constexpr unsigned IN_MASK = -#ifdef IN_ONLYDIR - IN_ONLYDIR| -#endif - IN_ATTRIB|IN_CLOSE_WRITE|IN_CREATE|IN_DELETE|IN_DELETE_SELF - |IN_MOVE|IN_MOVE_SELF; - -struct WatchDirectory { - WatchDirectory *parent; - - AllocatedPath name; - - int descriptor; - - std::forward_list children; - - template - WatchDirectory(WatchDirectory *_parent, N &&_name, - int _descriptor) - :parent(_parent), name(std::forward(_name)), - descriptor(_descriptor) {} - - WatchDirectory(const WatchDirectory &) = delete; - WatchDirectory &operator=(const WatchDirectory &) = delete; -}; - -static InotifySource *inotify_source; -static InotifyQueue *inotify_queue; - -static unsigned inotify_max_depth; -static WatchDirectory *inotify_root; -static std::map inotify_directories; - -static void -tree_add_watch_directory(WatchDirectory *directory) -{ - inotify_directories.insert(std::make_pair(directory->descriptor, - directory)); -} - -static void -tree_remove_watch_directory(WatchDirectory *directory) -{ - auto i = inotify_directories.find(directory->descriptor); - assert(i != inotify_directories.end()); - inotify_directories.erase(i); -} - -static WatchDirectory * -tree_find_watch_directory(int wd) -{ - auto i = inotify_directories.find(wd); - if (i == inotify_directories.end()) - return nullptr; - - return i->second; -} - -static void -disable_watch_directory(WatchDirectory &directory) -{ - tree_remove_watch_directory(&directory); - - for (WatchDirectory &child : directory.children) - disable_watch_directory(child); - - inotify_source->Remove(directory.descriptor); -} - -static void -remove_watch_directory(WatchDirectory *directory) -{ - assert(directory != nullptr); - - if (directory->parent == nullptr) { - LogWarning(inotify_domain, - "music directory was removed - " - "cannot continue to watch it"); - return; - } - - disable_watch_directory(*directory); - - /* remove it from the parent, which effectively deletes it */ - directory->parent->children.remove_if([directory](const WatchDirectory &child){ - return &child == directory; - }); -} - -static AllocatedPath -watch_directory_get_uri_fs(const WatchDirectory *directory) -{ - if (directory->parent == nullptr) - return AllocatedPath::Null(); - - const auto uri = watch_directory_get_uri_fs(directory->parent); - if (uri.IsNull()) - return directory->name; - - return AllocatedPath::Build(uri, directory->name); -} - -/* we don't look at "." / ".." nor files with newlines in their name */ -static bool skip_path(const char *path) -{ - return (path[0] == '.' && path[1] == 0) || - (path[0] == '.' && path[1] == '.' && path[2] == 0) || - strchr(path, '\n') != nullptr; -} - -static void -recursive_watch_subdirectories(WatchDirectory *directory, - const AllocatedPath &path_fs, unsigned depth) -{ - Error error; - DIR *dir; - struct dirent *ent; - - assert(directory != nullptr); - assert(depth <= inotify_max_depth); - assert(!path_fs.IsNull()); - - ++depth; - - if (depth > inotify_max_depth) - return; - - dir = opendir(path_fs.c_str()); - if (dir == nullptr) { - FormatErrno(inotify_domain, - "Failed to open directory %s", path_fs.c_str()); - return; - } - - while ((ent = readdir(dir))) { - struct stat st; - int ret; - - if (skip_path(ent->d_name)) - continue; - - const auto child_path_fs = - AllocatedPath::Build(path_fs, ent->d_name); - ret = StatFile(child_path_fs, st); - if (ret < 0) { - FormatErrno(inotify_domain, - "Failed to stat %s", - child_path_fs.c_str()); - continue; - } - - if (!S_ISDIR(st.st_mode)) - continue; - - ret = inotify_source->Add(child_path_fs.c_str(), IN_MASK, - error); - if (ret < 0) { - FormatError(error, - "Failed to register %s", - child_path_fs.c_str()); - error.Clear(); - continue; - } - - WatchDirectory *child = tree_find_watch_directory(ret); - if (child != nullptr) - /* already being watched */ - continue; - - directory->children.emplace_front(directory, - AllocatedPath::FromFS(ent->d_name), - ret); - child = &directory->children.front(); - - tree_add_watch_directory(child); - - recursive_watch_subdirectories(child, child_path_fs, depth); - } - - closedir(dir); -} - -gcc_pure -static unsigned -watch_directory_depth(const WatchDirectory *d) -{ - assert(d != nullptr); - - unsigned depth = 0; - while ((d = d->parent) != nullptr) - ++depth; - - return depth; -} - -static void -mpd_inotify_callback(int wd, unsigned mask, - gcc_unused const char *name, gcc_unused void *ctx) -{ - WatchDirectory *directory; - - /*FormatDebug(inotify_domain, "wd=%d mask=0x%x name='%s'", wd, mask, name);*/ - - directory = tree_find_watch_directory(wd); - if (directory == nullptr) - return; - - const auto uri_fs = watch_directory_get_uri_fs(directory); - - if ((mask & (IN_DELETE_SELF|IN_MOVE_SELF)) != 0) { - remove_watch_directory(directory); - return; - } - - if ((mask & (IN_ATTRIB|IN_CREATE|IN_MOVE)) != 0 && - (mask & IN_ISDIR) != 0) { - /* a sub directory was changed: register those in - inotify */ - const auto &root = mapper_get_music_directory_fs(); - - const auto path_fs = uri_fs.IsNull() - ? root - : AllocatedPath::Build(root, uri_fs.c_str()); - - recursive_watch_subdirectories(directory, path_fs, - watch_directory_depth(directory)); - } - - if ((mask & (IN_CLOSE_WRITE|IN_MOVE|IN_DELETE)) != 0 || - /* at the maximum depth, we watch out for newly created - directories */ - (watch_directory_depth(directory) == inotify_max_depth && - (mask & (IN_CREATE|IN_ISDIR)) == (IN_CREATE|IN_ISDIR))) { - /* a file was changed, or a directory was - moved/deleted: queue a database update */ - - if (!uri_fs.IsNull()) { - const std::string uri_utf8 = uri_fs.ToUTF8(); - if (!uri_utf8.empty()) - inotify_queue->Enqueue(uri_utf8.c_str()); - } - else - inotify_queue->Enqueue(""); - } -} - -void -mpd_inotify_init(unsigned max_depth) -{ - LogDebug(inotify_domain, "initializing inotify"); - - const auto &path = mapper_get_music_directory_fs(); - if (path.IsNull()) { - LogDebug(inotify_domain, "no music directory configured"); - return; - } - - Error error; - inotify_source = InotifySource::Create(*main_loop, - mpd_inotify_callback, nullptr, - error); - if (inotify_source == nullptr) { - LogError(error); - return; - } - - inotify_max_depth = max_depth; - - int descriptor = inotify_source->Add(path.c_str(), IN_MASK, error); - if (descriptor < 0) { - LogError(error); - delete inotify_source; - inotify_source = nullptr; - return; - } - - inotify_root = new WatchDirectory(nullptr, path, descriptor); - - tree_add_watch_directory(inotify_root); - - recursive_watch_subdirectories(inotify_root, path, 0); - - inotify_queue = new InotifyQueue(*main_loop); - - LogDebug(inotify_domain, "watching music directory"); -} - -void -mpd_inotify_finish(void) -{ - if (inotify_source == nullptr) - return; - - delete inotify_queue; - delete inotify_source; - delete inotify_root; - inotify_directories.clear(); -} diff --git a/src/update/InotifyUpdate.hxx b/src/update/InotifyUpdate.hxx deleted file mode 100644 index 2d7d4e3b4..000000000 --- a/src/update/InotifyUpdate.hxx +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_INOTIFY_UPDATE_HXX -#define MPD_INOTIFY_UPDATE_HXX - -#include "check.h" - -#ifdef HAVE_INOTIFY_INIT - -void -mpd_inotify_init(unsigned max_depth); - -void -mpd_inotify_finish(void); - -#else /* !HAVE_INOTIFY_INIT */ - -static inline void -mpd_inotify_init(gcc_unused unsigned max_depth) -{ -} - -static inline void -mpd_inotify_finish(void) -{ -} - -#endif /* !HAVE_INOTIFY_INIT */ - -#endif diff --git a/src/update/UpdateArchive.cxx b/src/update/UpdateArchive.cxx deleted file mode 100644 index ec46a4e17..000000000 --- a/src/update/UpdateArchive.cxx +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateArchive.hxx" -#include "UpdateInternal.hxx" -#include "UpdateDomain.hxx" -#include "DatabaseLock.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "Mapper.hxx" -#include "fs/AllocatedPath.hxx" -#include "archive/ArchiveList.hxx" -#include "archive/ArchivePlugin.hxx" -#include "archive/ArchiveFile.hxx" -#include "archive/ArchiveVisitor.hxx" -#include "util/Error.hxx" -#include "Log.hxx" - -#include - -#include - -static void -update_archive_tree(Directory &directory, const char *name) -{ - const char *tmp = strchr(name, '/'); - if (tmp) { - const std::string child_name(name, tmp); - //add dir is not there already - db_lock(); - Directory *subdir = - directory.MakeChild(child_name.c_str()); - subdir->device = DEVICE_INARCHIVE; - db_unlock(); - - //create directories first - update_archive_tree(*subdir, tmp+1); - } else { - if (strlen(name) == 0) { - LogWarning(update_domain, - "archive returned directory only"); - return; - } - - //add file - db_lock(); - Song *song = directory.FindSong(name); - db_unlock(); - if (song == nullptr) { - song = Song::LoadFile(name, directory); - if (song != nullptr) { - db_lock(); - directory.AddSong(song); - db_unlock(); - - modified = true; - FormatDefault(update_domain, "added %s/%s", - directory.GetPath(), name); - } - } - } -} - -/** - * Updates the file listing from an archive file. - * - * @param parent the parent directory the archive file resides in - * @param name the UTF-8 encoded base name of the archive file - * @param st stat() information on the archive file - * @param plugin the archive plugin which fits this archive type - */ -static void -update_archive_file2(Directory &parent, const char *name, - const struct stat *st, - const struct archive_plugin *plugin) -{ - db_lock(); - Directory *directory = parent.FindChild(name); - db_unlock(); - - if (directory != nullptr && directory->mtime == st->st_mtime && - !walk_discard) - /* MPD has already scanned the archive, and it hasn't - changed since - don't consider updating it */ - return; - - const auto path_fs = map_directory_child_fs(parent, name); - - /* open archive */ - Error error; - ArchiveFile *file = archive_file_open(plugin, path_fs.c_str(), error); - if (file == nullptr) { - LogError(error); - return; - } - - FormatDebug(update_domain, "archive %s opened", path_fs.c_str()); - - if (directory == nullptr) { - FormatDebug(update_domain, - "creating archive directory: %s", name); - db_lock(); - directory = parent.CreateChild(name); - /* mark this directory as archive (we use device for - this) */ - directory->device = DEVICE_INARCHIVE; - db_unlock(); - } - - directory->mtime = st->st_mtime; - - class UpdateArchiveVisitor final : public ArchiveVisitor { - Directory *directory; - - public: - UpdateArchiveVisitor(Directory *_directory) - :directory(_directory) {} - - virtual void VisitArchiveEntry(const char *path_utf8) override { - FormatDebug(update_domain, - "adding archive file: %s", path_utf8); - update_archive_tree(*directory, path_utf8); - } - }; - - UpdateArchiveVisitor visitor(directory); - file->Visit(visitor); - file->Close(); -} - -bool -update_archive_file(Directory &directory, - const char *name, const char *suffix, - const struct stat *st) -{ -#ifdef ENABLE_ARCHIVE - const struct archive_plugin *plugin = - archive_plugin_from_suffix(suffix); - if (plugin == nullptr) - return false; - - update_archive_file2(directory, name, st, plugin); - return true; -#else - (void)directory; - (void)name; - (void)suffix; - (void)st; - - return false; -#endif -} diff --git a/src/update/UpdateArchive.hxx b/src/update/UpdateArchive.hxx deleted file mode 100644 index 1fc9af349..000000000 --- a/src/update/UpdateArchive.hxx +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_ARCHIVE_HXX -#define MPD_UPDATE_ARCHIVE_HXX - -#include "check.h" -#include "Compiler.h" - -#include - -struct Directory; - -#ifdef ENABLE_ARCHIVE - -bool -update_archive_file(Directory &directory, - const char *name, const char *suffix, - const struct stat *st); - -#else - -static inline bool -update_archive_file(gcc_unused Directory &directory, - gcc_unused const char *name, - gcc_unused const char *suffix, - gcc_unused const struct stat *st) -{ - return false; -} - -#endif - -#endif diff --git a/src/update/UpdateContainer.cxx b/src/update/UpdateContainer.cxx deleted file mode 100644 index 0417aa999..000000000 --- a/src/update/UpdateContainer.cxx +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateContainer.hxx" -#include "UpdateInternal.hxx" -#include "UpdateDatabase.hxx" -#include "UpdateDomain.hxx" -#include "DatabaseLock.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "decoder/DecoderPlugin.hxx" -#include "decoder/DecoderList.hxx" -#include "Mapper.hxx" -#include "fs/AllocatedPath.hxx" -#include "tag/TagHandler.hxx" -#include "tag/TagBuilder.hxx" -#include "Log.hxx" - -#include - -/** - * Create the specified directory object if it does not exist already - * or if the #stat object indicates that it has been modified since - * the last update. Returns nullptr when it exists already and is - * unmodified. - * - * The caller must lock the database. - */ -static Directory * -make_directory_if_modified(Directory &parent, const char *name, - const struct stat *st) -{ - Directory *directory = parent.FindChild(name); - - // directory exists already - if (directory != nullptr) { - if (directory->mtime == st->st_mtime && !walk_discard) { - /* not modified */ - return nullptr; - } - - delete_directory(directory); - modified = true; - } - - directory = parent.MakeChild(name); - directory->mtime = st->st_mtime; - return directory; -} - -static bool -SupportsContainerSuffix(const DecoderPlugin &plugin, const char *suffix) -{ - return plugin.container_scan != nullptr && - plugin.SupportsSuffix(suffix); -} - -bool -update_container_file(Directory &directory, - const char *name, - const struct stat *st, - const char *suffix) -{ - const DecoderPlugin *_plugin = decoder_plugins_find([suffix](const DecoderPlugin &plugin){ - return SupportsContainerSuffix(plugin, suffix); - }); - if (_plugin == nullptr) - return false; - const DecoderPlugin &plugin = *_plugin; - - db_lock(); - Directory *contdir = make_directory_if_modified(directory, name, st); - if (contdir == nullptr) { - /* not modified */ - db_unlock(); - return true; - } - - contdir->device = DEVICE_CONTAINER; - db_unlock(); - - const auto pathname = map_directory_child_fs(directory, name); - - char *vtrack; - unsigned int tnum = 0; - TagBuilder tag_builder; - while ((vtrack = plugin.container_scan(pathname.c_str(), ++tnum)) != nullptr) { - Song *song = Song::NewFile(vtrack, *contdir); - - // shouldn't be necessary but it's there.. - song->mtime = st->st_mtime; - - const auto child_path_fs = - map_directory_child_fs(*contdir, vtrack); - - plugin.ScanFile(child_path_fs.c_str(), - add_tag_handler, &tag_builder); - - tag_builder.Commit(song->tag); - - db_lock(); - contdir->AddSong(song); - db_unlock(); - - modified = true; - - FormatDefault(update_domain, "added %s/%s", - directory.GetPath(), vtrack); - g_free(vtrack); - } - - if (tnum == 1) { - db_lock(); - delete_directory(contdir); - db_unlock(); - return false; - } else - return true; -} diff --git a/src/update/UpdateContainer.hxx b/src/update/UpdateContainer.hxx deleted file mode 100644 index 8125f71ee..000000000 --- a/src/update/UpdateContainer.hxx +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_CONTAINER_HXX -#define MPD_UPDATE_CONTAINER_HXX - -#include "check.h" - -#include - -struct Directory; -struct DecoderPlugin; - -bool -update_container_file(Directory &directory, - const char *name, - const struct stat *st, - const char *suffix); - -#endif diff --git a/src/update/UpdateDatabase.cxx b/src/update/UpdateDatabase.cxx deleted file mode 100644 index fe49a90fb..000000000 --- a/src/update/UpdateDatabase.cxx +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateDatabase.hxx" -#include "UpdateRemove.hxx" -#include "PlaylistVector.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "DatabaseLock.hxx" - -#include -#include - -void -delete_song(Directory &dir, Song *del) -{ - assert(del->parent == &dir); - - /* first, prevent traversers in main task from getting this */ - dir.RemoveSong(del); - - db_unlock(); /* temporary unlock, because update_remove_song() blocks */ - - /* now take it out of the playlist (in the main_task) */ - update_remove_song(del); - - /* finally, all possible references gone, free it */ - del->Free(); - - db_lock(); -} - -/** - * Recursively remove all sub directories and songs from a directory, - * leaving an empty directory. - * - * Caller must lock the #db_mutex. - */ -static void -clear_directory(Directory &directory) -{ - Directory *child, *n; - directory_for_each_child_safe(child, n, directory) - delete_directory(child); - - Song *song, *ns; - directory_for_each_song_safe(song, ns, directory) { - assert(song->parent == &directory); - delete_song(directory, song); - } -} - -void -delete_directory(Directory *directory) -{ - assert(directory->parent != nullptr); - - clear_directory(*directory); - - directory->Delete(); -} - -bool -delete_name_in(Directory &parent, const char *name) -{ - bool modified = false; - - db_lock(); - Directory *directory = parent.FindChild(name); - - if (directory != nullptr) { - delete_directory(directory); - modified = true; - } - - Song *song = parent.FindSong(name); - if (song != nullptr) { - delete_song(parent, song); - modified = true; - } - - parent.playlists.erase(name); - - db_unlock(); - - return modified; -} diff --git a/src/update/UpdateDatabase.hxx b/src/update/UpdateDatabase.hxx deleted file mode 100644 index bd7c395f2..000000000 --- a/src/update/UpdateDatabase.hxx +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_DATABASE_HXX -#define MPD_UPDATE_DATABASE_HXX - -#include "check.h" - -struct Directory; -struct Song; - -/** - * Caller must lock the #db_mutex. - */ -void -delete_song(Directory &parent, Song *song); - -/** - * Recursively free a directory and all its contents. - * - * Caller must lock the #db_mutex. - */ -void -delete_directory(Directory *directory); - -/** - * Caller must NOT lock the #db_mutex. - * - * @return true if the database was modified - */ -bool -delete_name_in(Directory &parent, const char *name); - -#endif diff --git a/src/update/UpdateDomain.cxx b/src/update/UpdateDomain.cxx deleted file mode 100644 index 80ad4fd22..000000000 --- a/src/update/UpdateDomain.cxx +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "UpdateDomain.hxx" -#include "util/Domain.hxx" - -const Domain update_domain("update"); diff --git a/src/update/UpdateDomain.hxx b/src/update/UpdateDomain.hxx deleted file mode 100644 index a6e994390..000000000 --- a/src/update/UpdateDomain.hxx +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_DOMAIN_HXX -#define MPD_UPDATE_DOMAIN_HXX - -extern const class Domain update_domain; - -#endif diff --git a/src/update/UpdateGlue.cxx b/src/update/UpdateGlue.cxx deleted file mode 100644 index 29e5f3ca7..000000000 --- a/src/update/UpdateGlue.cxx +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "UpdateGlue.hxx" -#include "UpdateQueue.hxx" -#include "UpdateWalk.hxx" -#include "UpdateRemove.hxx" -#include "UpdateDomain.hxx" -#include "Mapper.hxx" -#include "DatabaseSimple.hxx" -#include "Idle.hxx" -#include "GlobalEvents.hxx" -#include "util/Error.hxx" -#include "Log.hxx" -#include "Main.hxx" -#include "Instance.hxx" -#include "system/FatalError.hxx" -#include "thread/Id.hxx" -#include "thread/Thread.hxx" -#include "thread/Util.hxx" - -#include - -static enum update_progress { - UPDATE_PROGRESS_IDLE = 0, - UPDATE_PROGRESS_RUNNING = 1, - UPDATE_PROGRESS_DONE = 2 -} progress; - -static bool modified; - -static Thread update_thread; - -static const unsigned update_task_id_max = 1 << 15; - -static unsigned update_task_id; - -static UpdateQueueItem next; - -unsigned -isUpdatingDB(void) -{ - return next.id; -} - -static void -update_task(gcc_unused void *ctx) -{ - if (!next.path_utf8.empty()) - FormatDebug(update_domain, "starting: %s", - next.path_utf8.c_str()); - else - LogDebug(update_domain, "starting"); - - SetThreadIdlePriority(); - - modified = update_walk(next.path_utf8.c_str(), next.discard); - - if (modified || !db_exists()) { - Error error; - if (!db_save(error)) - LogError(error, "Failed to save database"); - } - - if (!next.path_utf8.empty()) - FormatDebug(update_domain, "finished: %s", - next.path_utf8.c_str()); - else - LogDebug(update_domain, "finished"); - - progress = UPDATE_PROGRESS_DONE; - GlobalEvents::Emit(GlobalEvents::UPDATE); -} - -static void -spawn_update_task(UpdateQueueItem &&i) -{ - assert(main_thread.IsInside()); - - progress = UPDATE_PROGRESS_RUNNING; - modified = false; - - next = std::move(i); - - Error error; - if (!update_thread.Start(update_task, nullptr, error)) - FatalError(error); - - FormatDebug(update_domain, - "spawned thread for update job id %i", next.id); -} - -static unsigned -generate_update_id() -{ - unsigned id = update_task_id + 1; - if (id > update_task_id_max) - id = 1; - return id; -} - -unsigned -update_enqueue(const char *path, bool discard) -{ - assert(main_thread.IsInside()); - - if (!db_is_simple() || !mapper_has_music_directory()) - return 0; - - if (progress != UPDATE_PROGRESS_IDLE) { - const unsigned id = generate_update_id(); - if (!update_queue_push(path, discard, id)) - return 0; - - update_task_id = id; - return id; - } - - const unsigned id = update_task_id = generate_update_id(); - spawn_update_task(UpdateQueueItem(path, discard, id)); - - idle_add(IDLE_UPDATE); - - return id; -} - -/** - * Called in the main thread after the database update is finished. - */ -static void update_finished_event(void) -{ - assert(progress == UPDATE_PROGRESS_DONE); - assert(next.IsDefined()); - - update_thread.Join(); - next = UpdateQueueItem(); - - idle_add(IDLE_UPDATE); - - if (modified) - /* send "idle" events */ - instance->DatabaseModified(); - - auto i = update_queue_shift(); - if (i.IsDefined()) { - /* schedule the next path */ - spawn_update_task(std::move(i)); - } else { - progress = UPDATE_PROGRESS_IDLE; - } -} - -void update_global_init(void) -{ - GlobalEvents::Register(GlobalEvents::UPDATE, update_finished_event); - - update_remove_global_init(); - update_walk_global_init(); -} - -void update_global_finish(void) -{ - update_walk_global_finish(); -} diff --git a/src/update/UpdateGlue.hxx b/src/update/UpdateGlue.hxx deleted file mode 100644 index 6e247414e..000000000 --- a/src/update/UpdateGlue.hxx +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_GLUE_HXX -#define MPD_UPDATE_GLUE_HXX - -#include "Compiler.h" - -void update_global_init(void); - -void update_global_finish(void); - -unsigned -isUpdatingDB(void); - -/** - * Add this path to the database update queue. - * - * @param path a path to update; if an empty string, - * the whole music directory is updated - * @return the job id, or 0 on error - */ -gcc_nonnull_all -unsigned -update_enqueue(const char *path, bool discard); - -#endif diff --git a/src/update/UpdateIO.cxx b/src/update/UpdateIO.cxx deleted file mode 100644 index 8e4406dc8..000000000 --- a/src/update/UpdateIO.cxx +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateIO.hxx" -#include "UpdateDomain.hxx" -#include "Directory.hxx" -#include "Mapper.hxx" -#include "fs/AllocatedPath.hxx" -#include "fs/FileSystem.hxx" -#include "Log.hxx" - -#include -#include - -int -stat_directory(const Directory &directory, struct stat *st) -{ - const auto path_fs = map_directory_fs(directory); - if (path_fs.IsNull()) - return -1; - - if (!StatFile(path_fs, *st)) { - int error = errno; - const std::string path_utf8 = path_fs.ToUTF8(); - FormatErrno(update_domain, error, - "Failed to stat %s", path_utf8.c_str()); - return -1; - } - - return 0; -} - -int -stat_directory_child(const Directory &parent, const char *name, - struct stat *st) -{ - const auto path_fs = map_directory_child_fs(parent, name); - if (path_fs.IsNull()) - return -1; - - if (!StatFile(path_fs, *st)) { - int error = errno; - const std::string path_utf8 = path_fs.ToUTF8(); - FormatErrno(update_domain, error, - "Failed to stat %s", path_utf8.c_str()); - return -1; - } - - return 0; -} - -bool -directory_exists(const Directory &directory) -{ - const auto path_fs = map_directory_fs(directory); - if (path_fs.IsNull()) - /* invalid path: cannot exist */ - return false; - - return directory.device == DEVICE_INARCHIVE || - directory.device == DEVICE_CONTAINER - ? FileExists(path_fs) - : DirectoryExists(path_fs); -} - -bool -directory_child_is_regular(const Directory &directory, - const char *name_utf8) -{ - const auto path_fs = map_directory_child_fs(directory, name_utf8); - if (path_fs.IsNull()) - return false; - - return FileExists(path_fs); -} - -bool -directory_child_access(const Directory &directory, - const char *name, int mode) -{ -#ifdef WIN32 - /* CheckAccess() is useless on WIN32 */ - (void)directory; - (void)name; - (void)mode; - return true; -#else - const auto path = map_directory_child_fs(directory, name); - if (path.IsNull()) - /* something went wrong, but that isn't a permission - problem */ - return true; - - return CheckAccess(path, mode) || errno != EACCES; -#endif -} diff --git a/src/update/UpdateIO.hxx b/src/update/UpdateIO.hxx deleted file mode 100644 index 819879422..000000000 --- a/src/update/UpdateIO.hxx +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_IO_HXX -#define MPD_UPDATE_IO_HXX - -#include "check.h" - -#include - -struct Directory; - -int -stat_directory(const Directory &directory, struct stat *st); - -int -stat_directory_child(const Directory &parent, const char *name, - struct stat *st); - -bool -directory_exists(const Directory &directory); - -bool -directory_child_is_regular(const Directory &directory, - const char *name_utf8); - -/** - * Checks if the given permissions on the mapped file are given. - */ -bool -directory_child_access(const Directory &directory, - const char *name, int mode); - -#endif diff --git a/src/update/UpdateInternal.hxx b/src/update/UpdateInternal.hxx deleted file mode 100644 index 2e373bd06..000000000 --- a/src/update/UpdateInternal.hxx +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_INTERNAL_H -#define MPD_UPDATE_INTERNAL_H - -#include "check.h" - -extern bool walk_discard; -extern bool modified; - -#endif diff --git a/src/update/UpdateQueue.cxx b/src/update/UpdateQueue.cxx deleted file mode 100644 index a6002f854..000000000 --- a/src/update/UpdateQueue.cxx +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" -#include "UpdateQueue.hxx" - -#include -#include - -static constexpr unsigned MAX_UPDATE_QUEUE_SIZE = 32; - -static std::queue> update_queue; - -bool -update_queue_push(const char *path, bool discard, unsigned id) -{ - if (update_queue.size() >= MAX_UPDATE_QUEUE_SIZE) - return false; - - update_queue.emplace(path, discard, id); - return true; -} - -UpdateQueueItem -update_queue_shift() -{ - if (update_queue.empty()) - return UpdateQueueItem(); - - auto i = std::move(update_queue.front()); - update_queue.pop(); - return i; -} diff --git a/src/update/UpdateQueue.hxx b/src/update/UpdateQueue.hxx deleted file mode 100644 index e4228f5ed..000000000 --- a/src/update/UpdateQueue.hxx +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_QUEUE_HXX -#define MPD_UPDATE_QUEUE_HXX - -#include "check.h" - -#include - -struct UpdateQueueItem { - std::string path_utf8; - unsigned id; - bool discard; - - UpdateQueueItem():id(0) {} - UpdateQueueItem(const char *_path, bool _discard, - unsigned _id) - :path_utf8(_path), id(_id), discard(_discard) {} - - bool IsDefined() const { - return id != 0; - } -}; - -bool -update_queue_push(const char *path, bool discard, unsigned id); - -UpdateQueueItem -update_queue_shift(); - -#endif diff --git a/src/update/UpdateRemove.cxx b/src/update/UpdateRemove.cxx deleted file mode 100644 index 30898b00b..000000000 --- a/src/update/UpdateRemove.cxx +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateRemove.hxx" -#include "UpdateDomain.hxx" -#include "GlobalEvents.hxx" -#include "thread/Mutex.hxx" -#include "thread/Cond.hxx" -#include "Song.hxx" -#include "LightSong.hxx" -#include "Main.hxx" -#include "Instance.hxx" -#include "Log.hxx" - -#ifdef ENABLE_SQLITE -#include "sticker/StickerDatabase.hxx" -#include "sticker/SongSticker.hxx" -#endif - -#include - -static const Song *removed_song; - -static Mutex remove_mutex; -static Cond remove_cond; - -/** - * Safely remove a song from the database. This must be done in the - * main task, to be sure that there is no pointer left to it. - */ -static void -song_remove_event(void) -{ - assert(removed_song != nullptr); - - { - const auto uri = removed_song->GetURI(); - FormatDefault(update_domain, "removing %s", uri.c_str()); - } - -#ifdef ENABLE_SQLITE - /* if the song has a sticker, remove it */ - if (sticker_enabled()) - sticker_song_delete(removed_song->Export()); -#endif - - { - const auto uri = removed_song->GetURI(); - instance->DeleteSong(uri.c_str()); - } - - /* clear "removed_song" and send signal to update thread */ - remove_mutex.lock(); - removed_song = nullptr; - remove_cond.signal(); - remove_mutex.unlock(); -} - -void -update_remove_global_init(void) -{ - GlobalEvents::Register(GlobalEvents::DELETE, song_remove_event); -} - -void -update_remove_song(const Song *song) -{ - assert(removed_song == nullptr); - - removed_song = song; - - GlobalEvents::Emit(GlobalEvents::DELETE); - - remove_mutex.lock(); - - while (removed_song != nullptr) - remove_cond.wait(remove_mutex); - - remove_mutex.unlock(); -} diff --git a/src/update/UpdateRemove.hxx b/src/update/UpdateRemove.hxx deleted file mode 100644 index d54e3aa80..000000000 --- a/src/update/UpdateRemove.hxx +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_REMOVE_HXX -#define MPD_UPDATE_REMOVE_HXX - -#include "check.h" - -struct Song; - -void -update_remove_global_init(void); - -/** - * Sends a signal to the main thread which will in turn remove the - * song: from the sticker database and from the playlist. This - * serialized access is implemented to avoid excessive locking. - */ -void -update_remove_song(const Song *song); - -#endif diff --git a/src/update/UpdateSong.cxx b/src/update/UpdateSong.cxx deleted file mode 100644 index 2db85a674..000000000 --- a/src/update/UpdateSong.cxx +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateSong.hxx" -#include "UpdateInternal.hxx" -#include "UpdateIO.hxx" -#include "UpdateDatabase.hxx" -#include "UpdateContainer.hxx" -#include "UpdateDomain.hxx" -#include "DatabaseLock.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "decoder/DecoderList.hxx" -#include "Log.hxx" - -#include - -static void -update_song_file2(Directory &directory, - const char *name, const struct stat *st, - const char *suffix) -{ - db_lock(); - Song *song = directory.FindSong(name); - db_unlock(); - - if (!directory_child_access(directory, name, R_OK)) { - FormatError(update_domain, - "no read permissions on %s/%s", - directory.GetPath(), name); - if (song != nullptr) { - db_lock(); - delete_song(directory, song); - db_unlock(); - } - - return; - } - - if (!(song != nullptr && st->st_mtime == song->mtime && - !walk_discard) && - update_container_file(directory, name, st, suffix)) { - if (song != nullptr) { - db_lock(); - delete_song(directory, song); - db_unlock(); - } - - return; - } - - if (song == nullptr) { - FormatDebug(update_domain, "reading %s/%s", - directory.GetPath(), name); - song = Song::LoadFile(name, directory); - if (song == nullptr) { - FormatDebug(update_domain, - "ignoring unrecognized file %s/%s", - directory.GetPath(), name); - return; - } - - db_lock(); - directory.AddSong(song); - db_unlock(); - - modified = true; - FormatDefault(update_domain, "added %s/%s", - directory.GetPath(), name); - } else if (st->st_mtime != song->mtime || walk_discard) { - FormatDefault(update_domain, "updating %s/%s", - directory.GetPath(), name); - if (!song->UpdateFile()) { - FormatDebug(update_domain, - "deleting unrecognized file %s/%s", - directory.GetPath(), name); - db_lock(); - delete_song(directory, song); - db_unlock(); - } - - modified = true; - } -} - -bool -update_song_file(Directory &directory, - const char *name, const char *suffix, - const struct stat *st) -{ - if (!decoder_plugins_supports_suffix(suffix)) - return false; - - update_song_file2(directory, name, st, suffix); - return true; -} diff --git a/src/update/UpdateSong.hxx b/src/update/UpdateSong.hxx deleted file mode 100644 index 5feb01928..000000000 --- a/src/update/UpdateSong.hxx +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_SONG_HXX -#define MPD_UPDATE_SONG_HXX - -#include "check.h" - -#include - -struct Directory; - -bool -update_song_file(Directory &directory, - const char *name, const char *suffix, - const struct stat *st); - -#endif diff --git a/src/update/UpdateWalk.cxx b/src/update/UpdateWalk.cxx deleted file mode 100644 index 21754b31d..000000000 --- a/src/update/UpdateWalk.cxx +++ /dev/null @@ -1,484 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "config.h" /* must be first for large file support */ -#include "UpdateWalk.hxx" -#include "UpdateIO.hxx" -#include "UpdateDatabase.hxx" -#include "UpdateSong.hxx" -#include "UpdateArchive.hxx" -#include "UpdateDomain.hxx" -#include "DatabaseLock.hxx" -#include "DatabaseSimple.hxx" -#include "Directory.hxx" -#include "Song.hxx" -#include "PlaylistVector.hxx" -#include "playlist/PlaylistRegistry.hxx" -#include "Mapper.hxx" -#include "ExcludeList.hxx" -#include "config/ConfigGlobal.hxx" -#include "config/ConfigOption.hxx" -#include "fs/AllocatedPath.hxx" -#include "fs/Traits.hxx" -#include "fs/FileSystem.hxx" -#include "fs/DirectoryReader.hxx" -#include "util/Alloc.hxx" -#include "util/UriUtil.hxx" -#include "Log.hxx" - -#include -#include -#include -#include -#include - -bool walk_discard; -bool modified; - -#ifndef WIN32 - -static constexpr bool DEFAULT_FOLLOW_INSIDE_SYMLINKS = true; -static constexpr bool DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true; - -static bool follow_inside_symlinks; -static bool follow_outside_symlinks; - -#endif - -void -update_walk_global_init(void) -{ -#ifndef WIN32 - follow_inside_symlinks = - config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS, - DEFAULT_FOLLOW_INSIDE_SYMLINKS); - - follow_outside_symlinks = - config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS, - DEFAULT_FOLLOW_OUTSIDE_SYMLINKS); -#endif -} - -void -update_walk_global_finish(void) -{ -} - -static void -directory_set_stat(Directory &dir, const struct stat *st) -{ - dir.inode = st->st_ino; - dir.device = st->st_dev; - dir.have_stat = true; -} - -static void -remove_excluded_from_directory(Directory &directory, - const ExcludeList &exclude_list) -{ - db_lock(); - - Directory *child, *n; - directory_for_each_child_safe(child, n, directory) { - const auto name_fs = AllocatedPath::FromUTF8(child->GetName()); - - if (name_fs.IsNull() || exclude_list.Check(name_fs)) { - delete_directory(child); - modified = true; - } - } - - Song *song, *ns; - directory_for_each_song_safe(song, ns, directory) { - assert(song->parent == &directory); - - const auto name_fs = AllocatedPath::FromUTF8(song->uri); - if (name_fs.IsNull() || exclude_list.Check(name_fs)) { - delete_song(directory, song); - modified = true; - } - } - - db_unlock(); -} - -static void -purge_deleted_from_directory(Directory &directory) -{ - Directory *child, *n; - directory_for_each_child_safe(child, n, directory) { - if (directory_exists(*child)) - continue; - - db_lock(); - delete_directory(child); - db_unlock(); - - modified = true; - } - - Song *song, *ns; - directory_for_each_song_safe(song, ns, directory) { - const auto path = map_song_fs(*song); - if (path.IsNull() || !FileExists(path)) { - db_lock(); - delete_song(directory, song); - db_unlock(); - - modified = true; - } - } - - for (auto i = directory.playlists.begin(), - end = directory.playlists.end(); - i != end;) { - if (!directory_child_is_regular(directory, i->name.c_str())) { - db_lock(); - i = directory.playlists.erase(i); - db_unlock(); - } else - ++i; - } -} - -#ifndef WIN32 -static int -update_directory_stat(Directory &directory) -{ - struct stat st; - if (stat_directory(directory, &st) < 0) - return -1; - - directory_set_stat(directory, &st); - return 0; -} -#endif - -static int -find_inode_ancestor(Directory *parent, ino_t inode, dev_t device) -{ -#ifndef WIN32 - while (parent) { - if (!parent->have_stat && update_directory_stat(*parent) < 0) - return -1; - - if (parent->inode == inode && parent->device == device) { - LogDebug(update_domain, "recursive directory found"); - return 1; - } - - parent = parent->parent; - } -#else - (void)parent; - (void)inode; - (void)device; -#endif - - return 0; -} - -static bool -update_playlist_file2(Directory &directory, - const char *name, const char *suffix, - const struct stat *st) -{ - if (!playlist_suffix_supported(suffix)) - return false; - - PlaylistInfo pi(name, st->st_mtime); - - db_lock(); - if (directory.playlists.UpdateOrInsert(std::move(pi))) - modified = true; - db_unlock(); - return true; -} - -static bool -update_regular_file(Directory &directory, - const char *name, const struct stat *st) -{ - const char *suffix = uri_get_suffix(name); - if (suffix == nullptr) - return false; - - return update_song_file(directory, name, suffix, st) || - update_archive_file(directory, name, suffix, st) || - update_playlist_file2(directory, name, suffix, st); -} - -static bool -update_directory(Directory &directory, const struct stat *st); - -static void -update_directory_child(Directory &directory, - const char *name, const struct stat *st) -{ - assert(strchr(name, '/') == nullptr); - - if (S_ISREG(st->st_mode)) { - update_regular_file(directory, name, st); - } else if (S_ISDIR(st->st_mode)) { - if (find_inode_ancestor(&directory, st->st_ino, st->st_dev)) - return; - - db_lock(); - Directory *subdir = directory.MakeChild(name); - db_unlock(); - - assert(&directory == subdir->parent); - - if (!update_directory(*subdir, st)) { - db_lock(); - delete_directory(subdir); - db_unlock(); - } - } else { - FormatDebug(update_domain, - "%s is not a directory, archive or music", name); - } -} - -/* we don't look at "." / ".." nor files with newlines in their name */ -gcc_pure -static bool skip_path(Path path_fs) -{ - const char *path = path_fs.c_str(); - return (path[0] == '.' && path[1] == 0) || - (path[0] == '.' && path[1] == '.' && path[2] == 0) || - strchr(path, '\n') != nullptr; -} - -gcc_pure -static bool -skip_symlink(const Directory *directory, const char *utf8_name) -{ -#ifndef WIN32 - const auto path_fs = map_directory_child_fs(*directory, utf8_name); - if (path_fs.IsNull()) - return true; - - const auto target = ReadLink(path_fs); - if (target.IsNull()) - /* don't skip if this is not a symlink */ - return errno != EINVAL; - - if (!follow_inside_symlinks && !follow_outside_symlinks) { - /* ignore all symlinks */ - return true; - } else if (follow_inside_symlinks && follow_outside_symlinks) { - /* consider all symlinks */ - return false; - } - - const char *target_str = target.c_str(); - - if (PathTraitsFS::IsAbsolute(target_str)) { - /* if the symlink points to an absolute path, see if - that path is inside the music directory */ - const char *relative = map_to_relative_path(target_str); - return relative > target_str - ? !follow_inside_symlinks - : !follow_outside_symlinks; - } - - const char *p = target_str; - while (*p == '.') { - if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) { - /* "../" moves to parent directory */ - directory = directory->parent; - if (directory == nullptr) { - /* we have moved outside the music - directory - skip this symlink - if such symlinks are not allowed */ - return !follow_outside_symlinks; - } - p += 3; - } else if (PathTraitsFS::IsSeparator(p[1])) - /* eliminate "./" */ - p += 2; - else - break; - } - - /* we are still in the music directory, so this symlink points - to a song which is already in the database - skip according - to the follow_inside_symlinks param*/ - return !follow_inside_symlinks; -#else - /* no symlink checking on WIN32 */ - - (void)directory; - (void)utf8_name; - - return false; -#endif -} - -static bool -update_directory(Directory &directory, const struct stat *st) -{ - assert(S_ISDIR(st->st_mode)); - - directory_set_stat(directory, st); - - const auto path_fs = map_directory_fs(directory); - if (path_fs.IsNull()) - return false; - - DirectoryReader reader(path_fs); - if (reader.HasFailed()) { - int error = errno; - const auto path_utf8 = path_fs.ToUTF8(); - FormatErrno(update_domain, error, - "Failed to open directory %s", - path_utf8.c_str()); - return false; - } - - ExcludeList exclude_list; - exclude_list.LoadFile(AllocatedPath::Build(path_fs, ".mpdignore")); - - if (!exclude_list.IsEmpty()) - remove_excluded_from_directory(directory, exclude_list); - - purge_deleted_from_directory(directory); - - while (reader.ReadEntry()) { - std::string utf8; - struct stat st2; - - const auto entry = reader.GetEntry(); - - if (skip_path(entry) || exclude_list.Check(entry)) - continue; - - utf8 = entry.ToUTF8(); - if (utf8.empty()) - continue; - - if (skip_symlink(&directory, utf8.c_str())) { - modified |= delete_name_in(directory, utf8.c_str()); - continue; - } - - if (stat_directory_child(directory, utf8.c_str(), &st2) == 0) - update_directory_child(directory, utf8.c_str(), &st2); - else - modified |= delete_name_in(directory, utf8.c_str()); - } - - directory.mtime = st->st_mtime; - - return true; -} - -static Directory * -directory_make_child_checked(Directory &parent, const char *name_utf8) -{ - db_lock(); - Directory *directory = parent.FindChild(name_utf8); - db_unlock(); - - if (directory != nullptr) - return directory; - - struct stat st; - if (stat_directory_child(parent, name_utf8, &st) < 0 || - find_inode_ancestor(&parent, st.st_ino, st.st_dev)) - return nullptr; - - if (skip_symlink(&parent, name_utf8)) - return nullptr; - - /* if we're adding directory paths, make sure to delete filenames - with potentially the same name */ - db_lock(); - Song *conflicting = parent.FindSong(name_utf8); - if (conflicting) - delete_song(parent, conflicting); - - directory = parent.CreateChild(name_utf8); - db_unlock(); - - directory_set_stat(*directory, &st); - return directory; -} - -static Directory * -directory_make_uri_parent_checked(const char *uri) -{ - Directory *directory = db_get_root(); - char *duplicated = xstrdup(uri); - char *name_utf8 = duplicated, *slash; - - while ((slash = strchr(name_utf8, '/')) != nullptr) { - *slash = 0; - - if (*name_utf8 == 0) - continue; - - directory = directory_make_child_checked(*directory, - name_utf8); - if (directory == nullptr) - break; - - name_utf8 = slash + 1; - } - - free(duplicated); - return directory; -} - -static void -update_uri(const char *uri) -{ - Directory *parent = directory_make_uri_parent_checked(uri); - if (parent == nullptr) - return; - - const char *name = PathTraitsUTF8::GetBase(uri); - - struct stat st; - if (!skip_symlink(parent, name) && - stat_directory_child(*parent, name, &st) == 0) - update_directory_child(*parent, name, &st); - else - modified |= delete_name_in(*parent, name); -} - -bool -update_walk(const char *path, bool discard) -{ - walk_discard = discard; - modified = false; - - if (path != nullptr && !isRootDirectory(path)) { - update_uri(path); - } else { - Directory *directory = db_get_root(); - struct stat st; - - if (stat_directory(*directory, &st) == 0) - update_directory(*directory, &st); - } - - return modified; -} diff --git a/src/update/UpdateWalk.hxx b/src/update/UpdateWalk.hxx deleted file mode 100644 index e908829e3..000000000 --- a/src/update/UpdateWalk.hxx +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2003-2014 The Music Player Daemon Project - * http://www.musicpd.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#ifndef MPD_UPDATE_WALK_HXX -#define MPD_UPDATE_WALK_HXX - -#include "check.h" - -void -update_walk_global_init(void); - -void -update_walk_global_finish(void); - -/** - * Returns true if the database was modified. - */ -bool -update_walk(const char *path, bool discard); - -#endif diff --git a/test/DumpDatabase.cxx b/test/DumpDatabase.cxx index 034bad72b..0be1191d2 100644 --- a/test/DumpDatabase.cxx +++ b/test/DumpDatabase.cxx @@ -18,12 +18,12 @@ */ #include "config.h" -#include "DatabaseRegistry.hxx" -#include "DatabasePlugin.hxx" -#include "DatabaseSelection.hxx" -#include "DatabaseListener.hxx" -#include "LightDirectory.hxx" -#include "LightSong.hxx" +#include "db/Registry.hxx" +#include "db/DatabasePlugin.hxx" +#include "db/Selection.hxx" +#include "db/DatabaseListener.hxx" +#include "db/LightDirectory.hxx" +#include "db/LightSong.hxx" #include "PlaylistVector.hxx" #include "config/ConfigGlobal.hxx" #include "config/ConfigData.hxx" diff --git a/test/run_inotify.cxx b/test/run_inotify.cxx index 9ed00d3d6..7d77372f0 100644 --- a/test/run_inotify.cxx +++ b/test/run_inotify.cxx @@ -19,7 +19,7 @@ #include "config.h" #include "ShutdownHandler.hxx" -#include "update/InotifySource.hxx" +#include "db/update/InotifySource.hxx" #include "event/Loop.hxx" #include "util/Error.hxx" #include "Log.hxx" diff --git a/test/test_translate_song.cxx b/test/test_translate_song.cxx index fcda35f13..006bd56aa 100644 --- a/test/test_translate_song.cxx +++ b/test/test_translate_song.cxx @@ -11,7 +11,7 @@ #include "fs/AllocatedPath.hxx" #include "ls.hxx" #include "Log.hxx" -#include "DatabaseSong.hxx" +#include "db/DatabaseSong.hxx" #include "Mapper.hxx" #include -- cgit v1.2.3