directory.c 3.54 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 "song.h"
21
#include "utils.h"
22
#include "path.h"
23

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

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

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

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

	return directory;
}

45
void
46
directory_free(struct directory *directory)
Avuton Olrich's avatar
Avuton Olrich committed
47
{
48 49 50 51 52 53
	for (unsigned i = 0; i < directory->songs.nr; ++i)
		song_free(directory->songs.base[i]);

	for (unsigned i = 0; i < directory->children.nr; ++i)
		directory_free(directory->children.base[i]);

54
	dirvec_destroy(&directory->children);
55
	songvec_destroy(&directory->songs);
Warren Dukes's avatar
Warren Dukes committed
56
	free(directory);
57
	/* this resets last dir returned */
58
	/*directory_get_path(NULL); */
Warren Dukes's avatar
Warren Dukes committed
59 60
}

61 62 63
const char *
directory_get_name(const struct directory *directory)
{
64
	return g_basename(directory->path);
65 66
}

67
void
68
directory_prune_empty(struct directory *directory)
Avuton Olrich's avatar
Avuton Olrich committed
69
{
70 71 72 73
	int i;
	struct dirvec *dv = &directory->children;

	for (i = dv->nr; --i >= 0; ) {
74
		directory_prune_empty(dv->base[i]);
75
		if (directory_is_empty(dv->base[i]))
76
			dirvec_delete(dv, dv->base[i]);
Warren Dukes's avatar
Warren Dukes committed
77
	}
78 79
	if (!dv->nr)
		dirvec_destroy(dv);
Warren Dukes's avatar
Warren Dukes committed
80 81
}

82
struct directory *
83
directory_get_directory(struct directory *directory, const char *name)
84
{
85 86
	struct directory *cur = directory;
	struct directory *found = NULL;
87 88
	char *duplicated;
	char *locate;
Warren Dukes's avatar
Warren Dukes committed
89

90 91
	assert(name != NULL);

92
	if (isRootDirectory(name))
Warren Dukes's avatar
Warren Dukes committed
93 94
		return directory;

95 96 97 98 99
	duplicated = xstrdup(name);
	locate = strchr(duplicated, '/');
	while (1) {
		if (locate)
			*locate = '\0';
100
		if (!(found = directory_get_child(cur, duplicated)))
101
			break;
102
		assert(cur == found->parent);
103 104 105 106 107 108
		cur = found;
		if (!locate)
			break;
		*locate = '/';
		locate = strchr(locate + 1, '/');
	}
Warren Dukes's avatar
Warren Dukes committed
109

110
	free(duplicated);
111

112
	return found;
113 114
}

115
void
116
directory_sort(struct directory *directory)
Avuton Olrich's avatar
Avuton Olrich committed
117
{
118 119
	int i;
	struct dirvec *dv = &directory->children;
Avuton Olrich's avatar
Avuton Olrich committed
120

121
	dirvec_sort(dv);
122
	songvec_sort(&directory->songs);
Warren Dukes's avatar
Warren Dukes committed
123

124
	for (i = dv->nr; --i >= 0; )
125
		directory_sort(dv->base[i]);
Warren Dukes's avatar
Warren Dukes committed
126 127
}

128
int
129 130 131 132
directory_walk(struct directory *directory,
	       int (*forEachSong)(struct song *, void *),
	       int (*forEachDir)(struct directory *, void *),
	       void *data)
Warren Dukes's avatar
Warren Dukes committed
133
{
134
	struct dirvec *dv = &directory->children;
135
	int err = 0;
136
	size_t j;
Avuton Olrich's avatar
Avuton Olrich committed
137

138 139
	if (forEachDir && (err = forEachDir(directory, data)) < 0)
		return err;
Avuton Olrich's avatar
Avuton Olrich committed
140 141

	if (forEachSong) {
142 143 144
		err = songvec_for_each(&directory->songs, forEachSong, data);
		if (err < 0)
			return err;
Avuton Olrich's avatar
Avuton Olrich committed
145 146
	}

147
	for (j = 0; err >= 0 && j < dv->nr; ++j)
148
		err = directory_walk(dv->base[j], forEachSong,
149
						forEachDir, data);
Avuton Olrich's avatar
Avuton Olrich committed
150

151
	return err;
Warren Dukes's avatar
Warren Dukes committed
152
}