directory.h 6.74 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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_H
#define MPD_DIRECTORY_H
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "check.h"
24
#include "util/list.h"
25
#include "gcc.h"
Warren Dukes's avatar
Warren Dukes committed
26

27 28 29 30
#ifdef __cplusplus
#include "DatabaseVisitor.hxx"
#endif

31
#include <glib.h>
32
#include <stdbool.h>
33
#include <sys/types.h>
34

35 36
#define DEVICE_INARCHIVE (dev_t)(-1)
#define DEVICE_CONTAINER (dev_t)(-2)
37

38 39 40 41 42 43
#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)

44 45 46 47 48 49
#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)

50 51 52 53 54 55
#define directory_for_each_playlist(pos, directory) \
	list_for_each_entry(pos, &directory->playlists, siblings)

#define directory_for_each_playlist_safe(pos, n, directory) \
	list_for_each_entry_safe(pos, n, &directory->playlists, siblings)

56
struct song;
57
struct db_visitor;
58 59 60 61

#ifdef __cplusplus
class SongFilter;
#endif
62

63
struct directory {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	/**
	 * 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;

82 83 84 85 86 87 88
	/**
	 * 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;
89

90
	struct list_head playlists;
91

92
	struct directory *parent;
93
	time_t mtime;
94 95
	ino_t inode;
	dev_t device;
96
	bool have_stat; /* not needed if ino_t == dev_t == 0 is impossible */
97
	char path[sizeof(long)];
98 99 100 101 102

#ifdef __cplusplus
	/**
	 * Caller must lock #db_mutex.
	 */
103
	bool Walk(bool recursive, const SongFilter *match,
104 105 106 107
		  VisitDirectory visit_directory, VisitSong visit_song,
		  VisitPlaylist visit_playlist,
		  GError **error_r) const;
#endif
108
};
109

110 111
G_BEGIN_DECLS

112 113 114 115 116
static inline bool
isRootDirectory(const char *name)
{
	return name[0] == 0 || (name[0] == '/' && name[1] == 0);
}
117

118 119 120
/**
 * Generic constructor for #directory object.
 */
121
gcc_malloc
122
struct directory *
123
directory_new(const char *dirname, struct directory *parent);
124

125 126 127
/**
 * Create a new root #directory object.
 */
128
gcc_malloc
129 130 131 132 133 134 135 136 137 138
static inline struct directory *
directory_new_root(void)
{
	return directory_new("", NULL);
}

/**
 * Free this #directory object (and the whole object tree within it),
 * assuming it was already removed from the parent.
 */
139
void
140
directory_free(struct directory *directory);
141

142 143 144
/**
 * Remove this #directory object from its parent and free it.  This
 * must not be called with the root directory.
145 146
 *
 * Caller must lock the #db_mutex.
147 148 149 150
 */
void
directory_delete(struct directory *directory);

151
static inline bool
152
directory_is_empty(const struct directory *directory)
153
{
154
	return list_empty(&directory->children) &&
155
		list_empty(&directory->songs) &&
156
		list_empty(&directory->playlists);
157 158
}

159
static inline const char *
160
directory_get_path(const struct directory *directory)
161 162 163 164
{
	return directory->path;
}

165 166 167 168 169 170 171 172 173
/**
 * Is this the root directory of the music database?
 */
static inline bool
directory_is_root(const struct directory *directory)
{
	return directory->parent == NULL;
}

174 175 176
/**
 * Returns the base name of the directory.
 */
177
gcc_pure
178 179 180
const char *
directory_get_name(const struct directory *directory);

181 182 183
/**
 * Caller must lock the #db_mutex.
 */
184
gcc_pure
185 186
struct directory *
directory_get_child(const struct directory *directory, const char *name);
187

188 189 190
/**
 * Create a new #directory object as a child of the given one.
 *
191 192
 * Caller must lock the #db_mutex.
 *
193 194 195
 * @param parent the parent directory the new one will be added to
 * @param name_utf8 the UTF-8 encoded name of the new sub directory
 */
196
gcc_malloc
197 198 199 200 201 202
struct directory *
directory_new_child(struct directory *parent, const char *name_utf8);

/**
 * Look up a sub directory, and create the object if it does not
 * exist.
203 204
 *
 * Caller must lock the #db_mutex.
205
 */
206
static inline struct directory *
207
directory_make_child(struct directory *directory, const char *name_utf8)
208
{
209 210 211 212
	struct directory *child = directory_get_child(directory, name_utf8);
	if (child == NULL)
		child = directory_new_child(directory, name_utf8);
	return child;
213 214
}

215 216 217
/**
 * Caller must lock the #db_mutex.
 */
218
void
219
directory_prune_empty(struct directory *directory);
Warren Dukes's avatar
Warren Dukes committed
220

221 222 223 224 225 226 227
/**
 * Looks up a directory by its relative URI.
 *
 * @param directory the parent (or grandparent, ...) directory
 * @param uri the relative URI
 * @return the directory, or NULL if none was found
 */
228
struct directory *
229
directory_lookup_directory(struct directory *directory, const char *uri);
Warren Dukes's avatar
Warren Dukes committed
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/**
 * Add a song object to this directory.  Its "parent" attribute must
 * be set already.
 */
void
directory_add_song(struct directory *directory, struct 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
directory_remove_song(struct directory *directory, struct song *song);

/**
 * Look up a song in this directory by its name.
248 249
 *
 * Caller must lock the #db_mutex.
250
 */
251
gcc_pure
252 253 254
struct song *
directory_get_song(const struct directory *directory, const char *name_utf8);

255 256 257
/**
 * Looks up a song by its relative URI.
 *
258 259
 * Caller must lock the #db_mutex.
 *
260 261 262 263 264 265 266
 * @param directory the parent (or grandparent, ...) directory
 * @param uri the relative URI
 * @return the song, or NULL if none was found
 */
struct song *
directory_lookup_song(struct directory *directory, const char *uri);

267 268 269 270 271
/**
 * Sort all directory entries recursively.
 *
 * Caller must lock the #db_mutex.
 */
272
void
273 274
directory_sort(struct directory *directory);

275
G_END_DECLS
276

Warren Dukes's avatar
Warren Dukes committed
277
#endif