StateFile.cxx 3.55 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 41 42 43
constexpr std::chrono::steady_clock::duration StateFile::DEFAULT_INTERVAL;

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

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

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

70 71 72 73 74 75 76 77
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);
}

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

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

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

100
	RememberVersions();
101 102
}

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

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

110
	TextFile file(path);
111

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

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

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

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

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