DatabaseSave.cxx 4.23 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"
Max Kellermann's avatar
Max Kellermann committed
22
#include "DatabaseLock.hxx"
23
#include "DatabaseError.hxx"
24
#include "Directory.hxx"
25
#include "DirectorySave.hxx"
26
#include "fs/TextFile.hxx"
27
#include "tag/Tag.hxx"
28
#include "tag/TagSettings.h"
29
#include "fs/Charset.hxx"
30
#include "util/StringUtil.hxx"
31
#include "util/Error.hxx"
32
#include "Log.hxx"
33

34
#include <string.h>
35 36 37 38 39 40 41 42 43
#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: "

44
static constexpr unsigned DB_FORMAT = 1;
45 46

void
47
db_save_internal(FILE *fp, const Directory &music_root)
48 49 50 51
{
	fprintf(fp, "%s\n", DIRECTORY_INFO_BEGIN);
	fprintf(fp, DB_FORMAT_PREFIX "%u\n", DB_FORMAT);
	fprintf(fp, "%s%s\n", DIRECTORY_MPD_VERSION, VERSION);
52
	fprintf(fp, "%s%s\n", DIRECTORY_FS_CHARSET, GetFSCharset());
53 54 55 56 57 58 59 60 61 62 63

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
		if (!ignore_tag_items[i])
			fprintf(fp, DB_TAG_PREFIX "%s\n", tag_item_names[i]);

	fprintf(fp, "%s\n", DIRECTORY_INFO_END);

	directory_save(fp, music_root);
}

bool
64
db_load_internal(TextFile &file, Directory &music_root, Error &error)
65 66
{
	char *line;
67
	unsigned format = 0;
68 69 70 71 72
	bool found_charset = false, found_version = false;
	bool success;
	bool tags[TAG_NUM_OF_ITEM_TYPES];

	/* get initial info */
73
	line = file.ReadLine();
74
	if (line == nullptr || strcmp(DIRECTORY_INFO_BEGIN, line) != 0) {
75
		error.Set(db_domain, "Database corrupted");
76 77 78 79 80
		return false;
	}

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

81
	while ((line = file.ReadLine()) != nullptr &&
82
	       strcmp(line, DIRECTORY_INFO_END) != 0) {
83
		if (StringStartsWith(line, DB_FORMAT_PREFIX)) {
84
			format = atoi(line + sizeof(DB_FORMAT_PREFIX) - 1);
85
		} else if (StringStartsWith(line, DIRECTORY_MPD_VERSION)) {
86
			if (found_version) {
87
				error.Set(db_domain, "Duplicate version line");
88 89 90 91
				return false;
			}

			found_version = true;
92
		} else if (StringStartsWith(line, DIRECTORY_FS_CHARSET)) {
93
			const char *new_charset;
94 95

			if (found_charset) {
96
				error.Set(db_domain, "Duplicate charset line");
97 98 99 100 101 102
				return false;
			}

			found_charset = true;

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

			tags[tag] = true;
		} else {
126
			error.Format(db_domain, "Malformed line: %s", line);
127 128 129 130 131
			return false;
		}
	}

	if (format != DB_FORMAT) {
132 133 134
		error.Set(db_domain,
			  "Database format mismatch, "
			  "discarding database file");
135 136 137 138 139
		return false;
	}

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
		if (!ignore_tag_items[i] && !tags[i]) {
140 141 142
			error.Set(db_domain,
				  "Tag list mismatch, "
				  "discarding database file");
143 144 145 146
			return false;
		}
	}

147
	LogDebug(db_domain, "reading DB");
148

149
	db_lock();
150
	success = directory_load(file, music_root, error);
151
	db_unlock();
152 153 154

	return success;
}