directory.c 3.33 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "directory.h"
20
#include "utils.h"
21
#include "path.h"
22

23 24
#include <assert.h>
#include <string.h>
25
#include <glib.h>
26

27
struct directory *
28
directory_new(const char *path, struct directory *parent)
Avuton Olrich's avatar
Avuton Olrich committed
29
{
30
	struct directory *directory;
31
	size_t pathlen = strlen(path);
Warren Dukes's avatar
Warren Dukes committed
32

33 34
	assert(path != NULL);
	assert((*path == 0) == (parent == NULL));
Warren Dukes's avatar
Warren Dukes committed
35

36 37
	directory = xcalloc(1, sizeof(*directory) -
			    sizeof(directory->path) + pathlen + 1);
38
	directory->parent = parent;
39
	memcpy(directory->path, path, pathlen + 1);
Warren Dukes's avatar
Warren Dukes committed
40 41 42 43

	return directory;
}

44
void
45
directory_free(struct directory *directory)
Avuton Olrich's avatar
Avuton Olrich committed
46
{
47
	dirvec_destroy(&directory->children);
48
	songvec_destroy(&directory->songs);
Warren Dukes's avatar
Warren Dukes committed
49
	free(directory);
50
	/* this resets last dir returned */
51
	/*directory_get_path(NULL); */
Warren Dukes's avatar
Warren Dukes committed
52 53
}

54 55 56
const char *
directory_get_name(const struct directory *directory)
{
57
	return g_basename(directory->path);
58 59
}

60
void
61
directory_prune_empty(struct directory *directory)
Avuton Olrich's avatar
Avuton Olrich committed
62
{
63 64 65 66
	int i;
	struct dirvec *dv = &directory->children;

	for (i = dv->nr; --i >= 0; ) {
67
		directory_prune_empty(dv->base[i]);
68
		if (directory_is_empty(dv->base[i]))
69
			dirvec_delete(dv, dv->base[i]);
Warren Dukes's avatar
Warren Dukes committed
70
	}
71 72
	if (!dv->nr)
		dirvec_destroy(dv);
Warren Dukes's avatar
Warren Dukes committed
73 74
}

75
struct directory *
76
directory_get_directory(struct directory *directory, const char *name)
77
{
78 79
	struct directory *cur = directory;
	struct directory *found = NULL;
80 81
	char *duplicated;
	char *locate;
Warren Dukes's avatar
Warren Dukes committed
82

83 84
	assert(name != NULL);

85
	if (isRootDirectory(name))
Warren Dukes's avatar
Warren Dukes committed
86 87
		return directory;

88 89 90 91 92
	duplicated = xstrdup(name);
	locate = strchr(duplicated, '/');
	while (1) {
		if (locate)
			*locate = '\0';
93
		if (!(found = directory_get_child(cur, duplicated)))
94
			break;
95
		assert(cur == found->parent);
96 97 98 99 100 101
		cur = found;
		if (!locate)
			break;
		*locate = '/';
		locate = strchr(locate + 1, '/');
	}
Warren Dukes's avatar
Warren Dukes committed
102

103
	free(duplicated);
104

105
	return found;
106 107
}

108
void
109
directory_sort(struct directory *directory)
Avuton Olrich's avatar
Avuton Olrich committed
110
{
111 112
	int i;
	struct dirvec *dv = &directory->children;
Avuton Olrich's avatar
Avuton Olrich committed
113

114
	dirvec_sort(dv);
115
	songvec_sort(&directory->songs);
Warren Dukes's avatar
Warren Dukes committed
116

117
	for (i = dv->nr; --i >= 0; )
118
		directory_sort(dv->base[i]);
Warren Dukes's avatar
Warren Dukes committed
119 120
}

121
int
122 123 124 125
directory_walk(struct directory *directory,
	       int (*forEachSong)(struct song *, void *),
	       int (*forEachDir)(struct directory *, void *),
	       void *data)
Warren Dukes's avatar
Warren Dukes committed
126
{
127
	struct dirvec *dv = &directory->children;
128
	int err = 0;
129
	size_t j;
Avuton Olrich's avatar
Avuton Olrich committed
130

131 132
	if (forEachDir && (err = forEachDir(directory, data)) < 0)
		return err;
Avuton Olrich's avatar
Avuton Olrich committed
133 134

	if (forEachSong) {
135 136 137
		err = songvec_for_each(&directory->songs, forEachSong, data);
		if (err < 0)
			return err;
Avuton Olrich's avatar
Avuton Olrich committed
138 139
	}

140
	for (j = 0; err >= 0 && j < dv->nr; ++j)
141
		err = directory_walk(dv->base[j], forEachSong,
142
						forEachDir, data);
Avuton Olrich's avatar
Avuton Olrich committed
143

144
	return err;
Warren Dukes's avatar
Warren Dukes committed
145
}