directory.h 2.8 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * 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 "dirvec.h"
24
#include "songvec.h"
Warren Dukes's avatar
Warren Dukes committed
25

26
#include <stdbool.h>
27
#include <sys/types.h>
28

29 30
#define DIRECTORY_DIR		"directory: "

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

34
struct directory {
35
	struct dirvec children;
36
	struct songvec songs;
37
	struct directory *parent;
38
	time_t mtime;
39 40 41
	ino_t inode;
	dev_t device;
	unsigned stat; /* not needed if ino_t == dev_t == 0 is impossible */
42
	char path[sizeof(long)];
43
};
44

45 46 47 48 49
static inline bool
isRootDirectory(const char *name)
{
	return name[0] == 0 || (name[0] == '/' && name[1] == 0);
}
50

51
struct directory *
52
directory_new(const char *dirname, struct directory *parent);
53 54

void
55
directory_free(struct directory *directory);
56

57
static inline bool
58
directory_is_empty(const struct directory *directory)
59 60 61 62
{
	return directory->children.nr == 0 && directory->songs.nr == 0;
}

63
static inline const char *
64
directory_get_path(const struct directory *directory)
65 66 67 68
{
	return directory->path;
}

69 70 71 72 73 74 75 76 77
/**
 * Is this the root directory of the music database?
 */
static inline bool
directory_is_root(const struct directory *directory)
{
	return directory->parent == NULL;
}

78 79 80 81 82 83
/**
 * Returns the base name of the directory.
 */
const char *
directory_get_name(const struct directory *directory);

84 85 86 87 88 89 90 91 92 93 94 95 96 97
static inline struct directory *
directory_get_child(const struct directory *directory, const char *name)
{
	return dirvec_find(&directory->children, name);
}

static inline struct directory *
directory_new_child(struct directory *directory, const char *name)
{
	struct directory *subdir = directory_new(name, directory);
	dirvec_add(&directory->children, subdir);
	return subdir;
}

98
void
99
directory_prune_empty(struct directory *directory);
Warren Dukes's avatar
Warren Dukes committed
100

101
struct directory *
102
directory_get_directory(struct directory *directory, const char *name);
Warren Dukes's avatar
Warren Dukes committed
103

104
void
105 106
directory_sort(struct directory *directory);

107
int
108 109 110 111
directory_walk(struct directory *directory,
	       int (*forEachSong)(struct song *, void *),
	       int (*forEachDir)(struct directory *, void *),
	       void *data);
112

Warren Dukes's avatar
Warren Dukes committed
113
#endif