StateFile.cxx 4.03 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 42 43 44
constexpr std::chrono::steady_clock::duration StateFile::DEFAULT_INTERVAL;

StateFile::StateFile(AllocatedPath &&_path,
		     std::chrono::steady_clock::duration _interval,
45
		     Partition &_partition, EventLoop &_loop)
46
	:path(std::move(_path)), path_utf8(path.ToUTF8()),
47
	 interval(_interval),
48
	 timer_event(_loop, BIND_THIS_METHOD(OnTimeout)),
49
	 partition(_partition)
50
{
51
}
52

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

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

78 79 80 81 82
inline void
StateFile::Write(BufferedOutputStream &os)
{
	save_sw_volume_state(os);
	audio_output_state_save(os, partition.outputs);
83 84 85 86 87

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

88 89 90
	playlist_state_save(os, partition.playlist, partition.pc);
}

91 92
inline void
StateFile::Write(OutputStream &os)
93 94 95
{
	BufferedOutputStream bos(os);
	Write(bos);
96
	bos.Flush();
97 98
}

99 100 101
void
StateFile::Write()
{
102 103
	FormatDebug(state_file_domain,
		    "Saving state file %s", path_utf8.c_str());
104

105 106
	try {
		FileOutputStream fos(path);
107
		Write(fos);
108
		fos.Commit();
109 110
	} catch (const std::exception &e) {
		LogError(e);
111 112
	}

113
	RememberVersions();
114 115
}

116 117
void
StateFile::Read()
118
try {
119
	bool success;
120

121
	FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
122

123
	TextFile file(path);
124

125
#ifdef ENABLE_DATABASE
126 127
	const SongLoader song_loader(partition.instance.database,
				     partition.instance.storage);
128
#else
129
	const SongLoader song_loader(nullptr, nullptr);
130
#endif
131

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

143
		if (!success)
144 145 146
			FormatError(state_file_domain,
				    "Unrecognized line in state file: %s",
				    line);
147
	}
148

149
	RememberVersions();
150 151
} catch (const std::exception &e) {
	LogError(e);
152
}
153

154 155
void
StateFile::CheckModified()
156
{
157 158
	if (!timer_event.IsActive() && IsModified())
		timer_event.Schedule(interval);
159 160
}

161
void
162
StateFile::OnTimeout()
163
{
164
	Write();
165
}