StateFile.cxx 3.53 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/OutputState.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 "Partition.hxx"
28
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "mixer/Volume.hxx"
30
#include "SongLoader.hxx"
31 32
#include "util/Domain.hxx"
#include "Log.hxx"
33

34 35
#include <exception>

36
#include <string.h>
37

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

40
StateFile::StateFile(AllocatedPath &&_path, unsigned _interval,
41 42 43
		     Partition &_partition, EventLoop &_loop)
	:TimeoutMonitor(_loop),
	 path(std::move(_path)), path_utf8(path.ToUTF8()),
44
	 interval(_interval),
45
	 partition(_partition),
46 47
	 prev_volume_version(0), prev_output_version(0),
	 prev_playlist_version(0)
48
{
49
}
50

51 52 53 54 55
void
StateFile::RememberVersions()
{
	prev_volume_version = sw_volume_state_get_hash();
	prev_output_version = audio_output_state_get_version();
56 57
	prev_playlist_version = playlist_state_get_hash(partition.playlist,
							partition.pc);
58 59 60 61 62 63 64
}

bool
StateFile::IsModified() const
{
	return prev_volume_version != sw_volume_state_get_hash() ||
		prev_output_version != audio_output_state_get_version() ||
65 66
		prev_playlist_version != playlist_state_get_hash(partition.playlist,
								 partition.pc);
67 68
}

69 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);
	playlist_state_save(os, partition.playlist, partition.pc);
}

77 78
inline void
StateFile::Write(OutputStream &os)
79 80 81
{
	BufferedOutputStream bos(os);
	Write(bos);
82
	bos.Flush();
83 84
}

85 86 87
void
StateFile::Write()
{
88 89
	FormatDebug(state_file_domain,
		    "Saving state file %s", path_utf8.c_str());
90

91 92
	try {
		FileOutputStream fos(path);
93
		Write(fos);
94
		fos.Commit();
95 96
	} catch (const std::exception &e) {
		LogError(e);
97 98
	}

99
	RememberVersions();
100 101
}

102 103
void
StateFile::Read()
104
try {
105
	bool success;
106

107
	FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
108

109
	TextFile file(path);
110

111
#ifdef ENABLE_DATABASE
112 113
	const SongLoader song_loader(partition.instance.database,
				     partition.instance.storage);
114
#else
115
	const SongLoader song_loader(nullptr, nullptr);
116
#endif
117

118
	const char *line;
119
	while ((line = file.ReadLine()) != nullptr) {
120 121
		success = read_sw_volume_state(line, partition.outputs) ||
			audio_output_state_read(line, partition.outputs) ||
122 123
			playlist_state_restore(line, file, song_loader,
					       partition.playlist,
124
					       partition.pc);
125
		if (!success)
126 127 128
			FormatError(state_file_domain,
				    "Unrecognized line in state file: %s",
				    line);
129
	}
130

131
	RememberVersions();
132 133
} catch (const std::exception &e) {
	LogError(e);
134
}
135

136 137
void
StateFile::CheckModified()
138
{
139
	if (!IsActive() && IsModified())
140
		ScheduleSeconds(interval);
141 142
}

143
void
144
StateFile::OnTimeout()
145
{
146
	Write();
147
}