Directory.hxx 6.04 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20 21
#ifndef MPD_DIRECTORY_HXX
#define MPD_DIRECTORY_HXX
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "check.h"
24
#include "util/list.h"
25
#include "Compiler.h"
26
#include "DatabaseVisitor.hxx"
27
#include "PlaylistVector.hxx"
28

29
#include <sys/types.h>
30

31 32
#define DEVICE_INARCHIVE (dev_t)(-1)
#define DEVICE_CONTAINER (dev_t)(-2)
33

34
#define directory_for_each_child(pos, directory) \
35
	list_for_each_entry(pos, &(directory).children, siblings)
36 37

#define directory_for_each_child_safe(pos, n, directory) \
38
	list_for_each_entry_safe(pos, n, &(directory).children, siblings)
39

40
#define directory_for_each_song(pos, directory) \
41
	list_for_each_entry(pos, &(directory).songs, siblings)
42 43

#define directory_for_each_song_safe(pos, n, directory) \
44
	list_for_each_entry_safe(pos, n, &(directory).songs, siblings)
45

46
struct Song;
47
struct db_visitor;
48
class SongFilter;
49
class Error;
50

51
struct Directory {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	/**
	 * 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;

70 71 72 73 74 75 76
	/**
	 * 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;
77

78
	PlaylistVector playlists;
79

80
	Directory *parent;
81
	time_t mtime;
82 83
	ino_t inode;
	dev_t device;
84
	bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */
85
	char path[sizeof(long)];
86

87 88 89 90 91 92 93 94 95 96
protected:
	Directory(const char *path);

	gcc_malloc gcc_nonnull_all
	static Directory *Allocate(const char *path);

public:
	/**
	 * Default constructor, needed for #detached_root.
	 */
97
	Directory();
98 99
	~Directory();

100
	/**
101
	 * Generic constructor for #Directory object.
102
	 */
103
	gcc_malloc
104
	static Directory *NewGeneric(const char *path_utf8, Directory *parent);
105

106
	/**
107
	 * Create a new root #Directory object.
108 109
	 */
	gcc_malloc
110
	static Directory *NewRoot() {
111 112
		return NewGeneric("", nullptr);
	}
113

114
	/**
115
	 * Free this #Directory object (and the whole object tree within it),
116 117 118
	 * assuming it was already removed from the parent.
	 */
	void Free();
119

120
	/**
121 122
	 * Remove this #Directory object from its parent and free it.  This
	 * must not be called with the root Directory.
123 124 125 126
	 *
	 * Caller must lock the #db_mutex.
	 */
	void Delete();
127

128
	/**
129
	 * Create a new #Directory object as a child of the given one.
130 131 132 133 134 135
	 *
	 * Caller must lock the #db_mutex.
	 *
	 * @param name_utf8 the UTF-8 encoded name of the new sub directory
	 */
	gcc_malloc
136
	Directory *CreateChild(const char *name_utf8);
137

138 139 140 141
	/**
	 * Caller must lock the #db_mutex.
	 */
	gcc_pure
142
	const Directory *FindChild(const char *name) const;
143

144
	gcc_pure
145 146 147
	Directory *FindChild(const char *name) {
		const Directory *cthis = this;
		return const_cast<Directory *>(cthis->FindChild(name));
148
	}
149

150 151 152 153 154 155
	/**
	 * Look up a sub directory, and create the object if it does not
	 * exist.
	 *
	 * Caller must lock the #db_mutex.
	 */
156 157
	Directory *MakeChild(const char *name_utf8) {
		Directory *child = FindChild(name_utf8);
158 159 160 161
		if (child == nullptr)
			child = CreateChild(name_utf8);
		return child;
	}
162

163 164 165 166
	/**
	 * Looks up a directory by its relative URI.
	 *
	 * @param uri the relative URI
167
	 * @return the Directory, or nullptr if none was found
168 169
	 */
	gcc_pure
170
	Directory *LookupDirectory(const char *uri);
171

172 173 174 175
	gcc_pure
	bool IsEmpty() const {
		return list_empty(&children) &&
			list_empty(&songs) &&
176
			playlists.empty();
177
	}
178

179 180 181 182
	gcc_pure
	const char *GetPath() const {
		return path;
	}
183

184 185 186 187 188
	/**
	 * Returns the base name of the directory.
	 */
	gcc_pure
	const char *GetName() const;
189

190 191 192 193 194
	/**
	 * Is this the root directory of the music database?
	 */
	gcc_pure
	bool IsRoot() const {
195
		return parent == nullptr;
196
	}
197

198 199 200 201 202 203
	/**
	 * Look up a song in this directory by its name.
	 *
	 * Caller must lock the #db_mutex.
	 */
	gcc_pure
204
	const Song *FindSong(const char *name_utf8) const;
Warren Dukes's avatar
Warren Dukes committed
205

206
	gcc_pure
207
	Song *FindSong(const char *name_utf8) {
208
		const Directory *cthis = this;
209
		return const_cast<Song *>(cthis->FindSong(name_utf8));
210
	}
Warren Dukes's avatar
Warren Dukes committed
211

212 213 214 215 216 217
	/**
	 * Looks up a song by its relative URI.
	 *
	 * Caller must lock the #db_mutex.
	 *
	 * @param uri the relative URI
218
	 * @return the song, or nullptr if none was found
219 220
	 */
	gcc_pure
221
	Song *LookupSong(const char *uri);
222

223 224 225 226
	/**
	 * Add a song object to this directory.  Its "parent" attribute must
	 * be set already.
	 */
227
	void AddSong(Song *song);
228

229 230 231 232 233
	/**
	 * Remove a song object from this directory (which effectively
	 * invalidates the song object, because the "parent" attribute becomes
	 * stale), but does not free it.
	 */
234
	void RemoveSong(Song *song);
235

236 237 238 239
	/**
	 * Caller must lock the #db_mutex.
	 */
	void PruneEmpty();
240

241 242 243 244 245 246 247 248 249 250 251 252 253
	/**
	 * 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,
254
		  Error &error) const;
255 256 257 258 259 260 261
};

static inline bool
isRootDirectory(const char *name)
{
	return name[0] == 0 || (name[0] == '/' && name[1] == 0);
}
262

Warren Dukes's avatar
Warren Dukes committed
263
#endif