directory_save.c 3.85 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * http://www.musicpd.org
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.
18 19 20 21 22 23 24 25
 */

#include "directory_save.h"
#include "directory.h"
#include "song.h"
#include "path.h"
#include "song_save.h"

Max Kellermann's avatar
Max Kellermann committed
26 27 28
#include <assert.h>
#include <string.h>

29
#define DIRECTORY_MTIME "mtime: "
30 31 32
#define DIRECTORY_BEGIN "begin: "
#define DIRECTORY_END "end: "

33 34 35 36 37 38 39 40 41
/**
 * The quark used for GError.domain.
 */
static inline GQuark
directory_quark(void)
{
	return g_quark_from_static_string("directory");
}

42 43 44 45 46 47 48 49
/* TODO error checking */
int
directory_save(FILE *fp, struct directory *directory)
{
	struct dirvec *children = &directory->children;
	size_t i;
	int retv;

50
	if (!directory_is_root(directory)) {
51 52 53
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
			(unsigned long)directory->mtime);

54 55 56 57 58 59 60 61
		retv = fprintf(fp, "%s%s\n", DIRECTORY_BEGIN,
			       directory_get_path(directory));
		if (retv < 0)
			return -1;
	}

	for (i = 0; i < children->nr; ++i) {
		struct directory *cur = children->base[i];
62
		char *base = g_path_get_basename(cur->path);
63 64

		retv = fprintf(fp, DIRECTORY_DIR "%s\n", base);
65
		g_free(base);
66 67 68 69 70 71 72 73
		if (retv < 0)
			return -1;
		if (directory_save(fp, cur) < 0)
			return -1;
	}

	songvec_save(fp, &directory->songs);

74
	if (!directory_is_root(directory) &&
75 76 77 78 79 80
	    fprintf(fp, DIRECTORY_END "%s\n",
		    directory_get_path(directory)) < 0)
		return -1;
	return 0;
}

81 82
bool
directory_load(FILE *fp, struct directory *directory, GError **error)
83 84 85 86
{
	char buffer[MPD_PATH_MAX * 2];
	char key[MPD_PATH_MAX * 2];
	char *name;
87
	bool success;
88

89
	while (fgets(buffer, sizeof(buffer), fp)
90 91
	       && !g_str_has_prefix(buffer, DIRECTORY_END)) {
		if (g_str_has_prefix(buffer, DIRECTORY_DIR)) {
92 93
			struct directory *subdir;

94
			g_strchomp(buffer);
95
			strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
96 97 98 99 100
			if (!fgets(buffer, sizeof(buffer), fp)) {
				g_set_error(error, directory_quark(), 0,
					    "Unexpected end of file");
				return false;
			}
101

102
			if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) {
103 104 105 106
				directory->mtime =
					g_ascii_strtoull(buffer + sizeof(DIRECTORY_MTIME) - 1,
							 NULL, 10);

107 108 109 110 111 112 113 114 115 116 117
				if (!fgets(buffer, sizeof(buffer), fp)) {
					g_set_error(error, directory_quark(), 0,
						    "Unexpected end of file");
					return false;
				}
			}

			if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN)) {
				g_set_error(error, directory_quark(), 0,
					    "Malformed line: %s", buffer);
				return false;
118
			}
119 120

			g_strchomp(buffer);
121
			name = &(buffer[strlen(DIRECTORY_BEGIN)]);
122 123 124 125 126 127
			if (!g_str_has_prefix(name, directory->path) != 0) {
				g_set_error(error, directory_quark(), 0,
					    "Wrong path in database: '%s' in '%s'",
					    name, directory->path);
				return false;
			}
128

129 130
			subdir = directory_get_child(directory, name);
			if (subdir != NULL) {
131 132 133 134 135
				assert(subdir->parent == directory);
			} else {
				subdir = directory_new(name, directory);
				dirvec_add(&directory->children, subdir);
			}
136 137 138 139

			success = directory_load(fp, subdir, error);
			if (!success)
				return false;
140
		} else if (g_str_has_prefix(buffer, SONG_BEGIN)) {
141 142
			readSongInfoIntoList(fp, &directory->songs, directory);
		} else {
143 144 145
			g_set_error(error, directory_quark(), 0,
				    "Malformed line: %s", buffer);
			return false;
146 147
		}
	}
148 149

	return true;
150
}