StateFile.cxx 3.93 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3
 * 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
#include "config.h"
21
#include "StateFile.hxx"
22
#include "output/State.hxx"
23
#include "queue/PlaylistState.hxx"
24 25 26
#include "fs/io/TextFile.hxx"
#include "fs/io/FileOutputStream.hxx"
#include "fs/io/BufferedOutputStream.hxx"
27
#include "storage/StorageState.hxx"
28
#include "Partition.hxx"
29
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "mixer/Volume.hxx"
31
#include "SongLoader.hxx"
32 33
#include "util/Domain.hxx"
#include "Log.hxx"
34

35 36
#include <exception>

37
static constexpr Domain state_file_domain("state_file");
38

39
StateFile::StateFile(StateFileConfig &&_config,
40
		     Partition &_partition, EventLoop &_loop)
41
	:config(std::move(_config)), path_utf8(config.path.ToUTF8()),
42
	 timer_event(_loop, BIND_THIS_METHOD(OnTimeout)),
43
	 partition(_partition)
44
{
45
}
46

47
void
48
StateFile::RememberVersions() noexcept
49 50 51
{
	prev_volume_version = sw_volume_state_get_hash();
	prev_output_version = audio_output_state_get_version();
52 53
	prev_playlist_version = playlist_state_get_hash(partition.playlist,
							partition.pc);
54 55 56
#ifdef ENABLE_DATABASE
	prev_storage_version = storage_state_get_hash(partition.instance);
#endif
57 58 59
}

bool
60
StateFile::IsModified() const noexcept
61 62 63
{
	return prev_volume_version != sw_volume_state_get_hash() ||
		prev_output_version != audio_output_state_get_version() ||
64
		prev_playlist_version != playlist_state_get_hash(partition.playlist,
65 66 67 68 69
								 partition.pc)
#ifdef ENABLE_DATABASE
		|| prev_storage_version != storage_state_get_hash(partition.instance)
#endif
		;
70 71
}

72 73 74 75 76
inline void
StateFile::Write(BufferedOutputStream &os)
{
	save_sw_volume_state(os);
	audio_output_state_save(os, partition.outputs);
77 78 79 80 81

#ifdef ENABLE_DATABASE
	storage_state_save(os, partition.instance);
#endif

82 83 84
	playlist_state_save(os, partition.playlist, partition.pc);
}

85 86
inline void
StateFile::Write(OutputStream &os)
87 88 89
{
	BufferedOutputStream bos(os);
	Write(bos);
90
	bos.Flush();
91 92
}

93 94 95
void
StateFile::Write()
{
96 97
	FormatDebug(state_file_domain,
		    "Saving state file %s", path_utf8.c_str());
98

99
	try {
100
		FileOutputStream fos(config.path);
101
		Write(fos);
102
		fos.Commit();
103 104
	} catch (...) {
		LogError(std::current_exception());
105 106
	}

107
	RememberVersions();
108 109
}

110 111
void
StateFile::Read()
112
try {
113
	bool success;
114

115
	FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
116

117
	TextFile file(config.path);
118

119
#ifdef ENABLE_DATABASE
120
	const SongLoader song_loader(partition.instance.GetDatabase(),
121
				     partition.instance.storage);
122
#else
123
	const SongLoader song_loader(nullptr, nullptr);
124
#endif
125

126
	const char *line;
127
	while ((line = file.ReadLine()) != nullptr) {
128 129
		success = read_sw_volume_state(line, partition.outputs) ||
			audio_output_state_read(line, partition.outputs) ||
130
			playlist_state_restore(config, line, file, song_loader,
131
					       partition.playlist,
132
					       partition.pc);
133 134 135 136
#ifdef ENABLE_DATABASE
		success = success || storage_state_restore(line, file, partition.instance);
#endif

137
		if (!success)
138 139 140
			FormatError(state_file_domain,
				    "Unrecognized line in state file: %s",
				    line);
141
	}
142

143
	RememberVersions();
144 145
} catch (...) {
	LogError(std::current_exception());
146
}
147

148
void
149
StateFile::CheckModified() noexcept
150
{
151
	if (!timer_event.IsActive() && IsModified())
152
		timer_event.Schedule(config.interval);
153 154
}

155
void
156
StateFile::OnTimeout() noexcept
157
{
158
	Write();
159
}