DatabaseSave.cxx 4.31 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
21
#include "DatabaseSave.hxx"
22 23
#include "db/DatabaseLock.hxx"
#include "db/DatabaseError.hxx"
24
#include "Directory.hxx"
25
#include "DirectorySave.hxx"
26 27
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/TextFile.hxx"
28
#include "tag/Tag.hxx"
29
#include "tag/Settings.hxx"
30
#include "fs/Charset.hxx"
31
#include "util/StringCompare.hxx"
32
#include "util/Error.hxx"
33
#include "Log.hxx"
34

35
#include <string.h>
36 37 38 39 40 41 42 43 44
#include <stdlib.h>

#define DIRECTORY_INFO_BEGIN "info_begin"
#define DIRECTORY_INFO_END "info_end"
#define DB_FORMAT_PREFIX "format: "
#define DIRECTORY_MPD_VERSION "mpd_version: "
#define DIRECTORY_FS_CHARSET "fs_charset: "
#define DB_TAG_PREFIX "tag: "

45
static constexpr unsigned DB_FORMAT = 2;
46

47 48 49 50 51
/**
 * The oldest database format understood by this MPD version.
 */
static constexpr unsigned OLDEST_DB_FORMAT = 1;

52
void
53
db_save_internal(BufferedOutputStream &os, const Directory &music_root)
54
{
55 56 57 58
	os.Format("%s\n", DIRECTORY_INFO_BEGIN);
	os.Format(DB_FORMAT_PREFIX "%u\n", DB_FORMAT);
	os.Format("%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
	os.Format("%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset());
59 60

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
61
		if (IsTagEnabled(i))
62
			os.Format(DB_TAG_PREFIX "%s\n", tag_item_names[i]);
63

64
	os.Format("%s\n", DIRECTORY_INFO_END);
65

66
	directory_save(os, music_root);
67 68 69
}

bool
70
db_load_internal(TextFile &file, Directory &music_root, Error &error)
71 72
{
	char *line;
73
	unsigned format = 0;
74 75 76 77
	bool found_charset = false, found_version = false;
	bool tags[TAG_NUM_OF_ITEM_TYPES];

	/* get initial info */
78
	line = file.ReadLine();
79
	if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) {
80
		error.Set(db_domain, "Database corrupted");
81 82 83 84 85
		return false;
	}

	memset(tags, false, sizeof(tags));

86
	while ((line = file.ReadLine()) != nullptr &&
87
	       strcmp(line, DIRECTORY_INFO_END) != 0) {
88 89 90 91
		const char *p;

		if ((p = StringAfterPrefix(line, DB_FORMAT_PREFIX))) {
			format = atoi(p);
92
		} else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) {
93
			if (found_version) {
94
				error.Set(db_domain, "Duplicate version line");
95 96 97 98
				return false;
			}

			found_version = true;
99
		} else if ((p = StringAfterPrefix(line, DIRECTORY_FS_CHARSET))) {
100
			if (found_charset) {
101
				error.Set(db_domain, "Duplicate charset line");
102 103 104 105 106
				return false;
			}

			found_charset = true;

107
			const char *new_charset = p;
108 109 110
			const char *const old_charset = GetFSCharset();
			if (*old_charset != 0
			    && strcmp(new_charset, old_charset) != 0) {
111 112 113 114
				error.Format(db_domain,
					     "Existing database has charset "
					     "\"%s\" instead of \"%s\"; "
					     "discarding database file",
115
					     new_charset, old_charset);
116 117
				return false;
			}
118 119
		} else if ((p = StringAfterPrefix(line, DB_TAG_PREFIX))) {
			const char *name = p;
120
			TagType tag = tag_name_parse(name);
121
			if (tag == TAG_NUM_OF_ITEM_TYPES) {
122 123 124 125
				error.Format(db_domain,
					     "Unrecognized tag '%s', "
					     "discarding database file",
					     name);
126 127 128 129 130
				return false;
			}

			tags[tag] = true;
		} else {
131
			error.Format(db_domain, "Malformed line: %s", line);
132 133 134 135
			return false;
		}
	}

136
	if (format < OLDEST_DB_FORMAT || format > DB_FORMAT) {
137 138 139
		error.Set(db_domain,
			  "Database format mismatch, "
			  "discarding database file");
140 141 142 143
		return false;
	}

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
144
		if (IsTagEnabled(i) && !tags[i]) {
145 146 147
			error.Set(db_domain,
				  "Tag list mismatch, "
				  "discarding database file");
148 149 150 151
			return false;
		}
	}

152
	LogDebug(db_domain, "reading DB");
153

154 155
	const ScopeDatabaseLock protect;
	return directory_load(file, music_root, error);
156
}