DatabaseSave.cxx 4.42 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/TagSettings.h"
30
#include "fs/Charset.hxx"
31
#include "util/StringUtil.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 61

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
		if (!ignore_tag_items[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 78
	bool found_charset = false, found_version = false;
	bool success;
	bool tags[TAG_NUM_OF_ITEM_TYPES];

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

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

87
	while ((line = file.ReadLine()) != nullptr &&
88
	       strcmp(line, DIRECTORY_INFO_END) != 0) {
89
		if (StringStartsWith(line, DB_FORMAT_PREFIX)) {
90
			format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1);
91
		} else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) {
92
			if (found_version) {
93
				error.Set(db_domain, "Duplicate version line");
94 95 96 97
				return false;
			}

			found_version = true;
98
		} else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) {
99
			const char *new_charset;
100 101

			if (found_charset) {
102
				error.Set(db_domain, "Duplicate charset line");
103 104 105 106 107 108
				return false;
			}

			found_charset = true;

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

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

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

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

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

155
	db_lock();
156
	success = directory_load(file, music_root, error);
157
	db_unlock();
158 159 160

	return success;
}