StateFile.cxx 3.93 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include <string.h>
38

39
static constexpr Domain state_file_domain("state_file");
40

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

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

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

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

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

84 85 86
	playlist_state_save(os, partition.playlist, partition.pc);
}

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

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

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

109
	RememberVersions();
110 111
}

112 113
void
StateFile::Read()
114
try {
115
	bool success;
116

117
	FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
118

119
	TextFile file(config.path);
120

121
#ifdef ENABLE_DATABASE
122 123
	const SongLoader song_loader(partition.instance.database,
				     partition.instance.storage);
124
#else
125
	const SongLoader song_loader(nullptr, nullptr);
126
#endif
127

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

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

145
	RememberVersions();
146 147
} catch (...) {
	LogError(std::current_exception());
148
}
149

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

157
void
158
StateFile::OnTimeout()
159
{
160
	Write();
161
}