StateFile.cxx 3.62 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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
#include "fs/FileSystem.hxx"
32 33
#include "util/Domain.hxx"
#include "Log.hxx"
34

35
#include <string.h>
36

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

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

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

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

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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);
}

inline bool
StateFile::Write(OutputStream &os, Error &error)
{
	BufferedOutputStream bos(os);
	Write(bos);
	return bos.Flush(error);
}

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

90 91 92 93
	Error error;
	FileOutputStream fos(path, error);
	if (!fos.IsDefined() || !Write(fos, error) || !fos.Commit(error)) {
		LogError(error);
94 95 96
		return;
	}

97
	RememberVersions();
98 99
}

100 101
void
StateFile::Read()
102
{
103
	bool success;
104

105
	FormatDebug(state_file_domain, "Loading state file %s", path_utf8.c_str());
106

107 108
	Error error;
	TextFile file(path, error);
109
	if (file.HasFailed()) {
110
		LogError(error);
111
		return;
112
	}
113

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

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

134
	RememberVersions();
135
}
136

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

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