MPD  0.20.6
Directory.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_DIRECTORY_HXX
21 #define MPD_DIRECTORY_HXX
22 
23 #include "check.h"
24 #include "Compiler.h"
25 #include "db/Visitor.hxx"
26 #include "db/PlaylistVector.hxx"
27 #include "Song.hxx"
28 
29 #include <boost/intrusive/list.hpp>
30 
31 #include <string>
32 
37 static constexpr unsigned DEVICE_INARCHIVE = -1;
38 
44 static constexpr unsigned DEVICE_CONTAINER = -2;
45 
46 class SongFilter;
47 class Database;
48 
49 struct Directory {
50  static constexpr auto link_mode = boost::intrusive::normal_link;
51  typedef boost::intrusive::link_mode<link_mode> LinkMode;
52  typedef boost::intrusive::list_member_hook<LinkMode> Hook;
53 
62  Hook siblings;
63 
64  typedef boost::intrusive::member_hook<Directory, Hook,
66  typedef boost::intrusive::list<Directory, SiblingsHook,
67  boost::intrusive::constant_time_size<false>> List;
68 
75  List children;
76 
84 
86 
87  Directory *parent;
88  time_t mtime;
89  unsigned inode, device;
90 
91  std::string path;
92 
98 
99 public:
100  Directory(std::string &&_path_utf8, Directory *_parent);
101  ~Directory();
102 
106  gcc_malloc
107  static Directory *NewRoot() {
108  return new Directory(std::string(), nullptr);
109  }
110 
111  bool IsMount() const {
112  return mounted_database != nullptr;
113  }
114 
121  void Delete();
122 
130  gcc_malloc
131  Directory *CreateChild(const char *name_utf8);
132 
136  gcc_pure
137  const Directory *FindChild(const char *name) const;
138 
139  gcc_pure
140  Directory *FindChild(const char *name) {
141  const Directory *cthis = this;
142  return const_cast<Directory *>(cthis->FindChild(name));
143  }
144 
151  Directory *MakeChild(const char *name_utf8) {
152  Directory *child = FindChild(name_utf8);
153  if (child == nullptr)
154  child = CreateChild(name_utf8);
155  return child;
156  }
157 
158  struct LookupResult {
164  Directory *directory;
165 
170  const char *uri;
171  };
172 
179  gcc_pure
180  LookupResult LookupDirectory(const char *uri);
181 
182  gcc_pure
183  bool IsEmpty() const {
184  return children.empty() &&
185  songs.empty() &&
186  playlists.empty();
187  }
188 
189  gcc_pure
190  const char *GetPath() const {
191  return path.c_str();
192  }
193 
197  gcc_pure
198  const char *GetName() const;
199 
203  gcc_pure
204  bool IsRoot() const {
205  return parent == nullptr;
206  }
207 
208  template<typename T>
209  void ForEachChildSafe(T &&t) {
210  const auto end = children.end();
211  for (auto i = children.begin(), next = i; i != end; i = next) {
212  next = std::next(i);
213  t(*i);
214  }
215  }
216 
217  template<typename T>
218  void ForEachSongSafe(T &&t) {
219  const auto end = songs.end();
220  for (auto i = songs.begin(), next = i; i != end; i = next) {
221  next = std::next(i);
222  t(*i);
223  }
224  }
225 
231  gcc_pure
232  const Song *FindSong(const char *name_utf8) const;
233 
234  gcc_pure
235  Song *FindSong(const char *name_utf8) {
236  const Directory *cthis = this;
237  return const_cast<Song *>(cthis->FindSong(name_utf8));
238  }
239 
244  void AddSong(Song *song);
245 
251  void RemoveSong(Song *song);
252 
256  void PruneEmpty();
257 
263  void Sort();
264 
268  void Walk(bool recursive, const SongFilter *match,
269  VisitDirectory visit_directory, VisitSong visit_song,
270  VisitPlaylist visit_playlist) const;
271 
272  gcc_pure
273  LightDirectory Export() const;
274 };
275 
276 #endif
static constexpr unsigned DEVICE_CONTAINER
Virtual directory that is really a song file with one or more "sub" songs as specified by DecoderPlug...
Definition: Directory.hxx:44
const char * uri
The remaining URI part (without leading slash) or nullptr if the given URI was consumed completely...
Definition: Directory.hxx:170
gcc_pure LookupResult LookupDirectory(const char *uri)
Looks up a directory by its relative URI.
SongList songs
A doubly linked list of songs within this directory.
Definition: Directory.hxx:83
unsigned device
Definition: Directory.hxx:89
unsigned inode
Definition: Directory.hxx:89
boost::intrusive::list< Song, boost::intrusive::member_hook< Song, Song::Hook,&Song::siblings >, boost::intrusive::constant_time_size< false > > SongList
Definition: Song.hxx:135
boost::intrusive::link_mode< link_mode > LinkMode
Definition: Directory.hxx:51
gcc_pure bool IsEmpty() const
Definition: Directory.hxx:183
static constexpr auto link_mode
Definition: Directory.hxx:50
std::function< void(const LightDirectory &)> VisitDirectory
Definition: Visitor.hxx:28
void RemoveSong(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.
#define gcc_malloc
Definition: Compiler.h:112
void PruneEmpty()
Caller must lock the db_mutex.
bool IsMount() const
Definition: Directory.hxx:111
PlaylistVector playlists
Definition: Directory.hxx:85
List children
A doubly linked list of child directories.
Definition: Directory.hxx:75
std::function< void(const PlaylistInfo &, const LightDirectory &)> VisitPlaylist
Definition: Visitor.hxx:33
Hook siblings
Pointers to the siblings of this directory within the parent directory.
Definition: Directory.hxx:62
void ForEachChildSafe(T &&t)
Definition: Directory.hxx:209
void AddSong(Song *song)
Add a song object to this directory.
A song file inside the configured music directory.
Definition: Song.hxx:44
void Walk(bool recursive, const SongFilter *match, VisitDirectory visit_directory, VisitSong visit_song, VisitPlaylist visit_playlist) const
Caller must lock db_mutex.
gcc_pure const char * GetPath() const
Definition: Directory.hxx:190
boost::intrusive::list< Directory, SiblingsHook, boost::intrusive::constant_time_size< false > > List
Definition: Directory.hxx:67
gcc_pure const char * GetName() const
Returns the base name of the directory.
Directory * directory
The last directory that was found.
Definition: Directory.hxx:164
time_t mtime
Definition: Directory.hxx:88
gcc_pure const Song * FindSong(const char *name_utf8) const
Look up a song in this directory by its name.
gcc_pure Song * FindSong(const char *name_utf8)
Definition: Directory.hxx:235
void Delete()
Remove this Directory object from its parent and free it.
Directory * parent
Definition: Directory.hxx:87
static gcc_malloc Directory * NewRoot()
Create a new root Directory object.
Definition: Directory.hxx:107
std::function< void(const LightSong &)> VisitSong
Definition: Visitor.hxx:31
static constexpr unsigned DEVICE_INARCHIVE
Virtual directory that is really an archive file or a folder inside the archive (special value for Di...
Definition: Directory.hxx:37
boost::intrusive::list_member_hook< LinkMode > Hook
Definition: Directory.hxx:52
Directory * MakeChild(const char *name_utf8)
Look up a sub directory, and create the object if it does not exist.
Definition: Directory.hxx:151
void ForEachSongSafe(T &&t)
Definition: Directory.hxx:218
Directory(std::string &&_path_utf8, Directory *_parent)
void Sort()
Sort all directory entries recursively.
Database * mounted_database
If this is not nullptr, then this directory does not really exist, but is a mount point for another D...
Definition: Directory.hxx:97
gcc_pure const Directory * FindChild(const char *name) const
Caller must lock the db_mutex.
gcc_pure bool IsRoot() const
Is this the root directory of the music database?
Definition: Directory.hxx:204
gcc_malloc Directory * CreateChild(const char *name_utf8)
Create a new Directory object as a child of the given one.
gcc_pure LightDirectory Export() const
A reference to a directory.
#define gcc_pure
Definition: Compiler.h:116
gcc_pure Directory * FindChild(const char *name)
Definition: Directory.hxx:140
const Storage const char * uri
boost::intrusive::member_hook< Directory, Hook,&Directory::siblings > SiblingsHook
Definition: Directory.hxx:65
std::string path
Definition: Directory.hxx:91
const Partition const char * name
Definition: Count.hxx:34